From 959cd8903fd012e63dbb156db56708dd3934b5df Mon Sep 17 00:00:00 2001 From: Werner Koch <wk@gnupg.org> Date: Mon, 5 Sep 2016 09:49:06 +0200 Subject: [PATCH] common: Add string operator gt,ge,le,lt to recsel. * common/recsel.c (recsel_parse_expr): Add them. (recsel_dump): Print them. (recsel_select): Evaluate them. Signed-off-by: Werner Koch <wk@gnupg.org> --- common/recsel.c | 57 +++++++++++++++++++++++++++++++++++++++++++++-- common/t-recsel.c | 33 +++++++++++++++++++++++++++ doc/gpg.texi | 14 +++++++++++- 3 files changed, 101 insertions(+), 3 deletions(-) diff --git a/common/recsel.c b/common/recsel.c index 5dc685fe9..866861b87 100644 --- a/common/recsel.c +++ b/common/recsel.c @@ -48,7 +48,11 @@ typedef enum SELECT_LE, SELECT_GE, SELECT_LT, - SELECT_GT + SELECT_GT, + SELECT_STRLE, /* String is less or equal. */ + SELECT_STRGE, + SELECT_STRLT, + SELECT_STRGT } select_op_t; @@ -347,6 +351,26 @@ recsel_parse_expr (recsel_expr_t *selector, const char *expression) se->op = SELECT_ISTRUE; s += 2; } + else if (!strncmp (s, "-le", 3)) + { + se->op = SELECT_STRLE; + s += 3; + } + else if (!strncmp (s, "-ge", 3)) + { + se->op = SELECT_STRGE; + s += 3; + } + else if (!strncmp (s, "-lt", 3)) + { + se->op = SELECT_STRLT; + s += 3; + } + else if (!strncmp (s, "-gt", 3)) + { + se->op = SELECT_STRGT; + s += 3; + } else { log_error ("invalid operator in expression\n"); @@ -467,7 +491,12 @@ recsel_dump (recsel_expr_t selector) se->op == SELECT_LT? "< ": se->op == SELECT_LE? "<=": se->op == SELECT_GT? "> ": - se->op == SELECT_GE? ">=":"[oops]", + se->op == SELECT_GE? ">=": + se->op == SELECT_STRLT? "-lt": + se->op == SELECT_STRLE? "-le": + se->op == SELECT_STRGT? "-gt": + se->op == SELECT_STRGE? "-ge": + /**/ "[oops]", se->value); } log_debug ("--- End selectors ---\n"); @@ -541,6 +570,30 @@ recsel_select (recsel_expr_t selector, case SELECT_LE: result = (numvalue <= se->numvalue); break; + case SELECT_STRGT: + if (se->xcase) + result = strcmp (value, se->value) > 0; + else + result = strcasecmp (value, se->value) > 0; + break; + case SELECT_STRGE: + if (se->xcase) + result = strcmp (value, se->value) >= 0; + else + result = strcasecmp (value, se->value) >= 0; + break; + case SELECT_STRLT: + if (se->xcase) + result = strcmp (value, se->value) < 0; + else + result = strcasecmp (value, se->value) < 0; + break; + case SELECT_STRLE: + if (se->xcase) + result = strcmp (value, se->value) <= 0; + else + result = strcasecmp (value, se->value) <= 0; + break; } } diff --git a/common/t-recsel.c b/common/t-recsel.c index fe2a7b9d0..faddc97ed 100644 --- a/common/t-recsel.c +++ b/common/t-recsel.c @@ -171,6 +171,8 @@ test_2_getval (void *cookie, const char *name) return " "; else if (!strcmp (name, "letters")) return "abcde"; + else if (!strcmp (name, "str1")) + return "aaa"; else return cookie; } @@ -263,6 +265,37 @@ run_test_2 (void) fail (0, 0); + FREEEXPR(); + ADDEXPR ("str1 -gt aa"); + if (!recsel_select (se, test_2_getval, NULL)) + fail (0, 0); + FREEEXPR(); + ADDEXPR ("str1 -gt aaa"); + if (recsel_select (se, test_2_getval, NULL)) + fail (0, 0); + FREEEXPR(); + ADDEXPR ("str1 -ge aaa"); + if (!recsel_select (se, test_2_getval, NULL)) + fail (0, 0); + FREEEXPR(); + ADDEXPR ("str1 -lt aab"); + if (!recsel_select (se, test_2_getval, NULL)) + fail (0, 0); + FREEEXPR(); + ADDEXPR ("str1 -le aaa"); + if (!recsel_select (se, test_2_getval, NULL)) + fail (0, 0); + + FREEEXPR(); + ADDEXPR ("-c str1 -lt AAB"); + if (recsel_select (se, test_2_getval, NULL)) + fail (0, 0); + FREEEXPR(); + ADDEXPR ("str1 -lt AAB"); + if (!recsel_select (se, test_2_getval, NULL)) + fail (0, 0); + + FREEEXPR(); ADDEXPR ("uid -n"); if (!recsel_select (se, test_2_getval, NULL)) diff --git a/doc/gpg.texi b/doc/gpg.texi index 68b21b62a..901d1ee15 100644 --- a/doc/gpg.texi +++ b/doc/gpg.texi @@ -3500,12 +3500,24 @@ The supported operators (@var{op}) are: @item < The numerical value of the field must be LT than the value. - @item >= + @item > The numerical value of the field must be GT than the value. @item >= The numerical value of the field must be GE than the value. + @item -le + The string value of the field must be less or equal than the value. + + @item -lt + The string value of the field must be less than the value. + + @item -gt + The string value of the field must be greater than the value. + + @item -ge + The string value of the field must be greater or equal than the value. + @item -n True if value is not empty (no value allowed).