Revision: 5661
          http://jnode.svn.sourceforge.net/jnode/?rev=5661&view=rev
Author:   crawley
Date:     2009-08-24 14:11:14 +0000 (Mon, 24 Aug 2009)

Log Message:
-----------
Shell functions now support arguments and redirection

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/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-24 10:52:13 UTC (rev 5660)
+++ trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java     
2009-08-24 14:11:14 UTC (rev 5661)
@@ -1183,6 +1183,27 @@
     }
     
     /**
+     * Evaluate the redirections for this command against a set of streams, 
saving the context's existing IOs.
+     * 
+     * @param redirects the redirection nodes to be evaluated
+     * @param streams the base CommandIOs for the redirection calculation.
+     * @throws ShellException
+     */
+    void evaluateRedirectionsAndPushHolders(RedirectionNode[] redirects, 
CommandIO[] streams) 
+        throws ShellException {
+        if (savedHolders == null) {
+            savedHolders = new ArrayList<CommandIOHolder[]>(1);
+        }
+        savedHolders.add(holders);
+        holders = new CommandIOHolder[streams.length];
+        for (int i = 0; i < holders.length; i++) {
+            // Don't take ownership of the streams.
+            holders[i] = new CommandIOHolder(streams[i], false);
+        }
+        evaluateRedirections(redirects, holders);
+    }
+    
+    /**
      * Close the context's current IO, restoring the previous ones.
      * @throws ShellException
      */
@@ -1199,8 +1220,8 @@
      * @return the stream state after redirections
      * @throws ShellException
      */
-    void evaluateRedirections(
-            RedirectionNode[] redirects, CommandIOHolder[] holders) throws 
ShellException {
+    void evaluateRedirections(RedirectionNode[] redirects, CommandIOHolder[] 
holders) 
+        throws ShellException {
         if (redirects == null) {
             return;
         }

Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java 
2009-08-24 10:52:13 UTC (rev 5660)
+++ trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java 
2009-08-24 14:11:14 UTC (rev 5661)
@@ -322,27 +322,34 @@
         } else {
             CommandNode body = context.getFunction(commandName);
             if (body != null) {
-                // FIXME setup a new context, streams and args.
-                return body.execute(context);
+                
context.evaluateRedirectionsAndPushHolders(body.getRedirects(), streams);
+                String[] savedArgs = context.getArgs();
+                try {
+                    context.setArgs(cmdLine.getArguments());
+                    return body.execute(context);
+                } finally {
+                    context.popHolders();
+                    context.setArgs(savedArgs);
+                }
             }
         }
         cmdLine.setStreams(streams);
         return shell.invoke(cmdLine, sysProps, env);
     }
 
-    public BjorneContext createContext() throws ShellFailureException {
+    BjorneContext createContext() throws ShellFailureException {
         return new BjorneContext(this);
     }
 
-    public CommandShell getShell() {
+    CommandShell getShell() {
         return shell;
     }
 
-    public PrintStream resolvePrintStream(CommandIO commandIOIF) {
+    PrintStream resolvePrintStream(CommandIO commandIOIF) {
         return shell.resolvePrintStream(commandIOIF);
     }
 
-    public InputStream resolveInputStream(CommandIO stream) {
+    InputStream resolveInputStream(CommandIO stream) {
         return shell.resolveInputStream(stream);
     }
     
@@ -350,7 +357,7 @@
         return subshellCount++;
     }
 
-    public String getUniqueName() {
+    String getUniqueName() {
         return getName() + "-" + getSubshellNumber();
     }
 }

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-24 10:52:13 UTC (rev 5660)
+++ trunk/shell/src/test/org/jnode/test/shell/bjorne/bjorne-shell-tests.xml     
2009-08-24 14:11:14 UTC (rev 5661)
@@ -976,7 +976,20 @@
                 }
                 foo
         </script>
-        <output >hi
+        <output>hi
 </output>
     </testSpec>
+    <testSpec title="shell function 2" command="test" runMode="AS_SCRIPT" 
rc="0">
+        <script>#!bjorne
+                foo() {
+                    echo $1 $2
+                }
+                foo hi there
+                foo bye mum &gt;&amp;2
+        </script>
+        <output>hi there
+</output>
+        <error>bye mum
+</error>
+    </testSpec>
 </testSet>


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