Subversion Repositories pub

Compare Revisions

Ignore whitespace Rev 61 → Rev 62

/pluggablejs/trunk/src/net/outlyer/plugins/Shell.java
27,34 → 27,95
// $Id$
 
import java.io.File;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import net.outlyer.plugins.utils.*;
 
/**
* Sample shell for tests, files passed as arguments will be loaded and
* executed.
* Sample shell for tests, or scripts.
* Files passed as arguments will be loaded and executed.
*/
public final class Shell {
public static void interactive(final PluginEnvironment pe) {
try {
final Sandbox sandbox = pe.createSandbox(null);
final ScriptEngine rhino = ((SandboxImpl) sandbox).executeAndGetEngine();
 
final Input in = new Input();
final Output out = new Output();
System.err.printf("pluggablejsShell v%d.%d.%d\n",API.VERSION,API.MIN_SUPPORTED_VERSION,API.REVISION);
final boolean isWin = System.getProperty("os.name").toLowerCase().startsWith("windows");
// apparently windows doesn't get EOF until enter is pressed
System.err.printf(" use EOF (%s) or break (CTRL+C) to exit\n", (isWin)?"CTRL+Z,Enter":"CTRL+D");
 
try {
// undocumented feature: when
// $net.outlyer.runtime.internal.shell is true an extra println() is
// issued after each eval
rhino.eval("$net.outlyer.runtime.internal.shell = { autoEOL: false, };");
}
catch (final ScriptException e) {
e.printStackTrace();
}
while (true) {
out.print("pjsh% : ");
final String line = in.readline();
if (null == line) {
break;
}
try {
rhino.eval(line);
final boolean eol = (Boolean) rhino.eval("$net.outlyer.runtime.internal.shell.autoEOL");
if (eol) {
out.println();
}
}
catch (final ScriptException e) {
System.err.println("//[E] Error: " + e.getMessage());
}
}
// EOF
System.exit(0);
}
catch (final PluginException e) {
System.err.printf("Exception while running on-the-fly " +
"interpreted script", e.getLocalizedMessage());
System.exit(5);
}
}
public static void main(String...args) {
if (args.length == 0) {
System.err.printf("Usage: java [...] %s [file1.js [file2.js [...]]]\n",
System.err.printf("Usage: \n" +
" java [...] %s <file1.js [file2.js [...]]>\n"+
" or\n"+
" java [...] %1$s <-i|--interactive>\n",
Shell.class.getName());
System.exit(1);
}
 
final PluginEnvironment pe = new PluginEnvironment();
pe.exportGlobalObject("input", new Input());
pe.exportGlobalObject("out", new Output());
pe.exportGlobalObject("err", new Output(System.err));
pe.exportGlobalObject("hooks", new Hooks());
pe.exportObject("input", new Input());
pe.exportObject("out", new Output());
pe.exportObject("err", new Output(System.err));
pe.exportObject("hooks", new Hooks());
// Generally either UI or GUI should be used but not both
// since GUI falls-back to UI if appropriate (so the ui object
// will use the "best" implementation)
pe.exportGlobalObject("ui", new GUI());
pe.exportGlobalObject("gui", new GUI());
pe.exportGlobalObject("cui", new UI()); // cui stands for Console UI
pe.exportObject("ui", new GUI());
pe.exportObject("gui", new GUI());
pe.exportObject("cui", new UI()); // cui stands for Console UI
pe.exportObject("lang", new LanguageExtensions());
final PluginObject po = new PluginObject();
pe.setPluginObject(po);
if (args.length == 1 && (args[0].equals("-i") || args[0].equals("--interactive"))) {
interactive(pe);
assert false;
}
// Non-interactive mode
for (final String path : args) {
try {
final Sandbox sandbox = pe.createSandbox(new File(path).toURI());