+ } catch (Shell.BadPathException e) {
+ w.write("error: cannot replace '");
+ w.write(c[1]);
+ w.write("': ");
+ w.write(e.getMessage() == null ? "no such path" : e.getMessage());
+ w.write("\n");
+ }
+ }
+ }
+
+ public static class Mkdir extends Command {
+ public Mkdir(Shell s) { super(s); }
+ public String name() { return "mkdir"; }
+ public String params() { return "[path]"; }
+ public String summary() { return "Creates a new object ready to handle keys."; }
+ public String help() { return
+ "Creates a new object ready to handle keys. This function " +
+ "is similar to calling replace [path] {}, only it will not " +
+ "overwrite an existing key.";
+ }
+ public void execute(Writer w, String[] c) throws IOException {
+ if (c.length != 2) { usage(w); return; }
+ try {
+ List path = fromShellPath(c[1]);
+ Object key = path.remove(path.size() - 1);
+ Object po = shell.getFromPath(path.toArray());
+
+ if (po == null || !(po instanceof JS))
+ throw new Shell.BadPathException();
+
+ JS parent = (JS)po;
+ if (parent.containsKey(key))
+ throw new Shell.BadPathException("already exists");
+
+ String func = "prevalent.";
+ for (int i=0; i < path.size(); i++)
+ func += path.get(i) + ".";
+ func += key + " = {};\n";
+
+ shell.transaction(JS.fromReader(
+ "mkdir-transaction", 0, new StringReader(func)));
+
+ } catch (JSExn e) {
+ w.write("error: cannot create '");
+ w.write(c[1]);
+ w.write("': ");
+ w.write(e.getMessage());
+ w.write("\n");
+ } catch (Shell.BadPathException e) {
+ w.write("error: cannot create '");
+ w.write(c[1]);
+ w.write("': ");
+ w.write(e.getMessage() == null ? "no such path" : e.getMessage());
+ w.write("\n");
+ }
+ }
+ }
+
+ public static class Rm extends Command {
+ public Rm(Shell s) { super(s); }
+ public String name() { return "rm"; }
+ public String params() { return "[options] [path]"; }
+ public String summary() { return "Removes objects."; }
+ public String help() { return
+ "Removes objects. If any one of the specified paths does " +
+ "not exist, the entire remove process is cancelled."; }
+ public void execute(Writer w, String[] c) throws IOException {
+ if (c.length == 1) { usage(w); return; }
+
+ boolean force = false; // FIXME provide ability to set
+
+ StringBuffer func = new StringBuffer();
+
+ for (int ic=1; ic < c.length; ic++) {
+ String p = c.length == 1 ? "*" : c[ic];
+ if (p.endsWith("/")) p += "*";
+
+ try {
+ // get the base of the path
+ List path = fromShellPath(p);
+ Object key = path.remove(path.size() - 1);
+ Object po = shell.getFromPath(path.toArray());
+ if (po == null || !(po instanceof JS))
+ throw new Shell.BadPathException();
+ JS cur = (JS)po;
+
+ if (cur.containsKey(key)) {
+ Object o = cur.get(key);
+ if (!force && o != null && o instanceof JS && ((JS)o).keys().size() > 0)
+ throw new Shell.BadPathException("key is not empty");
+ func.append("prevalent.");
+ for(int i=0; i < path.size(); i++) {
+ func.append(path.get(i)); func.append('.'); }
+ func.append("Delete(\"");
+ func.append(key.toString());
+ func.append("\");\n");
+ } else if (key instanceof String && ((String)key).indexOf('*') >= 0) {
+ String last = (String)key;
+ last = last.replaceAll("\\.", "\\.");
+ last = last.replaceAll("\\*", ".*");
+ Pattern pat = Pattern.compile(last);
+ Collection curkeys = cur.keys();
+ if (curkeys.size() == 0) throw new Shell.BadPathException();
+ Iterator it = curkeys.iterator(); while (it.hasNext()) {
+ Object o = it.next();
+ if (o == null || !(o instanceof String)) continue;
+ String s = (String)o;
+ if (!pat.matcher(s).matches()) continue;
+
+ func.append("prevalent.");
+ for(int i=0; i < path.size(); i++) {
+ func.append(path.get(i)); func.append('.'); }
+ func.append("Delete(\"");
+ func.append(s);
+ func.append("\");\n");
+ }
+ } else throw new Shell.BadPathException();
+
+ } catch (JSExn e) {
+ w.write("error: cannot remove '");
+ w.write(p);
+ w.write("': ");
+ w.write(e.getMessage());
+ w.write("\n");
+ return;
+ } catch (Shell.BadPathException e) {
+ w.write("error: cannot remove '");
+ w.write(p);
+ w.write("': ");
+ w.write(e.getMessage() != null ? e.getMessage() : "no such path");