|
From: <cr...@us...> - 2009-03-29 04:20:42
|
Revision: 5174
http://jnode.svn.sourceforge.net/jnode/?rev=5174&view=rev
Author: crawley
Date: 2009-03-29 04:20:40 +0000 (Sun, 29 Mar 2009)
Log Message:
-----------
Bug fix: an empty line in a script would clear $?. Also added a partly
implemented / partly tested version of the 'alias' builtin.
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-builtin-tests.xml
trunk/shell/src/test/org/jnode/test/shell/bjorne/bjorne-shell-tests.xml
Added Paths:
-----------
trunk/shell/src/shell/org/jnode/shell/bjorne/AliasBuiltin.java
Added: trunk/shell/src/shell/org/jnode/shell/bjorne/AliasBuiltin.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/bjorne/AliasBuiltin.java (rev 0)
+++ trunk/shell/src/shell/org/jnode/shell/bjorne/AliasBuiltin.java 2009-03-29 04:20:40 UTC (rev 5174)
@@ -0,0 +1,90 @@
+/*
+ * $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 java.io.PrintStream;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.TreeMap;
+
+import org.jnode.shell.Command;
+import org.jnode.shell.CommandLine;
+import org.jnode.shell.ShellException;
+
+/**
+ * This class implements the 'alias' built-in.
+ *
+ * @author cr...@jn...
+ */
+final class AliasBuiltin extends BjorneBuiltin {
+ @SuppressWarnings("deprecation")
+ public int invoke(CommandLine command, BjorneInterpreter interpreter,
+ BjorneContext context) throws ShellException {
+ Iterator<String> args = command.iterator();
+ context = context.getParent();
+ PrintStream ps = context.resolvePrintStream(context.getIO(Command.STD_ERR));
+ int rc = 0;
+ if (!args.hasNext()) {
+ printAliases(ps, context.getAliases());
+ } else {
+ while (args.hasNext()) {
+ String arg = args.next();
+ int pos = arg.indexOf('=');
+ String aliasName;
+ String alias;
+ if (pos <= 0) {
+ aliasName = arg;
+ alias = null;
+ } else {
+ aliasName = arg.substring(0, pos);
+ alias = arg.substring(pos + 1);
+ }
+ if (alias == null) {
+ alias = context.getAlias(aliasName);
+ if (alias == null) {
+ error("alias: " + aliasName + " not found", context);
+ rc = 1;
+ } else {
+ printAlias(ps, aliasName, alias);
+ }
+ } else {
+ if (!BjorneToken.isName(aliasName)) {
+ error("alias: " + aliasName + ": not a valid alias name", context);
+ }
+ context.defineAlias(aliasName, alias);
+ }
+ }
+ }
+ return rc;
+ }
+
+ private void printAliases(PrintStream ps, TreeMap<String, String> aliases) {
+ for (Map.Entry<String, String> entry : aliases.entrySet()) {
+ printAlias(ps, entry.getKey(), entry.getValue());
+ }
+ }
+
+ private void printAlias(PrintStream ps, String aliasName, String alias) {
+ ps.println(aliasName + "=" + alias);
+ }
+
+
+}
Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java 2009-03-29 03:11:28 UTC (rev 5173)
+++ trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java 2009-03-29 04:20:40 UTC (rev 5174)
@@ -45,6 +45,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
+import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -102,6 +103,8 @@
private final BjorneInterpreter interpreter;
private Map<String, VariableSlot> variables;
+
+ private TreeMap<String, String> aliases;
private String command = "";
@@ -129,6 +132,7 @@
this.interpreter = interpreter;
this.holders = holders;
this.variables = new HashMap<String, VariableSlot>();
+ this.aliases = new TreeMap<String, String>();
}
public BjorneContext(BjorneInterpreter interpreter) {
@@ -299,8 +303,42 @@
var.exported = exported;
}
}
+
+ /**
+ * Get the complete alias map.
+ * @return the alias map
+ */
+ TreeMap<String, String> getAliases() {
+ return aliases;
+ }
+
+ /**
+ * Lookup an alias
+ * @param aliasName the (possible) alias name
+ * @return the alias string or {@code null}
+ */
+ String getAlias(String aliasName) {
+ return aliases.get(aliasName);
+ }
+
+ /**
+ * Define an alias
+ * @param aliasName the alias name
+ * @param alias the alias.
+ */
+ void defineAlias(String aliasName, String alias) {
+ aliases.put(aliasName, alias);
+ }
/**
+ * Undefine an alias
+ * @param aliasName the alias name
+ */
+ void undefineAlias(String aliasName) {
+ aliases.remove(aliasName);
+ }
+
+ /**
* Perform expand-and-split processing on a list of word tokens. The resulting
* wordTokens are assembled into a CommandLine.
*
Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java 2009-03-29 03:11:28 UTC (rev 5173)
+++ trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java 2009-03-29 04:20:40 UTC (rev 5174)
@@ -140,6 +140,7 @@
private static long subshellCount;
static {
+ BUILTINS.put("alias", new AliasBuiltin());
BUILTINS.put("break", new BreakBuiltin());
BUILTINS.put("continue", new ContinueBuiltin());
BUILTINS.put("exit", new ExitBuiltin());
@@ -242,7 +243,7 @@
CommandNode tree = new BjorneParser(tokens, "> ").parse();
if (tree == null) {
// An empty command line
- return 0;
+ return myContext.getLastReturnCode();
}
if (DEBUG) {
System.err.println(tree);
Modified: trunk/shell/src/test/org/jnode/test/shell/bjorne/bjorne-builtin-tests.xml
===================================================================
--- trunk/shell/src/test/org/jnode/test/shell/bjorne/bjorne-builtin-tests.xml 2009-03-29 03:11:28 UTC (rev 5173)
+++ trunk/shell/src/test/org/jnode/test/shell/bjorne/bjorne-builtin-tests.xml 2009-03-29 04:20:40 UTC (rev 5174)
@@ -3,12 +3,25 @@
<plugin id="org.jnode.shell.command.posix"/>
<testSpec title="exit 0" command="test" runMode="AS_SCRIPT" rc="0">
<script>#!bjorne
-exit 0
+ exit 0
</script>
</testSpec>
- <testSpec title="exit 99" command="test" runMode="AS_SCRIPT" rc="99">
+ <testSpec title="exit 99" runMode="AS_SCRIPT" rc="99">
<script>#!bjorne
-exit 99
+ exit 99
</script>
</testSpec>
+ <testSpec title="alias" command="test" runMode="AS_SCRIPT" rc="0">
+ <script>#!bjorne
+ alias > @TEMP_DIR@/1
+ </script>
+ <file name="1" input="false"></file>
+ </testSpec>
+ <testSpec title="alias 2" command="test" runMode="AS_SCRIPT" rc="1">
+ <script>#!bjorne
+ alias fred
+ </script>
+ <error>alias: fred not found
+</error>
+ </testSpec>
</testSet>
\ No newline at end of file
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-03-29 03:11:28 UTC (rev 5173)
+++ trunk/shell/src/test/org/jnode/test/shell/bjorne/bjorne-shell-tests.xml 2009-03-29 04:20:40 UTC (rev 5174)
@@ -22,9 +22,17 @@
echo $?
false
echo $?
+false
+
+echo $?
+false
+# no comment
+echo $?
</script>
<output>0
1
+1
+1
</output>
</testSpec>
<testSpec title="``" command="test" runMode="AS_SCRIPT" rc="0">
@@ -164,7 +172,7 @@
A is 1
</output>
</testSpec>
- <testSpec title="while ... do ... done" command="test" runMode="AS_SCRIPT" rc="0">
+ <testSpec title="while ... do ... done" command="test" runMode="AS_SCRIPT" rc="1">
<script>#!bjorne
A=5
while expr $A != 0 ; do echo A is $A ; A=`expr $A - 1`; done
@@ -182,7 +190,7 @@
0
</output>
</testSpec>
- <testSpec title="while ... do ... done multi-line" command="test" runMode="AS_SCRIPT" rc="0">
+ <testSpec title="while ... do ... done multi-line" command="test" runMode="AS_SCRIPT" rc="1">
<script>#!bjorne
A=5
while expr $A != 0 ;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|