add rm command
authorcrawshaw <crawshaw@ibex.org>
Sun, 28 Nov 2004 13:14:36 +0000 (13:14 +0000)
committercrawshaw <crawshaw@ibex.org>
Sun, 28 Nov 2004 13:14:36 +0000 (13:14 +0000)
darcs-hash:20041128131436-2eb37-4e988a500d9e15f46f97d7705da9f54d8d1b143a.gz

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

index 38756d9..4696869 100644 (file)
@@ -32,6 +32,7 @@ public class Shell {
         new LsCommand(),
         new PwdCommand(),
         new CdCommand(),
+        new RmCommand(),
         new HelpCommand()
     };
 
@@ -85,6 +86,23 @@ public class Shell {
         }
     }
 
+    /** Returns a path, based on console input. */
+    private String path(String c) {
+        if (c.equals("") || c.equals(".") || c.equals("/")) {
+            c = ".";
+        } else if (c.equals("..")) {
+            c = c.substring(0, c.lastIndexOf('.'));
+            if (c.equals("")) c = ".";
+        } else {
+            if (c.charAt(0) != '/') c = pwd + c;
+            c = c.replaceAll("/+", ".");
+            if (c.length() > 1 && c.charAt(c.length() - 1) == '.')
+                c = c.substring(0, c.length() - 1);
+        }
+
+        return c;
+    }
+
     private String cookie = null;
     public Object send(Request request) throws IOException {
         URLConnection c = server.openConnection();
@@ -186,20 +204,7 @@ public class Shell {
         public void execute(Writer w, String[] c) throws IOException {
             if (c.length > 2) { w.write(usage()); return; }
 
-            String path = pwd;
-            String matcher = ".*";
-            if (c.length == 2) {
-                int pos = c[1].lastIndexOf('/') + 1;
-                path += "." + c[1].substring(0, pos);
-                path = path.replaceAll("/+", ".");
-                if (pos < c[1].length()) {
-                    matcher = c[1].substring(pos);
-                    matcher = matcher.replaceAll("\\.", "\\.");
-                    matcher = matcher.replaceAll("\\*", ".*");
-                }
-            }
-
-            Object ret = send(new KeyRequest(path, matcher));
+            Object ret = send(new KeyRequest(path(c[1])));
             if (ret == null) {
                 w.write("error: (unexpected) returned object is null\n");
             } else if (ret instanceof JSExn) {
@@ -245,7 +250,7 @@ public class Shell {
         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 "+
+            "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.";
@@ -257,16 +262,8 @@ public class Shell {
                 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));
+                String n = path(c[1]);
+                Object ret = send(new KeyRequest(n));
 
                 if (ret == null) {
                     w.write("error: (unexpected) server returned null\n");
@@ -286,6 +283,20 @@ public class Shell {
         }
     }
 
+    public class RmCommand 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) 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] = path(c[i + 1]);
+            // Object ret = send(new KeyRequest( FIXME: CompositeRequest
+        }
+    }
+
     public static abstract class Request implements Serializable {
         public abstract Object process(JSScope root) throws JSExn;
     }
@@ -293,6 +304,11 @@ public class Shell {
     public static class KeyRequest extends Request {
         private String path, matcher;
         public KeyRequest() {}
+        public KeyRequest(String c) {
+            int pos = c.lastIndexOf('.');
+            path = c.substring(0, pos);
+            matcher = c.substring(pos + 1).replaceAll("\\*+", ".*");
+        }
         public KeyRequest(String path, String matcher) {
             this.path = path; this.matcher = matcher;
         }
index bc53366..e9fc0a8 100644 (file)
@@ -1,14 +1,9 @@
 package org.ibex.xt;
 
 import java.io.*;
-import java.net.*;
-import java.util.*;
-import java.util.regex.*;
 import javax.servlet.*;
 import javax.servlet.http.*;
 
-import org.ibex.util.*;
-import org.ibex.util.Collections;
 import org.ibex.js.*;
 
 import org.prevayler.*;
@@ -36,10 +31,16 @@ public class ShellServlet extends HttpServlet {
 
         JSScope scope = (JSScope)rq.getSession().getAttribute("scope");
         if (scope == null) {
-            System.out.println("creating new scope");
             try {
-                scope = new JSScope(null);
-                scope.put("prevalent", prevalent);
+                scope = new JSScope(null) {
+                    { super.put("prevalent", prevalent); }
+
+                    public void put(String k, String v) throws JSExn {
+                        if (k != null && k.equals("prevalent")) throw new JSExn(
+                            "can not replace key prevalent");
+                        super.put(k, v);
+                    }
+                };
             } catch (JSExn e) {
                 e.printStackTrace();
                 throw new IOException("unexpected JSExn");