1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
if os.getenv "installcheck" == nil then
-- Unless we're running inside `make installcheck`, add the dev-tree
-- directories to the module search paths.
local std = require "specl.std"
local top_srcdir = os.getenv "top_srcdir" or "."
local top_builddir = os.getenv "top_builddir" or "."
package.path = std.package.normalize (
top_builddir .. "/lib/?.lua",
top_srcdir .. "/lib/?.lua",
top_builddir .. "/lib/?/init.lua",
top_srcdir .. "/lib/?/init.lua",
package.path)
package.cpath = std.package.normalize (
top_builddir .. "/ext/.libs/?.so",
top_srcdir .. "/ext/.libs/?.so",
top_builddir .. "/ext/_libs/?.dll",
top_srcdir .. "/ext/_libs/?.dll",
package.cpath)
end
badargs = require "specl.badargs"
hell = require "specl.shell"
curses = require "curses"
-- Allow user override of LUA binary used by hell.spawn, falling
-- back to environment PATH search for "lua" if nothing else works.
local LUA = os.getenv "LUA" or "lua"
-- Easily check for std.object.type compatibility.
function prototype (o)
return (getmetatable (o) or {})._type or io.type (o) or type (o)
end
local function mkscript (code)
local f = os.tmpname ()
local h = io.open (f, "w")
h:write (code)
h:close ()
return f
end
--- Run some Lua code with the given arguments and input.
-- @string code valid Lua code
-- @tparam[opt={}] string|table arg single argument, or table of
-- arguments for the script invocation
-- @string[opt] stdin standard input contents for the script process
-- @treturn specl.shell.Process|nil status of resulting process if
-- execution was successful, otherwise nil
function luaproc (code, arg, stdin)
local f = mkscript (code)
if type (arg) ~= "table" then arg = {arg} end
local cmd = {LUA, f, unpack (arg)}
-- inject env and stdin keys separately to avoid truncating `...` in
-- cmd constructor
cmd.stdin = stdin
cmd.env = {
LUA_CPATH = package.cpath,
LUA_PATH = package.path,
LUA_INIT = "",
LUA_INIT_5_2 = ""
}
local proc = hell.spawn (cmd)
os.remove (f)
return proc
end
-- Allow comparison against the error message of a function call result.
function Emsg (_, msg) return msg or "" end
-- Collect stdout from a shell command, and strip surrounding whitespace.
function cmd_output (cmd)
return hell.spawn (cmd).output:gsub ("^%s+", ""):gsub ("%s+$", "")
end
-- Create an empty file at PATH.
function touch (path) io.open (path, "w+"):close () end
-- Format a bad argument type error.
local function typeerrors (fname, i, want, field, got)
return {
badargs.format ("?", i, want, field, got), -- LuaJIT
badargs.format (fname, i, want, field, got), -- PUC-Rio
}
end
function init (M, fname)
return M[fname], function (...) return typeerrors (fname, ...) end
end
|