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;
  27 
  28 import static jdk.nashorn.internal.lookup.Lookup.MH;
  29 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
  30 
  31 import java.io.BufferedReader;
  32 import java.io.IOException;
  33 import java.io.InputStreamReader;
  34 import java.lang.invoke.MethodHandle;
  35 import java.lang.invoke.MethodHandles;
  36 import jdk.nashorn.internal.runtime.JSType;
  37 import jdk.nashorn.internal.runtime.ScriptingFunctions;
  38 import jdk.nashorn.internal.objects.Global;
  39 
  40 /**
  41  * Global functions supported only in shell interactive mode.
  42  */
  43 public final class ShellFunctions {
  44 
  45     /** Handle to implementation of {@link ShellFunctions#input} - Nashorn extension */
  46     public static final MethodHandle INPUT = findOwnMH("input", Object.class, Object.class, Object.class, Object.class);
  47 
  48     /** Handle to implementation of {@link ShellFunctions#evalinput} - Nashorn extension */
  49     public static final MethodHandle EVALINPUT = findOwnMH("evalinput",     Object.class, Object.class, Object.class, Object.class);
  50 
  51     private ShellFunctions() {
  52     }
  53 
  54     /**
  55      * Nashorn extension: global.input (shell-interactive-mode-only)
  56      * Read one or more lines of input from the standard input till the
  57      * given end marker is seen in standard input.
  58      *
  59      * @param self   self reference
  60      * @param endMarker String used as end marker for input
  61      * @param prompt String used as input prompt
  62      *
  63      * @return line that was read
  64      *
  65      * @throws IOException if an exception occurs
  66      */
  67     public static Object input(final Object self, final Object endMarker, final Object prompt) throws IOException {
  68         final String endMarkerStr = (endMarker != UNDEFINED)? JSType.toString(endMarker) : "";
  69         final String promptStr = (prompt != UNDEFINED)? JSType.toString(prompt)  : ">> ";
  70         final StringBuilder buf = new StringBuilder();
  71         while (true) {
  72             final String line = ScriptingFunctions.readLine(promptStr);
  73             if (line == null || line.equals(endMarkerStr)) {
  74                 break;
  75             }
  76             buf.append(line);
  77             buf.append('\n');
  78         }
  79         return buf.toString();
  80     }
  81 
  82     /**
  83      * Nashorn extension: Reads zero or more lines from standard input and
  84      * evaluates the concatenated string as code
  85      *
  86      * @param self self reference
  87      * @param endMarker String used as end marker for input
  88      * @param prompt String used as input prompt
  89      *
  90      * @return output from evaluating the script
  91      *
  92      * @throws IOException if an exception occurs
  93      */
  94     public static Object evalinput(final Object self, final Object endMarker, final Object prompt) throws IOException {
  95         return Global.eval(self, input(self, endMarker, prompt));
  96     }
  97 
  98     private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
  99         return MH.findStatic(MethodHandles.lookup(), ShellFunctions.class, name, MH.type(rtype, types));
 100     }
 101 }