Deleted Added
sdiff udiff text old ( 224938 ) new ( 226035 )
full compact
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 $ */
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 $");
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 "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;
235
236 /* Count the matches if we have a match limit */
237 if (mflag) {
238 mcount -= t;
239 if (mcount <= 0)
240 break;
241 }
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
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;
288
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;
316
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 }
341 }
342
343 if (vflag) {
344 c = !c;
345 break;
346 }
347 /* One pass if we are not recording matches */
348 if ((color == NULL && !oflag) || qflag || lflag)
349 break;
350
351 if (st == (size_t)pmatch.rm_so)
352 break; /* No matches */
353 }
354 } else
355 c = !vflag;
356
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 ---