diff options
author | Kyle Evans <kevans@FreeBSD.org> | 2018-06-10 02:36:38 +0000 |
---|---|---|
committer | Kyle Evans <kevans@FreeBSD.org> | 2018-06-10 02:36:38 +0000 |
commit | deb8c8f55c1d8a28966cf79d379720cf7131cdbe (patch) | |
tree | 3f3629e25a58aa50985d9ba1e946fbbdb4603473 /stand/lua | |
parent | 6387091e9ad676a28206aed59ee953e27876aa51 (diff) | |
download | src-deb8c8f55c1d8a28966cf79d379720cf7131cdbe.tar.gz src-deb8c8f55c1d8a28966cf79d379720cf7131cdbe.zip |
lualoader: Support variable substitution in env var settings
We support both of the following cases of substitution:
bar="y"
foo="${bar}"
foo="$bar"
The latter substitution syntax is, of course, not recommended- all
punctuation must be considered potential variable names, and we do not go
through the effort of searching the different combinations of, for instance,
"$x.y.z" to determine if the variable is $x, $x.y, or $x.y.z.
This is not officially documented as supported, but it has worked in
forthloader for what is most likely a long time as `evaluate` is used to
process the right hand side of the assignment.
Notes
Notes:
svn path=/head/; revision=334912
Diffstat (limited to 'stand/lua')
-rw-r--r-- | stand/lua/config.lua | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/stand/lua/config.lua b/stand/lua/config.lua index 082cca4e2e69..c082cb8e473a 100644 --- a/stand/lua/config.lua +++ b/stand/lua/config.lua @@ -102,6 +102,25 @@ local function setKey(key, name, value) modules[key][name] = value end +-- Escapes the named value for use as a literal in a replacement pattern. +-- e.g. dhcp.host-name gets turned into dhcp%.host%-name to remove the special +-- meaning. +local function escapeName(name) + return name:gsub("([%p])", "%%%1") +end + +local function processEnvVar(value) + for name in value:gmatch("${([^}]+)}") do + local replacement = loader.getenv(name) or "" + value = value:gsub("${" .. escapeName(name) .. "}", replacement) + end + for name in value:gmatch("$([%w%p]+)%s*") do + local replacement = loader.getenv(name) or "" + value = value:gsub("$" .. escapeName(name), replacement) + end + return value +end + local pattern_table = { { str = "^%s*(#.*)", @@ -172,7 +191,7 @@ local pattern_table = { { str = "^%s*([%w%p]+)%s*=%s*\"([%w%s%p]-)\"%s*(.*)", process = function(k, v) - if setEnv(k, v) ~= 0 then + if setEnv(k, processEnvVar(v)) ~= 0 then print(MSG_FAILSETENV:format(k, v)) end end, @@ -181,7 +200,7 @@ local pattern_table = { { str = "^%s*([%w%p]+)%s*=%s*(%d+)%s*(.*)", process = function(k, v) - if setEnv(k, v) ~= 0 then + if setEnv(k, processEnvVar(v)) ~= 0 then print(MSG_FAILSETENV:format(k, tostring(v))) end end, |