comparison http/src/luan/modules/http/shell.luan @ 505:7bc63886d4f2

web page modules now return a function
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 21 May 2015 01:45:49 -0600
parents dbdf4b8193a8
children 342964519194
comparison
equal deleted inserted replaced
504:dbdf4b8193a8 505:7bc63886d4f2
1 local Luan = require "luan:Luan" 1 return require("luan:http/ShellMod").respond
2 local ipairs = Luan.ipairs
3 local load = Luan.load
4 local try = Luan.try
5 local Io = require "luan:Io"
6 local print = Io.print
7 local Debug = require "luan:Debug"
8 local Http = require "luan:http/Http"
9 local Html = require "luan:Html"
10
11 local M = {}
12
13 Http.per_session(M)
14
15 local history = {}
16 M.env = {}
17
18 function M.respond()
19 if Http.request.parameter.clear ~= nil then
20 history = {}
21 else
22 local cmd = Http.request.parameter.cmd
23 if cmd ~= nil then
24 Io.stdout = {}
25 function Io.stdout.write(...)
26 for v in Luan.values(...) do
27 history[#history+1] = v
28 end
29 end
30 print( "% "..cmd )
31 try {
32 function()
33 local line = load(cmd,"<web_shell>",M.env,true)
34 Debug.print_if_something( line() )
35 end;
36 catch = function(e)
37 Io.print_to(Io.stderr,e)
38 print(e)
39 end;
40 }
41 end
42 end
43
44 Io.stdout = Http.response.text_writer()
45 %>
46 <html>
47 <head>
48 <% Html.simply_html_head() %>
49 <title>Luan Shell</title>
50 </head>
51 <body>
52 <div container>
53 <h3>Luan Shell</h3>
54 <p>This is a command shell. Enter commands below.</p>
55 <pre><%
56 for _,v in ipairs(history) do
57 Io.stdout.write(v)
58 end
59 %></pre>
60 <form name='form0' method='post'>
61 % <input name='cmd' size="80" autofocus>
62 <input type="submit" value="run" textcolor="white" bgcolor="#337ab7">
63 <input type="submit" name="clear" value="clear" textcolor="white" bgcolor="#337ab7">
64 </form>
65 </div>
66 <% Html.simply_html_body_bottom() %>
67 </body>
68 </html>
69 <%
70 end
71
72 return M