From 058c692e15d6d031553a4ec31cb5aeccd38846ab Mon Sep 17 00:00:00 2001 From: Kyle Evans Date: Tue, 21 Aug 2018 23:42:20 +0000 Subject: [PATCH] lualoader: Refactor config line expressions A couple of issues addressed: 1.) Modules with - in the name were not recognized as modules 2.) The module regex was repeated for each place a module name may appear 3.) The 'strip leading space' bits were repeated for each expression 4.) The trailing 'comment validation' stuff was repeated every expression #4 still has some more work to be done. exec lines, for instance, don't capture a 'value' -- there's only one capture pattern. This throws off the 'c' value that we match, so the trailing bits aren't *actually* being validated. This isn't a new issue, though, so a future comit will address this. --- stand/lua/config.lua | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/stand/lua/config.lua b/stand/lua/config.lua index 139c61d64220..477311d834db 100644 --- a/stand/lua/config.lua +++ b/stand/lua/config.lua @@ -54,6 +54,8 @@ local MSG_KERNLOADING = "Loading kernel..." local MSG_MODLOADING = "Loading configured modules..." local MSG_MODLOADFAIL = "Could not load one or more modules!" +local MODULEEXPR = '([%w-_]+)' + local function restoreEnv() -- Examine changed environment variables for k, v in pairs(env_changed) do @@ -121,14 +123,19 @@ local function processEnvVar(value) return value end +-- str in this table is a regex pattern. It will automatically be anchored to +-- the beginning of a line and any preceding whitespace will be skipped. The +-- pattern should have no more than two captures patterns, which correspond to +-- the two parameters (usually 'key' and 'value') that are passed to the +-- process function. All trailing characters will be validated. local pattern_table = { { - str = "^%s*(#.*)", + str = "(#.*)", process = function(_, _) end, }, -- module_load="value" { - str = "^%s*([%w_]+)_load%s*=%s*\"([%w%s%p]-)\"%s*(.*)", + str = MODULEEXPR .. "_load%s*=%s*\"([%w%s%p]-)\"", process = function(k, v) if modules[k] == nil then modules[k] = {} @@ -138,49 +145,49 @@ local pattern_table = { }, -- module_name="value" { - str = "^%s*([%w_]+)_name%s*=%s*\"([%w%s%p]-)\"%s*(.*)", + str = MODULEEXPR .. "_name%s*=%s*\"([%w%s%p]-)\"", process = function(k, v) setKey(k, "name", v) end, }, -- module_type="value" { - str = "^%s*([%w_]+)_type%s*=%s*\"([%w%s%p]-)\"%s*(.*)", + str = MODULEEXPR .. "_type%s*=%s*\"([%w%s%p]-)\"", process = function(k, v) setKey(k, "type", v) end, }, -- module_flags="value" { - str = "^%s*([%w_]+)_flags%s*=%s*\"([%w%s%p]-)\"%s*(.*)", + str = MODULEEXPR .. "_flags%s*=%s*\"([%w%s%p]-)\"", process = function(k, v) setKey(k, "flags", v) end, }, -- module_before="value" { - str = "^%s*([%w_]+)_before%s*=%s*\"([%w%s%p]-)\"%s*(.*)", + str = MODULEEXPR .. "_before%s*=%s*\"([%w%s%p]-)\"", process = function(k, v) setKey(k, "before", v) end, }, -- module_after="value" { - str = "^%s*([%w_]+)_after%s*=%s*\"([%w%s%p]-)\"%s*(.*)", + str = MODULEEXPR .. "_after%s*=%s*\"([%w%s%p]-)\"", process = function(k, v) setKey(k, "after", v) end, }, -- module_error="value" { - str = "^%s*([%w_]+)_error%s*=%s*\"([%w%s%p]-)\"%s*(.*)", + str = MODULEEXPR .. "_error%s*=%s*\"([%w%s%p]-)\"", process = function(k, v) setKey(k, "error", v) end, }, -- exec="command" { - str = "^%s*exec%s*=%s*\"([%w%s%p]-)\"%s*(.*)", + str = "exec%s*=%s*\"([%w%s%p]-)\"", process = function(k, _) if cli_execute_unparsed(k) ~= 0 then print(MSG_FAILEXEC:format(k)) @@ -189,7 +196,7 @@ local pattern_table = { }, -- env_var="value" { - str = "^%s*([%w%p]+)%s*=%s*\"([%w%s%p]-)\"%s*(.*)", + str = "([%w%p]+)%s*=%s*\"([%w%s%p]-)\"", process = function(k, v) if setEnv(k, processEnvVar(v)) ~= 0 then print(MSG_FAILSETENV:format(k, v)) @@ -198,7 +205,7 @@ local pattern_table = { }, -- env_var=num { - str = "^%s*([%w%p]+)%s*=%s*(-?%d+)%s*(.*)", + str = "([%w%p]+)%s*=%s*(-?%d+)", process = function(k, v) if setEnv(k, processEnvVar(v)) ~= 0 then print(MSG_FAILSETENV:format(k, tostring(v))) @@ -390,7 +397,8 @@ function config.parse(text) local found = false for _, val in ipairs(pattern_table) do - local k, v, c = line:match(val.str) + local pattern = '^%s*' .. val.str .. '%s*(.*)'; + local k, v, c = line:match(pattern) if k ~= nil then found = true