162
163
164
165
166
167
168
169
170
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
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
282
283
284
285
286
287
288
289
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
315
316
317
318
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
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
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
282
283
284
285
286
287
288
289
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
315
316
317
318
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
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
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
|
-
-
-
-
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
-
+
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
+
-
-
-
-
-
-
+
+
+
+
+
+
+
-
-
-
+
-
-
-
-
-
-
+
+
-
-
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
-
-
-
-
+
-
-
-
-
+
-
-
-
-
-
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
+
+
-
-
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
-
-
-
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
}
result_c = strdup(Tcl_DStringValue(&result));
return result_c;
}
int
TclReadlineCmd(
ClientData clientData, /* Main window associated with interpreter */
Tcl_Interp* interp, /* Current interpreter */
int argc, /* Number of arguments */
char** argv /* Argument strings */
ClientData clientData,
Tcl_Interp* interp, /* Current interpreter */
int argc, /* Number of arguments */
char** argv /* Argument strings */
)
{
int i, obj_idx, status;
Tcl_Obj** objv = (Tcl_Obj**) MALLOC((argc + 1) * sizeof(Tcl_Obj *));
int c, length;
if (argc < 2)
goto BAD_COMMAND;
c = argv[1][0];
length = strlen(argv[1]);
if (c == 'r' && strncmp(argv[1], "read", length) == 0) {
static char *subCmds[] = {
"read", "initialize", "write", "add", "complete",
"customcompleter", "builtincompleter", "eofchar",
(char *) NULL
};
enum SubCmdIdx {
TCLRL_READ, TCLRL_INITIALIZE, TCLRL_WRITE, TCLRL_ADD, TCLRL_COMPLETE,
TCLRL_CUSTOMCOMPLETER, TCLRL_BUILTINCOMPLETER, TCLRL_EOFCHAR
};
Tcl_ResetResult(interp); /* clear the result space */
for (i = 0; i < argc; i++) {
Tcl_Obj* objPtr = Tcl_NewStringObj(argv[i], -1);
Tcl_IncrRefCount(objPtr);
objv[i] = objPtr;
}
objv[argc] = 0; /* terminate */
if (argc < 2) {
Tcl_WrongNumArgs(interp, 1, objv, "option ?arg arg ...?");
return TCL_ERROR;
}
status = Tcl_GetIndexFromObj
(interp, objv[1], subCmds, "option", 0, (int *) &obj_idx);
char* expansion = (char*) NULL;
int status;
tclrl_line_complete = LINE_PENDING;
tclrl_state = TCL_OK;
rl_callback_handler_install(argc == 3 ? argv[2] : "%",
if (status != TCL_OK) {
FREE(objv)
return status;
}
switch (obj_idx) {
case TCLRL_READ:
rl_callback_handler_install(argc == 3 ? argv[2] : "%",
TclReadlineLineCompleteHandler);
Tcl_CreateFileHandler(0, TCL_READABLE,
Tcl_CreateFileHandler(0, TCL_READABLE,
TclReadlineReadHandler, (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);
}
/**
* 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
*/
tclrl_state = LINE_PENDING;
while (!TclReadlineLineComplete()) {
#ifdef EXECUTING_MACRO_HACK
/**
* check first, if more characters are
* available from _rl_executing_macro,
* because Tcl_DoOneEvent() will (naturally)
* not detect this `event'.
*/
if (_rl_executing_macro)
TclReadlineReadHandler((ClientData) NULL, TCL_READABLE);
else
#endif
Tcl_DoOneEvent(TCL_ALL_EVENTS);
}
Tcl_DeleteFileHandler(0);
Tcl_DeleteFileHandler(0);
if (TCL_OK != tclrl_state)
switch (tclrl_state) {
return tclrl_state; /* !! */
if ((LINE_EOF == tclrl_line_complete) && tclrl_eof_string) {
Tcl_Eval(interp, tclrl_eof_string);
return tclrl_state;
}
case LINE_COMPLETE:
return TCL_OK;
/* NOTREACHED */
break;
case LINE_EOF:
status = history_expand(tclrl_line, &expansion);
if (status >= 1) {
#if 0
if (tclrl_eof_string)
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
return Tcl_Eval(interp, tclrl_eof_string);
else
printf("%s\n", expansion);
#endif
return TCL_OK;
/* NOTREACHED */
break;
}
else if (status == -1) {
Tcl_AppendResult
(interp, "error in history expansion\n", (char*) NULL);
return tclrl_state;
}
default:
return tclrl_state;
/* NOTREACHED */
break;
}
/**
* TODO: status == 2 ...
*/
break;
if (expansion && *expansion)
add_history(expansion);
Tcl_SetResult(interp, expansion, TCL_VOLATILE);
case TCLRL_INITIALIZE:
FREE(tclrl_line);
FREE(expansion);
return tclrl_state;
} else if (c == 'i' && strncmp(argv[1], "initialize", length) == 0) {
if (3 != argc)
if (3 != argc) {
goto BAD_COMMAND;
else
return TclReadlineInitialize(interp, argv[2]);
} else if (c == 'w' && strncmp(argv[1], "write", length) == 0) {
if (3 != argc) {
Tcl_WrongNumArgs(interp, 2, objv, "historyfile");
return TCL_ERROR;
} else {
return TclReadlineInitialize(interp, argv[2]);
}
break;
case TCLRL_WRITE:
if (3 != argc) {
goto BAD_COMMAND;
} else if (write_history(argv[2])) {
Tcl_AppendResult(interp, "unable to write history to `",
Tcl_WrongNumArgs(interp, 2, objv, "historyfile");
return TCL_ERROR;
} else if (write_history(argv[2])) {
Tcl_AppendResult(interp, "unable to write history to `",
argv[2], "'\n", (char*) NULL);
return TCL_ERROR;
}
if (tclrl_history_length >= 0) {
history_truncate_file(argv[2], tclrl_history_length);
}
return TCL_OK;
} else if (c == 'a' && strncmp(argv[1], "add", length) == 0) {
if (3 != argc)
return TCL_ERROR;
}
if (tclrl_history_length >= 0) {
history_truncate_file(argv[2], tclrl_history_length);
}
return TCL_OK;
break;
case TCLRL_ADD:
if (3 != argc) {
goto BAD_COMMAND;
else if (TclReadlineKnownCommands(argv[2], (int) 0, _CMD_SET))
Tcl_AppendResult(interp, "unable to add command \"",
Tcl_WrongNumArgs(interp, 2, objv, "completerLine");
return TCL_ERROR;
} else if (TclReadlineKnownCommands(argv[2], (int) 0, _CMD_SET)) {
Tcl_AppendResult(interp, "unable to add command \"",
argv[2], "\"\n", (char*) NULL);
}
break;
} else if (c == 'c' && strncmp(argv[1], "complete", length) == 0) {
if (3 != argc)
case TCLRL_COMPLETE:
if (3 != argc) {
goto BAD_COMMAND;
else if (Tcl_CommandComplete(argv[2]))
Tcl_AppendResult(interp, "1", (char*) NULL);
else
Tcl_AppendResult(interp, "0", (char*) NULL);
} else if (c == 'c' && strncmp(argv[1], "customcompleter", length) == 0) {
if (3 != argc && 2 != argc)
Tcl_WrongNumArgs(interp, 2, objv, "line");
return TCL_ERROR;
} else if (Tcl_CommandComplete(argv[2])) {
Tcl_AppendResult(interp, "1", (char*) NULL);
} else {
Tcl_AppendResult(interp, "0", (char*) NULL);
}
break;
case TCLRL_CUSTOMCOMPLETER:
if (argc > 3) {
goto BAD_COMMAND;
if (3 == argc) {
if (tclrl_custom_completer)
FREE(tclrl_custom_completer);
if (!blank_line(argv[2]))
tclrl_custom_completer = stripwhite(strdup(argv[2]));
}
Tcl_AppendResult(interp, tclrl_custom_completer, (char*) NULL);
} else if (c == 'b' && strncmp(argv[1], "builtincompleter", length) == 0) {
int bool = tclrl_use_builtin_completer;
if (3 != argc && 2 != argc)
Tcl_WrongNumArgs(interp, 2, objv, "?scriptCompleter?");
return TCL_ERROR;
} else if (3 == argc) {
if (tclrl_custom_completer)
FREE(tclrl_custom_completer);
if (!blank_line(argv[2]))
tclrl_custom_completer = stripwhite(strdup(argv[2]));
}
Tcl_AppendResult(interp, tclrl_custom_completer, (char*) NULL);
break;
case TCLRL_BUILTINCOMPLETER:
if (argc > 3) {
goto BAD_COMMAND;
if (3 == argc) {
if (TCL_OK != Tcl_GetBoolean(interp, argv[2], &bool)) {
Tcl_AppendResult(interp,
"wrong # args: should be a boolean value.", (char*) NULL);
return TCL_ERROR;
} else {
tclrl_use_builtin_completer = bool;
}
}
Tcl_AppendResult(interp, tclrl_use_builtin_completer ? "1" : "0",
(char*) NULL);
} else if (c == 'e' && strncmp(argv[1], "eofchar", length) == 0) {
if (3 != argc && 2 != argc)
Tcl_WrongNumArgs(interp, 2, objv, "?boolean?");
return TCL_ERROR;
} else if (3 == argc) {
int bool = tclrl_use_builtin_completer;
if (TCL_OK != Tcl_GetBoolean(interp, argv[2], &bool)) {
Tcl_AppendResult(interp,
"wrong # args: should be a boolean value.",
(char*) NULL);
return TCL_ERROR;
} else {
tclrl_use_builtin_completer = bool;
}
}
Tcl_AppendResult(interp, tclrl_use_builtin_completer ? "1" : "0",
(char*) NULL);
break;
case TCLRL_EOFCHAR:
if (argc > 3) {
goto BAD_COMMAND;
if (3 == argc) {
if (tclrl_eof_string)
FREE(tclrl_eof_string);
if (!blank_line(argv[2]))
tclrl_eof_string = stripwhite(strdup(argv[2]));
}
Tcl_AppendResult(interp, tclrl_eof_string, (char*) NULL);
} else {
goto BAD_COMMAND;
Tcl_WrongNumArgs(interp, 2, objv, "?script?");
return TCL_ERROR;
} else if (3 == argc) {
if (tclrl_eof_string)
FREE(tclrl_eof_string);
if (!blank_line(argv[2]))
tclrl_eof_string = stripwhite(strdup(argv[2]));
}
Tcl_AppendResult(interp, tclrl_eof_string, (char*) NULL);
break;
default:
goto BAD_COMMAND;
/* NOTREACHED */
break;
}
return TCL_OK;
BAD_COMMAND:
Tcl_AppendResult(interp,
"wrong # args: should be \"readline option ?arg ...?\"",
(char*) NULL);
return TCL_ERROR;
}
void
TclReadlineReadHandler(ClientData clientData, int mask)
{
if (mask & TCL_READABLE) {
#ifdef EXECUTING_MACRO_HACK
do {
#endif
rl_callback_read_char();
rl_callback_read_char();
#ifdef EXECUTING_MACRO_HACK
/**
* check, if we're inside a macro and
* if so, read all macro characters.
*/
while (_rl_executing_macro) {
/**
* check, if we're inside a macro and
* if so, read all macro characters
* until the next eol.
*/
} while (_rl_executing_macro && !TclReadlineLineComplete());
rl_callback_read_char();
}
#endif
}
}
void
TclReadlineLineCompleteHandler(char* ptr)
{
#if 1
if (!ptr) { /* <c-d> */
tclrl_line_complete = LINE_EOF;
rl_callback_handler_remove();
} else if (*ptr) {
tclrl_line_complete = LINE_COMPLETE;
rl_callback_handler_remove();
tclrl_line = ptr;
}
TclReadlineTerminate(LINE_EOF);
} else {
/**
* From version 0.9.3 upwards, all lines are
* returned, even empty lines. (Only non-empty
* lines are stuffed in readline's history.)
* The calling script is responsible for handling
* empty strings.
*/
char* expansion = (char*) NULL;
int status = history_expand(ptr, &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
/* TODO: make this a valid tcl output */
printf("%s\n", expansion);
#endif
if (ptr && *ptr) {
tclrl_line_complete = 1;
rl_callback_handler_remove();
tclrl_line = ptr;
}
} else if (-1 == status) {
Tcl_AppendResult
(tclrl_interp, "error in history expansion\n", (char*) NULL);
TclReadlineTerminate(TCL_ERROR);
}
/**
* TODO: status == 2 ...
*/
Tcl_AppendResult(tclrl_interp, expansion, (char*) NULL);
#ifdef EXECUTING_MACRO_HACK
/**
* don't stuff macro lines
* into readline's history.
*/
if(!_rl_executing_macro) {
#endif
/**
* don't stuff empty lines
* into readline's history.
* don't stuff twice the same
* line into readline's history.
*/
if (expansion && *expansion && (!tclrl_last_line ||
strcmp(tclrl_last_line, expansion))) {
add_history(expansion);
}
if (tclrl_last_line)
free(tclrl_last_line);
tclrl_last_line = strdup(expansion);
#ifdef EXECUTING_MACRO_HACK
}
#endif
/**
* tell the calling routines to terminate.
*/
TclReadlineTerminate(LINE_COMPLETE);
FREE(ptr);
FREE(expansion);
}
}
int
Tclreadline_SafeInit(Tcl_Interp *interp)
{
return Tclreadline_Init(interp);
}
|
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
|
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
|
+
-
+
-
+
-
+
|
}
if (tclrl_custom_completer) {
char start_s[BUFSIZ], end_s[BUFSIZ];
Tcl_Obj* obj;
Tcl_Obj** objv;
int objc;
int state;
char* quoted_text = TclReadlineQuote(text, "$[]{}\"");
char* quoted_rl_line_buffer
= TclReadlineQuote(rl_line_buffer, "$[]{}\"");
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,
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) {
if (TCL_OK != 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);
TclReadlineTerminate(state);
return matches;
}
obj = Tcl_GetObjResult(tclrl_interp);
status = Tcl_ListObjGetElements(tclrl_interp, obj, &objc, &objv);
if (TCL_OK != status)
return matches;
|