Re: [tcljava-dev] PATCH: Enhancement to Shell.java
Brought to you by:
mdejong
|
From: Shawn B. <sh...@qc...> - 2003-03-13 22:45:36
|
Mo,
Your recent email regarding Shell.java changes reminded me of my Shell
patch.
Any thoughts?
-Shawn
Shawn Boyce wrote:
> I have attached some enhancements I've made to Shell.java
> in the hope that they can be incorporated into the source tree.
> I've been using these changes in our Tcl-based simulators for
> awhile now.
>
> The purpose of these changes are to allow the Shell to
> be used by an application, but with
> * a custom command prompt instead of "% "
> * a custom interpreter,
> * support tcl_rcFileName
>
> The changes keep the same API with a new method
> called startShell
> public static void startShell( Interp interp, String[] args, String
> prompt );
>
> ChangeLog:
> 2002-06-12 Shawn Boyce <sh...@qc...>
> * src/jacl/tcl/lang/Shell.java
> new method startShell which accepts an Interp and prompt String
> main(): modified to use invoke the startShell method
> support tcl_rcFileName Tcl var by evaluating the named file if set
>
>
>------------------------------------------------------------------------
>
>Index: src/jacl/tcl/lang/Shell.java
>===================================================================
>RCS file: /cvsroot/tcljava/tcljava/src/jacl/tcl/lang/Shell.java,v
>retrieving revision 1.11
>diff -u -r1.11 Shell.java
>--- src/jacl/tcl/lang/Shell.java 12 Apr 2002 21:00:27 -0000 1.11
>+++ src/jacl/tcl/lang/Shell.java 12 Jun 2002 20:57:27 -0000
>@@ -48,13 +48,23 @@
> main(
> String args[]) // Array of command-line argument strings.
> {
>- String fileName = null;
>-
> // Create the interpreter. This will also create the built-in
> // Tcl commands.
>
> Interp interp = new Interp();
>
>+ startShell( interp, args, "% " );
>+}
>+
>+
>+public static void
>+startShell(
>+ Interp interp,
>+ String args[], // Array of command-line argument strings.
>+ String prompt )
>+{
>+ String fileName = null;
>+
> // Make command-line arguments available in the Tcl variables "argc"
> // and "argv". If the first argument doesn't start with a "-" then
> // strip it off and use it as the name of a script file to process.
>@@ -94,6 +104,36 @@
> // Normally we would do application specific initialization here.
> // However, that feature is not currently supported.
>
>+ // Source the RCfile
>+ try
>+ {
>+ TclObject rc_obj = interp.getVar("tcl_rcFileName", TCL.GLOBAL_ONLY);
>+ if ( rc_obj != null )
>+ {
>+ try
>+ {
>+ interp.evalFile( rc_obj.toString() );
>+ }
>+ catch ( TclException e )
>+ {
>+ int code = e.getCompletionCode();
>+ if (code == TCL.RETURN) {
>+ code = interp.updateReturnInfo();
>+ if (code != TCL.OK) {
>+ System.err.println("command returned bad code: " + code);
>+ }
>+ } else if (code == TCL.ERROR) {
>+ System.err.println(interp.getResult().toString());
>+ } else {
>+ System.err.println("command returned bad code: " + code);
>+ }
>+ }
>+ }
>+ }
>+ catch ( TclException ee )
>+ {
>+ }
>+
> // If a script file was specified then just source that file
> // and quit.
>
>@@ -131,7 +171,7 @@
> // We are running in interactive mode. Start the ConsoleThread
> // that loops, grabbing stdin and passing it to the interp.
>
>- ConsoleThread consoleThread = new ConsoleThread(interp);
>+ ConsoleThread consoleThread = new ConsoleThread(interp, prompt);
> consoleThread.setDaemon(true);
> consoleThread.start();
>
>@@ -180,6 +220,8 @@
> private Channel out;
> private Channel err;
>
>+protected String _prompt = "% ";
>+
> // set to true to get extra debug output
> private static final boolean debug = false;
>
>@@ -240,6 +282,16 @@
>
> out = TclIO.getStdChannel(StdChannel.STDOUT);
> err = TclIO.getStdChannel(StdChannel.STDERR);
>+
>+}
>+
>+
>+ConsoleThread(
>+ Interp i, // Initial value for interp.
>+ String prompt) // prompt to be used
>+{
>+ this( i );
>+ _prompt = prompt;
> }
>
> /*
>@@ -267,7 +319,7 @@
> }
>
>
>- put(out, "% ");
>+ put(out, _prompt);
>
> while (true) {
> // Loop forever to collect user inputs in a StringBuffer.
>@@ -383,10 +435,10 @@
> try {
> interp.eval(prompt.toString(), TCL.EVAL_GLOBAL);
> } catch (TclException e) {
>- put(out, "% ");
>+ put(out, _prompt);
> }
> } else {
>- put(out, "% ");
>+ put(out, _prompt);
> }
>
> return 1;
>
>
|