From fc9f528e7acb1baf7c145b22dd0b6469968aaec0 Mon Sep 17 00:00:00 2001 From: crawshaw Date: Sun, 28 Nov 2004 18:32:35 +0000 Subject: [PATCH] bugfixes and working rm command darcs-hash:20041128183235-2eb37-3eac789a749c1501ffcd06a636f9f75ee0f5c680.gz --- src/java/org/ibex/xt/shell/Command.java | 33 ++++++++++- src/java/org/ibex/xt/shell/Request.java | 95 +++++++++++++++++++++++-------- 2 files changed, 102 insertions(+), 26 deletions(-) diff --git a/src/java/org/ibex/xt/shell/Command.java b/src/java/org/ibex/xt/shell/Command.java index 0ff2952..238f8af 100644 --- a/src/java/org/ibex/xt/shell/Command.java +++ b/src/java/org/ibex/xt/shell/Command.java @@ -131,7 +131,7 @@ public abstract class Command { } else { List l = ((Request.Key.Res)ret).keys(); if (l.size() == 0) { - w.write("cd "); + w.write("error: "); w.write(c[1]); w.write(": no such path\n"); } else { @@ -150,8 +150,35 @@ public abstract class Command { 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]); + Request.Key[] r = new Request.Key[c.length - 1]; + for (int i=0; i < r.length; i++) r[i] = new Request.RemoveKey(env.path(c[i + 1])); + + Request.Response ret = env.send(new Request.Composite(r, false)); + if (!(ret instanceof Request.Composite.Res)) { + w.write("error: "); + w.write(ret.error().getMessage()); + w.write("\n"); + } else { + Request.Response[] res = ((Request.Composite.Res)ret).responses(); + for (int i=0; i < res.length; i++) { + if (res[i] instanceof Request.RemoveKey.Res) { + boolean rm = ((Request.RemoveKey.Res)res[i]).removed(); + if (!rm) { + w.write("error: cannot remove '"); + w.write(c[i + 1]); + w.write("': no such key\n"); + } + } else { + w.write("error: cannot remove '"); + w.write(c[i + 1]); + w.write("': "); + w.write(res[i].error().getMessage()); + w.write("\n"); + } + } + } + + // Object ret = send(new KeyRequest( FIXME: CompositeRequest } } diff --git a/src/java/org/ibex/xt/shell/Request.java b/src/java/org/ibex/xt/shell/Request.java index eec18b7..f0d3697 100644 --- a/src/java/org/ibex/xt/shell/Request.java +++ b/src/java/org/ibex/xt/shell/Request.java @@ -18,44 +18,58 @@ public abstract class Request implements Serializable { } public static class Key extends Request { - private String path, matcher; + protected String path, matcher; public Key() {} + + /** Expects a shell-path that uses '.' as a seperator and * for wildcard. */ public Key(String c) { int pos = c.lastIndexOf('.'); path = c.substring(0, pos); matcher = c.substring(pos + 1).replaceAll("\\*+", ".*"); } + public Key(String path, String matcher) { this.path = path; this.matcher = matcher; } - public Response process(JSScope root) throws JSExn { + /** Returns the object referenced by path. */ + protected Object path(JSScope root) throws JSExn { String p = path == null ? "" : path.replaceAll("\\.+", "\\."); if (p.length() > 0 && p.charAt(0) == '.') p = p.substring(1); if (p.length() > 0 && p.charAt(p.length() - 1) == '.') p = p.substring(0, p.length() - 1); - System.out.println("searching path '"+p+"' for pattern '"+matcher+"'"); - - Object o = p.equals("") ? root : root.get(p); - if (o == null || o instanceof JSDate || - o instanceof JSArray || - !(o instanceof JS)) { - System.out.println("hit bad object: "+o+", class="+ - (o == null ? null : o.getClass().getName())); - return new Key.Res(); - } else { - Pattern pat = Pattern.compile(matcher); - List keys = new ArrayList(); - - Iterator i = ((JS)o).keys().iterator(); while(i.hasNext()) { - String k = i.next().toString(); - if (pat.matcher(k).matches()) keys.add(k); - } + return p.equals("") ? root : root.get(p); + } + + /** Returns the keys in js that match matcher. */ + protected List matches(JS js) throws JSExn { + Pattern pat = Pattern.compile(matcher); + List keys = new ArrayList(); - return new Res(keys); + Iterator i = js.keys().iterator(); while(i.hasNext()) { + String k = i.next().toString(); + if (pat.matcher(k).matches()) keys.add(k); } + + return keys; + } + + /** Returns o cast as a JS if it is such and has keys, + * otherwise returns null. */ + protected JS keyed(Object o) { + // FIXME: replace this with a canHaveKeys() function in org.ibex.js.JS + return o == null || !(o instanceof JS) || + o instanceof JSDate || o instanceof Directory || + o instanceof Grammar || o instanceof JSMath || + o instanceof JSReflection || o instanceof JSRegexp ? + null : (JS)o; + } + + public Response process(JSScope root) throws JSExn { + JS js = keyed(path(root)); + return js == null ? new Res() : new Res(matches(js)); } public static class Res extends Response { @@ -67,14 +81,39 @@ public abstract class Request implements Serializable { } } + public static class RemoveKey extends Key { + public RemoveKey(String c) { super(c); } + public RemoveKey(String p, String m) { super(p, m); } + public Response process(JSScope root) throws JSExn { + JS js = keyed(path(root)); + if (js == null) throw new JSExn("no such path"); + boolean rm = js.containsKey(matcher); + if (rm) js.remove(matcher); + return new Res(rm); + } + + public static class Res extends Response { + private boolean removed; + public Res() { } + public Res(boolean rm) { removed = rm; } + public boolean removed() { return removed; } + } + } + + /** Runs a series of requests. */ public static class Composite extends Request { + private boolean breakOnError = false; private Request[] requests; public Composite() {} - public Composite(Request[] r) { requests = r; } - public Composite(List r) { + public Composite(Request[] r, boolean breakOnError) { + requests = r; + this.breakOnError = breakOnError; + } + public Composite(List r, boolean breakOnError) { Request[] req = new Request[r.size()]; r.toArray(req); requests = req; + this.breakOnError = breakOnError; } public Response process(JSScope root) { @@ -82,7 +121,16 @@ public abstract class Request implements Serializable { for (int i=0; i < requests.length; i++) { try { res[i] = requests[i].process(root); } - catch (JSExn e) { res[i] = new Response(e); } + catch (JSExn e) { + res[i] = new Response(e); + + if (breakOnError) { + Response[] newres = new Response[i + 1]; + System.arraycopy(res, 0, newres, 0, newres.length); + res = newres; + break; + } + } } return new Res(res); @@ -94,6 +142,7 @@ public abstract class Request implements Serializable { public Res(Response[] r) { responses = r; } public Response get(int i) { return responses[i]; } public int size() { return responses.length; } + public Response[] responses() { return responses; } } } } -- 1.7.10.4