1 package org.ibex.xt.shell;
3 import java.io.StringReader;
5 import java.io.IOException;
11 public abstract class Command {
12 /** Returns the command name. */
13 public abstract String name();
15 /** Returns single-line of usage information, eg. <tt>pattern]</tt> */
16 public abstract String usage();
18 /** Returns single-line description of command. */
19 public abstract String summary();
21 /** Returns multi-line description. */
22 public abstract String help();
24 /** Writes result of execution, even if result is an error. */
25 public abstract void execute(Writer w, String[] args, Env env) throws IOException;
27 public static class Help extends Command {
28 public String name() { return "help"; }
29 public String usage() { return "[command name]"; }
30 public String summary() { return "Lists available commands."; }
31 public String help() { return ""; }
33 public void execute(Writer w, String[] c, Env env) throws IOException {
35 Command cmd = env.command(c[1]);
39 w.write(": command not found\n");
51 for (int i=0; i < env.commands.length; i++)
52 len = Math.max(env.commands[i].name().length(), len);
54 w.write("Available commands:\n");
55 for (int i=0; i < env.commands.length; i++) {
56 Command cmd = env.commands[i];
59 for (int j=len - cmd.name().length(); j >= 0; j--) w.write(" ");
61 w.write(cmd.summary());
64 w.write("\nFor usage details, type help [command name].\n");
69 public static class Ls extends Command {
70 public String name() { return "ls"; }
71 public String usage() { return "[path]"; }
72 public String summary() { return "List object entries."; }
73 public String help() { return
74 "Lists the keys in an object. Modelled after the UNIX ls command.";
77 public void execute(Writer w, String[] c, Env env) throws IOException {
78 if (c.length > 2) { w.write(usage()); return; }
79 String p = env.path(c.length == 1 ? "*" : c[1]);
81 Request.Response ret = env.send(new Request.Key(p));
82 if (!(ret instanceof Request.Key.Res)) {
84 w.write(ret.error().getMessage());
87 List l = ((Request.Key.Res)ret).keys();
88 Iterator i = l.iterator(); while (i.hasNext()) {
89 w.write(i.next().toString());
96 public static class Pwd extends Command {
97 public String name() { return "pwd"; }
98 public String usage() { return ""; }
99 public String summary() { return "Path to current object."; }
100 public String help() { return "Print the path to the current object."; }
101 public void execute(Writer w, String[] c, Env env) throws IOException {
102 if (c.length != 1) { w.write(usage()); return; }
103 w.write(env.path.equals("") ? "." : env.path);
108 public static class Cd extends Command {
109 public String name() { return "cd"; }
110 public String usage() { return "[path]"; }
111 public String summary() { return "Change current object."; }
112 public String help() { return
113 "Chnages the current object that all other commands use "+
114 "as the base for running.\n Pass either a relative path "+
115 "(e.g. in .prevalent, type cd myob, now in .prevalent.myob) "+
116 "or an absolute path (e.g. cd .prevalent.myob).\n\n" +
117 "To go up one level, cd .. can be used.";
119 public void execute(Writer w, String[] c, Env env) throws IOException {
120 if (c.length > 2) w.write(usage());
121 else if (c.length == 1 || c[1].equals("") || c[1].equals(".")) env.path = "";
122 else if (c[1].equals("..")) {
124 n = n.substring(0, n.lastIndexOf('.'));
127 String n = env.path(c[1]);
129 Request.Response ret = env.send(new Request.Key(n));
130 if (!(ret instanceof Request.Key.Res)) {
132 w.write(ret.error().getMessage());
135 List l = ((Request.Key.Res)ret).keys();
139 w.write(": no such path\n");
148 public static class Rm extends Command {
149 public String name() { return "rm"; }
150 public String usage() { return "[options] [path]"; }
151 public String summary() { return "Removes objects."; }
152 public String help() { return "Removes objects."; } // FIXME
153 public void execute(Writer w, String[] c, Env env) throws IOException {
154 if (c.length == 1) { w.write(usage()); }
156 Request.Key[] r = new Request.Key[c.length - 1];
157 for (int i=0; i < r.length; i++) r[i] = new Request.RemoveKey(env.path(c[i + 1]));
159 Request.Response ret = env.send(new Request.Composite(r, false));
160 if (!(ret instanceof Request.Composite.Res)) {
162 w.write(ret.error().getMessage());
165 Request.Response[] res = ((Request.Composite.Res)ret).responses();
166 for (int i=0; i < res.length; i++) {
167 if (res[i] instanceof Request.RemoveKey.Res) {
168 boolean rm = ((Request.RemoveKey.Res)res[i]).removed();
170 w.write("error: cannot remove '");
172 w.write("': no such key\n");
175 w.write("error: cannot remove '");
178 w.write(res[i].error().getMessage());
186 public static class Set extends Command {
187 public String name() { return "set"; }
188 public String usage() { return "[name] [object]"; }
189 public String summary() { return "Sets ."; }
190 public String help() { return "Removes objects."; } // FIXME
192 public void execute(Writer w, String[] c, Env env) throws IOException {
193 if (c.length < 3) { w.write(usage()); }
196 String s = "return (";
197 for (int i=2; i < c.length; i++) s += c[i];
201 try { js = JS.fromReader("input", 0, new StringReader(s)); }
202 catch (IOException e) {
204 w.write(e.getMessage());
209 Request.Response ret = env.send(new Request.SetKey(env.path, m, js));