Deleted Added
full compact
util.c (224938) util.c (226035)
1/* $NetBSD: util.c,v 1.9 2011/02/27 17:33:37 joerg Exp $ */
1/* $NetBSD: util.c,v 1.9 2011/02/27 17:33:37 joerg Exp $ */
2/* $FreeBSD: head/usr.bin/grep/util.c 224938 2011-08-17 13:58:39Z gabor $ */
2/* $FreeBSD: head/usr.bin/grep/util.c 226035 2011-10-05 09:56:43Z gabor $ */
3/* $OpenBSD: util.c,v 1.39 2010/07/02 22:18:03 tedu Exp $ */
4
5/*-
6 * Copyright (c) 1999 James Howard and Dag-Erling Co��dan Sm��rgrav
7 * Copyright (C) 2008-2010 Gabor Kovesdan <gabor@FreeBSD.org>
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without

--- 14 unchanged lines hidden (view full) ---

25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
3/* $OpenBSD: util.c,v 1.39 2010/07/02 22:18:03 tedu Exp $ */
4
5/*-
6 * Copyright (c) 1999 James Howard and Dag-Erling Co��dan Sm��rgrav
7 * Copyright (C) 2008-2010 Gabor Kovesdan <gabor@FreeBSD.org>
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without

--- 14 unchanged lines hidden (view full) ---

25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/usr.bin/grep/util.c 224938 2011-08-17 13:58:39Z gabor $");
33__FBSDID("$FreeBSD: head/usr.bin/grep/util.c 226035 2011-10-05 09:56:43Z gabor $");
34
35#include <sys/stat.h>
36#include <sys/types.h>
37
38#include <ctype.h>
39#include <err.h>
40#include <errno.h>
41#include <fnmatch.h>
42#include <fts.h>
43#include <libgen.h>
44#include <stdbool.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <unistd.h>
49#include <wchar.h>
50#include <wctype.h>
51
34
35#include <sys/stat.h>
36#include <sys/types.h>
37
38#include <ctype.h>
39#include <err.h>
40#include <errno.h>
41#include <fnmatch.h>
42#include <fts.h>
43#include <libgen.h>
44#include <stdbool.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <unistd.h>
49#include <wchar.h>
50#include <wctype.h>
51
52#include "fastmatch.h"
52#include "grep.h"
53
54static int linesqueued;
55static int procline(struct str *l, int);
56
57bool
58file_matching(const char *fname)
59{

--- 167 unchanged lines hidden (view full) ---

227 return (0);
228 }
229 /* Process the file line-by-line */
230 if ((t = procline(&ln, f->binary)) == 0 && Bflag > 0) {
231 enqueue(&ln);
232 linesqueued++;
233 }
234 c += t;
53#include "grep.h"
54
55static int linesqueued;
56static int procline(struct str *l, int);
57
58bool
59file_matching(const char *fname)
60{

--- 167 unchanged lines hidden (view full) ---

228 return (0);
229 }
230 /* Process the file line-by-line */
231 if ((t = procline(&ln, f->binary)) == 0 && Bflag > 0) {
232 enqueue(&ln);
233 linesqueued++;
234 }
235 c += t;
235
236 /* Count the matches if we have a match limit */
237 if (mflag) {
238 mcount -= t;
239 if (mcount <= 0)
240 break;
241 }
236 if (mflag && mcount < 0)
237 break;
242 }
243 if (Bflag > 0)
244 clearqueue();
245 grep_close(f);
246
247 if (cflag) {
248 if (!hflag)
249 printf("%s:", ln.file);

--- 25 unchanged lines hidden (view full) ---

275procline(struct str *l, int nottext)
276{
277 regmatch_t matches[MAX_LINE_MATCHES];
278 regmatch_t pmatch;
279 size_t st = 0;
280 unsigned int i;
281 int c = 0, m = 0, r = 0;
282
238 }
239 if (Bflag > 0)
240 clearqueue();
241 grep_close(f);
242
243 if (cflag) {
244 if (!hflag)
245 printf("%s:", ln.file);

--- 25 unchanged lines hidden (view full) ---

271procline(struct str *l, int nottext)
272{
273 regmatch_t matches[MAX_LINE_MATCHES];
274 regmatch_t pmatch;
275 size_t st = 0;
276 unsigned int i;
277 int c = 0, m = 0, r = 0;
278
283 if (!matchall) {
284 /* Loop to process the whole line */
285 while (st <= l->len) {
286 pmatch.rm_so = st;
287 pmatch.rm_eo = l->len;
279 /* Loop to process the whole line */
280 while (st <= l->len) {
281 pmatch.rm_so = st;
282 pmatch.rm_eo = l->len;
288
283
289 /* Loop to compare with all the patterns */
290 for (i = 0; i < patterns; i++) {
291/*
292 * XXX: grep_search() is a workaround for speed up and should be
293 * removed in the future. See fastgrep.c.
294 */
295 if (fg_pattern[i].pattern)
296 r = grep_search(&fg_pattern[i],
297 (unsigned char *)l->dat,
298 l->len, &pmatch);
299 else
300 r = regexec(&r_pattern[i], l->dat, 1,
301 &pmatch, eflags);
302 r = (r == 0) ? 0 : REG_NOMATCH;
303 st = (cflags & REG_NOSUB)
304 ? (size_t)l->len
305 : (size_t)pmatch.rm_eo;
306 if (r == REG_NOMATCH)
307 continue;
308 /* Check for full match */
309 if (r == 0 && xflag)
310 if (pmatch.rm_so != 0 ||
311 (size_t)pmatch.rm_eo != l->len)
312 r = REG_NOMATCH;
313 /* Check for whole word match */
314 if (r == 0 && (wflag || fg_pattern[i].word)) {
315 wint_t wbegin, wend;
284 /* Loop to compare with all the patterns */
285 for (i = 0; i < patterns; i++) {
286 if (fg_pattern[i].pattern)
287 r = fastexec(&fg_pattern[i],
288 l->dat, 1, &pmatch, eflags);
289 else
290 r = regexec(&r_pattern[i], l->dat, 1,
291 &pmatch, eflags);
292 r = (r == 0) ? 0 : REG_NOMATCH;
293 st = (cflags & REG_NOSUB)
294 ? (size_t)l->len
295 : (size_t)pmatch.rm_eo;
296 if (r == REG_NOMATCH)
297 continue;
298 /* Check for full match */
299 if (r == 0 && xflag)
300 if (pmatch.rm_so != 0 ||
301 (size_t)pmatch.rm_eo != l->len)
302 r = REG_NOMATCH;
303 /* Check for whole word match */
304 if (r == 0 && (wflag || fg_pattern[i].word)) {
305 wint_t wbegin, wend;
316
306
317 wbegin = wend = L' ';
318 if (pmatch.rm_so != 0 &&
319 sscanf(&l->dat[pmatch.rm_so - 1],
320 "%lc", &wbegin) != 1)
321 r = REG_NOMATCH;
322 else if ((size_t)pmatch.rm_eo !=
323 l->len &&
324 sscanf(&l->dat[pmatch.rm_eo],
325 "%lc", &wend) != 1)
326 r = REG_NOMATCH;
327 else if (iswword(wbegin) ||
328 iswword(wend))
329 r = REG_NOMATCH;
330 }
331 if (r == 0) {
332 if (m == 0)
333 c++;
334 if (m < MAX_LINE_MATCHES)
335 matches[m++] = pmatch;
336 /* matches - skip further patterns */
337 if ((color == NULL && !oflag) ||
338 qflag || lflag)
339 break;
340 }
307 wbegin = wend = L' ';
308 if (pmatch.rm_so != 0 &&
309 sscanf(&l->dat[pmatch.rm_so - 1],
310 "%lc", &wbegin) != 1)
311 r = REG_NOMATCH;
312 else if ((size_t)pmatch.rm_eo !=
313 l->len &&
314 sscanf(&l->dat[pmatch.rm_eo],
315 "%lc", &wend) != 1)
316 r = REG_NOMATCH;
317 else if (iswword(wbegin) ||
318 iswword(wend))
319 r = REG_NOMATCH;
341 }
320 }
342
343 if (vflag) {
344 c = !c;
345 break;
321 if (r == 0) {
322 if (m == 0)
323 c++;
324 if (m < MAX_LINE_MATCHES)
325 matches[m++] = pmatch;
326 /* matches - skip further patterns */
327 if ((color == NULL && !oflag) ||
328 qflag || lflag)
329 break;
346 }
330 }
347 /* One pass if we are not recording matches */
348 if ((color == NULL && !oflag) || qflag || lflag)
349 break;
331 }
350
332
351 if (st == (size_t)pmatch.rm_so)
352 break; /* No matches */
333 if (vflag) {
334 c = !c;
335 break;
353 }
336 }
354 } else
355 c = !vflag;
356
337
338 /* One pass if we are not recording matches */
339 if ((color == NULL && !oflag) || qflag || lflag)
340 break;
341
342 if (st == (size_t)pmatch.rm_so)
343 break; /* No matches */
344 }
345
346
347 /* Count the matches if we have a match limit */
348 if (mflag)
349 mcount -= c;
350
357 if (c && binbehave == BINFILE_BIN && nottext)
358 return (c); /* Binary file */
359
360 /* Dealing with the context */
361 if ((tail || c) && !cflag && !qflag && !lflag && !Lflag) {
362 if (c) {
363 if (!first && !prev && !tail && Aflag)
364 printf("--\n");

--- 134 unchanged lines hidden ---
351 if (c && binbehave == BINFILE_BIN && nottext)
352 return (c); /* Binary file */
353
354 /* Dealing with the context */
355 if ((tail || c) && !cflag && !qflag && !lflag && !Lflag) {
356 if (c) {
357 if (!first && !prev && !tail && Aflag)
358 printf("--\n");

--- 134 unchanged lines hidden ---