X-Git-Url: http://git.megacz.com/?p=org.ibex.xt-crawshaw.git;a=blobdiff_plain;f=src%2Fjava%2Forg%2Fibex%2Fxt%2Fshell%2FShell.java;fp=src%2Fjava%2Forg%2Fibex%2Fxt%2Fshell%2FShell.java;h=746479ed20b534ef9fff876bc8f1994415ea66fe;hp=0000000000000000000000000000000000000000;hb=79f04e59b0a9997c56f0bc3859be30f6fd262717;hpb=001892c7393bfd314472dba1d87f804be4ed8936 diff --git a/src/java/org/ibex/xt/shell/Shell.java b/src/java/org/ibex/xt/shell/Shell.java new file mode 100644 index 0000000..746479e --- /dev/null +++ b/src/java/org/ibex/xt/shell/Shell.java @@ -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"); + } + } +}