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