"/\\* *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)];
/* 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",
};
static char shell_punct[] = {
/* Punctuation and whitespace */
- ':', ';', '\n','\t', '\v',
+ ' ', ':', ';', '\n','\t', '\v',
};
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)
{
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);
}
}
{
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];
fputs(buf, stdout);
}
}
+#endif /* TEST */
/* filter.c ends here */