From 42ed14b529ee090a5e98631a054df36a4695bd7c Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Thu, 6 May 2021 15:55:14 +0200 Subject: [PATCH] MINOR: config: add predicate "defined()" to conditional expression blocks "defined(name)" will return true if is a defined environment variable otherwise false, regardless of its contents. --- doc/configuration.txt | 15 +++++++++++++-- src/cfgparse.c | 6 ++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/doc/configuration.txt b/doc/configuration.txt index a6af2f267..ff90526d8 100644 --- a/doc/configuration.txt +++ b/doc/configuration.txt @@ -807,9 +807,20 @@ The conditions are currently limited to: - an empty string, always returns "false" - the integer zero ('0'), always returns "false" - a non-nul integer (e.g. '1'), always returns "true". + - a predicate optionally followed by argument(s) in parenthesis. -Other patterns are not supported yet but the purpose is to bring a few -functions to test for certain build options and supported features. +The list of currently supported predicates is the following: + + - defined() : returns true if an environment variable + exists, regardless of its contents + +Example: + + .if defined(HAPROXY_MWORKER) + listen mwcli_px + bind :1111 + ... + .endif Three other directives are provided to report some status: diff --git a/src/cfgparse.c b/src/cfgparse.c index c7a746fe6..d8ba76a8b 100644 --- a/src/cfgparse.c +++ b/src/cfgparse.c @@ -136,6 +136,7 @@ enum nested_cond_state { /* supported conditional predicates for .if/.elif */ enum cond_predicate { CFG_PRED_NONE, // none + CFG_PRED_DEFINED, // "defined" }; struct cond_pred_kw { @@ -146,6 +147,7 @@ struct cond_pred_kw { /* supported condition predicates */ const struct cond_pred_kw cond_predicates[] = { + { "defined", CFG_PRED_DEFINED, ARG1(1, STR) }, { NULL, CFG_PRED_NONE, 0 } }; @@ -1712,6 +1714,10 @@ static int cfg_eval_condition(char **args, char **err, const char **errptr) * arguments, placed in (which we'll need to free). */ switch (cond_pred->prd) { + case CFG_PRED_DEFINED: // checks if arg exists as an environment variable + ret = getenv(argp[0].data.str.area) != NULL; + goto done; + default: memprintf(err, "internal error: unhandled conditional expression predicate '%s'", cond_pred->word); if (errptr)