View | Details | Raw Unified | Return to bug 195763 | Differences between
and this patch

Collapse All | Expand All

(-)b/usr.bin/grep/grep.c (-4 / +9 lines)
Lines 339-345 main(int argc, char *argv[]) Link Here
339
	const char *pn;
339
	const char *pn;
340
	unsigned long long l;
340
	unsigned long long l;
341
	unsigned int aargc, eargc, i;
341
	unsigned int aargc, eargc, i;
342
	int c, lastc, needpattern, newarg, prevoptind;
342
	int c, lastc, needpattern, newarg, prevoptind, pofs;
343
343
344
	setlocale(LC_ALL, "");
344
	setlocale(LC_ALL, "");
345
345
Lines 704-721 main(int argc, char *argv[]) Link Here
704
704
705
	fg_pattern = grep_calloc(patterns, sizeof(*fg_pattern));
705
	fg_pattern = grep_calloc(patterns, sizeof(*fg_pattern));
706
	r_pattern = grep_calloc(patterns, sizeof(*r_pattern));
706
	r_pattern = grep_calloc(patterns, sizeof(*r_pattern));
707
	pofs = 0;
707
708
708
	/* Check if cheating is allowed (always is for fgrep). */
709
	/* Check if cheating is allowed (always is for fgrep). */
709
	for (i = 0; i < patterns; ++i) {
710
	for (i = 0; i < patterns; ++i) {
710
		if (fastncomp(&fg_pattern[i], pattern[i].pat,
711
		if (fastncomp(&fg_pattern[i - pofs], pattern[i].pat,
711
		    pattern[i].len, cflags) != 0) {
712
		    pattern[i].len, cflags) != 0) {
712
			/* Fall back to full regex library */
713
			/* Fall back to full regex library */
713
			c = regcomp(&r_pattern[i], pattern[i].pat, cflags);
714
			c = regcomp(&r_pattern[i - pofs], pattern[i].pat, cflags);
714
			if (c != 0) {
715
			if (c != 0) {
715
				regerror(c, &r_pattern[i], re_error,
716
				regerror(c, &r_pattern[i - pofs], re_error,
716
				    RE_ERROR_BUF);
717
				    RE_ERROR_BUF);
717
				errx(2, "%s", re_error);
718
				errx(2, "%s", re_error);
718
			}
719
			}
720
		} else {
721
			/* Deduct one for invalid pattern */
722
			patterns --;
723
			pofs ++;
719
		}
724
		}
720
	}
725
	}
721
726
(-)b/usr.bin/grep/util.c (-4 / +12 lines)
Lines 294-306 procline(struct str *l, int nottext) Link Here
294
	regmatch_t pmatch;
294
	regmatch_t pmatch;
295
	size_t st = 0;
295
	size_t st = 0;
296
	unsigned int i;
296
	unsigned int i;
297
	int c = 0, m = 0, r = 0;
297
	int c = 0, m = 0, r = 0, lastmatches = 0;
298
298
299
	/* Loop to process the whole line */
299
	/* Loop to process the whole line */
300
	while (st <= l->len) {
300
	while (st <= l->len) {
301
		pmatch.rm_so = st;
301
		pmatch.rm_so = st;
302
		pmatch.rm_eo = l->len;
302
		pmatch.rm_eo = l->len;
303
303
		lastmatches = 0;
304
		/* Loop to compare with all the patterns */
304
		/* Loop to compare with all the patterns */
305
		for (i = 0; i < patterns; i++) {
305
		for (i = 0; i < patterns; i++) {
306
			if (fg_pattern[i].pattern)
306
			if (fg_pattern[i].pattern)
Lines 339-348 procline(struct str *l, int nottext) Link Here
339
					r = REG_NOMATCH;
339
					r = REG_NOMATCH;
340
			}
340
			}
341
			if (r == 0) {
341
			if (r == 0) {
342
				lastmatches ++;
343
				/* Skip over zero-length matches */
344
				if (pmatch.rm_so == pmatch.rm_eo)
345
					continue;
342
				if (m == 0)
346
				if (m == 0)
343
					c++;
347
					c++;
344
				if (m < MAX_LINE_MATCHES)
348
				if (m < MAX_LINE_MATCHES) {
345
					matches[m++] = pmatch;
349
					matches[m++] = pmatch;
350
				}
351
346
				/* matches - skip further patterns */
352
				/* matches - skip further patterns */
347
				if ((color == NULL && !oflag) ||
353
				if ((color == NULL && !oflag) ||
348
				    qflag || lflag)
354
				    qflag || lflag)
Lines 359-366 procline(struct str *l, int nottext) Link Here
359
		if (!wflag && ((color == NULL && !oflag) || qflag || lflag || Lflag))
365
		if (!wflag && ((color == NULL && !oflag) || qflag || lflag || Lflag))
360
			break;
366
			break;
361
367
362
		if (st == (size_t)pmatch.rm_so)
368
		if (lastmatches == 0)
363
			break; 	/* No matches */
369
			break; 	/* No matches */
370
		else if (st == (size_t)pmatch.rm_so)
371
			st ++;	/* Zero-length match -- advance one more */
364
	}
372
	}
365
373
366
374

Return to bug 195763