001: /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
002: *
003: * ***** BEGIN LICENSE BLOCK *****
004: * Version: MPL 1.1/GPL 2.0
005: *
006: * The contents of this file are subject to the Mozilla Public License Version
007: * 1.1 (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: * http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the
014: * License.
015: *
016: * The Original Code is Rhino code, released
017: * May 6, 1998.
018: *
019: * The Initial Developer of the Original Code is
020: * Netscape Communications Corporation.
021: * Portions created by the Initial Developer are Copyright (C) 1997-1999
022: * the Initial Developer. All Rights Reserved.
023: *
024: * Contributor(s):
025: * Patrick Beard
026: * Norris Boyd
027: * Igor Bukanov
028: * Rob Ginda
029: * Kurt Westerfeld
030: *
031: * Alternatively, the contents of this file may be used under the terms of
032: * the GNU General Public License Version 2 or later (the "GPL"), in which
033: * case the provisions of the GPL are applicable instead of those above. If
034: * you wish to allow use of your version of this file only under the terms of
035: * the GPL and not to allow others to use your version of this file under the
036: * MPL, indicate your decision by deleting the provisions above and replacing
037: * them with the notice and other provisions required by the GPL. If you do
038: * not delete the provisions above, a recipient may use your version of this
039: * file under either the MPL or the GPL.
040: *
041: * ***** END LICENSE BLOCK ***** */
042:
043: package org.mozilla.javascript.tools.shell;
044:
045: import java.io.*;
046: import java.net.URL;
047: import java.net.URLConnection;
048: import java.net.MalformedURLException;
049: import java.util.*;
050: import org.mozilla.javascript.*;
051: import org.mozilla.javascript.tools.ToolErrorReporter;
052:
053: /**
054: * The shell program.
055: *
056: * Can execute scripts interactively or in batch mode at the command line.
057: * An example of controlling the JavaScript engine.
058: *
059: * @author Norris Boyd
060: */
061: public class Main {
062: public static ShellContextFactory shellContextFactory = new ShellContextFactory();
063:
064: public static Global global = new Global();
065: static protected ToolErrorReporter errorReporter;
066: static protected int exitCode = 0;
067: static private final int EXITCODE_RUNTIME_ERROR = 3;
068: static private final int EXITCODE_FILE_NOT_FOUND = 4;
069: static boolean processStdin = true;
070: static Vector fileList = new Vector(5);
071: private static SecurityProxy securityImpl;
072:
073: static {
074: global.initQuitAction(new IProxy(IProxy.SYSTEM_EXIT));
075: }
076:
077: /**
078: * Proxy class to avoid proliferation of anonymous classes.
079: */
080: private static class IProxy implements ContextAction, QuitAction {
081: private static final int PROCESS_FILES = 1;
082: private static final int EVAL_INLINE_SCRIPT = 2;
083: private static final int SYSTEM_EXIT = 3;
084:
085: private int type;
086: String[] args;
087: String scriptText;
088:
089: IProxy(int type) {
090: this .type = type;
091: }
092:
093: public Object run(Context cx) {
094: if (type == PROCESS_FILES) {
095: processFiles(cx, args);
096: } else if (type == EVAL_INLINE_SCRIPT) {
097: Script script = loadScriptFromSource(cx, scriptText,
098: "<command>", 1, null);
099: if (script != null) {
100: evaluateScript(script, cx, getGlobal());
101: }
102: } else {
103: throw Kit.codeBug();
104: }
105: return null;
106: }
107:
108: public void quit(Context cx, int exitCode) {
109: if (type == SYSTEM_EXIT) {
110: System.exit(exitCode);
111: return;
112: }
113: throw Kit.codeBug();
114: }
115: }
116:
117: /**
118: * Main entry point.
119: *
120: * Process arguments as would a normal Java program. Also
121: * create a new Context and associate it with the current thread.
122: * Then set up the execution environment and begin to
123: * execute scripts.
124: */
125: public static void main(String args[]) {
126: try {
127: if (Boolean.getBoolean("rhino.use_java_policy_security")) {
128: initJavaPolicySecuritySupport();
129: }
130: } catch (SecurityException ex) {
131: ex.printStackTrace(System.err);
132: }
133:
134: int result = exec(args);
135: if (result != 0) {
136: System.exit(result);
137: }
138: }
139:
140: /**
141: * Execute the given arguments, but don't System.exit at the end.
142: */
143: public static int exec(String origArgs[]) {
144: errorReporter = new ToolErrorReporter(false, global.getErr());
145: shellContextFactory.setErrorReporter(errorReporter);
146: String[] args = processOptions(origArgs);
147: if (processStdin)
148: fileList.addElement(null);
149:
150: if (!global.initialized) {
151: global.init(shellContextFactory);
152: }
153: IProxy iproxy = new IProxy(IProxy.PROCESS_FILES);
154: iproxy.args = args;
155: shellContextFactory.call(iproxy);
156:
157: return exitCode;
158: }
159:
160: static void processFiles(Context cx, String[] args) {
161: // define "arguments" array in the top-level object:
162: // need to allocate new array since newArray requires instances
163: // of exactly Object[], not ObjectSubclass[]
164: Object[] array = new Object[args.length];
165: System.arraycopy(args, 0, array, 0, args.length);
166: Scriptable argsObj = cx.newArray(global, array);
167: global.defineProperty("arguments", argsObj,
168: ScriptableObject.DONTENUM);
169:
170: for (int i = 0; i < fileList.size(); i++) {
171: processSource(cx, (String) fileList.elementAt(i));
172: }
173:
174: }
175:
176: public static Global getGlobal() {
177: return global;
178: }
179:
180: /**
181: * Parse arguments.
182: */
183: public static String[] processOptions(String args[]) {
184: String usageError;
185: goodUsage: for (int i = 0;; ++i) {
186: if (i == args.length) {
187: return new String[0];
188: }
189: String arg = args[i];
190: if (!arg.startsWith("-")) {
191: processStdin = false;
192: fileList.addElement(arg);
193: String[] result = new String[args.length - i - 1];
194: System.arraycopy(args, i + 1, result, 0, args.length
195: - i - 1);
196: return result;
197: }
198: if (arg.equals("-version")) {
199: if (++i == args.length) {
200: usageError = arg;
201: break goodUsage;
202: }
203: int version;
204: try {
205: version = Integer.parseInt(args[i]);
206: } catch (NumberFormatException ex) {
207: usageError = args[i];
208: break goodUsage;
209: }
210: if (!Context.isValidLanguageVersion(version)) {
211: usageError = args[i];
212: break goodUsage;
213: }
214: shellContextFactory.setLanguageVersion(version);
215: continue;
216: }
217: if (arg.equals("-opt") || arg.equals("-O")) {
218: if (++i == args.length) {
219: usageError = arg;
220: break goodUsage;
221: }
222: int opt;
223: try {
224: opt = Integer.parseInt(args[i]);
225: } catch (NumberFormatException ex) {
226: usageError = args[i];
227: break goodUsage;
228: }
229: if (opt == -2) {
230: // Compatibility with Cocoon Rhino fork
231: opt = -1;
232: } else if (!Context.isValidOptimizationLevel(opt)) {
233: usageError = args[i];
234: break goodUsage;
235: }
236: shellContextFactory.setOptimizationLevel(opt);
237: continue;
238: }
239: if (arg.equals("-strict")) {
240: shellContextFactory.setStrictMode(true);
241: errorReporter.setIsReportingWarnings(true);
242: continue;
243: }
244: if (arg.equals("-fatal-warnings")) {
245: shellContextFactory.setWarningAsError(true);
246: continue;
247: }
248: if (arg.equals("-e")) {
249: processStdin = false;
250: if (++i == args.length) {
251: usageError = arg;
252: break goodUsage;
253: }
254: if (!global.initialized) {
255: global.init(shellContextFactory);
256: }
257: IProxy iproxy = new IProxy(IProxy.EVAL_INLINE_SCRIPT);
258: iproxy.scriptText = args[i];
259: shellContextFactory.call(iproxy);
260: continue;
261: }
262: if (arg.equals("-w")) {
263: errorReporter.setIsReportingWarnings(true);
264: continue;
265: }
266: if (arg.equals("-f")) {
267: processStdin = false;
268: if (++i == args.length) {
269: usageError = arg;
270: break goodUsage;
271: }
272: fileList.addElement(args[i].equals("-") ? null
273: : args[i]);
274: continue;
275: }
276: if (arg.equals("-sealedlib")) {
277: global.setSealedStdLib(true);
278: continue;
279: }
280: if (arg.equals("-debug")) {
281: shellContextFactory.setGeneratingDebug(true);
282: continue;
283: }
284: if (arg.equals("-?") || arg.equals("-help")) {
285: // print usage message
286: global.getOut().println(
287: ToolErrorReporter.getMessage("msg.shell.usage",
288: Main.class.getName()));
289: System.exit(1);
290: }
291: usageError = arg;
292: break goodUsage;
293: }
294: // print error and usage message
295: global.getOut().println(
296: ToolErrorReporter.getMessage("msg.shell.invalid",
297: usageError));
298: global.getOut().println(
299: ToolErrorReporter.getMessage("msg.shell.usage",
300: Main.class.getName()));
301: System.exit(1);
302: return null;
303: }
304:
305: private static void initJavaPolicySecuritySupport() {
306: Throwable exObj;
307: try {
308: Class cl = Class
309: .forName("org.mozilla.javascript.tools.shell.JavaPolicySecurity");
310: securityImpl = (SecurityProxy) cl.newInstance();
311: SecurityController.initGlobal(securityImpl);
312: return;
313: } catch (ClassNotFoundException ex) {
314: exObj = ex;
315: } catch (IllegalAccessException ex) {
316: exObj = ex;
317: } catch (InstantiationException ex) {
318: exObj = ex;
319: } catch (LinkageError ex) {
320: exObj = ex;
321: }
322: throw Kit.initCause(new IllegalStateException(
323: "Can not load security support: " + exObj), exObj);
324: }
325:
326: /**
327: * Evaluate JavaScript source.
328: *
329: * @param cx the current context
330: * @param filename the name of the file to compile, or null
331: * for interactive mode.
332: */
333: public static void processSource(Context cx, String filename) {
334: if (filename == null || filename.equals("-")) {
335: PrintStream ps = global.getErr();
336: if (filename == null) {
337: // print implementation version
338: ps.println(cx.getImplementationVersion());
339: }
340:
341: // Use the interpreter for interactive input
342: cx.setOptimizationLevel(-1);
343:
344: BufferedReader in = new BufferedReader(
345: new InputStreamReader(global.getIn()));
346: int lineno = 1;
347: boolean hitEOF = false;
348: while (!hitEOF) {
349: String[] prompts = global.getPrompts(cx);
350: if (filename == null)
351: ps.print(prompts[0]);
352: ps.flush();
353: String source = "";
354:
355: // Collect lines of source to compile.
356: while (true) {
357: String newline;
358: try {
359: newline = in.readLine();
360: } catch (IOException ioe) {
361: ps.println(ioe.toString());
362: break;
363: }
364: if (newline == null) {
365: hitEOF = true;
366: break;
367: }
368: source = source + newline + "\n";
369: lineno++;
370: if (cx.stringIsCompilableUnit(source))
371: break;
372: ps.print(prompts[1]);
373: }
374: Script script = loadScriptFromSource(cx, source,
375: "<stdin>", lineno, null);
376: if (script != null) {
377: Object result = evaluateScript(script, cx, global);
378: // Avoid printing out undefined or function definitions.
379: if (result != Context.getUndefinedValue()
380: && !(result instanceof Function && source
381: .trim().startsWith("function"))) {
382: try {
383: ps.println(Context.toString(result));
384: } catch (RhinoException rex) {
385: ToolErrorReporter.reportException(cx
386: .getErrorReporter(), rex);
387: }
388: }
389: NativeArray h = global.history;
390: h.put((int) h.getLength(), h, source);
391: }
392: }
393: ps.println();
394: } else {
395: processFile(cx, global, filename);
396: }
397: System.gc();
398: }
399:
400: public static void processFile(Context cx, Scriptable scope,
401: String filename) {
402: if (securityImpl == null) {
403: processFileSecure(cx, scope, filename, null);
404: } else {
405: securityImpl.callProcessFileSecure(cx, scope, filename);
406: }
407: }
408:
409: static void processFileSecure(Context cx, Scriptable scope,
410: String path, Object securityDomain) {
411: Script script;
412: if (path.endsWith(".class")) {
413: script = loadCompiledScript(cx, path, securityDomain);
414: } else {
415: String source = (String) readFileOrUrl(path, true);
416: if (source == null) {
417: exitCode = EXITCODE_FILE_NOT_FOUND;
418: return;
419: }
420:
421: // Support the executable script #! syntax: If
422: // the first line begins with a '#', treat the whole
423: // line as a comment.
424: if (source.length() > 0 && source.charAt(0) == '#') {
425: for (int i = 1; i != source.length(); ++i) {
426: int c = source.charAt(i);
427: if (c == '\n' || c == '\r') {
428: source = source.substring(i);
429: break;
430: }
431: }
432: }
433: script = loadScriptFromSource(cx, source, path, 1,
434: securityDomain);
435: }
436: if (script != null) {
437: evaluateScript(script, cx, scope);
438: }
439: }
440:
441: public static Script loadScriptFromSource(Context cx,
442: String scriptSource, String path, int lineno,
443: Object securityDomain) {
444: try {
445: return cx.compileString(scriptSource, path, lineno,
446: securityDomain);
447: } catch (EvaluatorException ee) {
448: // Already printed message.
449: exitCode = EXITCODE_RUNTIME_ERROR;
450: } catch (RhinoException rex) {
451: ToolErrorReporter.reportException(cx.getErrorReporter(),
452: rex);
453: exitCode = EXITCODE_RUNTIME_ERROR;
454: } catch (VirtualMachineError ex) {
455: // Treat StackOverflow and OutOfMemory as runtime errors
456: ex.printStackTrace();
457: String msg = ToolErrorReporter.getMessage(
458: "msg.uncaughtJSException", ex.toString());
459: exitCode = EXITCODE_RUNTIME_ERROR;
460: Context.reportError(msg);
461: }
462: return null;
463: }
464:
465: private static Script loadCompiledScript(Context cx, String path,
466: Object securityDomain) {
467: byte[] data = (byte[]) readFileOrUrl(path, false);
468: if (data == null) {
469: exitCode = EXITCODE_FILE_NOT_FOUND;
470: return null;
471: }
472: // XXX: For now extract class name of compiled Script from path
473: // instead of parsing class bytes
474: int nameStart = path.lastIndexOf('/');
475: if (nameStart < 0) {
476: nameStart = 0;
477: } else {
478: ++nameStart;
479: }
480: int nameEnd = path.lastIndexOf('.');
481: if (nameEnd < nameStart) {
482: // '.' does not exist in path (nameEnd < 0)
483: // or it comes before nameStart
484: nameEnd = path.length();
485: }
486: String name = path.substring(nameStart, nameEnd);
487: try {
488: GeneratedClassLoader loader = SecurityController
489: .createLoader(cx.getApplicationClassLoader(),
490: securityDomain);
491: Class clazz = loader.defineClass(name, data);
492: loader.linkClass(clazz);
493: if (!Script.class.isAssignableFrom(clazz)) {
494: throw Context
495: .reportRuntimeError("msg.must.implement.Script");
496: }
497: return (Script) clazz.newInstance();
498: } catch (RhinoException rex) {
499: ToolErrorReporter.reportException(cx.getErrorReporter(),
500: rex);
501: exitCode = EXITCODE_RUNTIME_ERROR;
502: } catch (IllegalAccessException iaex) {
503: exitCode = EXITCODE_RUNTIME_ERROR;
504: Context.reportError(iaex.toString());
505: } catch (InstantiationException inex) {
506: exitCode = EXITCODE_RUNTIME_ERROR;
507: Context.reportError(inex.toString());
508: }
509: return null;
510: }
511:
512: public static Object evaluateScript(Script script, Context cx,
513: Scriptable scope) {
514: try {
515: return script.exec(cx, scope);
516: } catch (RhinoException rex) {
517: ToolErrorReporter.reportException(cx.getErrorReporter(),
518: rex);
519: exitCode = EXITCODE_RUNTIME_ERROR;
520: } catch (VirtualMachineError ex) {
521: // Treat StackOverflow and OutOfMemory as runtime errors
522: ex.printStackTrace();
523: String msg = ToolErrorReporter.getMessage(
524: "msg.uncaughtJSException", ex.toString());
525: exitCode = EXITCODE_RUNTIME_ERROR;
526: Context.reportError(msg);
527: }
528: return Context.getUndefinedValue();
529: }
530:
531: public static InputStream getIn() {
532: return getGlobal().getIn();
533: }
534:
535: public static void setIn(InputStream in) {
536: getGlobal().setIn(in);
537: }
538:
539: public static PrintStream getOut() {
540: return getGlobal().getOut();
541: }
542:
543: public static void setOut(PrintStream out) {
544: getGlobal().setOut(out);
545: }
546:
547: public static PrintStream getErr() {
548: return getGlobal().getErr();
549: }
550:
551: public static void setErr(PrintStream err) {
552: getGlobal().setErr(err);
553: }
554:
555: /**
556: * Read file or url specified by <tt>path</tt>.
557: * @return file or url content as <tt>byte[]</tt> or as <tt>String</tt> if
558: * <tt>convertToString</tt> is true.
559: */
560: private static Object readFileOrUrl(String path,
561: boolean convertToString) {
562: URL url = null;
563: // Assume path is URL if it contains dot and there are at least
564: // 2 characters in the protocol part. The later allows under Windows
565: // to interpret paths with driver letter as file, not URL.
566: if (path.indexOf(':') >= 2) {
567: try {
568: url = new URL(path);
569: } catch (MalformedURLException ex) {
570: }
571: }
572:
573: InputStream is = null;
574: int capacityHint = 0;
575: if (url == null) {
576: File file = new File(path);
577: capacityHint = (int) file.length();
578: try {
579: is = new FileInputStream(file);
580: } catch (IOException ex) {
581: Context.reportError(ToolErrorReporter.getMessage(
582: "msg.couldnt.open", path));
583: return null;
584: }
585: } else {
586: try {
587: URLConnection uc = url.openConnection();
588: is = uc.getInputStream();
589: capacityHint = uc.getContentLength();
590: // Ignore insane values for Content-Length
591: if (capacityHint > (1 << 20)) {
592: capacityHint = -1;
593: }
594: } catch (IOException ex) {
595: Context.reportError(ToolErrorReporter.getMessage(
596: "msg.couldnt.open.url", url.toString(), ex
597: .toString()));
598: return null;
599: }
600: }
601: if (capacityHint <= 0) {
602: capacityHint = 4096;
603: }
604:
605: byte[] data;
606: try {
607: try {
608: data = Kit.readStream(is, capacityHint);
609: } finally {
610: is.close();
611: }
612: } catch (IOException ex) {
613: Context.reportError(ex.toString());
614: return null;
615: }
616:
617: Object result;
618: if (!convertToString) {
619: result = data;
620: } else {
621: // Convert to String using the default encoding
622: // XXX: Use 'charset=' argument of Content-Type if URL?
623: result = new String(data);
624: }
625: return result;
626: }
627:
628: }
|