|
From: <cr...@us...> - 2009-02-01 04:53:38
|
Revision: 4951
http://jnode.svn.sourceforge.net/jnode/?rev=4951&view=rev
Author: crawley
Date: 2009-02-01 04:53:32 +0000 (Sun, 01 Feb 2009)
Log Message:
-----------
The exit builtin now sets the return code correctly in a script
Modified Paths:
--------------
trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java
trunk/shell/src/shell/org/jnode/shell/bjorne/ExitBuiltin.java
trunk/shell/src/test/org/jnode/test/shell/bjorne/bjorne-shell-tests.xml
Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java 2009-02-01 03:12:50 UTC (rev 4950)
+++ trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java 2009-02-01 04:53:32 UTC (rev 4951)
@@ -159,7 +159,28 @@
@Override
public int interpret(CommandShell shell, String command) throws ShellException {
- return interpret(shell, command, null, false);
+ try {
+ return interpret(shell, command, null, false);
+ } catch (BjorneControlException ex) {
+ switch (ex.getControl()) {
+ case BjorneInterpreter.BRANCH_EXIT:
+ // FIXME this is not right. If 'exit' is run in an interactive
+ // shell, the shell needs to exit.
+ return ex.getCount();
+ case BjorneInterpreter.BRANCH_BREAK:
+ throw new ShellSyntaxException(
+ "'break' has been executed in an inappropriate context");
+ case BjorneInterpreter.BRANCH_CONTINUE:
+ throw new ShellSyntaxException(
+ "'continue' has been executed in an inappropriate context");
+ case BjorneInterpreter.BRANCH_RETURN:
+ throw new ShellSyntaxException(
+ "'return' has been executed in an inappropriate context");
+ default:
+ throw new ShellFailureException("control exception with bad control code (" +
+ ex.getControl() + ")");
+ }
+ }
}
@Override
@@ -214,25 +235,25 @@
if (DEBUG) {
System.err.println(tree);
}
- try {
+// try {
if (capture == null) {
// FIXME ... this may add an empty line to the command history
shell.addCommandToHistory(command);
}
return tree.execute((BjorneContext) myContext);
- } catch (BjorneControlException ex) {
- switch (ex.getControl()) {
- case BRANCH_EXIT:
- return ex.getCount();
- case BRANCH_BREAK:
- case BRANCH_CONTINUE:
- return 0;
- case BRANCH_RETURN:
- return (source) ? ex.getCount() : 1;
- default:
- throw new ShellFailureException("unknown control " + ex.getControl());
- }
- }
+// } catch (BjorneControlException ex) {
+// switch (ex.getControl()) {
+// case BRANCH_EXIT:
+// return ex.getCount();
+// case BRANCH_BREAK:
+// case BRANCH_CONTINUE:
+// return 0;
+// case BRANCH_RETURN:
+// return (source) ? ex.getCount() : 1;
+// default:
+// throw new ShellFailureException("unknown control " + ex.getControl());
+// }
+// }
}
@Override
@@ -243,7 +264,24 @@
String line;
int rc = 0;
while ((line = br.readLine()) != null) {
- rc = interpret(shell, line);
+ try {
+ rc = interpret(shell, line, null, false);
+ } catch (BjorneControlException ex) {
+ switch (ex.getControl()) {
+ case BjorneInterpreter.BRANCH_EXIT:
+ // The script will exit immediately
+ return ex.getCount();
+ case BjorneInterpreter.BRANCH_BREAK:
+ throw new ShellSyntaxException(
+ "'break' has been executed in an inappropriate context");
+ case BjorneInterpreter.BRANCH_CONTINUE:
+ throw new ShellSyntaxException(
+ "'continue' has been executed in an inappropriate context");
+ case BjorneInterpreter.BRANCH_RETURN:
+ throw new ShellSyntaxException(
+ "'return' has been executed in an inappropriate context");
+ }
+ }
}
return rc;
} catch (IOException ex) {
Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/ExitBuiltin.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/bjorne/ExitBuiltin.java 2009-02-01 03:12:50 UTC (rev 4950)
+++ trunk/shell/src/shell/org/jnode/shell/bjorne/ExitBuiltin.java 2009-02-01 04:53:32 UTC (rev 4951)
@@ -32,7 +32,7 @@
Iterator<String> args = command.iterator();
if (!args.hasNext()) {
throw new BjorneControlException(BjorneInterpreter.BRANCH_EXIT,
- context.getLastReturnCode());
+ context.getParent().getLastReturnCode());
} else {
String arg = args.next();
try {
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-02-01 03:12:50 UTC (rev 4950)
+++ trunk/shell/src/test/org/jnode/test/shell/bjorne/bjorne-shell-tests.xml 2009-02-01 04:53:32 UTC (rev 4951)
@@ -112,4 +112,27 @@
</output>
<rc>0</rc>
</testSpec>
+ <testSpec>
+ <title>while ... do ... break ... done</title>
+ <command>test</command>
+ <runMode>AS_SCRIPT</runMode>
+ <script>#!bjorne
+A=5
+while expr $A != 0 ; do echo A is $A ; if expr $A = 2 ; then break ; fi ; A=`expr $A - 1`; done
+ </script>
+ <output>1
+A is 5
+0
+1
+A is 4
+0
+1
+A is 3
+0
+1
+A is 2
+1
+</output>
+ <rc>0</rc>
+ </testSpec>
</testSpecs>
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|