001: /*=============================================================================
002: * Copyright Texas Instruments 2000. All Rights Reserved.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * $ProjectHeader: OSCRIPT 0.155 Fri, 20 Dec 2002 18:34:22 -0800 rclark $
019: */
020:
021: package oscript;
022:
023: import oscript.data.OArray;
024: import oscript.data.OString;
025: import oscript.data.Value;
026: import oscript.data.JavaBridge;
027: import oscript.fs.AbstractFile;
028: import oscript.util.InputStreamFile;
029:
030: /**
031: * An interactive oscript interpreter. This basically creates an instance
032: * of <code>OscriptInterpreter</code> and then enters a read-eval-print
033: * loop, or reads and evalates a file.
034: *
035: * @author Rob Clark (rob@ti.com)
036: * <!--$Format: " * @version $Revision$"$-->
037: * @version 1.21
038: */
039: public class Main extends OscriptInterpreter {
040: private static void usage(int rc) {
041: System.err.println("usage:");
042: System.err
043: .println(" oscript [-d | --dump] [-i | --interactive] [-v | --version] [(-b | --breakpoint) file line] [file]* -- [script-args]");
044: System.exit(rc);
045: }
046:
047: public static void main(String[] args) throws java.io.IOException {
048: try {
049: OArray filenames = new OArray();
050: boolean interactive = false;
051: boolean dump = false;
052: boolean translatedDump = false;
053:
054: // default to "-i" if no args:
055: if (args.length == 0)
056: args = new String[] { "-i" };
057:
058: int i;
059:
060: for (i = 0; i < args.length; i++) {
061: if (args[i].equals("-d") || args[i].equals("--dump")) {
062: dump = true;
063: } else if (args[i].equals("-t")
064: || args[i].equals("--translated-dump")) {
065: translatedDump = true;
066: } else if (args[i].equals("-i")
067: || args[i].equals("--interactive")) {
068: interactive = true;
069: } else if (args[i].equals("-v")
070: || args[i].equals("--version")) {
071: System.out.println(getVersionString());
072: System.exit(0);
073: } else if (args[i].equals("-h")
074: || args[i].equals("--help")) {
075: usage(0);
076: } else if (args[i].equals("-b")
077: || args[i].equals("--breakpoint")) {
078: if ((i + 2) >= args.length)
079: usage(1);
080:
081: AbstractFile file = resolve(args[++i], false);
082: int line = Integer.decode(args[++i]).intValue();
083: if (file.exists())
084: eval("pkg.dbg.setBreakpoint;")
085: .callAsFunction(
086: new Value[] {
087: JavaBridge
088: .convertToScriptObject(file
089: .getPath()),
090: JavaBridge
091: .convertToScriptObject(line) });
092: } else if (args[i].equals("--")) {
093: i++;
094: break;
095: } else {
096: filenames.push1(new OString(args[i]));
097: }
098: }
099:
100: oscript.data.OArray scriptArgs = new oscript.data.OArray();
101:
102: for (int j = 0; i < args.length; i++, j++)
103: scriptArgs.elementAt(j).opAssign(
104: oscript.data.JavaBridge
105: .convertToScriptObject(args[i]));
106:
107: getGlobalScope().createMember("args", 0).opAssign(
108: scriptArgs);
109:
110: try {
111: for (int j = 0; j < filenames.length(); j++) {
112: String path = filenames.elementAt(j).castToString();
113: AbstractFile file = path.equals("-") ? new InputStreamFile(
114: System.in, "stdin")
115: : resolve(path, false);
116:
117: if (dump)
118: parse(file).accept(
119: new oscript.visitor.TreeDumper(
120: OscriptBuiltins.getOut()));
121: else
122: eval(file);
123: }
124:
125: if (interactive) {
126: java.io.BufferedReader br = new java.io.BufferedReader(
127: new java.io.InputStreamReader(System.in));
128: java.io.PrintWriter pw = new java.io.PrintWriter(
129: new java.io.OutputStreamWriter(System.out));
130:
131: Shell shell;
132:
133: pw.println(getVersionString());
134:
135: if (dump) {
136: shell = new Shell(br, pw) {
137: public oscript.data.Value evalStr(
138: String line)
139: throws oscript.parser.ParseException,
140: oscript.exceptions.PackagedScriptObjectException {
141: parse(line).accept(
142: new oscript.visitor.TreeDumper(
143: System.out));
144: return null;
145: }
146: };
147: } else if (translatedDump) {
148: shell = new Shell(br, pw) {
149: public oscript.data.Value evalStr(
150: String line)
151: throws oscript.parser.ParseException,
152: oscript.exceptions.PackagedScriptObjectException {
153: parse(line)
154: .accept(
155: new oscript.visitor.TranslatedTreeDumper(
156: System.out));
157: return null;
158: }
159: };
160: } else {
161: shell = new Shell(br, pw);
162: }
163:
164: shell.run();
165: }
166: } catch (oscript.exceptions.PackagedScriptObjectException e) {
167: System.out.println(e.val.castToString());
168: OscriptBuiltins.exit(-1);
169: } catch (oscript.parser.ParseException e) {
170: System.out.println(e.getMessage());
171: OscriptBuiltins.exit(-1);
172: }
173: } catch (java.io.EOFException e) {
174: System.out.println(e.getMessage());
175: OscriptBuiltins.exit(-1);
176: } catch (Exception e) {
177: e.printStackTrace();
178: OscriptBuiltins.exit(-1);
179: }
180:
181: OscriptBuiltins.exit(0);
182: }
183: }
184:
185: /*
186: * Local Variables:
187: * tab-width: 2
188: * indent-tabs-mode: nil
189: * mode: java
190: * c-indentation-style: java
191: * c-basic-offset: 2
192: * eval: (c-set-offset 'substatement-open '0)
193: * eval: (c-set-offset 'case-label '+)
194: * eval: (c-set-offset 'inclass '+)
195: * eval: (c-set-offset 'inline-open '0)
196: * End:
197: */
|