An Introduction To Embedded Tk (page 20 of 32)

[Previous Page][Next Page][Table of Contents]

10 Reading From Standard Input

There's one last feature of ET that we haven't discussed: the Et_ReadStdin() procedure. If this procedure is called (with no arguments) in between the calls to Et_Init() and Et_MainLoop(), ET will make arrangements to read all data that appears on standard input and interpret that data as Tcl/Tk commands.

You can use the Et_ReadStdin() to implement the interactive wish interpreter for Tcl/Tk. The code would look like this:

  main(int argc, char **argv){
    Et_Init(&argc,argv);
    Et_ReadStdin();
    Et_MainLoop();
  }
Let's call this program etwish in order to distinguish it from the standard wish that comes with Tcl/tk. The etwish program differs from wish in two ways. First, wish reads a set of 15 or so Tcl/Tk scripts from a well-known directory when it first starts up. Thus, to install wish, you have to have both the wish executable and the 15 startup scripts. But with etwish, the 15 startup scripts are compiled into the executable (using ET_INCLUDE() statements inside the Et_Init() function) so the external scripts are no longer required. This does make the etwish executable slightly larger (by about 64K bytes), but it also makes the program much easier to install and administer.

The second difference between wish and the etwish program shown above is that etwish is always interactive. It will not read a script from a file given as a command line argument like standard wish will. But we can remove that difference using a little more code.

  main(int argc, char **argv){
    Et_Init(&argc,argv);
    if( argc>2 && (strcmp(argv[1],"-f")==0 || strcmp(argv[1],"-file")==0) ){
      ET( source "%q(argv[2])" );
    }else if( argc>1 ){
      ET( source "%q(argv[1])" );
    }else{
      Et_ReadStdin();
    }
    Et_MainLoop();
  }
This revised program serves as a great template for building customized editions of wish that have one or more new Tcl/Tk commands written in C. All you have to do is code the new commands using the ET_PROC() mechanism and insert a single ET_INSTALL_COMMANDS statement right after the Et_Init().

[Next Page][Table of Contents]