001: /*
002: * @(#)Main.java 1.2 05/06/21
003: *
004: * Copyright (c) 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package pnuts.tools;
010:
011: import java.io.BufferedReader;
012: import java.io.FileReader;
013: import java.io.IOException;
014: import java.io.InputStream;
015: import java.io.InputStreamReader;
016: import java.io.PrintWriter;
017: import java.io.Reader;
018: import java.net.URL;
019: import java.util.Properties;
020: import java.util.StringTokenizer;
021:
022: import javax.swing.JFrame;
023:
024: import pnuts.lang.Context;
025: import pnuts.lang.Pnuts;
026: import pnuts.lang.Runtime;
027:
028: //import pnuts.security.SecurePnutsImpl;
029:
030: /**
031: * The main class of "pnuts" command
032: */
033: public class Main {
034:
035: private final static String pnutsDebuggerProperty = "pnuts.debugger";
036: static String java_vendor = System.getProperty("java.vendor");
037: static String java_version = System.getProperty("java.version");
038:
039: static String version() {
040: try {
041: Class.forName("java.lang.Package");
042: java.lang.Package pkg = Pnuts.class.getPackage();
043: return pkg.getSpecificationVersion() + " ("
044: + pkg.getImplementationVersion() + ")";
045: } catch (ClassNotFoundException e) {
046: return Pnuts.pnuts_version;
047: }
048: }
049:
050: static void greeting(Context context) {
051: PrintWriter w = context.getTerminalWriter();
052: if (java_vendor == null) {
053: java_vendor = System.getProperty("java.vendor");
054: }
055: w.print("Pnuts version " + version() + ", " + java_version
056: + " (" + java_vendor + ")");
057: String vm_name = System.getProperty("java.vm.name");
058: String vm_info = System.getProperty("java.vm.info");
059: if (vm_name != null) {
060: w.print("\n" + vm_name);
061: if (vm_info != null) {
062: w.println(" (" + vm_info + ")");
063: } else {
064: w.println();
065: }
066: } else {
067: w.println();
068: }
069: }
070:
071: static void printHelp(String command) {
072: try {
073: InputStream in = Main.class.getResourceAsStream(command
074: + ".help");
075: Reader reader = new InputStreamReader(in, "UTF-8");
076: PrintWriter writer = new PrintWriter(System.err);
077: char[] buf = new char[512];
078: int n;
079: while ((n = reader.read(buf, 0, 512)) != -1) {
080: writer.write(buf, 0, n);
081: }
082: writer.flush();
083: } catch (IOException e) {
084: e.printStackTrace();
085: }
086: }
087:
088: /**
089: * starts the command shell interpreter
090: */
091: public static void main(String args[]) throws Throwable {
092: String param = null;
093:
094: Context context = null;
095: Properties defaultSettings = new Properties();
096:
097: boolean evaluated = false;
098: boolean interactive = false;
099: boolean quiet = false;
100: // String inputlog = null;
101: // ContextFactory contextFactory = null;
102:
103: String iparam = Runtime.getProperty("pnuts.interactive");
104: if (iparam != null) {
105: if ("false".equals(iparam)) {
106: quiet = true;
107: } else if ("true".equals(iparam)) {
108: interactive = true;
109: }
110: }
111:
112: boolean use_compiler = true;
113: boolean optimize = false;
114: // boolean debug_mode = false;
115: // boolean accessible = false;
116: // boolean publicAccess = false;
117: // boolean guiConsole = false;
118: // boolean sandbox = false;
119: boolean verbose = false;
120:
121: for (int i = 0; i < args.length; i++) {
122: String arg = args[i];
123: if ("-help".equals(arg)) {
124: printHelp("pnuts");
125: System.exit(0);
126: } else if ("-version".equals(arg)) {
127: System.err.println(version());
128: System.exit(0);
129: // } else if ("-vd".equals(arg)) {
130: // defaultSettings.setProperty(pnutsDebuggerProperty,
131: // "pnuts.tools.VisualDebugger");
132: // contextFactory = new VisualDebugger();
133: // use_compiler = false;
134: // debug_mode = true;
135: // } else if (arg.startsWith("-d")) {
136:
137: // String pnutsContextFactory = Runtime
138: // .getProperty(pnutsDebuggerProperty);
139: // if (pnutsContextFactory != null) {
140: // Class cls = Class.forName(pnutsContextFactory);
141: // contextFactory = (ContextFactory) cls.newInstance();//
142: // } else {
143: // if (arg.length() > 3 && arg.charAt(2) == ':') { // -d:file
144: // String debug_script = arg.substring(3);
145: // System.out.println("reading " + debug_script);
146: // Reader reader = new FileReader(debug_script);////
147: // contextFactory = new TerminalDebugger(reader);
148: // } else {
149: // contextFactory = new TerminalDebugger();
150: // }
151: // }
152: // use_compiler = false;
153: // debug_mode = true;
154: } else if ("-pure".equals(arg)) {
155: use_compiler = false;
156: } else if ("-O".equals(arg)) {
157: optimize = true;
158: // } else if ("-a".equals(arg)) {
159: // accessible = true;
160: } else if ("-b".equals(arg)) {
161: // default
162: // } else if ("-p".equals(arg)) {
163: // publicAccess = true;
164: } else if ("-v".equals(arg)) {
165: verbose = true;
166: // } else if ("-w".equals(arg)) {
167: // guiConsole = true;
168: // } else if ("-s".equals(arg)) {
169: // sandbox = true;
170: // } else if ("-inputlog".equals(arg)) {
171: } else if ("-a".equals(arg) || "-u".equals(arg)
172: || "-U".equals(arg) || "-f".equals(arg)
173: || "-F".equals(arg) || "-r".equals(arg)
174: || "-R".equals(arg) || "-e".equals(arg)
175: || "-m".equals(arg)) {
176: i++;
177: } else if (arg.startsWith("-J")) {
178: } else {
179: break;
180: }
181: }
182:
183: /*
184: * PnutsImpl
185: */
186: if (use_compiler) {
187: defaultSettings.setProperty("pnuts.lang.defaultPnutsImpl",
188: "pnuts.compiler.CompilerPnutsImpl");
189: // if (!accessible && !"false".equals(System.getProperty("pnuts.compiler.useDynamicProxy"))) {
190: if (!"false".equals(System
191: .getProperty("pnuts.compiler.useDynamicProxy"))) {
192: defaultSettings.setProperty(
193: "pnuts.compiler.useDynamicProxy", "true");
194: }
195: if (optimize) {
196: defaultSettings.setProperty("pnuts.compiler.optimize",
197: "true");
198: }
199: }
200:
201: if (verbose) {
202: defaultSettings.setProperty("pnuts.verbose", "true");
203: }
204:
205: Pnuts.setDefaults(defaultSettings);
206:
207: /*
208: * Debugger
209: */
210: // if (debug_mode) {
211: // context = contextFactory.createContext();
212: // } else {
213: context = new Context();
214: // }
215:
216: /*
217: * Configuration
218: */
219: // if (accessible) {
220: // context.setConfiguration(new pnuts.ext.NonPublicMemberAccessor());
221: // } else if (publicAccess) {
222: // context.setConfiguration(new pnuts.ext.PublicMemberAccessor());
223: // }
224: /*
225: * sandbox
226: */
227: // if (sandbox && Pnuts.isJava2()) {
228: // if (System.getSecurityManager() == null) {
229: // System.setSecurityManager(new SecurityManager());
230: // }
231: // context.setImplementation(new SecurePnutsImpl(context
232: // .getImplementation()));
233: // }
234: String module_property = Runtime
235: .getProperty("pnuts.tools.modules");
236: if (module_property != null) {
237: StringTokenizer stoken = new StringTokenizer(
238: module_property, ",");
239: while (stoken.hasMoreTokens()) {
240: context.usePackage(stoken.nextToken());
241: }
242: }
243:
244: for (int i = 0; i < args.length; i++) {
245:
246: try {
247: if ("-r".equals(args[i])) {
248: if (++i < args.length) {
249: Pnuts.load(args[i], (Context) context.clone());
250: continue;
251: } else {
252: break;
253: }
254: } else if ("-R".equals(args[i])) {
255: if (++i < args.length) {
256: Pnuts.load(args[i], (Context) context.clone());
257: evaluated = true;
258: continue;
259: } else {
260: break;
261: }
262: } else if ("-f".equals(args[i])) {
263: if (++i < args.length) {
264: Pnuts.loadFile(args[i], (Context) context
265: .clone());
266: continue;
267: } else {
268: break;
269: }
270: } else if ("-F".equals(args[i])) {
271: if (++i < args.length) {
272: Pnuts.loadFile(args[i], (Context) context
273: .clone());
274: evaluated = true;
275: continue;
276: } else {
277: break;
278: }
279: } else if ("-u".equals(args[i])) {
280: if (++i < args.length) {
281: Pnuts.load(new URL(args[i]), (Context) context
282: .clone());
283: continue;
284: } else {
285: break;
286: }
287: } else if ("-U".equals(args[i])) {
288: if (++i < args.length) {
289: Pnuts.load(new URL(args[i]), (Context) context
290: .clone());
291: evaluated = true;
292: continue;
293: } else {
294: break;
295: }
296: } else if ("-e".equals(args[i])) {
297: if (++i < args.length) {
298: Pnuts.eval(args[i], (Context) context.clone());
299: evaluated = true;
300: continue;
301: } else {
302: break;
303: }
304: } else if ("-m".equals(args[i])) {
305: if (++i < args.length) {
306: context.usePackage(args[i]);
307: continue;
308: } else {
309: break;
310: }
311: } else if ("-encoding".equals(args[i])) {
312: if (++i < args.length) {
313: context.setScriptEncoding(args[i]);
314: continue;
315: } else {
316: break;
317: }
318: // } else if ("-inputlog".equals(args[i])) {
319: // if (++i < args.length) {
320: // inputlog = args[i];
321: // continue;
322: // } else {
323: // break;
324: // }
325: } else if (args[i].startsWith("-pure")) {
326: } else if (args[i].startsWith("-O")) {
327: } else if (args[i].startsWith("-a")) {
328: } else if (args[i].startsWith("-b")) {
329: } else if (args[i].startsWith("-p")) {
330: } else if (args[i].startsWith("-d")) {
331: } else if (args[i].startsWith("-vd")) {
332: } else if ("-v".equals(args[i])) {
333: // } else if ("-w".equals(args[i])) {
334: // } else if ("-s".equals(args[i])) {
335: /* skip */
336: } else {
337: evaluated = true;
338: String[] scriptArgs = new String[args.length - i];
339: System.arraycopy(args, i, scriptArgs, 0,
340: scriptArgs.length);
341: context.getCurrentPackage().set("$args".intern(),
342: scriptArgs);
343: Pnuts.loadFile(args[i], (Context) context.clone());
344: break;
345: }
346: } catch (Throwable t) {
347: Runtime.printError(t, context);
348: return;
349: }
350: }
351:
352: if (!interactive && evaluated) {
353: return;
354: }
355:
356: if (quiet) {
357: interactive = false;
358: } else if (!interactive) {
359: int av = 0;
360: try {
361: av = System.in.available();
362: } catch (IOException e) {
363: }
364: if (av < 1) {
365: interactive = true;
366: }
367: }
368:
369: if (interactive) {
370: // if (guiConsole) {
371: // PnutsConsole console = new PnutsConsole(null, context, inputlog, true);
372: // JFrame f = console.getFrame();
373: // f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
374: // f.setVisible(true);
375: // Reader in = console.getReader();
376: // if (contextFactory instanceof TerminalDebugger) {
377: // ((TerminalDebugger) contextFactory)
378: // .setInput(new BufferedReader(in));
379: // }
380: // } else {
381: InputStream in;
382: // if (Pnuts.isJava2()){
383: in = System.in;
384: // } else {
385: // in = new TerminalInputStream(System.in);
386: // }
387: greeting(context);
388: Reader r = Runtime.getScriptReader(in, context);
389: // if (inputlog != null) {
390: // try {
391: // r = new LogReader(r, inputlog);
392: // } catch (IOException e) {
393: // }
394: // }
395: // if (contextFactory instanceof TerminalDebugger) {
396: // ((TerminalDebugger) contextFactory)
397: // .setInput(new BufferedReader(r));
398: // }
399: Pnuts.load(r, true, context);
400: // }
401: } else {
402: try {
403: Pnuts.load(System.in, false, context);
404: } catch (Throwable t) {
405: Runtime.printError(t, context);
406: }
407: }
408: }
409: }
|