简体   繁体   中英

How to override the default init.tcl

I'm working on a project where I want to make use of TCL as the command interpreter. I have a working c library object which I can load from within the tcl shell but my problem is finding a way to automatically do this while starting a tclsh.

Essentially my ultimate goal is to be able to run a script and have it load my library and run some initial startup tcl code before dropping me back to the tclsh command prompt in interactive mode.

eg

tclsh -f myscript.tcl --then-switch-to-interactive

or

EXPORT TCLINIT=myscript.tcl 
tclsh 

The basic goal is to avoid having to distribute tclsh but rather rely in local user installations of tcl. All I would like to distribute is my library, a startup script and a shell command to launch the tclsh with the library preloaded.

I've tried using the environment variables TCLINIT and TCL_LIBRARY but they seem to have no effect.

The only workable solutions I've found so far are to add "source myscript.tcl" to either the end of /usr/share/tcltk/tcl8.5.init.tcl or ~/.tclshrc

However both of these "solutions" are non perfect as they require modification of the default users workspace.

It strikes me that there must be a way to handle this in TCL, but my research so far hasn't yielded anything.

Does anyone have any suggestions?

Tcl does not provide any ability to replace init.tcl ; it's used to set up parts of the language before user or library code is supposed to run. The only time it gets adjusted is in packaging systems like tclkit (which does extra work in there to initialize the internal virtual filesystem).

I also don't advise relying on the user to edit their ~/.tclshrc , as that's theirs and not yours. It's also never sourced when running non-interactively (well, not unless you add in code to actually read it).

Instead, I advise that you consider:

  • Distributing/installing your library as a Tcl package, so code that wants it can just do:

     package require itsname 

    Like that, code that wants it can just declare the fact and code that doesn't is unaffected.

  • Distributing/installing your library with a script that acts as an interactive shell. The commandloop command from the TclX package may help here, but that Wiki page also has code for doing your own.

In myscript.tcl:

package require Tclx
# Your script
commandloop; # Start interactive session
# other commands

The commandloop command will create an interactive command loop. At this point, if the user type exit , then the script will exit without executing the other commands that follows the commandloop line. On the other hand, if the user types Crtl+D or Ctrl+Z to exit the commandloop , the other commands will be executed.

Reference for commandloop : http://wiki.tcl.tk/1968

Custom Prompt

If you don't like the default prompt, create a proc to return a custom prompt and pass it to commandloop :

proc customPrompt {} { return "tcl> " }
commandloop -prompt1 customPrompt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM