1 package org.ibex.xt.shell;
6 public class Shell extends Env {
8 public static void main(String[] args) throws Exception {
9 if (args.length == 0 || args.length > 2|| !args[0].startsWith("http://")) {
12 Shell shell = new Shell(new URL(args[0]));
14 if (args.length == 2) {
17 shell.listen(new InputStreamReader(System.in), new OutputStreamWriter(System.out));
21 private static void printUsage() {
22 System.out.println("Usage: xish url [command]");
28 /** Server cookie. Reduces server load. */
29 private String cookie = null;
31 /** Create a new Shell using the given url for the server. */
32 public Shell(URL url) {
34 commands = new Command[] {
44 public void listen(Reader r, Writer w) throws IOException {
45 LineNumberReader in = new LineNumberReader(r);
46 PrintWriter out = new PrintWriter(w);
48 out.println("ibex xt shell: type help or exit");
54 int countBraceOpen = 0, countBraceClose = 0;
55 int countBracketOpen = 0, countBracketClose = 0;
56 int countSQBracketOpen = 0, countSQBracketClose = 0;
57 while ((line = in.readLine()) != null) {
58 if (line.length() > 0) {
59 if (line.startsWith("exit")) return;
61 for (int i=0; i < line.length(); i++) {
62 switch (line.charAt(i)) {
63 case '{': countBraceOpen++; break;
64 case '}': countBraceClose++; break;
65 case '(': countBracketOpen++; break;
66 case ')': countBracketClose++; break;
67 case '[': countSQBracketOpen++; break;
68 case ']': countSQBracketClose++; break;
72 boolean nonendchar = line.charAt(line.length() - 1) == '\\';
73 if (nonendchar) line = line.substring(0, line.length() - 1);
77 countBracketOpen != countBracketClose ||
78 countBraceOpen != countBraceClose ||
79 countSQBracketOpen != countSQBracketClose) {
81 out.flush(); continue;
85 if (buffer.length() > 0) {
86 String[] c = buffer.split(" ");
87 Command cmd = command(c[0]);
91 w.write(": command not found\n");
92 } else cmd.execute(out, c, this);
101 public Request.Response send(Request request) throws IOException {
102 URLConnection c = server.openConnection();
103 ((HttpURLConnection)c).setRequestMethod("POST");
105 if (cookie != null) c.setRequestProperty("Cookie", cookie);
109 ObjectOutputStream out = new ObjectOutputStream(c.getOutputStream());
110 out.writeObject(request);
113 String cook = c.getHeaderField("Set-Cookie");
114 if (cook != null && !cook.equals("")) cookie = cook.substring(0, cook.indexOf(';'));
117 Object o = new ObjectInputStream(c.getInputStream()).readObject();
119 throw new IOException("unexpected null object returned");
120 } else if (!(o instanceof Request.Response)) {
121 throw new IOException("unexpected object returned: "+o.getClass().getName());
123 return (Request.Response)o;
125 } catch (ClassNotFoundException e) {
127 throw new IOException("unexpected ClassNotFoundException");