1 /* $NetBSD: util.c,v 1.9 2011/02/27 17:33:37 joerg Exp $ */ 2 /* $FreeBSD$ */ 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 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 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$"); 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 54 static int linesqueued; 55 static int procline(struct str *l, int); 56 57 bool 58 file_matching(const char *fname) 59 { 60 char *fname_base; 61 bool ret; 62 63 ret = finclude ? false : true; 64 fname_base = basename(fname); 65 66 for (unsigned int i = 0; i < fpatterns; ++i) { 67 if (fnmatch(fpattern[i].pat, fname, 0) == 0 || 68 fnmatch(fpattern[i].pat, fname_base, 0) == 0) { 69 if (fpattern[i].mode == EXCL_PAT) 70 return (false); 71 else 72 ret = true; 73 } 74 } 75 return (ret); 76 } 77 78 static inline bool 79 dir_matching(const char *dname) 80 { 81 bool ret; 82 83 ret = dinclude ? false : true; 84 85 for (unsigned int i = 0; i < dpatterns; ++i) { 86 if (dname != NULL && 87 fnmatch(dname, dpattern[i].pat, 0) == 0) { 88 if (dpattern[i].mode == EXCL_PAT) 89 return (false); 90 else 91 ret = true; 92 } 93 } 94 return (ret); 95 } 96 97 /* 98 * Processes a directory when a recursive search is performed with 99 * the -R option. Each appropriate file is passed to procfile(). 100 */ 101 int 102 grep_tree(char **argv) 103 { 104 FTS *fts; 105 FTSENT *p; 106 char *d, *dir = NULL; 107 int c, fts_flags; 108 bool ok; 109 110 c = fts_flags = 0; 111 112 switch(linkbehave) { 113 case LINK_EXPLICIT: 114 fts_flags = FTS_COMFOLLOW; 115 break; 116 case LINK_SKIP: 117 fts_flags = FTS_PHYSICAL; 118 break; 119 default: 120 fts_flags = FTS_LOGICAL; 121 122 } 123 124 fts_flags |= FTS_NOSTAT | FTS_NOCHDIR; 125 126 if (!(fts = fts_open(argv, fts_flags, NULL))) 127 err(2, "fts_open"); 128 while ((p = fts_read(fts)) != NULL) { 129 switch (p->fts_info) { 130 case FTS_DNR: 131 /* FALLTHROUGH */ 132 case FTS_ERR: 133 errx(2, "%s: %s", p->fts_path, strerror(p->fts_errno)); 134 break; 135 case FTS_D: 136 /* FALLTHROUGH */ 137 case FTS_DP: 138 break; 139 case FTS_DC: 140 /* Print a warning for recursive directory loop */ 141 warnx("warning: %s: recursive directory loop", 142 p->fts_path); 143 break; 144 default: 145 /* Check for file exclusion/inclusion */ 146 ok = true; 147 if (dexclude || dinclude) { 148 if ((d = strrchr(p->fts_path, '/')) != NULL) { 149 dir = grep_malloc(sizeof(char) * 150 (d - p->fts_path + 1)); 151 memcpy(dir, p->fts_path, 152 d - p->fts_path); 153 dir[d - p->fts_path] = '\0'; 154 } 155 ok = dir_matching(dir); 156 free(dir); 157 dir = NULL; 158 } 159 if (fexclude || finclude) 160 ok &= file_matching(p->fts_path); 161 162 if (ok) 163 c += procfile(p->fts_path); 164 break; 165 } 166 } 167 168 fts_close(fts); 169 return (c); 170 } 171 172 /* 173 * Opens a file and processes it. Each file is processed line-by-line 174 * passing the lines to procline(). 175 */ 176 int 177 procfile(const char *fn) 178 { 179 struct file *f; 180 struct stat sb; 181 struct str ln; 182 mode_t s; 183 int c, t; 184 185 if (mflag && (mcount <= 0)) 186 return (0); 187 188 if (strcmp(fn, "-") == 0) { 189 fn = label != NULL ? label : getstr(1); 190 f = grep_open(NULL); 191 } else { 192 if (!stat(fn, &sb)) { 193 /* Check if we need to process the file */ 194 s = sb.st_mode & S_IFMT; 195 if (s == S_IFDIR && dirbehave == DIR_SKIP) 196 return (0); 197 if ((s == S_IFIFO || s == S_IFCHR || s == S_IFBLK 198 || s == S_IFSOCK) && devbehave == DEV_SKIP) 199 return (0); 200 } 201 f = grep_open(fn); 202 } 203 if (f == NULL) { 204 if (!sflag) 205 warn("%s", fn); 206 if (errno == ENOENT) 207 notfound = true; 208 return (0); 209 } 210 211 ln.file = grep_malloc(strlen(fn) + 1); 212 strcpy(ln.file, fn); 213 ln.line_no = 0; 214 ln.len = 0; 215 linesqueued = 0; 216 tail = 0; 217 ln.off = -1; 218 219 for (c = 0; c == 0 || !(lflag || qflag); ) { 220 ln.off += ln.len + 1; 221 if ((ln.dat = grep_fgetln(f, &ln.len)) == NULL || ln.len == 0) { 222 if (ln.line_no == 0 && matchall) 223 exit(0); 224 else 225 break; 226 } 227 if (ln.len > 0 && ln.dat[ln.len - 1] == '\n') 228 --ln.len; 229 ln.line_no++; 230 231 /* Return if we need to skip a binary file */ 232 if (f->binary && binbehave == BINFILE_SKIP) { 233 grep_close(f); 234 free(ln.file); 235 free(f); 236 return (0); 237 } 238 /* Process the file line-by-line */ 239 if ((t = procline(&ln, f->binary)) == 0 && Bflag > 0) { 240 enqueue(&ln); 241 linesqueued++; 242 } 243 c += t; 244 245 /* Count the matches if we have a match limit */ 246 if (mflag) { 247 mcount -= t; 248 if (mcount <= 0) 249 break; 250 } 251 } 252 if (Bflag > 0) 253 clearqueue(); 254 grep_close(f); 255 256 if (cflag) { 257 if (!hflag) 258 printf("%s:", ln.file); 259 printf("%u\n", c); 260 } 261 if (lflag && !qflag && c != 0) 262 printf("%s\n", fn); 263 if (Lflag && !qflag && c == 0) 264 printf("%s\n", fn); 265 if (c && !cflag && !lflag && !Lflag && 266 binbehave == BINFILE_BIN && f->binary && !qflag) 267 printf(getstr(8), fn); 268 269 free(ln.file); 270 free(f); 271 return (c); 272 } 273 274 #define iswword(x) (iswalnum((x)) || (x) == L'_') 275 276 /* 277 * Processes a line comparing it with the specified patterns. Each pattern 278 * is looped to be compared along with the full string, saving each and every 279 * match, which is necessary to colorize the output and to count the 280 * matches. The matching lines are passed to printline() to display the 281 * appropriate output. 282 */ 283 static int 284 procline(struct str *l, int nottext) 285 { 286 regmatch_t matches[MAX_LINE_MATCHES]; 287 regmatch_t pmatch; 288 size_t st = 0; 289 unsigned int i; 290 int c = 0, m = 0, r = 0; 291 292 if (!matchall) { 293 /* Loop to process the whole line */ 294 while (st <= l->len) { 295 pmatch.rm_so = st; 296 pmatch.rm_eo = l->len; 297 298 /* Loop to compare with all the patterns */ 299 for (i = 0; i < patterns; i++) { 300 /* 301 * XXX: grep_search() is a workaround for speed up and should be 302 * removed in the future. See fastgrep.c. 303 */ 304 if (fg_pattern[i].pattern) { 305 r = grep_search(&fg_pattern[i], 306 (unsigned char *)l->dat, 307 l->len, &pmatch); 308 r = (r == 0) ? 0 : REG_NOMATCH; 309 st = pmatch.rm_eo; 310 } else { 311 r = regexec(&r_pattern[i], l->dat, 1, 312 &pmatch, eflags); 313 r = (r == 0) ? 0 : REG_NOMATCH; 314 st = pmatch.rm_eo; 315 } 316 if (r == REG_NOMATCH) 317 continue; 318 /* Check for full match */ 319 if (r == 0 && xflag) 320 if (pmatch.rm_so != 0 || 321 (size_t)pmatch.rm_eo != l->len) 322 r = REG_NOMATCH; 323 /* Check for whole word match */ 324 if (r == 0 && fg_pattern[i].word && 325 pmatch.rm_so != 0) { 326 wint_t wbegin, wend; 327 328 wbegin = wend = L' '; 329 if (pmatch.rm_so != 0 && 330 sscanf(&l->dat[pmatch.rm_so - 1], 331 "%lc", &wbegin) != 1) 332 r = REG_NOMATCH; 333 else if ((size_t)pmatch.rm_eo != l->len && 334 sscanf(&l->dat[pmatch.rm_eo], 335 "%lc", &wend) != 1) 336 r = REG_NOMATCH; 337 else if (iswword(wbegin) || iswword(wend)) 338 r = REG_NOMATCH; 339 } 340 if (r == 0) { 341 if (m == 0) 342 c++; 343 if (m < MAX_LINE_MATCHES) 344 matches[m++] = pmatch; 345 /* matches - skip further patterns */ 346 if ((color != NULL && !oflag) || qflag || lflag) 347 break; 348 } 349 } 350 351 if (vflag) { 352 c = !c; 353 break; 354 } 355 /* One pass if we are not recording matches */ 356 if ((color != NULL && !oflag) || qflag || lflag) 357 break; 358 359 if (st == (size_t)pmatch.rm_so) 360 break; /* No matches */ 361 } 362 } else 363 c = !vflag; 364 365 if (c && binbehave == BINFILE_BIN && nottext) 366 return (c); /* Binary file */ 367 368 /* Dealing with the context */ 369 if ((tail || c) && !cflag && !qflag && !lflag && !Lflag) { 370 if (c) { 371 if (!first && !prev && !tail && Aflag) 372 printf("--\n"); 373 tail = Aflag; 374 if (Bflag > 0) { 375 if (!first && !prev) 376 printf("--\n"); 377 printqueue(); 378 } 379 linesqueued = 0; 380 printline(l, ':', matches, m); 381 } else { 382 printline(l, '-', matches, m); 383 tail--; 384 } 385 } 386 387 if (c) { 388 prev = true; 389 first = false; 390 } else 391 prev = false; 392 393 return (c); 394 } 395 396 /* 397 * Safe malloc() for internal use. 398 */ 399 void * 400 grep_malloc(size_t size) 401 { 402 void *ptr; 403 404 if ((ptr = malloc(size)) == NULL) 405 err(2, "malloc"); 406 return (ptr); 407 } 408 409 /* 410 * Safe calloc() for internal use. 411 */ 412 void * 413 grep_calloc(size_t nmemb, size_t size) 414 { 415 void *ptr; 416 417 if ((ptr = calloc(nmemb, size)) == NULL) 418 err(2, "calloc"); 419 return (ptr); 420 } 421 422 /* 423 * Safe realloc() for internal use. 424 */ 425 void * 426 grep_realloc(void *ptr, size_t size) 427 { 428 429 if ((ptr = realloc(ptr, size)) == NULL) 430 err(2, "realloc"); 431 return (ptr); 432 } 433 434 /* 435 * Safe strdup() for internal use. 436 */ 437 char * 438 grep_strdup(const char *str) 439 { 440 char *ret; 441 442 if ((ret = strdup(str)) == NULL) 443 err(2, "strdup"); 444 return (ret); 445 } 446 447 /* 448 * Prints a matching line according to the command line options. 449 */ 450 void 451 printline(struct str *line, int sep, regmatch_t *matches, int m) 452 { 453 size_t a = 0; 454 int i, n = 0; 455 456 if (!hflag) { 457 if (nullflag == 0) 458 fputs(line->file, stdout); 459 else { 460 printf("%s", line->file); 461 putchar(0); 462 } 463 ++n; 464 } 465 if (nflag) { 466 if (n > 0) 467 putchar(sep); 468 printf("%d", line->line_no); 469 ++n; 470 } 471 if (bflag) { 472 if (n > 0) 473 putchar(sep); 474 printf("%lld", (long long)line->off); 475 ++n; 476 } 477 if (n) 478 putchar(sep); 479 /* --color and -o */ 480 if ((oflag || color) && m > 0) { 481 for (i = 0; i < m; i++) { 482 if (!oflag) 483 fwrite(line->dat + a, matches[i].rm_so - a, 1, 484 stdout); 485 if (color) 486 fprintf(stdout, "\33[%sm\33[K", color); 487 488 fwrite(line->dat + matches[i].rm_so, 489 matches[i].rm_eo - matches[i].rm_so, 1, 490 stdout); 491 if (color) 492 fprintf(stdout, "\33[m\33[K"); 493 a = matches[i].rm_eo; 494 if (oflag) 495 putchar('\n'); 496 } 497 if (!oflag) { 498 if (line->len - a > 0) 499 fwrite(line->dat + a, line->len - a, 1, stdout); 500 putchar('\n'); 501 } 502 } else { 503 fwrite(line->dat, line->len, 1, stdout); 504 putchar('\n'); 505 } 506 } 507