|
From: <cr...@us...> - 2009-02-08 04:04:09
|
Revision: 5003
http://jnode.svn.sourceforge.net/jnode/?rev=5003&view=rev
Author: crawley
Date: 2009-02-08 04:04:06 +0000 (Sun, 08 Feb 2009)
Log Message:
-----------
Implemented script arguments (via RunCommand, etc) and a first cut for
$* and $@.
Modified Paths:
--------------
trunk/shell/src/shell/org/jnode/shell/CommandInterpreter.java
trunk/shell/src/shell/org/jnode/shell/CommandShell.java
trunk/shell/src/shell/org/jnode/shell/DefaultInterpreter.java
trunk/shell/src/shell/org/jnode/shell/Shell.java
trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java
trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java
trunk/shell/src/shell/org/jnode/shell/command/RunCommand.java
trunk/shell/src/test/org/jnode/test/shell/bjorne/bjorne-shell-tests.xml
trunk/shell/src/test/org/jnode/test/shell/harness/ScriptTestRunner.java
Modified: trunk/shell/src/shell/org/jnode/shell/CommandInterpreter.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/CommandInterpreter.java 2009-02-07 09:59:22 UTC (rev 5002)
+++ trunk/shell/src/shell/org/jnode/shell/CommandInterpreter.java 2009-02-08 04:04:06 UTC (rev 5003)
@@ -56,10 +56,13 @@
* @param shell the CommandShell that provides low-level command invocation,
* command history and so on.
* @param file the file to be interpreted
+ * @param alias this will supply the script's notional command name to the interpreter.
+ * @param args command line arguments to be passed to the script. If this parameter
+ * is {@code null}, no arguments are passed.
* @return the return code.
* @throws ShellException
*/
- int interpret(CommandShell shell, File file) throws ShellException;
+ int interpret(CommandShell shell, File file, String alias, String[] args) throws ShellException;
/**
* Parse and execute a command file, returning the resulting return code.
@@ -67,10 +70,13 @@
* @param shell the CommandShell that provides low-level command invocation,
* command history and so on.
* @param reader the reader to be interpreted. <b>The implementation must close it.</b>
+ * @param alias this will supply the script's notional command name to the interpreter.
+ * @param args command line arguments to be passed to the script. If this parameter
+ * is {@code null}, no arguments are passed.
* @return the return code.
* @throws ShellException
*/
- int interpret(CommandShell shell, Reader reader) throws ShellException;
+ int interpret(CommandShell shell, Reader reader, String alias, String[] args) throws ShellException;
/**
* Parse a partial command line, returning the command line fragment to be
Modified: trunk/shell/src/shell/org/jnode/shell/CommandShell.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/CommandShell.java 2009-02-07 09:59:22 UTC (rev 5002)
+++ trunk/shell/src/shell/org/jnode/shell/CommandShell.java 2009-02-08 04:04:06 UTC (rev 5003)
@@ -296,9 +296,9 @@
final File jnode_ini = new File(java_home + '/' + name);
try {
if (jnode_ini.exists()) {
- runCommandFile(jnode_ini);
+ runCommandFile(jnode_ini, null, null);
} else if (getClass().getResource(name) != null) {
- runCommandResource(name);
+ runCommandResource(name, null);
}
} catch (ShellException ex) {
errPW.println("Error while processing " + jnode_ini + ": " + ex.getMessage());
@@ -316,9 +316,9 @@
final File shell_ini = new File(user_home + '/' + name);
try {
if (shell_ini.exists()) {
- runCommandFile(shell_ini);
+ runCommandFile(shell_ini, null, null);
} else if (getClass().getResource(name) != null) {
- runCommandResource(name);
+ runCommandResource(name, null);
}
} catch (ShellException ex) {
errPW.println("Error while processing " + shell_ini + ": " + ex.getMessage());
@@ -847,12 +847,15 @@
return ShellUtils.createInvoker("default", this);
}
- public int runCommandFile(File file) throws ShellException {
+ public int runCommandFile(File file, String alias, String[] args) throws ShellException {
// FIXME extend to allow arguments to be passed to the script.
boolean enabled = setHistoryEnabled(false);
try {
CommandInterpreter interpreter = createInterpreter(new FileReader(file));
- return interpreter.interpret(this, file);
+ if (alias == null) {
+ alias = file.getAbsolutePath();
+ }
+ return interpreter.interpret(this, file, alias, args);
} catch (IOException ex) {
throw new ShellException("Cannot open command file: " + ex.getMessage(), ex);
} finally {
@@ -860,7 +863,7 @@
}
}
- public int runCommandResource(String resource) throws ShellException {
+ public int runCommandResource(String resource, String[] args) throws ShellException {
boolean enabled = setHistoryEnabled(false);
try {
int result;
@@ -871,7 +874,7 @@
} else {
CommandInterpreter interpreter = createInterpreter(new InputStreamReader(input));
Reader reader = new InputStreamReader(getClass().getResourceAsStream(resource));
- result = interpreter.interpret(this, reader);
+ result = interpreter.interpret(this, reader, resource, args);
}
return result;
} finally {
Modified: trunk/shell/src/shell/org/jnode/shell/DefaultInterpreter.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/DefaultInterpreter.java 2009-02-07 09:59:22 UTC (rev 5002)
+++ trunk/shell/src/shell/org/jnode/shell/DefaultInterpreter.java 2009-02-08 04:04:06 UTC (rev 5003)
@@ -107,12 +107,22 @@
throw new ShellException("Command arguments don't match syntax", ex);
}
}
-
+
+ /**
+ * {@inheritDoc}
+ *
+ * The default interpreter and its subtypes treat a command script as a sequence of commands.
+ * Commands are expected to consist of exactly one line. Any line whose first non-whitespace
+ * character is '#' will be ignored. Command line arguments from the script are not supported,
+ * and will result in a {@link ShellException} being thrown.
+ */
@Override
- public int interpret(CommandShell shell, Reader reader) throws ShellException {
- // The default interpreter and subtypes process the command file one line at a time,
- // ignoring any line whose first non-whitespace character is '#'. There is no notion
- // of multi-line commands.
+ public int interpret(CommandShell shell, Reader reader, String alias, String[] args)
+ throws ShellException {
+ if (args != null && args.length > 0) {
+ throw new ShellException(
+ "The " + getName() + " interpreter does not support script file arguments");
+ }
try {
BufferedReader br = new BufferedReader(reader);
String line;
@@ -143,10 +153,19 @@
return false;
}
+ /**
+ * {@inheritDoc}
+ *
+ * The default interpreter and its subtypes treat a command script as a sequence of commands.
+ * Commands are expected to consist of exactly one line. Any line whose first non-whitespace
+ * character is '#' will be ignored. Command line arguments from the script are not supported,
+ * and will result in a {@link ShellException} being thrown.
+ */
@Override
- public int interpret(CommandShell shell, File file) throws ShellException {
+ public int interpret(CommandShell shell, File file, String alias, String[] args)
+ throws ShellException {
try {
- return interpret(shell, new FileReader(file));
+ return interpret(shell, new FileReader(file), alias, args);
} catch (FileNotFoundException ex) {
throw new ShellException("Problem reading command file: " + ex.getMessage(), ex);
}
Modified: trunk/shell/src/shell/org/jnode/shell/Shell.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/Shell.java 2009-02-07 09:59:22 UTC (rev 5002)
+++ trunk/shell/src/shell/org/jnode/shell/Shell.java 2009-02-08 04:04:06 UTC (rev 5003)
@@ -70,9 +70,13 @@
* the shell gets to decide which interpreter to use.
*
* @param file the command file
+ * @param alias the command alias used to launch the script. If this parameter is
+ * {@code null}, the command file name will be used.
+ * @param args command line arguments to be passed to the script. If this parameter
+ * is {@code null}, no arguments are passed.
* @throws ShellException
*/
- public int runCommandFile(File file) throws ShellException;
+ public int runCommandFile(File file, String alias, String[] args) throws ShellException;
/**
* Resolve a command name to a CommandInfo object.
Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java 2009-02-07 09:59:22 UTC (rev 5002)
+++ trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java 2009-02-08 04:04:06 UTC (rev 5003)
@@ -39,6 +39,7 @@
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
@@ -239,6 +240,14 @@
return copyStreamHolders(holders);
}
+ void setArgs(String[] args) {
+ this.args = Arrays.asList(args.clone());
+ }
+
+ void setCommand(String command) {
+ this.command = command;
+ }
+
/**
* This method implements 'NAME=VALUE'. If variable NAME does not exist, it
* is created as an unexported shell variable.
@@ -262,7 +271,7 @@
* @param name the name of the variable to be tested
* @return <code>true</code> if the variable is set.
*/
- public boolean isVariableSet(String name) {
+ boolean isVariableSet(String name) {
return variables.get(name) != null;
}
@@ -271,7 +280,7 @@
*
* @param name the name of the variable to be unset
*/
- public void unsetVariableValue(String name) {
+ void unsetVariableValue(String name) {
variables.remove(name);
}
@@ -280,7 +289,7 @@
*
* @param name the name of the variable to be exported / unexported
*/
- public void setExported(String name, boolean exported) {
+ void setExported(String name, boolean exported) {
VariableSlot var = variables.get(name);
if (var == null) {
if (exported) {
@@ -854,8 +863,9 @@
case '#':
return Integer.toString(args.size());
case '@':
+ return concatenateArgs(false);
case '*':
- throw new ShellFailureException("not implemented");
+ return concatenateArgs(false);
case '?':
return Integer.toString(lastReturnCode);
case '!':
@@ -872,17 +882,30 @@
case '7':
case '8':
case '9':
- return argVariable(ch - '0');
+ return argVariable(ch);
default:
return null;
}
}
- private String argVariable(int argNo) {
+ private String concatenateArgs(boolean isStar) {
+ // FIXME - implement $@ versus $* differences; i.e. quoting and $IFS behavior.
+ StringBuilder sb = new StringBuilder();
+ for (String arg : args) {
+ if (sb.length() > 0) {
+ sb.append(' ');
+ }
+ sb.append(arg);
+ }
+ return sb.toString();
+ }
+
+ private String argVariable(int argChar) {
+ int argNo = argChar - '0';
if (argNo == 0) {
return command;
} else if (argNo <= args.size()) {
- return args.get(argNo);
+ return args.get(argNo - 1);
} else {
return "";
}
Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java 2009-02-07 09:59:22 UTC (rev 5002)
+++ trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java 2009-02-08 04:04:06 UTC (rev 5003)
@@ -250,8 +250,10 @@
}
@Override
- public int interpret(CommandShell shell, Reader reader) throws ShellException {
- // FIXME ... update this to support multi-line commands.
+ public int interpret(CommandShell shell, Reader reader, String alias, String[] args)
+ throws ShellException {
+ context.setCommand(alias == null ? "" : alias);
+ context.setArgs(args == null ? new String[0] : args);
try {
BufferedReader br = new BufferedReader(reader);
String line;
@@ -301,9 +303,9 @@
}
@Override
- public int interpret(CommandShell shell, File file) throws ShellException {
+ public int interpret(CommandShell shell, File file, String alias, String[] args) throws ShellException {
try {
- return interpret(shell, new FileReader(file));
+ return interpret(shell, new FileReader(file), alias, args);
} catch (FileNotFoundException ex) {
throw new ShellException("Problem reading command file: " + ex.getMessage(), ex);
}
Modified: trunk/shell/src/shell/org/jnode/shell/command/RunCommand.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/command/RunCommand.java 2009-02-07 09:59:22 UTC (rev 5002)
+++ trunk/shell/src/shell/org/jnode/shell/command/RunCommand.java 2009-02-08 04:04:06 UTC (rev 5003)
@@ -30,6 +30,7 @@
import org.jnode.shell.ShellUtils;
import org.jnode.shell.syntax.Argument;
import org.jnode.shell.syntax.FileArgument;
+import org.jnode.shell.syntax.StringArgument;
/**
* Load and execute a command file.
@@ -39,11 +40,14 @@
*/
public class RunCommand extends AbstractCommand {
- private final FileArgument ARG_FILE = new FileArgument("file", Argument.MANDATORY, "The command file name");
+ private final FileArgument ARG_FILE =
+ new FileArgument("file", Argument.MANDATORY, "The command script file name");
+ private final StringArgument ARG_ARGS =
+ new StringArgument("args", Argument.OPTIONAL | Argument.MULTIPLE, "Arguments passed to the script");
public RunCommand() {
super("Run a command file");
- registerArguments(ARG_FILE);
+ registerArguments(ARG_FILE, ARG_ARGS);
}
public static void main(String[] args) throws Exception {
@@ -67,8 +71,10 @@
err.println("Shell is null.");
exit(2);
}
+
+ String[] args = ARG_ARGS.getValues();
- int rc = shell.runCommandFile(file);
+ int rc = shell.runCommandFile(file, null, args);
if (rc != 0) {
exit(rc);
}
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-07 09:59:22 UTC (rev 5002)
+++ trunk/shell/src/test/org/jnode/test/shell/bjorne/bjorne-shell-tests.xml 2009-02-08 04:04:06 UTC (rev 5003)
@@ -472,4 +472,23 @@
B % *t is caaa
</output>
</testSpec>
+ <testSpec title="script arguments" command="test" runMode="AS_SCRIPT" rc="0">
+ <script>#!bjorne
+ echo $0
+ echo $1
+ echo $2
+ echo $3
+ echo $#
+ echo $*
+ </script>
+ <arg>arg1</arg>
+ <arg>arg2</arg>
+ <output>test
+arg1
+arg2
+
+2
+arg1 arg2
+</output>
+ </testSpec>
</testSet>
\ No newline at end of file
Modified: trunk/shell/src/test/org/jnode/test/shell/harness/ScriptTestRunner.java
===================================================================
--- trunk/shell/src/test/org/jnode/test/shell/harness/ScriptTestRunner.java 2009-02-07 09:59:22 UTC (rev 5002)
+++ trunk/shell/src/test/org/jnode/test/shell/harness/ScriptTestRunner.java 2009-02-08 04:04:06 UTC (rev 5003)
@@ -41,8 +41,6 @@
@Override
public int run() throws Exception {
-// String[] args = spec.getArgs().toArray(new String[0]);
-// CommandLine cmdLine = new CommandLine(spec.getCommand(), args);
tempScriptFile = new File(System.getProperty("java.io.tmpdir"), spec.getCommand());
Writer w = null;
try {
@@ -52,7 +50,8 @@
} finally {
w.close();
}
- int rc = getShell().runCommandFile(tempScriptFile);
+ int rc = getShell().runCommandFile(tempScriptFile,
+ spec.getCommand(), spec.getArgs().toArray(new String[0]));
return check(rc) ? 0 : 1;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|