introduce a shell command object
[org.ibex.xt-crawshaw.git] / src / java / org / ibex / xt / Shell.java
1 package org.ibex.xt;
2
3 import java.io.*;
4 import java.net.*;
5 import java.util.*;
6 import java.util.regex.*;
7
8 import org.ibex.util.*;
9 import org.ibex.util.Collections;
10 import org.ibex.js.*;
11
12 public class Shell {
13
14     public static void main(String[] args) throws Exception {
15         if (args.length == 0 || args.length > 2||  !args[0].startsWith("http://")) {
16             printUsage(); return;
17         }
18         Shell shell = new Shell(new URL(args[0]));
19
20         if (args.length == 2) {
21             // FIXME
22         } else {
23             shell.listen(new InputStreamReader(System.in), new OutputStreamWriter(System.out));
24         }
25     }
26
27     private static void printUsage() {
28         System.out.println("Usage: xish url [command]");
29     }
30
31     protected Command[] commands = new Command[] {
32         new LsCommand(),
33         new PwdCommand(),
34         new HelpCommand()
35     };
36
37     /** URL of server. */
38     protected URL server;
39
40     /** Current JS path using '.' as a seperator. */
41     protected String pwd = ".";
42
43     /** Create a new Shell using the given url for the server. */
44     public Shell(URL url) { server = url; }
45
46     public void listen(Reader r, Writer w) throws IOException {
47         LineNumberReader in = new LineNumberReader(r);
48         PrintWriter out = new PrintWriter(w);
49
50         out.println("ibex xt shell: type help or exit");
51         out.print("xt: ");
52         out.flush();
53
54         String line;
55         String buffer = "";
56         while ((line = in.readLine()) != null) {
57             if (line.length() > 0) {
58                 if (line.startsWith("exit")) return;
59                 if (line.charAt(line.length() - 1) == '\\') {
60                     buffer += line.substring(0, line.length() - 1);
61                     out.print('>');
62                     out.flush(); continue;
63                 }
64
65                 buffer += line;
66             }
67
68             if (buffer.length() > 0) {
69                 String[] c = buffer.split(" ");
70                 int i=0; while (i < commands.length) {
71                     if (commands[i].name().equals(c[0])) {
72                         commands[i].execute(out, c);
73                         out.write('\n'); break;
74                     }
75                     i++;
76                 }
77                 if (i == commands.length) {
78                     out.write(c[0]);
79                     w.write(": command not found\n");
80                 }
81                 buffer = "";
82             }
83             out.print("xt: ");
84             out.flush();
85         }
86     }
87
88     private String cookie = null;
89     public Object send(Request request) throws IOException {
90         URLConnection c = server.openConnection();
91         ((HttpURLConnection)c).setRequestMethod("POST");
92         c.setDoOutput(true);
93         if (cookie != null) c.setRequestProperty("Cookie", cookie);
94
95         c.connect();
96
97         ObjectOutputStream out = new ObjectOutputStream(c.getOutputStream());
98         out.writeObject(request);
99         out.close();
100
101         String cook = c.getHeaderField("Set-Cookie");
102         if (cook != null && !cook.equals("")) cookie = cook.substring(0, cook.indexOf(';'));
103
104         try {
105             return new ObjectInputStream(c.getInputStream()).readObject();
106         } catch (ClassNotFoundException e) {
107             e.printStackTrace();
108             throw new IOException("unexpected ClassNotFoundException");
109         }
110     }
111
112     public abstract class Command {
113         /** Returns the command name. */
114         public abstract String name();
115
116         /** Returns single-line of usage information, eg. <tt>pattern]</tt> */
117         public abstract String usage();
118
119         /** Returns single-line description of command. */
120         public abstract String summary();
121
122         /** Returns multi-line description. */
123         public abstract String help();
124
125         /** Writes result of execution, even if result is an error. */
126         public abstract void execute(Writer w, String[] args) throws IOException;
127     }
128
129     /** Returns the command matching the given name. */
130     protected Command command(String name) {
131         for (int i=0; i < commands.length; i++)
132             if (commands[i].name().equals(name)) return commands[i];
133         return null;
134     }
135
136     public class HelpCommand extends Command {
137         public String name() { return "help"; }
138         public String usage() { return "[command name]"; }
139         public String summary() { return "Lists available commands."; }
140         public String help() { return ""; }
141
142         public void execute(Writer w, String[] c) throws IOException {
143             if (c.length > 1) {
144                 Command cmd = command(c[1]);
145                 if (c == null) {
146                     w.write("help: ");
147                     w.write(c[1]);
148                     w.write(": command not found");
149                 } else {
150                     w.write("usage: ");
151                     w.write(cmd.name());
152                     w.write(" ");
153                     w.write(cmd.usage());
154                     w.write("\n\n");
155                     w.write(cmd.help());
156                     w.write("\n");
157                 }
158             } else {
159                 int len = 3;
160                 for (int i=0; i < commands.length; i++)
161                     len = Math.max(commands[i].name().length(), len);
162
163                 w.write("Available commands:\n");
164                 for (int i=0; i < commands.length; i++) {
165                     Command cmd = commands[i];
166                     w.write("  ");
167                     w.write(cmd.name());
168                     for (int j=len - cmd.name().length(); j >= 0; j--) w.write(" ");
169                     w.write(" - ");
170                     w.write(cmd.summary());
171                     w.write("\n");
172                 }
173                 w.write("\nFor usage details, type help [command name].");
174             }
175         }
176     }
177
178     public class LsCommand extends Command {
179         public String name() { return "ls"; }
180         public String usage() { return "[path]"; }
181         public String summary() { return "List object entries."; }
182         public String help() { return
183             "The ls command, modelled after the UNIX ls command lists the " +
184             "keys in an object. ";
185         }
186
187         public void execute(Writer w, String[] c) throws IOException {
188             if (c.length > 2) { w.write(usage()); return; }
189
190             String path = pwd;
191             String matcher = ".*";
192             if (c.length == 2) {
193                 int pos = c[1].lastIndexOf('/') + 1;
194                 path += c[1].substring(0, pos);
195                 path = path.replaceAll("/", "\\.");
196                 if (pos < c[1].length()) {
197                     matcher = c[1].substring(pos);
198                     matcher = matcher.replaceAll("\\.", "\\.");
199                     matcher = matcher.replaceAll("\\*", ".*");
200                 }
201             }
202
203             Object ret = send(new KeyRequest(path, matcher));
204             if (ret == null) {
205                 w.write("error: (unexpected) returned object is null");
206             } else if (ret instanceof JSExn) {
207                 w.write("error: ");
208                 w.write(((JSExn)ret).getMessage());
209             } else if (ret instanceof List) {
210                 List l = (List)ret; Collections.sort(l);
211                 w.write("total items: ");
212                 w.write(l.size()+"");
213                 Iterator i = l.iterator(); while (i.hasNext()) {
214                     w.write("\n  ");
215                     w.write(i.next().toString());
216                 }
217             } else {
218                 w.write("error: (unexpected) returned object is of unknown type: ");
219                 w.write(ret.getClass().getName());
220             }
221         }
222     }
223
224     public class PwdCommand extends Command {
225         public String name() { return "pwd"; }
226         public String usage() { return ""; }
227         public String summary() { return "Path to current object."; }
228         public String help() { return "Print the path to the current object."; }
229         public void execute(Writer w, String[] c) throws IOException {
230             w.write(c.length == 1 ? pwd.replace('.', '/') : usage());
231         }
232     }
233
234     public static abstract class Request implements Serializable {
235         public abstract Object process(JSScope root) throws JSExn;
236     }
237
238     public static class KeyRequest extends Request {
239         private String path, matcher;
240         public KeyRequest() {}
241         public KeyRequest(String path, String matcher) {
242             this.path = path; this.matcher = matcher;
243         }
244
245         /** Returns a List. */
246         public Object process(JSScope root) throws JSExn {
247             String p = path == null ? "" : path.replaceAll("\\.+", "\\.");
248             if (p.length() > 0 && p.charAt(0) == '.') p = p.substring(1);
249             if (p.length() > 0 && p.charAt(p.length() - 1) == '.') p = p.substring(0, p.length() - 1);
250             System.out.println("searching path '"+p+"' for pattern '"+matcher+"'");
251
252             Object o = p.equals("") ? root : root.get(p);
253             if (o == null || o instanceof JSDate ||
254                              o instanceof JSArray ||
255                              !(o instanceof JS)) {
256                 System.out.println("hit bad object: "+o+", class="+
257                     (o == null ? null : o.getClass().getName()));
258                 throw new JSExn("path /" + p + " does not exist");
259             } else {
260                 Pattern pat = Pattern.compile(matcher);
261                 List keys = new ArrayList();
262
263                 Iterator i = ((JS)o).keys().iterator(); while(i.hasNext()) {
264                     String k = i.next().toString();
265                     if (pat.matcher(k).matches()) keys.add(k);
266                 }
267
268                 return keys;
269             }
270         }
271     }
272
273     public static class ExecRequest extends Request {
274         private JS exec;
275         public ExecRequest() {}
276         public ExecRequest(JS exec) { this.exec = exec; }
277         public ExecRequest(String source) throws IOException, JSExn {
278             this(new StringReader(source));
279         }
280         public ExecRequest(Reader source) throws IOException, JSExn {
281             exec = JS.fromReader("xsh", 0, source);
282         }
283
284         /** Returns the result of <tt>JS.eval()</tt>. */
285         public Object process(JSScope root) throws JSExn {
286             return JS.eval(JS.cloneWithNewParentScope(exec, root));
287         }
288     }
289 }