Revision: 5634
          http://jnode.svn.sourceforge.net/jnode/?rev=5634&view=rev
Author:   crawley
Date:     2009-08-09 14:52:17 +0000 (Sun, 09 Aug 2009)

Log Message:
-----------
Implemented command substitution using the $(...) syntax.

Modified Paths:
--------------
    trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java
    trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneContextTest.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-08-09 07:20:10 UTC (rev 5633)
+++ trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java     
2009-08-09 14:52:17 UTC (rev 5634)
@@ -613,7 +613,7 @@
         }
     }
 
-    private StringBuffer runBacktickCommand(String commandLine) throws 
ShellException {
+    protected StringBuffer runBacktickCommand(String commandLine) throws 
ShellException {
         StringWriter capture = new StringWriter();
         interpreter.interpret(interpreter.getShell(), new 
StringReader(commandLine), false, capture, false);
         StringBuffer output = capture.getBuffer();
@@ -1027,11 +1027,26 @@
     public boolean isSet(String name) {
         return variables.get(name) != null;
     }
-    
-    private String dollarParenExpand(CharIterator ci) throws ShellException {
-        throw new ShellSyntaxException("$( and $(( not implemented yet");
+
+    private CharSequence dollarParenExpand(CharIterator ci) throws 
ShellException {
+        if (ci.peekCh() == '(') {
+            ci.nextCh();
+            return dollarParenParenExpand(ci);
+        }
+        else {
+            String commandLine = dollarBacktickExpand(ci, ')').toString();
+            if (ci.nextCh() != ')') {
+                throw new ShellSyntaxException("Unmatched \"(\" (left 
parenthesis)");
+            }
+            return runBacktickCommand(commandLine);
+        }
     }
 
+    private CharSequence dollarParenParenExpand(CharIterator ci) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
 //    private String dollarParenExpand(CharIterator ci) throws ShellException {
 //        StringBuilder sb = extractToMatchingParen(ci);
 //        if (sb.length() > 0 && sb.charAt(sb.length()) == ')') {

Modified: 
trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneContextTest.java
===================================================================
--- trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneContextTest.java     
2009-08-09 07:20:10 UTC (rev 5633)
+++ trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneContextTest.java     
2009-08-09 14:52:17 UTC (rev 5634)
@@ -31,7 +31,6 @@
 import org.jnode.shell.PathnamePattern;
 import org.jnode.shell.ShellException;
 import org.jnode.shell.bjorne.BjorneContext;
-import org.jnode.shell.bjorne.BjorneInterpreter;
 import org.jnode.shell.bjorne.BjorneToken;
 import org.jnode.shell.io.CommandIOHolder;
 
@@ -45,77 +44,94 @@
     
     // This class simply allows us to call the setVariable method directly
     private static class TestBjorneContext extends BjorneContext {
-        TestBjorneContext(BjorneInterpreter interpreter, CommandIOHolder[] 
holders) {
-            super(interpreter, holders);
+        TestBjorneContext(CommandIOHolder[] holders) {
+            super(null, holders);
         }
         
+        TestBjorneContext() {
+            super(null, null);
+        }
+        
+        /**
+         * Expose method for testing
+         */
         @Override
         protected void setVariable(String name, String value) {
             super.setVariable(name, value);
         }
+
+        /**
+         * For testing, 'execute' a command by converting to lowercase with 
'-' guards.
+         */
+        @Override
+        protected StringBuffer runBacktickCommand(String commandLine) throws 
ShellException {
+            return new StringBuffer("-" + commandLine.toLowerCase() + "-");
+        }
     }
+    
+    
 
     public void testContext() {
         new BjorneContext(null, null);
     }
 
     public void testExpand1() throws ShellException {
-        BjorneContext context = new BjorneContext(null, null);
+        BjorneContext context = new TestBjorneContext();
         List<BjorneToken> expansion = context.expandAndSplit();
         checkExpansion(expansion, new String[] {});
     }
 
     public void testExpand3() throws ShellException {
-        BjorneContext context = new BjorneContext(null, null);
+        BjorneContext context = new TestBjorneContext();
         List<BjorneToken> expansion = context.expandAndSplit(
                 new BjorneToken("hi"));
         checkExpansion(expansion, new String[] {"hi"});
     }
 
     public void testExpand4() throws ShellException {
-        BjorneContext context = new BjorneContext(null, null);
+        BjorneContext context = new TestBjorneContext();
         List<BjorneToken> expansion = context.expandAndSplit(
                 new BjorneToken("hi there"));
         checkExpansion(expansion, new String[] {"hi", "there"});
     }
 
     public void testExpand5() throws ShellException {
-        BjorneContext context = new BjorneContext(null, null);
+        BjorneContext context = new TestBjorneContext();
         List<BjorneToken> expansion = context.expandAndSplit(
                 new BjorneToken("'hi there '"));
         checkExpansion(expansion, new String[] {"hi there "});
     }
 
     public void testExpand6() throws ShellException {
-        BjorneContext context = new BjorneContext(null, null);
+        BjorneContext context = new TestBjorneContext();
         List<BjorneToken> expansion = context.expandAndSplit(
                 new BjorneToken("\"hi there \" "));
         checkExpansion(expansion, new String[] {"hi there "});
     }
 
     public void testExpand7() throws ShellException {
-        BjorneContext context = new BjorneContext(null, null);
+        BjorneContext context = new TestBjorneContext();
         List<BjorneToken> expansion = context.expandAndSplit(
                 new BjorneToken("hi\\ there"));
         checkExpansion(expansion, new String[] {"hi there"});
     }
 
     public void testExpand8() throws ShellException {
-        BjorneContext context = new BjorneContext(null, null);
+        BjorneContext context = new TestBjorneContext();
         List<BjorneToken> expansion = context.expandAndSplit(
                 new BjorneToken("\\\"hi\\ there\\\""));
         checkExpansion(expansion, new String[] {"\"hi there\""});
     }
 
     public void testExpand9() throws ShellException {
-        BjorneContext context = new BjorneContext(null, null);
+        BjorneContext context = new TestBjorneContext();
         List<BjorneToken> expansion = context.expandAndSplit(
                 new BjorneToken("$?"));
         checkExpansion(expansion, new String[] {"0"});
     }
 
     public void testExpand10() throws ShellException {
-        TestBjorneContext context = new TestBjorneContext(null, null);
+        TestBjorneContext context = new TestBjorneContext();
         context.setVariable("A", "A");
         List<BjorneToken> expansion = context.expandAndSplit(
                 new BjorneToken("$A"));
@@ -123,7 +139,7 @@
     }
 
     public void testExpand11() throws ShellException {
-        TestBjorneContext context = new TestBjorneContext(null, null);
+        TestBjorneContext context = new TestBjorneContext();
         context.setVariable("A", "A");
         List<BjorneToken> expansion = context.expandAndSplit(
                 new BjorneToken("\\$A"));
@@ -131,7 +147,7 @@
     }
 
     public void testExpand12() throws ShellException {
-        TestBjorneContext context = new TestBjorneContext(null, null);
+        TestBjorneContext context = new TestBjorneContext();
         context.setVariable("A", "A");
         List<BjorneToken> expansion = context.expandAndSplit(
                 new BjorneToken("\"$A\""));
@@ -139,7 +155,7 @@
     }
 
     public void testExpand13() throws ShellException {
-        TestBjorneContext context = new TestBjorneContext(null, null);
+        TestBjorneContext context = new TestBjorneContext();
         context.setVariable("A", "A");
         List<BjorneToken> expansion = context.expandAndSplit(
                 new BjorneToken("'$A'"));
@@ -147,7 +163,7 @@
     }
 
     public void testExpand14() throws ShellException {
-        TestBjorneContext parentContext = new TestBjorneContext(null, new 
CommandIOHolder[0]);
+        TestBjorneContext parentContext = new TestBjorneContext(new 
CommandIOHolder[0]);
         parentContext.setVariable("A", "A");
         BjorneContext context = new BjorneContext(parentContext);
         List<BjorneToken> expansion = context.expandAndSplit(
@@ -157,7 +173,7 @@
 
     public void testExpand15() throws Exception {
         PathnamePattern.clearCache();
-        BjorneContext context = new BjorneContext(null, null);
+        BjorneContext context = new TestBjorneContext();
         assertEquals(true, context.isGlobbing());
         assertEquals(true, context.isTildeExpansion());
         if (new File("../README.txt").exists()) {
@@ -180,7 +196,7 @@
     }
 
     public void testExpand16() throws Exception {
-        BjorneContext context = new BjorneContext(null, null);
+        BjorneContext context = new TestBjorneContext();
         assertEquals(true, context.isGlobbing());
         assertEquals(true, context.isTildeExpansion());
         CommandLine expansion = context.buildCommandLine(new BjorneToken("~"));
@@ -191,7 +207,7 @@
     }
     
     public void testExpand17() throws ShellException {
-        TestBjorneContext context = new TestBjorneContext(null, null);
+        TestBjorneContext context = new TestBjorneContext();
         context.setVariable("A", "A");
         List<BjorneToken> expansion = context.expandAndSplit(
                 new BjorneToken("${A}"));
@@ -199,7 +215,7 @@
     }
 
     public void testExpand18() throws ShellException {
-        TestBjorneContext context = new TestBjorneContext(null, null);
+        TestBjorneContext context = new TestBjorneContext();
         context.setVariable("A", "A");
         context.setVariable("B", "");
         List<BjorneToken> expansion = context.expandAndSplit(
@@ -208,7 +224,7 @@
     }
 
     public void testExpand19() throws ShellException {
-        TestBjorneContext context = new TestBjorneContext(null, null);
+        TestBjorneContext context = new TestBjorneContext();
         context.setVariable("A", "A");
         context.setVariable("B", "");
         List<BjorneToken> expansion = context.expandAndSplit(
@@ -217,14 +233,28 @@
     }
 
     public void testExpand20() throws ShellException {
-        TestBjorneContext context = new TestBjorneContext(null, null);
+        TestBjorneContext context = new TestBjorneContext();
         context.setVariable("A", "");
         context.setVariable("B", "B");
         List<BjorneToken> expansion = context.expandAndSplit(
                 new BjorneToken("${A:-$B} ${A:-${B}} ${A:-${A:-$B}} 
${A:-'${B}'}"));
         checkExpansion(expansion, new String[] {"B", "B", "B", "${B}"});
     }
+    
+    public void testExpand21() throws ShellException {
+        TestBjorneContext context = new TestBjorneContext();
+        List<BjorneToken> expansion = context.expandAndSplit(
+                new BjorneToken("`Hello`"));
+        checkExpansion(expansion, new String[] {"-hello-"});
+    }
 
+    public void testExpand22() throws ShellException {
+        TestBjorneContext context = new TestBjorneContext();
+        List<BjorneToken> expansion = context.expandAndSplit(
+                new BjorneToken("$(Hello)"));
+        checkExpansion(expansion, new String[] {"-hello-"});
+    }
+
     private void checkExpansion(List<BjorneToken> expansion, String[] 
expected) {
         int i;
         Iterator<BjorneToken> it = expansion.iterator();

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-08-09 07:20:10 UTC (rev 5633)
+++ trunk/shell/src/test/org/jnode/test/shell/bjorne/bjorne-shell-tests.xml     
2009-08-09 14:52:17 UTC (rev 5634)
@@ -70,7 +70,7 @@
 ] +
 </output>
     </testSpec>
-    <testSpec title="``" command="test" runMode="AS_SCRIPT" rc="0">
+    <testSpec title="`...`" command="test" runMode="AS_SCRIPT" rc="0">
         <script>#!bjorne
 A=1
 echo $A
@@ -84,6 +84,20 @@
 1 1
 </output>
     </testSpec>
+    <testSpec title="$(...)" command="test" runMode="AS_SCRIPT" rc="0">
+        <script>#!bjorne
+A=1
+echo $A
+B="$(expr $A + 1)"
+echo $B
+C="$(echo $A $A)"
+echo $C
+</script>
+        <output>1
+2
+1 1
+</output>
+    </testSpec>
     <testSpec title="if ... then ... fi" command="test" runMode="AS_SCRIPT" 
rc="0">
         <script>#!bjorne
 if true ; then echo HI ; fi


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Jnode-svn-commits mailing list
Jnode-svn-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jnode-svn-commits

Reply via email to