--- /dev/null
+package org.ibex.xt.shell;
+
+import java.util.*;
+
+import java.io.Writer;
+import java.io.IOException;
+
+public abstract class Command {
+ /** Returns the command name. */
+ public abstract String name();
+
+ /** Returns single-line of usage information, eg. <tt>pattern]</tt> */
+ public abstract String usage();
+
+ /** Returns single-line description of command. */
+ public abstract String summary();
+
+ /** Returns multi-line description. */
+ public abstract String help();
+
+ /** Writes result of execution, even if result is an error. */
+ public abstract void execute(Writer w, String[] args, Env env) throws IOException;
+
+ public static class Help extends Command {
+ public String name() { return "help"; }
+ public String usage() { return "[command name]"; }
+ public String summary() { return "Lists available commands."; }
+ public String help() { return ""; }
+
+ public void execute(Writer w, String[] c, Env env) throws IOException {
+ if (c.length > 1) {
+ Command cmd = env.command(c[1]);
+ if (c == null) {
+ w.write("help: ");
+ w.write(c[1]);
+ w.write(": command not found\n");
+ } else {
+ w.write("usage: ");
+ w.write(cmd.name());
+ w.write(" ");
+ w.write(cmd.usage());
+ w.write("\n\n");
+ w.write(cmd.help());
+ w.write("\n");
+ }
+ } else {
+ int len = 3;
+ for (int i=0; i < env.commands.length; i++)
+ len = Math.max(env.commands[i].name().length(), len);
+
+ w.write("Available commands:\n");
+ for (int i=0; i < env.commands.length; i++) {
+ Command cmd = env.commands[i];
+ w.write(" ");
+ w.write(cmd.name());
+ for (int j=len - cmd.name().length(); j >= 0; j--) w.write(" ");
+ w.write(" - ");
+ w.write(cmd.summary());
+ w.write("\n");
+ }
+ w.write("\nFor usage details, type help [command name].\n");
+ }
+ }
+ }
+
+ public static class Ls extends Command {
+ public String name() { return "ls"; }
+ public String usage() { return "[path]"; }
+ public String summary() { return "List object entries."; }
+ public String help() { return
+ "Lists the keys in an object. Modelled after the UNIX ls command.";
+ }
+
+ public void execute(Writer w, String[] c, Env env) throws IOException {
+ if (c.length > 2) { w.write(usage()); return; }
+ String p = env.path(c.length == 1 ? "*" : c[1]);
+
+ Request.Response ret = env.send(new Request.Key(p));
+ if (!(ret instanceof Request.Key.Res)) {
+ w.write("error: ");
+ w.write(ret.error().getMessage());
+ w.write("\n");
+ } else {
+ List l = ((Request.Key.Res)ret).keys();
+ Iterator i = l.iterator(); while (i.hasNext()) {
+ w.write(i.next().toString());
+ w.write("\n");
+ }
+ }
+ }
+ }
+
+ public static class Pwd extends Command {
+ public String name() { return "pwd"; }
+ public String usage() { return ""; }
+ public String summary() { return "Path to current object."; }
+ public String help() { return "Print the path to the current object."; }
+ public void execute(Writer w, String[] c, Env env) throws IOException {
+ if (c.length != 1) { w.write(usage()); return; }
+ w.write(env.path.equals("") ? "/" : env.path.replace('.', '/'));
+ w.write("\n");
+ }
+ }
+
+ public static class Cd extends Command {
+ public String name() { return "cd"; }
+ public String usage() { return "[path]"; }
+ public String summary() { return "Change current object."; }
+ public String help() { return
+ "Chnages the current object that all other commands use "+
+ "as the base for running.\n Pass either a relative path "+
+ "(e.g. in /prevalent, type cd myob, now in /prevalent/myob) "+
+ "or an absolute path (e.g. cd /prevalent/myob).\n\n" +
+ "To go up one level, cd .. can be used.";
+ }
+ public void execute(Writer w, String[] c, Env env) throws IOException {
+ if (c.length > 2) w.write(usage());
+ else if (c.length == 1 || c[1].equals("") || c[1].equals("/")) env.path = "";
+ else if (c[1].equals("..")) {
+ String n = env.path;
+ n = n.substring(0, n.lastIndexOf('.'));
+ env.path = n;
+ } else {
+ String n = env.path(c[1]);
+
+ Request.Response ret = env.send(new Request.Key(n));
+ if (!(ret instanceof Request.Key.Res)) {
+ w.write("error: ");
+ w.write(ret.error().getMessage());
+ w.write("\n");
+ } else {
+ List l = ((Request.Key.Res)ret).keys();
+ if (l.size() == 0) {
+ w.write("cd ");
+ w.write(c[1]);
+ w.write(": no such path\n");
+ } else {
+ env.path = n;
+ }
+ }
+ }
+ }
+ }
+
+ public static class Rm extends Command {
+ public String name() { return "rm"; }
+ public String usage() { return "[options] [path]"; }
+ public String summary() { return "Removes objects."; }
+ public String help() { return "Removes objects."; } // FIXME
+ public void execute(Writer w, String[] c, Env env) throws IOException {
+ if (c.length == 1) { w.write(usage()); }
+
+ String[] r = new String[c.length - 1];
+ for (int i=0; i < r.length; i++) r[i] = env.path(c[i + 1]);
+ // Object ret = send(new KeyRequest( FIXME: CompositeRequest
+ }
+ }
+
+
+}