4 import dolda.jsvc.util.*;
6 import bsh.Interpreter;
8 public class ShellPage extends SimpleWriter {
9 private RConsole cons = new RConsole();
10 private Interpreter ip = new Interpreter(cons);
12 private static class RConsole implements bsh.ConsoleInterface {
14 Reader in = new StringReader("");
16 public void error(Object msg) {
21 public void print(Object msg) {
26 public void println(Object msg) {
31 public PrintStream getOut() {
34 return(back.getOut());
37 public PrintStream getErr() {
40 return(back.getErr());
43 public Reader getIn() {
50 private static class Console implements bsh.ConsoleInterface {
51 ByteArrayOutputStream obuf = new ByteArrayOutputStream();
52 ByteArrayOutputStream ebuf = new ByteArrayOutputStream();
53 Reader in = new StringReader("");
58 out = new PrintStream(obuf, false, "UTF-8");
59 err = new PrintStream(ebuf, false, "UTF-8");
60 } catch(UnsupportedEncodingException e) {
65 public void error(Object msg) {
66 getErr().println(msg);
69 public void print(Object o) {
73 public void println(Object o) {
77 public PrintStream getOut() {
81 public PrintStream getErr() {
85 public Reader getIn() {
90 public void respond(Request req, PrintWriter out) {
91 MultiMap<String, String> params = req.params();
92 String cmd = params.get("cmd");
94 out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
95 out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
96 out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en-US\">");
97 out.println("<head>");
98 out.println("<title>Shell</title>");
99 out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"css\" />");
100 out.println("</head>");
101 out.println("<body>");
102 out.println("<h1>Shell</h1>");
103 if((req.method() == "POST") && (cmd != null)) {
104 Console cons = new Console();
106 this.cons.back = cons;
112 out.println("<pre>");
113 out.println(Misc.htmlq((resp == null)?"(null)":(resp.toString())));
114 out.println("</pre>");
115 } catch(bsh.EvalError exc) {
116 out.println("<h2>Evaluation error</h2>");
117 out.println("<pre>");
118 out.print(exc.toString());
119 out.println("</pre>");
120 if(exc instanceof bsh.TargetError) {
121 bsh.TargetError te = (bsh.TargetError)exc;
122 out.println("<h3>Target error</h3>");
123 out.println("<pre>");
124 te.getTarget().printStackTrace(out);
125 out.println("</pre>");
129 this.cons.back = null;
132 String eo = new String(cons.obuf.toByteArray(), Misc.utf8);
133 String ee = new String(cons.ebuf.toByteArray(), Misc.utf8);
135 if(eo.length() > 0) {
136 out.println("<h2>Output</h2>");
137 out.println("<pre>");
138 out.println(Misc.htmlq(eo));
139 out.println("</pre>");
141 if(ee.length() > 0) {
142 out.println("<h2>Errors</h2>");
143 out.println("<pre>");
144 out.println(Misc.htmlq(ee));
145 out.println("</pre>");
148 out.println("<form action=\"sh\" method=\"post\">");
149 out.println("<textarea cols=\"80\" rows=\"5\" name=\"cmd\">");
152 out.println("</textarea>");
153 out.println("<input type=\"submit\" value=\"Evaluate\" />");
154 out.println("<input type=\"reset\" value=\"Reset\" />");
155 out.println("</form>");
156 out.println("</body>");
157 out.println("</html>");