4 import dolda.jsvc.util.*;
6 import bsh.Interpreter;
8 public class ShellPage extends SimpleWriter {
9 private Console cons = new Console();
10 private Interpreter ip = new Interpreter(cons);
12 private static class Console implements bsh.ConsoleInterface {
13 ByteArrayOutputStream obuf = new ByteArrayOutputStream();
14 ByteArrayOutputStream ebuf = new ByteArrayOutputStream();
15 Reader in = new StringReader("");
20 out = new PrintStream(obuf, false, "UTF-8");
21 err = new PrintStream(ebuf, false, "UTF-8");
22 } catch(UnsupportedEncodingException e) {
27 public void error(Object msg) {
28 getErr().println(msg);
31 public void print(Object o) {
35 public void println(Object o) {
39 public PrintStream getOut() {
43 public PrintStream getErr() {
47 public Reader getIn() {
57 public void respond(Request req, PrintWriter out) {
58 MultiMap<String, String> params = req.params();
59 String cmd = params.get("cmd");
61 out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
62 out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
63 out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en-US\">");
64 out.println("<head>");
65 out.println("<title>Shell</title>");
66 out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"css\" />");
67 out.println("</head>");
68 out.println("<body>");
69 out.println("<h1>Shell</h1>");
70 if((req.method() == "POST") && (cmd != null)) {
79 out.println(Misc.htmlq((resp == null)?"(null)":(resp.toString())));
80 out.println("</pre>");
81 } catch(bsh.EvalError exc) {
82 out.println("<h2>Evaluation error</h2>");
84 out.print(exc.toString());
85 out.println("</pre>");
86 if(exc instanceof bsh.TargetError) {
87 bsh.TargetError te = (bsh.TargetError)exc;
88 out.println("<h3>Target error</h3>");
90 te.getTarget().printStackTrace(out);
91 out.println("</pre>");
94 eo = new String(cons.obuf.toByteArray(), Misc.utf8);
95 ee = new String(cons.ebuf.toByteArray(), Misc.utf8);
98 out.println("<h2>Output</h2>");
100 out.println(Misc.htmlq(eo));
101 out.println("</pre>");
103 if(ee.length() > 0) {
104 out.println("<h2>Errors</h2>");
105 out.println("<pre>");
106 out.println(Misc.htmlq(ee));
107 out.println("</pre>");
110 out.println("<form action=\"sh\" method=\"post\">");
111 out.println("<textarea cols=\"80\" rows=\"5\" name=\"cmd\">");
114 out.println("</textarea>");
115 out.println("<input type=\"submit\" value=\"Evaluate\" />");
116 out.println("<input type=\"reset\" value=\"Reset\" />");
117 out.println("</form>");
118 out.println("</body>");
119 out.println("</html>");