1 /*
   2  * Copyright (c) 2010, 2013, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * Initialization script for shell when running in interactive mode.
  26  */
  27 
  28 /**
  29  * Reads zero or more lines from standard input and returns concatenated string
  30  *
  31  * @param endMarker marker string that signals end of input
  32  * @param prompt prompt printed for each line
  33  */
  34 Object.defineProperty(this, "input", {
  35     value: function input(endMarker, prompt) {
  36         if (!endMarker) {
  37             endMarker = "";
  38         }
  39 
  40         if (!prompt) {
  41             prompt = " >> ";
  42         }
  43 
  44         var imports = new JavaImporter(java.io, java.lang);
  45         var str = "";
  46         with (imports) {
  47             var reader = new BufferedReader(new InputStreamReader(System['in']));
  48             var line;
  49             while (true) {
  50                 System.out.print(prompt);
  51                 line = reader.readLine();
  52                 if (line == null || line == endMarker) {
  53                     break;
  54                 }
  55                 str += line + "\n";
  56             }
  57         }
  58 
  59         return str;
  60     },
  61     enumerable: false,
  62     writable: true,
  63     configurable: true
  64 });
  65 
  66 
  67 /**
  68  * Reads zero or more lines from standard input and evaluates the concatenated
  69  * string as code
  70  *
  71  * @param endMarker marker string that signals end of input
  72  * @param prompt prompt printed for each line
  73  */
  74 Object.defineProperty(this, "evalinput", {
  75     value: function evalinput(endMarker, prompt) {
  76         var code = input(endMarker, prompt);
  77         // make sure everything is evaluated in global scope!
  78         return this.eval(code);
  79     },
  80     enumerable: false,
  81     writable: true,
  82     configurable: true
  83 });