diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/http/src/luan/modules/http/ShellMod.luan	Thu May 21 01:45:49 2015 -0600
@@ -0,0 +1,72 @@
+local Luan = require "luan:Luan"
+local ipairs = Luan.ipairs
+local load = Luan.load
+local try = Luan.try
+local Io = require "luan:Io"
+local print = Io.print
+local Debug = require "luan:Debug"
+local Http = require "luan:http/Http"
+local Html = require "luan:Html"
+
+local M = {}
+
+local history = {}
+M.env = {}
+
+function M.respond()
+	if Http.request.parameter.clear ~= nil then
+		history = {}
+	else
+		local cmd = Http.request.parameter.cmd
+		if cmd ~= nil then
+			Io.stdout = {}
+			function Io.stdout.write(...)
+				for v in Luan.values(...) do
+					history[#history+1] = v
+				end
+			end
+			print( "% "..cmd )
+			try {
+				function()
+					local line = load(cmd,"<web_shell>",M.env,true)
+					Debug.print_if_something( line() )
+				end;
+				catch = function(e)
+					Io.print_to(Io.stderr,e)
+					print(e)
+				end;
+			}
+		end
+	end
+
+	Io.stdout = Http.response.text_writer()
+%>
+<html>
+	<head>
+		<% Html.simply_html_head() %>
+		<title>Luan Shell</title>
+	</head>
+	<body>
+		<div container>
+			<h3>Luan Shell</h3>
+			<p>This is a command shell.  Enter commands below.</p>
+			<pre><%
+			for _,v in ipairs(history) do
+				Io.stdout.write(v)
+			end
+			%></pre>
+			<form name='form0' method='post'>
+				% <input name='cmd' size="80" autofocus>
+				<input type="submit" value="run" textcolor="white" bgcolor="#337ab7">
+				<input type="submit" name="clear" value="clear" textcolor="white" bgcolor="#337ab7">
+			</form>
+		</div>
+		<% Html.simply_html_body_bottom() %>
+	</body>
+</html>
+<%
+end
+
+Http.per_session(M.respond)
+
+return M