~ubuntu-branches/ubuntu/karmic/rhino/karmic

« back to all changes in this revision

Viewing changes to toolsrc/org/mozilla/javascript/tools/shell/Main.java

  • Committer: Bazaar Package Importer
  • Author(s): Jerry Haltom
  • Date: 2005-03-19 16:56:07 UTC
  • mto: (11.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050319165607-geu3j3fnqlkpqkh1
Tags: upstream-1.6.R1
ImportĀ upstreamĀ versionĀ 1.6.R1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2
 
 *
3
 
 * The contents of this file are subject to the Netscape Public
4
 
 * License Version 1.1 (the "License"); you may not use this file
5
 
 * except in compliance with the License. You may obtain a copy of
6
 
 * the License at http://www.mozilla.org/NPL/
7
 
 *
8
 
 * Software distributed under the License is distributed on an "AS
9
 
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr
10
 
 * implied. See the License for the specific language governing
11
 
 * rights and limitations under the License.
12
 
 *
13
 
 * The Original Code is Rhino code, released
14
 
 * May 6, 1998.
15
 
 *
16
 
 * The Initial Developer of the Original Code is Netscape
17
 
 * Communications Corporation.  Portions created by Netscape are
18
 
 * Copyright (C) 1997-1999 Netscape Communications Corporation. All
19
 
 * Rights Reserved.
20
 
 *
21
 
 * Contributor(s): 
22
 
 * Patrick Beard
23
 
 * Norris Boyd
24
 
 * Rob Ginda
25
 
 * Kurt Westerfeld
26
 
 *
27
 
 * Alternatively, the contents of this file may be used under the
28
 
 * terms of the GNU Public License (the "GPL"), in which case the
29
 
 * provisions of the GPL are applicable instead of those above.
30
 
 * If you wish to allow use of your version of this file only
31
 
 * under the terms of the GPL and not to allow others to use your
32
 
 * version of this file under the NPL, indicate your decision by
33
 
 * deleting the provisions above and replace them with the notice
34
 
 * and other provisions required by the GPL.  If you do not delete
35
 
 * the provisions above, a recipient may use your version of this
36
 
 * file under either the NPL or the GPL.
37
 
 */
38
 
 
39
 
package org.mozilla.javascript.tools.shell;
40
 
 
41
 
import java.io.*;
42
 
import java.util.*;
43
 
import java.lang.reflect.*;
44
 
import org.mozilla.javascript.*;
45
 
import org.mozilla.javascript.tools.ToolErrorReporter;
46
 
 
47
 
/**
48
 
 * The shell program.
49
 
 *
50
 
 * Can execute scripts interactively or in batch mode at the command line.
51
 
 * An example of controlling the JavaScript engine.
52
 
 *
53
 
 * @author Norris Boyd
54
 
 */
55
 
public class Main {
56
 
 
57
 
    /**
58
 
     * Main entry point.
59
 
     *
60
 
     * Process arguments as would a normal Java program. Also
61
 
     * create a new Context and associate it with the current thread.
62
 
     * Then set up the execution environment and begin to
63
 
     * execute scripts.
64
 
     */
65
 
    public static void main(String args[]) {
66
 
        int result = exec(args);
67
 
        if (result != 0)
68
 
            System.exit(result);
69
 
    }
70
 
    
71
 
    /**
72
 
     *  Execute the given arguments, but don't System.exit at the end.
73
 
     */
74
 
    public static int exec(String args[]) {
75
 
        Context cx = Context.enter();
76
 
        // Create the top-level scope object.
77
 
        global = new Global(cx);
78
 
        errorReporter = new ToolErrorReporter(false, global.getErr());
79
 
        cx.setErrorReporter(errorReporter);
80
 
 
81
 
        args = processOptions(cx, args);
82
 
 
83
 
        int skip = 0;
84
 
        if (fileList.size() == 0 && args.length > 0) {
85
 
            skip = 1;
86
 
            fileList.addElement(args[0]);
87
 
        }
88
 
        if (processStdin)
89
 
            fileList.addElement(null);
90
 
 
91
 
        // get the command line arguments after the name of the script,
92
 
        // and define "arguments" array in the top-level object
93
 
        Object[] array = args;
94
 
        if (args.length > 0) {
95
 
            int length = args.length - skip;
96
 
            array = new Object[length];
97
 
            System.arraycopy(args, skip, array, 0, length);
98
 
        }
99
 
        Scriptable argsObj = cx.newArray(global, array);
100
 
        global.defineProperty("arguments", argsObj,
101
 
                              ScriptableObject.DONTENUM);
102
 
        
103
 
        for (int i=0; i < fileList.size(); i++) {
104
 
            processSource(cx, (String) fileList.get(i));
105
 
        }
106
 
 
107
 
        cx.exit();
108
 
        return exitCode;
109
 
    }
110
 
 
111
 
    /**
112
 
     * Parse arguments.
113
 
     */
114
 
    public static String[] processOptions(Context cx, String args[]) {
115
 
        cx.setTargetPackage("");    // default to no package
116
 
        for (int i=0; i < args.length; i++) {
117
 
            String arg = args[i];
118
 
            if (!arg.startsWith("-")) {
119
 
                processStdin = false;
120
 
                String[] result = new String[args.length - i];
121
 
                System.arraycopy(args, i, result, 0, args.length - i);
122
 
                return result;
123
 
            }
124
 
            if (arg.equals("-version")) {
125
 
                if (++i == args.length)
126
 
                    usage(arg);
127
 
                double d = cx.toNumber(args[i]);
128
 
                if (d != d)
129
 
                    usage(arg);
130
 
                cx.setLanguageVersion((int) d);
131
 
                continue;
132
 
            }
133
 
            if (arg.equals("-opt") || arg.equals("-O")) {
134
 
                if (++i == args.length)
135
 
                    usage(arg);
136
 
                double d = cx.toNumber(args[i]);
137
 
                if (d != d)
138
 
                    usage(arg);
139
 
                cx.setOptimizationLevel((int)d);
140
 
                continue;
141
 
            }
142
 
            if (arg.equals("-e")) {
143
 
                processStdin = false;
144
 
                if (++i == args.length)
145
 
                    usage(arg);
146
 
                Reader reader = new StringReader(args[i]);
147
 
                evaluateReader(cx, global, reader, "<command>", 1);
148
 
                continue;
149
 
            }
150
 
            if (arg.equals("-w")) {
151
 
                errorReporter.setIsReportingWarnings(true);
152
 
                continue;
153
 
            }
154
 
            if (arg.equals("-f")) {
155
 
                processStdin = false;
156
 
                if (++i == args.length)
157
 
                    usage(arg);
158
 
                fileList.addElement(args[i].equals("-") ? null : args[i]);
159
 
                continue;
160
 
            }
161
 
            usage(arg);
162
 
        }
163
 
        return new String[0];
164
 
    }
165
 
 
166
 
    /**
167
 
     * Print a usage message.
168
 
     */
169
 
    public static void usage(String s) {
170
 
        p(ToolErrorReporter.getMessage("msg.shell.usage", s));
171
 
        System.exit(1);
172
 
    }
173
 
 
174
 
    /**
175
 
     * Evaluate JavaScript source.
176
 
     *
177
 
     * @param cx the current context
178
 
     * @param filename the name of the file to compile, or null
179
 
     *                 for interactive mode.
180
 
     */
181
 
    public static void processSource(Context cx, String filename) {
182
 
        if (filename == null || filename.equals("-")) {
183
 
            // Use the interpreter for interactive input
184
 
            cx.setOptimizationLevel(-1);
185
 
            
186
 
            BufferedReader in = new BufferedReader
187
 
                (new InputStreamReader(global.getIn()));
188
 
            int lineno = 1;
189
 
            boolean hitEOF = false;
190
 
            while (!hitEOF) {
191
 
                int startline = lineno;
192
 
                if (filename == null)
193
 
                    global.getErr().print("js> ");
194
 
                global.getErr().flush();
195
 
                String source = "";
196
 
                    
197
 
                // Collect lines of source to compile.
198
 
                while (true) {
199
 
                    String newline;
200
 
                    try {
201
 
                        newline = in.readLine();
202
 
                    }
203
 
                    catch (IOException ioe) {
204
 
                        global.getErr().println(ioe.toString());
205
 
                        break;
206
 
                    }
207
 
                    if (newline == null) {
208
 
                        hitEOF = true;
209
 
                        break;
210
 
                    }
211
 
                    source = source + newline + "\n";
212
 
                    lineno++;
213
 
                    if (cx.stringIsCompilableUnit(source))
214
 
                        break;
215
 
                }
216
 
                Reader reader = new StringReader(source);
217
 
                Object result = evaluateReader(cx, global, reader, 
218
 
                                               "<stdin>", startline);
219
 
                if (result != cx.getUndefinedValue()) {
220
 
                    try {
221
 
                        global.getErr().println(cx.toString(result));
222
 
                    } catch (EcmaError ee) {
223
 
                        String msg = ToolErrorReporter.getMessage(
224
 
                            "msg.uncaughtJSException", ee.toString());
225
 
                        exitCode = EXITCODE_RUNTIME_ERROR;
226
 
                        if (ee.getSourceName() != null) {
227
 
                            Context.reportError(msg, ee.getSourceName(), 
228
 
                                                ee.getLineNumber(), 
229
 
                                                ee.getLineSource(), 
230
 
                                                ee.getColumnNumber());
231
 
                        } else {
232
 
                            Context.reportError(msg);
233
 
                        }
234
 
                    }
235
 
                }
236
 
                NativeArray h = global.history;
237
 
                h.put((int)h.jsGet_length(), h, source);
238
 
            }
239
 
            global.getErr().println();
240
 
        } else processFile(cx, global, filename);
241
 
        System.gc();
242
 
    }
243
 
    
244
 
    public static void processFile(Context cx, Scriptable scope,
245
 
                                   String filename)
246
 
    {
247
 
            Reader in = null;
248
 
            try {
249
 
                in = new PushbackReader(new FileReader(filename));
250
 
                int c = in.read();
251
 
                // Support the executable script #! syntax:  If
252
 
                // the first line begins with a '#', treat the whole
253
 
                // line as a comment.
254
 
                if (c == '#') {
255
 
                    while ((c = in.read()) != -1) {
256
 
                        if (c == '\n' || c == '\r')
257
 
                            break;
258
 
                    }
259
 
                    ((PushbackReader) in).unread(c);
260
 
                } else {
261
 
                    // No '#' line, just reopen the file and forget it
262
 
                    // ever happened.  OPT closing and reopening
263
 
                    // undoubtedly carries some cost.  Is this faster
264
 
                    // or slower than leaving the PushbackReader
265
 
                    // around?
266
 
                    in.close();
267
 
                    in = new FileReader(filename);
268
 
                }
269
 
                filename = new java.io.File(filename).getCanonicalPath();
270
 
            }
271
 
            catch (FileNotFoundException ex) {
272
 
                Context.reportError(ToolErrorReporter.getMessage(
273
 
                    "msg.couldnt.open",
274
 
                    filename));
275
 
                exitCode = EXITCODE_FILE_NOT_FOUND;
276
 
                return;
277
 
            } catch (IOException ioe) {
278
 
                global.getErr().println(ioe.toString());
279
 
            }
280
 
            
281
 
            // Here we evalute the entire contents of the file as
282
 
            // a script. Text is printed only if the print() function
283
 
            // is called.
284
 
            evaluateReader(cx, scope, in, filename, 1);
285
 
    }
286
 
 
287
 
    public static Object evaluateReader(Context cx, Scriptable scope, 
288
 
                                        Reader in, String sourceName, 
289
 
                                        int lineno)
290
 
    {
291
 
        Object result = cx.getUndefinedValue();
292
 
        try {
293
 
            result = cx.evaluateReader(scope, in, sourceName, lineno, null);
294
 
        }
295
 
        catch (WrappedException we) {
296
 
            global.getErr().println(we.getWrappedException().toString());
297
 
            we.printStackTrace();
298
 
        }
299
 
        catch (EcmaError ee) {
300
 
            String msg = ToolErrorReporter.getMessage(
301
 
                "msg.uncaughtJSException", ee.toString());
302
 
            exitCode = EXITCODE_RUNTIME_ERROR;
303
 
            if (ee.getSourceName() != null) {
304
 
                Context.reportError(msg, ee.getSourceName(), 
305
 
                                    ee.getLineNumber(), 
306
 
                                    ee.getLineSource(), 
307
 
                                    ee.getColumnNumber());
308
 
            } else {
309
 
                Context.reportError(msg);
310
 
            }
311
 
        }
312
 
        catch (EvaluatorException ee) {
313
 
            // Already printed message.
314
 
            exitCode = EXITCODE_RUNTIME_ERROR;
315
 
        }
316
 
        catch (JavaScriptException jse) {
317
 
                // Need to propagate ThreadDeath exceptions.
318
 
                Object value = jse.getValue();
319
 
                if (value instanceof ThreadDeath)
320
 
                        throw (ThreadDeath) value;
321
 
            exitCode = EXITCODE_RUNTIME_ERROR;
322
 
            Context.reportError(ToolErrorReporter.getMessage(
323
 
                "msg.uncaughtJSException",
324
 
                jse.getMessage()));
325
 
        }
326
 
        catch (IOException ioe) {
327
 
            global.getErr().println(ioe.toString());
328
 
        }
329
 
        finally {
330
 
            try {
331
 
                in.close();
332
 
            }
333
 
            catch (IOException ioe) {
334
 
                global.getErr().println(ioe.toString());
335
 
            }
336
 
        }
337
 
        return result;
338
 
    }
339
 
 
340
 
    private static void p(String s) {
341
 
        global.getOut().println(s);
342
 
    }
343
 
    
344
 
    public static ScriptableObject getScope() {
345
 
        return global;
346
 
    }
347
 
 
348
 
    public static InputStream getIn() {
349
 
        return Global.getInstance(global).getIn();
350
 
    }
351
 
    
352
 
    public static void setIn(InputStream in) {
353
 
        Global.getInstance(global).setIn(in);
354
 
    }
355
 
 
356
 
    public static PrintStream getOut() {
357
 
        return Global.getInstance(global).getOut();
358
 
    }
359
 
    
360
 
    public static void setOut(PrintStream out) {
361
 
        Global.getInstance(global).setOut(out);
362
 
    }
363
 
 
364
 
    public static PrintStream getErr() { 
365
 
        return Global.getInstance(global).getErr();
366
 
    }
367
 
 
368
 
    public static void setErr(PrintStream err) {
369
 
        Global.getInstance(global).setErr(err);
370
 
    }
371
 
 
372
 
    static protected ToolErrorReporter errorReporter;
373
 
    static protected Global global;
374
 
    static protected int exitCode = 0;
375
 
    static private final int EXITCODE_RUNTIME_ERROR = 3;
376
 
    static private final int EXITCODE_FILE_NOT_FOUND = 4;
377
 
    //static private DebugShell debugShell;
378
 
    static boolean processStdin = true;
379
 
    static Vector fileList = new Vector(5);
380
 
}
 
1
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 
2
 *
 
3
 * The contents of this file are subject to the Netscape Public
 
4
 * License Version 1.1 (the "License"); you may not use this file
 
5
 * except in compliance with the License. You may obtain a copy of
 
6
 * the License at http://www.mozilla.org/NPL/
 
7
 *
 
8
 * Software distributed under the License is distributed on an "AS
 
9
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
10
 * implied. See the License for the specific language governing
 
11
 * rights and limitations under the License.
 
12
 *
 
13
 * The Original Code is Rhino code, released
 
14
 * May 6, 1998.
 
15
 *
 
16
 * The Initial Developer of the Original Code is Netscape
 
17
 * Communications Corporation.  Portions created by Netscape are
 
18
 * Copyright (C) 1997-1999 Netscape Communications Corporation. All
 
19
 * Rights Reserved.
 
20
 *
 
21
 * Contributor(s):
 
22
 * Patrick Beard
 
23
 * Norris Boyd
 
24
 * Igor Bukanov
 
25
 * Rob Ginda
 
26
 * Kurt Westerfeld
 
27
 *
 
28
 * Alternatively, the contents of this file may be used under the
 
29
 * terms of the GNU Public License (the "GPL"), in which case the
 
30
 * provisions of the GPL are applicable instead of those above.
 
31
 * If you wish to allow use of your version of this file only
 
32
 * under the terms of the GPL and not to allow others to use your
 
33
 * version of this file under the NPL, indicate your decision by
 
34
 * deleting the provisions above and replace them with the notice
 
35
 * and other provisions required by the GPL.  If you do not delete
 
36
 * the provisions above, a recipient may use your version of this
 
37
 * file under either the NPL or the GPL.
 
38
 */
 
39
 
 
40
package org.mozilla.javascript.tools.shell;
 
41
 
 
42
import java.io.*;
 
43
import java.net.URL;
 
44
import java.net.MalformedURLException;
 
45
import java.util.*;
 
46
import java.lang.reflect.*;
 
47
import org.mozilla.javascript.*;
 
48
import org.mozilla.javascript.tools.ToolErrorReporter;
 
49
 
 
50
/**
 
51
 * The shell program.
 
52
 *
 
53
 * Can execute scripts interactively or in batch mode at the command line.
 
54
 * An example of controlling the JavaScript engine.
 
55
 *
 
56
 * @author Norris Boyd
 
57
 */
 
58
public class Main
 
59
{
 
60
    public static final ShellContextFactory
 
61
        shellContextFactory = new ShellContextFactory();
 
62
 
 
63
    /**
 
64
     * Proxy class to avoid proliferation of anonymous classes.
 
65
     */
 
66
    private static class IProxy implements ContextAction
 
67
    {
 
68
        private static final int PROCESS_FILES = 1;
 
69
        private static final int EVAL_INLINE_SCRIPT = 2;
 
70
 
 
71
        private int type;
 
72
        String[] args;
 
73
        String scriptText;
 
74
 
 
75
        IProxy(int type)
 
76
        {
 
77
            this.type = type;
 
78
        }
 
79
 
 
80
        public Object run(Context cx)
 
81
        {
 
82
            if (type == PROCESS_FILES) {
 
83
                processFiles(cx, args);
 
84
            } else if (type == EVAL_INLINE_SCRIPT) {
 
85
                evaluateScript(cx, getGlobal(), null, scriptText,
 
86
                               "<command>", 1, null);
 
87
            } else {
 
88
                throw Kit.codeBug();
 
89
            }
 
90
            return null;
 
91
        }
 
92
    }
 
93
 
 
94
 
 
95
    /**
 
96
     * Main entry point.
 
97
     *
 
98
     * Process arguments as would a normal Java program. Also
 
99
     * create a new Context and associate it with the current thread.
 
100
     * Then set up the execution environment and begin to
 
101
     * execute scripts.
 
102
     */
 
103
    public static void main(String args[]) {
 
104
        try {
 
105
            if (Boolean.getBoolean("rhino.use_java_policy_security")) {
 
106
                initJavaPolicySecuritySupport();
 
107
            }
 
108
        } catch (SecurityException ex) {
 
109
            ex.printStackTrace(System.err);
 
110
        }
 
111
 
 
112
        int result = exec(args);
 
113
        if (result != 0) {
 
114
            System.exit(result);
 
115
        }
 
116
    }
 
117
 
 
118
    /**
 
119
     *  Execute the given arguments, but don't System.exit at the end.
 
120
     */
 
121
    public static int exec(String origArgs[])
 
122
    {
 
123
 
 
124
        for (int i=0; i < origArgs.length; i++) {
 
125
            String arg = origArgs[i];
 
126
            if (arg.equals("-sealedlib")) {
 
127
                sealedStdLib = true;
 
128
                break;
 
129
            }
 
130
        }
 
131
 
 
132
        errorReporter = new ToolErrorReporter(false, global.getErr());
 
133
        shellContextFactory.setErrorReporter(errorReporter);
 
134
        String[] args = processOptions(origArgs);
 
135
        if (processStdin)
 
136
            fileList.addElement(null);
 
137
 
 
138
        IProxy iproxy = new IProxy(IProxy.PROCESS_FILES);
 
139
        iproxy.args = args;
 
140
        shellContextFactory.call(iproxy);
 
141
 
 
142
        return exitCode;
 
143
    }
 
144
 
 
145
    static void processFiles(Context cx, String[] args)
 
146
    {
 
147
        if (!global.initialized) {
 
148
            global.init(cx);
 
149
        }
 
150
        // define "arguments" array in the top-level object:
 
151
        // need to allocate new array since newArray requires instances
 
152
        // of exactly Object[], not ObjectSubclass[]
 
153
        Object[] array = new Object[args.length];
 
154
        System.arraycopy(args, 0, array, 0, args.length);
 
155
        Scriptable argsObj = cx.newArray(global, array);
 
156
        global.defineProperty("arguments", argsObj,
 
157
                              ScriptableObject.DONTENUM);
 
158
 
 
159
        for (int i=0; i < fileList.size(); i++) {
 
160
            processSource(cx, (String) fileList.elementAt(i));
 
161
        }
 
162
 
 
163
    }
 
164
 
 
165
    public static Global getGlobal()
 
166
    {
 
167
        return global;
 
168
    }
 
169
 
 
170
    /**
 
171
     * Parse arguments.
 
172
     */
 
173
    public static String[] processOptions(String args[])
 
174
    {
 
175
        String usageError;
 
176
        goodUsage: for (int i = 0; ; ++i) {
 
177
            if (i == args.length) {
 
178
                return new String[0];
 
179
            }
 
180
            String arg = args[i];
 
181
            if (!arg.startsWith("-")) {
 
182
                processStdin = false;
 
183
                fileList.addElement(arg);
 
184
                String[] result = new String[args.length - i - 1];
 
185
                System.arraycopy(args, i+1, result, 0, args.length - i - 1);
 
186
                return result;
 
187
            }
 
188
            if (arg.equals("-version")) {
 
189
                if (++i == args.length) {
 
190
                    usageError = arg;
 
191
                    break goodUsage;
 
192
                }
 
193
                int version;
 
194
                try {
 
195
                    version = Integer.parseInt(args[i]);
 
196
                } catch (NumberFormatException ex) {
 
197
                    usageError = args[i];
 
198
                    break goodUsage;
 
199
                }
 
200
                if (!Context.isValidLanguageVersion(version)) {
 
201
                    usageError = args[i];
 
202
                    break goodUsage;
 
203
                }
 
204
                shellContextFactory.setLanguageVersion(version);
 
205
                continue;
 
206
            }
 
207
            if (arg.equals("-opt") || arg.equals("-O")) {
 
208
                if (++i == args.length) {
 
209
                    usageError = arg;
 
210
                    break goodUsage;
 
211
                }
 
212
                int opt;
 
213
                try {
 
214
                    opt = Integer.parseInt(args[i]);
 
215
                } catch (NumberFormatException ex) {
 
216
                    usageError = args[i];
 
217
                    break goodUsage;
 
218
                }
 
219
                if (opt == -2) {
 
220
                    // Compatibility with Cocoon Rhino fork
 
221
                    opt = -1;
 
222
                } else if (!Context.isValidOptimizationLevel(opt)) {
 
223
                    usageError = args[i];
 
224
                    break goodUsage;
 
225
                }
 
226
                shellContextFactory.setOptimizationLevel(opt);
 
227
                continue;
 
228
            }
 
229
            if (arg.equals("-strict")) {
 
230
                shellContextFactory.setStrictMode(true);
 
231
                continue;
 
232
            }
 
233
            if (arg.equals("-e")) {
 
234
                processStdin = false;
 
235
                if (++i == args.length) {
 
236
                    usageError = arg;
 
237
                    break goodUsage;
 
238
                }
 
239
                IProxy iproxy = new IProxy(IProxy.EVAL_INLINE_SCRIPT);
 
240
                iproxy.scriptText = args[i];
 
241
                shellContextFactory.call(iproxy);
 
242
                continue;
 
243
            }
 
244
            if (arg.equals("-w")) {
 
245
                errorReporter.setIsReportingWarnings(true);
 
246
                continue;
 
247
            }
 
248
            if (arg.equals("-f")) {
 
249
                processStdin = false;
 
250
                if (++i == args.length) {
 
251
                    usageError = arg;
 
252
                    break goodUsage;
 
253
                }
 
254
                fileList.addElement(args[i].equals("-") ? null : args[i]);
 
255
                continue;
 
256
            }
 
257
            if (arg.equals("-sealedlib")) {
 
258
                // Should already be processed
 
259
                if (!sealedStdLib) Kit.codeBug();
 
260
                continue;
 
261
            }
 
262
            usageError = arg;
 
263
            break goodUsage;
 
264
        }
 
265
        // print usage message
 
266
        p(ToolErrorReporter.getMessage("msg.shell.usage", usageError));
 
267
        System.exit(1);
 
268
        return null;
 
269
    }
 
270
 
 
271
    private static void initJavaPolicySecuritySupport()
 
272
    {
 
273
        Throwable exObj;
 
274
        try {
 
275
            Class cl = Class.forName
 
276
                ("org.mozilla.javascript.tools.shell.JavaPolicySecurity");
 
277
            securityImpl = (SecurityProxy)cl.newInstance();
 
278
            SecurityController.initGlobal(securityImpl);
 
279
            return;
 
280
        } catch (ClassNotFoundException ex) {
 
281
            exObj = ex;
 
282
        } catch (IllegalAccessException ex) {
 
283
            exObj = ex;
 
284
        } catch (InstantiationException ex) {
 
285
            exObj = ex;
 
286
        } catch (LinkageError ex) {
 
287
            exObj = ex;
 
288
        }
 
289
        throw Kit.initCause(new IllegalStateException(
 
290
            "Can not load security support: "+exObj), exObj);
 
291
    }
 
292
 
 
293
    /**
 
294
     * Evaluate JavaScript source.
 
295
     *
 
296
     * @param cx the current context
 
297
     * @param filename the name of the file to compile, or null
 
298
     *                 for interactive mode.
 
299
     */
 
300
    public static void processSource(Context cx, String filename)
 
301
    {
 
302
        if (filename == null || filename.equals("-")) {
 
303
            if (filename == null) {
 
304
                // print implementation version
 
305
                getOut().println(cx.getImplementationVersion());
 
306
            }
 
307
 
 
308
            // Use the interpreter for interactive input
 
309
            cx.setOptimizationLevel(-1);
 
310
 
 
311
            BufferedReader in = new BufferedReader
 
312
                (new InputStreamReader(global.getIn()));
 
313
            int lineno = 1;
 
314
            boolean hitEOF = false;
 
315
            while (!hitEOF) {
 
316
                int startline = lineno;
 
317
                if (filename == null)
 
318
                    global.getErr().print("js> ");
 
319
                global.getErr().flush();
 
320
                String source = "";
 
321
 
 
322
                // Collect lines of source to compile.
 
323
                while (true) {
 
324
                    String newline;
 
325
                    try {
 
326
                        newline = in.readLine();
 
327
                    }
 
328
                    catch (IOException ioe) {
 
329
                        global.getErr().println(ioe.toString());
 
330
                        break;
 
331
                    }
 
332
                    if (newline == null) {
 
333
                        hitEOF = true;
 
334
                        break;
 
335
                    }
 
336
                    source = source + newline + "\n";
 
337
                    lineno++;
 
338
                    if (cx.stringIsCompilableUnit(source))
 
339
                        break;
 
340
                }
 
341
                Object result = evaluateScript(cx, global, null, source,
 
342
                                               "<stdin>", startline, null);
 
343
                if (result != cx.getUndefinedValue()) {
 
344
                    try {
 
345
                        global.getErr().println(cx.toString(result));
 
346
                    } catch (RhinoException rex) {
 
347
                        errorReporter.reportException(rex);
 
348
                    }
 
349
                }
 
350
                NativeArray h = global.history;
 
351
                h.put((int)h.getLength(), h, source);
 
352
            }
 
353
            global.getErr().println();
 
354
        } else {
 
355
            processFile(cx, global, filename);
 
356
        }
 
357
        System.gc();
 
358
    }
 
359
 
 
360
    public static void processFile(Context cx, Scriptable scope,
 
361
                                   String filename)
 
362
    {
 
363
        if (securityImpl == null) {
 
364
            processFileSecure(cx, scope, filename, null);
 
365
        } else {
 
366
            securityImpl.callProcessFileSecure(cx, scope, filename);
 
367
        }
 
368
    }
 
369
 
 
370
    static void processFileSecure(Context cx, Scriptable scope,
 
371
                                  String filename, Object securityDomain)
 
372
    {
 
373
        Reader in = null;
 
374
        // Try filename first as URL
 
375
        try {
 
376
            URL url = new URL(filename);
 
377
            InputStream is = url.openStream();
 
378
            in = new BufferedReader(new InputStreamReader(is));
 
379
        }  catch (MalformedURLException mfex) {
 
380
            // fall through to try it as a file
 
381
            in = null;
 
382
        } catch (IOException ioex) {
 
383
            Context.reportError(ToolErrorReporter.getMessage(
 
384
                "msg.couldnt.open.url", filename, ioex.toString()));
 
385
            exitCode = EXITCODE_FILE_NOT_FOUND;
 
386
            return;
 
387
        }
 
388
 
 
389
        if (in == null) {
 
390
            // Try filename as file
 
391
            try {
 
392
                in = new PushbackReader(new FileReader(filename));
 
393
                int c = in.read();
 
394
                // Support the executable script #! syntax:  If
 
395
                // the first line begins with a '#', treat the whole
 
396
                // line as a comment.
 
397
                if (c == '#') {
 
398
                    while ((c = in.read()) != -1) {
 
399
                        if (c == '\n' || c == '\r')
 
400
                            break;
 
401
                    }
 
402
                    ((PushbackReader) in).unread(c);
 
403
                } else {
 
404
                    // No '#' line, just reopen the file and forget it
 
405
                    // ever happened.  OPT closing and reopening
 
406
                    // undoubtedly carries some cost.  Is this faster
 
407
                    // or slower than leaving the PushbackReader
 
408
                    // around?
 
409
                    in.close();
 
410
                    in = new FileReader(filename);
 
411
                }
 
412
                filename = new java.io.File(filename).getCanonicalPath();
 
413
            }
 
414
            catch (FileNotFoundException ex) {
 
415
                Context.reportError(ToolErrorReporter.getMessage(
 
416
                    "msg.couldnt.open",
 
417
                    filename));
 
418
                exitCode = EXITCODE_FILE_NOT_FOUND;
 
419
                return;
 
420
            } catch (IOException ioe) {
 
421
                global.getErr().println(ioe.toString());
 
422
            }
 
423
        }
 
424
        // Here we evalute the entire contents of the file as
 
425
        // a script. Text is printed only if the print() function
 
426
        // is called.
 
427
        evaluateScript(cx, scope, in, null, filename, 1, securityDomain);
 
428
    }
 
429
 
 
430
    public static Object evaluateScript(Context cx, Scriptable scope,
 
431
                                        Reader in, String script,
 
432
                                        String sourceName,
 
433
                                        int lineno, Object securityDomain)
 
434
    {
 
435
        if (!global.initialized) {
 
436
            global.init(cx);
 
437
        }
 
438
        Object result = cx.getUndefinedValue();
 
439
        try {
 
440
            if (in != null) {
 
441
                try {
 
442
                    try {
 
443
                        result = cx.evaluateReader(scope, in,
 
444
                                                   sourceName, lineno,
 
445
                                                   securityDomain);
 
446
                    } finally {
 
447
                        in.close();
 
448
                    }
 
449
                } catch (IOException ioe) {
 
450
                    global.getErr().println(ioe.toString());
 
451
                }
 
452
            } else {
 
453
                result = cx.evaluateString(scope, script, sourceName, lineno,
 
454
                                           securityDomain);
 
455
            }
 
456
        } catch (WrappedException we) {
 
457
            global.getErr().println(we.getWrappedException().toString());
 
458
            we.printStackTrace();
 
459
        } catch (EvaluatorException ee) {
 
460
            // Already printed message.
 
461
            exitCode = EXITCODE_RUNTIME_ERROR;
 
462
        } catch (RhinoException rex) {
 
463
            errorReporter.reportException(rex);
 
464
            exitCode = EXITCODE_RUNTIME_ERROR;
 
465
        } catch (VirtualMachineError ex) {
 
466
            // Treat StackOverflow and OutOfMemory as runtime errors
 
467
            ex.printStackTrace();
 
468
            String msg = ToolErrorReporter.getMessage(
 
469
                "msg.uncaughtJSException", ex.toString());
 
470
            exitCode = EXITCODE_RUNTIME_ERROR;
 
471
            Context.reportError(msg);
 
472
        }
 
473
        return result;
 
474
    }
 
475
 
 
476
    private static void p(String s) {
 
477
        global.getOut().println(s);
 
478
    }
 
479
 
 
480
    public static ScriptableObject getScope() {
 
481
        if (!global.initialized) {
 
482
            global.init(Context.getCurrentContext());
 
483
        }
 
484
        return global;
 
485
    }
 
486
 
 
487
    public static InputStream getIn() {
 
488
        return Global.getInstance(getGlobal()).getIn();
 
489
    }
 
490
 
 
491
    public static void setIn(InputStream in) {
 
492
        Global.getInstance(getGlobal()).setIn(in);
 
493
    }
 
494
 
 
495
    public static PrintStream getOut() {
 
496
        return Global.getInstance(getGlobal()).getOut();
 
497
    }
 
498
 
 
499
    public static void setOut(PrintStream out) {
 
500
        Global.getInstance(getGlobal()).setOut(out);
 
501
    }
 
502
 
 
503
    public static PrintStream getErr() {
 
504
        return Global.getInstance(getGlobal()).getErr();
 
505
    }
 
506
 
 
507
    public static void setErr(PrintStream err) {
 
508
        Global.getInstance(getGlobal()).setErr(err);
 
509
    }
 
510
 
 
511
    static protected final Global global = new Global();
 
512
    static protected ToolErrorReporter errorReporter;
 
513
    static protected int exitCode = 0;
 
514
    static private final int EXITCODE_RUNTIME_ERROR = 3;
 
515
    static private final int EXITCODE_FILE_NOT_FOUND = 4;
 
516
    //static private DebugShell debugShell;
 
517
    static boolean processStdin = true;
 
518
    static boolean sealedStdLib = false;
 
519
    static Vector fileList = new Vector(5);
 
520
    private static SecurityProxy securityImpl;
 
521
}