From 6430dcd3c8bf58a04a370614e375a74f5f2dce8b Mon Sep 17 00:00:00 2001 From: crawshaw Date: Sun, 28 Nov 2004 22:03:40 +0000 Subject: [PATCH] add set command and switch completely to . as an object seperator darcs-hash:20041128220340-2eb37-fdf4914c2ca2df08b4926376d7820f3a6d72dc9e.gz --- src/java/org/ibex/xt/shell/Command.java | 42 +++++++++++++++++++++++++------ src/java/org/ibex/xt/shell/Env.java | 6 ++--- src/java/org/ibex/xt/shell/Request.java | 39 ++++++++++++++++++++++++++++ src/java/org/ibex/xt/shell/Shell.java | 28 ++++++++++++++++++--- 4 files changed, 100 insertions(+), 15 deletions(-) diff --git a/src/java/org/ibex/xt/shell/Command.java b/src/java/org/ibex/xt/shell/Command.java index 238f8af..f5ad6fb 100644 --- a/src/java/org/ibex/xt/shell/Command.java +++ b/src/java/org/ibex/xt/shell/Command.java @@ -1,10 +1,13 @@ package org.ibex.xt.shell; -import java.util.*; - +import java.io.StringReader; import java.io.Writer; import java.io.IOException; +import java.util.*; + +import org.ibex.js.*; + public abstract class Command { /** Returns the command name. */ public abstract String name(); @@ -97,7 +100,7 @@ public abstract class Command { 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(env.path.equals("") ? "." : env.path); w.write("\n"); } } @@ -109,13 +112,13 @@ public abstract class Command { 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" + + "(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.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('.')); @@ -177,11 +180,34 @@ public abstract class Command { } } } + } + } + + public static class Set extends Command { + public String name() { return "set"; } + public String usage() { return "[name] [object]"; } + public String summary() { return "Sets ."; } + public String help() { return "Removes objects."; } // FIXME + + public void execute(Writer w, String[] c, Env env) throws IOException { + if (c.length < 3) { w.write(usage()); } + + String m = c[1]; + String s = "return ("; + for (int i=2; i < c.length; i++) s += c[i]; + s += ");"; + JS js; + try { js = JS.fromReader("input", 0, new StringReader(s)); } + catch (IOException e) { + w.write("error: "); + w.write(e.getMessage()); + w.write("\n"); + return; + } - // Object ret = send(new KeyRequest( FIXME: CompositeRequest + Request.Response ret = env.send(new Request.SetKey(env.path, m, js)); } } - } diff --git a/src/java/org/ibex/xt/shell/Env.java b/src/java/org/ibex/xt/shell/Env.java index 6f37146..f09cc95 100644 --- a/src/java/org/ibex/xt/shell/Env.java +++ b/src/java/org/ibex/xt/shell/Env.java @@ -17,14 +17,14 @@ public abstract class Env { /** Returns a path, based on console-style representation. */ public String path(String c) { - if (c.equals("") || c.equals(".") || c.equals("/")) { + if (c.equals("") || c.equals(".")) { c = "."; } else if (c.equals("..")) { + c = path; c = c.substring(0, c.lastIndexOf('.')); if (c.equals("")) c = "."; } else { - if (c.charAt(0) != '/') c = path + "." + c; - c = c.replaceAll("/+", "."); + if (c.charAt(0) != '.') c = path + "." + c; if (c.length() > 1 && c.charAt(c.length() - 1) == '.') c = c.substring(0, c.length() - 1); } diff --git a/src/java/org/ibex/xt/shell/Request.java b/src/java/org/ibex/xt/shell/Request.java index f0d3697..2f85247 100644 --- a/src/java/org/ibex/xt/shell/Request.java +++ b/src/java/org/ibex/xt/shell/Request.java @@ -100,6 +100,45 @@ public abstract class Request implements Serializable { } } + public static class SetKey extends Key { + protected JS value; + public SetKey() {} + public SetKey(String p, String m, JS val) { super(p, m); this.value = val; } + + public Response process(JSScope root) throws JSExn { + JS js = keyed(path(root)); + if (js == null) throw new JSExn("no such path"); + + js.put(matcher, JS.eval(JS.cloneWithNewParentScope( + value, new JSResolve(root, js)))); + return new Res(); // TODO: return put() value when it has one + } + + public class Res extends Response { + } + + public class JSResolve extends JSScope { + private JS path; + public JSResolve(JSScope parent, JS path) { super(parent); this.path = path; } + public Object get(Object key) throws JSExn { + if (key != null && key instanceof String) { + String k = (String)key; + if (k.startsWith(".")) k = k.substring(1); + else return path.get(k); + } + return super.get(key); + } + public void put(Object key, Object val) throws JSExn { + if (key != null && key instanceof String) { + String k = (String)key; + if (k.startsWith(".")) k = k.substring(1); + else { path.put(key, val); return; } + } + super.put(key, val); + } + } + } + /** Runs a series of requests. */ public static class Composite extends Request { private boolean breakOnError = false; diff --git a/src/java/org/ibex/xt/shell/Shell.java b/src/java/org/ibex/xt/shell/Shell.java index 746479e..17a2f57 100644 --- a/src/java/org/ibex/xt/shell/Shell.java +++ b/src/java/org/ibex/xt/shell/Shell.java @@ -36,6 +36,7 @@ public class Shell extends Env { new Command.Pwd(), new Command.Cd(), new Command.Rm(), + new Command.Set(), new Command.Help() }; } @@ -50,16 +51,35 @@ public class Shell extends Env { String line; String buffer = ""; + int countBraceOpen = 0, countBraceClose = 0; + int countBracketOpen = 0, countBracketClose = 0; + int countSQBracketOpen = 0, countSQBracketClose = 0; while ((line = in.readLine()) != null) { if (line.length() > 0) { if (line.startsWith("exit")) return; - if (line.charAt(line.length() - 1) == '\\') { - buffer += line.substring(0, line.length() - 1); - out.print('>'); - out.flush(); continue; + + for (int i=0; i < line.length(); i++) { + switch (line.charAt(i)) { + case '{': countBraceOpen++; break; + case '}': countBraceClose++; break; + case '(': countBracketOpen++; break; + case ')': countBracketClose++; break; + case '[': countSQBracketOpen++; break; + case ']': countSQBracketClose++; break; + } } + boolean nonendchar = line.charAt(line.length() - 1) == '\\'; + if (nonendchar) line = line.substring(0, line.length() - 1); buffer += line; + + if (nonendchar || + countBracketOpen != countBracketClose || + countBraceOpen != countBraceClose || + countSQBracketOpen != countSQBracketClose) { + out.print('>'); + out.flush(); continue; + } } if (buffer.length() > 0) { -- 1.7.10.4