|
From: <cr...@us...> - 2009-06-13 11:49:18
|
Revision: 5571
http://jnode.svn.sourceforge.net/jnode/?rev=5571&view=rev
Author: crawley
Date: 2009-06-13 11:49:13 +0000 (Sat, 13 Jun 2009)
Log Message:
-----------
Implement 'unset' 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
Added Paths:
-----------
trunk/shell/src/shell/org/jnode/shell/bjorne/UnsetBuiltin.java
trunk/shell/src/shell/org/jnode/shell/bjorne/VariableNameArgument.java
Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java 2009-06-13 05:09:38 UTC (rev 5570)
+++ trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java 2009-06-13 11:49:13 UTC (rev 5571)
@@ -275,7 +275,7 @@
}
/**
- * Test if the variable is currently set here in this context.
+ * Test if the variable is set in this context.
*
* @param name the name of the variable to be tested
* @return <code>true</code> if the variable is set.
@@ -285,6 +285,17 @@
}
/**
+ * Test if the variable is readonly in this context.
+ *
+ * @param name the name of the variable to be tested
+ * @return <code>true</code> if the variable is set.
+ */
+ boolean isVariableReadonly(String name) {
+ VariableSlot var = variables.get(name);
+ return var != null && var.isReadOnly();
+ }
+
+ /**
* This method implements 'unset NAME'
*
* @param name the name of the variable to be unset
Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java 2009-06-13 05:09:38 UTC (rev 5570)
+++ trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java 2009-06-13 11:49:13 UTC (rev 5571)
@@ -149,6 +149,7 @@
BUILTINS.put("shift", ShiftBuiltin.FACTORY);
BUILTINS.put("source", SourceBuiltin.FACTORY);
BUILTINS.put("unalias", UnaliasBuiltin.FACTORY);
+ BUILTINS.put("unset", UnsetBuiltin.FACTORY);
BUILTINS.put(".", SourceBuiltin.FACTORY);
BUILTINS.put(":", ColonBuiltin.FACTORY);
}
Added: trunk/shell/src/shell/org/jnode/shell/bjorne/UnsetBuiltin.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/bjorne/UnsetBuiltin.java (rev 0)
+++ trunk/shell/src/shell/org/jnode/shell/bjorne/UnsetBuiltin.java 2009-06-13 11:49:13 UTC (rev 5571)
@@ -0,0 +1,71 @@
+/*
+ * $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.syntax.Argument;
+import org.jnode.shell.syntax.ArgumentSyntax;
+import org.jnode.shell.syntax.RepeatSyntax;
+import org.jnode.shell.syntax.SyntaxBundle;
+
+/**
+ * This class implements the 'unset' built-in.
+ *
+ * @author cr...@jn...
+ */
+final class UnsetBuiltin extends BjorneBuiltin {
+ private static final SyntaxBundle SYNTAX =
+ new SyntaxBundle("unset", new RepeatSyntax(new ArgumentSyntax("unset")));
+
+ static final Factory FACTORY = new Factory() {
+ public BjorneBuiltinCommandInfo buildCommandInfo(BjorneContext context) {
+ return new BjorneBuiltinCommandInfo("unset", SYNTAX, new UnsetBuiltin(context), context);
+ }
+ };
+
+ private final VariableNameArgument argVariables;
+
+
+ UnsetBuiltin(BjorneContext context) {
+ super("Export shell variables to the environment");
+ argVariables = new VariableNameArgument(
+ "unset", context, Argument.MANDATORY, "variables to be unset");
+ registerArguments(argVariables);
+ }
+
+ public void execute() throws Exception {
+ int errorCount = 0;
+ if (!argVariables.isSet()) {
+ // FIXME - implement this?
+ } else {
+ BjorneContext pc = getParentContext();
+ for (String var : argVariables.getValues()) {
+ if (pc.isVariableReadonly(var)) {
+ errorCount++;
+ } else {
+ pc.unsetVariable(var);
+ }
+ }
+ }
+ if (errorCount > 0) {
+ exit(1);
+ }
+ }
+}
Added: trunk/shell/src/shell/org/jnode/shell/bjorne/VariableNameArgument.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/bjorne/VariableNameArgument.java (rev 0)
+++ trunk/shell/src/shell/org/jnode/shell/bjorne/VariableNameArgument.java 2009-06-13 11:49:13 UTC (rev 5571)
@@ -0,0 +1,65 @@
+/*
+ * $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.driver.console.CompletionInfo;
+import org.jnode.shell.CommandLine.Token;
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.CommandSyntaxException;
+
+/**
+ * The VariableNameArgument understands 'arguments' of the form <name> where
+ * the name is a Bjorne shell variable name.
+ * It will perform completion of the <name> component against the variable names
+ * defined in a supplied BjorneContext.
+ *
+ * @author cr...@jn...
+ */
+public class VariableNameArgument extends Argument<String> {
+ private final BjorneContext context;
+
+ public VariableNameArgument(String label, BjorneContext context, int flags, String description) {
+ super(label, flags, new String[0], description);
+ this.context = context;
+ }
+
+ @Override
+ protected String argumentKind() {
+ return "bjorne variable name";
+ }
+
+ @Override
+ protected String doAccept(Token value, int flags) throws CommandSyntaxException {
+ String name = value.text;
+ if (!BjorneToken.isName(name)) {
+ throw new CommandSyntaxException("invalid name ('" + name + "')");
+ }
+ return name;
+ }
+
+ @Override
+ public void doComplete(CompletionInfo completions, String partial, int flags) {
+ for (String varName : context.getVariableNames()) {
+ if (varName.startsWith(partial)) {
+ completions.addCompletion(varName, true);
+ }
+ }
+ }
+}
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-06-13 05:09:38 UTC (rev 5570)
+++ trunk/shell/src/test/org/jnode/test/shell/bjorne/bjorne-builtin-tests.xml 2009-06-13 11:49:13 UTC (rev 5571)
@@ -115,7 +115,17 @@
B -1-
B -1-
B -1-
-B -1-
</output>
</testSpec>
+ <testSpec title="unset" runMode="AS_SCRIPT" rc="0">
+ <script>#!bjorne
+ A=1
+ echo A -${A}-
+ unset A
+ echo A -${A}-
+ </script>
+ <output>A -1-
+A --
+</output>
+ </testSpec>
</testSet>
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|