diff options
author | Karen Arutyunov <karen@codesynthesis.com> | 2017-06-21 18:40:49 +0300 |
---|---|---|
committer | Karen Arutyunov <karen@codesynthesis.com> | 2017-06-21 20:02:08 +0300 |
commit | af8747969925a0815c09825ee8420a1be9dcc6c7 (patch) | |
tree | 7d43c43f4c0ff2b53a276e4d6ff95d60554c51a0 /build2/functions-builtin.cxx | |
parent | f6c20ad37b2ececb446b5051837bccba93c81d7a (diff) |
Add support for $envvar() function
Diffstat (limited to 'build2/functions-builtin.cxx')
-rw-r--r-- | build2/functions-builtin.cxx | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/build2/functions-builtin.cxx b/build2/functions-builtin.cxx index 81a190b..f9bbda5 100644 --- a/build2/functions-builtin.cxx +++ b/build2/functions-builtin.cxx @@ -2,6 +2,8 @@ // copyright : Copyright (c) 2014-2017 Code Synthesis Ltd // license : MIT; see accompanying LICENSE file +#include <cstdlib> // getenv() + #include <build2/function.hxx> #include <build2/variable.hxx> @@ -9,6 +11,15 @@ using namespace std; namespace build2 { + // Return NULL value if an environment variable is not set. + // + static inline value + getenv (const string& name) + { + const char* v (::getenv (name.c_str ())); + return v != nullptr ? value (v) : value (); + } + void builtin_functions () { @@ -26,5 +37,26 @@ namespace build2 f["string"] = [](bool b) {return b ? "true" : "false";}; f["string"] = [](uint64_t i) {return to_string (i);}; f["string"] = [](name n) {return to_string (n);}; + + // getenv + // + f["getenv"] = [](string name) + { + return getenv (name); + }; + + f["getenv"] = [](names name) + { + // Note that if the name size is greater than one, we will fail with the + // proper diagnostics. + // + assert (!name.empty ()); + + string n (name.size () == 1 + ? convert<string> (move (name[0])) + : convert<string> (move (name))); + + return getenv (n); + }; } } |