void grep_source_load_driver(struct grep_source *gs) { if (gs->driver) return; grep_attr_lock(); if (gs->path) gs->driver = userdiff_find_by_path(gs->path); if (!gs->driver) gs->driver = userdiff_find_by_name("default"); grep_attr_unlock(); }
static const char *parse_range_funcname(const char *arg, nth_line_fn_t nth_line_cb, void *cb_data, long lines, long *begin, long *end, const char *path) { char *pattern; const char *term; struct userdiff_driver *drv; xdemitconf_t *xecfg = NULL; const char *start; const char *p; int reg_error; regex_t regexp; assert(*arg == ':'); term = arg+1; while (*term && *term != ':') { if (*term == '\\' && *(term+1)) term++; term++; } if (term == arg+1) return NULL; if (!begin) /* skip_range_arg case */ return term; pattern = xstrndup(arg+1, term-(arg+1)); start = nth_line_cb(cb_data, 0); drv = userdiff_find_by_path(path); if (drv && drv->funcname.pattern) { const struct userdiff_funcname *pe = &drv->funcname; xecfg = xcalloc(1, sizeof(*xecfg)); xdiff_set_find_func(xecfg, pe->pattern, pe->cflags); } reg_error = regcomp(®exp, pattern, REG_NEWLINE); if (reg_error) { char errbuf[1024]; regerror(reg_error, ®exp, errbuf, 1024); die("-L parameter '%s': %s", pattern, errbuf); } p = find_funcname_matching_regexp(xecfg, (char*) start, ®exp); if (!p) die("-L parameter '%s': no match", pattern); *begin = 0; while (p > nth_line_cb(cb_data, *begin)) (*begin)++; if (*begin >= lines) die("-L parameter '%s' matches at EOF", pattern); *end = *begin+1; while (*end < lines) { const char *bol = nth_line_cb(cb_data, *end); const char *eol = nth_line_cb(cb_data, *end+1); if (match_funcname(xecfg, bol, eol)) break; (*end)++; } regfree(®exp); free(xecfg); free(pattern); /* compensate for 1-based numbering */ (*begin)++; return term; }
static int grep_buffer_1(struct grep_opt *opt, const char *name, char *buf, unsigned long size, int collect_hits) { char *bol = buf; unsigned long left = size; unsigned lno = 1; unsigned last_hit = 0; int binary_match_only = 0; unsigned count = 0; int try_lookahead = 0; enum grep_context ctx = GREP_CONTEXT_HEAD; xdemitconf_t xecfg; opt->last_shown = 0; if (!opt->output) opt->output = std_output; if (buffer_is_binary(buf, size)) { switch (opt->binary) { case GREP_BINARY_DEFAULT: binary_match_only = 1; break; case GREP_BINARY_NOMATCH: return 0; /* Assume unmatch */ break; default: break; } } memset(&xecfg, 0, sizeof(xecfg)); if (opt->funcname && !opt->unmatch_name_only && !opt->status_only && !opt->name_only && !binary_match_only && !collect_hits) { struct userdiff_driver *drv = userdiff_find_by_path(name); if (drv && drv->funcname.pattern) { const struct userdiff_funcname *pe = &drv->funcname; xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags); opt->priv = &xecfg; } } try_lookahead = should_lookahead(opt); while (left) { char *eol, ch; int hit; /* * look_ahead() skips quicly to the line that possibly * has the next hit; don't call it if we need to do * something more than just skipping the current line * in response to an unmatch for the current line. E.g. * inside a post-context window, we will show the current * line as a context around the previous hit when it * doesn't hit. */ if (try_lookahead && !(last_hit && lno <= last_hit + opt->post_context) && look_ahead(opt, &left, &lno, &bol)) break; eol = end_of_line(bol, &left); ch = *eol; *eol = 0; if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol)) ctx = GREP_CONTEXT_BODY; hit = match_line(opt, bol, eol, ctx, collect_hits); *eol = ch; if (collect_hits) goto next_line; /* "grep -v -e foo -e bla" should list lines * that do not have either, so inversion should * be done outside. */ if (opt->invert) hit = !hit; if (opt->unmatch_name_only) { if (hit) return 0; goto next_line; } if (hit) { count++; if (opt->status_only) return 1; if (binary_match_only) { opt->output(opt, "Binary file ", 12); output_color(opt, name, strlen(name), opt->color_filename); opt->output(opt, " matches\n", 9); return 1; } if (opt->name_only) { show_name(opt, name); return 1; } /* Hit at this line. If we haven't shown the * pre-context lines, we would need to show them. * When asked to do "count", this still show * the context which is nonsense, but the user * deserves to get that ;-). */ if (opt->pre_context) show_pre_context(opt, name, buf, bol, lno); else if (opt->funcname) show_funcname_line(opt, name, buf, bol, lno); if (!opt->count) show_line(opt, bol, eol, name, lno, ':'); last_hit = lno; } else if (last_hit && lno <= last_hit + opt->post_context) { /* If the last hit is within the post context, * we need to show this line. */ show_line(opt, bol, eol, name, lno, '-'); } next_line: bol = eol + 1; if (!left) break; left--; lno++; } if (collect_hits) return 0; if (opt->status_only) return 0; if (opt->unmatch_name_only) { /* We did not see any hit, so we want to show this */ show_name(opt, name); return 1; } xdiff_clear_find_func(&xecfg); opt->priv = NULL; /* NEEDSWORK: * The real "grep -c foo *.c" gives many "bar.c:0" lines, * which feels mostly useless but sometimes useful. Maybe * make it another option? For now suppress them. */ if (opt->count && count) { char buf[32]; output_color(opt, name, strlen(name), opt->color_filename); output_sep(opt, ':'); snprintf(buf, sizeof(buf), "%u\n", count); opt->output(opt, buf, strlen(buf)); } return !!last_hit; }
static int grep_buffer_1(struct grep_opt *opt, const char *name, char *buf, unsigned long size, int collect_hits) { char *bol = buf; unsigned long left = size; unsigned lno = 1; unsigned last_hit = 0; int binary_match_only = 0; unsigned count = 0; enum grep_context ctx = GREP_CONTEXT_HEAD; xdemitconf_t xecfg; opt->last_shown = 0; if (buffer_is_binary(buf, size)) { switch (opt->binary) { case GREP_BINARY_DEFAULT: binary_match_only = 1; break; case GREP_BINARY_NOMATCH: return 0; /* Assume unmatch */ break; default: break; } } memset(&xecfg, 0, sizeof(xecfg)); if (opt->funcname && !opt->unmatch_name_only && !opt->status_only && !opt->name_only && !binary_match_only && !collect_hits) { struct userdiff_driver *drv = userdiff_find_by_path(name); if (drv && drv->funcname.pattern) { const struct userdiff_funcname *pe = &drv->funcname; xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags); opt->priv = &xecfg; } } while (left) { char *eol, ch; int hit; eol = end_of_line(bol, &left); ch = *eol; *eol = 0; if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol)) ctx = GREP_CONTEXT_BODY; hit = match_line(opt, bol, eol, ctx, collect_hits); *eol = ch; if (collect_hits) goto next_line; /* "grep -v -e foo -e bla" should list lines * that do not have either, so inversion should * be done outside. */ if (opt->invert) hit = !hit; if (opt->unmatch_name_only) { if (hit) return 0; goto next_line; } if (hit) { count++; if (opt->status_only) return 1; if (binary_match_only) { printf("Binary file %s matches\n", name); return 1; } if (opt->name_only) { show_name(opt, name); return 1; } /* Hit at this line. If we haven't shown the * pre-context lines, we would need to show them. * When asked to do "count", this still show * the context which is nonsense, but the user * deserves to get that ;-). */ if (opt->pre_context) show_pre_context(opt, name, buf, bol, lno); else if (opt->funcname) show_funcname_line(opt, name, buf, bol, lno); if (!opt->count) show_line(opt, bol, eol, name, lno, ':'); last_hit = lno; } else if (last_hit && lno <= last_hit + opt->post_context) { /* If the last hit is within the post context, * we need to show this line. */ show_line(opt, bol, eol, name, lno, '-'); } next_line: bol = eol + 1; if (!left) break; left--; lno++; } if (collect_hits) return 0; if (opt->status_only) return 0; if (opt->unmatch_name_only) { /* We did not see any hit, so we want to show this */ show_name(opt, name); return 1; } xdiff_clear_find_func(&xecfg); opt->priv = NULL; /* NEEDSWORK: * The real "grep -c foo *.c" gives many "bar.c:0" lines, * which feels mostly useless but sometimes useful. Maybe * make it another option? For now suppress them. */ if (opt->count && count) printf("%s%c%u\n", name, opt->null_following_name ? '\0' : ':', count); return !!last_hit; }