give xt.shell its own package
[org.ibex.xt-crawshaw.git] / src / java / org / ibex / xt / shell / Shell.java
diff --git a/src/java/org/ibex/xt/shell/Shell.java b/src/java/org/ibex/xt/shell/Shell.java
new file mode 100644 (file)
index 0000000..746479e
--- /dev/null
@@ -0,0 +1,110 @@
+package org.ibex.xt.shell;
+
+import java.io.*;
+import java.net.*;
+
+public class Shell extends Env {
+
+    public static void main(String[] args) throws Exception {
+        if (args.length == 0 || args.length > 2||  !args[0].startsWith("http://")) {
+            printUsage(); return;
+        }
+        Shell shell = new Shell(new URL(args[0]));
+
+        if (args.length == 2) {
+            // FIXME
+        } else {
+            shell.listen(new InputStreamReader(System.in), new OutputStreamWriter(System.out));
+        }
+    }
+
+    private static void printUsage() {
+        System.out.println("Usage: xish url [command]");
+    }
+
+    /** URL of server. */
+    protected URL server;
+
+    /** Server cookie. Reduces server load. */
+    private String cookie = null;
+
+    /** Create a new Shell using the given url for the server. */
+    public Shell(URL url) {
+        server = url;
+        commands = new Command[] {
+            new Command.Ls(),
+            new Command.Pwd(),
+            new Command.Cd(),
+            new Command.Rm(),
+            new Command.Help()
+        };
+    }
+
+    public void listen(Reader r, Writer w) throws IOException {
+        LineNumberReader in = new LineNumberReader(r);
+        PrintWriter out = new PrintWriter(w);
+
+        out.println("ibex xt shell: type help or exit");
+        out.print("xt: ");
+        out.flush();
+
+        String line;
+        String buffer = "";
+        while ((line = in.readLine()) != null) {
+            if (line.length() > 0) {
+                if (line.startsWith("exit")) return;
+                if (line.charAt(line.length() - 1) == '\\') {
+                    buffer += line.substring(0, line.length() - 1);
+                    out.print('>');
+                    out.flush(); continue;
+                }
+
+                buffer += line;
+            }
+
+            if (buffer.length() > 0) {
+                String[] c = buffer.split(" ");
+                Command cmd = command(c[0]);
+
+                if (cmd == null) {
+                    out.write(c[0]);
+                    w.write(": command not found\n");
+                } else cmd.execute(out, c, this);
+
+                buffer = "";
+            }
+            out.print("xt: ");
+            out.flush();
+        }
+    }
+
+    public Request.Response send(Request request) throws IOException {
+        URLConnection c = server.openConnection();
+        ((HttpURLConnection)c).setRequestMethod("POST");
+        c.setDoOutput(true);
+        if (cookie != null) c.setRequestProperty("Cookie", cookie);
+
+        c.connect();
+
+        ObjectOutputStream out = new ObjectOutputStream(c.getOutputStream());
+        out.writeObject(request);
+        out.close();
+
+        String cook = c.getHeaderField("Set-Cookie");
+        if (cook != null && !cook.equals("")) cookie = cook.substring(0, cook.indexOf(';'));
+
+        try {
+            Object o = new ObjectInputStream(c.getInputStream()).readObject();
+            if (o == null) {
+                throw new IOException("unexpected null object returned");
+            } else if (!(o instanceof Request.Response)) {
+                throw new IOException("unexpected object returned: "+o.getClass().getName());
+            } else {
+                return (Request.Response)o;
+            }
+        } catch (ClassNotFoundException e) {
+            e.printStackTrace();
+            throw new IOException("unexpected ClassNotFoundException");
+        }
+    }
+}