view http/src/luan/modules/http/Shell_mod.luan @ 554:18504c41b0be

move debug() to Io and remove Debug module; Io.print() now doesn't print newline if there is nothing to be printed;
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 18 Jun 2015 03:30:18 -0600
parents 342964519194
children 2f39468680be
line wrap: on
line source

local Luan = require "luan:Luan"
local error = Luan.error
local ipairs = Luan.ipairs or error()
local load = Luan.load or error()
local try = Luan.try or error()
local Io = require "luan:Io"
local print = Io.print or error()
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)
					print( 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