|
From: <cr...@us...> - 2009-06-09 14:05:17
|
Revision: 5566
http://jnode.svn.sourceforge.net/jnode/?rev=5566&view=rev
Author: crawley
Date: 2009-06-09 14:04:19 +0000 (Tue, 09 Jun 2009)
Log Message:
-----------
Compound commands now support redirection
Modified Paths:
--------------
trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java
trunk/shell/src/shell/org/jnode/shell/bjorne/CaseCommandNode.java
trunk/shell/src/shell/org/jnode/shell/bjorne/ForCommandNode.java
trunk/shell/src/shell/org/jnode/shell/bjorne/IfCommandNode.java
trunk/shell/src/shell/org/jnode/shell/bjorne/ListCommandNode.java
trunk/shell/src/shell/org/jnode/shell/bjorne/LoopCommandNode.java
trunk/shell/src/test/org/jnode/test/shell/bjorne/bjorne-shell-tests.xml
Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java 2009-06-09 12:33:55 UTC (rev 5565)
+++ trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java 2009-06-09 14:04:19 UTC (rev 5566)
@@ -125,6 +125,8 @@
private String options = "";
private CommandIOHolder[] holders;
+
+ private List<CommandIOHolder[]> savedHolders;
private boolean echoExpansions;
@@ -1094,6 +1096,30 @@
}
/**
+ * Evaluate the redirections for this command, saving the context's existing IOs
+ *
+ * @param redirects the redirection nodes to be evaluated
+ * @throws ShellException
+ */
+ void evaluateRedirectionsAndPushHolders(RedirectionNode[] redirects) throws ShellException {
+ if (savedHolders == null) {
+ savedHolders = new ArrayList<CommandIOHolder[]>(1);
+ }
+ savedHolders.add(holders);
+ holders = copyStreamHolders(holders);
+ evaluateRedirections(redirects, holders);
+ }
+
+ /**
+ * Close the context's current IO, restoring the previous ones.
+ * @throws ShellException
+ */
+ void popHolders() {
+ closeIOs();
+ holders = savedHolders.remove(savedHolders.size() - 1);
+ }
+
+ /**
* Evaluate the redirections for this command.
*
* @param redirects the redirection nodes to be evaluated
Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/CaseCommandNode.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/bjorne/CaseCommandNode.java 2009-06-09 12:33:55 UTC (rev 5565)
+++ trunk/shell/src/shell/org/jnode/shell/bjorne/CaseCommandNode.java 2009-06-09 14:04:19 UTC (rev 5566)
@@ -60,24 +60,28 @@
@Override
public int execute(BjorneContext context) throws ShellException {
- int rc = 0;
-
- CharSequence expandedWord = context.dollarBacktickExpand(word.text);
- LOOP:
- for (CaseItemNode caseItem : caseItems) {
- for (BjorneToken pattern : caseItem.getPattern()) {
- CharSequence pat = context.dollarBacktickExpand(pattern.text);
- if (context.patternMatch(expandedWord, pat)) {
- rc = caseItem.getBody().execute(context);
- break LOOP;
+ try {
+ int rc = 0;
+ context.evaluateRedirectionsAndPushHolders(getRedirects());
+ CharSequence expandedWord = context.dollarBacktickExpand(word.text);
+ LOOP:
+ for (CaseItemNode caseItem : caseItems) {
+ for (BjorneToken pattern : caseItem.getPattern()) {
+ CharSequence pat = context.dollarBacktickExpand(pattern.text);
+ if (context.patternMatch(expandedWord, pat)) {
+ rc = caseItem.getBody().execute(context);
+ break LOOP;
+ }
}
}
- }
- if ((getFlags() & BjorneInterpreter.FLAG_BANG) != 0) {
- rc = (rc == 0) ? -1 : 0;
+ if ((getFlags() & BjorneInterpreter.FLAG_BANG) != 0) {
+ rc = (rc == 0) ? -1 : 0;
+ }
+ return rc;
+ } finally {
+ context.popHolders();
}
- return rc;
}
@Override
Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/ForCommandNode.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/bjorne/ForCommandNode.java 2009-06-09 12:33:55 UTC (rev 5565)
+++ trunk/shell/src/shell/org/jnode/shell/bjorne/ForCommandNode.java 2009-06-09 14:04:19 UTC (rev 5566)
@@ -92,13 +92,18 @@
@Override
public int execute(BjorneContext context) throws ShellException {
- int rc = 0;
- List<BjorneToken> expanded = context.expandAndSplit(words);
- for (BjorneToken word : expanded) {
- context.setVariable(var.getText(), word.getText());
- rc = body.execute(context);
+ try {
+ int rc = 0;
+ context.evaluateRedirectionsAndPushHolders(getRedirects());
+ List<BjorneToken> expanded = context.expandAndSplit(words);
+ for (BjorneToken word : expanded) {
+ context.setVariable(var.getText(), word.getText());
+ rc = body.execute(context);
+ }
+ return rc;
+ } finally {
+ context.popHolders();
}
- return rc;
}
@Override
Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/IfCommandNode.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/bjorne/IfCommandNode.java 2009-06-09 12:33:55 UTC (rev 5565)
+++ trunk/shell/src/shell/org/jnode/shell/bjorne/IfCommandNode.java 2009-06-09 14:04:19 UTC (rev 5566)
@@ -100,20 +100,25 @@
@Override
public int execute(BjorneContext context) throws ShellException {
- int rc = cond.execute(context);
- if (rc == 0) {
- if (thenPart != null) {
- return thenPart.execute(context);
+ try {
+ context.evaluateRedirectionsAndPushHolders(getRedirects());
+ int rc = cond.execute(context);
+ if (rc == 0) {
+ if (thenPart != null) {
+ return thenPart.execute(context);
+ }
+ } else {
+ if (elsePart != null) {
+ return elsePart.execute(context);
+ }
}
- } else {
- if (elsePart != null) {
- return elsePart.execute(context);
+ if ((getFlags() & BjorneInterpreter.FLAG_BANG) != 0) {
+ rc = (rc == 0) ? -1 : 0;
}
+ return rc;
+ } finally {
+ context.popHolders();
}
- if ((getFlags() & BjorneInterpreter.FLAG_BANG) != 0) {
- rc = (rc == 0) ? -1 : 0;
- }
- return rc;
}
@Override
Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/ListCommandNode.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/bjorne/ListCommandNode.java 2009-06-09 12:33:55 UTC (rev 5565)
+++ trunk/shell/src/shell/org/jnode/shell/bjorne/ListCommandNode.java 2009-06-09 14:04:19 UTC (rev 5566)
@@ -30,7 +30,6 @@
import org.jnode.shell.Completable;
import org.jnode.shell.ShellException;
import org.jnode.shell.help.CompletionException;
-import org.jnode.shell.io.CommandIOHolder;
public class ListCommandNode extends CommandNode implements Completable {
@@ -59,25 +58,26 @@
public int execute(BjorneContext context) throws ShellException {
int listFlags = getFlags();
int rc = 0;
- try {
- if ((listFlags & BjorneInterpreter.FLAG_PIPE) != 0) {
- BjornePipeline pipeline = buildPipeline(context);
- try {
- pipeline.wire();
- rc = pipeline.run(context.getShell());
- } finally {
- pipeline.closeStreams();
- }
- } else {
- if (getNodeType() == BjorneInterpreter.CMD_SUBSHELL) {
+ if ((listFlags & BjorneInterpreter.FLAG_PIPE) != 0) {
+ BjornePipeline pipeline = buildPipeline(context);
+ try {
+ pipeline.wire();
+ rc = pipeline.run(context.getShell());
+ } finally {
+ pipeline.closeStreams();
+ }
+ } else {
+ int nt = getNodeType();
+ try {
+ if (nt == BjorneInterpreter.CMD_SUBSHELL) {
// This simulates creating a 'subshell'.
context = new BjorneContext(context);
- CommandIOHolder[] holders = context.evaluateRedirections(getRedirects());
- for (int i = 0; i < holders.length; i++) {
- context.setIO(i, holders[i]);
- }
+ context.evaluateRedirectionsAndPushHolders(getRedirects());
+ } else if (nt == BjorneInterpreter.CMD_BRACE_GROUP) {
+ context.evaluateRedirectionsAndPushHolders(getRedirects());
}
+
for (CommandNode command : commands) {
int commandFlags = command.getFlags();
if ((commandFlags & BjorneInterpreter.FLAG_AND_IF) != 0) {
@@ -92,11 +92,11 @@
}
rc = command.execute(context);
}
+ } finally {
+ if (nt == BjorneInterpreter.CMD_SUBSHELL || nt == BjorneInterpreter.CMD_BRACE_GROUP) {
+ context.popHolders();
+ }
}
- } finally {
- if (getNodeType() == BjorneInterpreter.CMD_SUBSHELL) {
- context.closeIOs();
- }
}
if ((listFlags & BjorneInterpreter.FLAG_BANG) != 0) {
rc = (rc == 0) ? -1 : 0;
Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/LoopCommandNode.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/bjorne/LoopCommandNode.java 2009-06-09 12:33:55 UTC (rev 5565)
+++ trunk/shell/src/shell/org/jnode/shell/bjorne/LoopCommandNode.java 2009-06-09 14:04:19 UTC (rev 5566)
@@ -74,39 +74,44 @@
}
public int execute(BjorneContext context) throws ShellException {
- int rc = 0;
- while (true) {
- rc = cond.execute(context);
- if (rc != 0 && getNodeType() == BjorneInterpreter.CMD_WHILE ||
- rc == 0 && getNodeType() == BjorneInterpreter.CMD_UNTIL) {
- break;
- }
- try {
- if (body != null) {
- body.execute(context);
+ try {
+ int rc = 0;
+ context.evaluateRedirectionsAndPushHolders(getRedirects());
+ while (true) {
+ rc = cond.execute(context);
+ if (rc != 0 && getNodeType() == BjorneInterpreter.CMD_WHILE ||
+ rc == 0 && getNodeType() == BjorneInterpreter.CMD_UNTIL) {
+ break;
}
- } catch (BjorneControlException ex) {
- int control = ex.getControl();
- if (control == BjorneInterpreter.BRANCH_BREAK ||
- control == BjorneInterpreter.BRANCH_CONTINUE) {
- if (ex.getCount() > 1) {
- ex.decrementCount();
- throw ex;
+ try {
+ if (body != null) {
+ body.execute(context);
}
- if (control == BjorneInterpreter.BRANCH_BREAK) {
- break;
+ } catch (BjorneControlException ex) {
+ int control = ex.getControl();
+ if (control == BjorneInterpreter.BRANCH_BREAK ||
+ control == BjorneInterpreter.BRANCH_CONTINUE) {
+ if (ex.getCount() > 1) {
+ ex.decrementCount();
+ throw ex;
+ }
+ if (control == BjorneInterpreter.BRANCH_BREAK) {
+ break;
+ } else {
+ continue;
+ }
} else {
- continue;
+ throw ex;
}
- } else {
- throw ex;
}
}
+ if ((getFlags() & BjorneInterpreter.FLAG_BANG) != 0) {
+ rc = (rc == 0) ? -1 : 0;
+ }
+ return rc;
+ } finally {
+ context.popHolders();
}
- if ((getFlags() & BjorneInterpreter.FLAG_BANG) != 0) {
- rc = (rc == 0) ? -1 : 0;
- }
- return rc;
}
@Override
Modified: trunk/shell/src/test/org/jnode/test/shell/bjorne/bjorne-shell-tests.xml
===================================================================
--- trunk/shell/src/test/org/jnode/test/shell/bjorne/bjorne-shell-tests.xml 2009-06-09 12:33:55 UTC (rev 5565)
+++ trunk/shell/src/test/org/jnode/test/shell/bjorne/bjorne-shell-tests.xml 2009-06-09 14:04:19 UTC (rev 5566)
@@ -859,4 +859,47 @@
false
</script>
</testSpec>
+ <testSpec title="brace group" command="test" runMode="AS_SCRIPT" rc="0">
+ <script>#!bjorne
+ { echo hi ; echo there ; }
+ </script>
+ <output>hi
+there
+</output>
+ </testSpec>
+ <testSpec title="brace group redir" command="test" runMode="AS_SCRIPT" rc="0">
+ <script>#!bjorne
+ { echo hi ; echo there ; } > @TEMP_DIR@/1
+ </script>
+ <file name="1" input="false">hi
+there
+</file>
+ </testSpec>
+ <testSpec title="for redir" command="test" runMode="AS_SCRIPT" rc="0">
+ <script>#!bjorne
+ for i in 1 2 3 ; do echo $i ; done > @TEMP_DIR@/1
+ </script>
+ <file name="1" input="false">1
+2
+3
+</file>
+ </testSpec>
+ <testSpec title="while redir" command="test" runMode="AS_SCRIPT" rc="1">
+ <script>#!bjorne
+ A=5
+ while expr $A != 0 ; do echo $A ; A=`expr $A - 1`; done > @TEMP_DIR@/1
+ </script>
+ <file name="1" input="false">1
+5
+1
+4
+1
+3
+1
+2
+1
+1
+0
+</file>
+ </testSpec>
</testSet>
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|