From 92ef78516cf6305e4c151c32061abb84ba788e7e Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Sun, 14 Jul 2013 17:59:46 -0400 Subject: [PATCH] ?= conditional environment variables --- mpw/environ.rl | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/mpw/environ.rl b/mpw/environ.rl index 796df78..c888b14 100644 --- a/mpw/environ.rl +++ b/mpw/environ.rl @@ -12,8 +12,19 @@ //printf("emplacing %s\n", name.c_str()); // trim any whitespace. //while (value.length() && isspace(value.back())) - // value.pop_back(); - env[std::move(name)] = std::move(value); + // value.pop_back(); + // ?= only sets if not already set. + + auto iter = env.find(name); + if (iter == env.end()) + { + env[std::move(name)] = std::move(value); + } + else + { + if (overwrite) + iter->second = std::move(value); + } } value := |* @@ -53,7 +64,11 @@ assignment := word+ ${ name.push_back(fc); } ws* - '=' + ( + '?=' ${ overwrite = false; } + | + '=' ${ overwrite = true; } + ) ws* # ws does not include '\n', so that will be handled # as a value. @@ -62,10 +77,13 @@ main := |* ws; # leading space + '\n'; # blank line. + '#' => { fgoto comment; }; + word => { fhold; fgoto assignment; }; - *|; + *|; }%% #include @@ -119,6 +137,7 @@ void LoadEnvironment(std::string &envfile, std::unordered_map