/*1* tkConsole.c --2*3* This file implements a Tcl console for systems that may not4* otherwise have access to a console. It uses the Text widget5* and provides special access via a console command.6*7* Copyright (c) 1995-1996 Sun Microsystems, Inc.8*9* See the file "license.terms" for information on usage and redistribution10* of this file, and for a DISCLAIMER OF ALL WARRANTIES.11*12* SCCS: @(#) tkConsole.c 1.43 96/08/26 19:42:5113*/1415#include "tkInt.h"1617/*18* A data structure of the following type holds information for each console19* which a handler (i.e. a Tcl command) has been defined for a particular20* top-level window.21*/2223typedef struct ConsoleInfo {24Tcl_Interp *consoleInterp; /* Interpreter for the console. */25Tcl_Interp *interp; /* Interpreter to send console commands. */26} ConsoleInfo;2728static Tcl_Interp *gStdoutInterp = NULL;2930/*31* Forward declarations for procedures defined later in this file:32*/3334static int ConsoleCmd _ANSI_ARGS_((ClientData clientData,35Tcl_Interp *interp, int argc, char **argv));36static void ConsoleDeleteProc _ANSI_ARGS_((ClientData clientData));37static void ConsoleEventProc _ANSI_ARGS_((ClientData clientData,38XEvent *eventPtr));39static int InterpreterCmd _ANSI_ARGS_((ClientData clientData,40Tcl_Interp *interp, int argc, char **argv));4142static int ConsoleInput _ANSI_ARGS_((ClientData instanceData,43char *buf, int toRead, int *errorCode));44static int ConsoleOutput _ANSI_ARGS_((ClientData instanceData,45char *buf, int toWrite, int *errorCode));46static int ConsoleClose _ANSI_ARGS_((ClientData instanceData,47Tcl_Interp *interp));48static void ConsoleWatch _ANSI_ARGS_((ClientData instanceData,49int mask));50static int ConsoleReady _ANSI_ARGS_((ClientData instanceData,51int mask));52static Tcl_File ConsoleFile _ANSI_ARGS_((ClientData instanceData,53int direction));5455/*56* This structure describes the channel type structure for file based IO:57*/5859static Tcl_ChannelType consoleChannelType = {60"console", /* Type name. */61NULL, /* Always non-blocking.*/62ConsoleClose, /* Close proc. */63ConsoleInput, /* Input proc. */64ConsoleOutput, /* Output proc. */65NULL, /* Seek proc. */66NULL, /* Set option proc. */67NULL, /* Get option proc. */68ConsoleWatch, /* Watch for events on console. */69ConsoleReady, /* Are events present? */70ConsoleFile, /* Get a Tcl_File from the device. */71};7273/*74*----------------------------------------------------------------------75*76* TkConsoleCreate --77*78* Create the console channels and install them as the standard79* channels. All I/O will be discarded until TkConsoleInit is80* called to attach the console to a text widget.81*82* Results:83* None.84*85* Side effects:86* Creates the console channel and installs it as the standard87* channels.88*89*----------------------------------------------------------------------90*/9192void93TkConsoleCreate()94{95Tcl_Channel consoleChannel;9697consoleChannel = Tcl_CreateChannel(&consoleChannelType, "console0",98(ClientData) TCL_STDIN, TCL_READABLE);99if (consoleChannel != NULL) {100Tcl_SetChannelOption(NULL, consoleChannel, "-translation", "lf");101Tcl_SetChannelOption(NULL, consoleChannel, "-buffering", "none");102}103Tcl_SetStdChannel(consoleChannel, TCL_STDIN);104consoleChannel = Tcl_CreateChannel(&consoleChannelType, "console1",105(ClientData) TCL_STDOUT, TCL_WRITABLE);106if (consoleChannel != NULL) {107Tcl_SetChannelOption(NULL, consoleChannel, "-translation", "lf");108Tcl_SetChannelOption(NULL, consoleChannel, "-buffering", "none");109}110Tcl_SetStdChannel(consoleChannel, TCL_STDOUT);111consoleChannel = Tcl_CreateChannel(&consoleChannelType, "console2",112(ClientData) TCL_STDERR, TCL_WRITABLE);113if (consoleChannel != NULL) {114Tcl_SetChannelOption(NULL, consoleChannel, "-translation", "lf");115Tcl_SetChannelOption(NULL, consoleChannel, "-buffering", "none");116}117Tcl_SetStdChannel(consoleChannel, TCL_STDERR);118}119120/*121*----------------------------------------------------------------------122*123* TkConsoleInit --124*125* Initialize the console. This code actually creates a new126* application and associated interpreter. This effectivly hides127* the implementation from the main application.128*129* Results:130* None.131*132* Side effects:133* A new console it created.134*135*----------------------------------------------------------------------136*/137138int139TkConsoleInit(interp)140Tcl_Interp *interp; /* Interpreter to use for prompting. */141{142Tcl_Interp *consoleInterp;143ConsoleInfo *info;144Tk_Window mainWindow = Tk_MainWindow(interp);145#ifdef MAC_TCL146static char initCmd[] = "source -rsrc {Console}";147#else148static char initCmd[] = "source $tk_library/console.tcl";149#endif150151consoleInterp = Tcl_CreateInterp();152if (consoleInterp == NULL) {153goto error;154}155156/*157* Initialized Tcl and Tk.158*/159160if (Tcl_Init(consoleInterp) != TCL_OK) {161goto error;162}163if (Tk_Init(consoleInterp) != TCL_OK) {164goto error;165}166gStdoutInterp = interp;167168/*169* Add console commands to the interp170*/171info = (ConsoleInfo *) ckalloc(sizeof(ConsoleInfo));172info->interp = interp;173info->consoleInterp = consoleInterp;174Tcl_CreateCommand(interp, "console", ConsoleCmd, (ClientData) info,175(Tcl_CmdDeleteProc *) ConsoleDeleteProc);176Tcl_CreateCommand(consoleInterp, "interp", InterpreterCmd,177(ClientData) info, (Tcl_CmdDeleteProc *) NULL);178179Tk_CreateEventHandler(mainWindow, StructureNotifyMask, ConsoleEventProc,180(ClientData) info);181182Tcl_Preserve((ClientData) consoleInterp);183if (Tcl_Eval(consoleInterp, initCmd) == TCL_ERROR) {184/* goto error; -- no problem for now... */185printf("Eval error: %s", consoleInterp->result);186}187Tcl_Release((ClientData) consoleInterp);188return TCL_OK;189190error:191if (consoleInterp != NULL) {192Tcl_DeleteInterp(consoleInterp);193}194return TCL_ERROR;195}196197/*198*----------------------------------------------------------------------199*200* ConsoleOutput--201*202* Writes the given output on the IO channel. Returns count of how203* many characters were actually written, and an error indication.204*205* Results:206* A count of how many characters were written is returned and an207* error indication is returned in an output argument.208*209* Side effects:210* Writes output on the actual channel.211*212*----------------------------------------------------------------------213*/214215static int216ConsoleOutput(instanceData, buf, toWrite, errorCode)217ClientData instanceData; /* Indicates which device to use. */218char *buf; /* The data buffer. */219int toWrite; /* How many bytes to write? */220int *errorCode; /* Where to store error code. */221{222*errorCode = 0;223Tcl_SetErrno(0);224225if (gStdoutInterp != NULL) {226TkConsolePrint(gStdoutInterp, (int) instanceData, buf, toWrite);227}228229return toWrite;230}231232/*233*----------------------------------------------------------------------234*235* ConsoleInput --236*237* Read input from the console. Not currently implemented.238*239* Results:240* Always returns EOF.241*242* Side effects:243* None.244*245*----------------------------------------------------------------------246*/247248/* ARGSUSED */249static int250ConsoleInput(instanceData, buf, bufSize, errorCode)251ClientData instanceData; /* Unused. */252char *buf; /* Where to store data read. */253int bufSize; /* How much space is available254* in the buffer? */255int *errorCode; /* Where to store error code. */256{257return 0; /* Always return EOF. */258}259260/*261*----------------------------------------------------------------------262*263* ConsoleClose --264*265* Closes the IO channel.266*267* Results:268* Always returns 0 (success).269*270* Side effects:271* Frees the dummy file associated with the channel.272*273*----------------------------------------------------------------------274*/275276/* ARGSUSED */277static int278ConsoleClose(instanceData, interp)279ClientData instanceData; /* Unused. */280Tcl_Interp *interp; /* Unused. */281{282return 0;283}284285/*286*----------------------------------------------------------------------287*288* ConsoleWatch --289*290* Called by the notifier to set up the console device so that291* events will be noticed. Since there are no events on the292* console, this routine just returns without doing anything.293*294* Results:295* None.296*297* Side effects:298* None.299*300*----------------------------------------------------------------------301*/302303/* ARGSUSED */304static void305ConsoleWatch(instanceData, mask)306ClientData instanceData; /* Device ID for the channel. */307int mask; /* OR-ed combination of308* TCL_READABLE, TCL_WRITABLE and309* TCL_EXCEPTION, for the events310* we are interested in. */311{312}313314/*315*----------------------------------------------------------------------316*317* ConsoleReady --318*319* Invoked by the notifier to notice whether any events are present320* on the console. Since there are no events on the console, this321* routine always returns zero.322*323* Results:324* Always 0.325*326* Side effects:327* None.328*329*----------------------------------------------------------------------330*/331332/* ARGSUSED */333static int334ConsoleReady(instanceData, mask)335ClientData instanceData; /* Device ID for the channel. */336int mask; /* OR-ed combination of337* TCL_READABLE, TCL_WRITABLE and338* TCL_EXCEPTION, for the events339* we are interested in. */340{341return 0;342}343344/*345*----------------------------------------------------------------------346*347* ConsoleFile --348*349* Invoked by the generic IO layer to get a Tcl_File from a channel.350* Because console channels do not use Tcl_Files, this function always351* returns NULL.352*353* Results:354* Always NULL.355*356* Side effects:357* None.358*359*----------------------------------------------------------------------360*/361362/* ARGSUSED */363static Tcl_File364ConsoleFile(instanceData, direction)365ClientData instanceData; /* Device ID for the channel. */366int direction; /* TCL_READABLE or TCL_WRITABLE367* to indicate which direction of368* the channel is being requested. */369{370return (Tcl_File) NULL;371}372373/*374*----------------------------------------------------------------------375*376* ConsoleCmd --377*378* The console command implements a Tcl interface to the various console379* options.380*381* Results:382* None.383*384* Side effects:385* None.386*387*----------------------------------------------------------------------388*/389390static int391ConsoleCmd(clientData, interp, argc, argv)392ClientData clientData; /* Not used. */393Tcl_Interp *interp; /* Current interpreter. */394int argc; /* Number of arguments. */395char **argv; /* Argument strings. */396{397ConsoleInfo *info = (ConsoleInfo *) clientData;398char c;399int length;400int result;401Tcl_Interp *consoleInterp;402403if (argc < 2) {404Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],405" option ?arg arg ...?\"", (char *) NULL);406return TCL_ERROR;407}408409c = argv[1][0];410length = strlen(argv[1]);411result = TCL_OK;412consoleInterp = info->consoleInterp;413Tcl_Preserve((ClientData) consoleInterp);414if ((c == 't') && (strncmp(argv[1], "title", length)) == 0) {415Tcl_DString dString;416char *wmCmd = "wm title . {";417418Tcl_DStringInit(&dString);419Tcl_DStringAppend(&dString, wmCmd, strlen(wmCmd));420Tcl_DStringAppend(&dString, argv[2], strlen(argv[2]));421Tcl_DStringAppend(&dString, "}", strlen("}"));422Tcl_Eval(consoleInterp, dString.string);423Tcl_DStringFree(&dString);424} else if ((c == 'h') && (strncmp(argv[1], "hide", length)) == 0) {425Tcl_Eval(info->consoleInterp, "wm withdraw .");426} else if ((c == 's') && (strncmp(argv[1], "show", length)) == 0) {427Tcl_Eval(info->consoleInterp, "wm deiconify .");428} else if ((c == 'e') && (strncmp(argv[1], "eval", length)) == 0) {429Tcl_Eval(info->consoleInterp, argv[2]);430} else {431Tcl_AppendResult(interp, "bad option \"", argv[1],432"\": should be hide, show, or title",433(char *) NULL);434result = TCL_ERROR;435}436Tcl_Release((ClientData) consoleInterp);437return result;438}439440/*441*----------------------------------------------------------------------442*443* InterpreterCmd --444*445* This command allows the console interp to communicate with the446* main interpreter.447*448* Results:449* None.450*451* Side effects:452* None.453*454*----------------------------------------------------------------------455*/456457static int458InterpreterCmd(clientData, interp, argc, argv)459ClientData clientData; /* Not used. */460Tcl_Interp *interp; /* Current interpreter. */461int argc; /* Number of arguments. */462char **argv; /* Argument strings. */463{464ConsoleInfo *info = (ConsoleInfo *) clientData;465char c;466int length;467int result;468Tcl_Interp *otherInterp;469470if (argc < 2) {471Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],472" option ?arg arg ...?\"", (char *) NULL);473return TCL_ERROR;474}475476c = argv[1][0];477length = strlen(argv[1]);478result = TCL_OK;479otherInterp = info->interp;480Tcl_Preserve((ClientData) otherInterp);481if ((c == 'e') && (strncmp(argv[1], "eval", length)) == 0) {482result = Tcl_GlobalEval(otherInterp, argv[2]);483Tcl_AppendResult(interp, otherInterp->result, (char *) NULL);484} else if ((c == 'r') && (strncmp(argv[1], "record", length)) == 0) {485Tcl_RecordAndEval(otherInterp, argv[2], TCL_EVAL_GLOBAL);486result = TCL_OK;487Tcl_AppendResult(interp, otherInterp->result, (char *) NULL);488} else {489Tcl_AppendResult(interp, "bad option \"", argv[1],490"\": should be eval or record",491(char *) NULL);492result = TCL_ERROR;493}494Tcl_Release((ClientData) otherInterp);495return result;496}497498/*499*----------------------------------------------------------------------500*501* ConsoleDeleteProc --502*503* If the console command is deleted we destroy the console window504* and all associated data structures.505*506* Results:507* None.508*509* Side effects:510* A new console it created.511*512*----------------------------------------------------------------------513*/514515static void516ConsoleDeleteProc(clientData)517ClientData clientData;518{519ConsoleInfo *info = (ConsoleInfo *) clientData;520521Tcl_DeleteInterp(info->consoleInterp);522info->consoleInterp = NULL;523}524525/*526*----------------------------------------------------------------------527*528* ConsoleEventProc --529*530* This event procedure is registered on the main window of the531* slave interpreter. If the user or a running script causes the532* main window to be destroyed, then we need to inform the console533* interpreter by invoking "tkConsoleExit".534*535* Results:536* None.537*538* Side effects:539* Invokes the "tkConsoleExit" procedure in the console interp.540*541*----------------------------------------------------------------------542*/543544static void545ConsoleEventProc(clientData, eventPtr)546ClientData clientData;547XEvent *eventPtr;548{549ConsoleInfo *info = (ConsoleInfo *) clientData;550Tcl_Interp *consoleInterp;551552if (eventPtr->type == DestroyNotify) {553consoleInterp = info->consoleInterp;554Tcl_Preserve((ClientData) consoleInterp);555Tcl_Eval(consoleInterp, "tkConsoleExit");556Tcl_Release((ClientData) consoleInterp);557}558}559560/*561*----------------------------------------------------------------------562*563* TkConsolePrint --564*565* Prints to the give text to the console. Given the main interp566* this functions find the appropiate console interp and forwards567* the text to be added to that console.568*569* Results:570* None.571*572* Side effects:573* None.574*575*----------------------------------------------------------------------576*/577578void579TkConsolePrint(interp, devId, buffer, size)580Tcl_Interp *interp; /* Main interpreter. */581int devId; /* TCL_STDOUT for stdout, TCL_STDERR for582* stderr. */583char *buffer; /* Text buffer. */584long size; /* Size of text buffer. */585{586Tcl_DString command, output;587Tcl_CmdInfo cmdInfo;588char *cmd;589ConsoleInfo *info;590Tcl_Interp *consoleInterp;591int result;592593if (interp == NULL) {594return;595}596597if (devId == TCL_STDERR) {598cmd = "tkConsoleOutput stderr ";599} else {600cmd = "tkConsoleOutput stdout ";601}602603result = Tcl_GetCommandInfo(interp, "console", &cmdInfo);604if (result == 0) {605return;606}607info = (ConsoleInfo *) cmdInfo.clientData;608609Tcl_DStringInit(&output);610Tcl_DStringAppend(&output, buffer, size);611612Tcl_DStringInit(&command);613Tcl_DStringAppend(&command, cmd, strlen(cmd));614Tcl_DStringAppendElement(&command, output.string);615616consoleInterp = info->consoleInterp;617Tcl_Preserve((ClientData) consoleInterp);618Tcl_Eval(consoleInterp, command.string);619Tcl_Release((ClientData) consoleInterp);620621Tcl_DStringFree(&command);622Tcl_DStringFree(&output);623}624625626