OSDN Git Service

Move regexec0 into lib (regexec that takes length and matches after NUL).
authorRob Landley <rob@landley.net>
Tue, 26 Jul 2016 18:35:56 +0000 (13:35 -0500)
committerRob Landley <rob@landley.net>
Tue, 26 Jul 2016 18:35:56 +0000 (13:35 -0500)
lib/lib.c
lib/lib.h
toys/posix/sed.c

index 09264c9..5c7696d 100644 (file)
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -1149,3 +1149,38 @@ int readlink0(char *path, char *buf, int len)
 {
   return readlinkat0(AT_FDCWD, path, buf, len);
 }
+
+// Do regex matching handling embedded NUL bytes in string (hence extra len
+// argument). Note that neither the pattern nor the match can currently include
+// NUL bytes (even with wildcards) and string must be null terminated at
+// string[len]. But this can find a match after the first NUL.
+int regexec0(regex_t *preg, char *string, long len, int nmatch,
+  regmatch_t pmatch[], int eflags)
+{
+  char *s = string;
+
+  for (;;) {
+    long ll = 0;
+    int rc;
+
+    while (len && !*s) {
+      s++;
+      len--;
+    }
+    while (s[ll] && ll<len) ll++;
+
+    rc = regexec(preg, s, nmatch, pmatch, eflags);
+    if (!rc) {
+      for (rc = 0; rc<nmatch && pmatch[rc].rm_so!=-1; rc++) {
+        pmatch[rc].rm_so += s-string;
+        pmatch[rc].rm_eo += s-string;
+      }
+
+      return 0;
+    }
+    if (ll==len) return rc;
+
+    s += ll;
+    len -= ll;
+  }
+}
index f97940a..c3aa7e7 100644 (file)
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -218,6 +218,8 @@ struct passwd *bufgetpwuid(uid_t uid);
 struct group *bufgetgrgid(gid_t gid);
 int readlinkat0(int dirfd, char *path, char *buf, int len);
 int readlink0(char *path, char *buf, int len);
+int regexec0(regex_t *preg, char *string, long len, int nmatch,
+  regmatch_t pmatch[], int eflags);
 
 #define HR_SPACE 1 // Space between number and units
 #define HR_B     2 // Use "B" for single byte units
index 45331ba..063c20c 100644 (file)
@@ -219,41 +219,6 @@ static int emit(char *line, long len, int eol)
   return 0;
 }
 
-// Do regex matching handling embedded NUL bytes in string. Note that
-// neither the pattern nor the match can currently include NUL bytes
-// (even with wildcards) and string must be null terminated at string[len].
-// But this can find a match after the first NUL.
-static int regex_null(regex_t *preg, char *string, long len, int nmatch,
-  regmatch_t pmatch[], int eflags)
-{
-  char *s = string;
-
-  for (;;) {
-    long ll = 0;
-    int rc;
-
-    while (len && !*s) {
-      s++;
-      len--;
-    }
-    while (s[ll] && ll<len) ll++;
-
-    rc = regexec(preg, s, nmatch, pmatch, eflags);
-    if (!rc) {
-      for (rc = 0; rc<nmatch && pmatch[rc].rm_so!=-1; rc++) {
-        pmatch[rc].rm_so += s-string;
-        pmatch[rc].rm_eo += s-string;
-      }
-          
-      return 0;
-    }
-    if (ll==len) return rc;
-
-    s += ll;
-    len -= ll;
-  }
-}
-
 // Extend allocation to include new string, with newline between if newlen<0
 
 static char *extend_string(char **old, char *new, int oldlen, int newlen)
@@ -330,7 +295,7 @@ static void process_line(char **pline, long plen)
             void *rm = get_regex(command, command->rmatch[1]);
 
             // regex match end includes matching line, so defer deactivation
-            if (line && !regex_null(rm, line, len, 0, 0, 0)) miss = 1;
+            if (line && !regexec0(rm, line, len, 0, 0, 0)) miss = 1;
           }
         } else if (lm > 0 && lm < TT.count) command->hit = 0;
 
@@ -339,7 +304,7 @@ static void process_line(char **pline, long plen)
         if (!(lm = *command->lmatch)) {
           void *rm = get_regex(command, *command->rmatch);
 
-          if (line && !regex_null(rm, line, len, 0, 0, 0)) command->hit++;
+          if (line && !regexec0(rm, line, len, 0, 0, 0)) command->hit++;
         } else if (lm == TT.count || (lm == -1 && !pline)) command->hit++;
 
         if (!command->lmatch[1] && !command->rmatch[1]) miss = 1;
@@ -501,7 +466,7 @@ static void process_line(char **pline, long plen)
       int mflags = 0, count = 0, zmatch = 1, rlen = len, mlen, off, newlen;
 
       // Find match in remaining line (up to remaining len)
-      while (!regex_null(reg, rline, rlen, 10, match, mflags)) {
+      while (!regexec0(reg, rline, rlen, 10, match, mflags)) {
         mflags = REG_NOTBOL;
 
         // Zero length matches don't count immediately after a previous match