2018-01-19 11:32:32 +00:00
|
|
|
local cjson = require 'cjson'
|
|
|
|
|
2018-03-20 16:15:55 +00:00
|
|
|
local function config_error(src, ...)
|
|
|
|
error(src .. ' error: ' .. string.format(...), 0)
|
2018-01-19 12:26:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local has_domains = (os.execute('ls -d "$IPKG_INSTROOT"/lib/gluon/domains/ >/dev/null 2>&1') == 0)
|
|
|
|
|
|
|
|
|
2018-01-19 11:32:32 +00:00
|
|
|
local function load_json(filename)
|
|
|
|
local f = assert(io.open(filename))
|
|
|
|
local json = cjson.decode(f:read('*a'))
|
|
|
|
f:close()
|
|
|
|
return json
|
|
|
|
end
|
|
|
|
|
2018-01-19 12:26:54 +00:00
|
|
|
|
|
|
|
local function get_domains()
|
|
|
|
local domains = {}
|
|
|
|
local dirs = io.popen("find \"$IPKG_INSTROOT\"/lib/gluon/domains/ -name '*.json'")
|
|
|
|
for filename in dirs:lines() do
|
|
|
|
local name = string.match(filename, '([^/]+).json$')
|
|
|
|
domains[name] = load_json(filename)
|
|
|
|
end
|
|
|
|
dirs:close()
|
|
|
|
|
|
|
|
if not next(domains) then
|
2018-03-20 16:15:55 +00:00
|
|
|
config_error('site', 'no domain configurations found')
|
2018-01-19 12:26:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return domains
|
|
|
|
end
|
|
|
|
|
|
|
|
local site, domain_code, domain, conf
|
|
|
|
|
|
|
|
|
|
|
|
local function merge(a, b)
|
|
|
|
local function is_array(t)
|
|
|
|
local n = 0
|
|
|
|
for k, v in pairs(t) do
|
|
|
|
n = n + 1
|
|
|
|
end
|
|
|
|
return n == #t
|
|
|
|
end
|
|
|
|
|
|
|
|
if not b then return a end
|
|
|
|
if type(a) ~= type(b) then return b end
|
|
|
|
if type(b) ~= 'table' then return b end
|
|
|
|
if not next(b) then return a end
|
|
|
|
if is_array(a) ~= is_array(b) then return b end
|
|
|
|
|
|
|
|
local m = {}
|
|
|
|
for k, v in pairs(a) do
|
|
|
|
m[k] = v
|
|
|
|
end
|
|
|
|
for k, v in pairs(b) do
|
|
|
|
m[k] = merge(m[k], v)
|
|
|
|
end
|
|
|
|
|
|
|
|
return m
|
|
|
|
end
|
2018-01-19 11:32:32 +00:00
|
|
|
|
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
local function path_to_string(path)
|
2018-06-29 18:47:59 +00:00
|
|
|
return table.concat(path, '.')
|
2014-07-05 00:48:09 +00:00
|
|
|
end
|
|
|
|
|
2019-04-28 17:16:28 +00:00
|
|
|
local function format(val)
|
|
|
|
if type(val) == 'string' then
|
|
|
|
return string.format('%q', val)
|
|
|
|
else
|
|
|
|
return tostring(val)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-03 08:50:24 +00:00
|
|
|
local function array_to_string(array)
|
2019-04-28 17:16:28 +00:00
|
|
|
local strings = {}
|
|
|
|
for i, v in ipairs(array) do
|
|
|
|
strings[i] = format(v)
|
|
|
|
end
|
|
|
|
return '[' .. table.concat(strings, ', ') .. ']'
|
2016-08-03 08:50:24 +00:00
|
|
|
end
|
|
|
|
|
2018-03-13 17:56:11 +00:00
|
|
|
function table_keys(tbl)
|
|
|
|
local keys = {}
|
|
|
|
for k, _ in pairs(tbl) do
|
|
|
|
keys[#keys + 1] = k
|
|
|
|
end
|
|
|
|
return keys
|
|
|
|
end
|
|
|
|
|
2018-01-19 12:26:54 +00:00
|
|
|
|
|
|
|
local loadpath
|
|
|
|
|
|
|
|
local function site_src()
|
|
|
|
return 'site.conf'
|
|
|
|
end
|
|
|
|
|
|
|
|
local function domain_src()
|
|
|
|
return 'domains/' .. domain_code .. '.conf'
|
|
|
|
end
|
|
|
|
|
2019-04-28 12:38:08 +00:00
|
|
|
local function conf_src(path)
|
2018-01-19 12:26:54 +00:00
|
|
|
local src
|
|
|
|
|
|
|
|
if has_domains then
|
|
|
|
if loadpath(nil, domain, unpack(path)) ~= nil then
|
|
|
|
src = domain_src()
|
|
|
|
elseif loadpath(nil, site, unpack(path)) ~= nil then
|
|
|
|
src = site_src()
|
|
|
|
else
|
|
|
|
src = site_src() .. ' / ' .. domain_src()
|
|
|
|
end
|
|
|
|
else
|
|
|
|
src = site_src()
|
|
|
|
end
|
|
|
|
|
2019-04-28 12:38:08 +00:00
|
|
|
return src
|
|
|
|
end
|
|
|
|
|
|
|
|
local function var_error(path, val, msg)
|
|
|
|
if type(val) == 'string' then
|
|
|
|
val = string.format('%q', val)
|
|
|
|
end
|
|
|
|
|
2018-04-27 15:13:42 +00:00
|
|
|
local found = 'unset'
|
|
|
|
if val ~= nil then
|
|
|
|
found = string.format('%s (a %s value)', tostring(val), type(val))
|
|
|
|
end
|
|
|
|
|
2019-04-28 12:38:08 +00:00
|
|
|
config_error(conf_src(path), 'expected %s to %s, but it is %s', path_to_string(path), msg, found)
|
2016-08-03 08:50:24 +00:00
|
|
|
end
|
|
|
|
|
2018-01-20 04:32:18 +00:00
|
|
|
function in_site(path)
|
|
|
|
if has_domains and loadpath(nil, domain, unpack(path)) ~= nil then
|
2018-03-20 16:15:55 +00:00
|
|
|
config_error(domain_src(), '%s is allowed in site configuration only', path_to_string(path))
|
2018-01-20 04:32:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return path
|
|
|
|
end
|
|
|
|
|
|
|
|
function in_domain(path)
|
|
|
|
if has_domains and loadpath(nil, site, unpack(path)) ~= nil then
|
2018-03-20 16:15:55 +00:00
|
|
|
config_error(site_src(), '%s is allowed in domain configuration only', path_to_string(path))
|
2018-01-20 04:32:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return path
|
|
|
|
end
|
|
|
|
|
|
|
|
function this_domain()
|
|
|
|
return domain_code
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
function extend(path, c)
|
2018-01-19 12:26:54 +00:00
|
|
|
if not path then return nil end
|
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
local p = {unpack(path)}
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
for _, e in ipairs(c) do
|
|
|
|
p[#p+1] = e
|
|
|
|
end
|
|
|
|
return p
|
2015-10-11 17:31:44 +00:00
|
|
|
end
|
|
|
|
|
2018-01-19 12:26:54 +00:00
|
|
|
function loadpath(path, base, c, ...)
|
2018-01-19 09:12:43 +00:00
|
|
|
if not c or base == nil then
|
|
|
|
return base
|
|
|
|
end
|
2015-10-11 17:31:44 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
if type(base) ~= 'table' then
|
2018-01-19 12:26:54 +00:00
|
|
|
if path then
|
|
|
|
var_error(path, base, 'be a table')
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
2018-01-19 09:12:43 +00:00
|
|
|
end
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
return loadpath(extend(path, {c}), base[c], ...)
|
2014-07-05 00:48:09 +00:00
|
|
|
end
|
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
local function loadvar(path)
|
2018-01-19 12:26:54 +00:00
|
|
|
return loadpath({}, conf, unpack(path))
|
2018-01-19 09:12:43 +00:00
|
|
|
end
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
local function check_type(t)
|
|
|
|
return function(val)
|
|
|
|
return type(val) == t
|
|
|
|
end
|
2014-07-05 00:48:09 +00:00
|
|
|
end
|
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
local function check_one_of(array)
|
|
|
|
return function(val)
|
|
|
|
for _, v in ipairs(array) do
|
|
|
|
if v == val then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-03-20 16:15:55 +00:00
|
|
|
|
|
|
|
function alternatives(...)
|
|
|
|
local errs = {'All of the following alternatives have failed:'}
|
|
|
|
for i, f in ipairs({...}) do
|
|
|
|
local ok, err = pcall(f)
|
|
|
|
if ok then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
errs[#errs+1] = string.format('%i) %s', i, err)
|
|
|
|
end
|
|
|
|
|
|
|
|
error(table.concat(errs, '\n '), 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2018-02-28 01:57:12 +00:00
|
|
|
local function check_chanlist(channels)
|
|
|
|
local is_valid_channel = check_one_of(channels)
|
|
|
|
return function(chanlist)
|
|
|
|
for group in chanlist:gmatch("%S+") do
|
|
|
|
if group:match("^%d+$") then
|
|
|
|
channel = tonumber(group)
|
|
|
|
if not is_valid_channel(channel) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
elseif group:match("^%d+-%d+$") then
|
|
|
|
from, to = group:match("^(%d+)-(%d+)$")
|
|
|
|
from = tonumber(from)
|
|
|
|
to = tonumber(to)
|
|
|
|
if from >= to then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
if not is_valid_channel(from) or not is_valid_channel(to) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
function need(path, check, required, msg)
|
|
|
|
local val = loadvar(path)
|
|
|
|
if required == false and val == nil then
|
|
|
|
return nil
|
|
|
|
end
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
if not check(val) then
|
|
|
|
var_error(path, val, msg)
|
|
|
|
end
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
return val
|
2014-07-05 00:48:09 +00:00
|
|
|
end
|
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
local function need_type(path, type, required, msg)
|
|
|
|
return need(path, check_type(type), required, msg)
|
|
|
|
end
|
2014-07-16 14:31:31 +00:00
|
|
|
|
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
function need_alphanumeric_key(path)
|
|
|
|
local val = path[#path]
|
|
|
|
-- We don't use character classes like %w here to be independent of the locale
|
2018-04-01 21:18:22 +00:00
|
|
|
if type(val) ~= 'string' or not val:match('^[0-9a-zA-Z_]+$') then
|
|
|
|
var_error(path, val, 'have a string key using only alphanumeric characters and underscores')
|
2018-01-19 09:12:43 +00:00
|
|
|
end
|
2014-07-16 14:31:31 +00:00
|
|
|
end
|
|
|
|
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
function need_string(path, required)
|
|
|
|
return need_type(path, 'string', required, 'be a string')
|
|
|
|
end
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
function need_string_match(path, pat, required)
|
|
|
|
local val = need_string(path, required)
|
|
|
|
if not val then
|
|
|
|
return nil
|
|
|
|
end
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
if not val:match(pat) then
|
|
|
|
var_error(path, val, "match pattern '" .. pat .. "'")
|
|
|
|
end
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
return val
|
2014-07-05 00:48:09 +00:00
|
|
|
end
|
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
function need_number(path, required)
|
|
|
|
return need_type(path, 'number', required, 'be a number')
|
|
|
|
end
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
function need_boolean(path, required)
|
|
|
|
return need_type(path, 'boolean', required, 'be a boolean')
|
|
|
|
end
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
function need_array(path, subcheck, required)
|
|
|
|
local val = need_type(path, 'table', required, 'be an array')
|
|
|
|
if not val then
|
|
|
|
return nil
|
|
|
|
end
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
if subcheck then
|
|
|
|
for i = 1, #val do
|
|
|
|
subcheck(extend(path, {i}))
|
|
|
|
end
|
|
|
|
end
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
return val
|
2014-07-05 00:48:09 +00:00
|
|
|
end
|
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
function need_table(path, subcheck, required)
|
|
|
|
local val = need_type(path, 'table', required, 'be a table')
|
|
|
|
if not val then
|
|
|
|
return nil
|
|
|
|
end
|
2016-08-03 08:50:24 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
if subcheck then
|
|
|
|
for k, _ in pairs(val) do
|
|
|
|
subcheck(extend(path, {k}))
|
|
|
|
end
|
|
|
|
end
|
2016-08-03 08:50:24 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
return val
|
|
|
|
end
|
|
|
|
|
|
|
|
function need_value(path, value, required)
|
|
|
|
return need(path, function(v)
|
|
|
|
return v == value
|
|
|
|
end, required, 'be ' .. tostring(value))
|
|
|
|
end
|
2016-08-03 08:50:24 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
function need_one_of(path, array, required)
|
|
|
|
return need(path, check_one_of(array), required, 'be one of the given array ' .. array_to_string(array))
|
2016-08-03 08:50:24 +00:00
|
|
|
end
|
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
function need_string_array(path, required)
|
|
|
|
return need_array(path, need_string, required)
|
2014-07-05 00:48:09 +00:00
|
|
|
end
|
2016-08-03 08:50:24 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
function need_string_array_match(path, pat, required)
|
|
|
|
return need_array(path, function(e) need_string_match(e, pat) end, required)
|
2017-02-21 08:45:33 +00:00
|
|
|
end
|
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
function need_array_of(path, array, required)
|
|
|
|
return need_array(path, function(e) need_one_of(e, array) end, required)
|
2016-08-03 08:50:24 +00:00
|
|
|
end
|
2018-01-19 11:32:32 +00:00
|
|
|
|
2018-02-28 01:57:12 +00:00
|
|
|
function need_chanlist(path, channels, required)
|
|
|
|
local valid_chanlist = check_chanlist(channels)
|
|
|
|
return need(path, valid_chanlist, required, 'be a space-separated list of WiFi channels or channel-ranges (separated by a hyphen). ' ..
|
|
|
|
'Valid channels are: ' .. array_to_string(channels))
|
|
|
|
end
|
|
|
|
|
2019-02-12 10:00:29 +00:00
|
|
|
function need_domain_name(path)
|
|
|
|
need_string(path)
|
|
|
|
need(path, function(domain_name)
|
|
|
|
local f = io.open(os.getenv('IPKG_INSTROOT') .. '/lib/gluon/domains/' .. domain_name .. '.json')
|
|
|
|
if not f then return false end
|
|
|
|
f:close()
|
|
|
|
return true
|
|
|
|
end, nil, 'be a valid domain name')
|
|
|
|
end
|
2018-01-19 11:32:32 +00:00
|
|
|
|
2019-04-28 12:38:08 +00:00
|
|
|
function obsolete(path, msg)
|
|
|
|
local val = loadvar(path)
|
|
|
|
if val == nil then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if not msg then
|
|
|
|
msg = 'Check the release notes and documentation for details.'
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
config_error(conf_src(path), '%s is obsolete. %s', path_to_string(path), msg)
|
|
|
|
end
|
|
|
|
|
2018-01-19 12:26:54 +00:00
|
|
|
local check = assert(loadfile())
|
|
|
|
|
|
|
|
site = load_json(os.getenv('IPKG_INSTROOT') .. '/lib/gluon/site.json')
|
|
|
|
|
2018-03-20 16:15:55 +00:00
|
|
|
local ok, err = pcall(function()
|
|
|
|
if has_domains then
|
|
|
|
for k, v in pairs(get_domains()) do
|
|
|
|
domain_code = k
|
|
|
|
domain = v
|
|
|
|
conf = merge(site, domain)
|
|
|
|
check()
|
|
|
|
end
|
|
|
|
else
|
|
|
|
conf = site
|
2018-01-19 12:26:54 +00:00
|
|
|
check()
|
|
|
|
end
|
2018-03-20 16:15:55 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
if not ok then
|
|
|
|
io.stderr:write('*** ', err, '\n')
|
|
|
|
os.exit(1)
|
2018-01-19 12:26:54 +00:00
|
|
|
end
|