MINOR: config: add predicate "defined()" to conditional expression blocks

"defined(name)" will return true if <name> is a defined environment variable
otherwise false, regardless of its contents.
This commit is contained in:
Willy Tarreau 2021-05-06 15:55:14 +02:00
parent 732525fae7
commit 42ed14b529
2 changed files with 19 additions and 2 deletions

View File

@ -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(<name>) : returns true if an environment variable <name>
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:

View File

@ -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 <argp> (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)