Skip to content

Commit

Permalink
REPL console: improved command exit status evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Feb 15, 2020
1 parent 811c2f0 commit 8b28847
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 41 deletions.
20 changes: 19 additions & 1 deletion builtins/src/main/java/org/jline/builtins/ConsoleEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ default Object execute(File script) throws Exception {
* @param output command redirected output
* @return processed result
*/
Object postProcess(String line, Object result, String output);
ExecutionResult postProcess(String line, Object result, String output);

/**
* @param object object to print
Expand Down Expand Up @@ -182,6 +182,24 @@ default Object execute(File script) throws Exception {
* @return true if consoleEngine is executing script
*/
boolean isExecuting();

static class ExecutionResult {
final int status;
final Object result;

public ExecutionResult(int status, Object result) {
this.status = status;
this.result = result;
}

public int status() {
return status;
}

public Object result() {
return result;
}
}

static class WidgetCreator implements Widget {
private ConsoleEngine consoleEngine;
Expand Down
43 changes: 24 additions & 19 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,8 @@ public boolean executeWidget(Object function) {
} catch (Exception e) {
trace(e);
return false;
} finally {
purge();
}
return true;
}
Expand All @@ -651,47 +653,50 @@ private boolean splitCommandOutput() {


@Override
public Object postProcess(String line, Object result, String output) {
Object out = result;
Object _output = output != null && splitCommandOutput() ? output.split("\\r?\\n") : output;
public ExecutionResult postProcess(String line, Object result, String output) {
ExecutionResult out = new ExecutionResult(1, null);
Object _output = output != null && !output.trim().isEmpty() && splitCommandOutput() ? output.split("\\r?\\n") : output;
String consoleVar = parser().getVariable(line);
if (consoleVar != null && result != null) {
engine.put("output", _output);
}
if (systemRegistry.hasCommand(parser().getCommand(line))) {
out = postProcess(line, consoleVar != null && result == null ? _output : result);
} else if (consoleVar != null) {
if (result == null) {
saveResult(consoleVar, _output);
}
out = null;
int status = saveResult(consoleVar, result == null ? _output : result);
out = new ExecutionResult(status, null);
}
return out;
}

private Object postProcess(String line, Object result) {
Object out = result instanceof String && ((String)result).trim().length() == 0 ? null : result;
private ExecutionResult postProcess(String line, Object result) {
int status = 0;
Object out = result != null && result instanceof String && ((String)result).trim().isEmpty() ? null : result;
String consoleVar = parser().getVariable(line);
if (consoleVar != null) {
saveResult(consoleVar, result);
status = saveResult(consoleVar, result);
out = null;
} else if (!parser().getCommand(line).equals("show") && result != null) {
engine.put("_", result);
status = saveResult("_", result);
}
return out;
return new ExecutionResult(status, out);
}

private void saveResult(String var, Object result) {
if (var.contains(".") || var.contains("[")) {
private int saveResult(String var, Object result) {
int out = 0;
try {
engine.put("_executionResult", result);
try {
if (var.contains(".") || var.contains("[")) {
engine.execute(var + " = _executionResult");
} catch (Exception e) {
trace(e);
} else {
engine.put(var, result);
}
} else {
engine.put(var, result);
out = (int) engine.execute("_executionResult ? 0 : 1");
} catch (Exception e) {
trace(e);
out = 1;
}
return out;
}

@Override
Expand Down
58 changes: 37 additions & 21 deletions builtins/src/main/java/org/jline/builtins/SystemRegistryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.jline.builtins.Completers.OptDesc;
import org.jline.builtins.Completers.OptionCompleter;
import org.jline.builtins.CommandRegistry;
import org.jline.builtins.ConsoleEngine.ExecutionResult;
import org.jline.builtins.Widgets;
import org.jline.builtins.Builtins.CommandMethods;
import org.jline.builtins.Options.HelpException;
Expand Down Expand Up @@ -559,32 +560,30 @@ private List<CommandData> compileCommandLine(String commandLine) {
}
}
last = words.size();
int lastarg = last;
File file = null;
boolean append = false;
boolean pipeStart = false;
argsParser = new ArgsParser();
List<String> _words = new ArrayList<>();
for (int i = first; i < last; i++) {
argsParser.next(words.get(i));
if (argsParser.isEnclosed()) {
// do nothing
_words.add(words.get(i));
} else if (words.get(i).equals(">") || words.get(i).equals(">>")) {
pipes.add(words.get(i));
append = words.get(i).equals(">>");
if (i + 2 != last) {
if (i + 1 >= last) {
throw new IllegalArgumentException();
}
file = new File(words.get(i + 1));
last = i + 1;
lastarg = i;
break;
} else if (words.get(i).equals(pipeName.get(Pipe.FLIP))) {
if (variable != null || file != null || pipeResult != null || consoleId == null) {
throw new IllegalArgumentException();
}
pipes.add(words.get(i));
last = i;
lastarg = i;
variable = "_pipe" + (pipes.size() - 1);
break;
} else if ( words.get(i).equals(pipeName.get(Pipe.NAMED))
Expand All @@ -601,37 +600,53 @@ private List<CommandData> compileCommandLine(String commandLine) {
}
pipes.add(pipe);
last = i;
lastarg = i;
if (pipeSource == null) {
pipeSource = "_pipe" + (pipes.size() - 1);
pipeResult = variable;
variable = pipeSource;
pipeStart = true;
}
break;
} else if (words.get(i).equals(pipeName.get(Pipe.OR)) || words.get(i).equals(pipeName.get(Pipe.AND))) {
pipes.add(words.get(i));
last = i;
lastarg = i;
if (pipeResult != null) {
pipes.add(words.get(i));
variable = pipeResult;
pipeResult = null;
} else if (pipeSource != null
|| variable != null
|| pipes.size() > 0 && (
pipes.get(pipes.size() - 1).equals(">")
|| pipes.get(pipes.size() - 1).equals(">>"))) {
pipes.add(words.get(i));
} else {
pipes.add(pipeName.get(Pipe.FLIP));
pipeSource = "_pipe" + (pipes.size() - 1);
variable = pipeSource;
pipeStart = true;
i = i - 1;
}
last = i;
break;
} else {
_words.add(words.get(i));
}
}
if (last == words.size()) {
pipes.add("END_PIPE");
}
argsParser = new ArgsParser();
String subLine = last < words.size() || first > 0
? argsParser.join(words.subList(first, lastarg))
? argsParser.join(_words)
: pl.line();
if (last + 1 < words.size()) {
nextRawLine = argsParser.join(words.subList(last + 1, words.size()));
}
boolean done = true;
boolean statement = false;
List<String> arglist = new ArrayList<>();
arglist.addAll(words.subList(first + 1, lastarg));
if (_words.size() > 0) {
arglist.addAll(_words.subList(1, _words.size()));
}
if (rawLine != null || (pipes.size() > 1 && customPipes.containsKey(pipes.get(pipes.size() - 2)))) {
done = false;
if (rawLine == null) {
Expand Down Expand Up @@ -762,7 +777,9 @@ private String flipArgument(final String command, final String subLine, final Li
if (pipes.size() > 1 && pipes.get(pipes.size() - 2).equals(pipeName.get(Pipe.FLIP))) {
String s = isCommandOrScript(command) ? "$" : "";
out = subLine + " " + s + "_pipe" + (pipes.size() - 2);
arglist.add(s + "_pipe" + (pipes.size() - 2));
if (!command.isEmpty()) {
arglist.add(s + "_pipe" + (pipes.size() - 2));
}
} else {
out = subLine;
}
Expand Down Expand Up @@ -895,29 +912,28 @@ public Object execute(String line) throws Exception {
out = consoleEngine().execute(cmd.command(), cmd.rawLine(), cmd.args());
}
if (consoleId != null && cmd.pipe().equals(pipeName.get(Pipe.OR)) || cmd.pipe().equals(pipeName.get(Pipe.AND))) {
out = postProcess(cmd, statement, out);
consoleEngine().println(out);
consoleEngine().putVariable("_executionResult", out);
ExecutionResult er = postProcess(cmd, statement, out);
consoleEngine().println(er.result());
out = null;
boolean status = (boolean)consoleEngine().execute("", "_executionResult ? true : false", new String[] {}); // Groovy syntax!!!!
if ( (cmd.pipe().equals(pipeName.get(Pipe.OR)) && status)
|| (cmd.pipe().equals(pipeName.get(Pipe.AND)) && !status)) {
boolean success = er.status() == 0 ? true : false;
if ( (cmd.pipe().equals(pipeName.get(Pipe.OR)) && success)
|| (cmd.pipe().equals(pipeName.get(Pipe.AND)) && !success)) {
break;
}
}
} catch (HelpException e) {
trace(e);
} finally {
if (consoleId != null && !consoleEngine().isExecuting()) {
out = postProcess(cmd, statement, out);
out = postProcess(cmd, statement, out).result();
}
}
}
return out;
}

private Object postProcess(CommandData cmd, boolean statement, Object result) {
Object out = result;
private ExecutionResult postProcess(CommandData cmd, boolean statement, Object result) {
ExecutionResult out = new ExecutionResult(result != null ? 0 : 1, result);
if (!statement) {
outputStream.flush();
out = consoleEngine().postProcess(cmd.rawLine(), result, outputStream.getOutput());
Expand Down

0 comments on commit 8b28847

Please sign in to comment.