]> thyrsus.com Git - comparator.git/commitdiff
Now seems to work.
authorEric S. Raymond <esr@thyrsus.com>
Wed, 17 Sep 2003 21:01:02 +0000 (21:01 +0000)
committerEric S. Raymond <esr@thyrsus.com>
Wed, 17 Sep 2003 21:01:02 +0000 (21:01 +0000)
linebyline.c

index d1b86fc7fc6458a7ea112d0ba3e6878ad709fcfe..f15611ddce64dd160fd85097a78d76a78a646c97 100644 (file)
@@ -13,31 +13,29 @@ static char *c_patterns[] = {
     "/\\* *NOTREACHED *\\*/", 
     "/\\* *FALL *THRO?UG?H? *\\*/",
     /* Bare C keywords */
-
-    "[:<:]break[:>:]",  "[:<:]case[:>:]","[:<:]continue[:>:]", 
-    "[:<:]default[:>:]", "[:<:]do[:>:]", "[:<:]else[:>:]", 
-    "[:<:]enum[:>:]", "[:<:]if[:>:]", "[:<:]goto[:>:]",
-    "[:<:]return[:>:]", "[:<:]switch[:>:]", "[:<:]while[:>:]",
-    "[:<:]enum[:>:]", "[:<:]int[:>:]", "[:<:]long[:>:]",
-    "[:<:]short[:>:]", "[:<:]static[:>:]", "[:<:]struct[:>:]",
-    "[:<:]typedef[:>:]", "[:<:]union[:>:]", "[:<:]void[:>:]",
+    " break ",  " case "," continue ", 
+    " default ", " do ", " else ", 
+    " enum ", " if ", " goto ",
+    " return ", " switch ", " while ",
+    " enum ", " int ", " long ",
+    " short ", " static ", " struct ",
+    " typedef ", " union ", " void ",
     /* Preprocessor constructs */
-    "/* *define","# *endif","# *else","# *if[:>:]",
-    "# *ifdef[:>:]","# *ifndef[:>:]",
+    "# *define","# *endif","# *else","# *if ", "# *ifdef ","# *ifndef ",
     /* Comment delimiters with no content, and line continuation */
     "/\\*+", "\\*+/", "\\*+", "^ *\\s*\\n", "\\\n",
     /* Common preprocessor macros, not significant by themselves. */
-    "[:<:]ASSERT[:>:]", 
-    "[:<:]FALSE[:>:]", 
-    "[:<:]NULL[:>:]",
-    "[:<:]STATIC[:>:]", 
-    "[:<:]TRUE[:>:]",
+    " ASSERT ", 
+    " FALSE ", 
+    " NULL ",
+    " STATIC ", 
+    " TRUE ",
     /* Macro include lines are noise, too. */
     "\\s*#include.*","#\\s*line.*",
     /* Common error macros. */
-    "[:<:]EFAULT[:>:]",
-    "[:<:]EINVAL[:>:]",
-    "[:<:]ENOSYS[:>:]",
+    " EFAULT ",
+    " EINVAL ",
+    " ENOSYS ",
 };
 static regex_t c_regexps[sizeof(c_patterns)/sizeof(*c_patterns)];
 
@@ -53,10 +51,10 @@ static char *shell_patterns[] = {
     /* Idioms that don't convey any meaning in isolation */
     "exit *[01];?",
     /* Bare shell keywords */
-    "[:<:]break[:>:]", "[:<:]case[:>:]", "[:<:]done[:>:]", 
-    "[:<:]do[:>:]", "[:<:]else[:>:]", "[:<:]esac[:>:]",
-    "[:<:]fi[:>:]", "[:<:]if[:>:]", "[:<:]return[:>:]",
-    "[:<:]shift[:>:]", "[:<:]true[:>:]", "[:<:]while[:>:]", 
+    " break ", " case ", " done ", 
+    " do ", " else ", " esac ",
+    " fi ", " if ", " return ",
+    " shift ", " true ", " while ", 
     /* Blank comment */
     "^#\n",
 };
@@ -64,7 +62,7 @@ static regex_t shell_regexps[sizeof(shell_patterns)/sizeof(*shell_patterns)];
 
 static char shell_punct[] = {
     /* Punctuation and whitespace */
-    ':', ';', '\n','\t', '\v',
+    ' ', ':', ';', '\n','\t', '\v',
 };
 
 void filter_init(void)
@@ -89,7 +87,9 @@ void filter_init(void)
 }
 
 static filtertype      active = none;
-static char *punct, **patterns;
+static char *punct;
+static regex_t *regexps;
+static int nregexps;
 
 void filter_set(filtertype newfilter)
 {
@@ -97,13 +97,15 @@ void filter_set(filtertype newfilter)
 
     if (newfilter == c)
     {
-       patterns = c_patterns;
+       regexps = c_regexps;
        punct = c_punct;
+       nregexps = sizeof(c_regexps)/sizeof(*c_regexps);
     }
     else
     {
-       patterns = shell_patterns;
+       regexps = shell_regexps;
        punct = shell_punct;
+       nregexps = sizeof(shell_regexps)/sizeof(*shell_regexps);
     }
 }
 
@@ -111,38 +113,50 @@ int filter_pass(const char *line)
 {
     if (active == none)
        return(1);
+    else if (strspn(line, punct) == strlen(line))
+       return(0);
     else
     {
-       char    buf[2][BUFSIZ];
-       int     changed , i, flip = 0;
-
-       buf[0][0] = ' ';
-       strncpy(buf[0]+1, line, BUFSIZ-2);
+       char    *sp, *tp;
+       char    buf[BUFSIZ];
+       int     changed;
+
+       /* change all punctuation to spaces */
+       buf[0] = ' ';
+       for (sp = (char *)line, tp = buf+1; *sp; sp++)
+           if (strchr(punct, *sp) && *sp != ' ')
+               *tp++ = ' ';
+           else
+               *tp++ = *sp;
+       *tp = '\0';
 
        do {
-           char *s, *t, *sp, *tp;
+           regex_t     *re;
 
-           s = buf[flip]; t = buf[!flip]; flip ^= 1; 
            changed = 0;
 
-           /* remove all punctuation */
-           for (sp = s, tp = t; *sp; sp++)
-               if (strchr(punct, *sp))
-                   changed++;
-               else
-                   *tp++ = *sp;
-           *tp = '\0';
-
            /* replace all regexps with the empty string */
+           for (re = regexps; re < regexps + nregexps; re++)
+           {
+               regmatch_t f;
 
-           if (t[0] == '\0')
-               return(0);
-       } while 
+               if (!regexec(re, buf, 1, &f, 0))
+               {
+                   char        *start, *end;
+
+                   for (start = buf+f.rm_so, end = buf+f.rm_eo; *end; end++)
+                       *start++ = *end;
+                   *start = '\0'; 
+                   changed++;
+               }
+           }
+       } while
            (changed);
-       return(1);
+       return !(buf[0] == '\0' || strspn(buf, " ") == strlen(buf));
     }
 }
 
+#ifdef TEST
 int main(int argc, char *argv[])
 {
     char       buf[BUFSIZ];
@@ -158,5 +172,6 @@ int main(int argc, char *argv[])
        fputs(buf, stdout);
     }
 }
+#endif /* TEST */
 
 /* filter.c ends here */