introduce cd function into shell
authorcrawshaw <crawshaw@ibex.org>
Sat, 27 Nov 2004 20:12:01 +0000 (20:12 +0000)
committercrawshaw <crawshaw@ibex.org>
Sat, 27 Nov 2004 20:12:01 +0000 (20:12 +0000)
darcs-hash:20041127201201-2eb37-3f9bb0def3068caa86a1b04b28d446cd6b3a8234.gz

src/java/org/ibex/xt/Shell.java

index 1596630..38756d9 100644 (file)
@@ -31,6 +31,7 @@ public class Shell {
     protected Command[] commands = new Command[] {
         new LsCommand(),
         new PwdCommand(),
+        new CdCommand(),
         new HelpCommand()
     };
 
@@ -69,8 +70,7 @@ public class Shell {
                 String[] c = buffer.split(" ");
                 int i=0; while (i < commands.length) {
                     if (commands[i].name().equals(c[0])) {
-                        commands[i].execute(out, c);
-                        out.write('\n'); break;
+                        commands[i].execute(out, c); break;
                     }
                     i++;
                 }
@@ -145,7 +145,7 @@ public class Shell {
                 if (c == null) {
                     w.write("help: ");
                     w.write(c[1]);
-                    w.write(": command not found");
+                    w.write(": command not found\n");
                 } else {
                     w.write("usage: ");
                     w.write(cmd.name());
@@ -170,7 +170,7 @@ public class Shell {
                     w.write(cmd.summary());
                     w.write("\n");
                 }
-                w.write("\nFor usage details, type help [command name].");
+                w.write("\nFor usage details, type help [command name].\n");
             }
         }
     }
@@ -180,8 +180,7 @@ public class Shell {
         public String usage() { return "[path]"; }
         public String summary() { return "List object entries."; }
         public String help() { return
-            "The ls command, modelled after the UNIX ls command lists the " +
-            "keys in an object. ";
+            "Lists the keys in an object. Modelled after the UNIX ls command.";
         }
 
         public void execute(Writer w, String[] c) throws IOException {
@@ -191,8 +190,8 @@ public class Shell {
             String matcher = ".*";
             if (c.length == 2) {
                 int pos = c[1].lastIndexOf('/') + 1;
-                path += c[1].substring(0, pos);
-                path = path.replaceAll("/", "\\.");
+                path += "." + c[1].substring(0, pos);
+                path = path.replaceAll("/+", ".");
                 if (pos < c[1].length()) {
                     matcher = c[1].substring(pos);
                     matcher = matcher.replaceAll("\\.", "\\.");
@@ -202,21 +201,29 @@ public class Shell {
 
             Object ret = send(new KeyRequest(path, matcher));
             if (ret == null) {
-                w.write("error: (unexpected) returned object is null");
+                w.write("error: (unexpected) returned object is null\n");
             } else if (ret instanceof JSExn) {
-                w.write("error: ");
-                w.write(((JSExn)ret).getMessage());
+                String e = ((JSExn)ret).getMessage();
+                // FIXME: messy way to get info from server
+                if (e.endsWith("does not exist")) {
+                    w.write("ls ");
+                    w.write(c[1]);
+                    w.write(": no such path\n");
+                } else {
+                    w.write("error: ");
+                    w.write(e);
+                    w.write("\n");
+                }
             } else if (ret instanceof List) {
                 List l = (List)ret; Collections.sort(l);
-                w.write("total items: ");
-                w.write(l.size()+"");
                 Iterator i = l.iterator(); while (i.hasNext()) {
-                    w.write("\n  ");
                     w.write(i.next().toString());
+                    w.write("\n");
                 }
             } else {
                 w.write("error: (unexpected) returned object is of unknown type: ");
                 w.write(ret.getClass().getName());
+                w.write("\n");
             }
         }
     }
@@ -228,6 +235,54 @@ public class Shell {
         public String help() { return "Print the path to the current object."; }
         public void execute(Writer w, String[] c) throws IOException {
             w.write(c.length == 1 ? pwd.replace('.', '/') : usage());
+            w.write("\n");
+        }
+    }
+
+    public class CdCommand 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. 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) throws IOException {
+            if (c.length > 2) w.write(usage());
+            else if (c.length == 1 || c[1].equals("") || c[1].equals("/")) pwd = ".";
+            else if (c[1].equals("..")) {
+                String n = pwd.substring(0, pwd.lastIndexOf('.'));
+                pwd = n.equals("") ? "." : n;
+            } else {
+                String n = c[1];
+                if (n.charAt(0) != '/') n = pwd + n;
+                n = n.replaceAll("/+", ".");
+                if (n.length() > 1 && n.charAt(n.length() - 1) == '.')
+                    n = n.substring(0, n.length() - 1);
+
+                int pos = n.lastIndexOf('.');
+                String path = n.substring(0, pos);
+                String matcher = n.substring(pos + 1);
+                Object ret = send(new KeyRequest(path, matcher));
+
+                if (ret == null) {
+                    w.write("error: (unexpected) server returned null\n");
+                } else if (ret instanceof List && ((List)ret).size() == 1) {
+                    pwd = n;
+                } else if (ret instanceof JSExn ||
+                           (ret instanceof List && ((List)ret).size() == 0)) {
+                    w.write("cd ");
+                    w.write(c[1]);
+                    w.write(": no such path\n");
+                } else {
+                    w.write("error: (unexpected) server returned ");
+                    w.write(ret.toString());
+                    w.write("\n");
+                }
+            }
         }
     }