1/* $NetBSD: util.c,v 1.9 2011/02/27 17:33:37 joerg Exp $ */ |
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> |
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 |
52#include "fastmatch.h" |
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; |
236 if (mflag && mcount < 0) 237 break; |
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 |
279 /* Loop to process the whole line */ 280 while (st <= l->len) { 281 pmatch.rm_so = st; 282 pmatch.rm_eo = l->len; |
283 |
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; |
306 |
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; |
320 } |
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; |
330 } |
331 } |
332 |
333 if (vflag) { 334 c = !c; 335 break; |
336 } |
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 |
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 --- |