From 4564a26c0b88d684c12c396d7ef5b0e66f686964 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Wed, 13 Oct 2021 20:05:27 +0300 Subject: Add --cwd|-t option to env pseudo-builtin --- libbuild2/script/parser.cxx | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) (limited to 'libbuild2/script/parser.cxx') diff --git a/libbuild2/script/parser.cxx b/libbuild2/script/parser.cxx index ebfd5fc..f234d58 100644 --- a/libbuild2/script/parser.cxx +++ b/libbuild2/script/parser.cxx @@ -1028,8 +1028,9 @@ namespace build2 if (prog && tt == type::word && t.value == "env") { parsed_env r (parse_env_builtin (t, tt)); + c.cwd = move (r.cwd); c.variables = move (r.variables); - c.timeout = r.timeout; + c.timeout = r.timeout; env = true; } @@ -1411,10 +1412,16 @@ namespace build2 return r; }; + auto bad = [&i, &o, this] (const string& v) + { + fail (i->second) << "env: invalid value '" << v << "' for option '" + << o << "'"; + }; + // As above but convert the option value to a number and fail on // error. // - auto num = [&i, &o, &str, this] (const char* ln, const char* sn) + auto num = [&str, &bad] (const char* ln, const char* sn) { optional r; if (optional s = str (ln, sn)) @@ -1422,8 +1429,29 @@ namespace build2 r = parse_number (*s); if (!r) - fail (i->second) << "env: invalid value '" << *s - << "' for option '" << o << "'"; + bad (*s); + } + + return r; + }; + + // As above but convert the option value to a directory path and fail + // on error. + // + auto dir = [&str, &bad] (const char* ln, const char* sn) + { + optional r; + if (optional s = str (ln, sn)) + try + { + // Note that we don't need to check that the path is not empty, + // since str() fails for empty values. + // + r = dir_path (move (*s)); + } + catch (const invalid_path& e) + { + bad (e.path); } return r; @@ -1435,6 +1463,10 @@ namespace build2 { r.timeout = chrono::seconds (*v); } + else if (optional v = dir ("--cwd", "-c")) + { + r.cwd = move (*v); + } else if (optional v = str ("--unset", "-u")) { verify_environment_var_name (*v, "env: ", i->second, o.c_str ()); -- cgit v1.1