4 * This file implements a Tcl console for systems that may not
5 * otherwise have access to a console. It uses the Text widget
6 * and provides special access via a console command.
8 * Copyright (c) 1995-1996 Sun Microsystems, Inc.
10 * See the file "license.terms" for information on usage and redistribution
11 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 * SCCS: @(#) tkConsole.c 1.37 96/04/20 15:17:32
19 * A data structure of the following type holds information for each console
20 * which a handler (i.e. a Tcl command) has been defined for a particular
24 typedef struct ConsoleInfo {
25 Tcl_Interp *consoleInterp; /* Interpreter for the console. */
26 Tcl_Interp *interp; /* Interpreter to send console commands. */
29 static Tcl_Interp *gStdoutInterp = NULL;
32 * Forward declarations for procedures defined later in this file:
35 static int ConsoleCmd _ANSI_ARGS_((ClientData clientData,
36 Tcl_Interp *interp, int argc, char **argv));
37 static void ConsoleDeleteProc _ANSI_ARGS_((ClientData clientData));
38 static void ConsoleEventProc _ANSI_ARGS_((ClientData clientData,
40 static int InterpreterCmd _ANSI_ARGS_((ClientData clientData,
41 Tcl_Interp *interp, int argc, char **argv));
43 static int ConsoleInput _ANSI_ARGS_((ClientData instanceData,
44 Tcl_File inFile, char *buf, int toRead,
46 static int ConsoleOutput _ANSI_ARGS_((ClientData instanceData,
47 Tcl_File outFile, char *buf, int toWrite,
49 static int ConsoleClose _ANSI_ARGS_((ClientData instanceData,
50 Tcl_Interp *interp, Tcl_File inFile,
54 * This structure describes the channel type structure for file based IO:
57 static Tcl_ChannelType consoleChannelType = {
58 "console", /* Type name. */
59 NULL, /* Always non-blocking.*/
60 ConsoleClose, /* Close proc. */
61 ConsoleInput, /* Input proc. */
62 ConsoleOutput, /* Output proc. */
63 NULL, /* Seek proc. */
64 NULL, /* Set option proc. */
65 NULL, /* Get option proc. */
69 *----------------------------------------------------------------------
73 * Create the console channels and install them as the standard
74 * channels. All I/O will be discarded until TkConsoleInit is
75 * called to attach the console to a text widget.
81 * Creates the console channel and installs it as the standard
84 *----------------------------------------------------------------------
90 Tcl_Channel consoleChannel;
91 Tcl_File inFile, outFile, errFile;
93 inFile = Tcl_GetFile((ClientData) 0, 0);
94 outFile = Tcl_GetFile((ClientData) 1, 0);
95 errFile = Tcl_GetFile((ClientData) 2, 0);
97 consoleChannel = Tcl_CreateChannel(&consoleChannelType, "console0",
98 inFile, NULL, (ClientData) NULL);
99 if (consoleChannel != NULL) {
100 Tcl_SetChannelOption(NULL, consoleChannel, "-translation", "lf");
101 Tcl_SetChannelOption(NULL, consoleChannel, "-buffering", "none");
103 Tcl_SetStdChannel(consoleChannel, TCL_STDIN);
104 consoleChannel = Tcl_CreateChannel(&consoleChannelType, "console1",
105 NULL, outFile, (ClientData) NULL);
106 if (consoleChannel != NULL) {
107 Tcl_SetChannelOption(NULL, consoleChannel, "-translation", "lf");
108 Tcl_SetChannelOption(NULL, consoleChannel, "-buffering", "none");
110 Tcl_SetStdChannel(consoleChannel, TCL_STDOUT);
111 consoleChannel = Tcl_CreateChannel(&consoleChannelType, "console2",
112 NULL, errFile, (ClientData) NULL);
113 if (consoleChannel != NULL) {
114 Tcl_SetChannelOption(NULL, consoleChannel, "-translation", "lf");
115 Tcl_SetChannelOption(NULL, consoleChannel, "-buffering", "none");
117 Tcl_SetStdChannel(consoleChannel, TCL_STDERR);
121 *----------------------------------------------------------------------
125 * Initialize the console. This code actually creates a new
126 * application and associated interpreter. This effectivly hides
127 * the implementation from the main application.
133 * A new console it created.
135 *----------------------------------------------------------------------
139 TkConsoleInit(interp)
140 Tcl_Interp *interp; /* Interpreter to use for prompting. */
142 Tcl_Interp *consoleInterp;
144 Tk_Window mainWindow = Tk_MainWindow(interp);
146 static char initCmd[] = "source -rsrc {Console}";
148 static char initCmd[] = "source $tk_library/console.tcl";
151 consoleInterp = Tcl_CreateInterp();
152 if (consoleInterp == NULL) {
157 * Initialized Tcl and Tk.
160 if (Tcl_Init(consoleInterp) != TCL_OK) {
163 if (Tk_Init(consoleInterp) != TCL_OK) {
166 gStdoutInterp = interp;
169 * Add console commands to the interp
171 info = (ConsoleInfo *) ckalloc(sizeof(ConsoleInfo));
172 info->interp = interp;
173 info->consoleInterp = consoleInterp;
174 Tcl_CreateCommand(interp, "console", ConsoleCmd, (ClientData) info,
175 (Tcl_CmdDeleteProc *) ConsoleDeleteProc);
176 Tcl_CreateCommand(consoleInterp, "interp", InterpreterCmd,
177 (ClientData) info, (Tcl_CmdDeleteProc *) NULL);
179 Tk_CreateEventHandler(mainWindow, StructureNotifyMask, ConsoleEventProc,
182 Tcl_Preserve((ClientData) consoleInterp);
183 if (Tcl_Eval(consoleInterp, initCmd) == TCL_ERROR) {
184 /* goto error; -- no problem for now... */
185 printf("Eval error: %s", consoleInterp->result);
187 Tcl_Release((ClientData) consoleInterp);
191 if (consoleInterp != NULL) {
192 Tcl_DeleteInterp(consoleInterp);
198 *----------------------------------------------------------------------
202 * Writes the given output on the IO channel. Returns count of how
203 * many characters were actually written, and an error indication.
206 * A count of how many characters were written is returned and an
207 * error indication is returned in an output argument.
210 * Writes output on the actual channel.
212 *----------------------------------------------------------------------
217 ConsoleOutput(instanceData, outFile, buf, toWrite, errorCode)
218 ClientData instanceData; /* Unused. */
219 Tcl_File outFile; /* Output device for channel. */
220 char *buf; /* The data buffer. */
221 int toWrite; /* How many bytes to write? */
222 int *errorCode; /* Where to store error code. */
227 if (gStdoutInterp != NULL) {
228 TkConsolePrint(gStdoutInterp, outFile, buf, toWrite);
235 *----------------------------------------------------------------------
239 * Read input from the console. Not currently implemented.
242 * Always returns EOF.
247 *----------------------------------------------------------------------
251 ConsoleInput(instanceData, inFile, buf, bufSize, errorCode)
252 ClientData instanceData; /* Unused. */
253 Tcl_File inFile; /* Input device for channel. */
254 char *buf; /* Where to store data read. */
255 int bufSize; /* How much space is available
257 int *errorCode; /* Where to store error code. */
259 return 0; /* Always return EOF. */
263 *----------------------------------------------------------------------
267 * Closes the IO channel.
270 * Always returns 0 (success).
273 * Frees the dummy file associated with the channel.
275 *----------------------------------------------------------------------
280 ConsoleClose(instanceData, interp, inFile, outFile)
281 ClientData instanceData; /* Unused. */
282 Tcl_Interp *interp; /* Unused. */
283 Tcl_File inFile; /* Input file to close. */
284 Tcl_File outFile; /* Output file to close. */
287 Tcl_FreeFile(inFile);
289 if (outFile && (outFile != inFile)) {
290 Tcl_FreeFile(outFile);
296 *----------------------------------------------------------------------
300 * The console command implements a Tcl interface to the various console
309 *----------------------------------------------------------------------
313 ConsoleCmd(clientData, interp, argc, argv)
314 ClientData clientData; /* Not used. */
315 Tcl_Interp *interp; /* Current interpreter. */
316 int argc; /* Number of arguments. */
317 char **argv; /* Argument strings. */
319 ConsoleInfo *info = (ConsoleInfo *) clientData;
323 Tcl_Interp *consoleInterp;
326 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
327 " option ?arg arg ...?\"", (char *) NULL);
332 length = strlen(argv[1]);
334 consoleInterp = info->consoleInterp;
335 Tcl_Preserve((ClientData) consoleInterp);
336 if ((c == 't') && (strncmp(argv[1], "title", length)) == 0) {
338 char *wmCmd = "wm title . {";
340 Tcl_DStringInit(&dString);
341 Tcl_DStringAppend(&dString, wmCmd, strlen(wmCmd));
342 Tcl_DStringAppend(&dString, argv[2], strlen(argv[2]));
343 Tcl_DStringAppend(&dString, "}", strlen("}"));
344 Tcl_Eval(consoleInterp, dString.string);
345 Tcl_DStringFree(&dString);
346 } else if ((c == 'h') && (strncmp(argv[1], "hide", length)) == 0) {
347 Tcl_Eval(info->consoleInterp, "wm withdraw .");
348 } else if ((c == 's') && (strncmp(argv[1], "show", length)) == 0) {
349 Tcl_Eval(info->consoleInterp, "wm deiconify .");
351 Tcl_AppendResult(interp, "bad option \"", argv[1],
352 "\": should be hide, show, or title",
356 Tcl_Release((ClientData) consoleInterp);
361 *----------------------------------------------------------------------
365 * This command allows the console interp to communicate with the
374 *----------------------------------------------------------------------
378 InterpreterCmd(clientData, interp, argc, argv)
379 ClientData clientData; /* Not used. */
380 Tcl_Interp *interp; /* Current interpreter. */
381 int argc; /* Number of arguments. */
382 char **argv; /* Argument strings. */
384 ConsoleInfo *info = (ConsoleInfo *) clientData;
388 Tcl_Interp *otherInterp;
391 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
392 " option ?arg arg ...?\"", (char *) NULL);
397 length = strlen(argv[1]);
399 otherInterp = info->interp;
400 Tcl_Preserve((ClientData) otherInterp);
401 if ((c == 'e') && (strncmp(argv[1], "eval", length)) == 0) {
402 result = Tcl_GlobalEval(otherInterp, argv[2]);
403 Tcl_AppendResult(interp, otherInterp->result, (char *) NULL);
404 } else if ((c == 'r') && (strncmp(argv[1], "record", length)) == 0) {
405 Tcl_RecordAndEval(otherInterp, argv[2], TCL_EVAL_GLOBAL);
407 Tcl_AppendResult(interp, otherInterp->result, (char *) NULL);
409 Tcl_AppendResult(interp, "bad option \"", argv[1],
410 "\": should be eval or record",
414 Tcl_Release((ClientData) otherInterp);
419 *----------------------------------------------------------------------
421 * ConsoleDeleteProc --
423 * If the console command is deleted we destroy the console window
424 * and all associated data structures.
430 * A new console it created.
432 *----------------------------------------------------------------------
436 ConsoleDeleteProc(clientData)
437 ClientData clientData;
439 ConsoleInfo *info = (ConsoleInfo *) clientData;
441 Tcl_DeleteInterp(info->consoleInterp);
442 info->consoleInterp = NULL;
446 *----------------------------------------------------------------------
448 * ConsoleEventProc --
450 * This event procedure is registered on the main window of the
451 * slave interpreter. If the user or a running script causes the
452 * main window to be destroyed, then we need to inform the console
453 * interpreter by invoking "tkConsoleExit".
459 * Invokes the "tkConsoleExit" procedure in the console interp.
461 *----------------------------------------------------------------------
465 ConsoleEventProc(clientData, eventPtr)
466 ClientData clientData;
469 ConsoleInfo *info = (ConsoleInfo *) clientData;
470 Tcl_Interp *consoleInterp;
472 if (eventPtr->type == DestroyNotify) {
473 consoleInterp = info->consoleInterp;
474 Tcl_Preserve((ClientData) consoleInterp);
475 Tcl_Eval(consoleInterp, "tkConsoleExit");
476 Tcl_Release((ClientData) consoleInterp);
481 *----------------------------------------------------------------------
485 * Prints to the give text to the console. Given the main interp
486 * this functions find the appropiate console interp and forwards
487 * the text to be added to that console.
495 *----------------------------------------------------------------------
499 TkConsolePrint(interp, outFile, buffer, size)
500 Tcl_Interp *interp; /* Main interpreter. */
501 Tcl_File outFile; /* Should be stdout or stderr. */
502 char *buffer; /* Text buffer. */
503 long size; /* Size of text buffer. */
505 Tcl_DString command, output;
509 Tcl_Interp *consoleInterp;
511 int fd = (int) Tcl_GetFileInfo(outFile, NULL);
513 if (interp == NULL) {
518 cmd = "tkConsoleOutput stderr ";
520 cmd = "tkConsoleOutput stdout ";
523 result = Tcl_GetCommandInfo(interp, "console", &cmdInfo);
527 info = (ConsoleInfo *) cmdInfo.clientData;
529 Tcl_DStringInit(&output);
530 Tcl_DStringAppend(&output, buffer, size);
532 Tcl_DStringInit(&command);
533 Tcl_DStringAppend(&command, cmd, strlen(cmd));
534 Tcl_DStringAppendElement(&command, output.string);
536 consoleInterp = info->consoleInterp;
537 Tcl_Preserve((ClientData) consoleInterp);
538 Tcl_Eval(consoleInterp, command.string);
539 Tcl_Release((ClientData) consoleInterp);
541 Tcl_DStringFree(&command);
542 Tcl_DStringFree(&output);