give xt.shell its own package
[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.Help()
40         };
41     }
42
43     public void listen(Reader r, Writer w) throws IOException {
44         LineNumberReader in = new LineNumberReader(r);
45         PrintWriter out = new PrintWriter(w);
46
47         out.println("ibex xt shell: type help or exit");
48         out.print("xt: ");
49         out.flush();
50
51         String line;
52         String buffer = "";
53         while ((line = in.readLine()) != null) {
54             if (line.length() > 0) {
55                 if (line.startsWith("exit")) return;
56                 if (line.charAt(line.length() - 1) == '\\') {
57                     buffer += line.substring(0, line.length() - 1);
58                     out.print('>');
59                     out.flush(); continue;
60                 }
61
62                 buffer += line;
63             }
64
65             if (buffer.length() > 0) {
66                 String[] c = buffer.split(" ");
67                 Command cmd = command(c[0]);
68
69                 if (cmd == null) {
70                     out.write(c[0]);
71                     w.write(": command not found\n");
72                 } else cmd.execute(out, c, this);
73
74                 buffer = "";
75             }
76             out.print("xt: ");
77             out.flush();
78         }
79     }
80
81     public Request.Response send(Request request) throws IOException {
82         URLConnection c = server.openConnection();
83         ((HttpURLConnection)c).setRequestMethod("POST");
84         c.setDoOutput(true);
85         if (cookie != null) c.setRequestProperty("Cookie", cookie);
86
87         c.connect();
88
89         ObjectOutputStream out = new ObjectOutputStream(c.getOutputStream());
90         out.writeObject(request);
91         out.close();
92
93         String cook = c.getHeaderField("Set-Cookie");
94         if (cook != null && !cook.equals("")) cookie = cook.substring(0, cook.indexOf(';'));
95
96         try {
97             Object o = new ObjectInputStream(c.getInputStream()).readObject();
98             if (o == null) {
99                 throw new IOException("unexpected null object returned");
100             } else if (!(o instanceof Request.Response)) {
101                 throw new IOException("unexpected object returned: "+o.getClass().getName());
102             } else {
103                 return (Request.Response)o;
104             }
105         } catch (ClassNotFoundException e) {
106             e.printStackTrace();
107             throw new IOException("unexpected ClassNotFoundException");
108         }
109     }
110 }