001: /*
002: * Copyright (c) 2005 Advanced Micro Devices, Inc.
003: *
004: * See the file "license.amd" for information on usage and
005: * redistribution of this file, and for a DISCLAIMER OF ALL
006: * WARRANTIES.
007: *
008: * RCS: @(#) $Id: TJCShell.java,v 1.2 2006/01/24 07:55:45 mdejong Exp $
009: *
010: */
011:
012: // This class implements the start up shell for the TJC compiler.
013: package tcl.lang;
014:
015: import java.util.*;
016: import java.io.*;
017:
018: public class TJCShell {
019:
020: /*
021: *----------------------------------------------------------------------
022: *
023: * main --
024: *
025: * Main program for tclsh and most other Tcl-based applications.
026: *
027: * Results:
028: * None.
029: *
030: * Side effects:
031: * This procedure initializes the Tcl world and then starts
032: * interpreting commands; almost anything could happen, depending
033: * on the script being interpreted.
034: *
035: *----------------------------------------------------------------------
036: */
037:
038: public static void main(String[] args) // Array of command-line argument strings.
039: {
040: String fileName = "resource:/tjc/library/tjc.tcl";
041: int startIndex = 0;
042:
043: // Create the interpreter. This will also create the built-in
044: // Tcl commands.
045:
046: Interp interp = new Interp();
047:
048: // Check for -shell or -s, this means the user wanted to start
049: // a TJC enabled shell instead of running the tjc program.
050:
051: if ((args.length > 0)
052: && (args[0].equals("-s") || args[0].equals("-shell"))) {
053: startIndex = 1;
054: }
055:
056: TclObject argv = TclList.newInstance();
057: argv.preserve();
058: try {
059: if (startIndex == 1) {
060: // Invoke tjc as Tcl shell with TJC commands.
061: interp.setVar("argv0", "tjc", TCL.GLOBAL_ONLY);
062: interp.setVar("tcl_interactive", "1", TCL.GLOBAL_ONLY);
063: } else {
064: // Invoke tjc as standalone executable
065: interp.setVar("argv0", "tjc.tcl", TCL.GLOBAL_ONLY);
066: interp.setVar("tcl_interactive", "0", TCL.GLOBAL_ONLY);
067: }
068: for (int i = startIndex; i < args.length; i++) {
069: TclList.append(interp, argv, TclString
070: .newInstance(args[i]));
071: }
072: interp.setVar("argv", argv, TCL.GLOBAL_ONLY);
073: interp.setVar("argc", TclInteger.newInstance(TclList
074: .getLength(interp, argv)), TCL.GLOBAL_ONLY);
075: } catch (TclException e) {
076: throw new TclRuntimeError("unexpected TclException: " + e);
077: } finally {
078: argv.release();
079: }
080:
081: // If a script file was specified then just source that file
082: // and quit. Load the TJC runtime support package in case
083: // the tjc.tcl to be sourced was itself compiled with TJC.
084: // We could load via TJC::package but this works just fine.
085:
086: if (fileName != null) {
087: int exitCode = 0;
088: try {
089: interp.eval("package require TJC");
090: interp.eval("source " + fileName);
091: } catch (TclException e) {
092: int code = e.getCompletionCode();
093: if (code == TCL.RETURN) {
094: code = interp.updateReturnInfo();
095: if (code != TCL.OK) {
096: System.err
097: .println("command returned bad code: "
098: + code);
099: exitCode = 2;
100: }
101: } else if (code == TCL.ERROR) {
102: System.err.println(interp.getResult().toString());
103: exitCode = 1;
104: } else {
105: System.err.println("command returned bad code: "
106: + code);
107: exitCode = 2;
108: }
109: }
110:
111: if (startIndex == 0) {
112: // Note that if the above interp.evalFile() returns the main
113: // thread will exit. This may bring down the VM and stop
114: // the execution of Tcl.
115: //
116: // If the script needs to handle events, it must call
117: // vwait or do something similar.
118: //
119: // Note that the script can create AWT widgets. This will
120: // start an AWT event handling thread and keep the VM up. However,
121: // the interpreter thread (the same as the main thread) would
122: // have exited and no Tcl scripts can be executed.
123:
124: interp.dispose();
125: System.exit(exitCode);
126: }
127: }
128:
129: if (startIndex == 1) {
130: // We are running in interactive mode. Start the ConsoleThread
131: // that loops, grabbing stdin and passing it to the interp.
132:
133: ConsoleThread consoleThread = new ConsoleThread(interp);
134: consoleThread.setDaemon(true);
135: consoleThread.start();
136:
137: // Loop forever to handle user input events in the command line.
138:
139: Notifier notifier = interp.getNotifier();
140: while (true) {
141: // process events until "exit" is called.
142:
143: notifier.doOneEvent(TCL.ALL_EVENTS);
144: }
145: }
146: }
147: } // end class TJCShell
|