add set command and switch completely to . as an object seperator
[org.ibex.xt-crawshaw.git] / src / java / org / ibex / xt / shell / Shell.java
1 package org.ibex.xt.shell;
2
3 import java.io.*;
4 import java.net.*;
5
6 public class Shell extends Env {
7
8     public static void main(String[] args) throws Exception {
9         if (args.length == 0 || args.length > 2||  !args[0].startsWith("http://")) {
10             printUsage(); return;
11         }
12         Shell shell = new Shell(new URL(args[0]));
13
14         if (args.length == 2) {
15             // FIXME
16         } else {
17             shell.listen(new InputStreamReader(System.in), new OutputStreamWriter(System.out));
18         }
19     }
20
21     private static void printUsage() {
22         System.out.println("Usage: xish url [command]");
23     }
24
25     /** URL of server. */
26     protected URL server;
27
28     /** Server cookie. Reduces server load. */
29     private String cookie = null;
30
31     /** Create a new Shell using the given url for the server. */
32     public Shell(URL url) {
33         server = url;
34         commands = new Command[] {
35             new Command.Ls(),
36             new Command.Pwd(),
37             new Command.Cd(),
38             new Command.Rm(),
39             new Command.Set(),
40             new Command.Help()
41         };
42     }
43
44     public void listen(Reader r, Writer w) throws IOException {
45         LineNumberReader in = new LineNumberReader(r);
46         PrintWriter out = new PrintWriter(w);
47
48         out.println("ibex xt shell: type help or exit");
49         out.print("xt: ");
50         out.flush();
51
52         String line;
53         String buffer = "";
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;
60
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;
69                     }
70                 }
71
72                 boolean nonendchar = line.charAt(line.length() - 1) == '\\';
73                 if (nonendchar) line = line.substring(0, line.length() - 1);
74                 buffer += line;
75
76                 if (nonendchar ||
77                         countBracketOpen != countBracketClose ||
78                         countBraceOpen != countBraceClose ||
79                         countSQBracketOpen != countSQBracketClose) {
80                     out.print('>');
81                     out.flush(); continue;
82                 }
83             }
84
85             if (buffer.length() > 0) {
86                 String[] c = buffer.split(" ");
87                 Command cmd = command(c[0]);
88
89                 if (cmd == null) {
90                     out.write(c[0]);
91                     w.write(": command not found\n");
92                 } else cmd.execute(out, c, this);
93
94                 buffer = "";
95             }
96             out.print("xt: ");
97             out.flush();
98         }
99     }
100
101     public Request.Response send(Request request) throws IOException {
102         URLConnection c = server.openConnection();
103         ((HttpURLConnection)c).setRequestMethod("POST");
104         c.setDoOutput(true);
105         if (cookie != null) c.setRequestProperty("Cookie", cookie);
106
107         c.connect();
108
109         ObjectOutputStream out = new ObjectOutputStream(c.getOutputStream());
110         out.writeObject(request);
111         out.close();
112
113         String cook = c.getHeaderField("Set-Cookie");
114         if (cook != null && !cook.equals("")) cookie = cook.substring(0, cook.indexOf(';'));
115
116         try {
117             Object o = new ObjectInputStream(c.getInputStream()).readObject();
118             if (o == null) {
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());
122             } else {
123                 return (Request.Response)o;
124             }
125         } catch (ClassNotFoundException e) {
126             e.printStackTrace();
127             throw new IOException("unexpected ClassNotFoundException");
128         }
129     }
130 }