Diff
Not logged in

Differences From Artifact [4b2effb6b7]:

To Artifact [9a72d128cc]:


1
2
3
4
5
6
7
8
9
10
11
12

 /* ==================================================================

    FILE: "/home/joze/src/tclreadline/tclreadline.c"
    LAST MODIFICATION: "Sat Aug 28 23:56:10 1999 (joze)"
    (C) 1998, 1999 by Johannes Zellner, <johannes@zellner.org>
    $Id$
    ---

    tclreadline -- gnu readline for tcl
    Copyright (C) 1999  Johannes Zellner





|







1
2
3
4
5
6
7
8
9
10
11
12

 /* ==================================================================

    FILE: "/home/joze/src/tclreadline/tclreadline.c"
    LAST MODIFICATION: "Sun Aug 29 15:04:07 1999 (joze)"
    (C) 1998, 1999 by Johannes Zellner, <johannes@zellner.org>
    $Id$
    ---

    tclreadline -- gnu readline for tcl
    Copyright (C) 1999  Johannes Zellner

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

    <johannes@zellner.org>, http://www.zellner.org/tclreadline/

    ================================================================== */  


#include <tcl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define READLINE_LIBRARY
#include <readline.h>
#include <history.h>

#include <tclreadline.h>

#define MALLOC(size) Tcl_Alloc((int) size)
#define FREE(ptr) if (ptr) { Tcl_Free((char*) ptr); ptr = 0; }

enum {
    _CMD_SET     = (1 << 0),
    _CMD_GET     = (1 << 1)







<








|







24
25
26
27
28
29
30

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

    <johannes@zellner.org>, http://www.zellner.org/tclreadline/

    ================================================================== */  


#include <tcl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define READLINE_LIBRARY
#include <readline.h>
#include <history.h>

#include "tclreadline.h"

#define MALLOC(size) Tcl_Alloc((int) size)
#define FREE(ptr) if (ptr) { Tcl_Free((char*) ptr); ptr = 0; }

enum {
    _CMD_SET     = (1 << 0),
    _CMD_GET     = (1 << 1)
66
67
68
69
70
71
72


73
74
75
76
77
78
79
char* TclReadlineQuote(char* text, char* quotechars);
int TclReadlineCmd(ClientData clientData, Tcl_Interp* interp,
    int argc, char** argv);
void TclReadlineDataAvailableHandler(ClientData clientData, int mask);
void TclReadlineLineCompleteHandler(char* ptr);
int Tclreadline_SafeInit(Tcl_Interp* interp);
int Tclreadline_Init(Tcl_Interp* interp);


int TclReadlineInitialize(Tcl_Interp* interp, char* historyfile);
int blank_line(char* str);
char** TclReadlineCompletion(char* text, int start, int end);
char* TclReadline0generator(char* text, int state);
char* TclReadlineKnownCommands(char* text, int state, int mode);
int TclReadlineParse(char** args, int maxargs, char* buf);








>
>







65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
char* TclReadlineQuote(char* text, char* quotechars);
int TclReadlineCmd(ClientData clientData, Tcl_Interp* interp,
    int argc, char** argv);
void TclReadlineDataAvailableHandler(ClientData clientData, int mask);
void TclReadlineLineCompleteHandler(char* ptr);
int Tclreadline_SafeInit(Tcl_Interp* interp);
int Tclreadline_Init(Tcl_Interp* interp);
char *TclReadlineFilenameQuotingFunction(
    char *text, int match_type, char* quote_ptr);
int TclReadlineInitialize(Tcl_Interp* interp, char* historyfile);
int blank_line(char* str);
char** TclReadlineCompletion(char* text, int start, int end);
char* TclReadline0generator(char* text, int state);
char* TclReadlineKnownCommands(char* text, int state, int mode);
int TclReadlineParse(char** args, int maxargs, char* buf);

171
172
173
174
175
176
177







178
179
180
181
182
183
184
185
186
187



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
        tclrl_state = TCL_OK;
        rl_callback_handler_install(argc == 3 ? argv[2] : "%",
                TclReadlineLineCompleteHandler);

        Tcl_CreateFileHandler(0, TCL_READABLE,
                TclReadlineDataAvailableHandler, (ClientData) NULL);








        while (LINE_PENDING == tclrl_line_complete
            && TCL_OK == tclrl_state && !rl_done) {
            Tcl_DoOneEvent(0);
            /*
            rl_inhibit_completion = 0;
            */
        }

        Tcl_DeleteFileHandler(0);




	if ((LINE_EOF == tclrl_line_complete) && tclrl_eof_string) {
	    Tcl_Eval(interp, tclrl_eof_string);
            return tclrl_state;
	}

        status = history_expand(tclrl_line, &expansion);
        if (status >= 1) {
#if 0
            Tcl_Channel channel = Tcl_MakeFileChannel(stdout, TCL_WRITABLE);
            /* Tcl_RegisterChannel(interp, channel); */
            (void) Tcl_WriteChars(channel, expansion, -1);
            Tcl_Flush(channel);
            Tcl_Close(interp, channel);
#else
            printf("%s\n", expansion);
#endif
        }
        else if (status == -1) {
            Tcl_AppendResult
                (interp, "error in history expansion\n", (char*) NULL);
            return TCL_ERROR;
        }
        /**
         * TODO: status == 2 ...
         */
        
        if (expansion && *expansion)
            add_history(expansion);







>
>
>
>
>
>
>







<


>
>
>




















|







172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
        tclrl_state = TCL_OK;
        rl_callback_handler_install(argc == 3 ? argv[2] : "%",
                TclReadlineLineCompleteHandler);

        Tcl_CreateFileHandler(0, TCL_READABLE,
                TclReadlineDataAvailableHandler, (ClientData) NULL);

        /**
         * Main Loop.
         * XXX each modification of the global variables
         *     which terminates the main loop must call
         *     rl_callback_handler_remove() to leave
         *     readline in a defined state.          XXX
         */
        while (LINE_PENDING == tclrl_line_complete
            && TCL_OK == tclrl_state && !rl_done) {
            Tcl_DoOneEvent(0);
            /*
            rl_inhibit_completion = 0;
            */
        }

        Tcl_DeleteFileHandler(0);

        if (TCL_OK != tclrl_state)
            return tclrl_state; /* !! */

	if ((LINE_EOF == tclrl_line_complete) && tclrl_eof_string) {
	    Tcl_Eval(interp, tclrl_eof_string);
            return tclrl_state;
	}

        status = history_expand(tclrl_line, &expansion);
        if (status >= 1) {
#if 0
            Tcl_Channel channel = Tcl_MakeFileChannel(stdout, TCL_WRITABLE);
            /* Tcl_RegisterChannel(interp, channel); */
            (void) Tcl_WriteChars(channel, expansion, -1);
            Tcl_Flush(channel);
            Tcl_Close(interp, channel);
#else
            printf("%s\n", expansion);
#endif
        }
        else if (status == -1) {
            Tcl_AppendResult
                (interp, "error in history expansion\n", (char*) NULL);
            return tclrl_state;
        }
        /**
         * TODO: status == 2 ...
         */
        
        if (expansion && *expansion)
            add_history(expansion);
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348










349

350






351
352
































353
354
355
356
357
358
359
360
361
362

363
364
365
366










367
368
369
370
371
372
373
        tclrl_line_complete = LINE_COMPLETE;
        rl_callback_handler_remove();
        tclrl_line = ptr;
    }
#else
    if (ptr && *ptr) {
        tclrl_line_complete = 1;
        rl_callback_handler_remove ();
        tclrl_line = ptr;
    }
#endif
}

int
Tclreadline_SafeInit(Tcl_Interp *interp)
{
    return Tclreadline_Init(interp);
}

int
Tclreadline_Init(Tcl_Interp *interp)
{
    int status;
    Tcl_CreateCommand(interp, "::tclreadline::readline", TclReadlineCmd,
	    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
    tclrl_interp = interp;
    status = Tcl_LinkVar
        (interp, "::tclreadline::historyLength",
         (char*) &tclrl_history_length, TCL_LINK_INT);










    if (TCL_OK != status)

        return status;






    return Tcl_PkgProvide(interp, "tclreadline", TCLREADLINE_VERSION);
}

































int
TclReadlineInitialize(Tcl_Interp* interp, char* historyfile)
{
    rl_readline_name = "tclreadline";
    /* rl_special_prefixes = "${\""; */
    rl_special_prefixes = "${";
    /**
     * default is " \t\n\"\\'`@$><=;|&{("
     * removed "{("

     * added "[]}"
     */
    rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&[]}";
    /* rl_completer_quote_characters = "\""; */











    using_history();
    if (!tclrl_eof_string)
        tclrl_eof_string = strdup("puts {}; exit");

    /*
     * try to read historyfile in home







|


















<
|
|
>
>
>
>
>
>
>
>
>
>
|
>

>
>
>
>
>
>
|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





<
|


|
>
|

|
|
>
>
>
>
>
>
>
>
>
>







330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355

356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415

416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
        tclrl_line_complete = LINE_COMPLETE;
        rl_callback_handler_remove();
        tclrl_line = ptr;
    }
#else
    if (ptr && *ptr) {
        tclrl_line_complete = 1;
        rl_callback_handler_remove();
        tclrl_line = ptr;
    }
#endif
}

int
Tclreadline_SafeInit(Tcl_Interp *interp)
{
    return Tclreadline_Init(interp);
}

int
Tclreadline_Init(Tcl_Interp *interp)
{
    int status;
    Tcl_CreateCommand(interp, "::tclreadline::readline", TclReadlineCmd,
	    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
    tclrl_interp = interp;

    if (TCL_OK != (status = Tcl_LinkVar(interp, "::tclreadline::historyLength",
                (char*) &tclrl_history_length, TCL_LINK_INT)))
        return status;
    if (TCL_OK != (status = Tcl_LinkVar(interp, "::tclreadline::library",
         (char*) &TCLRL_LIBRARY, TCL_LINK_STRING | TCL_LINK_READ_ONLY)))
        return status;
    if (TCL_OK != (status = Tcl_LinkVar(interp, "::tclreadline::version",
         (char*) &TCLRL_VERSION, TCL_LINK_STRING | TCL_LINK_READ_ONLY)))
        return status;
    if (TCL_OK != (status = Tcl_LinkVar(interp, "::tclreadline::patchLevel",
         (char*) &TCLRL_PATCHLEVEL, TCL_LINK_STRING | TCL_LINK_READ_ONLY)))
        return status;
    if (TCL_OK != (status = Tcl_LinkVar(interp, "tclreadline_library",
         (char*) &TCLRL_LIBRARY, TCL_LINK_STRING | TCL_LINK_READ_ONLY)))
        return status;
    if (TCL_OK != (status = Tcl_LinkVar(interp, "tclreadline_version",
         (char*) &TCLRL_VERSION, TCL_LINK_STRING | TCL_LINK_READ_ONLY)))
        return status;
    if (TCL_OK != (status = Tcl_LinkVar(interp, "tclreadline_patchLevel",
         (char*) &TCLRL_PATCHLEVEL, TCL_LINK_STRING | TCL_LINK_READ_ONLY)))
        return status;
    return Tcl_PkgProvide(interp, "tclreadline", TCLRL_VERSION);
}

#if 0
char *
TclReadlineFilenameQuotingFunction
(char *filename, int match_type, char* quote_ptr)
{
    char *res = (char*) malloc(sizeof(char) * (strlen(filename) + 2));
    int i = 0;
    fprintf (stderr, "(TclReadlineFilenameQuotingFunction) \n");
    if (quote_ptr && *quote_ptr) {
        *res = *quote_ptr;                     /* leading quote */
        i++;
    }
    strcpy (res + i, filename);              /* name          */
#if 0
    fprintf (stderr, "(Tclreadline_Init) filename=|%s|\n", filename);
    fprintf (stderr, "(Tclreadline_Init) *quote_ptr=|%c|\n", *quote_ptr);
#endif
    if (quote_ptr && '{' == *quote_ptr) {
        *quote_ptr = '}';
    }
    return res;

#if 0
    switch (match_type) {
        case SINGLE_MATCH:
            break;
        default:
    }
#endif
}
#endif

int
TclReadlineInitialize(Tcl_Interp* interp, char* historyfile)
{
    rl_readline_name = "tclreadline";

    rl_special_prefixes = "${\"";
    /**
     * default is " \t\n\"\\'`@$><=;|&{("
     * removed "(" <-- arrays
     * removed "{" <-- `${' variables 
     * added "[]"
     */
    rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&[]";
    rl_completer_quote_characters = "\"";
    /*
    rl_filename_quote_characters
    = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    rl_filename_quoting_function
        = (CPFunction*) TclReadlineFilenameQuotingFunction;
    */
    /*
    rl_filename_quoting_desired = 1;
    */

    using_history();
    if (!tclrl_eof_string)
        tclrl_eof_string = strdup("puts {}; exit");

    /*
     * try to read historyfile in home
466
467
468
469
470
471
472
473
474
475
476
477


478
479
480
481
482
483
484
        Tcl_ResetResult(tclrl_interp); /* clear result space */
        tclrl_state = Tcl_VarEval(tclrl_interp, tclrl_custom_completer,
            " \"", quoted_text, "\" ", start_s, " ", end_s,
            " \"", quoted_rl_line_buffer, "\"", (char*) NULL);
        FREE(quoted_text);
        FREE(quoted_rl_line_buffer);
        if (TCL_OK != tclrl_state) {
            fprintf(stderr, "%s\n", Tcl_GetStringResult(tclrl_interp));
#if 0
            Tcl_AppendResult (tclrl_interp, "`", tclrl_custom_completer,
                " {", text, "} ", start_s, " ", end_s,
                " {", rl_line_buffer, "}' failed.", (char*) NULL);


#endif
            return matches;
        }
        obj = Tcl_GetObjResult(tclrl_interp);
        Tcl_ListObjGetElements(tclrl_interp, obj, &objc, &objv);
        /* fprintf (stderr, "(TclReadlineCompletion) objc = %d\n", objc); */
        if (objc) {







|
<
|
|
|
>
>







534
535
536
537
538
539
540
541

542
543
544
545
546
547
548
549
550
551
552
553
        Tcl_ResetResult(tclrl_interp); /* clear result space */
        tclrl_state = Tcl_VarEval(tclrl_interp, tclrl_custom_completer,
            " \"", quoted_text, "\" ", start_s, " ", end_s,
            " \"", quoted_rl_line_buffer, "\"", (char*) NULL);
        FREE(quoted_text);
        FREE(quoted_rl_line_buffer);
        if (TCL_OK != tclrl_state) {
            rl_callback_handler_remove();

            Tcl_AppendResult (tclrl_interp, " `", tclrl_custom_completer,
                " \"", quoted_text, "\" ", start_s, " ", end_s,
                " \"", quoted_rl_line_buffer, "\"' failed.", (char*) NULL);
#if 0
            fprintf(stderr, "\n|%s|\n", Tcl_GetStringResult(tclrl_interp));
#endif
            return matches;
        }
        obj = Tcl_GetObjResult(tclrl_interp);
        Tcl_ListObjGetElements(tclrl_interp, obj, &objc, &objv);
        /* fprintf (stderr, "(TclReadlineCompletion) objc = %d\n", objc); */
        if (objc) {