4 import dolda.jsvc.util.*;
6 import bsh.Interpreter;
8 public class ShellPage extends SimpleWriter {
9 private static class Console implements bsh.ConsoleInterface {
10 ByteArrayOutputStream obuf = new ByteArrayOutputStream();
11 ByteArrayOutputStream ebuf = new ByteArrayOutputStream();
12 Reader in = new StringReader("");
17 out = new PrintStream(obuf, false, "UTF-8");
18 err = new PrintStream(ebuf, false, "UTF-8");
19 } catch(UnsupportedEncodingException e) {
24 public void error(Object msg) {
25 getErr().println(msg);
28 public void print(Object o) {
32 public void println(Object o) {
36 public PrintStream getOut() {
40 public PrintStream getErr() {
44 public Reader getIn() {
49 public void respond(Request req, PrintWriter out) {
50 MultiMap<String, String> params = req.params();
51 String cmd = params.get("cmd");
53 out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
54 out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
55 out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en-US\">");
56 out.println("<head>");
57 out.println("<title>Shell</title>");
58 out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"css\" />");
59 out.println("</head>");
60 out.println("<body>");
61 out.println("<h1>Shell</h1>");
62 if((req.method() == "POST") && (cmd != null)) {
63 Console cons = new Console();
64 Interpreter ip = new Interpreter(cons);
70 out.println(Misc.htmlq((resp == null)?"(null)":(resp.toString())));
71 out.println("</pre>");
72 } catch(bsh.EvalError exc) {
73 out.println("<h2>Evaluation error</h2>");
75 out.print(exc.toString());
76 out.println("</pre>");
77 if(exc instanceof bsh.TargetError) {
78 bsh.TargetError te = (bsh.TargetError)exc;
79 out.println("<h3>Target error</h3>");
81 te.getTarget().printStackTrace(out);
82 out.println("</pre>");
85 String eo = new String(cons.obuf.toByteArray(), Misc.utf8);
86 String ee = new String(cons.ebuf.toByteArray(), Misc.utf8);
89 out.println("<h2>Output</h2>");
91 out.println(Misc.htmlq(eo));
92 out.println("</pre>");
95 out.println("<h2>Errors</h2>");
97 out.println(Misc.htmlq(ee));
98 out.println("</pre>");
101 out.println("<form action=\"sh\" method=\"post\">");
102 out.println("<textarea cols=\"80\" rows=\"5\" name=\"cmd\">");
105 out.println("</textarea>");
106 out.println("<input type=\"submit\" value=\"Evaluate\" />");
107 out.println("<input type=\"reset\" value=\"Reset\" />");
108 out.println("</form>");
109 out.println("</body>");
110 out.println("</html>");