📄 commandshell.java
字号:
/**
* $Id: CommandShell.java,v 1.12 2004/02/21 13:54:21 vchira_2000 Exp $
*/
package org.jnode.shell;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.PrintStream;
import java.io.StringReader;
import java.text.DateFormat;
import java.util.Date;
import java.util.StringTokenizer;
import javax.naming.NameNotFoundException;
import org.apache.log4j.Logger;
import org.jnode.driver.console.Console;
import org.jnode.driver.console.ConsoleManager;
import org.jnode.driver.console.x86.ScrollableShellConsole;
import org.jnode.driver.input.KeyboardEvent;
import org.jnode.driver.input.KeyboardListener;
import org.jnode.naming.InitialNaming;
import org.jnode.shell.alias.AliasManager;
import org.jnode.shell.alias.NoSuchAliasException;
import org.jnode.shell.help.AliasArgument;
import org.jnode.shell.help.Argument;
import org.jnode.shell.help.CompletionException;
import org.jnode.shell.help.Help;
/**
* @author epr
*/
public class CommandShell implements Runnable, Shell, KeyboardListener {
public static final String PROMPT_PROPERTY_NAME = "jnode.prompt";
/** My logger */
private final Logger log = Logger.getLogger(getClass());
private PrintStream out;
private PrintStream err;
private AliasManager aliasMgr;
/** Keeps a reference to the console this CommandShell is using * */
private ScrollableShellConsole console = null;
/** Contains the archive of commands. * */
private CommandHistory history = new CommandHistory();
/**
* Contains an index to the current history line. 0 = first historical command. 2 = next historical command. -1 = the current command line.
*/
private int historyIndex = -1;
/**
* Contains the current position of the cursor on the currentLine
*/
private int posOnCurrentLine = 0;
/** Contains the current line * */
private String currentLine = "";
/** Contains the newest command being typed in * */
private String newestLine = "";
/** Flag to know if the shell is active to take the keystrokes * */
private boolean isActive = false;
private String currentPrompt = null;
/**
* Flag to know when to wait (while input is happening). This is (hopefully) a thread safe implementation. *
*/
private volatile boolean threadSuspended = false;
private static String DEFAULT_PROMPT = "JNode $P$G";
//private static final Class[] MAIN_ARG_TYPES = new Class[] { String[].class };
private CommandInvoker commandInvoker;
private ThreadCommandInvoker threadCommandInvoker;
private DefaultCommandInvoker defaultCommandInvoker;
public Console getConsole() {
return console;
}
public void setThreadCommandInvoker() {
this.commandInvoker = threadCommandInvoker;
}
public void setDefaultCommandInvoker() {
this.commandInvoker = defaultCommandInvoker;
}
/**
* Create a new instance
*
* @see java.lang.Object
*/
public CommandShell() throws NameNotFoundException, ShellException
{
this((ScrollableShellConsole) ((ConsoleManager) InitialNaming.lookup(ConsoleManager.NAME)).getFocus());
}
public CommandShell(ScrollableShellConsole cons) throws ShellException {
try {
this.console = cons;
this.out = this.console.getOut();
this.err = this.console.getErr();
// listen to the keyboard
this.console.addKeyboardListener(this);
defaultCommandInvoker = new DefaultCommandInvoker(this);
threadCommandInvoker = new ThreadCommandInvoker(this);
this.commandInvoker = threadCommandInvoker; //default to separate threads for commands.
aliasMgr = ((AliasManager) InitialNaming.lookup(AliasManager.NAME)).createAliasManager();
System.getProperties().setProperty(PROMPT_PROPERTY_NAME, DEFAULT_PROMPT);
ShellUtils.getShellManager().registerShell(this);
} catch (NameNotFoundException ex) {
throw new ShellException("Cannot find required resource", ex);
}
}
/**
* Run this shell until exit.
*
* @see java.lang.Runnable#run()
*/
public void run() {
// Run commands from the JNode commandline first
final String cmdLine = System.getProperty("jnode.cmdline", "");
final StringTokenizer tok = new StringTokenizer(cmdLine);
while (tok.hasMoreTokens()) {
final String e = tok.nextToken();
try {
if (e.startsWith("cmd=")) {
final String cmd = e.substring("cmd=".length());
currentPrompt = prompt();
out.println(currentPrompt + cmd);
processCommand(cmd);
}
} catch (Throwable ex) {
ex.printStackTrace(err);
}
}
// Now become interactive
boolean halt = false;
while (!halt) {
try {
synchronized (this) {
// Catch keyboard events
isActive = true;
currentPrompt = prompt();
out.print(currentPrompt);
// wait until enter is hit
threadSuspended = true;
while (threadSuspended)
wait();
if (currentLine.length() > 0)
processCommand(currentLine.trim());
if (currentLine.trim().equals("halt"))
halt = true;
currentLine = "";
historyIndex = -1;
}
} catch (Throwable ex) {
ex.printStackTrace(err);
}
}
}
protected void processCommand(String cmdLineStr) {
commandInvoker.invoke(cmdLineStr);
}
// /**
// * Execute a single command line.
// *
// * @param cmdLineStr
// */
// protected void processCommand(String cmdLineStr) {
//
// final CommandLine cmdLine = new CommandLine(cmdLineStr);
// if (!cmdLine.hasNext())
// return;
// String cmdName = cmdLine.next();
//
// // Add this command to the history.
// if (!cmdLineStr.equals(newestLine))
// history.addCommand(cmdLineStr);
//
// try {
// Class cmdClass = getCommandClass(cmdName);
// final Method main = cmdClass.getMethod("main", MAIN_ARG_TYPES);
// try {
// main.invoke(null, new Object[] { cmdLine.getRemainder().toStringArray()});
// } catch (InvocationTargetException ex) {
// Throwable tex = ex.getTargetException();
// if (tex instanceof SyntaxError) {
// Help.getInfo(cmdClass).usage();
// err.println(tex.getMessage());
// } else {
// err.println("Exception in command");
// tex.printStackTrace(err);
// }
// } catch (Exception ex) {
// err.println("Exception in command");
// ex.printStackTrace(err);
// } catch (Error ex) {
// err.println("Fatal error in command");
// ex.printStackTrace(err);
// }
// } catch (NoSuchMethodException ex) {
// err.println("Alias class has no main method " + cmdName);
// } catch (ClassNotFoundException ex) {
// err.println("Unknown alias class " + ex.getMessage());
// } catch (ClassCastException ex) {
// err.println("Invalid command " + cmdName);
// } catch (Exception ex) {
// err.println("I FOUND AN ERROR: " + ex);
// }
// }
protected Class getCommandClass(String cmd) throws ClassNotFoundException {
try {
return aliasMgr.getAliasClass(cmd);
} catch (NoSuchAliasException ex) {
final ClassLoader cl = Thread.currentThread().getContextClassLoader();
return cl.loadClass(cmd);
}
}
/**
* Gets the alias manager of this shell
*/
public AliasManager getAliasManager() {
return aliasMgr;
}
/**
* Gets the CommandHistory object associated with this shell.
*/
public CommandHistory getCommandHistory() {
return history;
}
/**
* Gets the expanded prompt
*/
protected String prompt() {
String prompt = System.getProperty(PROMPT_PROPERTY_NAME);
final StringBuffer result = new StringBuffer();
boolean commandMode = false;
try {
StringReader reader = new StringReader(prompt);
int i;
while ((i = reader.read()) != -1) {
char c = (char) i;
if (commandMode) {
switch (c) {
case 'P' :
result.append(new File(System.getProperty("user.dir")));
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -