Debian Codegraph @

pTk/mTk/tixWin/tkConsole80.c

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