grep -f support, adds only 60 Bytes, but regex's now compiled as

required, rather than being pre-compiled.
This commit is contained in:
Glenn L McGrath 2003-04-27 01:50:57 +00:00
parent e2aed7ffd7
commit 26df70a823

View File

@ -41,6 +41,7 @@ static int be_quiet = 0;
static int invert_search = 0; static int invert_search = 0;
static int suppress_err_msgs = 0; static int suppress_err_msgs = 0;
static int print_files_with_matches = 0; static int print_files_with_matches = 0;
static int fgrep_flag = 0;
#ifdef CONFIG_FEATURE_GREP_CONTEXT #ifdef CONFIG_FEATURE_GREP_CONTEXT
extern char *optarg; /* in getopt.h */ extern char *optarg; /* in getopt.h */
@ -51,8 +52,7 @@ static int last_line_printed = 0;
#endif /* CONFIG_FEATURE_GREP_CONTEXT */ #endif /* CONFIG_FEATURE_GREP_CONTEXT */
/* globals used internally */ /* globals used internally */
static regex_t *regexes = NULL; /* growable array of compiled regular expressions */ static llist_t *pattern_head = NULL; /* growable list of patterns to match */
static int nregexes = 0; /* number of elements in above arrary */
static int matched; /* keeps track of whether we ever matched */ static int matched; /* keeps track of whether we ever matched */
static char *cur_file = NULL; /* the current file we are reading */ static char *cur_file = NULL; /* the current file we are reading */
@ -81,7 +81,6 @@ static void grep_file(FILE *file)
int ret; int ret;
int linenum = 0; int linenum = 0;
int nmatches = 0; int nmatches = 0;
int i;
#ifdef CONFIG_FEATURE_GREP_CONTEXT #ifdef CONFIG_FEATURE_GREP_CONTEXT
int print_n_lines_after = 0; int print_n_lines_after = 0;
int curpos = 0; /* track where we are in the circular 'before' buffer */ int curpos = 0; /* track where we are in the circular 'before' buffer */
@ -89,16 +88,30 @@ static void grep_file(FILE *file)
#endif /* CONFIG_FEATURE_GREP_CONTEXT */ #endif /* CONFIG_FEATURE_GREP_CONTEXT */
while ((line = bb_get_chomped_line_from_file(file)) != NULL) { while ((line = bb_get_chomped_line_from_file(file)) != NULL) {
llist_t *pattern_ptr = pattern_head;
linenum++; linenum++;
for (i = 0; i < nregexes; i++) { while (pattern_ptr) {
/* if (fgrep_flag) {
* test for a postitive-assertion match (regexec returns success (0) if (strstr(line, pattern_ptr->data)) {
* and the user did not specify invert search), or a negative-assertion /* Match found */
* match (regexec returns failure (REG_NOMATCH) and the user specified ret = 0;
* invert search) } else {
*/ ret = 1;
ret = regexec(&regexes[i], line, 0, NULL, 0); }
} else {
/*
* test for a postitive-assertion match (regexec returns success (0)
* and the user did not specify invert search), or a negative-assertion
* match (regexec returns failure (REG_NOMATCH) and the user specified
* invert search)
*/
regex_t regex;
xregcomp(&regex, pattern_ptr->data, reflags);
ret = regexec(&regex, line, 0, NULL, 0);
regfree(&regex);
}
if ((ret == 0 && !invert_search) || (ret == REG_NOMATCH && invert_search)) { if ((ret == 0 && !invert_search) || (ret == REG_NOMATCH && invert_search)) {
/* if we found a match but were told to be quiet, stop here and /* if we found a match but were told to be quiet, stop here and
@ -164,6 +177,7 @@ static void grep_file(FILE *file)
print_n_lines_after--; print_n_lines_after--;
} }
#endif /* CONFIG_FEATURE_GREP_CONTEXT */ #endif /* CONFIG_FEATURE_GREP_CONTEXT */
pattern_ptr = pattern_ptr->link;
} /* for */ } /* for */
free(line); free(line);
} }
@ -193,21 +207,21 @@ static void grep_file(FILE *file)
matched = 1; matched = 1;
} }
#if 0
static void add_regex(const char *restr) static void add_pattern(char *restr)
{ {
regexes = xrealloc(regexes, sizeof(regex_t) * (++nregexes)); // regexes = xrealloc(regexes, sizeof(regex_t) * (++nregexes));
xregcomp(&regexes[nregexes-1], restr, reflags); // xregcomp(&regexes[nregexes-1], restr, reflags);
pattern_head = llist_add_to(pattern_head, restr);
} }
#endif
static void load_regexes_from_file(const char *filename) static void load_regexes_from_file(const char *filename)
{ {
char *line; char *line;
FILE *f = bb_xfopen(filename, "r"); FILE *f = bb_xfopen(filename, "r");
while ((line = bb_get_chomped_line_from_file(f)) != NULL) { while ((line = bb_get_chomped_line_from_file(f)) != NULL) {
add_regex(line); pattern_head = llist_add_to(pattern_head, line);
free(line);
} }
} }
@ -215,14 +229,18 @@ static void load_regexes_from_file(const char *filename)
#ifdef CONFIG_FEATURE_CLEAN_UP #ifdef CONFIG_FEATURE_CLEAN_UP
static void destroy_regexes(void) static void destroy_regexes(void)
{ {
if (regexes == NULL) llist_t *pattern_head_ptr;
if (pattern_head == NULL)
return; return;
/* destroy all the elments in the array */ /* destroy all the elments in the pattern list */
while (--nregexes >= 0) { while (pattern_head) {
regfree(&(regexes[nregexes])); pattern_head_ptr = pattern_head;
pattern_head = pattern_head->link;
free(pattern_head_ptr->data);
free(pattern_head_ptr);
} }
free(regexes);
} }
#endif #endif
@ -245,7 +263,7 @@ extern int grep_main(int argc, char **argv)
#endif #endif
/* do normal option parsing */ /* do normal option parsing */
while ((opt = getopt(argc, argv, "iHhlnqvsce:f:" while ((opt = getopt(argc, argv, "iHhlnqvsce:f:F"
#ifdef CONFIG_FEATURE_GREP_CONTEXT #ifdef CONFIG_FEATURE_GREP_CONTEXT
"A:B:C:" "A:B:C:"
#endif #endif
@ -282,13 +300,16 @@ extern int grep_main(int argc, char **argv)
print_match_counts++; print_match_counts++;
break; break;
case 'e': case 'e':
add_regex(optarg); pattern_head = llist_add_to(pattern_head, strdup(optarg));
break; break;
#ifdef CONFIG_FEATURE_GREP_EGREP_ALIAS #ifdef CONFIG_FEATURE_GREP_EGREP_ALIAS
case 'E': case 'E':
reflags |= REG_EXTENDED; reflags |= REG_EXTENDED;
break; break;
#endif #endif
case 'F':
fgrep_flag = 1;
break;
case 'f': case 'f':
load_regexes_from_file(optarg); load_regexes_from_file(optarg);
break; break;
@ -318,11 +339,11 @@ extern int grep_main(int argc, char **argv)
/* if we didn't get a pattern from a -e and no command file was specified, /* if we didn't get a pattern from a -e and no command file was specified,
* argv[optind] should be the pattern. no pattern, no worky */ * argv[optind] should be the pattern. no pattern, no worky */
if (nregexes == 0) { if (pattern_head == NULL) {
if (argv[optind] == NULL) if (argv[optind] == NULL)
bb_show_usage(); bb_show_usage();
else { else {
add_regex(argv[optind]); pattern_head = llist_add_to(pattern_head, argv[optind]);
optind++; optind++;
} }
} }