Skip to content

Commit

Permalink
REPL console: redirect output to null device (command > null)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Mar 6, 2020
1 parent e839b9c commit 089f989
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions builtins/src/main/java/org/jline/builtins/SystemRegistryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -509,14 +509,14 @@ private boolean isCommandAlias(String command) {
return false;
}
String value = consoleEngine().getAlias(command).split("\\s+")[0];
return !isPipe(value);
return !isPipe(value);
}

private String replaceCommandAlias(String variable, String command, String rawLine) {
return variable == null ? rawLine.replaceFirst(command + "(\\b|$)", consoleEngine().getAlias(command))
: rawLine.replaceFirst("=" + command + "(\\b|$)", "=" + consoleEngine().getAlias(command));
}

private List<CommandData> compileCommandLine(String commandLine) {
List<CommandData> out = new ArrayList<>();
ArgsParser ap = new ArgsParser();
Expand Down Expand Up @@ -608,7 +608,7 @@ private List<CommandData> compileCommandLine(String commandLine) {
if (i + 1 >= last) {
throw new IllegalArgumentException();
}
file = new File(words.get(i + 1));
file = redirectFile(words.get(i + 1));
last = i + 1;
break;
} else if (words.get(i).equals(pipeName.get(Pipe.FLIP))) {
Expand Down Expand Up @@ -739,6 +739,16 @@ private List<CommandData> compileCommandLine(String commandLine) {
return out;
}

private File redirectFile(String name) {
File out = null;
if (name.equals("null")) {
out = OSUtils.IS_WINDOWS ? new File("NUL") : new File("/dev/null");
} else {
out = new File(name);
}
return out;
}

private static class ArgsParser {
private int round = 0;
private int curly = 0;
Expand All @@ -751,7 +761,7 @@ private static class ArgsParser {
private List<String> args;

public ArgsParser() {}

private void reset() {
round = 0;
curly = 0;
Expand Down Expand Up @@ -796,7 +806,7 @@ private void next(String arg) {
private boolean isEnclosed() {
return round == 0 && curly == 0 && square == 0 && !quoted && !doubleQuoted;
}

private void enclosedArgs(List<String> words) {
args = new ArrayList<>();
reset();
Expand All @@ -818,7 +828,7 @@ private void enclosedArgs(List<String> words) {
}
}
if (!first) {
args.add(sb.toString());
args.add(sb.toString());
}
}

Expand All @@ -830,7 +840,7 @@ public void parse(Parser parser, String line) {
if (!parser.validCommandName(command)) {
this.command = "";
}
this.variable = parser.getVariable(args.get(0));
this.variable = parser.getVariable(args.get(0));
}

public String line() {
Expand Down Expand Up @@ -874,7 +884,7 @@ protected static class CommandData {
private String variable;
private String pipe;

public CommandData (Parser parser, boolean statement, String rawLine, String variable, File file, boolean append, String pipe) {
public CommandData (Parser parser, boolean statement, String rawLine, String variable, File file, boolean append, String pipe) {
this.rawLine = rawLine;
this.variable = variable;
this.file = file;
Expand Down

0 comments on commit 089f989

Please sign in to comment.