samples/shell.js

Fri, 21 Dec 2012 16:36:24 -0400

author
jlaskey
date
Fri, 21 Dec 2012 16:36:24 -0400
changeset 3
da1e581c933b
child 7
5a1b0714df0e
permissions
-rw-r--r--

8005403: Open-source Nashorn
Reviewed-by: attila, hannesw, lagergren, sundar
Contributed-by: james.laskey@oracle.com, akhil.arora@oracle.com, andreas.woess@jku.at, attila.szegedi@oracle.com, hannes.wallnoefer@oracle.com, henry.jen@oracle.com, marcus.lagergren@oracle.com, pavel.semenov@oracle.com, pavel.stepanov@oracle.com, petr.hejl@oracle.com, petr.pisl@oracle.com, sundararajan.athijegannathan@oracle.com

jlaskey@3 1 /*
jlaskey@3 2 * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
jlaskey@3 3 *
jlaskey@3 4 * Redistribution and use in source and binary forms, with or without
jlaskey@3 5 * modification, are permitted provided that the following conditions
jlaskey@3 6 * are met:
jlaskey@3 7 *
jlaskey@3 8 * - Redistributions of source code must retain the above copyright
jlaskey@3 9 * notice, this list of conditions and the following disclaimer.
jlaskey@3 10 *
jlaskey@3 11 * - Redistributions in binary form must reproduce the above copyright
jlaskey@3 12 * notice, this list of conditions and the following disclaimer in the
jlaskey@3 13 * documentation and/or other materials provided with the distribution.
jlaskey@3 14 *
jlaskey@3 15 * - Neither the name of Oracle nor the names of its
jlaskey@3 16 * contributors may be used to endorse or promote products derived
jlaskey@3 17 * from this software without specific prior written permission.
jlaskey@3 18 *
jlaskey@3 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
jlaskey@3 20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
jlaskey@3 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
jlaskey@3 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
jlaskey@3 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
jlaskey@3 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
jlaskey@3 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
jlaskey@3 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
jlaskey@3 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
jlaskey@3 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
jlaskey@3 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
jlaskey@3 30 */
jlaskey@3 31
jlaskey@3 32 /**
jlaskey@3 33 * This is a simple shell tool in JavaScript.
jlaskey@3 34 *
jlaskey@3 35 * Runs any operating system command using Java "exec". When "eval" command is
jlaskey@3 36 * used, evaluates argument(s) as JavaScript code.
jlaskey@3 37 */
jlaskey@3 38
jlaskey@3 39 var imports = new JavaImporter(java.io, java.lang, java.util);
jlaskey@3 40
jlaskey@3 41 function prompt() {
jlaskey@3 42 java.lang.System.out.print(">");
jlaskey@3 43 }
jlaskey@3 44
jlaskey@3 45 with (imports) {
jlaskey@3 46 var reader = new BufferedReader(new InputStreamReader(System["in"]));
jlaskey@3 47 var line = null;
jlaskey@3 48 prompt();
jlaskey@3 49 while ((line = reader.readLine()) != null) {
jlaskey@3 50 if (line != "") {
jlaskey@3 51 var args = line.split(" ");
jlaskey@3 52 try {
jlaskey@3 53 if (args[0] == "eval") {
jlaskey@3 54 var code = line.substring("eval".length);
jlaskey@3 55 var res = eval(code);
jlaskey@3 56 if (res != undefined) {
jlaskey@3 57 print(res);
jlaskey@3 58 }
jlaskey@3 59 } else {
jlaskey@3 60 var argList = new ArrayList();
jlaskey@3 61 for (i in args) { argList.add(args[i]); }
jlaskey@3 62 var procBuilder = new ProcessBuilder(argList);
jlaskey@3 63 procBuilder.redirectErrorStream();
jlaskey@3 64 var proc = procBuilder.start();
jlaskey@3 65 var out = new BufferedReader(new InputStreamReader(proc.getInputStream()));
jlaskey@3 66 var line = null;
jlaskey@3 67 while ((line = out.readLine()) != null) {
jlaskey@3 68 System.out.println(line);
jlaskey@3 69 }
jlaskey@3 70 proc.waitFor();
jlaskey@3 71 }
jlaskey@3 72 } catch (e) {
jlaskey@3 73 print(e);
jlaskey@3 74 }
jlaskey@3 75 }
jlaskey@3 76 prompt();
jlaskey@3 77 }
jlaskey@3 78 }

mercurial