1
2
3
4
5
6
7
8
9
10
11
12
|
1
2
3
4
5
6
7
8
9
10
11
12
|
-
-
+
+
|
/* ==================================================================
FILE: "/home/joze/src/tclreadline/tclreadline.c"
LAST MODIFICATION: "Mon Sep 13 02:21:35 1999 (joze)"
FILE: "/diska/home/joze/src/tclreadline/tclreadline.c"
LAST MODIFICATION: "Mon Sep 13 18:04:01 1999 (joze)"
(C) 1998, 1999 by Johannes Zellner, <johannes@zellner.org>
$Id$
---
tclreadline -- gnu readline for tcl
Copyright (C) 1999 Johannes Zellner
|
︙ | | |
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
+
+
+
+
+
+
+
+
+
+
+
+
+
|
You should have received a copy of the GNU General Public License
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/
================================================================== */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <tcl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define READLINE_LIBRARY
#include <readline.h>
#include <history.h>
/**
* this prototype is missing
* in readline.h
*/
void rl_extend_line_buffer(int len);
#ifdef EXECUTING_MACRO_HACK
/**
* this prototype is private in readline's file `macro.c'.
* We need it here to decide, if we should read more
* characters from a macro. Dirty, but it should work.
*/
extern char* _rl_executing_macro;
#endif
#include "tclreadline.h"
#define MALLOC(size) Tcl_Alloc((int) size)
#define FREE(ptr) if (ptr) { Tcl_Free((char*) ptr); ptr = 0; }
enum {
|
︙ | | |
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
-
-
-
-
|
*/
char* stripleft(char* in);
char* stripright(char* in);
char* stripwhite(char* in);
char* TclReadlineQuote(char* text, char* quotechars);
int TclReadlineCmd(ClientData clientData, Tcl_Interp* interp,
int argc, char** argv);
int TclReadlineEventHook(void);
void TclReadlineReadHandler(ClientData clientData, int mask);
void TclReadlineWriteHandler(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);
|
︙ | | |
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
226
227
|
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
|
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
|
length = strlen(argv[1]);
if (c == 'r' && strncmp(argv[1], "read", length) == 0) {
char* expansion = (char*) NULL;
int status;
#if 1
tclrl_line_complete = LINE_PENDING;
tclrl_state = TCL_OK;
rl_callback_handler_install(argc == 3 ? argv[2] : "%",
TclReadlineLineCompleteHandler);
Tcl_CreateFileHandler(0, TCL_READABLE,
TclReadlineReadHandler, (ClientData) NULL);
/*
Tcl_CreateFileHandler(1, TCL_WRITABLE,
TclReadlineWriteHandler, (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(TCL_ALL_EVENTS);
/*
Tcl_DoOneEvent(0);
fprintf (stderr, "(TclReadlineCmd) \n");
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;
}
#else
rl_event_hook = TclReadlineEventHook;
tclrl_line = readline(argc == 3 ? argv[2] : "%");
#endif
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);
|
︙ | | |
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
374
375
376
377
378
|
319
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
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
+
-
-
-
-
+
+
+
+
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
|
Tcl_AppendResult(interp,
"wrong # args: should be \"readline option ?arg ...?\"",
(char*) NULL);
return TCL_ERROR;
}
int TclReadlineEventHook(void)
{
Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
/*
TCL_DONT_WAIT
TCL_WINDOW_EVENTS
TCL_FILE_EVENTS
TCL_TIMER_EVENTS
TCL_IDLE_EVENTS
TCL_ALL_EVENTS
*/
return TCL_OK;
}
void
TclReadlineReadHandler(ClientData clientData, int mask)
{
#if 0
fprintf(stderr, "(TclReadlineReadHandler) mask = %d\n", mask);
#endif
if (mask & TCL_READABLE) {
rl_callback_read_char();
#ifdef EXECUTING_MACRO_HACK
/*
/**
fprintf(stderr, "(TclReadlineReadHandler) mask = readable\n");
rl_event_hook = TclReadlineEventHook;
while (!rl_done) {
*/
* check, if we're inside a macro and
* if so, read all macro characters.
*/
while (_rl_executing_macro) {
rl_callback_read_char();
/*
}
fflush(stdin);
*/
}
}
#endif
void
TclReadlineWriteHandler(ClientData clientData, int mask)
{
if (mask & TCL_WRITABLE) {
/*
fprintf(stderr, "(TclReadlineReadHandler) mask = writable\n");
*/
fflush(stdout);
rl_redisplay();
}
}
void
TclReadlineLineCompleteHandler(char* ptr)
{
#if 1
|
︙ | | |
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
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
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
+
+
|
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 = "${\"[";
rl_special_prefixes = "$";
/**
* default is " \t\n\"\\'`@$><=;|&{("
* removed "(" <-- arrays
* removed "{" <-- `${' variables
* removed "<" <-- completion lists with < ... >
* added "[]"
* added "}"
*/
/* 11.Sep rl_basic_word_break_characters = " \t\n\"\\@$}=;|&[]"; */
/* besser (11. Sept) 2. (removed \") */
/* rl_basic_word_break_characters = " \t\n\\@$}=;|&[]"; */
/* besser (11. Sept) 3. (removed }) */
rl_basic_word_break_characters = " \t\n\\@$=;|&[]";
#if 0
// rl_basic_quote_characters = "\"{"; // XXX ??? XXX
// rl_completer_quote_characters = "\"";
rl_basic_quote_characters = "\"{"; // XXX ??? XXX
rl_completer_quote_characters = "\"";
#endif
/*
rl_filename_quote_characters
= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
rl_filename_quoting_function
= (CPFunction*) TclReadlineFilenameQuotingFunction;
*/
|
︙ | | |
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
|
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
|
}
char**
TclReadlineCompletion(char* text, int start, int end)
{
char** matches = (char**) NULL;
int status;
// rl_attempted_completion_over = 0;
rl_completion_append_character = ' '; /* reset, just in case ... */
#if 0
fprintf(stderr, "DEBUG> TclReadlineCompletion: text=|%s|\n", text);
fprintf(stderr, "DEBUG> TclReadlineCompletion: start=|%d|\n", start);
fprintf(stderr, "DEBUG> TclReadlineCompletion: end=|%d|\n", end);
#endif
#if 0
char* history_event = (char*) NULL;
if (text) {
if ('!' == text[0])
history_event = strdup(text);
else if (start && rl_line_buffer[start - 1] == '!' /* for '$' */) {
int len = strlen(text);
history_event = strncpy((char*) malloc(sizeof(char) * (len + 1)),
rl_line_buffer[start - 1], len);
history_event[len] = '\0'; /* terminate */
}
}
if (history_event)
#endif
if (text && ('!' == text[0]
|| (start && rl_line_buffer[start - 1] == '!' /* for '$' */))) {
char* expansion = (char*) NULL;
int oldlen = strlen(rl_line_buffer);
status = history_expand(rl_line_buffer, &expansion);
if (status >= 1) {
rl_extend_line_buffer(strlen(expansion) + 1);
strcpy(rl_line_buffer, expansion);
rl_end = strlen(expansion);
rl_point += strlen(expansion) - oldlen;
FREE(expansion);
/* rl_redisplay(); */
/*
* TODO:
* because we return 0 == matches,
* the filename completer will still beep.
rl_inhibit_completion = 1;
*/
return matches;
}
FREE(expansion);
}
if (tclrl_custom_completer) {
char start_s[BUFSIZ], end_s[BUFSIZ];
Tcl_Obj* obj;
Tcl_Obj** objv;
int objc;
char* quoted_text = TclReadlineQuote(text, "$[]{}\"");
char* quoted_rl_line_buffer
= TclReadlineQuote(rl_line_buffer, "$[]{}\"");
#if 0
fprintf (stderr, "(TclReadlineCompletion) rl_line_buffer = |%s|\n",
rl_line_buffer);
fprintf (stderr, "(TclReadlineCompletion) quoted_rl_line_buffer = |%s|\n",
quoted_rl_line_buffer);
fprintf (stderr, "(TclReadlineCompletion) text = |%s|\n", text);
fprintf (stderr, "(TclReadlineCompletion) quoted_text = |%s|\n",
quoted_text);
#endif
sprintf(start_s, "%d", start);
sprintf(end_s, "%d", end);
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);
return matches;
}
#if 0
fprintf(stderr, "\nscript returned |%s|\n",
Tcl_GetStringResult(tclrl_interp));
#endif
obj = Tcl_GetObjResult(tclrl_interp);
status = Tcl_ListObjGetElements(tclrl_interp, obj, &objc, &objv);
if (TCL_OK != status)
return matches;
/* fprintf (stderr, "(TclReadlineCompletion) objc = %d\n", objc); */
if (objc) {
int i, length;
matches = (char**) MALLOC(sizeof(char*) * (objc + 1));
for (i = 0; i < objc; i++) {
matches[i] = strdup(Tcl_GetStringFromObj(objv[i], &length));
if (1 == objc && !strlen(matches[i])) {
// rl_attempted_completion_over = 1;
FREE(matches[i]);
FREE(matches);
return (char**) NULL;
}
/*
fprintf (stderr, "(TclReadlineCompletion) len[%s]=%d\n",
matches[i], strlen(matches[i]));
*/
}
/**
* this is a special one:
* if the script returns exactly two arguments
* and the second argument is the empty string,
* the rl_completion_append_character is set
|
︙ | | |
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
|
548
549
550
551
552
553
554
555
556
557
558
559
560
561
|
-
-
-
-
-
-
-
|
}
Tcl_ResetResult(tclrl_interp); /* clear result space */
}
if (!matches && tclrl_use_builtin_completer) {
matches = completion_matches(text, TclReadline0generator);
}
#if 0
{
char **ptr;
for (ptr = matches; ptr && *ptr; ptr++)
fprintf (stderr, "(TclReadlineCompletion) |%s|\n", *ptr);
}
#endif
return matches;
}
char*
TclReadline0generator(char* text, int state)
{
|
︙ | | |
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
|
606
607
608
609
610
611
612
613
614
615
616
617
618
619
|
-
-
-
-
|
break;
case _CMD_GET:
local_line = strdup(rl_line_buffer);
sub = TclReadlineParse(args, sizeof(args), local_line);
/*
* fprintf (stderr, "(TclReadlineKnownCommands) state=%d\n", state);
* fprintf (stderr, "(TclReadlineKnownCommands) text = |%s|\n", text);
*/
if (0 == sub || (1 == sub && '\0' != text[0])) {
if (!state) {
new = cmds;
len = strlen(text);
}
while (new && (name = new->cmd)) {
|
︙ | | |