aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--build2/functions-builtin.cxx32
-rw-r--r--build2/functions-process-path.cxx6
-rw-r--r--tests/function/builtin/testscript61
3 files changed, 96 insertions, 3 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);
+ };
}
}
diff --git a/build2/functions-process-path.cxx b/build2/functions-process-path.cxx
index 1b7459b..bbfc427 100644
--- a/build2/functions-process-path.cxx
+++ b/build2/functions-process-path.cxx
@@ -18,8 +18,8 @@ namespace build2
//
f["recall"] = &process_path::recall;
f["effect"] = [](process_path p)
- {
- return move (p.effect.empty () ? p.recall : p.effect);
- };
+ {
+ return move (p.effect.empty () ? p.recall : p.effect);
+ };
}
}
diff --git a/tests/function/builtin/testscript b/tests/function/builtin/testscript
index 0b005f3..3e27958 100644
--- a/tests/function/builtin/testscript
+++ b/tests/function/builtin/testscript
@@ -60,3 +60,64 @@
$* <'print $identity(abc)' >'abc';
$* <'print $type($identity(abc))' >'' : untyped
}
+
+: getenv
+:
+{
+ : set
+ :
+ : Here we rely on the facts that on POSIX PATH environment variable most
+ : likely is set at the time of login, and on Windows it is set by build2 on
+ : startup.
+ :
+ : @@ Use a custom variable, when an ability to set environment variables in
+ : testscript is implemented.
+ :
+ {
+ : string
+ :
+ $* <'print $getenv([string] "PATH")' | set v;
+ ($v != '[null]') || exit "PATH environment variable is not set"
+
+ : untyped
+ :
+ $* <'print $getenv("PATH")' | set v;
+ ($v != '[null]') || exit "PATH environment variable is not set"
+
+ : path
+ :
+ $* <'print $getenv([path] a)' 2>>~/EOE/ != 0
+ <stdin>:1:8: error: unmatched call to getenv(path)
+ /((
+ info: candidate: getenv(<untyped>), qualified name builtin.getenv
+ info: candidate: getenv(string), qualified name builtin.getenv
+ /)|(
+ info: candidate: getenv(string), qualified name builtin.getenv
+ info: candidate: getenv(<untyped>), qualified name builtin.getenv
+ /))
+ EOE
+
+ : none
+ :
+ $* <'print $getenv()' 2>>~/EOE/ != 0
+ <stdin>:1:8: error: unmatched call to getenv()
+ /((
+ info: candidate: getenv(<untyped>), qualified name builtin.getenv
+ info: candidate: getenv(string), qualified name builtin.getenv
+ /)|(
+ info: candidate: getenv(string), qualified name builtin.getenv
+ info: candidate: getenv(<untyped>), qualified name builtin.getenv
+ /))
+ EOE
+
+ : names
+ :
+ $* <'print $getenv(a b)' 2>>EOE != 0
+ error: invalid argument: invalid string value: multiple names
+ EOE
+ }
+
+ : unset
+ :
+ $* <'print $getenv("non-existent-var-name")' >'[null]'
+}