Skip to content

Commit

Permalink
Implemented named pipe operator '|* <name>'
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Feb 9, 2020
1 parent 8c03cdf commit 95ff132
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,9 @@ private Object pipe(Builtins.CommandInput input) {
out = pipes;
} else if (opt.args().size() != 3) {
exception = new IllegalArgumentException("Bad number of arguments!");
} else if (opt.args().get(0).equals(SystemRegistryImpl.PIPE_FLIP)
|| opt.args().get(0).equals(SystemRegistryImpl.PIPE_NAMED)) {
exception = new IllegalArgumentException("Reserved pipe operator");
} else {
List<String> fixes = new ArrayList<>();
fixes.add(opt.args().get(1));
Expand Down
55 changes: 41 additions & 14 deletions builtins/src/main/java/org/jline/builtins/SystemRegistryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class SystemRegistryImpl implements SystemRegistry {
public enum Command {
EXIT, HELP
};
protected final static String PIPE_FLIP = "|;";
protected final static String PIPE_NAMED = "|*";

private static final Class<?>[] BUILTIN_REGISTERIES = { Builtins.class, ConsoleEngineImpl.class };
private CommandRegistry[] commandRegistries;
Expand Down Expand Up @@ -488,7 +490,7 @@ private List<CommandData> compileCommandLine(String commandLine) {
last = i + 1;
lastarg = i;
break;
} else if (words.get(i).equals("|;")) {
} else if (words.get(i).equals(PIPE_FLIP)) {
if (variable != null || file != null || pipeResult != null || consoleId == null) {
throw new IllegalArgumentException();
}
Expand All @@ -497,8 +499,19 @@ private List<CommandData> compileCommandLine(String commandLine) {
lastarg = i;
variable = "_pipe" + (pipes.size() - 1);
break;
} else if (customPipes.containsKey(words.get(i))) {
pipes.add(words.get(i));
} else if ( words.get(i).equals(PIPE_NAMED)
|| (words.get(i).matches("^.*[^a-zA-Z0-9 ].*$") && customPipes.containsKey(words.get(i)))) {
String pipe = words.get(i);
if (pipe.equals(PIPE_NAMED)) {
if (i + 1 >= last) {
throw new IllegalArgumentException("Pipe is NULL!");
}
pipe = words.get(i + 1);
if (!pipe.matches("\\w+") || !customPipes.containsKey(pipe)) {
throw new IllegalArgumentException("Unknown or illegal pipe name: " + pipe);
}
}
pipes.add(pipe);
last = i;
lastarg = i;
if (pipeSource == null) {
Expand All @@ -510,7 +523,7 @@ private List<CommandData> compileCommandLine(String commandLine) {
}
}
if (last == words.size()) {
pipes.add("END");
pipes.add("END_PIPE");
}
String subLine = last < words.size() || first > 0
? words.subList(first, lastarg).stream().collect(Collectors.joining(" "))
Expand All @@ -529,31 +542,33 @@ private List<CommandData> compileCommandLine(String commandLine) {
}
if (customPipes.containsKey(pipes.get(pipes.size() - 2))) {
List<String> fixes = customPipes.get(pipes.get(pipes.size() - 2));
if (pipes.get(pipes.size() - 2).matches("\\w+")) {
int idx = subLine.indexOf(" ");
subLine = idx > 0 ? subLine.substring(idx + 1) : "";
}
rawLine += fixes.get(0) + subLine + fixes.get(1);
statement = true;
}
if (pipes.get(pipes.size() - 1).equals("|;")) {
if (pipes.get(pipes.size() - 1).equals(PIPE_FLIP)) {
done = true;
pipeSource = null;
rawLine = variable + " = " + rawLine;
}
if (last + 1 >= words.size() || file != null) {
done = true;
pipeSource = null;
if (pipeResult != null) {
rawLine = pipeResult + " = " + rawLine;
}
}

} else if (pipes.get(pipes.size() - 1).equals("|;") || pipeStart) {
}
} else if (pipes.get(pipes.size() - 1).equals(PIPE_FLIP) || pipeStart) {
if (pipeStart && pipeResult != null) {
subLine = subLine.substring(subLine.indexOf("=") + 1);
}
rawLine = variable + "=" + subLine;
} else if (pipes.size() > 1 && pipes.get(pipes.size() - 2).equals("|;")) {
String s = isCommandOrScript(command) ? "$" : "";
rawLine = subLine + " " + s + "_pipe" + (pipes.size() - 2);
arglist.add(s + "_pipe" + (pipes.size() - 2));
rawLine = flipArgument(command, subLine, pipes, arglist);
rawLine = variable + "=" + rawLine;
} else {
rawLine = subLine;
rawLine = flipArgument(command, subLine, pipes, arglist);
}
if (done) {
String[] args = statement ? new String[] {} : arglist.toArray(new String[0]);
Expand All @@ -565,6 +580,18 @@ private List<CommandData> compileCommandLine(String commandLine) {
return out;
}

private String flipArgument(final String command, final String subLine, final List<String> pipes, List<String> arglist) {
String out = null;
if (pipes.size() > 1 && pipes.get(pipes.size() - 2).equals(PIPE_FLIP)) {
String s = isCommandOrScript(command) ? "$" : "";
out = subLine + " " + s + "_pipe" + (pipes.size() - 2);
arglist.add(s + "_pipe" + (pipes.size() - 2));
} else {
out = subLine;
}
return out;
}

protected static class CommandData {
private String rawLine;
private String command;
Expand Down
1 change: 1 addition & 0 deletions demo/src/main/scripts/init.jline
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ pipe |?1 '.find{' '}'
pipe |! '.each{' '}'
pipe |# '.take(' ')'
pipe |& '.' ' '
pipe grep '.collect{it.toString()}.findAll{it=~/' '/}'
4 changes: 2 additions & 2 deletions reader/src/main/java/org/jline/reader/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public static boolean validCommandName(String name) {

static String getCommand(final String line) {
String out = null;
Pattern patternCommand = Pattern.compile("^\\s*" + REGEX_VARIABLE + "=(" + REGEX_COMMAND + ")(\\s+|$)");
Pattern patternCommand = Pattern.compile("^\\s*" + REGEX_VARIABLE + "=(" + REGEX_COMMAND + ")(\\s+.*|$)");
Matcher matcher = patternCommand.matcher(line);
if (matcher.find()) {
out = matcher.group(1);
out = matcher.group(4);
} else {
out = line.trim().split("\\s+")[0];
int idx = out.indexOf("=");
Expand Down

0 comments on commit 95ff132

Please sign in to comment.