Java Code Examples for jdk.nashorn.internal.runtime.ScriptingFunctions

The following examples show how to use jdk.nashorn.internal.runtime.ScriptingFunctions. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: TencentKona-8   Source File: Parser.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Convert execString to a call to $EXEC.
 *
 * @param primaryToken Original string token.
 * @return callNode to $EXEC.
 */
CallNode execString(final int primaryLine, final long primaryToken) {
    // Synthesize an ident to call $EXEC.
    final IdentNode execIdent = new IdentNode(primaryToken, finish, ScriptingFunctions.EXEC_NAME);
    // Skip over EXECSTRING.
    next();
    // Set up argument list for call.
    // Skip beginning of edit string expression.
    expect(LBRACE);
    // Add the following expression to arguments.
    final List<Expression> arguments = Collections.singletonList(expression());
    // Skip ending of edit string expression.
    expect(RBRACE);

    return new CallNode(primaryLine, primaryToken, finish, execIdent, arguments, false);
}
 
Example 2
Source Project: jdk8u60   Source File: Parser.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Convert execString to a call to $EXEC.
 *
 * @param primaryToken Original string token.
 * @return callNode to $EXEC.
 */
CallNode execString(final int primaryLine, final long primaryToken) {
    // Synthesize an ident to call $EXEC.
    final IdentNode execIdent = new IdentNode(primaryToken, finish, ScriptingFunctions.EXEC_NAME);
    // Skip over EXECSTRING.
    next();
    // Set up argument list for call.
    // Skip beginning of edit string expression.
    expect(LBRACE);
    // Add the following expression to arguments.
    final List<Expression> arguments = Collections.singletonList(expression());
    // Skip ending of edit string expression.
    expect(RBRACE);

    return new CallNode(primaryLine, primaryToken, finish, execIdent, arguments, false);
}
 
Example 3
Source Project: openjdk-jdk8u   Source File: Parser.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Convert execString to a call to $EXEC.
 *
 * @param primaryToken Original string token.
 * @return callNode to $EXEC.
 */
CallNode execString(final int primaryLine, final long primaryToken) {
    // Synthesize an ident to call $EXEC.
    final IdentNode execIdent = new IdentNode(primaryToken, finish, ScriptingFunctions.EXEC_NAME);
    // Skip over EXECSTRING.
    next();
    // Set up argument list for call.
    // Skip beginning of edit string expression.
    expect(LBRACE);
    // Add the following expression to arguments.
    final List<Expression> arguments = Collections.singletonList(expression());
    // Skip ending of edit string expression.
    expect(RBRACE);

    return new CallNode(primaryLine, primaryToken, finish, execIdent, arguments, false);
}
 
Example 4
Source Project: openjdk-jdk8u-backup   Source File: Parser.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Convert execString to a call to $EXEC.
 *
 * @param primaryToken Original string token.
 * @return callNode to $EXEC.
 */
CallNode execString(final int primaryLine, final long primaryToken) {
    // Synthesize an ident to call $EXEC.
    final IdentNode execIdent = new IdentNode(primaryToken, finish, ScriptingFunctions.EXEC_NAME);
    // Skip over EXECSTRING.
    next();
    // Set up argument list for call.
    // Skip beginning of edit string expression.
    expect(LBRACE);
    // Add the following expression to arguments.
    final List<Expression> arguments = Collections.singletonList(expression());
    // Skip ending of edit string expression.
    expect(RBRACE);

    return new CallNode(primaryLine, primaryToken, finish, execIdent, arguments, false);
}
 
Example 5
Source Project: hottub   Source File: Parser.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Convert execString to a call to $EXEC.
 *
 * @param primaryToken Original string token.
 * @return callNode to $EXEC.
 */
CallNode execString(final int primaryLine, final long primaryToken) {
    // Synthesize an ident to call $EXEC.
    final IdentNode execIdent = new IdentNode(primaryToken, finish, ScriptingFunctions.EXEC_NAME);
    // Skip over EXECSTRING.
    next();
    // Set up argument list for call.
    // Skip beginning of edit string expression.
    expect(LBRACE);
    // Add the following expression to arguments.
    final List<Expression> arguments = Collections.singletonList(expression());
    // Skip ending of edit string expression.
    expect(RBRACE);

    return new CallNode(primaryLine, primaryToken, finish, execIdent, arguments, false);
}
 
Example 6
Source Project: openjdk-8-source   Source File: Parser.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Convert execString to a call to $EXEC.
 *
 * @param primaryToken Original string token.
 * @return callNode to $EXEC.
 */
CallNode execString(final int primaryLine, final long primaryToken) {
    // Synthesize an ident to call $EXEC.
    final IdentNode execIdent = new IdentNode(primaryToken, finish, ScriptingFunctions.EXEC_NAME);
    // Skip over EXECSTRING.
    next();
    // Set up argument list for call.
    // Skip beginning of edit string expression.
    expect(LBRACE);
    // Add the following expression to arguments.
    final List<Expression> arguments = Collections.singletonList(expression());
    // Skip ending of edit string expression.
    expect(RBRACE);

    return new CallNode(primaryLine, primaryToken, finish, execIdent, arguments);
}
 
Example 7
Source Project: openjdk-8   Source File: Parser.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Convert execString to a call to $EXEC.
 *
 * @param primaryToken Original string token.
 * @return callNode to $EXEC.
 */
CallNode execString(final int primaryLine, final long primaryToken) {
    // Synthesize an ident to call $EXEC.
    final IdentNode execIdent = new IdentNode(primaryToken, finish, ScriptingFunctions.EXEC_NAME);
    // Skip over EXECSTRING.
    next();
    // Set up argument list for call.
    // Skip beginning of edit string expression.
    expect(LBRACE);
    // Add the following expression to arguments.
    final List<Expression> arguments = Collections.singletonList(expression());
    // Skip ending of edit string expression.
    expect(RBRACE);

    return new CallNode(primaryLine, primaryToken, finish, execIdent, arguments);
}
 
Example 8
Source Project: jdk8u_nashorn   Source File: Parser.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Convert execString to a call to $EXEC.
 *
 * @param primaryToken Original string token.
 * @return callNode to $EXEC.
 */
CallNode execString(final int primaryLine, final long primaryToken) {
    // Synthesize an ident to call $EXEC.
    final IdentNode execIdent = new IdentNode(primaryToken, finish, ScriptingFunctions.EXEC_NAME);
    // Skip over EXECSTRING.
    next();
    // Set up argument list for call.
    // Skip beginning of edit string expression.
    expect(LBRACE);
    // Add the following expression to arguments.
    final List<Expression> arguments = Collections.singletonList(expression());
    // Skip ending of edit string expression.
    expect(RBRACE);

    return new CallNode(primaryLine, primaryToken, finish, execIdent, arguments, false);
}
 
Example 9
Source Project: nashorn   Source File: Parser.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Convert execString to a call to $EXEC.
 *
 * @param primaryToken Original string token.
 * @return callNode to $EXEC.
 */
CallNode execString(final int primaryLine, final long primaryToken) {
    // Synthesize an ident to call $EXEC.
    final IdentNode execIdent = new IdentNode(primaryToken, finish, ScriptingFunctions.EXEC_NAME);
    // Skip over EXECSTRING.
    next();
    // Set up argument list for call.
    // Skip beginning of edit string expression.
    expect(LBRACE);
    // Add the following expression to arguments.
    final List<Expression> arguments = Collections.singletonList(expression());
    // Skip ending of edit string expression.
    expect(RBRACE);

    return new CallNode(primaryLine, primaryToken, finish, execIdent, arguments);
}
 
Example 10
Source Project: openjdk-jdk9   Source File: ShellFunctions.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: global.input (shell-interactive-mode-only)
 * Read one or more lines of input from the standard input till the
 * given end marker is seen in standard input.
 *
 * @param self   self reference
 * @param endMarker String used as end marker for input
 * @param prompt String used as input prompt
 *
 * @return line that was read
 *
 * @throws IOException if an exception occurs
 */
public static Object input(final Object self, final Object endMarker, final Object prompt) throws IOException {
    final String endMarkerStr = (endMarker != UNDEFINED)? JSType.toString(endMarker) : "";
    final String promptStr = (prompt != UNDEFINED)? JSType.toString(prompt)  : ">> ";
    final StringBuilder buf = new StringBuilder();
    while (true) {
        final String line = ScriptingFunctions.readLine(promptStr);
        if (line == null || line.equals(endMarkerStr)) {
            break;
        }
        buf.append(line);
        buf.append('\n');
    }
    return buf.toString();
}
 
Example 11
Source Project: openjdk-8-source   Source File: Global.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void initScripting(final ScriptEnvironment scriptEnv) {
    Object value;
    value = ScriptFunctionImpl.makeFunction("readLine", ScriptingFunctions.READLINE);
    addOwnProperty("readLine", Attribute.NOT_ENUMERABLE, value);

    value = ScriptFunctionImpl.makeFunction("readFully", ScriptingFunctions.READFULLY);
    addOwnProperty("readFully", Attribute.NOT_ENUMERABLE, value);

    final String execName = ScriptingFunctions.EXEC_NAME;
    value = ScriptFunctionImpl.makeFunction(execName, ScriptingFunctions.EXEC);
    addOwnProperty(execName, Attribute.NOT_ENUMERABLE, value);

    // Nashorn extension: global.echo (scripting-mode-only)
    // alias for "print"
    value = get("print");
    addOwnProperty("echo", Attribute.NOT_ENUMERABLE, value);

    // Nashorn extension: global.$OPTIONS (scripting-mode-only)
    final ScriptObject options = newObject();
    copyOptions(options, scriptEnv);
    addOwnProperty("$OPTIONS", Attribute.NOT_ENUMERABLE, options);

    // Nashorn extension: global.$ENV (scripting-mode-only)
    if (System.getSecurityManager() == null) {
        // do not fill $ENV if we have a security manager around
        // Retrieve current state of ENV variables.
        final ScriptObject env = newObject();
        env.putAll(System.getenv(), scriptEnv._strict);
        addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, env);
    } else {
        addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    }

    // add other special properties for exec support
    addOwnProperty(ScriptingFunctions.OUT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    addOwnProperty(ScriptingFunctions.ERR_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    addOwnProperty(ScriptingFunctions.EXIT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
}
 
Example 12
Source Project: openjdk-8   Source File: Global.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void initScripting(final ScriptEnvironment scriptEnv) {
    Object value;
    value = ScriptFunctionImpl.makeFunction("readLine", ScriptingFunctions.READLINE);
    addOwnProperty("readLine", Attribute.NOT_ENUMERABLE, value);

    value = ScriptFunctionImpl.makeFunction("readFully", ScriptingFunctions.READFULLY);
    addOwnProperty("readFully", Attribute.NOT_ENUMERABLE, value);

    final String execName = ScriptingFunctions.EXEC_NAME;
    value = ScriptFunctionImpl.makeFunction(execName, ScriptingFunctions.EXEC);
    addOwnProperty(execName, Attribute.NOT_ENUMERABLE, value);

    // Nashorn extension: global.echo (scripting-mode-only)
    // alias for "print"
    value = get("print");
    addOwnProperty("echo", Attribute.NOT_ENUMERABLE, value);

    // Nashorn extension: global.$OPTIONS (scripting-mode-only)
    final ScriptObject options = newObject();
    copyOptions(options, scriptEnv);
    addOwnProperty("$OPTIONS", Attribute.NOT_ENUMERABLE, options);

    // Nashorn extension: global.$ENV (scripting-mode-only)
    if (System.getSecurityManager() == null) {
        // do not fill $ENV if we have a security manager around
        // Retrieve current state of ENV variables.
        final ScriptObject env = newObject();
        env.putAll(System.getenv(), scriptEnv._strict);
        addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, env);
    } else {
        addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    }

    // add other special properties for exec support
    addOwnProperty(ScriptingFunctions.OUT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    addOwnProperty(ScriptingFunctions.ERR_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    addOwnProperty(ScriptingFunctions.EXIT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
}
 
Example 13
Source Project: nashorn   Source File: Global.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void initScripting(final ScriptEnvironment scriptEnv) {
    Object value;
    value = ScriptFunctionImpl.makeFunction("readLine", ScriptingFunctions.READLINE);
    addOwnProperty("readLine", Attribute.NOT_ENUMERABLE, value);

    value = ScriptFunctionImpl.makeFunction("readFully", ScriptingFunctions.READFULLY);
    addOwnProperty("readFully", Attribute.NOT_ENUMERABLE, value);

    final String execName = ScriptingFunctions.EXEC_NAME;
    value = ScriptFunctionImpl.makeFunction(execName, ScriptingFunctions.EXEC);
    addOwnProperty(execName, Attribute.NOT_ENUMERABLE, value);

    // Nashorn extension: global.echo (scripting-mode-only)
    // alias for "print"
    value = get("print");
    addOwnProperty("echo", Attribute.NOT_ENUMERABLE, value);

    // Nashorn extension: global.$OPTIONS (scripting-mode-only)
    final ScriptObject options = newObject();
    copyOptions(options, scriptEnv);
    addOwnProperty("$OPTIONS", Attribute.NOT_ENUMERABLE, options);

    // Nashorn extension: global.$ENV (scripting-mode-only)
    if (System.getSecurityManager() == null) {
        // do not fill $ENV if we have a security manager around
        // Retrieve current state of ENV variables.
        final ScriptObject env = newObject();
        env.putAll(System.getenv(), scriptEnv._strict);
        addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, env);
    } else {
        addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    }

    // add other special properties for exec support
    addOwnProperty(ScriptingFunctions.OUT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    addOwnProperty(ScriptingFunctions.ERR_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    addOwnProperty(ScriptingFunctions.EXIT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
}
 
Example 14
Source Project: TencentKona-8   Source File: Shell.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Preprocess the command line arguments passed in by the shell. This method checks, for the first non-option
 * argument, whether the file denoted by it begins with a shebang line. If so, it is assumed that execution in
 * shebang mode is intended. The consequence of this is that the identified script file will be treated as the
 * <em>only</em> script file, and all subsequent arguments will be regarded as arguments to the script.
 * <p>
 * This method canonicalizes the command line arguments to the form {@code <options> <script> -- <arguments>} if a
 * shebang script is identified. On platforms that pass shebang arguments as single strings, the shebang arguments
 * will be broken down into single arguments; whitespace is used as separator.
 * <p>
 * Shebang mode is entered regardless of whether the script is actually run directly from the shell, or indirectly
 * via the {@code jjs} executable. It is the user's / script author's responsibility to ensure that the arguments
 * given on the shebang line do not lead to a malformed argument sequence. In particular, the shebang arguments
 * should not contain any whitespace for purposes other than separating arguments, as the different platforms deal
 * with whitespace in different and incompatible ways.
 * <p>
 * @implNote Example:<ul>
 * <li>Shebang line in {@code script.js}: {@code #!/path/to/jjs --language=es6}</li>
 * <li>Command line: {@code ./script.js arg2}</li>
 * <li>{@code args} array passed to Nashorn: {@code --language=es6,./script.js,arg}</li>
 * <li>Required canonicalized arguments array: {@code --language=es6,./script.js,--,arg2}</li>
 * </ul>
 *
 * @param args the command line arguments as passed into Nashorn.
 * @return the passed and possibly canonicalized argument list
 */
private static String[] preprocessArgs(final String[] args) {
    if (args.length == 0) {
        return args;
    }

    final List<String> processedArgs = new ArrayList<>();
    processedArgs.addAll(Arrays.asList(args));

    // Nashorn supports passing multiple shebang arguments. On platforms that pass anything following the
    // shebang interpreter notice as one argument, the first element of the argument array needs to be special-cased
    // as it might actually contain several arguments. Mac OS X splits shebang arguments, other platforms don't.
    // This special handling is also only necessary if the first argument actually starts with an option.
    if (args[0].startsWith("-") && !System.getProperty("os.name", "generic").startsWith("Mac OS X")) {
        processedArgs.addAll(0, ScriptingFunctions.tokenizeString(processedArgs.remove(0)));
    }

    int shebangFilePos = -1; // -1 signifies "none found"
    // identify a shebang file and its position in the arguments array (if any)
    for (int i = 0; i < processedArgs.size(); ++i) {
        final String a = processedArgs.get(i);
        if (!a.startsWith("-")) {
            final Path p = Paths.get(a);
            String l = "";
            try (final BufferedReader r = Files.newBufferedReader(p)) {
                l = r.readLine();
            } catch (IOException ioe) {
                // ignore
            }
            if (l.startsWith("#!")) {
                shebangFilePos = i;
            }
            // We're only checking the first non-option argument. If it's not a shebang file, we're in normal
            // execution mode.
            break;
        }
    }
    if (shebangFilePos != -1) {
        // Insert the argument separator after the shebang script file.
        processedArgs.add(shebangFilePos + 1, "--");
    }
    return processedArgs.toArray(new String[0]);
}
 
Example 15
Source Project: jdk8u60   Source File: Global.java    License: GNU General Public License v2.0 4 votes vote down vote up
private void initScripting(final ScriptEnvironment scriptEnv) {
    Object value;
    value = ScriptFunctionImpl.makeFunction("readLine", ScriptingFunctions.READLINE);
    addOwnProperty("readLine", Attribute.NOT_ENUMERABLE, value);

    value = ScriptFunctionImpl.makeFunction("readFully", ScriptingFunctions.READFULLY);
    addOwnProperty("readFully", Attribute.NOT_ENUMERABLE, value);

    final String execName = ScriptingFunctions.EXEC_NAME;
    value = ScriptFunctionImpl.makeFunction(execName, ScriptingFunctions.EXEC);
    addOwnProperty(execName, Attribute.NOT_ENUMERABLE, value);

    // Nashorn extension: global.echo (scripting-mode-only)
    // alias for "print"
    value = get("print");
    addOwnProperty("echo", Attribute.NOT_ENUMERABLE, value);

    // Nashorn extension: global.$OPTIONS (scripting-mode-only)
    final ScriptObject options = newObject();
    copyOptions(options, scriptEnv);
    addOwnProperty("$OPTIONS", Attribute.NOT_ENUMERABLE, options);

    // Nashorn extension: global.$ENV (scripting-mode-only)
    if (System.getSecurityManager() == null) {
        // do not fill $ENV if we have a security manager around
        // Retrieve current state of ENV variables.
        final ScriptObject env = newObject();
        env.putAll(System.getenv(), scriptEnv._strict);

        // Some platforms, e.g., Windows, do not define the PWD environment
        // variable, so that the $ENV.PWD property needs to be explicitly
        // set.
        if (!env.containsKey(ScriptingFunctions.PWD_NAME)) {
            env.put(ScriptingFunctions.PWD_NAME, System.getProperty("user.dir"), scriptEnv._strict);
        }

        addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, env);
    } else {
        addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    }

    // add other special properties for exec support
    addOwnProperty(ScriptingFunctions.OUT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    addOwnProperty(ScriptingFunctions.ERR_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    addOwnProperty(ScriptingFunctions.EXIT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
}
 
Example 16
Source Project: openjdk-jdk8u   Source File: Shell.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Preprocess the command line arguments passed in by the shell. This method checks, for the first non-option
 * argument, whether the file denoted by it begins with a shebang line. If so, it is assumed that execution in
 * shebang mode is intended. The consequence of this is that the identified script file will be treated as the
 * <em>only</em> script file, and all subsequent arguments will be regarded as arguments to the script.
 * <p>
 * This method canonicalizes the command line arguments to the form {@code <options> <script> -- <arguments>} if a
 * shebang script is identified. On platforms that pass shebang arguments as single strings, the shebang arguments
 * will be broken down into single arguments; whitespace is used as separator.
 * <p>
 * Shebang mode is entered regardless of whether the script is actually run directly from the shell, or indirectly
 * via the {@code jjs} executable. It is the user's / script author's responsibility to ensure that the arguments
 * given on the shebang line do not lead to a malformed argument sequence. In particular, the shebang arguments
 * should not contain any whitespace for purposes other than separating arguments, as the different platforms deal
 * with whitespace in different and incompatible ways.
 * <p>
 * @implNote Example:<ul>
 * <li>Shebang line in {@code script.js}: {@code #!/path/to/jjs --language=es6}</li>
 * <li>Command line: {@code ./script.js arg2}</li>
 * <li>{@code args} array passed to Nashorn: {@code --language=es6,./script.js,arg}</li>
 * <li>Required canonicalized arguments array: {@code --language=es6,./script.js,--,arg2}</li>
 * </ul>
 *
 * @param args the command line arguments as passed into Nashorn.
 * @return the passed and possibly canonicalized argument list
 */
private static String[] preprocessArgs(final String[] args) {
    if (args.length == 0) {
        return args;
    }

    final List<String> processedArgs = new ArrayList<>();
    processedArgs.addAll(Arrays.asList(args));

    // Nashorn supports passing multiple shebang arguments. On platforms that pass anything following the
    // shebang interpreter notice as one argument, the first element of the argument array needs to be special-cased
    // as it might actually contain several arguments. Mac OS X splits shebang arguments, other platforms don't.
    // This special handling is also only necessary if the first argument actually starts with an option.
    if (args[0].startsWith("-") && !System.getProperty("os.name", "generic").startsWith("Mac OS X")) {
        processedArgs.addAll(0, ScriptingFunctions.tokenizeString(processedArgs.remove(0)));
    }

    int shebangFilePos = -1; // -1 signifies "none found"
    // identify a shebang file and its position in the arguments array (if any)
    for (int i = 0; i < processedArgs.size(); ++i) {
        final String a = processedArgs.get(i);
        if (!a.startsWith("-")) {
            final Path p = Paths.get(a);
            String l = "";
            try (final BufferedReader r = Files.newBufferedReader(p)) {
                l = r.readLine();
            } catch (IOException ioe) {
                // ignore
            }
            if (l.startsWith("#!")) {
                shebangFilePos = i;
            }
            // We're only checking the first non-option argument. If it's not a shebang file, we're in normal
            // execution mode.
            break;
        }
    }
    if (shebangFilePos != -1) {
        // Insert the argument separator after the shebang script file.
        processedArgs.add(shebangFilePos + 1, "--");
    }
    return processedArgs.toArray(new String[0]);
}
 
Example 17
Source Project: openjdk-jdk8u-backup   Source File: Shell.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Preprocess the command line arguments passed in by the shell. This method checks, for the first non-option
 * argument, whether the file denoted by it begins with a shebang line. If so, it is assumed that execution in
 * shebang mode is intended. The consequence of this is that the identified script file will be treated as the
 * <em>only</em> script file, and all subsequent arguments will be regarded as arguments to the script.
 * <p>
 * This method canonicalizes the command line arguments to the form {@code <options> <script> -- <arguments>} if a
 * shebang script is identified. On platforms that pass shebang arguments as single strings, the shebang arguments
 * will be broken down into single arguments; whitespace is used as separator.
 * <p>
 * Shebang mode is entered regardless of whether the script is actually run directly from the shell, or indirectly
 * via the {@code jjs} executable. It is the user's / script author's responsibility to ensure that the arguments
 * given on the shebang line do not lead to a malformed argument sequence. In particular, the shebang arguments
 * should not contain any whitespace for purposes other than separating arguments, as the different platforms deal
 * with whitespace in different and incompatible ways.
 * <p>
 * @implNote Example:<ul>
 * <li>Shebang line in {@code script.js}: {@code #!/path/to/jjs --language=es6}</li>
 * <li>Command line: {@code ./script.js arg2}</li>
 * <li>{@code args} array passed to Nashorn: {@code --language=es6,./script.js,arg}</li>
 * <li>Required canonicalized arguments array: {@code --language=es6,./script.js,--,arg2}</li>
 * </ul>
 *
 * @param args the command line arguments as passed into Nashorn.
 * @return the passed and possibly canonicalized argument list
 */
private static String[] preprocessArgs(final String[] args) {
    if (args.length == 0) {
        return args;
    }

    final List<String> processedArgs = new ArrayList<>();
    processedArgs.addAll(Arrays.asList(args));

    // Nashorn supports passing multiple shebang arguments. On platforms that pass anything following the
    // shebang interpreter notice as one argument, the first element of the argument array needs to be special-cased
    // as it might actually contain several arguments. Mac OS X splits shebang arguments, other platforms don't.
    // This special handling is also only necessary if the first argument actually starts with an option.
    if (args[0].startsWith("-") && !System.getProperty("os.name", "generic").startsWith("Mac OS X")) {
        processedArgs.addAll(0, ScriptingFunctions.tokenizeString(processedArgs.remove(0)));
    }

    int shebangFilePos = -1; // -1 signifies "none found"
    // identify a shebang file and its position in the arguments array (if any)
    for (int i = 0; i < processedArgs.size(); ++i) {
        final String a = processedArgs.get(i);
        if (!a.startsWith("-")) {
            final Path p = Paths.get(a);
            String l = "";
            try (final BufferedReader r = Files.newBufferedReader(p)) {
                l = r.readLine();
            } catch (IOException ioe) {
                // ignore
            }
            if (l.startsWith("#!")) {
                shebangFilePos = i;
            }
            // We're only checking the first non-option argument. If it's not a shebang file, we're in normal
            // execution mode.
            break;
        }
    }
    if (shebangFilePos != -1) {
        // Insert the argument separator after the shebang script file.
        processedArgs.add(shebangFilePos + 1, "--");
    }
    return processedArgs.toArray(new String[0]);
}
 
Example 18
Source Project: jdk8u_nashorn   Source File: Shell.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Preprocess the command line arguments passed in by the shell. This method checks, for the first non-option
 * argument, whether the file denoted by it begins with a shebang line. If so, it is assumed that execution in
 * shebang mode is intended. The consequence of this is that the identified script file will be treated as the
 * <em>only</em> script file, and all subsequent arguments will be regarded as arguments to the script.
 * <p>
 * This method canonicalizes the command line arguments to the form {@code <options> <script> -- <arguments>} if a
 * shebang script is identified. On platforms that pass shebang arguments as single strings, the shebang arguments
 * will be broken down into single arguments; whitespace is used as separator.
 * <p>
 * Shebang mode is entered regardless of whether the script is actually run directly from the shell, or indirectly
 * via the {@code jjs} executable. It is the user's / script author's responsibility to ensure that the arguments
 * given on the shebang line do not lead to a malformed argument sequence. In particular, the shebang arguments
 * should not contain any whitespace for purposes other than separating arguments, as the different platforms deal
 * with whitespace in different and incompatible ways.
 * <p>
 * @implNote Example:<ul>
 * <li>Shebang line in {@code script.js}: {@code #!/path/to/jjs --language=es6}</li>
 * <li>Command line: {@code ./script.js arg2}</li>
 * <li>{@code args} array passed to Nashorn: {@code --language=es6,./script.js,arg}</li>
 * <li>Required canonicalized arguments array: {@code --language=es6,./script.js,--,arg2}</li>
 * </ul>
 *
 * @param args the command line arguments as passed into Nashorn.
 * @return the passed and possibly canonicalized argument list
 */
private static String[] preprocessArgs(final String[] args) {
    if (args.length == 0) {
        return args;
    }

    final List<String> processedArgs = new ArrayList<>();
    processedArgs.addAll(Arrays.asList(args));

    // Nashorn supports passing multiple shebang arguments. On platforms that pass anything following the
    // shebang interpreter notice as one argument, the first element of the argument array needs to be special-cased
    // as it might actually contain several arguments. Mac OS X splits shebang arguments, other platforms don't.
    // This special handling is also only necessary if the first argument actually starts with an option.
    if (args[0].startsWith("-") && !System.getProperty("os.name", "generic").startsWith("Mac OS X")) {
        processedArgs.addAll(0, ScriptingFunctions.tokenizeString(processedArgs.remove(0)));
    }

    int shebangFilePos = -1; // -1 signifies "none found"
    // identify a shebang file and its position in the arguments array (if any)
    for (int i = 0; i < processedArgs.size(); ++i) {
        final String a = processedArgs.get(i);
        if (!a.startsWith("-")) {
            final Path p = Paths.get(a);
            String l = "";
            try (final BufferedReader r = Files.newBufferedReader(p)) {
                l = r.readLine();
            } catch (IOException ioe) {
                // ignore
            }
            if (l.startsWith("#!")) {
                shebangFilePos = i;
            }
            // We're only checking the first non-option argument. If it's not a shebang file, we're in normal
            // execution mode.
            break;
        }
    }
    if (shebangFilePos != -1) {
        // Insert the argument separator after the shebang script file.
        processedArgs.add(shebangFilePos + 1, "--");
    }
    return processedArgs.toArray(new String[0]);
}