From 8d3e4e8c12984565ca30fe58c4612c8a85bc4d43 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Thu, 9 Jun 2022 10:22:37 +0000 Subject: Add =~ operator to s6-test Signed-off-by: Laurent Bercot --- doc/s6-test.html | 15 +++++++++++++++ src/skaembutils/s6-test.c | 25 ++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/doc/s6-test.html b/doc/s6-test.html index 3764a5c..a8424cd 100644 --- a/doc/s6-test.html +++ b/doc/s6-test.html @@ -41,6 +41,16 @@ disambiguation technique that has unfortunately not been chosen by the standard. s6-test accepts an arbitrary number of arguments.

+

Exit codes

+ + +

Posixness

@@ -55,6 +65,11 @@ exact same behaviour.

diff --git a/src/skaembutils/s6-test.c b/src/skaembutils/s6-test.c index be8201b..4bbd5ee 100644 --- a/src/skaembutils/s6-test.c +++ b/src/skaembutils/s6-test.c @@ -4,6 +4,9 @@ #include #include #include +#include + +#include #include #include #include @@ -53,7 +56,8 @@ enum opnum T_NUMGREATERE, T_NUMLESSER, T_NUMLESSERE, - T_ENV + T_ENV, + T_MATCH } ; struct token @@ -74,7 +78,7 @@ struct node static unsigned int lex (struct node *tree, char const *const *argv) { - static struct token const tokens[45] = + static struct token const tokens[46] = { { "-n", T_NONZERO, 2 }, { "-z", T_ZERO, 2 }, @@ -120,6 +124,7 @@ static unsigned int lex (struct node *tree, char const *const *argv) { ">", T_STRGREATER, 3 }, { ">=", T_STRGREATERE, 3 }, { "-v", T_ENV, 2 }, + { "=~", T_MATCH, 3 }, { 0, 0, 0 } } ; unsigned int pos = 0 ; @@ -492,8 +497,22 @@ static int run (struct node const *tree, unsigned int root) } case T_ENV : return !!getenv(tree[tree[root].arg1].data) ; + case T_MATCH : + { + regex_t re ; + int r = skalibs_regcomp(&re, tree[tree[root].arg2].data, REG_EXTENDED | REG_NOSUB) ; + if (r) + { + char buf[256] ; + regerror(r, &re, buf, 256) ; + strerr_diefu5x(r == REG_ESPACE ? 111 : 100, "compile ", tree[tree[root].arg2].data, " into a", " regular expression: ", buf) ; + } + r = regexec(&re, tree[tree[root].arg1].data, 0, 0, 0) ; + regfree(&re) ; + return !r ; + } default: - strerr_dief1x(111, "operation not implemented") ; + strerr_dief1x(101, "operation not implemented") ; } errorint: -- cgit v1.2.3