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>
| 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>
|
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{ 61 char *fname_base; 62 bool ret; 63 64 ret = finclude ? false : true; 65 fname_base = basename(fname); 66 67 for (unsigned int i = 0; i < fpatterns; ++i) { 68 if (fnmatch(fpattern[i].pat, fname, 0) == 0 || 69 fnmatch(fpattern[i].pat, fname_base, 0) == 0) { 70 if (fpattern[i].mode == EXCL_PAT) 71 return (false); 72 else 73 ret = true; 74 } 75 } 76 return (ret); 77} 78 79static inline bool 80dir_matching(const char *dname) 81{ 82 bool ret; 83 84 ret = dinclude ? false : true; 85 86 for (unsigned int i = 0; i < dpatterns; ++i) { 87 if (dname != NULL && 88 fnmatch(dpattern[i].pat, dname, 0) == 0) { 89 if (dpattern[i].mode == EXCL_PAT) 90 return (false); 91 else 92 ret = true; 93 } 94 } 95 return (ret); 96} 97 98/* 99 * Processes a directory when a recursive search is performed with 100 * the -R option. Each appropriate file is passed to procfile(). 101 */ 102int 103grep_tree(char **argv) 104{ 105 FTS *fts; 106 FTSENT *p; 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:
| 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{ 61 char *fname_base; 62 bool ret; 63 64 ret = finclude ? false : true; 65 fname_base = basename(fname); 66 67 for (unsigned int i = 0; i < fpatterns; ++i) { 68 if (fnmatch(fpattern[i].pat, fname, 0) == 0 || 69 fnmatch(fpattern[i].pat, fname_base, 0) == 0) { 70 if (fpattern[i].mode == EXCL_PAT) 71 return (false); 72 else 73 ret = true; 74 } 75 } 76 return (ret); 77} 78 79static inline bool 80dir_matching(const char *dname) 81{ 82 bool ret; 83 84 ret = dinclude ? false : true; 85 86 for (unsigned int i = 0; i < dpatterns; ++i) { 87 if (dname != NULL && 88 fnmatch(dpattern[i].pat, dname, 0) == 0) { 89 if (dpattern[i].mode == EXCL_PAT) 90 return (false); 91 else 92 ret = true; 93 } 94 } 95 return (ret); 96} 97 98/* 99 * Processes a directory when a recursive search is performed with 100 * the -R option. Each appropriate file is passed to procfile(). 101 */ 102int 103grep_tree(char **argv) 104{ 105 FTS *fts; 106 FTSENT *p; 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:
|
134 break; 135 case FTS_D: 136 /* FALLTHROUGH */ 137 case FTS_DP: 138 if (dexclude || dinclude) 139 if (!dir_matching(p->fts_name) || 140 !dir_matching(p->fts_path)) 141 fts_set(fts, p, FTS_SKIP); 142 break; 143 case FTS_DC: 144 /* Print a warning for recursive directory loop */ 145 warnx("warning: %s: recursive directory loop", 146 p->fts_path); 147 break; 148 default: 149 /* Check for file exclusion/inclusion */ 150 ok = true; 151 if (fexclude || finclude) 152 ok &= file_matching(p->fts_path); 153 154 if (ok) 155 c += procfile(p->fts_path); 156 break; 157 } 158 } 159 160 fts_close(fts); 161 return (c); 162} 163 164/* 165 * Opens a file and processes it. Each file is processed line-by-line 166 * passing the lines to procline(). 167 */ 168int 169procfile(const char *fn) 170{ 171 struct file *f; 172 struct stat sb; 173 struct str ln; 174 mode_t s; 175 int c, t; 176 177 if (mflag && (mcount <= 0)) 178 return (0); 179 180 if (strcmp(fn, "-") == 0) { 181 fn = label != NULL ? label : getstr(1); 182 f = grep_open(NULL); 183 } else { 184 if (!stat(fn, &sb)) { 185 /* Check if we need to process the file */ 186 s = sb.st_mode & S_IFMT; 187 if (s == S_IFDIR && dirbehave == DIR_SKIP) 188 return (0); 189 if ((s == S_IFIFO || s == S_IFCHR || s == S_IFBLK 190 || s == S_IFSOCK) && devbehave == DEV_SKIP) 191 return (0); 192 } 193 f = grep_open(fn); 194 } 195 if (f == NULL) { 196 if (!sflag) 197 warn("%s", fn); 198 if (errno == ENOENT) 199 notfound = true; 200 return (0); 201 } 202 203 ln.file = grep_malloc(strlen(fn) + 1); 204 strcpy(ln.file, fn); 205 ln.line_no = 0; 206 ln.len = 0; 207 linesqueued = 0; 208 tail = 0; 209 ln.off = -1; 210 211 for (c = 0; c == 0 || !(lflag || qflag); ) { 212 ln.off += ln.len + 1; 213 if ((ln.dat = grep_fgetln(f, &ln.len)) == NULL || ln.len == 0) { 214 if (ln.line_no == 0 && matchall) 215 exit(0); 216 else 217 break; 218 } 219 if (ln.len > 0 && ln.dat[ln.len - 1] == '\n') 220 --ln.len; 221 ln.line_no++; 222 223 /* Return if we need to skip a binary file */ 224 if (f->binary && binbehave == BINFILE_SKIP) { 225 grep_close(f); 226 free(ln.file); 227 free(f); 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); 246 printf("%u\n", c); 247 } 248 if (lflag && !qflag && c != 0) 249 printf("%s%c", fn, nullflag ? 0 : '\n'); 250 if (Lflag && !qflag && c == 0) 251 printf("%s%c", fn, nullflag ? 0 : '\n'); 252 if (c && !cflag && !lflag && !Lflag && 253 binbehave == BINFILE_BIN && f->binary && !qflag) 254 printf(getstr(8), fn); 255 256 free(ln.file); 257 free(f); 258 return (c); 259} 260 261#define iswword(x) (iswalnum((x)) || (x) == L'_') 262 263/* 264 * Processes a line comparing it with the specified patterns. Each pattern 265 * is looped to be compared along with the full string, saving each and every 266 * match, which is necessary to colorize the output and to count the 267 * matches. The matching lines are passed to printline() to display the 268 * appropriate output. 269 */ 270static int 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"); 359 tail = Aflag; 360 if (Bflag > 0) { 361 if (!first && !prev) 362 printf("--\n"); 363 printqueue(); 364 } 365 linesqueued = 0; 366 printline(l, ':', matches, m); 367 } else { 368 printline(l, '-', matches, m); 369 tail--; 370 } 371 } 372 373 if (c) { 374 prev = true; 375 first = false; 376 } else 377 prev = false; 378 379 return (c); 380} 381 382/* 383 * Safe malloc() for internal use. 384 */ 385void * 386grep_malloc(size_t size) 387{ 388 void *ptr; 389 390 if ((ptr = malloc(size)) == NULL) 391 err(2, "malloc"); 392 return (ptr); 393} 394 395/* 396 * Safe calloc() for internal use. 397 */ 398void * 399grep_calloc(size_t nmemb, size_t size) 400{ 401 void *ptr; 402 403 if ((ptr = calloc(nmemb, size)) == NULL) 404 err(2, "calloc"); 405 return (ptr); 406} 407 408/* 409 * Safe realloc() for internal use. 410 */ 411void * 412grep_realloc(void *ptr, size_t size) 413{ 414 415 if ((ptr = realloc(ptr, size)) == NULL) 416 err(2, "realloc"); 417 return (ptr); 418} 419 420/* 421 * Safe strdup() for internal use. 422 */ 423char * 424grep_strdup(const char *str) 425{ 426 char *ret; 427 428 if ((ret = strdup(str)) == NULL) 429 err(2, "strdup"); 430 return (ret); 431} 432 433/* 434 * Prints a matching line according to the command line options. 435 */ 436void 437printline(struct str *line, int sep, regmatch_t *matches, int m) 438{ 439 size_t a = 0; 440 int i, n = 0; 441 442 if (!hflag) { 443 if (!nullflag) { 444 fputs(line->file, stdout); 445 ++n; 446 } else { 447 printf("%s", line->file); 448 putchar(0); 449 } 450 } 451 if (nflag) { 452 if (n > 0) 453 putchar(sep); 454 printf("%d", line->line_no); 455 ++n; 456 } 457 if (bflag) { 458 if (n > 0) 459 putchar(sep); 460 printf("%lld", (long long)line->off); 461 ++n; 462 } 463 if (n) 464 putchar(sep); 465 /* --color and -o */ 466 if ((oflag || color) && m > 0) { 467 for (i = 0; i < m; i++) { 468 if (!oflag) 469 fwrite(line->dat + a, matches[i].rm_so - a, 1, 470 stdout); 471 if (color) 472 fprintf(stdout, "\33[%sm\33[K", color); 473 474 fwrite(line->dat + matches[i].rm_so, 475 matches[i].rm_eo - matches[i].rm_so, 1, 476 stdout); 477 if (color) 478 fprintf(stdout, "\33[m\33[K"); 479 a = matches[i].rm_eo; 480 if (oflag) 481 putchar('\n'); 482 } 483 if (!oflag) { 484 if (line->len - a > 0) 485 fwrite(line->dat + a, line->len - a, 1, stdout); 486 putchar('\n'); 487 } 488 } else { 489 fwrite(line->dat, line->len, 1, stdout); 490 putchar('\n'); 491 } 492}
| 136 break; 137 case FTS_D: 138 /* FALLTHROUGH */ 139 case FTS_DP: 140 if (dexclude || dinclude) 141 if (!dir_matching(p->fts_name) || 142 !dir_matching(p->fts_path)) 143 fts_set(fts, p, FTS_SKIP); 144 break; 145 case FTS_DC: 146 /* Print a warning for recursive directory loop */ 147 warnx("warning: %s: recursive directory loop", 148 p->fts_path); 149 break; 150 default: 151 /* Check for file exclusion/inclusion */ 152 ok = true; 153 if (fexclude || finclude) 154 ok &= file_matching(p->fts_path); 155 156 if (ok) 157 c += procfile(p->fts_path); 158 break; 159 } 160 } 161 162 fts_close(fts); 163 return (c); 164} 165 166/* 167 * Opens a file and processes it. Each file is processed line-by-line 168 * passing the lines to procline(). 169 */ 170int 171procfile(const char *fn) 172{ 173 struct file *f; 174 struct stat sb; 175 struct str ln; 176 mode_t s; 177 int c, t; 178 179 if (mflag && (mcount <= 0)) 180 return (0); 181 182 if (strcmp(fn, "-") == 0) { 183 fn = label != NULL ? label : getstr(1); 184 f = grep_open(NULL); 185 } else { 186 if (!stat(fn, &sb)) { 187 /* Check if we need to process the file */ 188 s = sb.st_mode & S_IFMT; 189 if (s == S_IFDIR && dirbehave == DIR_SKIP) 190 return (0); 191 if ((s == S_IFIFO || s == S_IFCHR || s == S_IFBLK 192 || s == S_IFSOCK) && devbehave == DEV_SKIP) 193 return (0); 194 } 195 f = grep_open(fn); 196 } 197 if (f == NULL) { 198 if (!sflag) 199 warn("%s", fn); 200 if (errno == ENOENT) 201 notfound = true; 202 return (0); 203 } 204 205 ln.file = grep_malloc(strlen(fn) + 1); 206 strcpy(ln.file, fn); 207 ln.line_no = 0; 208 ln.len = 0; 209 linesqueued = 0; 210 tail = 0; 211 ln.off = -1; 212 213 for (c = 0; c == 0 || !(lflag || qflag); ) { 214 ln.off += ln.len + 1; 215 if ((ln.dat = grep_fgetln(f, &ln.len)) == NULL || ln.len == 0) { 216 if (ln.line_no == 0 && matchall) 217 exit(0); 218 else 219 break; 220 } 221 if (ln.len > 0 && ln.dat[ln.len - 1] == '\n') 222 --ln.len; 223 ln.line_no++; 224 225 /* Return if we need to skip a binary file */ 226 if (f->binary && binbehave == BINFILE_SKIP) { 227 grep_close(f); 228 free(ln.file); 229 free(f); 230 return (0); 231 } 232 /* Process the file line-by-line */ 233 if ((t = procline(&ln, f->binary)) == 0 && Bflag > 0) { 234 enqueue(&ln); 235 linesqueued++; 236 } 237 c += t; 238 if (mflag && mcount <= 0) 239 break; 240 } 241 if (Bflag > 0) 242 clearqueue(); 243 grep_close(f); 244 245 if (cflag) { 246 if (!hflag) 247 printf("%s:", ln.file); 248 printf("%u\n", c); 249 } 250 if (lflag && !qflag && c != 0) 251 printf("%s%c", fn, nullflag ? 0 : '\n'); 252 if (Lflag && !qflag && c == 0) 253 printf("%s%c", fn, nullflag ? 0 : '\n'); 254 if (c && !cflag && !lflag && !Lflag && 255 binbehave == BINFILE_BIN && f->binary && !qflag) 256 printf(getstr(8), fn); 257 258 free(ln.file); 259 free(f); 260 return (c); 261} 262 263#define iswword(x) (iswalnum((x)) || (x) == L'_') 264 265/* 266 * Processes a line comparing it with the specified patterns. Each pattern 267 * is looped to be compared along with the full string, saving each and every 268 * match, which is necessary to colorize the output and to count the 269 * matches. The matching lines are passed to printline() to display the 270 * appropriate output. 271 */ 272static int 273procline(struct str *l, int nottext) 274{ 275 regmatch_t matches[MAX_LINE_MATCHES]; 276 regmatch_t pmatch; 277 size_t st = 0; 278 unsigned int i; 279 int c = 0, m = 0, r = 0; 280 281 /* Loop to process the whole line */ 282 while (st <= l->len) { 283 pmatch.rm_so = st; 284 pmatch.rm_eo = l->len; 285 286 /* Loop to compare with all the patterns */ 287 for (i = 0; i < patterns; i++) { 288 if (fg_pattern[i].pattern) 289 r = fastexec(&fg_pattern[i], 290 l->dat, 1, &pmatch, eflags); 291 else 292 r = regexec(&r_pattern[i], l->dat, 1, 293 &pmatch, eflags); 294 r = (r == 0) ? 0 : REG_NOMATCH; 295 st = (cflags & REG_NOSUB) 296 ? (size_t)l->len 297 : (size_t)pmatch.rm_eo; 298 if (r == REG_NOMATCH) 299 continue; 300 /* Check for full match */ 301 if (r == 0 && xflag) 302 if (pmatch.rm_so != 0 || 303 (size_t)pmatch.rm_eo != l->len) 304 r = REG_NOMATCH; 305 /* Check for whole word match */ 306 if (r == 0 && (wflag || fg_pattern[i].word)) { 307 wint_t wbegin, wend; 308 309 wbegin = wend = L' '; 310 if (pmatch.rm_so != 0 && 311 sscanf(&l->dat[pmatch.rm_so - 1], 312 "%lc", &wbegin) != 1) 313 r = REG_NOMATCH; 314 else if ((size_t)pmatch.rm_eo != 315 l->len && 316 sscanf(&l->dat[pmatch.rm_eo], 317 "%lc", &wend) != 1) 318 r = REG_NOMATCH; 319 else if (iswword(wbegin) || 320 iswword(wend)) 321 r = REG_NOMATCH; 322 } 323 if (r == 0) { 324 if (m == 0) 325 c++; 326 if (m < MAX_LINE_MATCHES) 327 matches[m++] = pmatch; 328 /* matches - skip further patterns */ 329 if ((color == NULL && !oflag) || 330 qflag || lflag) 331 break; 332 } 333 } 334 335 if (vflag) { 336 c = !c; 337 break; 338 } 339 340 /* One pass if we are not recording matches */ 341 if ((color == NULL && !oflag) || qflag || lflag) 342 break; 343 344 if (st == (size_t)pmatch.rm_so) 345 break; /* No matches */ 346 } 347 348 349 /* Count the matches if we have a match limit */ 350 if (mflag) 351 mcount -= c; 352 353 if (c && binbehave == BINFILE_BIN && nottext) 354 return (c); /* Binary file */ 355 356 /* Dealing with the context */ 357 if ((tail || c) && !cflag && !qflag && !lflag && !Lflag) { 358 if (c) { 359 if (!first && !prev && !tail && Aflag) 360 printf("--\n"); 361 tail = Aflag; 362 if (Bflag > 0) { 363 if (!first && !prev) 364 printf("--\n"); 365 printqueue(); 366 } 367 linesqueued = 0; 368 printline(l, ':', matches, m); 369 } else { 370 printline(l, '-', matches, m); 371 tail--; 372 } 373 } 374 375 if (c) { 376 prev = true; 377 first = false; 378 } else 379 prev = false; 380 381 return (c); 382} 383 384/* 385 * Safe malloc() for internal use. 386 */ 387void * 388grep_malloc(size_t size) 389{ 390 void *ptr; 391 392 if ((ptr = malloc(size)) == NULL) 393 err(2, "malloc"); 394 return (ptr); 395} 396 397/* 398 * Safe calloc() for internal use. 399 */ 400void * 401grep_calloc(size_t nmemb, size_t size) 402{ 403 void *ptr; 404 405 if ((ptr = calloc(nmemb, size)) == NULL) 406 err(2, "calloc"); 407 return (ptr); 408} 409 410/* 411 * Safe realloc() for internal use. 412 */ 413void * 414grep_realloc(void *ptr, size_t size) 415{ 416 417 if ((ptr = realloc(ptr, size)) == NULL) 418 err(2, "realloc"); 419 return (ptr); 420} 421 422/* 423 * Safe strdup() for internal use. 424 */ 425char * 426grep_strdup(const char *str) 427{ 428 char *ret; 429 430 if ((ret = strdup(str)) == NULL) 431 err(2, "strdup"); 432 return (ret); 433} 434 435/* 436 * Prints a matching line according to the command line options. 437 */ 438void 439printline(struct str *line, int sep, regmatch_t *matches, int m) 440{ 441 size_t a = 0; 442 int i, n = 0; 443 444 if (!hflag) { 445 if (!nullflag) { 446 fputs(line->file, stdout); 447 ++n; 448 } else { 449 printf("%s", line->file); 450 putchar(0); 451 } 452 } 453 if (nflag) { 454 if (n > 0) 455 putchar(sep); 456 printf("%d", line->line_no); 457 ++n; 458 } 459 if (bflag) { 460 if (n > 0) 461 putchar(sep); 462 printf("%lld", (long long)line->off); 463 ++n; 464 } 465 if (n) 466 putchar(sep); 467 /* --color and -o */ 468 if ((oflag || color) && m > 0) { 469 for (i = 0; i < m; i++) { 470 if (!oflag) 471 fwrite(line->dat + a, matches[i].rm_so - a, 1, 472 stdout); 473 if (color) 474 fprintf(stdout, "\33[%sm\33[K", color); 475 476 fwrite(line->dat + matches[i].rm_so, 477 matches[i].rm_eo - matches[i].rm_so, 1, 478 stdout); 479 if (color) 480 fprintf(stdout, "\33[m\33[K"); 481 a = matches[i].rm_eo; 482 if (oflag) 483 putchar('\n'); 484 } 485 if (!oflag) { 486 if (line->len - a > 0) 487 fwrite(line->dat + a, line->len - a, 1, stdout); 488 putchar('\n'); 489 } 490 } else { 491 fwrite(line->dat, line->len, 1, stdout); 492 putchar('\n'); 493 } 494}
|