about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--doc/s6-test.html15
-rw-r--r--src/skaembutils/s6-test.c25
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.
 </p>
 
+<h2> Exit codes </h2>
+
+<ul>
+ <li> 0: the test is true </li>
+ <li> 1: the test is false </li>
+ <li> 100: wrong usage </li>
+ <li> 101: internal error (should never happen, warrants a bug-report) </li>
+ <li> 111: system call failure </li>
+</ul>
+
 <h2> Posixness </h2>
 
 <p>
@@ -55,6 +65,11 @@ exact same behaviour.
 <ul>
  <li> <tt>-v&nbsp;<em>VAR</em></tt>&nbsp;: tests whether the
 <em>VAR</em> variable is defined in the current environment. </li>
+ <li> <tt><em>string</em>&nbsp;=~&nbsp;<em>pattern</em></tt>&nbsp;:
+tries to match <em>string</em> against extended regular expression
+<em>pattern</em>. True if any part of <em>string</em> matches <em>pattern</em>;
+in order to match whole strings, you must anchor <em>pattern</em> with
+<tt>^</tt> and <tt>$</tt> markers. </li>
 </ul>
 
 </body>
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: