📄 shell.java
字号:
} catch (IOException ignored) { ; } } } /** * Process the a single command * * @param cmd the command string. */ private void processCmd(String cmd) { if (LOG.isLoggable(java.util.logging.Level.INFO)) { LOG.finer("BEGINING OF COMMAND : " + cmd); } // get the args as a list of tokens List args = new ArrayList( Arrays.asList( tokenizeLine(cmd) ) ); if( args.size() < 1 ) { return; } // Get the returnvar, if any. String returnvar = null; if( args.size() >= 2 ) { if( "=".equals( args.get(1) ) ) { returnvar = (String) args.remove(0); args.remove( 0 ); } } String app = (String) args.remove( 0 ); // echo the command if the echo enviroment variable is defined if (getEnv().contains("echo")) { consoleMessage( "Executing command : " + cmd ); } // "clear" is an internal command; just handle it here, nothing to load. if (app.equals("clear")) { cons.clear(); return; } else if (app.equals(cons.getCursorUpName())) { HistoryQueue queue = getHistoryQueue(); if (queue != null) { cons.setCommandLine(queue.getNextCommand()); } return; } else if (app.equals(cons.getCursorDownName())) { HistoryQueue queue = getHistoryQueue(); if (queue != null) { cons.setCommandLine(queue.getPreviousCommand()); } return; } else if (app.startsWith("!")) { try { int number = Integer.valueOf(app.substring(1)).intValue(); HistoryQueue queue = getHistoryQueue(); if (queue != null) { queue.removeLastCommand(); cons.setCommandLine(queue.getCommand(number)); return; } } catch (Exception iox) { // was not a history command, let the remainder of // the method handle the command /* * If there are no commands starting with * '!', then why not catch the exception, * tell the user that there is nothing like this * in the history, and return a null? */ } } ShellApp appl = loadApp( returnvar, app, getEnv()); if (null != appl) { exec( appl, (String[]) args.toArray(new String [args.size()]) ); } else { consoleMessage("Could not load command '" + app + "'"); } } /** * Process the <cmd>(";" <cmd>)* commands * * <p/>FIXME 20010611 bondolo@jxta.org does not handle quoting in any form. * * @param cmd the command string. */ private void processMultipleCmd(String cmd) { HistoryQueue queue = getHistoryQueue(); if (queue != null) { queue.addCommand(cmd); } StringTokenizer tokens = new StringTokenizer(cmd, ";"); while (tokens.hasMoreElements()) { processPipeCmd(tokens.nextToken()); } } /** * Process the <cmd> ("|" <cmd>)* commands * * <p/>FIXME 20010611 bondolo@jxta.org does not handle quoting in any form. * * @param cmd the command string. */ private void processPipeCmd(String cmd) { List cmds = new ArrayList(); StringTokenizer tokens = new StringTokenizer(cmd, "|"); while (tokens.hasMoreElements()) { cmds.add(tokens.nextToken()); } // at the beginning start with stdin and stdout PeerGroup current = (PeerGroup) getEnv().get("stdgroup").getObject(); InputPipe stdin = (InputPipe) getEnv().get("stdin").getObject(); OutputPipe stdout = (OutputPipe) getEnv().get("stdout").getObject(); // these are for building the pipeline InputPipe pipein = null; OutputPipe pipeout = null; InputPipe lastin = stdin; Thread willDependOn = null; // The first and last command in the pipe needs to be treated separatly PipeService pipes = current.getPipeService(); for (int i = 0; i < cmds.size() - 1; i++) { /* * create Shell cmd pipe to link the two */ PipeAdvertisement padv = null; try { padv = (PipeAdvertisement) AdvertisementFactory.newAdvertisement( PipeAdvertisement.getAdvertisementType()); padv.setPipeID(IDFactory.newPipeID(current.getPeerGroupID())); padv.setType(PipeService.UnicastType); pipein = pipes.createInputPipe(padv); pipeout = pipes.createOutputPipe(padv, Collections.singleton(current.getPeerID()), 0); } catch (IOException ex) { printStackTrace("Could not construct pipes for piped command.", ex); } /* * create the environment by cloning the parent. */ ShellEnv pipeenv = new ShellEnv(getEnv()); pipeenv.add("stdout", new ShellObject("Default OutputPipe", pipeout)); pipeenv.add("stdin", new ShellObject("Default InputPipe", lastin)); /* * create a new Shell process to run this pipe command */ Shell pipeShell = (Shell) loadApp(null, "Shell", pipeenv); pipeShell.setJoinedThread( willDependOn ); pipeShell.initPipe( (String) cmds.get(i) ); willDependOn = pipeShell.thread; /* * update last in pipe for the next command */ lastin = pipein; } /* * Set the pipeline for the last command and let it go/ * only stdin needs redirection since stdout is the right one */ getEnv().add("stdout", new ShellObject("Default OutputPipe", stdout)); ShellObject oldin = getEnv().get("stdin"); getEnv().add("stdin", new ShellObject("Default InputPipe", lastin)); setJoinedThread(willDependOn); processCmd( (String) cmds.get(cmds.size() - 1) ); setJoinedThread(null); // restore the original stdin getEnv().add("stdin", oldin); } /** * This method implements the default input stream (keyboard). */ private void runShell() { if ( execShell || (scriptReader == null) ) { consprintln("============================================="); consprintln("=======<[ Welcome to the JXTA Shell ]>======="); consprintln("============================================="); info(); } // check if there is a .jshrc file if( execShell ) { startupFile(); } while ( !stopped ) { String cmd = null; try { if (scriptReader != null) { cmd = scriptReader.readLine(); } else { cons.setPrompt(CMD_PROMPT); cmd = waitForInput(); } } catch (IOException e) { System.err.println("Shell is reconnecting to console"); // This shell has lost its standard InputPipe. Try // to reconnect to the special keyboard InputPipe. setInputPipe(getInputConsPipe()); continue; } if (cmd == null) { if( !stopped ) { exec( null, "exit", new String[0], getEnv() ); } break; } processMultipleCmd(cmd); } } /** * Return true if this is an embedded shell. IE used by an application that * wouldn't like it if System.exit were called. * *@return The embedded value *@author <a href="mailto:burton@openprivacy.org">Kevin A. Burton</a> */ public static boolean isEmbedded() { String value = System.getProperty(JXTA_SHELL_EMBEDDED_KEY, "false" ); return Boolean.valueOf(value).booleanValue(); } /** * converts a command line string into a series of tokens. **/ public String[] tokenizeLine( String line ) { List tokens = new ArrayList(); StringBuffer currentToken = new StringBuffer(); int current = 0; int quote = -1; boolean escape = false; while( current < line.length() ) { final char currentChar = line.charAt( current ); if( escape ) { currentToken.append( currentChar ); escape = false; } else if( -1 != quote ) { if( currentChar == quote ) { quote = -1; } else { currentToken.append( currentChar ); } } else { switch( currentChar ) { case ' ' : case '\t' : if( currentToken.length() > 0 ) { tokens.add( currentToken.toString() ); currentToken.setLength(0); } break; case '=' : case '|' : case ';' : if( currentToken.length() > 0 ) { tokens.add( currentToken.toString() ); currentToken.setLength(0); } tokens.add( Character.toString(currentChar) ); break; case '"' : case '\'' : quote = currentChar; break; case '\\' : escape = true; break; default : currentToken.append( currentChar ); break; } } current++; } if( currentToken.length() > 0 ) { tokens.add( currentToken.toString() ); currentToken.setLength(0); } return (String[]) tokens.toArray( new String[tokens.size()] ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -