Debian Codegraph @

generic/tkConsole.c

raw symbols SCIP
1
2 /*
3 * tkConsole.c --
4 *
5 * This file implements a Tcl console for systems that may not
6 * otherwise have access to a console. It uses the Text widget
7 * and provides special access via a console command.
8 *
9 * Copyright (c) 1995-1996 Sun Microsystems, Inc.
10 *
11 * See the file "license.terms" for information on usage and redistribution
12 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 *
14 * SCCS: @(#) tkConsole.c 1.55 98/01/02 17:40:37
15 */
16
17 #include "bltInt.h"
18
19 /*
20 * A data structure of the following type holds information for each console
21 * which a handler (i.e. a Tcl command) has been defined for a particular
22 * top-level window.
23 */
24
25 typedef struct {
26 Tcl_Interp *consoleInterp; /* Interpreter for the console. */
27 Tcl_Interp *interp; /* Interpreter to send console commands. */
28 } ConsoleInfo;
29
30 static Tcl_Interp *gStdoutInterp = NULL;
31
32 #if ((TCL_VERSION_NUMBER >= _VERSION(8,0,0)) && \
33 (TCL_VERSION_NUMBER < _VERSION(8,1,0)))
34 #define HAVE_BROKEN_LIB_PATH 1
35 #undef HAVE_UTF
36 #endif
37
38 #if (TCL_VERSION_NUMBER >= _VERSION(8,1,1))
39 #define HAVE_UTF 1
40 extern void TclInitSubsystems _ANSI_ARGS_((CONST char *argv0));
41 #else
42 #endif
43
44 /*
45 * Forward declarations for procedures defined later in this file:
46 *
47 * The first three will be used in the tk app shells...
48 */
49
50 void TkConsoleCreate _ANSI_ARGS_((void));
51 int TkConsoleInit _ANSI_ARGS_((Tcl_Interp *interp));
52 void TkConsolePrint _ANSI_ARGS_((Tcl_Interp *interp,
53 int devId, CONST char *buffer, long size));
54
55 static int ConsoleCmd _ANSI_ARGS_((ClientData clientData,
56 Tcl_Interp *interp, int argc, char **argv));
57 static void ConsoleDeleteProc _ANSI_ARGS_((ClientData clientData));
58 static void ConsoleEventProc _ANSI_ARGS_((ClientData clientData,
59 XEvent *eventPtr));
60 static int InterpreterCmd _ANSI_ARGS_((ClientData clientData,
61 Tcl_Interp *interp, int argc, char **argv));
62
63 static int ConsoleInput _ANSI_ARGS_((ClientData instanceData,
64 char *buf, int toRead, int *errorCode));
65 static int ConsoleOutput _ANSI_ARGS_((ClientData instanceData,
66 char *buf, int toWrite, int *errorCode));
67 static int ConsoleClose _ANSI_ARGS_((ClientData instanceData,
68 Tcl_Interp *interp));
69 static void ConsoleWatch _ANSI_ARGS_((ClientData instanceData,
70 int mask));
71 static int ConsoleHandle _ANSI_ARGS_((ClientData instanceData,
72 int direction, ClientData *handlePtr));
73
74 /*
75 * This structure describes the channel type structure for file based IO:
76 */
77
78 static Tcl_ChannelType consoleChannelType =
79 {
80 "console", /* Type name. */
81 NULL, /* Always non-blocking.*/
82 ConsoleClose, /* Close proc. */
83 ConsoleInput, /* Input proc. */
84 ConsoleOutput, /* Output proc. */
85 NULL, /* Seek proc. */
86 NULL, /* Set option proc. */
87 NULL, /* Get option proc. */
88 ConsoleWatch, /* Watch for events on console. */
89 ConsoleHandle, /* Get a handle from the device. */
90 };
91
92 /*
93 *----------------------------------------------------------------------
94 *
95 * TkConsoleCreate --
96 *
97 * Create the console channels and install them as the standard
98 * channels. All I/O will be discarded until TkConsoleInit is
99 * called to attach the console to a text widget.
100 *
101 * Results:
102 * None.
103 *
104 * Side effects:
105 * Creates the console channel and installs it as the standard
106 * channels.
107 *
108 *----------------------------------------------------------------------
109 */
110
111 void
112 TkConsoleCreate()
113 {
114 Tcl_Channel consoleChannel;
115
116 #ifdef HAVE_UTF
117 TclInitSubsystems(NULL);
118 #endif
119 consoleChannel = Tcl_CreateChannel(&consoleChannelType, "console0",
120 (ClientData)TCL_STDIN, TCL_READABLE);
121 if (consoleChannel != NULL) {
122 Tcl_SetChannelOption(NULL, consoleChannel, "-translation", "lf");
123 Tcl_SetChannelOption(NULL, consoleChannel, "-buffering", "none");
124 #ifdef HAVE_UTF
125 Tcl_SetChannelOption(NULL, consoleChannel, "-encoding", "utf-8");
126 #endif
127 }
128 Tcl_SetStdChannel(consoleChannel, TCL_STDIN);
129 consoleChannel = Tcl_CreateChannel(&consoleChannelType, "console1",
130 (ClientData)TCL_STDOUT, TCL_WRITABLE);
131 if (consoleChannel != NULL) {
132 Tcl_SetChannelOption(NULL, consoleChannel, "-translation", "lf");
133 Tcl_SetChannelOption(NULL, consoleChannel, "-buffering", "none");
134 #ifdef HAVE_UTF
135 Tcl_SetChannelOption(NULL, consoleChannel, "-encoding", "utf-8");
136 #endif
137 }
138 Tcl_SetStdChannel(consoleChannel, TCL_STDOUT);
139 consoleChannel = Tcl_CreateChannel(&consoleChannelType, "console2",
140 (ClientData)TCL_STDERR, TCL_WRITABLE);
141 if (consoleChannel != NULL) {
142 Tcl_SetChannelOption(NULL, consoleChannel, "-translation", "lf");
143 Tcl_SetChannelOption(NULL, consoleChannel, "-buffering", "none");
144 #ifdef HAVE_UTF
145 Tcl_SetChannelOption(NULL, consoleChannel, "-encoding", "utf-8");
146 #endif
147 }
148 Tcl_SetStdChannel(consoleChannel, TCL_STDERR);
149 }
150
151 /*
152 *----------------------------------------------------------------------
153 *
154 * TkConsoleInit --
155 *
156 * Initialize the console. This code actually creates a new
157 * application and associated interpreter. This effectivly hides
158 * the implementation from the main application.
159 *
160 * Results:
161 * None.
162 *
163 * Side effects:
164 * A new console it created.
165 *
166 *----------------------------------------------------------------------
167 */
168
169 int
170 TkConsoleInit(interp)
171 Tcl_Interp *interp; /* Interpreter to use for prompting. */
172 {
173 Tcl_Interp *consoleInterp;
174 ConsoleInfo *info;
175 Tk_Window mainWindow = Tk_MainWindow(interp);
176 #ifdef MAC_TCL
177 static char initCmd[] = "source -rsrc {Console}";
178 #else
179 static char initCmd[] = "source $tk_library/console.tcl";
180 #endif
181
182 consoleInterp = Tcl_CreateInterp();
183 if (consoleInterp == NULL) {
184 goto error;
185 }
186 #if defined(HAVE_BROKEN_LIB_PATH) && defined(TCLLIBPATH)
187 Tcl_SetVar(consoleInterp, "tclDefaultLibrary",
188 TCLLIBPATH, TCL_GLOBAL_ONLY);
189 #endif
190 /*
191 * Initialized Tcl and Tk.
192 */
193
194 if (Tcl_Init(consoleInterp) != TCL_OK) {
195 goto error;
196 }
197 if (Tk_Init(consoleInterp) != TCL_OK) {
198 goto error;
199 }
200 gStdoutInterp = interp;
201
202 /*
203 * Add console commands to the interp
204 */
205 info = (ConsoleInfo *) Tcl_Alloc(sizeof(ConsoleInfo));
206 info->interp = interp;
207 info->consoleInterp = consoleInterp;
208 Tcl_CreateCommand(interp, "console", ConsoleCmd, (ClientData)info,
209 (Tcl_CmdDeleteProc *)ConsoleDeleteProc);
210 Tcl_CreateCommand(consoleInterp, "consoleinterp", InterpreterCmd,
211 (ClientData)info, (Tcl_CmdDeleteProc *)NULL);
212
213 Tk_CreateEventHandler(mainWindow, StructureNotifyMask, ConsoleEventProc,
214 (ClientData)info);
215
216 Tcl_Preserve((ClientData)consoleInterp);
217 if (Tcl_Eval(consoleInterp, initCmd) == TCL_ERROR) {
218 /* goto error; -- no problem for now... */
219 printf("Eval error: %s", consoleInterp->result);
220 }
221 Tcl_Release((ClientData)consoleInterp);
222 return TCL_OK;
223
224 error:
225 if (consoleInterp != NULL) {
226 Tcl_DeleteInterp(consoleInterp);
227 }
228 return TCL_ERROR;
229 }
230
231 /*
232 *----------------------------------------------------------------------
233 *
234 * ConsoleOutput--
235 *
236 * Writes the given output on the IO channel. Returns count of how
237 * many characters were actually written, and an error indication.
238 *
239 * Results:
240 * A count of how many characters were written is returned and an
241 * error indication is returned in an output argument.
242 *
243 * Side effects:
244 * Writes output on the actual channel.
245 *
246 *----------------------------------------------------------------------
247 */
248
249 static int
250 ConsoleOutput(instanceData, buf, toWrite, errorCode)
251 ClientData instanceData; /* Indicates which device to use. */
252 char *buf; /* The data buffer. */
253 int toWrite; /* How many bytes to write? */
254 int *errorCode; /* Where to store error code. */
255 {
256 *errorCode = 0;
257 Tcl_SetErrno(0);
258
259 if (gStdoutInterp != NULL) {
260 TkConsolePrint(gStdoutInterp, (int)instanceData, buf, toWrite);
261 }
262 return toWrite;
263 }
264
265 /*
266 *----------------------------------------------------------------------
267 *
268 * ConsoleInput --
269 *
270 * Read input from the console. Not currently implemented.
271 *
272 * Results:
273 * Always returns EOF.
274 *
275 * Side effects:
276 * None.
277 *
278 *----------------------------------------------------------------------
279 */
280
281 /* ARGSUSED */
282 static int
283 ConsoleInput(instanceData, buf, bufSize, errorCode)
284 ClientData instanceData; /* Not Used.. */
285 char *buf; /* Where to store data read. */
286 int bufSize; /* How much space is available
287 * in the buffer? */
288 int *errorCode; /* Where to store error code. */
289 {
290 return 0; /* Always return EOF. */
291 }
292
293 /*
294 *----------------------------------------------------------------------
295 *
296 * ConsoleClose --
297 *
298 * Closes the IO channel.
299 *
300 * Results:
301 * Always returns 0 (success).
302 *
303 * Side effects:
304 * Frees the dummy file associated with the channel.
305 *
306 *----------------------------------------------------------------------
307 */
308
309 /* ARGSUSED */
310 static int
311 ConsoleClose(instanceData, interp)
312 ClientData instanceData; /* Not Used.. */
313 Tcl_Interp *interp; /* Not Used.. */
314 {
315 return 0;
316 }
317
318 /*
319 *----------------------------------------------------------------------
320 *
321 * ConsoleWatch --
322 *
323 * Called by the notifier to set up the console device so that
324 * events will be noticed. Since there are no events on the
325 * console, this routine just returns without doing anything.
326 *
327 * Results:
328 * None.
329 *
330 * Side effects:
331 * None.
332 *
333 *----------------------------------------------------------------------
334 */
335
336 /* ARGSUSED */
337 static void
338 ConsoleWatch(instanceData, mask)
339 ClientData instanceData; /* Device ID for the channel. */
340 int mask; /* OR-ed combination of
341 * TCL_READABLE, TCL_WRITABLE and
342 * TCL_EXCEPTION, for the events
343 * we are interested in. */
344 {
345 }
346
347 /*
348 *----------------------------------------------------------------------
349 *
350 * ConsoleHandle --
351 *
352 * Invoked by the generic IO layer to get a handle from a channel.
353 * Because console channels are not devices, this function always
354 * fails.
355 *
356 * Results:
357 * Always returns TCL_ERROR.
358 *
359 * Side effects:
360 * None.
361 *
362 *----------------------------------------------------------------------
363 */
364
365 /* ARGSUSED */
366 static int
367 ConsoleHandle(instanceData, direction, handlePtr)
368 ClientData instanceData; /* Device ID for the channel. */
369 int direction; /* TCL_READABLE or TCL_WRITABLE to indicate
370 * which direction of the channel is being
371 * requested. */
372 ClientData *handlePtr; /* Where to store handle */
373 {
374 return TCL_ERROR;
375 }
376
377 /*
378 *----------------------------------------------------------------------
379 *
380 * ConsoleCmd --
381 *
382 * The console command implements a Tcl interface to the various console
383 * options.
384 *
385 * Results:
386 * None.
387 *
388 * Side effects:
389 * None.
390 *
391 *----------------------------------------------------------------------
392 */
393
394 static int
395 ConsoleCmd(clientData, interp, argc, argv)
396 ClientData clientData; /* Not used. */
397 Tcl_Interp *interp; /* Current interpreter. */
398 int argc; /* Number of arguments. */
399 char **argv; /* Argument strings. */
400 {
401 ConsoleInfo *info = (ConsoleInfo *) clientData;
402 char c;
403 int length;
404 int result;
405 Tcl_Interp *consoleInterp;
406
407 if (argc < 2) {
408 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
409 " option ?arg arg ...?\"", (char *)NULL);
410 return TCL_ERROR;
411 }
412 c = argv[1][0];
413 length = strlen(argv[1]);
414 result = TCL_OK;
415 consoleInterp = info->consoleInterp;
416 Tcl_Preserve((ClientData)consoleInterp);
417 if ((c == 't') && (strncmp(argv[1], "title", length)) == 0) {
418 Tcl_DString dString;
419
420 Tcl_DStringInit(&dString);
421 Tcl_DStringAppend(&dString, "wm title . ", -1);
422 if (argc == 3) {
423 Tcl_DStringAppendElement(&dString, argv[2]);
424 }
425 Tcl_Eval(consoleInterp, Tcl_DStringValue(&dString));
426 Tcl_DStringFree(&dString);
427 } else if ((c == 'h') && (strncmp(argv[1], "hide", length)) == 0) {
428 Tcl_Eval(info->consoleInterp, "wm withdraw .");
429 } else if ((c == 's') && (strncmp(argv[1], "show", length)) == 0) {
430 Tcl_Eval(info->consoleInterp, "wm deiconify .");
431 } else if ((c == 'e') && (strncmp(argv[1], "eval", length)) == 0) {
432 if (argc == 3) {
433 Tcl_Eval(info->consoleInterp, argv[2]);
434 } else {
435 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
436 " eval command\"", (char *)NULL);
437 return TCL_ERROR;
438 }
439 } else {
440 Tcl_AppendResult(interp, "bad option \"", argv[1],
441 "\": should be hide, show, or title",
442 (char *)NULL);
443 result = TCL_ERROR;
444 }
445 Tcl_Release((ClientData)consoleInterp);
446 return result;
447 }
448
449 /*
450 *----------------------------------------------------------------------
451 *
452 * InterpreterCmd --
453 *
454 * This command allows the console interp to communicate with the
455 * main interpreter.
456 *
457 * Results:
458 * None.
459 *
460 * Side effects:
461 * None.
462 *
463 *----------------------------------------------------------------------
464 */
465
466 static int
467 InterpreterCmd(clientData, interp, argc, argv)
468 ClientData clientData; /* Not used. */
469 Tcl_Interp *interp; /* Current interpreter. */
470 int argc; /* Number of arguments. */
471 char **argv; /* Argument strings. */
472 {
473 ConsoleInfo *info = (ConsoleInfo *) clientData;
474 char c;
475 int length;
476 int result;
477 Tcl_Interp *otherInterp;
478
479 if (argc < 2) {
480 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
481 " option ?arg arg ...?\"", (char *)NULL);
482 return TCL_ERROR;
483 }
484 c = argv[1][0];
485 length = strlen(argv[1]);
486 otherInterp = info->interp;
487 Tcl_Preserve((ClientData)otherInterp);
488 if ((c == 'e') && (strncmp(argv[1], "eval", length)) == 0) {
489 result = Tcl_GlobalEval(otherInterp, argv[2]);
490 Tcl_AppendResult(interp, otherInterp->result, (char *)NULL);
491 } else if ((c == 'r') && (strncmp(argv[1], "record", length)) == 0) {
492 Tcl_RecordAndEval(otherInterp, argv[2], TCL_EVAL_GLOBAL);
493 result = TCL_OK;
494 Tcl_AppendResult(interp, otherInterp->result, (char *)NULL);
495 } else {
496 Tcl_AppendResult(interp, "bad option \"", argv[1],
497 "\": should be eval or record",
498 (char *)NULL);
499 result = TCL_ERROR;
500 }
501 Tcl_Release((ClientData)otherInterp);
502 return result;
503 }
504
505 /*
506 *----------------------------------------------------------------------
507 *
508 * ConsoleDeleteProc --
509 *
510 * If the console command is deleted we destroy the console window
511 * and all associated data structures.
512 *
513 * Results:
514 * None.
515 *
516 * Side effects:
517 * A new console it created.
518 *
519 *----------------------------------------------------------------------
520 */
521
522 void
523 ConsoleDeleteProc(clientData)
524 ClientData clientData;
525 {
526 ConsoleInfo *info = (ConsoleInfo *) clientData;
527
528 Tcl_DeleteInterp(info->consoleInterp);
529 info->consoleInterp = NULL;
530 }
531
532 /*
533 *----------------------------------------------------------------------
534 *
535 * ConsoleEventProc --
536 *
537 * This event procedure is registered on the main window of the
538 * slave interpreter. If the user or a running script causes the
539 * main window to be destroyed, then we need to inform the console
540 * interpreter by invoking "tkConsoleExit".
541 *
542 * Results:
543 * None.
544 *
545 * Side effects:
546 * Invokes the "tkConsoleExit" procedure in the console interp.
547 *
548 *----------------------------------------------------------------------
549 */
550
551 static void
552 ConsoleEventProc(clientData, eventPtr)
553 ClientData clientData;
554 XEvent *eventPtr;
555 {
556 ConsoleInfo *info = (ConsoleInfo *) clientData;
557 Tcl_Interp *consoleInterp;
558
559 if (eventPtr->type == DestroyNotify) {
560 consoleInterp = info->consoleInterp;
561
562 /*
563 * It is possible that the console interpreter itself has
564 * already been deleted. In that case the consoleInterp
565 * field will be set to NULL. If the interpreter is already
566 * gone, we do not have to do any work here.
567 */
568
569 if (consoleInterp == (Tcl_Interp *)NULL) {
570 return;
571 }
572 Tcl_Preserve((ClientData)consoleInterp);
573 Tcl_Eval(consoleInterp, "tkConsoleExit");
574 Tcl_Release((ClientData)consoleInterp);
575 }
576 }
577
578 /*
579 *----------------------------------------------------------------------
580 *
581 * TkConsolePrint --
582 *
583 * Prints to the give text to the console. Given the main interp
584 * this functions find the appropiate console interp and forwards
585 * the text to be added to that console.
586 *
587 * Results:
588 * None.
589 *
590 * Side effects:
591 * None.
592 *
593 *----------------------------------------------------------------------
594 */
595
596 void
597 TkConsolePrint(interp, devId, buffer, size)
598 Tcl_Interp *interp; /* Main interpreter. */
599 int devId; /* TCL_STDOUT for stdout, TCL_STDERR for
600 * stderr. */
601 CONST char *buffer; /* Text buffer. */
602 long size; /* Size of text buffer. */
603 {
604 Tcl_DString command, output;
605 Tcl_CmdInfo cmdInfo;
606 char *cmd;
607 ConsoleInfo *info;
608 Tcl_Interp *consoleInterp;
609 int result;
610
611 if (interp == NULL) {
612 return;
613 }
614 if (devId == TCL_STDERR) {
615 cmd = "tkConsoleOutput stderr ";
616 } else {
617 cmd = "tkConsoleOutput stdout ";
618 }
619
620 result = Tcl_GetCommandInfo(interp, "console", &cmdInfo);
621 if (result == 0) {
622 return;
623 }
624 info = (ConsoleInfo *) cmdInfo.clientData;
625
626 Tcl_DStringInit(&output);
627 Tcl_DStringAppend(&output, buffer, size);
628
629 Tcl_DStringInit(&command);
630 Tcl_DStringAppend(&command, cmd, strlen(cmd));
631 Tcl_DStringAppendElement(&command, output.string);
632
633 consoleInterp = info->consoleInterp;
634 Tcl_Preserve((ClientData)consoleInterp);
635 Tcl_Eval(consoleInterp, command.string);
636 Tcl_Release((ClientData)consoleInterp);
637
638 Tcl_DStringFree(&command);
639 Tcl_DStringFree(&output);
640 }