Search in sources :

Example 1 with CommandParser

use of com.ibm.j9ddr.command.CommandParser in project openj9 by eclipse.

the class ForeachCommand method run.

public void run(String command, final String[] args, final Context context, final PrintStream out) throws DDRInteractiveCommandException {
    if (command.equals("!do")) {
        boolean echo = false;
        boolean quiet = false;
        String token = null;
        int commandIndex = 0;
        while (commandIndex < args.length) {
            if (args[commandIndex].equals("help")) {
                out.println("The !do command is used to repeat a command on the elements gathered by the !foreach command.");
                out.println("Syntax:	!do [echo] [quiet] [token=<token>] <cmd> [args]");
                out.println();
                out.println("\techo\t\tOutput each command before it is run.");
                out.println("\tquiet\t\tSuppress delimiters between command outputs.");
                out.println("\ttoken=<chars>\tSpecify a substitution token. Occurrencs of this string in the args will be replaced by the current element.");
                return;
            } else if (args[commandIndex].equals("echo")) {
                echo = true;
                commandIndex += 1;
            } else if (args[commandIndex].equals("quiet")) {
                quiet = true;
                commandIndex += 1;
            } else if (args[commandIndex].startsWith("token=")) {
                token = args[commandIndex].substring("token=".length());
                commandIndex += 1;
            } else {
                break;
            }
        }
        if (commandIndex >= args.length) {
            out.println("The do command requires another command to repeat.");
            return;
        }
        if ((null == foreachArgs) || (0 == foreachArgs.length)) {
            out.println("Element list is empty. Use the foreach command to populate it.");
            return;
        }
        for (int i = 0; i < foreachArgs.length; i++) {
            if (!quiet && (i > 0)) {
                out.println("========================================");
            }
            try {
                String[] newArgs;
                if (null == token) {
                    newArgs = substituteArgs(args, commandIndex, i);
                } else {
                    newArgs = substituteArgs(args, commandIndex, i, token);
                }
                if (echo) {
                    System.out.println("> " + args[commandIndex] + " " + Arrays.toString(newArgs));
                }
                CommandParser commandParser = new CommandParser(args[commandIndex], newArgs);
                context.execute(commandParser, out);
            } catch (ParseException e) {
                e.printStackTrace(out);
            } catch (Throwable th) {
                out.println("Exception while executing " + args[commandIndex] + " " + foreachArgs[i]);
                th.printStackTrace(out);
            }
        }
    } else if (command.equals("!foreach")) {
        if (args.length > 0) {
            out.println("The !foreach command takes no arguments, but will read lines from the console until it encounters a blank line.");
            out.println("These lines can then be used as arguments to commands specified using !do.");
            return;
        }
        ArrayList<String> lines = new ArrayList<String>();
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            String line;
            try {
                line = reader.readLine();
            } catch (IOException e) {
                break;
            }
            if (0 == line.length()) {
                break;
            } else {
                lines.add(line);
            }
        }
        // split by ,
        String[] newArgs = new String[lines.size()];
        lines.toArray(newArgs);
        foreachArgs = newArgs;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) ParseException(java.text.ParseException) IOException(java.io.IOException) CommandParser(com.ibm.j9ddr.command.CommandParser)

Example 2 with CommandParser

use of com.ibm.j9ddr.command.CommandParser in project openj9 by eclipse.

the class TimeCommand method run.

public void run(String command, String[] args, Context context, PrintStream out) throws DDRInteractiveCommandException {
    String[] newArgs = null;
    if (args.length < 1) {
        out.println("The time command requires another command to run as an argument.");
        return;
    } else if (args.length > 1) {
        newArgs = new String[args.length - 1];
        System.arraycopy(args, 1, newArgs, 0, newArgs.length);
    } else {
        newArgs = new String[0];
    }
    CommandParser commandParser;
    try {
        commandParser = new CommandParser(args[0], newArgs);
        long startTime = System.currentTimeMillis();
        context.execute(commandParser, out);
        long endTime = System.currentTimeMillis();
        double secs = ((double) endTime - (double) startTime) / 1000.0;
        out.printf("\n---\nCommand '%s' took %f seconds\n", commandParser.toString(), secs);
    } catch (ParseException e) {
        e.printStackTrace(out);
    }
}
Also used : ParseException(java.text.ParseException) CommandParser(com.ibm.j9ddr.command.CommandParser)

Example 3 with CommandParser

use of com.ibm.j9ddr.command.CommandParser in project openj9 by eclipse.

the class LimitCommand method run.

@SuppressWarnings("deprecation")
public void run(String command, final String[] args, final Context context, final PrintStream out) throws DDRInteractiveCommandException {
    final String[] newArgs;
    if (args.length < 2) {
        out.println("The limit command requires a timeout and another command to run as an argument.");
        return;
    }
    long timeout = Integer.parseInt(args[0]) * 1000;
    if (args.length > 2) {
        newArgs = new String[args.length - 2];
        System.arraycopy(args, 2, newArgs, 0, newArgs.length);
    } else {
        newArgs = new String[0];
    }
    Thread runner = new Thread("LimitCommand: " + args[1]) {

        public void run() {
            try {
                CommandParser commandParser = new CommandParser(args[1], newArgs);
                context.execute(commandParser, out);
            } catch (ParseException e) {
                e.printStackTrace(out);
            }
        }
    };
    runner.start();
    try {
        runner.join(timeout);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    if (runner.isAlive()) {
        runner.stop(new DDRInteractiveCommandException("Timeout exceeded!"));
    }
}
Also used : DDRInteractiveCommandException(com.ibm.j9ddr.tools.ddrinteractive.DDRInteractiveCommandException) ParseException(java.text.ParseException) CommandParser(com.ibm.j9ddr.command.CommandParser)

Aggregations

CommandParser (com.ibm.j9ddr.command.CommandParser)3 ParseException (java.text.ParseException)3 DDRInteractiveCommandException (com.ibm.j9ddr.tools.ddrinteractive.DDRInteractiveCommandException)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1