NO_BLOCK v1.0 -- simulate a non-blocking tclsh Brian Starr, bstarr@ics.uci.edu 5/25/95 *** Description: no_block is a package that simulates a non_blocking tclsh, so when it's backgrounded, it can continue to process data or handle events until the user chooses to interact with it again. Why would someone want to do this? For example, one could use the non-blocking interpreter to poll for user modifications to variables or procedures while continuously executing some process in the "background", such as filtering mail messages, etc. Whenever the user wants to change a variable, he can foreground the job, type in whatever command he likes, and background it again. In order to gain this functionality, all the tcl script need do is periodically call the interpreter polling command (see below), and possibly check the return code to detect end of file (cntrl-d typed by the user). Although this functionality is already available if you use TK, not everyone has X available all the time. Besides, the application may not need all the functionality that X provides. non-block is a cheap and easy way to add backgrounding capability to tcl. *** Availability no_block is available via anonymous ftp in the tcl archive, or from its developers: Brian Starr, bstarr@ics.uci.edu -or- Mark Ackerman, ackerman@ics.uci.edu *** Adding package to tclsh: Simply add the lines: if (no_block_Init(interp) == TCL_ERROR) { return TCL_ERROR; } To the Tcl_AppInit function in tclClientInit.c, and compile and link normally. The package assumes ANSI C, but can be converted to use an older version. *** Invoking no_block from a tcl script: To create a new non-blocking interpreter, use the following command: no_block This will add a polling command to your interpreter called , and return the name. For example: no_block pollcmd -> pollcmd This new command can later be invoked whenever you wish to poll for incoming user commands. For example: while {[pollcmd]} {do_something} Will execute do_something repeatedly, while at the same time executing any commands that the user types at his terminal. The while loop will terminate when the user types a cntrl-d at his terminal. *** Controlling the tcl prompt: As with tclsh, an appropriate prompt will be displayed each time the user types a carriage return. If the "background" process generates screen output, it may be desirable to regenerate the prompt. The no_block poll command has an optional parameter to redisplay the prompt when needed. For example: while {[pollcmd $new_prompt]} {set new_prompt [do_something]} Where do_something returns a "1" if the prompt should be redisplayed, and a "0" otherwise. The prompt will still be redisplayed if the user types a carriage return, however. *** Creating a poll command via C: A new poll command can be created via C by invoking: int no_block_CreateInput(Tcl_Interp*, char*, FILE*) This function takes the interpreter, the poll command name, and the input file as parameters, and returns a tcl error code. Example: if (no_block_CreateInput(ti,"pollcmd",stdin)==TCL_ERROR) { return TCL_ERROR; } else { .... Creating a poll command in C has the advantage that the input file does not have to be stdin. *** Contacting the developers, reporting problems, etc.: This code was created as part of the CafeCK project at UC, Irvine. Please direct any questions or comments to: Brian Starr, bstarr@ics.uci.edu