2009-06-11

Auto-execute shell built-ins or executables in Tcl interactive mode

When I was comparing my customized Tcl line editing environment with the one provided by Tcl_Main(), I figured that one thing Tcl_Main has and my implementation hasn't is the auto-execute shell built-ins or executables. We both are calling Tcl_EvalObjEx() to evaluate commands and same commands are passed into this function on both sides. Why does my environment behave differently? After minutes of scratching heads and searching over Tcl source code, I noticed the Tcl command unknown defined in init.tcl that gets called after the command being executed is identified as non-existed in Tcl interpreter. It tries to run non-existed commands in normal shell if Tcl is at interactive mode. Tcl knows itself is in interactive mode by checking the value of a boolean variable tcl_interactive and the value of this variable is linked to the result of C call isatty(0). If you are customizing your own version of Tcl editing environment and want to execute shell built-ins automatically just like me, you probably need to tell Tcl about this interactive stuff by isatty(0).