From ce4848cfaaa569da3e11de51f4174eb21709b9b9 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 12 Apr 2024 17:09:35 +0200 Subject: Add -s|--timeout-success option to env script builtin The semantics is equivalent to the --success option we already have in the timeout builtin. --- doc/testscript.cli | 5 +++++ libbuild2/script/parser.cxx | 14 +++++++++++--- libbuild2/script/parser.hxx | 9 +++++---- libbuild2/script/run.cxx | 2 +- libbuild2/script/script.cxx | 5 +++++ libbuild2/script/script.hxx | 10 +++++++--- 6 files changed, 34 insertions(+), 11 deletions(-) diff --git a/doc/testscript.cli b/doc/testscript.cli index 20d9c2d..c539903 100644 --- a/doc/testscript.cli +++ b/doc/testscript.cli @@ -2777,6 +2777,11 @@ env - --unset=FOO -- $* Terminate the command if it fails to complete within the specified number of seconds. See also the \l{#builtins-timeout \c{timeout}} builtin.| +\li|\n\c{-s|--timeout-success} + + Assume the command terminated due to the timeout specified with the + \c{-t|--timeout} option to have succeeded.| + \li|\n\c{-c|--cwd } Change the command's working directory.| diff --git a/libbuild2/script/parser.cxx b/libbuild2/script/parser.cxx index ae6da76..84d2afc 100644 --- a/libbuild2/script/parser.cxx +++ b/libbuild2/script/parser.cxx @@ -1134,9 +1134,10 @@ namespace build2 if (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.cwd = move (r.cwd); + c.variables = move (r.variables); + c.timeout = r.timeout; + c.timeout_success = r.timeout_success; env = true; } else if (t.value == "for") @@ -1601,6 +1602,10 @@ namespace build2 { r.timeout = chrono::seconds (*v); } + else if (o == "-s" || o == "--timeout-success") + { + r.timeout_success = true; + } else if (optional v = dir ("--cwd", "-c")) { r.cwd = move (*v); @@ -1615,6 +1620,9 @@ namespace build2 break; } + if (r.timeout_success && !r.timeout) + fail (l) << "env: -s|--timeout-success specified without -t|--timeout"; + // Parse arguments (variable sets). // for (; i != e; ++i) diff --git a/libbuild2/script/parser.hxx b/libbuild2/script/parser.hxx index cadf909..795ce4e 100644 --- a/libbuild2/script/parser.hxx +++ b/libbuild2/script/parser.hxx @@ -163,14 +163,15 @@ namespace build2 pre_parse_line_start (token&, token_type&, lexer_mode); // Parse the env pseudo-builtin arguments up to the program name. Return - // the program execution timeout, CWD, the list of the variables that - // should be unset ("name") and/or set ("name=value") in the command - // environment, and the token/type that starts the program name. Note - // that the variable unsets come first, if present. + // the program execution timeout and its success flag, CWD, the list of + // the variables that should be unset ("name") and/or set ("name=value") + // in the command environment, and the token/type that starts the + // program name. Note that the variable unsets come first, if present. // struct parsed_env { optional timeout; + bool timeout_success = false; optional cwd; environment_vars variables; }; diff --git a/libbuild2/script/run.cxx b/libbuild2/script/run.cxx index 6d73a7e..f8f98c1 100644 --- a/libbuild2/script/run.cxx +++ b/libbuild2/script/run.cxx @@ -2069,7 +2069,7 @@ namespace build2 if (c.timeout) { - deadline d (system_clock::now () + *c.timeout, false /* success */); + deadline d (system_clock::now () + *c.timeout, c.timeout_success); if (!dl || d < *dl) dl = d; } diff --git a/libbuild2/script/script.cxx b/libbuild2/script/script.cxx index 4a6ca33..b53fc23 100644 --- a/libbuild2/script/script.cxx +++ b/libbuild2/script/script.cxx @@ -425,9 +425,14 @@ namespace build2 // Timeout. // if (c.timeout) + { o << " -t " << chrono::duration_cast (*c.timeout).count (); + if (c.timeout_success) + o << " -s"; + } + // CWD. // if (c.cwd) diff --git a/libbuild2/script/script.hxx b/libbuild2/script/script.hxx index cccad98..c406165 100644 --- a/libbuild2/script/script.hxx +++ b/libbuild2/script/script.hxx @@ -331,9 +331,13 @@ namespace build2 process_path program; strings arguments; - optional cwd; // From env builtin. - environment_vars variables; // From env builtin. - optional timeout; // From env builtin. + + // These come from the env builtin. + // + optional cwd; + environment_vars variables; + optional timeout; + bool timeout_success; optional in; optional out; -- cgit v1.1