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. 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.internal.runtime;
27
28 import static jdk.nashorn.internal.lookup.Lookup.MH;
29 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
30 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
31
32 import java.io.BufferedReader;
33 import java.io.File;
34 import java.io.IOException;
35 import java.io.InputStreamReader;
36 import java.io.OutputStreamWriter;
37 import java.lang.invoke.MethodHandle;
38 import java.lang.invoke.MethodHandles;
39 import java.util.Map;
40 import java.util.StringTokenizer;
41
42 /**
43 * Global functions supported only in scripting mode.
44 */
45 public final class ScriptingFunctions {
46
47 /** Handle to implementation of {@link ScriptingFunctions#readLine} - Nashorn extension */
48 public static final MethodHandle READLINE = findOwnMH("readLine", Object.class, Object.class, Object.class);
49
50 /** Handle to implementation of {@link ScriptingFunctions#readFully} - Nashorn extension */
51 public static final MethodHandle READFULLY = findOwnMH("readFully", Object.class, Object.class, Object.class);
52
53 /** Handle to implementation of {@link ScriptingFunctions#exec} - Nashorn extension */
54 public static final MethodHandle EXEC = findOwnMH("exec", Object.class, Object.class, Object.class, Object.class);
55
56 /** EXEC name - special property used by $EXEC API. */
57 public static final String EXEC_NAME = "$EXEC";
58
59 /** OUT name - special property used by $EXEC API. */
60 public static final String OUT_NAME = "$OUT";
116 }
117
118 return new String(Source.readFully(f));
119 }
120
121 /**
122 * Nashorn extension: exec a string in a separate process.
123 *
124 * @param self self reference
125 * @param string string to execute
126 * @param input input
127 *
128 * @return output string from the request
129 * @throws IOException if any stream access fails
130 * @throws InterruptedException if execution is interrupted
131 */
132 public static Object exec(final Object self, final Object string, final Object input) throws IOException, InterruptedException {
133 // Current global is need to fetch additional inputs and for additional results.
134 final ScriptObject global = Context.getGlobal();
135
136 // Break exec string into tokens.
137 final StringTokenizer tokenizer = new StringTokenizer(JSType.toString(string));
138 final String[] cmdArray = new String[tokenizer.countTokens()];
139 for (int i = 0; tokenizer.hasMoreTokens(); i++) {
140 cmdArray[i] = tokenizer.nextToken();
141 }
142
143 // Set up initial process.
144 final ProcessBuilder processBuilder = new ProcessBuilder(cmdArray);
145
146 // Current ENV property state.
147 final Object env = global.get(ENV_NAME);
148 if (env instanceof ScriptObject) {
149 final ScriptObject envProperties = (ScriptObject)env;
150
151 // If a working directory is present, use it.
152 final Object pwd = envProperties.get(PWD_NAME);
153 if (pwd != UNDEFINED) {
154 processBuilder.directory(new File(JSType.toString(pwd)));
155 }
156
157 // Set up ENV variables.
158 final Map<String, String> environment = processBuilder.environment();
159 environment.clear();
160 for (final Map.Entry<Object, Object> entry : envProperties.entrySet()) {
161 environment.put(JSType.toString(entry.getKey()), JSType.toString(entry.getValue()));
162 }
163 }
164
222
223 // Set globals for secondary results.
224 global.set(OUT_NAME, out, 0);
225 global.set(ERR_NAME, err, 0);
226 global.set(EXIT_NAME, exit, 0);
227
228 // Propagate exception if present.
229 for (final IOException element : exception) {
230 if (element != null) {
231 throw element;
232 }
233 }
234
235 // Return the result from stdout.
236 return out;
237 }
238
239 private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
240 return MH.findStatic(MethodHandles.lookup(), ScriptingFunctions.class, name, MH.type(rtype, types));
241 }
242 }
|
1 /*
2 * Copyright (c) 2010, 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.internal.runtime;
27
28 import static jdk.nashorn.internal.lookup.Lookup.MH;
29 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
30 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
31
32 import java.io.BufferedReader;
33 import java.io.File;
34 import java.io.IOException;
35 import java.io.InputStreamReader;
36 import java.io.OutputStreamWriter;
37 import java.io.StreamTokenizer;
38 import java.io.StringReader;
39 import java.lang.invoke.MethodHandle;
40 import java.lang.invoke.MethodHandles;
41 import java.util.ArrayList;
42 import java.util.List;
43 import java.util.Map;
44
45 /**
46 * Global functions supported only in scripting mode.
47 */
48 public final class ScriptingFunctions {
49
50 /** Handle to implementation of {@link ScriptingFunctions#readLine} - Nashorn extension */
51 public static final MethodHandle READLINE = findOwnMH("readLine", Object.class, Object.class, Object.class);
52
53 /** Handle to implementation of {@link ScriptingFunctions#readFully} - Nashorn extension */
54 public static final MethodHandle READFULLY = findOwnMH("readFully", Object.class, Object.class, Object.class);
55
56 /** Handle to implementation of {@link ScriptingFunctions#exec} - Nashorn extension */
57 public static final MethodHandle EXEC = findOwnMH("exec", Object.class, Object.class, Object.class, Object.class);
58
59 /** EXEC name - special property used by $EXEC API. */
60 public static final String EXEC_NAME = "$EXEC";
61
62 /** OUT name - special property used by $EXEC API. */
63 public static final String OUT_NAME = "$OUT";
119 }
120
121 return new String(Source.readFully(f));
122 }
123
124 /**
125 * Nashorn extension: exec a string in a separate process.
126 *
127 * @param self self reference
128 * @param string string to execute
129 * @param input input
130 *
131 * @return output string from the request
132 * @throws IOException if any stream access fails
133 * @throws InterruptedException if execution is interrupted
134 */
135 public static Object exec(final Object self, final Object string, final Object input) throws IOException, InterruptedException {
136 // Current global is need to fetch additional inputs and for additional results.
137 final ScriptObject global = Context.getGlobal();
138
139 // Set up initial process.
140 final ProcessBuilder processBuilder = new ProcessBuilder(tokenizeCommandLine(JSType.toString(string)));
141
142 // Current ENV property state.
143 final Object env = global.get(ENV_NAME);
144 if (env instanceof ScriptObject) {
145 final ScriptObject envProperties = (ScriptObject)env;
146
147 // If a working directory is present, use it.
148 final Object pwd = envProperties.get(PWD_NAME);
149 if (pwd != UNDEFINED) {
150 processBuilder.directory(new File(JSType.toString(pwd)));
151 }
152
153 // Set up ENV variables.
154 final Map<String, String> environment = processBuilder.environment();
155 environment.clear();
156 for (final Map.Entry<Object, Object> entry : envProperties.entrySet()) {
157 environment.put(JSType.toString(entry.getKey()), JSType.toString(entry.getValue()));
158 }
159 }
160
218
219 // Set globals for secondary results.
220 global.set(OUT_NAME, out, 0);
221 global.set(ERR_NAME, err, 0);
222 global.set(EXIT_NAME, exit, 0);
223
224 // Propagate exception if present.
225 for (final IOException element : exception) {
226 if (element != null) {
227 throw element;
228 }
229 }
230
231 // Return the result from stdout.
232 return out;
233 }
234
235 private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
236 return MH.findStatic(MethodHandles.lookup(), ScriptingFunctions.class, name, MH.type(rtype, types));
237 }
238
239 /**
240 * Break an exec string into tokens, honoring quoted arguments and escaped
241 * spaces.
242 *
243 * @param execString a String with the command line to execute.
244 * @return a {@link List} of {@link String}s representing the tokens that
245 * constitute the command line.
246 */
247 private static List<String> tokenizeCommandLine(String execString) throws IOException {
248 StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(execString));
249 tokenizer.resetSyntax();
250 tokenizer.wordChars(0, 255);
251 tokenizer.whitespaceChars(0, ' ');
252 tokenizer.commentChar('#');
253 tokenizer.quoteChar('"');
254 tokenizer.quoteChar('\'');
255 List<String> cmdList = new ArrayList<>();
256 String toAppend = "";
257 while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) {
258 String s = tokenizer.sval;
259 // The tokenizer understands about honoring quoted strings and recognizes
260 // them as one token that possibly contains multiple space-separated words.
261 // It does not recognize quoted spaces, though, and will split after the
262 // escaping \ character. This is handled here.
263 if (s.endsWith("\\")) {
264 // omit trailing \, append space instead
265 toAppend += s.substring(0, s.length() - 1) + " ";
266 } else {
267 cmdList.add(toAppend + s);
268 toAppend = "";
269 }
270 }
271 return cmdList;
272 }
273 }
|