sandbox.lua 2.71 KB
Newer Older
1 2 3 4
-- Units
kB = 1024
MB = 1024*1024
GB = 1024*1024
5 6 7 8
-- Time
sec = 1000
minute = 60 * sec
hour = 60 * minute
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

-- Function aliases
-- `env.VAR returns os.getenv(VAR)`
env = {}
setmetatable(env, {
	__index = function (t, k) return os.getenv(k) end
})

-- Quick access to interfaces
-- `net.<iface>` => `net.interfaces()[iface]`
setmetatable(net, {
	__index = function (t, k)
		local v = rawget(t, k)
		if v then return v
		else return net.interfaces()[k]
		end
	end
})

28 29
-- Syntactic sugar for module loading
-- `modules.<name> = <config>`
30
setmetatable(modules, {
31
	__newindex = function (t,k,v)
32 33 34 35 36
		if not rawget(_G, k) then
			modules.load(k)
			local mod = rawget(_G, k)
			if mod and mod['config'] then
				mod['config'](v)
37
			end
38

39
		end
40
	end
41 42
})

43 44 45 46 47 48 49
-- Register module in Lua environment
function modules_register(module)
	-- Syntactic sugar for get() and set() properties
	setmetatable(module, {
		__index = function (t, k)
			local  v = rawget(t, k)
			if     v     then return v
50
			elseif rawget(t, 'get') then return t.get(k)
51 52 53 54
			end
		end,
		__newindex = function (t, k, v)
			local  old_v = rawget(t, k)
55
			if not old_v and rawget(t, 'set') then
56 57 58 59 60 61
				t.set(k..' '..v)
			end
		end
	})
end

62
-- Make sandboxed environment
63
local function make_sandbox(defined)
64
	local __protected = { modules = true, cache = true, net = true }
65 66 67 68 69 70 71 72 73 74 75 76 77
	return setmetatable({}, {
		__index = defined,
		__newindex = function (t, k, v)
			if __protected[k] then
				for k2,v2 in pairs(v) do
					defined[k][k2] = v2
				end
			else
				defined[k] = v
			end
		end
	})
end
78

79
-- Compatibility sandbox
80 81 82 83 84 85
if setfenv then -- Lua 5.1 and less
	_G = make_sandbox(getfenv(0))
	setfenv(0, _G)
else -- Lua 5.2+
	_SANDBOX = make_sandbox(_ENV)
end
86

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
-- Interactive command evaluation
function eval_cmd(line)
	-- Compatibility sandbox code loading
	local function load_code(code)
	    if getfenv then -- Lua 5.1
	        return loadstring(code)
	    else            -- Lua 5.2+
	        return load(code, nil, 't', _ENV)
	    end
	end
	local status, err, chunk
	chunk, err = load_code('table_print('..line..')')
	if err then
		chunk, err = load_code(line)
	end
	if not err then
		chunk()
	end
	if err then
		print(err)
	end
end

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
-- Pretty printing
function table_print (tt, indent, done)
	done = done or {}
	indent = indent or 0
	if type(tt) == "table" then
		for key, value in pairs (tt) do
			io.write(string.rep (" ", indent))
			if type (value) == "table" and not done [value] then
				done [value] = true
				io.write(string.format("[%s] => {\n", tostring (key)));
				table_print (value, indent + 4, done)
				io.write(string.rep (" ", indent))
				io.write("}\n");
			else
				io.write(string.format("[%s] => %s\n",
				         tostring (key), tostring(value)))
			end
		end
	else
		io.write(tostring(tt) .. "\n")
	end
end