From: <ls...@us...> - 2007-02-10 19:23:33
|
Revision: 3113 http://jnode.svn.sourceforge.net/jnode/?rev=3113&view=rev Author: lsantha Date: 2007-02-10 11:23:31 -0800 (Sat, 10 Feb 2007) Log Message: ----------- Fixed bsh command and updated beanshell to 2.0b4. Modified Paths: -------------- trunk/shell/descriptors/org.jnode.shell.command.bsh.xml Added Paths: ----------- trunk/shell/lib/bsh-2.0b4.jar trunk/shell/src/shell/org/jnode/shell/command/bsh/BshCommand.java Removed Paths: ------------- trunk/shell/src/shell/org/jnode/shell/command/bsh/BeanshellCommand.java Modified: trunk/shell/descriptors/org.jnode.shell.command.bsh.xml =================================================================== --- trunk/shell/descriptors/org.jnode.shell.command.bsh.xml 2007-02-04 20:28:05 UTC (rev 3112) +++ trunk/shell/descriptors/org.jnode.shell.command.bsh.xml 2007-02-10 19:23:31 UTC (rev 3113) @@ -19,7 +19,7 @@ </runtime> <extension point="org.jnode.shell.aliases"> - <alias name="bsh" class="org.jnode.shell.command.bsh.BeanshellCommand"/> + <alias name="bsh" class="org.jnode.shell.command.bsh.BshCommand"/> </extension> <extension point="org.jnode.security.permissions"> Added: trunk/shell/lib/bsh-2.0b4.jar =================================================================== (Binary files differ) Property changes on: trunk/shell/lib/bsh-2.0b4.jar ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Deleted: trunk/shell/src/shell/org/jnode/shell/command/bsh/BeanshellCommand.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/command/bsh/BeanshellCommand.java 2007-02-04 20:28:05 UTC (rev 3112) +++ trunk/shell/src/shell/org/jnode/shell/command/bsh/BeanshellCommand.java 2007-02-10 19:23:31 UTC (rev 3113) @@ -1,235 +0,0 @@ -/* - * $Id$ - * - * JNode.org - * Copyright (C) 2003-2006 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.command.bsh; - -import gnu.java.io.NullOutputStream; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.PrintStream; -import java.io.Reader; -import java.io.StringReader; - -import bsh.ConsoleInterface; -import bsh.EvalError; -import bsh.Interpreter; - -/** - * User: Sam Reid Date: Jan 1, 2004 Time: 10:40:34 AM Copyright (c) Jan 1, 2004 - * by Sam Reid - */ -public class BeanshellCommand { - - private static Interpreter interpreter; - /*static { - //ConsoleInterface ci=new BshConsole(); - }*/ - - static class BshPrintStream extends PrintStream { - - public BshPrintStream() { - super(new NullOutputStream()); - } - - public void print(boolean b) { - // System.out.print - } - - public void print(char c) { - super.print(c); - } - - public void print(int i) { - super.print(i); - } - - public void print(long l) { - super.print(l); - } - - public void print(float f) { - super.print(f); - } - - public void print(double d) { - super.print(d); - } - - public void print(char[] s) { - super.print(s); - } - - public void print(String s) { - super.print(s); - } - - public void print(Object obj) { - super.print(obj); - } - - public void println() { - super.println(); - } - - public void println(boolean b) { - super.println(b); - } - - public void println(char c) { - super.println(c); - } - - public void println(int i) { - super.println(i); - } - - public void println(long l) { - super.println(l); - } - - public void println(float f) { - super.println(f); - } - - public void println(double d) { - super.println(d); - } - - public void println(char[] s) { - super.println(s); - } - - public void println(String s) { - super.println(s); - } - - public void println(Object obj) { - super.println(obj); - } - } - - static class BshConsole implements ConsoleInterface { - - StringReader in = new StringReader("234"); - - public Reader getIn() { - return in;//dummy - } - - public PrintStream getOut() { - return null; - } - - public PrintStream getErr() { - return null; - } - - public void println(Object o) { - } - - public void print(Object o) { - } - - public void error(Object o) { - } - - } - - /** - * Normal usage will be BeanshellCommand filename.bsh But you could also - * inline java code like this: BeanshellCommand -code "new - * JFrame().setVisible(true)"; <p/>BeanshellCommand should start an - * interactive session with beanshell. BeanshellCommand file.bsh should - * interpret and run the script. BeanshellCommand -code "code" should - * interpret and run the code. - */ - public static void showHelp() { - System.err - .println("Usage: \nbsh: interactive beanshell.\n" - + "bsh -help: this usage menu\n" - + "bsh -code CODE: evaluate the following shell code.\n" - + "bsh FILE: run the beanshell interpreter on the source FILE."); - } - - private static void evaluateFile(String arg) { - try { - System.err.println("Evaluating beanshell source=" + arg); - new Interpreter().source(arg); - } catch (IOException e) { - e.printStackTrace(); //To change body of catch statement use - // Options | File Templates. - } catch (EvalError evalError) { - evalError.printStackTrace(); //To change body of catch statement - // use Options | File Templates. - } - } - - public static void main(String[] args) { - // System.out.println("Started beanshell command."); - if (args.length == 0) { - //run interactive shell - // System.err.println("Interactive shell not yet written."); - runInteractiveShell(); - return; - } else if (args.length == 1 && args[ 0].toLowerCase().equals("-help")) { - System.err - .println("Usage: bsh: interactive beanshell. <not yet working>\n" - + "bsh FILE: run the beanshell interpreter on the source FILE. <not yet written>\n" - + "bsh -help: this usage menu\n" - + "bsh -code CODE: evaluate the following shell code."); - } else if (args.length == 1) { - evaluateFile(args[ 0]); - } else if (args.length == 2 && args[ 0].toLowerCase().equals("-code")) { - String sourcecode = args[ 1]; - evaluateSourceString(sourcecode); - } else if (args.length == 1 && args[ 0].toLowerCase().equals("-charva")) { - // runCharvaShell(); - } else { - showHelp(); - } - } - - private static void runInteractiveShell() { - System.err.println("Starting Interactive beanshell."); - Reader reader = new InputStreamReader(System.in); - Reader r2 = new BufferedReader(reader); - PrintStream out = new PrintStream(System.out); - PrintStream err = new PrintStream(System.err); - - Interpreter interpreter = new Interpreter(r2, out, err, true); - interpreter.run(); - System.err.println("Finished Interactive beanshell."); - } - - private static void evaluateSourceString(String sourcecode) { - System.out.println("Evaluating source string=" + sourcecode); - try { - Object out = interpreter.eval(sourcecode); - System.out.println(out); - } catch (EvalError evalError) { - evalError.printStackTrace(); //To change body of catch statement - // use Options | File Templates. - } - } - -} Copied: trunk/shell/src/shell/org/jnode/shell/command/bsh/BshCommand.java (from rev 3112, trunk/shell/src/shell/org/jnode/shell/command/bsh/BeanshellCommand.java) =================================================================== --- trunk/shell/src/shell/org/jnode/shell/command/bsh/BshCommand.java (rev 0) +++ trunk/shell/src/shell/org/jnode/shell/command/bsh/BshCommand.java 2007-02-10 19:23:31 UTC (rev 3113) @@ -0,0 +1,116 @@ +/* + * $Id$ + * + * JNode.org + * Copyright (C) 2003-2006 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.command.bsh; + +import java.io.*; + +import bsh.Interpreter; +import org.jnode.shell.help.*; +import org.jnode.shell.help.argument.StringArgument; +import org.jnode.shell.help.argument.FileArgument; +import org.jnode.shell.help.argument.OptionArgument; +import org.jnode.shell.CommandLine; + +/** + * @author Levente S\u00e1ntha + */ +public class BshCommand { + + private static final StringArgument ARG_CODE = new StringArgument("code", "a BeanShell code snippet to execute"); + static final OptionArgument ARG_C = new OptionArgument("-c", "execute code", + new OptionArgument.Option("-c", "execute code")); + private static final Parameter PARAM_C = new Parameter(ARG_C, Parameter.MANDATORY); + private static final Parameter PARAM_CODE = new Parameter(ARG_CODE, Parameter.MANDATORY); + private static final FileArgument ARG_FILE = new FileArgument("file", "a BeanShell script file to execute"); + static final OptionArgument ARG_F = new OptionArgument("-f", "execute file", + new OptionArgument.Option("-f", "execute file")); + private static final Parameter PARAM_F = new Parameter(ARG_F, Parameter.MANDATORY); + private static final Parameter PARAM_FILE = new Parameter(ARG_FILE, Parameter.MANDATORY); + static final OptionArgument ARG_I = new OptionArgument("-i", "go interactive", + new OptionArgument.Option("-i", "go interactive")); + private static final Parameter PARAM_INTERACTIVE = new Parameter(ARG_I, Parameter.MANDATORY); + public static Help.Info HELP_INFO = new Help.Info( + "bsh", + new Syntax("start the interactive BeanShell interpreter"), + new Syntax("invoke the BeanShell interpreter", PARAM_C, PARAM_CODE), + new Syntax("invoke the BeanShell interpreter", PARAM_F, PARAM_FILE), + new Syntax("invoke the BeanShell interpreter", PARAM_C, PARAM_CODE, PARAM_F, PARAM_FILE), + new Syntax("invoke the BeanShell interpreter", PARAM_INTERACTIVE, PARAM_C, PARAM_CODE), + new Syntax("invoke the BeanShell interpreter", PARAM_INTERACTIVE, PARAM_F, PARAM_FILE), + new Syntax("invoke the BeanShell interpreter", PARAM_INTERACTIVE, PARAM_C, PARAM_CODE, PARAM_F, PARAM_FILE) + ); + + public static void main(String[] args) throws Exception { + new BshCommand().execute(new CommandLine(args), System.in, System.out, System.err); + } + + public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err) throws Exception { + ParsedArguments parsedArguments = HELP_INFO.parse(commandLine.toStringArray()); + Interpreter bsh = null; + Object ret; + boolean interactive = false; + + if(PARAM_INTERACTIVE.isSet(parsedArguments)){ + bsh = createInterpreter(in, out, err, true); + interactive = true; + } + + + if (PARAM_C.isSet(parsedArguments)){ + if(bsh == null) + bsh = createInterpreter(in, out, err, false); + + String code = ARG_CODE.getValue(parsedArguments); + ret = bsh.eval(code); + + if(ret != null) + out.println(ret); + } + + + if(PARAM_F.isSet(parsedArguments)){ + if(bsh == null) + bsh = createInterpreter(in, out, err, false); + + String file = ARG_FILE.getValue(parsedArguments); + ret = bsh.source(file); + + if(ret != null) + out.println(ret); + } + + if(bsh == null){ + bsh = createInterpreter(in, out, err, true); + interactive = true; + } + + if(interactive) + bsh.run(); + } + + private static Interpreter createInterpreter(InputStream in, OutputStream out, OutputStream err, boolean interactive){ + return new Interpreter( + new BufferedReader(new InputStreamReader(in)), + new PrintStream(out), + new PrintStream(err), interactive); + } +} Property changes on: trunk/shell/src/shell/org/jnode/shell/command/bsh/BshCommand.java ___________________________________________________________________ Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |