|
|
import java.lang.reflect.Method; | import java.lang.reflect.Method; |
import java.lang.reflect.InvocationTargetException; | import java.lang.reflect.InvocationTargetException; |
| |
import bsh.util.BshCompleter; |
import bsh.util.NameCompletionTable; |
import bsh.classpath.ClassManagerImpl; |
import org.gnu.readline.Readline; |
import org.gnu.readline.ReadlineLibrary; |
import org.gnu.readline.ReadlineReader; |
|
/** | /** |
The BeanShell script interpreter. | The BeanShell script interpreter. |
| |
|
|
else | else |
src = System.in; | src = System.in; |
| |
Reader in = new CommandLineReader( new InputStreamReader(src)); |
Reader in = null; |
Interpreter interpreter = |
boolean usingReadline = false; |
new Interpreter( in, System.out, System.err, true ); |
String backingLib = System.getProperty("bsh.console.readlinelib"); System.out.println("backingLib is " + backingLib); |
interpreter.run(); |
if (backingLib != null && backingLib.length() > 0) { |
|
try { |
|
File history = new File(System.getProperty("user.home") + |
|
File.separator + ".bsh_history"); |
|
if (!history.exists()) { |
|
try { |
|
history.createNewFile(); |
|
} catch(IOException ioe) { |
|
debug("Unable to create history " + history.getAbsolutePath()); |
|
} |
|
} |
|
ReadlineLibrary lib = ReadlineLibrary.byName(backingLib); |
|
// should I wrap CommandLineReader around it? |
|
if (history.canWrite() && history.canRead()) { |
|
in = new ReadlineReader("bsh % ", history,lib); |
|
} else { |
|
in = new ReadlineReader("bsh % ",lib); |
|
debug("Unable to read/write history " + history.getAbsolutePath()); |
|
} |
|
} catch (IOException ioe) { |
|
System.err.println("Unable to invoke ReadlineReader " + |
|
"due to: " + ioe); |
|
} |
|
} |
|
if (in == null) |
|
in = new CommandLineReader( new InputStreamReader(src)); |
|
else |
|
usingReadline = true; |
|
Interpreter interpreter = |
|
new Interpreter( in, System.out, System.err, true ); |
|
if (usingReadline) { |
|
NameCompletionTable nct = new NameCompletionTable(); |
|
nct.add(interpreter.getNameSpace()); |
|
|
|
/** ClassManager does a lot of chatting to the stdout, |
|
* so this has been commented out for the time being |
|
**/ |
|
|
|
// try { |
|
// BshClassManager bcm = BshClassManager.getClassManager(); |
|
// if (bcm != null) { |
|
// nct.add(((ClassManagerImpl)bcm).getClassPath()); |
|
// } |
|
// } catch(ClassPathException cpe) { |
|
// debug("classpath exception in name compl:" + cpe); |
|
// } |
|
|
|
Readline.setCompleter(new BshCompleter(nct)); |
|
} |
|
interpreter.run(); |
} | } |
} | } |
| |
|
|
System.err.flush(); | System.err.flush(); |
Thread.yield(); // this helps a little | Thread.yield(); // this helps a little |
| |
if ( interactive ) |
if ( interactive && !(in instanceof ReadlineReader)) |
print( getBshPrompt() ); | print( getBshPrompt() ); |
| |
eof = Line(); | eof = Line(); |
|
|
} | } |
} | } |
| |
if ( interactive && exitOnEOF ) |
if ( interactive && exitOnEOF ) { |
System.exit(0); |
/* should be done for all streams in general, but this |
|
* ensures that the history for readline is flushed */ |
|
try { |
|
in.close(); |
|
} catch (IOException ioe) { |
|
} |
|
|
|
System.exit(0); |
} | } |
|
} |
// begin source and eval | // begin source and eval |
| |
/** | /** |
|
|
package bsh.util; |
|
import org.gnu.readline.ReadlineCompleter; |
|
/** |
* An adapter for org.gnu.readline's ReadlineCompleter interface to map to |
* BeanShell's NameCompleter interface. |
* |
* @see org.gnu.readline.ReadlineReader |
* @version $Revision: 1.1 $ |
* @author Shane Celis <shane@terraspring.com> |
**/ |
public class BshCompleter implements ReadlineCompleter { |
|
private NameCompletion completer; |
|
/** |
* Constructs a <code>ReadlineCompleter</code> out of a |
* <code>NameCompleter</code> object. |
**/ |
public BshCompleter(NameCompletion completer) { |
this.completer = completer; |
} |
|
/** |
* Returns String of completion if unambiguous, otherwise null |
**/ |
public String completer(String text, int state) { |
// Not sure what state is used for in ReadlineCompleter |
String[] completions = completer.completeName(text); |
if (completions.length == 1 && state == 0) { |
return completions[0]; |
} else { |
return null; // ambiguous result |
} |
} |
|
} |