📄 shell.java
字号:
setInputPipe(defaultInputPipe); setInputConsPipe(defaultInputPipe); // Create the default OutputPipe ShellOutputPipe defaultOutputPipe = new ShellOutputPipe(getGroup(), cons); env.add( "stdout", new ShellObject("Default OutputPipe", defaultOutputPipe)); env.add( "consout", new ShellObject("Default Console OutputPipe", defaultOutputPipe)); setOutputPipe(defaultOutputPipe); setOutputConsPipe(defaultOutputPipe); // start the shell on its own thread. thread = new Thread( getGroup().getHomeThreadGroup(), this, "JXTA Shell " + thisInstance ); thread.start(); return ShellApp.appSpawned; } else { ShellObject console = env.get( "console" ); cons = (ShellConsole) console.getObject(); // a child shell just runs until exit. if (null != pipecmd) { processCmd(pipecmd); } else { runShell(); } return ShellApp.appNoError; } } /** * {@inheritDoc} **/ public synchronized void stopApp() { super.stopApp(); // Remove itself from the parent ShellEnv (GC) if (parentEnv != null) { parentEnv.remove(parentEnvEnvName); } // Destroy ourself if (gotMyOwnFrame) { cons.setStatusGroup( null ); cons.destroy(); cons = null; } // Interrupt our parsing thread. if (thread != null) { thread.interrupt(); } if (isRootShell()) { getEnv().clear(); // Until we fix all non-daemon threads in non-jxta code... if(!isEmbedded()) System.exit(0); } } /** * {@inheritDoc} * * <p/>Use the value of stdgroup environment variable. **/ public PeerGroup getGroup() { ShellEnv env = getEnv(); ShellObject stdgroup = null; if( null != env ) { stdgroup = env.get( "stdgroup" ); } return (null != stdgroup) ? (PeerGroup) stdgroup.getObject() : super.getGroup(); } /** * if true then this is a root shell. A root shell is the shell which owns * the console. * * @return if true then this shell owns the console. **/ public boolean isRootShell() { return !gotParent; } /** * Description of the Method * * @return error result code. */ private int syntaxError() { consoleMessage("Usage: Shell [-f filename] [-e cmds] [-s] [-x]"); return ShellApp.appParamError; } /** * {@inheritDoc} **/ public String getDescription() { return "JXTA Shell command interpreter"; } /** * {@inheritDoc} **/ public void help() { println("NAME"); println(" Shell - " + getDescription() ); println(" "); println("SYNOPSIS"); println(" "); println(" Shell [Shell [-f filename] [-e cmds] [-s] [-x]"); println(" "); println("DESCRIPTION"); println(" "); info(); println(" "); println("Every Shell command is of the form <cmd>;..;<cmd>. The ';'"); println("is used as command separator. Multiple commands can be entered"); println("in one line. Pipelines can be created by combining the pipe stdout"); println("of a command into the pipe stdin of another command using the"); println("pipe '|' operator. For example the following command:"); println(" "); println(" JXTA> cat env1 | more"); println(" "); println("Pipes the output of the command 'cat' into the stdin of the command "); println("'more'. An arbitrary number of commands can be pipelined together with " ); println("th '|' pipe operator."); println(" "); println("The '=' operator can be used to assign the value of a command "); println("output to an environment variable. For example :"); println(" "); println(" JXTA> myadv = newpipe -n mypipe"); println(" "); println(" This command creates a new pipe advertisement and stores it in the"); println(" 'myadv' environment variable."); println(" "); println("COMMANDS"); println(" "); println(" The Shell provides the following built-in commands: "); println(" "); println(" clear Clear the shell's screen"); println(" "); println(" The additional commands available may be discovered by : "); println(" "); println(" man "); println(" "); println("OPTIONS"); println(" "); println(" [-f <filename>] Execute the script file"); println(" [-e <commands>] Execute the commands"); println(" [-s] Fork a new Shell console in a new window"); println(" [-x] Skip execution of .jshrc"); println(" "); println("EXAMPLE"); println(" "); println(" JXTA> Shell -f /home/tra/batch"); println(" "); println("This command executes the commands stored in the Shell script"); println("file '/home/tra/myfile' in the current Shell environment."); println(" "); println("ENVIRONMENT"); println(" "); println("The Java implementation of 'Shell' uses two Java system"); println("properties to configure the use of GUI:"); println(" SHELLNOWINDOW - if 'true' then no window is created."); println(" SHELLFONTSIZE - Size in points for the font used in GUI window."); println(" "); println("The following environment variables are defined by default:"); println(" "); println(" consin = Default Console InputPipe"); println(" consout = Default Console OutputPipe"); println(" stdout = Default OutputPipe"); println(" stdin = Default InputPipe"); println(" shell = This Shell object"); println(" stdgroup = Default peer group"); println(" rootgroup = Default Infrastructure group"); println(" worldgroup = World PeerGroup"); println(" echo = (if defined) Echo all commands before executing them"); println(" parentShell = (if defined) For child Shells this is the parent Shell"); println(" "); println("FILES"); println(" "); println(" $HOME/.jshrc"); println(" "); println(" A default startup script that is executed when the Shell " ); println(" is invoked." ); println(" "); println("SEE ALSO"); println(" exit env "); } /** * Description of the Method */ private void info() { println(" "); println("The JXTA Shell provides an interactive environment to the JXTA " + "platform. The Shell provides basic commands to discover peers and " + "peergroups, to join and resign from peergroups, to create pipes " + "between peers, and to send pipe messages. " ); println(" "); println("The Shell provides environment variables that permit binding " + "symbolic names to JXTA platform objects. Environment variables " + "allow Shell commands to exchange data. The command 'env' " + "displays all defined environment variables in the current Shell " + "session." ); println(" "); println("The Shell creates a JXTA InputPipe (stdin) for reading input from " + "the keyboard, and a JXTA OutputPipe (stdout) to display information " + "on the Shell console. All commands executed by the Shell have their " + "initial 'stdin' and 'stdout' set up to the Shell's stdin and stdout pipes. " + "The Shell also creates the environment variable 'stdgroup' that " + "contains the current JXTA PeerGroup in which the Shell and commands " + "are executed."); println(" "); println("The 'man' command is available to list the commands available. " + "Type 'man <command>' to get help about a particular command. " + "To exit the Shell, use the 'exit' command. "); } /** * Description of the Method * *@param script */ private void execScript(String script) { try { processMultipleCmd(script); } catch (Exception ez1) { printStackTrace("Failed with ", ez1); } } /** * Returns the ShellConsole object associated with this shell. It may be * null, if this shell does not run in its own window * * @return ShellConsole object associated with this shell */ public ShellConsole getConsole() { return cons; } /** * Returns the HistoryQueue that holds the cmds in a history list If the * queue does not exist, it is created. * *@return HistoryQueue object used to retrieve commands */ private HistoryQueue getHistoryQueue() { ShellEnv env = getEnv(); ShellObject obj = env.get(HISTORY_ENV_NAME); HistoryQueue queue = null; if (obj != null) { if (HistoryQueue.class.isAssignableFrom(obj.getObjectClass())) { queue = (HistoryQueue) obj.getObject(); } } else { String exclude[] = {cons.getCursorDownName(), cons.getCursorUpName()}; queue = new HistoryQueue(exclude); env.add(HISTORY_ENV_NAME, new ShellObject("History", queue)); } return queue; } /** * Prepare to run a command on a pipe. * *@param cmd The command to be executed. */ private void initPipe( String cmd ) { String myName = "JXTA Shell - " + thisInstance + " : [" + cmd + "]"; pipecmd = cmd; thread = new Thread(getGroup().getHomeThreadGroup(), this, myName); thread.start(); } /** * Description of the Method * * @param fn * @return {@code true} if the script file was found otherwise false {@code false}. */ private boolean initScriptFile( String fn ) { try { scriptReader = new BufferedReader(new FileReader(fn)); return true; } catch (Exception e) { printStackTrace("Failed with ", e); return false; } } /** * Description of the Method */ private void startupFile() { File startupFile = new File( ".jshrc" ); if( !startupFile.exists() ) { return; } BufferedReader scriptReader = null; try { scriptReader = new BufferedReader(new FileReader(startupFile)); // nothing to do String cmd = scriptReader.readLine(); while (cmd != null) { processMultipleCmd(cmd); cmd = scriptReader.readLine(); } } catch (Exception e) { if (LOG.isLoggable(java.util.logging.Level.WARNING)) { LOG.log(Level.WARNING, "Failure with .jshrc ", e); } } finally { try { if (null != scriptReader) { scriptReader.close(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -