|
From: <cr...@us...> - 2009-02-08 15:02:28
|
Revision: 5008
http://jnode.svn.sourceforge.net/jnode/?rev=5008&view=rev
Author: crawley
Date: 2009-02-08 15:02:25 +0000 (Sun, 08 Feb 2009)
Log Message:
-----------
Implemented 'shift' built-in
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
Added Paths:
-----------
trunk/shell/src/shell/org/jnode/shell/bjorne/ShiftBuiltin.java
Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java 2009-02-08 13:20:40 UTC (rev 5007)
+++ trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java 2009-02-08 15:02:25 UTC (rev 5008)
@@ -1090,4 +1090,12 @@
// TODO Auto-generated method stub
return false;
}
+
+ public String[] getArgs() {
+ return args.toArray(new String[args.size()]);
+ }
+
+ public int nosArgs() {
+ return args.size();
+ }
}
Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java 2009-02-08 13:20:40 UTC (rev 5007)
+++ trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java 2009-02-08 15:02:25 UTC (rev 5008)
@@ -142,6 +142,7 @@
BUILTINS.put("exit", new ExitBuiltin());
BUILTINS.put("return", new ReturnBuiltin());
BUILTINS.put("set", new SetBuiltin());
+ BUILTINS.put("shift", new ShiftBuiltin());
BUILTINS.put(".", new SourceBuiltin());
BUILTINS.put(":", new ColonBuiltin());
}
Added: trunk/shell/src/shell/org/jnode/shell/bjorne/ShiftBuiltin.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/bjorne/ShiftBuiltin.java (rev 0)
+++ trunk/shell/src/shell/org/jnode/shell/bjorne/ShiftBuiltin.java 2009-02-08 15:02:25 UTC (rev 5008)
@@ -0,0 +1,63 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2003-2009 JNode.org
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; If not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+package org.jnode.shell.bjorne;
+
+import org.jnode.shell.CommandLine;
+import org.jnode.shell.ShellException;
+import org.jnode.shell.ShellSyntaxException;
+
+final class ShiftBuiltin extends BjorneBuiltin {
+
+ @SuppressWarnings("deprecation")
+ public int invoke(CommandLine command, BjorneInterpreter interpreter,
+ BjorneContext context) throws ShellException {
+ context = context.getParent();
+ String[] args = command.getArguments();
+ int nos;
+ if (args.length == 0) {
+ nos = 1;
+ } else if (args.length == 1) {
+ try {
+ nos = Integer.parseInt(args[0]);
+ if (nos < 0) {
+ new ShellSyntaxException("Argument for 'shift' is negative: " + args[0]);
+ }
+ } catch (NumberFormatException ex) {
+ throw new ShellSyntaxException("Nonnumeric argument for 'shift': " + args[0]);
+ }
+ } else {
+ throw new ShellSyntaxException("Too many arguments for 'shift'");
+ }
+ if (nos == 0) {
+ return 0;
+ }
+ int nosOldArgs = context.nosArgs();
+ if (nos >= nosOldArgs) {
+ context.setArgs(new String[0]);
+ return nos == nosOldArgs ? 0 : 1;
+ }
+ String[] oldArgs = context.getArgs();
+ String[] newArgs = new String[oldArgs.length - nos];
+ System.arraycopy(oldArgs, nos, newArgs, 0, newArgs.length);
+ context.setArgs(newArgs);
+ return 0;
+ }
+}
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-08 13:20:40 UTC (rev 5007)
+++ trunk/shell/src/test/org/jnode/test/shell/bjorne/bjorne-shell-tests.xml 2009-02-08 15:02:25 UTC (rev 5008)
@@ -509,6 +509,27 @@
</output>
</testSpec>
+ <testSpec title="set arguments" command="test" runMode="AS_SCRIPT" rc="0">
+ <script>#!bjorne
+ echo $*
+ shift 0
+ echo $*
+ shift
+ echo $*
+ shift 2
+ echo $*
+ </script>
+ <arg>1</arg>
+ <arg>2</arg>
+ <arg>3</arg>
+ <arg>4</arg>
+ <arg>5</arg>
+ <output>1 2 3 4 5
+1 2 3 4 5
+2 3 4 5
+4 5
+</output>
+ </testSpec>
<testSpec title="quote handling" command="test" runMode="AS_SCRIPT" rc="0">
<script>#!bjorne
set -x
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|