| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.apache.xbean.command; |
| 18 | |
|
| 19 | |
import java.io.DataInputStream; |
| 20 | |
import java.io.IOException; |
| 21 | |
import java.io.InputStream; |
| 22 | |
import java.io.PrintStream; |
| 23 | |
import java.util.StringTokenizer; |
| 24 | |
import java.util.Vector; |
| 25 | |
|
| 26 | |
|
| 27 | |
public class CommandShell implements Command { |
| 28 | |
|
| 29 | |
private final String prompt; |
| 30 | |
|
| 31 | 0 | public CommandShell(String serverName) { |
| 32 | 0 | serverName = serverName.toLowerCase(); |
| 33 | 0 | prompt = TTY_Reset + TTY_Bright + "["+serverName+"]$ " + TTY_Reset; |
| 34 | 0 | } |
| 35 | |
|
| 36 | 0 | private boolean stop = false; |
| 37 | 0 | private int rc = 0; |
| 38 | |
|
| 39 | |
public static final char ESC = (char) 27; |
| 40 | |
public static final String TTY_Reset = ESC + "[0m"; |
| 41 | |
public static final String TTY_Bright = ESC + "[1m"; |
| 42 | |
public static final String TTY_Dim = ESC + "[2m"; |
| 43 | |
public static final String TTY_Underscore = ESC + "[4m"; |
| 44 | |
public static final String TTY_Blink = ESC + "[5m"; |
| 45 | |
public static final String TTY_Reverse = ESC + "[7m"; |
| 46 | |
public static final String TTY_Hidden = ESC + "[8m"; |
| 47 | |
|
| 48 | |
public static final String TTY_FG_Black = ESC + "[30m"; |
| 49 | |
public static final String TTY_FG_Red = ESC + "[31m"; |
| 50 | |
public static final String TTY_FG_Green = ESC + "[32m"; |
| 51 | |
public static final String TTY_FG_Yellow = ESC + "[33m"; |
| 52 | |
public static final String TTY_FG_Blue = ESC + "[34m"; |
| 53 | |
public static final String TTY_FG_Magenta = ESC + "[35m"; |
| 54 | |
public static final String TTY_FG_Cyan = ESC + "[36m"; |
| 55 | |
public static final String TTY_FG_White = ESC + "[37m"; |
| 56 | |
|
| 57 | |
public static final String TTY_BG_Black = ESC + "[40m"; |
| 58 | |
public static final String TTY_BG_Red = ESC + "[41m"; |
| 59 | |
public static final String TTY_BG_Green = ESC + "[42m"; |
| 60 | |
public static final String TTY_BG_Yellow = ESC + "[43m"; |
| 61 | |
public static final String TTY_BG_Blue = ESC + "[44m"; |
| 62 | |
public static final String TTY_BG_Magenta = ESC + "[45m"; |
| 63 | |
public static final String TTY_BG_Cyan = ESC + "[46m"; |
| 64 | |
public static final String TTY_BG_White = ESC + "[47m"; |
| 65 | |
|
| 66 | |
public int main(String[] args, InputStream input, PrintStream out) { |
| 67 | |
|
| 68 | 0 | DataInputStream in = new DataInputStream(input); |
| 69 | 0 | while (!stop) { |
| 70 | 0 | prompt(in, out); |
| 71 | |
} |
| 72 | 0 | return rc; |
| 73 | |
} |
| 74 | |
|
| 75 | |
protected void prompt(DataInputStream in, PrintStream out) { |
| 76 | |
try { |
| 77 | 0 | out.print(prompt); |
| 78 | 0 | out.flush(); |
| 79 | |
|
| 80 | 0 | String commandline = in.readLine(); |
| 81 | 0 | if( commandline == null ) { |
| 82 | 0 | this.stop = true; |
| 83 | 0 | return; |
| 84 | |
} |
| 85 | 0 | commandline = commandline.trim(); |
| 86 | 0 | if (commandline.length() < 1) { |
| 87 | 0 | return; |
| 88 | |
} |
| 89 | |
|
| 90 | 0 | String command = commandline; |
| 91 | |
|
| 92 | 0 | StringTokenizer cmdstr = new StringTokenizer(command); |
| 93 | 0 | command = cmdstr.nextToken(); |
| 94 | |
|
| 95 | |
|
| 96 | 0 | Vector p = new Vector(); |
| 97 | 0 | while ( cmdstr.hasMoreTokens() ) { |
| 98 | 0 | p.add(cmdstr.nextToken()); |
| 99 | |
} |
| 100 | 0 | String[] args = new String[p.size()]; |
| 101 | 0 | p.copyInto(args); |
| 102 | |
|
| 103 | 0 | Command cmd = CommandRegistry.getCommand(command); |
| 104 | |
|
| 105 | 0 | if (cmd == null) { |
| 106 | 0 | out.print(command); |
| 107 | 0 | out.println(": command not found"); |
| 108 | |
} else { |
| 109 | 0 | cmd.main(args, in, out); |
| 110 | |
} |
| 111 | 0 | } catch (UnsupportedOperationException e) { |
| 112 | 0 | this.rc=-1; |
| 113 | 0 | this.stop = true; |
| 114 | 0 | } catch (Throwable e) { |
| 115 | 0 | e.printStackTrace(out); |
| 116 | 0 | this.rc=-1; |
| 117 | 0 | this.stop = true; |
| 118 | 0 | } |
| 119 | 0 | } |
| 120 | |
|
| 121 | |
protected void badCommand(DataInputStream in, PrintStream out) throws IOException { |
| 122 | |
|
| 123 | 0 | } |
| 124 | |
|
| 125 | |
} |