From c8194c30dff48a4f3bf3d0fc496dd9a219f77fee Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Fri, 16 Jul 2021 16:38:58 +0200 Subject: [PATCH] MINOR: cfgcond: remerge all arguments into a single line Till now we were dealing with single-word expressions but in order to extend the configuration condition language a bit more, we'll need to support slightly more complex expressions involving operators, and we must absolutely support spaces around them to keep them readable. As all arguments are pointers to the same line with spaces replaced by zeroes, we can trivially rebuild the whole line before calling the condition evaluator, and remove the test for extraneous argument. This is what this patch does. --- src/cfgparse.c | 20 ++++++++------------ src/haproxy.c | 7 ++++++- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/cfgparse.c b/src/cfgparse.c index 4cee8a7b6..fed8f7e59 100644 --- a/src/cfgparse.c +++ b/src/cfgparse.c @@ -1889,13 +1889,11 @@ int readcfgfile(const char *file) const char *errptr = NULL; char *errmsg = NULL; int cond; + char *w; - if (*args[2]) { - ha_alert("parsing [%s:%d]: Unexpected argument '%s' for '%s'.\n", - file, linenum, args[2], args[0]); - err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT; - break; - } + /* remerge all words into a single expression */ + for (w = *args; (w += strlen(w)) < outline + outlen - 1; *w = ' ') + ; nested_cond_lvl++; if (nested_cond_lvl >= MAXNESTEDCONDS) { @@ -1938,13 +1936,11 @@ int readcfgfile(const char *file) const char *errptr = NULL; char *errmsg = NULL; int cond; + char *w; - if (*args[2]) { - ha_alert("parsing [%s:%d]: Unexpected argument '%s' for '%s'.\n", - file, linenum, args[2], args[0]); - err_code |= ERR_ALERT | ERR_FATAL | ERR_ABORT; - break; - } + /* remerge all words into a single expression */ + for (w = *args; (w += strlen(w)) < outline + outlen - 1; *w = ' ') + ; if (!nested_cond_lvl) { ha_alert("parsing [%s:%d]: lone '.elif' with no matching '.if'.\n", file, linenum); diff --git a/src/haproxy.c b/src/haproxy.c index c863e13ec..a4cbce5e3 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -1807,6 +1807,7 @@ static void init(int argc, char **argv) char *args[MAX_LINE_ARGS+1]; int arg = sizeof(args) / sizeof(*args); size_t outlen = strlen(check_condition) + 1; + char *w; err = parse_line(check_condition, check_condition, &outlen, args, &arg, PARSE_OPT_ENV | PARSE_OPT_WORD_EXPAND | PARSE_OPT_DQUOTE | PARSE_OPT_SQUOTE | PARSE_OPT_BKSLASH, @@ -1827,7 +1828,7 @@ static void init(int argc, char **argv) exit(2); } - if ((err & PARSE_ERR_TOOMANY) || *args[1]) { + if (err & PARSE_ERR_TOOMANY) { ha_alert("Error in condition: Too many words.\n"); exit(2); } @@ -1837,6 +1838,10 @@ static void init(int argc, char **argv) exit(2); } + /* remerge all words into a single expression */ + for (w = *args; (w += strlen(w)) < check_condition + outlen - 1; *w = ' ') + ; + result = cfg_eval_condition(args, &errmsg, &errptr); if (result < 0) {