about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2022-06-09 10:22:37 +0000
committerLaurent Bercot <ska@appnovation.com>2022-06-09 10:22:37 +0000
commit8d3e4e8c12984565ca30fe58c4612c8a85bc4d43 (patch)
treedcdeb079a2de753b2e8ba1be0927df435b211494 /src
parent6910ea0e98ca11275c006d609d432e47c321054d (diff)
downloads6-portable-utils-8d3e4e8c12984565ca30fe58c4612c8a85bc4d43.tar.gz
s6-portable-utils-8d3e4e8c12984565ca30fe58c4612c8a85bc4d43.tar.xz
s6-portable-utils-8d3e4e8c12984565ca30fe58c4612c8a85bc4d43.zip
Add =~ operator to s6-test
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src')
-rw-r--r--src/skaembutils/s6-test.c25
1 files changed, 22 insertions, 3 deletions
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 <unistd.h>
 #include <string.h>
 #include <stdlib.h>
+#include <regex.h>
+
+#include <skalibs/posixplz.h>
 #include <skalibs/types.h>
 #include <skalibs/strerr2.h>
 #include <skalibs/djbunix.h>
@@ -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: