MINOR: cfgcond: Implement strstr condition expression

Implement a way to match a substring in a string. The strstr expresionn can
now be used to do so.
This commit is contained in:
Christopher Faulet 2023-02-20 17:55:58 +01:00
parent 760a3841bd
commit a1fdad784b
3 changed files with 7 additions and 0 deletions

View File

@ -865,6 +865,7 @@ The list of currently supported predicates is the following:
- streq(<str1>,<str2>) : returns true only if the two strings are equal
- strneq(<str1>,<str2>) : returns true only if the two strings differ
- strstr(<str1>,<str2>) : returns true only if the second string is found in the first one
- version_atleast(<ver>): returns true if the current haproxy version is
at least as recent as <ver> otherwise false. The

View File

@ -48,6 +48,7 @@ enum cond_predicate {
CFG_PRED_FEATURE, // "feature"
CFG_PRED_STREQ, // "streq"
CFG_PRED_STRNEQ, // "strneq"
CFG_PRED_STRSTR, // "strstr"
CFG_PRED_VERSION_ATLEAST, // "version_atleast"
CFG_PRED_VERSION_BEFORE, // "version_before"
CFG_PRED_OSSL_VERSION_ATLEAST, // "openssl_version_atleast"

View File

@ -22,6 +22,7 @@ const struct cond_pred_kw cond_predicates[] = {
{ "feature", CFG_PRED_FEATURE, ARG1(1, STR) },
{ "streq", CFG_PRED_STREQ, ARG2(2, STR, STR) },
{ "strneq", CFG_PRED_STRNEQ, ARG2(2, STR, STR) },
{ "strstr", CFG_PRED_STRSTR, ARG2(2, STR, STR) },
{ "version_atleast", CFG_PRED_VERSION_ATLEAST, ARG1(1, STR) },
{ "version_before", CFG_PRED_VERSION_BEFORE, ARG1(1, STR) },
{ "openssl_version_atleast", CFG_PRED_OSSL_VERSION_ATLEAST, ARG1(1, STR) },
@ -225,6 +226,10 @@ int cfg_eval_cond_term(const struct cfg_cond_term *term, char **err)
ret = strcmp(term->args[0].data.str.area, term->args[1].data.str.area) != 0;
break;
case CFG_PRED_STRSTR: // checks if the 2nd arg is found in the first one
ret = strstr(term->args[0].data.str.area, term->args[1].data.str.area) != NULL;
break;
case CFG_PRED_VERSION_ATLEAST: // checks if the current version is at least this one
ret = compare_current_version(term->args[0].data.str.area) <= 0;
break;