From: <cr...@us...> - 2009-01-31 12:47:01
|
Revision: 4934 http://jnode.svn.sourceforge.net/jnode/?rev=4934&view=rev Author: crawley Date: 2009-01-31 12:46:57 +0000 (Sat, 31 Jan 2009) Log Message: ----------- Bug fix: backtick output capture was not working. Modified Paths: -------------- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java trunk/shell/src/shell/org/jnode/shell/bjorne/ListCommandNode.java trunk/shell/src/shell/org/jnode/shell/bjorne/SimpleCommandNode.java Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java 2009-01-31 12:45:49 UTC (rev 4933) +++ trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java 2009-01-31 12:46:57 UTC (rev 4934) @@ -30,13 +30,13 @@ import static org.jnode.shell.bjorne.BjorneInterpreter.REDIR_LESSAND; import static org.jnode.shell.bjorne.BjorneInterpreter.REDIR_LESSGREAT; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; +import java.io.StringWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -419,15 +419,14 @@ } } - private String runBacktickCommand(String commandLine) throws ShellException { - ByteArrayOutputStream capture = new ByteArrayOutputStream(); + private StringBuffer runBacktickCommand(String commandLine) throws ShellException { + StringWriter capture = new StringWriter(); interpreter.interpret(interpreter.getShell(), commandLine, capture, false); - String output = capture.toString(); - // Trim trailing newlines - int i; - for (i = output.length(); i > 0 && output.charAt(i - 1) == '\n'; i--) { /**/ + StringBuffer output = capture.getBuffer(); + while (output.length() > 0 && output.charAt(output.length() - 1) == '\n') { + output.setLength(output.length() - 1); } - return output.substring(0, i); + return output; } private StringBuffer accumulate(StringBuffer sb, char ch) { @@ -466,8 +465,8 @@ if (backtickStart == -1) { backtickStart = sb.length(); } else { - String tmp = runBacktickCommand(sb.substring(backtickStart)); - sb.replace(backtickStart, sb.length(), tmp); + StringBuffer tmp = runBacktickCommand(sb.substring(backtickStart)); + sb.replace(backtickStart, sb.length(), tmp.toString()); backtickStart = -1; } break; @@ -788,6 +787,14 @@ return null; } } + + void setStream(int index, CommandIO stream, boolean mine) { + if (index < 0 || index >= holders.length) { + throw new ShellFailureException("negative stream index"); + } else { + holders[index].setStream(stream, mine); + } + } public boolean isNoClobber() { return isVariableSet("NOCLOBBER"); @@ -989,8 +996,7 @@ } public static class StreamHolder { - public final CommandIO stream; - + private CommandIO stream; private boolean isMine; public StreamHolder(CommandIO stream, boolean isMine) { @@ -1002,7 +1008,17 @@ this.stream = other.stream; this.isMine = false; } + + public CommandIO getStream() { + return stream; + } + public void setStream(CommandIO stream, boolean isMine) { + close(); + this.stream = stream; + this.isMine = isMine; + } + public void close() { if (isMine) { try { Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java 2009-01-31 12:45:49 UTC (rev 4933) +++ trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java 2009-01-31 12:46:57 UTC (rev 4934) @@ -36,10 +36,10 @@ import java.io.FileReader; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.io.PrintStream; import java.io.PrintWriter; import java.io.Reader; +import java.io.StringWriter; import java.util.HashMap; import org.jnode.driver.console.CompletionInfo; @@ -54,6 +54,7 @@ import org.jnode.shell.ShellSyntaxException; import org.jnode.shell.help.CompletionException; import org.jnode.shell.io.CommandIO; +import org.jnode.shell.io.CommandOutput; import org.jnode.shell.syntax.CommandSyntaxException; /** @@ -191,7 +192,7 @@ return word; } - int interpret(CommandShell shell, String command, OutputStream capture, boolean source) + int interpret(CommandShell shell, String command, StringWriter capture, boolean source) throws ShellException { BjorneContext myContext; // FIXME ... I think there is something wrong / incomplete with the way I'm handling @@ -201,6 +202,7 @@ myContext = this.context; } else { myContext = new BjorneContext(this); + myContext.setStream(1, new CommandOutput(capture), true); } BjorneTokenizer tokens = new BjorneTokenizer(command); CommandNode tree = new BjorneParser(tokens).parse(); Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/ListCommandNode.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/bjorne/ListCommandNode.java 2009-01-31 12:45:49 UTC (rev 4933) +++ trunk/shell/src/shell/org/jnode/shell/bjorne/ListCommandNode.java 2009-01-31 12:46:57 UTC (rev 4934) @@ -148,7 +148,7 @@ for (PipelineStage stage : stages) { CommandIO[] streams = new CommandIO[stage.holders.length]; for (int i = 0; i < streams.length; i++) { - streams[i] = stage.holders[i].stream; + streams[i] = stage.holders[i].getStream(); } stage.thread = stage.context.fork(stage.command, streams); } Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/SimpleCommandNode.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/bjorne/SimpleCommandNode.java 2009-01-31 12:45:49 UTC (rev 4933) +++ trunk/shell/src/shell/org/jnode/shell/bjorne/SimpleCommandNode.java 2009-01-31 12:46:57 UTC (rev 4934) @@ -87,7 +87,7 @@ holders = childContext.evaluateRedirections(getRedirects()); CommandIO[] streams = new CommandIO[holders.length]; for (int i = 0; i < streams.length; i++) { - streams[i] = holders[i].stream; + streams[i] = holders[i].getStream(); } if ((getFlags() & BjorneInterpreter.FLAG_ASYNC) != 0) { throw new ShellFailureException( This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |