From: <cr...@us...> - 2009-01-04 14:07:59
|
Revision: 4827 http://jnode.svn.sourceforge.net/jnode/?rev=4827&view=rev Author: crawley Date: 2009-01-04 14:07:46 +0000 (Sun, 04 Jan 2009) Log Message: ----------- Bjorne shell command argument completion is now working for simple cases. I'm moving the bjorne shell plugin to the default plugin list to encourage testers to try it out. Modified Paths: -------------- trunk/all/conf/default-plugin-list.xml trunk/all/conf/full-plugin-list.xml trunk/shell/src/shell/org/jnode/shell/ArgumentCompleter.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/bjorne/BjorneToken.java Modified: trunk/all/conf/default-plugin-list.xml =================================================================== --- trunk/all/conf/default-plugin-list.xml 2009-01-04 12:43:05 UTC (rev 4826) +++ trunk/all/conf/default-plugin-list.xml 2009-01-04 14:07:46 UTC (rev 4827) @@ -156,6 +156,7 @@ <plugin id="org.acplt.oncrpc"/> <plugin id="org.jnode.shell"/> + <plugin id="org.jnode.shell.bjorne"/> <plugin id="org.jnode.shell.help"/> <plugin id="org.jnode.shell.syntax"/> <plugin id="org.jnode.shell.command"/> Modified: trunk/all/conf/full-plugin-list.xml =================================================================== --- trunk/all/conf/full-plugin-list.xml 2009-01-04 12:43:05 UTC (rev 4826) +++ trunk/all/conf/full-plugin-list.xml 2009-01-04 14:07:46 UTC (rev 4827) @@ -31,7 +31,6 @@ <plugin id="thinlet"/> - <plugin id="org.jnode.shell.bjorne"/> <plugin id="org.jnode.shell.command.unix"/> <plugin id="org.jnode.apps.jpartition"/> Modified: trunk/shell/src/shell/org/jnode/shell/ArgumentCompleter.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/ArgumentCompleter.java 2009-01-04 12:43:05 UTC (rev 4826) +++ trunk/shell/src/shell/org/jnode/shell/ArgumentCompleter.java 2009-01-04 14:07:46 UTC (rev 4827) @@ -25,7 +25,7 @@ import org.jnode.shell.syntax.Argument; /** - * This class wraps an old-style Argument as a Completable for use in a default syntax. + * This class wraps an Argument as a Completable for use in a default syntax. * * @author cr...@jn... */ Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java 2009-01-04 12:43:05 UTC (rev 4826) +++ trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java 2009-01-04 14:07:46 UTC (rev 4827) @@ -199,7 +199,7 @@ } /** - * Crreat a copy of a context with the same initial variable bindings and + * Create a copy of a context with the same initial variable bindings and * streams. Stream ownership is not transferred. * * @param parent the context that gives us our initial state. @@ -305,111 +305,123 @@ } /** - * Perform expand-and-split processing on an array of word/name tokens. + * Perform expand-and-split processing on an array of word tokens. The resulting + * wordTokens are assembled into a CommandLine. * * @param tokens the tokens to be expanded and split into words - * @return the resulting words + * @return the command line * @throws ShellException */ public CommandLine expandAndSplit(BjorneToken[] tokens) throws ShellException { - LinkedList<String> words = new LinkedList<String>(); + LinkedList<BjorneToken> wordTokens = new LinkedList<BjorneToken>(); for (BjorneToken token : tokens) { - splitAndAppend(expand(token.getText()), words); + splitAndAppend(token, wordTokens); } - return makeCommandLine(words); + return makeCommandLine(wordTokens); } /** - * Perform expand-and-split processing on sequence of characters + * Perform expand-and-split processing on a sequence of characters. The resulting + * wordTokens are assembled into a CommandLine. This method is only used in tests + * at the moment, and probably should be removed. (It does not set token attributes + * properly ...) * * @param text the characters to be split - * @return the resulting words + * @return the command line * @throws ShellException */ public CommandLine expandAndSplit(CharSequence text) throws ShellException { - LinkedList<String> words = split(expand(text)); + LinkedList<BjorneToken> words = split(expand(text)); return makeCommandLine(words); } - private CommandLine makeCommandLine(LinkedList<String> words) { + private CommandLine makeCommandLine(LinkedList<BjorneToken> wordTokens) { if (globbing || tildes) { - LinkedList<String> globbedWords = new LinkedList<String>(); - for (String word : words) { + LinkedList<BjorneToken> globbedWordTokens = new LinkedList<BjorneToken>(); + for (BjorneToken wordToken : wordTokens) { if (tildes) { - word = tildeExpand(word); + wordToken = tildeExpand(wordToken); } if (globbing) { - globAndAppend(word, globbedWords); + globAndAppend(wordToken, globbedWordTokens); } else { - globbedWords.add(word); + globbedWordTokens.add(wordToken); } } - words = globbedWords; + wordTokens = globbedWordTokens; } - int nosWords = words.size(); + int nosWords = wordTokens.size(); if (nosWords == 0) { return new CommandLine(null, null); - } else if (nosWords == 1) { - return new CommandLine(words.get(0), null); } else { - String alias = words.removeFirst(); - String[] args = words.toArray(new String[nosWords - 1]); - return new CommandLine(alias, args); + BjorneToken alias = wordTokens.removeFirst(); + BjorneToken[] args = wordTokens.toArray(new BjorneToken[nosWords - 1]); + return new CommandLine(alias, args, null); } } - private String tildeExpand(String word) { + private BjorneToken tildeExpand(BjorneToken wordToken) { + String word = wordToken.getText(); if (word.startsWith("~")) { int slashPos = word.indexOf(File.separatorChar); String name = (slashPos >= 0) ? word.substring(1, slashPos) : ""; - // FIXME ... support "~username" when we have kind of user info / management. + // FIXME ... support "~username" when we have some kind of user info / management. String home = (name.length() == 0) ? System.getProperty("user.home", "") : ""; if (home.length() == 0) { - return word; - } else if (slashPos == -1) { - return home; - } else { - return home + word.substring(slashPos); - } + return wordToken; + } + String expansion = (slashPos == -1) ? + home : (home + word.substring(slashPos)); + return wordToken.remake(expansion); } else { - return word; + return wordToken; } } - private void globAndAppend(String word, LinkedList<String> globbedWords) { + private void globAndAppend(BjorneToken wordToken, LinkedList<BjorneToken> globbedWordTokens) { // Try to deal with the 'not-a-pattern' case quickly and cheaply. + String word = wordToken.getText(); if (!PathnamePattern.isPattern(word)) { - globbedWords.add(word); + globbedWordTokens.add(wordToken); return; } PathnamePattern pattern = PathnamePattern.compile(word); LinkedList<String> paths = pattern.expand(new File(".")); // If it doesn't match anything, a pattern 'expands' to itself. if (paths.isEmpty()) { - globbedWords.add(word); + globbedWordTokens.add(wordToken); } else { - globbedWords.addAll(paths); + for (String path : paths) { + globbedWordTokens.add(wordToken.remake(path)); + } } } /** - * Split a character sequence into words, dealing with and removing any - * non-literal quotes. + * Split a character sequence into word tokens, dealing with and removing any + * non-literal * * @param text the characters to be split - * @return the resulting list of words. + * @return the destination for the tokens. * @throws ShellException */ - public LinkedList<String> split(CharSequence text) throws ShellException { - LinkedList<String> words = new LinkedList<String>(); - splitAndAppend(text, words); - return words; + public LinkedList<BjorneToken> split(CharSequence text) throws ShellException { + LinkedList<BjorneToken> wordTokens = new LinkedList<BjorneToken>(); + splitAndAppend(new BjorneToken(-1, text.toString(), -1, -1), wordTokens); + return wordTokens; } /** - * This method does the work of 'split'; see above. + * Split a token into a series of word tokens, dealing with and removing any + * non-literal quotes. The resulting tokens are appended to a supplied list. + * + * @param token the token to be split + * @param wordTokens the destination for the tokens. + * @throws ShellException */ - private void splitAndAppend(CharSequence text, LinkedList<String> words) throws ShellException { + private void splitAndAppend(BjorneToken token, LinkedList<BjorneToken> wordTokens) + throws ShellException { + String text = token.getText(); StringBuffer sb = null; int len = text.length(); int quote = 0; @@ -433,7 +445,7 @@ case '\t': if (quote == 0) { if (sb != null) { - words.add(sb.toString()); + wordTokens.add(token.remake(sb.toString())); sb = null; } } else { @@ -452,7 +464,7 @@ } } if (sb != null) { - words.add(sb.toString()); + wordTokens.add(token.remake(sb.toString())); } } Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java 2009-01-04 12:43:05 UTC (rev 4826) +++ trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneInterpreter.java 2009-01-04 14:07:46 UTC (rev 4827) @@ -51,10 +51,10 @@ import org.jnode.shell.syntax.CommandSyntaxException; /** - * This is a Java implementation of the Bourne Shell language. + * This is the JNode implementation of the Bourne Shell language. The long term + * goal is to faithfully implement the POSIX Shell specification. * * @author cr...@jn... - * */ public class BjorneInterpreter implements CommandInterpreter { @@ -141,6 +141,7 @@ private BjorneContext context; public BjorneInterpreter() { + this.context = new BjorneContext(this); } @Override @@ -155,15 +156,15 @@ @Override public Completable parsePartial(CommandShell shell, String partial) throws ShellSyntaxException { - init(shell); + bindShell(shell); BjorneTokenizer tokens = new BjorneTokenizer(partial); final CommandNode tree = new BjorneParser(tokens).parse(); if (tree instanceof BjorneCompletable) { return new Completable() { @Override - public void complete(CompletionInfo completion, + public void complete(CompletionInfo completions, CommandShell shell) throws CompletionException { - ((BjorneCompletable) tree).complete(completion, context, shell); + ((BjorneCompletable) tree).complete(completions, context, shell); } }; @@ -187,8 +188,10 @@ int interpret(CommandShell shell, String command, OutputStream capture, boolean source) throws ShellException { BjorneContext myContext; + // FIXME ... I think there is something wrong / incomplete with the way I'm handling + // the contexts here ... if (capture == null) { - init(shell); + bindShell(shell); myContext = this.context; } else { myContext = new BjorneContext(this); @@ -215,13 +218,12 @@ } } - private void init(CommandShell shell) { + private void bindShell(CommandShell shell) { if (this.shell != shell) { if (this.shell != null) { throw new ShellFailureException("my shell changed"); } this.shell = shell; - this.context = new BjorneContext(this); } } Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneToken.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneToken.java 2009-01-04 12:43:05 UTC (rev 4826) +++ trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneToken.java 2009-01-04 14:07:46 UTC (rev 4827) @@ -129,6 +129,10 @@ super(text == null ? "" : text, tokenType, start, end); validate(); } + + public BjorneToken remake(String newText) { + return new BjorneToken(this.tokenType, newText, this.start, this.end); + } private void validate() { switch (tokenType) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |