diff options
-rw-r--r-- | src/filter.c | 148 |
1 files changed, 76 insertions, 72 deletions
diff --git a/src/filter.c b/src/filter.c index 9ec7858..3a955ce 100644 --- a/src/filter.c +++ b/src/filter.c @@ -54,84 +54,88 @@ void filter_init (void) char *s; int cflags; - if (!fl && !already_init) { - fd = fopen (config.filter, "r"); - if (fd) { - p = NULL; - - cflags = REG_NEWLINE | REG_NOSUB; - if (config.filter_extended) - cflags |= REG_EXTENDED; - if (!config.filter_casesensitive) - cflags |= REG_ICASE; - - while (fgets (buf, FILTER_BUFFER_LEN, fd)) { + if (fl || already_init) { + return; + } + + fd = fopen (config.filter, "r"); + if (!fd) { + return; + } + + p = NULL; + + cflags = REG_NEWLINE | REG_NOSUB; + if (config.filter_extended) + cflags |= REG_EXTENDED; + if (!config.filter_casesensitive) + cflags |= REG_ICASE; + + while (fgets (buf, FILTER_BUFFER_LEN, fd)) { + /* + * Remove any trailing white space and + * comments. + */ + s = buf; + while (*s) { + if (isspace ((unsigned char) *s)) + break; + if (*s == '#') { /* - * Remove any trailing white space and - * comments. + * If the '#' char is preceeded by + * an escape, it's not a comment + * string. */ - s = buf; - while (*s) { - if (isspace ((unsigned char) *s)) - break; - if (*s == '#') { - /* - * If the '#' char is preceeded by - * an escape, it's not a comment - * string. - */ - if (s == buf - || *(s - 1) != '\\') - break; - } - ++s; - } - *s = '\0'; - - /* skip leading whitespace */ - s = buf; - while (*s && isspace ((unsigned char) *s)) - s++; - - /* skip blank lines and comments */ - if (*s == '\0') - continue; - - if (!p) /* head of list */ - fl = p = - (struct filter_list *) - safecalloc (1, - sizeof (struct - filter_list)); - else { /* next entry */ - p->next = - (struct filter_list *) - safecalloc (1, - sizeof (struct - filter_list)); - p = p->next; - } - - p->pat = safestrdup (s); - p->cpat = - (regex_t *) safemalloc (sizeof (regex_t)); - err = regcomp (p->cpat, p->pat, cflags); - if (err != 0) { - fprintf (stderr, - "Bad regex in %s: %s\n", - config.filter, p->pat); - exit (EX_DATAERR); - } - } - if (ferror (fd)) { - perror ("fgets"); - exit (EX_DATAERR); + if (s == buf + || *(s - 1) != '\\') + break; } - fclose (fd); + ++s; + } + *s = '\0'; + + /* skip leading whitespace */ + s = buf; + while (*s && isspace ((unsigned char) *s)) + s++; + + /* skip blank lines and comments */ + if (*s == '\0') + continue; + + if (!p) /* head of list */ + fl = p = + (struct filter_list *) + safecalloc (1, + sizeof (struct + filter_list)); + else { /* next entry */ + p->next = + (struct filter_list *) + safecalloc (1, + sizeof (struct + filter_list)); + p = p->next; + } - already_init = 1; + p->pat = safestrdup (s); + p->cpat = + (regex_t *) safemalloc (sizeof (regex_t)); + err = regcomp (p->cpat, p->pat, cflags); + if (err != 0) { + fprintf (stderr, + "Bad regex in %s: %s\n", + config.filter, p->pat); + exit (EX_DATAERR); } } + if (ferror (fd)) { + perror ("fgets"); + exit (EX_DATAERR); + } + fclose (fd); + + already_init = 1; } /* unlink the list */ |