Diff
Not logged in

Differences From Artifact [c635fda5be]:

To Artifact [4a41886173]:


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

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

    FILE: "/krispc6/home/joze/src/tclreadline/tclreadline.c"
    LAST MODIFICATION: "Sat May  8 16:20:59 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: "/diska/home/joze/src/tclreadline/tclreadline.c"
    LAST MODIFICATION: "Fri Aug 20 18:14:57 1999 (joze)"
    (C) 1998, 1999 by Johannes Zellner, <johannes@zellner.org>
    $Id$
    ---

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

92
93
94
95
96
97
98





99
100
101
102
103
104
105
106
107
char** TclReadlineCompletion	(char *text, int start, int end);
char*  TclReadline0generator	(char *text, int state);
char*  TclReadline1generator	(char *text, int state);
char*  TclReadlineKnownCommands	(char *text, int state, int mode);
int    TclReadlineEventHook	(void);
int    TclReadlineParse	(char **args, int maxargs, char *buf);







static int line_complete = 0;
static char *line = (char *) NULL;


/*
 * Script to set the tclreadline library path in the
 * variable global "tclreadline_library"
 */







>
>
>
>
>

|







92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
char** TclReadlineCompletion	(char *text, int start, int end);
char*  TclReadline0generator	(char *text, int state);
char*  TclReadline1generator	(char *text, int state);
char*  TclReadlineKnownCommands	(char *text, int state, int mode);
int    TclReadlineEventHook	(void);
int    TclReadlineParse	(char **args, int maxargs, char *buf);

enum { 
    LINE_PENDING,
    LINE_CONTROL_D,
    LINE_COMPLETE
};

static int line_complete = LINE_PENDING;
static char *line = (char *) NULL;


/*
 * Script to set the tclreadline library path in the
 * variable global "tclreadline_library"
 */
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175


    if (c == 'r'  && strncmp (argv[1], "read", length) == 0) {
        
        char *expansion = (char *) NULL;
        int status;
        
        line_complete = 0;
        rl_callback_handler_install (argc == 3 ? argv[2] : "%",
                TclReadlineLineCompleteHandler);

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

        while (!line_complete) {
            Tcl_DoOneEvent (0);
        }

        Tcl_DeleteFileHandler (0);

	if (line_complete < 0) {
	    Tcl_Eval(interp, "exit");
	}

        status = history_expand (line, &expansion);
        if (status == 1)
            printf ("%s\n", expansion);
        else if (status == -1)
            Tcl_AppendResult (interp, "error in history expansion\n",
                    (char *) NULL);
        
        if (expansion && *expansion)
            add_history (expansion);

        Tcl_AppendResult (interp, expansion, (char *) NULL);

        FREE (line);
        FREE (expansion);
        return (TCL_OK);

    }
    else if (c == 'i'  && strncmp (argv[1], "initialize", length) == 0) {
        if (argc != 3)
            goto BAD_COMMAND;
        else
            Tcl_AppendResult (interp, TclReadlineInitialize (argv[2]),
                    (char *) NULL);







|






|





|
|
















|
<







134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172

173
174
175
176
177
178
179


    if (c == 'r'  && strncmp (argv[1], "read", length) == 0) {
        
        char *expansion = (char *) NULL;
        int status;
        
        line_complete = LINE_PENDING;
        rl_callback_handler_install (argc == 3 ? argv[2] : "%",
                TclReadlineLineCompleteHandler);

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

        while (LINE_PENDING == line_complete) {
            Tcl_DoOneEvent (0);
        }

        Tcl_DeleteFileHandler (0);

	if (LINE_CONTROL_D == line_complete) {
	    Tcl_Eval (interp, "puts {}; exit");
	}

        status = history_expand (line, &expansion);
        if (status == 1)
            printf ("%s\n", expansion);
        else if (status == -1)
            Tcl_AppendResult (interp, "error in history expansion\n",
                    (char *) NULL);
        
        if (expansion && *expansion)
            add_history (expansion);

        Tcl_AppendResult (interp, expansion, (char *) NULL);

        FREE (line);
        FREE (expansion);
        return TCL_OK;

    }
    else if (c == 'i'  && strncmp (argv[1], "initialize", length) == 0) {
        if (argc != 3)
            goto BAD_COMMAND;
        else
            Tcl_AppendResult (interp, TclReadlineInitialize (argv[2]),
                    (char *) NULL);
202
203
204
205
206
207
208
209
210
211
212
213
214



215
216


217
218
219
220





221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267

    return TCL_OK;

BAD_COMMAND:
    Tcl_AppendResult (interp,
            "wrong # args: should be \"readline option ?arg ...?\"",
	    (char *) NULL);
    return (TCL_ERROR);

}

void TclReadlineDataAvailableHandler (ClientData clientData, int mask)
{



    if (mask & TCL_READABLE)
        rl_callback_read_char ();


}

void TclReadlineLineCompleteHandler (char *ptr)
{





    if (!ptr) {
        line_complete = -1;
        rl_callback_handler_remove ();
    } else if (*ptr) {
        line_complete = 1;
        rl_callback_handler_remove ();
        line = ptr;
    }
}


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

int Tclreadline_Init (Tcl_Interp *interp)
{
    Tcl_CreateCommand (interp, "::tclreadline::readline", TclReadlineCmd,
	    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
    return (Tcl_PkgProvide (interp, "tclreadline", TCLREADLINE_VERSION));
}

char *TclReadlineInitialize (char *historyfile)
{

    rl_readline_name = "tclreadline";
    using_history ();

    /*
     * try to read historyfile in home
     * directory. If this failes, this
     * is *not* an error.
     */
    rl_attempted_completion_function = (CPPFunction *) TclReadlineCompletion;
    if (read_history (historyfile))
        return ("unable to read history file");
    
    else
        return ("");
}

char **TclReadlineCompletion (char *text, int start, int end)
{
    char **matches = (char **) NULL;
    static char local_line[BUFSIZ];
       







|





>
>
>
|

>
>




>
>
>
>
>
|
|


|








|






|















|


|







206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281

    return TCL_OK;

BAD_COMMAND:
    Tcl_AppendResult (interp,
            "wrong # args: should be \"readline option ?arg ...?\"",
	    (char *) NULL);
    return TCL_ERROR;

}

void TclReadlineDataAvailableHandler (ClientData clientData, int mask)
{
#if 1
    fprintf (stderr, "(TclReadlineDataAvailableHandler) mask = %d\n",  mask);
#endif
    if (mask & TCL_READABLE) {
        rl_callback_read_char ();
        rl_redisplay ();
    }
}

void TclReadlineLineCompleteHandler (char *ptr)
{
#if 0
    fprintf (stderr, "(TclReadlineLineCompleteHandler)  ptr = %p\n",  ptr);
    if (ptr)
    fprintf (stderr, "(TclReadlineLineCompleteHandler) *ptr = %p\n", *ptr);
#endif
    if (!ptr) { /* <c-d> */
        line_complete = LINE_CONTROL_D;
        rl_callback_handler_remove ();
    } else if (*ptr) {
        line_complete = LINE_COMPLETE;
        rl_callback_handler_remove ();
        line = ptr;
    }
}


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

int Tclreadline_Init (Tcl_Interp *interp)
{
    Tcl_CreateCommand (interp, "::tclreadline::readline", TclReadlineCmd,
	    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
    return Tcl_PkgProvide (interp, "tclreadline", TCLREADLINE_VERSION);
}

char *TclReadlineInitialize (char *historyfile)
{

    rl_readline_name = "tclreadline";
    using_history ();

    /*
     * try to read historyfile in home
     * directory. If this failes, this
     * is *not* an error.
     */
    rl_attempted_completion_function = (CPPFunction *) TclReadlineCompletion;
    if (read_history (historyfile))
        return "unable to read history file";
    
    else
        return "";
}

char **TclReadlineCompletion (char *text, int start, int end)
{
    char **matches = (char **) NULL;
    static char local_line[BUFSIZ];
       
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
    */

    if (start == 0 || !strlen (local_line))
        matches = completion_matches (text, TclReadline0generator);
    else
        matches = completion_matches (text, TclReadline1generator);
    
    return (matches);
}

char *TclReadline0generator (char *text, int state)
{
    return (TclReadlineKnownCommands (text, state, _CMD_GET));
}

char *TclReadline1generator (char *text, int state)
{
    return (TclReadlineKnownCommands (text, state, _CMD_SUB_GET));
}

char *TclReadlineKnownCommands (char *text, int state, int mode)
{
    static int len;
    static cmds_t *cmds = (cmds_t *) NULL, *new;
    char *tmp, *args[256];







|




|




|







290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
    */

    if (start == 0 || !strlen (local_line))
        matches = completion_matches (text, TclReadline0generator);
    else
        matches = completion_matches (text, TclReadline1generator);
    
    return matches;
}

char *TclReadline0generator (char *text, int state)
{
    return TclReadlineKnownCommands (text, state, _CMD_GET);
}

char *TclReadline1generator (char *text, int state)
{
    return TclReadlineKnownCommands (text, state, _CMD_SUB_GET);
}

char *TclReadlineKnownCommands (char *text, int state, int mode)
{
    static int len;
    static cmds_t *cmds = (cmds_t *) NULL, *new;
    char *tmp, *args[256];
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
                new = cmds;
                len = strlen (text);
            }

            while (new && (name = new->cmd)) {
                new = new->next;
                if (!strncmp (name[0], text, len))
                    return (strdup (name[0]));
            }

            return (char *) NULL;
            break;

        case _CMD_SUB_GET:
            







|







352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
                new = cmds;
                len = strlen (text);
            }

            while (new && (name = new->cmd)) {
                new = new->next;
                if (!strncmp (name[0], text, len))
                    return strdup (name[0]);
            }

            return (char *) NULL;
            break;

        case _CMD_SUB_GET:
            
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
                
                if (!new)
                    return (char *) NULL;

                for (i = 0; new->cmd[i]; i++) /* EMPTY */;

                if (sub < i && !strncmp (new->cmd[sub], text, len))
                    return (strdup (new->cmd[sub]));
                else
                   return (char *) NULL;

            }
            else
                return (char *) NULL;








|







390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
                
                if (!new)
                    return (char *) NULL;

                for (i = 0; new->cmd[i]; i++) /* EMPTY */;

                if (sub < i && !strncmp (new->cmd[sub], text, len))
                    return strdup (new->cmd[sub]);
                else
                   return (char *) NULL;

            }
            else
                return (char *) NULL;

401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
    /* NOTREACHED */

}

int TclReadlineEventHook (void)
{
    Tcl_DoOneEvent (TCL_ALL_EVENTS | TCL_DONT_WAIT);
    return (TCL_OK);
}

int TclReadlineParse (char **args, int maxargs, char *buf)
{
    int nr = 0;

    while (*buf != '\0' && nr < maxargs) {







|







415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
    /* NOTREACHED */

}

int TclReadlineEventHook (void)
{
    Tcl_DoOneEvent (TCL_ALL_EVENTS | TCL_DONT_WAIT);
    return TCL_OK;
}

int TclReadlineParse (char **args, int maxargs, char *buf)
{
    int nr = 0;

    while (*buf != '\0' && nr < maxargs) {