summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2021-11-26 07:00:11 +0000
committerLaurent Bercot <ska@appnovation.com>2021-11-26 07:00:11 +0000
commit45ca80e0e1509c613f05cdb5fe8ec1157a4a7a48 (patch)
tree08da8661a666889a0e84201af36f3d8834aeac10 /src
parent901efc65acfee5cad65a6ed84fc540c5ba6ebdf0 (diff)
downloadexecline-45ca80e0e1509c613f05cdb5fe8ec1157a4a7a48.tar.gz
execline-45ca80e0e1509c613f05cdb5fe8ec1157a4a7a48.tar.xz
execline-45ca80e0e1509c613f05cdb5fe8ec1157a4a7a48.zip
Add shell matching option to case
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src')
-rw-r--r--src/execline/case.c68
1 files changed, 43 insertions, 25 deletions
diff --git a/src/execline/case.c b/src/execline/case.c
index 77f905e..b026256 100644
--- a/src/execline/case.c
+++ b/src/execline/case.c
@@ -2,6 +2,7 @@
 
 #include <string.h>
 #include <regex.h>
+#include <fnmatch.h>
 
 #include <skalibs/gccattributes.h>
 #include <skalibs/types.h>
@@ -11,7 +12,7 @@
 
 #include <execline/execline.h>
 
-#define USAGE "case [ -e | -E ] [ -n | -N ] [ -i ] value { re1 { prog1... } re2 { prog2... } ... } progdefault... "
+#define USAGE "case [ -s | -S ] [ -e | -E ] [ -n | -N ] [ -i ] value { re1 { prog1... } re2 { prog2... } ... } progdefault... "
 #define dieusage() strerr_dieusage(100, USAGE)
 
 static void execit (char const *const *argv, char const *expr, char const *s, regmatch_t const *pmatch, size_t n) gccattr_noreturn ;
@@ -47,6 +48,7 @@ static void execit (char const *const *argv, char const *expr, char const *s, re
 
 int main (int argc, char const **argv, char const *const *envp)
 {
+  int flagshell = 0 ;
   int flagextended = 1 ;
   int flagnosub = 1 ;
   int flagicase = 0 ;
@@ -58,10 +60,12 @@ int main (int argc, char const **argv, char const *const *envp)
     subgetopt l = SUBGETOPT_ZERO ;
     for (;;)
     {
-      int opt = subgetopt_r(argc, argv, "eEnNi", &l) ;
+      int opt = subgetopt_r(argc, argv, "sSeEnNi", &l) ;
       if (opt == -1) break ;
       switch (opt)
       {
+        case 's' : flagshell = 1 ; break ;
+        case 'S' : flagshell = 0 ; break ;
         case 'e' : flagextended = 0 ; break ;
         case 'E' : flagextended = 1 ; break ;
         case 'N' : flagnosub = 0 ; break ;
@@ -82,42 +86,56 @@ int main (int argc, char const **argv, char const *const *envp)
   {
     char const *expr = argv[i++] ;
     int argc2 ;
-    regex_t re ;
     if (i == argc1) strerr_dief1x(100, "malformed case block") ;
     argc2 = el_semicolon(argv + i) ;
     if (i + argc2 >= argc1) strerr_dief1x(100, "unterminated regex block") ;
+    if (flagshell)
     {
-      int r ;
-      size_t len = strlen(expr) ;
-      char tmp[len+3] ;
-      tmp[0] = '^' ;
-      memcpy(tmp + 1, expr, len) ;
-      tmp[1+len] = '$' ;
-      tmp[2+len] = 0 ;
-      r = regcomp(&re, tmp, (flagextended ? REG_EXTENDED : 0) | (flagicase ? REG_ICASE : 0) | (flagnosub ? REG_NOSUB : 0) | REG_NEWLINE) ;
-      if (r)
+      int r = fnmatch(expr, s, (flagextended ? 0 : FNM_NOESCAPE) | (flagicase ? FNM_PERIOD : 0) | (flagnosub ? 0 : FNM_PATHNAME)) ;
+      if (!r)
       {
-        char buf[256] ;
-        regerror(r, &re, buf, 256) ;
-        strerr_diefu4x(r == REG_ESPACE ? 111 : 100, "regcomp \"^", argv[i], "$\": ", buf) ;
+        argv[i + argc2] = 0 ;
+        xexec0(argv + i) ;
       }
+      else if (r != FNM_NOMATCH)
+        strerr_warnw2x("invalid fnmatch pattern: ", expr) ;
     }
+    else
     {
-      regmatch_t pmatch[re.re_nsub && !flagnosub ? re.re_nsub + 1 : 1] ;
-      int r = regexec(&re, s, re.re_nsub + 1, pmatch, 0) ;
-      if (!r)
+      regex_t re ;
       {
-        argv[i + argc2] = 0 ;
-        execit(argv + i, expr, s, pmatch, flagnosub ? 0 : 1 + re.re_nsub) ;
+        int r ;
+        size_t len = strlen(expr) ;
+        char tmp[len+3] ;
+        tmp[0] = '^' ;
+        memcpy(tmp + 1, expr, len) ;
+        tmp[1+len] = '$' ;
+        tmp[2+len] = 0 ;
+        r = regcomp(&re, tmp, (flagextended ? REG_EXTENDED : 0) | (flagicase ? REG_ICASE : 0) | (flagnosub ? REG_NOSUB : 0) | REG_NEWLINE) ;
+        if (r)
+        {
+          char buf[256] ;
+          regerror(r, &re, buf, 256) ;
+          strerr_diefu4x(r == REG_ESPACE ? 111 : 100, "regcomp \"^", argv[i], "$\": ", buf) ;
+        }
       }
-      if (r != REG_NOMATCH)
       {
-        char buf[256] ;
-        regerror(r, &re, buf, 256) ;
-        strerr_diefu6x(111, "match string \"", s, "\" against regex \"", expr, "\": ", buf) ;
+        regmatch_t pmatch[re.re_nsub && !flagnosub ? re.re_nsub + 1 : 1] ;
+        int r = regexec(&re, s, re.re_nsub + 1, pmatch, 0) ;
+        if (!r)
+        {
+          argv[i + argc2] = 0 ;
+          execit(argv + i, expr, s, pmatch, flagnosub ? 0 : 1 + re.re_nsub) ;
+        }
+        if (r != REG_NOMATCH)
+        {
+          char buf[256] ;
+          regerror(r, &re, buf, 256) ;
+          strerr_diefu6x(111, "match string \"", s, "\" against regex \"", expr, "\": ", buf) ;
+        }
       }
+      regfree(&re) ;
     }
-    regfree(&re) ;
     i += argc2 + 1 ;
   }
   xexec0(argv + argc1 + 1) ;