Changeset View
Changeset View
Standalone View
Standalone View
sys/tools/makesyscalls.lua
Show First 20 Lines • Show All 187 Lines • ▼ Show 20 Lines | if s == nil then | ||||
return nil | return nil | ||||
end | end | ||||
if char == nil then | if char == nil then | ||||
char = "%s" | char = "%s" | ||||
end | end | ||||
return s:gsub("^" .. char .. "+", ""):gsub(char .. "+$", "") | return s:gsub("^" .. char .. "+", ""):gsub(char .. "+$", "") | ||||
end | end | ||||
-- We have to io.popen it, making sure it's properly escaped, and grab the | |||||
-- output from the handle returned. | |||||
local function exec(cmd) | |||||
cmd = cmd:gsub('"', '\\"') | |||||
local shcmd = "/bin/sh -c \"" .. cmd .. "\"" | |||||
local fh = io.popen(shcmd) | |||||
local output = fh:read("a") | |||||
fh:close() | |||||
return output | |||||
end | |||||
-- config looks like a shell script; in fact, the previous makesyscalls.sh | -- config looks like a shell script; in fact, the previous makesyscalls.sh | ||||
-- script actually sourced it in. It had a pretty common format, so we should | -- script actually sourced it in. It had a pretty common format, so we should | ||||
-- be fine to make various assumptions | -- be fine to make various assumptions | ||||
local function process_config(file) | local function process_config(file) | ||||
local cfg = {} | local cfg = {} | ||||
local comment_line_expr = "^%s*#.*" | local comment_line_expr = "^%s*#.*" | ||||
-- We capture any whitespace padding here so we can easily advance to | -- We capture any whitespace padding here so we can easily advance to | ||||
-- the end of the line as needed to check for any trailing bogus bits. | -- the end of the line as needed to check for any trailing bogus bits. | ||||
Show All 16 Lines | for nextline in fh:lines() do | ||||
nextline = nextline:gsub(comment_line_expr, "") | nextline = nextline:gsub(comment_line_expr, "") | ||||
-- Parse it into key, value pairs | -- Parse it into key, value pairs | ||||
local key, value = nextline:match(line_expr) | local key, value = nextline:match(line_expr) | ||||
if key ~= nil and value ~= nil then | if key ~= nil and value ~= nil then | ||||
local kvp = key .. "=" .. value | local kvp = key .. "=" .. value | ||||
key = trim(key) | key = trim(key) | ||||
value = trim(value) | value = trim(value) | ||||
local delim = value:sub(1,1) | local delim = value:sub(1,1) | ||||
if delim == '`' or delim == '"' then | if delim == '"' then | ||||
local trailing_context | local trailing_context | ||||
-- Strip off the key/value part | -- Strip off the key/value part | ||||
trailing_context = nextline:sub(kvp:len() + 1) | trailing_context = nextline:sub(kvp:len() + 1) | ||||
-- Strip off any trailing comment | -- Strip off any trailing comment | ||||
trailing_context = trailing_context:gsub("#.*$", | trailing_context = trailing_context:gsub("#.*$", | ||||
"") | "") | ||||
-- Strip off leading/trailing whitespace | -- Strip off leading/trailing whitespace | ||||
trailing_context = trim(trailing_context) | trailing_context = trim(trailing_context) | ||||
if trailing_context ~= "" then | if trailing_context ~= "" then | ||||
print(trailing_context) | print(trailing_context) | ||||
abort(1, "Malformed line: " .. nextline) | abort(1, "Malformed line: " .. nextline) | ||||
end | end | ||||
end | |||||
if delim == '`' then | |||||
-- Command substition may use $1 and $2 to mean | |||||
-- the syscall definition file and itself | |||||
-- respectively. We'll go ahead and replace | |||||
-- $[0-9] with respective arg in case we want to | |||||
-- expand this in the future easily... | |||||
value = trim(value, delim) | |||||
for capture in value:gmatch("$([0-9]+)") do | |||||
capture = tonumber(capture) | |||||
if capture > #arg then | |||||
abort(1, "Not enough args: " .. | |||||
value) | |||||
end | |||||
value = value:gsub("$" .. capture, | |||||
arg[capture]) | |||||
end | |||||
value = exec(value) | |||||
elseif delim == '"' then | |||||
value = trim(value, delim) | value = trim(value, delim) | ||||
else | else | ||||
-- Strip off potential comments | -- Strip off potential comments | ||||
value = value:gsub("#.*$", "") | value = value:gsub("#.*$", "") | ||||
-- Strip off any padding whitespace | -- Strip off any padding whitespace | ||||
value = trim(value) | value = trim(value) | ||||
if value:match("%s") then | if value:match("%s") then | ||||
abort(1, "Malformed config line: " .. | abort(1, "Malformed config line: " .. | ||||
▲ Show 20 Lines • Show All 1,118 Lines • Show Last 20 Lines |