Version 0 of readline

Updated 2006-08-07 09:43:37

The GNU Readline library [L1 ] provides a set of functions for use by applications that allow users to edit command lines as they are typed in. Most of the work in the readline library is done by the readline function.

The C-code below will provide a tcl procedure readline which can be used as:

 readline prompt

While editing the line all the standard readline functionality is available. A build of the extension for Win32 can be downloaded from [L2 ]. -- [MJ]


''tclreadline.h'

 #ifndef _TCLREADLINE_TCL_H
 #define _TCLREADLINE_TCL_H

 static int ReadlineCmd( ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj * CONST objv[]) ;


 #endif _TCLREADLINE_TCL_H

tclreadline.c

 #include <tcl.h>
 #include <string.h>
 #include <readline/readline.h>


 #include "tclreadline.h"

 __declspec(dllexport)  int Tclreadline_Init(Tcl_Interp *interp) {
     if (Tcl_InitStubs(interp, TCL_VERSION, 0) == 0L) {
       return TCL_ERROR;
     }

     /* Disable file completion as Tcl doesn't understand the windows file format with \ */
     rl_bind_key ('\t', rl_insert);

     Tcl_CreateObjCommand(interp, "readline", ReadlineCmd, NULL, NULL);
     Tcl_PkgProvide(interp, "tclreadline", PKG_VERSION);
     return TCL_OK;
 }

 static int ReadlineCmd( ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj * CONST objv[]) {
   char * line_read;

   if(objc!=2) {
     Tcl_WrongNumArgs(interp,1,objv,"prompt");
     return TCL_ERROR;
   }

   /* Get a line from the user. */
   line_read = readline (Tcl_GetString(objv[1]));

   /* If the line has any text in it,
      save it on the history and return. */

   if (line_read && *line_read) {
     add_history (line_read);
     Tcl_SetObjResult(interp, Tcl_NewStringObj(line_read, -1));
     free(line_read);
     return TCL_OK;
   } else {
     Tcl_SetObjResult(interp, Tcl_NewStringObj("EOF on readline", -1));
     return TCL_ERROR;
   }
 }