1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.tools.jjs;
  27 
  28 import java.io.BufferedReader;
  29 import java.io.File;
  30 import java.io.InputStream;
  31 import java.io.InputStreamReader;
  32 import java.io.IOException;
  33 import java.io.OutputStream;
  34 import java.io.PrintWriter;
  35 import jdk.internal.jline.console.completer.Completer;
  36 import jdk.internal.jline.console.UserInterruptException;
  37 import jdk.nashorn.api.scripting.NashornException;
  38 import jdk.nashorn.internal.objects.Global;
  39 import jdk.nashorn.internal.runtime.Context;
  40 import jdk.nashorn.internal.runtime.JSType;
  41 import jdk.nashorn.internal.runtime.ScriptEnvironment;
  42 import jdk.nashorn.internal.runtime.ScriptRuntime;
  43 import jdk.nashorn.tools.Shell;
  44 
  45 /**
  46  * Interactive command line Shell for Nashorn.
  47  */
  48 public final class Main extends Shell {
  49     private Main() {}
  50 
  51     // file where history is persisted.
  52     private static final File HIST_FILE = new File(new File(System.getProperty("user.home")), ".jjs.history");
  53 
  54     /**
  55      * Main entry point with the default input, output and error streams.
  56      *
  57      * @param args The command line arguments
  58      */
  59     public static void main(final String[] args) {
  60         try {
  61             final int exitCode = main(System.in, System.out, System.err, args);
  62             if (exitCode != SUCCESS) {
  63                 System.exit(exitCode);
  64             }
  65         } catch (final IOException e) {
  66             System.err.println(e); //bootstrapping, Context.err may not exist
  67             System.exit(IO_ERROR);
  68         }
  69     }
  70 
  71     /**
  72      * Starting point for executing a {@code Shell}. Starts a shell with the
  73      * given arguments and streams and lets it run until exit.
  74      *
  75      * @param in input stream for Shell
  76      * @param out output stream for Shell
  77      * @param err error stream for Shell
  78      * @param args arguments to Shell
  79      *
  80      * @return exit code
  81      *
  82      * @throws IOException if there's a problem setting up the streams
  83      */
  84     public static int main(final InputStream in, final OutputStream out, final OutputStream err, final String[] args) throws IOException {
  85         return new Main().run(in, out, err, args);
  86     }
  87 
  88 
  89     /**
  90      * read-eval-print loop for Nashorn shell.
  91      *
  92      * @param context the nashorn context
  93      * @param global  global scope object to use
  94      * @return return code
  95      */
  96     protected int readEvalPrint(final Context context, final Global global) {
  97         final ScriptEnvironment env = context.getEnv();
  98         final String prompt = bundle.getString("shell.prompt");
  99         final PrintWriter err = context.getErr();
 100         final Global oldGlobal = Context.getGlobal();
 101         final boolean globalChanged = (oldGlobal != global);
 102         final Completer completer = new NashornCompleter(context, global, this);
 103 
 104         try (final Console in = new Console(System.in, System.out, HIST_FILE, completer)) {
 105             if (globalChanged) {
 106                 Context.setGlobal(global);
 107             }
 108 
 109             global.addShellBuiltins();
 110             // expose history object for reflecting on command line history
 111             global.put("history", new HistoryObject(in.getHistory()), false);
 112 
 113             while (true) {
 114                 String source = "";
 115                 try {
 116                     source = in.readLine(prompt);
 117                 } catch (final IOException ioe) {
 118                     err.println(ioe.toString());
 119                     if (env._dump_on_error) {
 120                         ioe.printStackTrace(err);
 121                     }
 122                     return IO_ERROR;
 123                 } catch (final UserInterruptException ex) {
 124                     break;
 125                 }
 126 
 127                 if (source.isEmpty()) {
 128                     continue;
 129                 }
 130 
 131                 try {
 132                     final Object res = context.eval(global, source, global, "<shell>");
 133                     if (res != ScriptRuntime.UNDEFINED) {
 134                         err.println(JSType.toString(res));
 135                     }
 136                 } catch (final Exception e) {
 137                     err.println(e);
 138                     if (env._dump_on_error) {
 139                         e.printStackTrace(err);
 140                     }
 141                 }
 142             }
 143         } catch (final Exception e) {
 144             err.println(e);
 145             if (env._dump_on_error) {
 146                 e.printStackTrace(err);
 147             }
 148         } finally {
 149             if (globalChanged) {
 150                 Context.setGlobal(oldGlobal);
 151             }
 152         }
 153 
 154         return SUCCESS;
 155     }
 156 
 157     // This override exists precisely give access to this method to NashornCompleter!
 158     @Override
 159     protected int getLastExpressionStart(final Context context, final String code) {
 160         return super.getLastExpressionStart(context, code);
 161     }
 162 }