|
From: <cr...@us...> - 2009-03-29 13:08:23
|
Revision: 5180
http://jnode.svn.sourceforge.net/jnode/?rev=5180&view=rev
Author: crawley
Date: 2009-03-29 13:08:19 +0000 (Sun, 29 Mar 2009)
Log Message:
-----------
Bjorne shell aliases should now work
Modified Paths:
--------------
trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java
trunk/shell/src/shell/org/jnode/shell/bjorne/SimpleCommandNode.java
trunk/shell/src/test/org/jnode/test/shell/bjorne/bjorne-builtin-tests.xml
Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java 2009-03-29 10:53:16 UTC (rev 5179)
+++ trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java 2009-03-29 13:08:19 UTC (rev 5180)
@@ -52,6 +52,7 @@
import org.jnode.shell.Command;
import org.jnode.shell.CommandLine;
import org.jnode.shell.CommandShell;
+import org.jnode.shell.IncompleteCommandException;
import org.jnode.shell.PathnamePattern;
import org.jnode.shell.ShellException;
import org.jnode.shell.ShellFailureException;
@@ -159,6 +160,7 @@
this.interpreter = parent.interpreter;
this.holders = copyStreamHolders(parent.holders);
this.variables = copyVariables(parent.variables);
+ this.aliases = new TreeMap<String, String>(parent.aliases);
this.globbing = parent.globbing;
this.tildeExpansion = parent.tildeExpansion;
this.echoExpansions = parent.echoExpansions;
@@ -1238,4 +1240,39 @@
return interpreter.getUniqueName();
}
+ public BjorneToken[] substituteAliases(BjorneToken[] words)
+ throws IncompleteCommandException {
+ String alias = aliases.get(words[0].getText());
+ if (alias == null) {
+ return words;
+ }
+ List<BjorneToken> list = new LinkedList<BjorneToken>(Arrays.asList(words));
+ substituteAliases(list, 0, 0);
+ return list.toArray(new BjorneToken[list.size()]);
+ }
+
+ private void substituteAliases(List<BjorneToken> list, int pos, int depth)
+ throws IncompleteCommandException {
+ if (depth > 10) {
+ throw new ShellFailureException("probable cycle detected in alias expansion");
+ }
+ String aliasName = list.get(pos).getText();
+ String alias = aliases.get(aliasName);
+ if (alias == null) {
+ return;
+ }
+ BjorneTokenizer tokens = new BjorneTokenizer(alias);
+ list.remove(pos);
+ int i = 0;
+ while (tokens.hasNext()) {
+ list.add(pos + i, tokens.next());
+ if (i == 0 && !aliasName.equals(list.get(pos + i).getText())) {
+ substituteAliases(list, pos + i, depth + 1);
+ }
+ i++;
+ }
+ if (alias.endsWith(" ") && pos + i < list.size()) {
+ substituteAliases(list, pos + i, depth + 1);
+ }
+ }
}
Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/SimpleCommandNode.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/bjorne/SimpleCommandNode.java 2009-03-29 10:53:16 UTC (rev 5179)
+++ trunk/shell/src/shell/org/jnode/shell/bjorne/SimpleCommandNode.java 2009-03-29 13:08:19 UTC (rev 5180)
@@ -83,6 +83,9 @@
childContext.evaluateRedirections(getRedirects());
rc = 0;
} else {
+ // FIXME ... strictly speaking, alias substitution should be performed
+ // before "applying the grammatical rules" (i.e. parsing).
+ words = context.substituteAliases(words);
CommandLine command = context.expandAndSplit(words);
// Assignments and redirections are done in the command's context
BjorneContext childContext = new BjorneContext(context);
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 10:53:16 UTC (rev 5179)
+++ trunk/shell/src/test/org/jnode/test/shell/bjorne/bjorne-builtin-tests.xml 2009-03-29 13:08:19 UTC (rev 5180)
@@ -38,6 +38,30 @@
jim='echo hi'
</output>
</testSpec>
+ <testSpec title="alias 4" command="test" runMode="AS_SCRIPT" rc="0">
+ <script>#!bjorne
+ alias fred="echo hi"
+ fred
+ fred ho
+ fred fred
+ </script>
+ <output>hi
+hi ho
+hi fred
+</output>
+ </testSpec>
+ <testSpec title="alias 5" command="test" runMode="AS_SCRIPT" rc="0">
+ <script>#!bjorne
+ alias fred="echo hi "
+ fred
+ fred ho
+ fred fred
+ </script>
+ <output>hi
+hi ho
+hi echo hi
+</output>
+ </testSpec>
<testSpec title="unalias" command="test" runMode="AS_SCRIPT" rc="0">
<script>#!bjorne
alias fred=ls
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|