samples/shell.js

changeset 851
41be00d23622
parent 7
5a1b0714df0e
child 952
6d5471a497fb
child 962
ac62e33a99b0
equal deleted inserted replaced
850:90d417fd526c 851:41be00d23622
1 /* 1 /*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 7 *
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32 /** 32 // Usage: jjs shell.js
33 * This is a simple shell tool in JavaScript. 33
34 /* This is a simple shell tool in JavaScript.
34 * 35 *
35 * Runs any operating system command using Java "exec". When "eval" command is 36 * Runs any operating system command using Java "exec". When "eval" command is
36 * used, evaluates argument(s) as JavaScript code. 37 * used, evaluates argument(s) as JavaScript code.
37 */ 38 */
38 39
39 var imports = new JavaImporter(java.io, java.lang, java.util); 40 (function() {
41 // Java classes used
42 var Arrays = Java.type("java.util.Arrays");
43 var BufferedReader = Java.type("java.io.BufferedReader");
44 var InputStreamReader = Java.type("java.io.InputStreamReader");
45 var ProcessBuilder = Java.type("java.lang.ProcessBuilder");
46 var System = Java.type("java.lang.System");
40 47
41 function prompt() { 48 // print prompt
42 java.lang.System.out.print(">"); 49 function prompt() {
43 } 50 System.out.print("> ");
51 }
44 52
45 with (imports) { 53 var reader = new BufferedReader(new InputStreamReader(System.in));
46 var reader = new BufferedReader(new InputStreamReader(System["in"]));
47 var line = null;
48 prompt(); 54 prompt();
49 while ((line = reader.readLine()) != null) { 55 // read and evaluate each line from stdin
50 if (line != "") { 56 reader.lines().forEach(function(line) {
51 var args = line.split(" "); 57 if (! line.isEmpty()) {
58 var args = line.split(' ');
52 try { 59 try {
53 if (args[0] == "eval") { 60 // special 'eval' command to evaluate JS code
54 var code = line.substring("eval".length); 61 if (args[0] == 'eval') {
62 var code = line.substring('eval'.length);
55 var res = eval(code); 63 var res = eval(code);
56 if (res != undefined) { 64 if (res != undefined) {
57 print(res); 65 print(res);
58 } 66 }
59 } else { 67 } else {
60 var argList = new ArrayList(); 68 // build child process and start it!
61 for (i in args) { argList.add(args[i]); } 69 new ProcessBuilder(Arrays.asList(args))
62 var procBuilder = new ProcessBuilder(argList); 70 .inheritIO()
63 procBuilder.redirectErrorStream(); 71 .start()
64 var proc = procBuilder.start(); 72 .waitFor();
65 var out = new BufferedReader(new InputStreamReader(proc.getInputStream()));
66 var line = null;
67 while ((line = out.readLine()) != null) {
68 System.out.println(line);
69 }
70 proc.waitFor();
71 } 73 }
72 } catch (e) { 74 } catch (e) {
75 // print exception, if any
73 print(e); 76 print(e);
74 } 77 }
75 } 78 }
76 prompt(); 79 prompt();
77 } 80 })
78 } 81 })()

mercurial