Skip to content

Commit

Permalink
REPL console: command exit status evaluation when redirecting to file
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Feb 16, 2020
1 parent 9b0c987 commit bd23b4f
Showing 1 changed file with 43 additions and 14 deletions.
57 changes: 43 additions & 14 deletions builtins/src/main/java/org/jline/builtins/SystemRegistryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/
package org.jline.builtins;

import static org.jline.keymap.KeyMap.ctrl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -391,7 +393,8 @@ public void open() throws IOException {
out = new PrintStream(outputStream);
System.setOut(out);
System.setErr(out);
in = new ByteArrayInputStream( "".getBytes() );
String input = ctrl('X') + "q";
in = new ByteArrayInputStream( input.getBytes() );
Attributes attrs = new Attributes();
if (OSUtils.IS_WINDOWS) {
attrs.setInputFlag(InputFlag.IGNCR, true);
Expand Down Expand Up @@ -466,12 +469,18 @@ public void reset() {
output = null;
System.setOut(origOut);
System.setErr(origErr);
terminal = null;
terminal = origTerminal;
PrintStream ps = new PrintStream(terminal.output());
this.commandSession = new CommandRegistry.CommandSession(terminal, terminal.input(), ps, ps);
redirecting = false;
}
}

public void closeAndReset() {
close();
reset();
}
}

private boolean hasPipes(Collection<String> args) {
Expand Down Expand Up @@ -562,6 +571,7 @@ private List<CommandData> compileCommandLine(String commandLine) {
File file = null;
boolean append = false;
boolean pipeStart = false;
boolean skipPipe = false;
ArgsParser argsParser = new ArgsParser();
List<String> _words = new ArrayList<>();
for (int i = first; i < last; i++) {
Expand Down Expand Up @@ -613,10 +623,14 @@ private List<CommandData> compileCommandLine(String commandLine) {
pipeResult = null;
} else if (pipeSource != null
|| variable != null
|| pipes.size() > 0 && (
) {
pipes.add(words.get(i));
} else if (pipes.size() > 0 && (
pipes.get(pipes.size() - 1).equals(">")
|| pipes.get(pipes.size() - 1).equals(">>"))) {
pipes.add(words.get(i));
pipes.remove(pipes.size() - 1);
out.get(out.size() - 1).setPipe(words.get(i));
skipPipe = true;
} else {
pipes.add(pipeName.get(Pipe.FLIP));
pipeSource = "_pipe" + (pipes.size() - 1);
Expand All @@ -632,6 +646,9 @@ private List<CommandData> compileCommandLine(String commandLine) {
}
if (last == words.size()) {
pipes.add("END_PIPE");
} else if (skipPipe) {
first = last + 1;
continue;
}
String subLine = last < words.size() || first > 0
? _words.stream().collect(Collectors.joining(" "))
Expand Down Expand Up @@ -781,6 +798,10 @@ public CommandData(String rawLine, String command, String[] args, String variabl
this.pipe = pipe;
}

public void setPipe(String pipe) {
this.pipe = pipe;
}

public File file() {
return file;
}
Expand Down Expand Up @@ -839,15 +860,16 @@ public Object execute(String line) throws Exception {
}
Object out = null;
boolean statement = false;
boolean postProcessed = false;
for (CommandData cmd : compileCommandLine(line)) {
try {
outputStream.close();
outputStream.reset();
outputStream.closeAndReset();
if (!consoleEngine().isExecuting()) {
trace(cmd);
}
exception = null;
statement = false;
postProcessed = false;
if (cmd.variable() != null || cmd.file() != null) {
if (cmd.file() != null) {
outputStream.redirect(cmd.file(), cmd.append());
Expand Down Expand Up @@ -882,14 +904,16 @@ public Object execute(String line) throws Exception {
statement = true;
}
if (statement && outputStream.isByteStream()) {
outputStream.close();
outputStream.reset();
outputStream.closeAndReset();
}
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))) {
ExecutionResult er = postProcess(cmd, statement, out);
consoleEngine().println(er.result());
postProcessed = true;
if (!consoleEngine().isExecuting()) {
consoleEngine().println(er.result());
}
out = null;
boolean success = er.status() == 0 ? true : false;
if ( (cmd.pipe().equals(pipeName.get(Pipe.OR)) && success)
Expand All @@ -900,7 +924,7 @@ public Object execute(String line) throws Exception {
} catch (HelpException e) {
trace(e);
} finally {
if (consoleId != null && !consoleEngine().isExecuting()) {
if (!postProcessed && consoleId != null && !consoleEngine().isExecuting()) {
out = postProcess(cmd, statement, out).result();
}
}
Expand All @@ -910,7 +934,14 @@ public Object execute(String line) throws Exception {

private ExecutionResult postProcess(CommandData cmd, boolean statement, Object result) {
ExecutionResult out = new ExecutionResult(result != null ? 0 : 1, result);
if (!statement) {
if (cmd.file() != null) {
int status = 1;
if (cmd.file().exists()) {
long delta = new Date().getTime() - cmd.file().lastModified();
status = delta < 100 ? 0 : 1;
}
out = new ExecutionResult(status, null);
} else if (!statement) {
outputStream.flush();
out = consoleEngine().postProcess(cmd.rawLine(), result, outputStream.getOutput());
}
Expand All @@ -919,8 +950,7 @@ private ExecutionResult postProcess(CommandData cmd, boolean statement, Object r

public void cleanUp() {
if (outputStream.isRedirecting()) {
outputStream.close();
outputStream.reset();
outputStream.closeAndReset();
}
if (consoleId != null) {
consoleEngine().purge();
Expand All @@ -939,8 +969,7 @@ private void trace(CommandData commandData) {
@Override
public void trace(Exception exception) {
if (outputStream.isRedirecting()) {
outputStream.close();
outputStream.reset();
outputStream.closeAndReset();
}
if (consoleId != null) {
consoleEngine().putVariable("exception", exception);
Expand Down

0 comments on commit bd23b4f

Please sign in to comment.