busybox/findutils/grep.c
<<
>>
Prefs
   1/* vi: set sw=4 ts=4: */
   2/*
   3 * Mini grep implementation for busybox using libc regex.
   4 *
   5 * Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley
   6 * Copyright (C) 1999,2000,2001 by Mark Whitley <markw@codepoet.org>
   7 *
   8 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
   9 */
  10/* BB_AUDIT SUSv3 defects - unsupported option -x "match whole line only". */
  11/* BB_AUDIT GNU defects - always acts as -a.  */
  12/* http://www.opengroup.org/onlinepubs/007904975/utilities/grep.html */
  13/*
  14 * 2004,2006 (C) Vladimir Oleynik <dzo@simtreas.ru> -
  15 * correction "-e pattern1 -e pattern2" logic and more optimizations.
  16 * precompiled regex
  17 *
  18 * (C) 2006 Jac Goudsmit added -o option
  19 */
  20//config:config GREP
  21//config:       bool "grep (8.5 kb)"
  22//config:       default y
  23//config:       help
  24//config:       grep is used to search files for a specified pattern.
  25//config:
  26//config:config EGREP
  27//config:       bool "egrep (7.6 kb)"
  28//config:       default y
  29//config:       help
  30//config:       Alias to "grep -E".
  31//config:
  32//config:config FGREP
  33//config:       bool "fgrep (7.6 kb)"
  34//config:       default y
  35//config:       help
  36//config:       Alias to "grep -F".
  37//config:
  38//config:config FEATURE_GREP_CONTEXT
  39//config:       bool "Enable before and after context flags (-A, -B and -C)"
  40//config:       default y
  41//config:       depends on GREP || EGREP || FGREP
  42//config:       help
  43//config:       Print the specified number of leading (-B) and/or trailing (-A)
  44//config:       context surrounding our matching lines.
  45//config:       Print the specified number of context lines (-C).
  46
  47//applet:IF_GREP(APPLET(grep, BB_DIR_BIN, BB_SUID_DROP))
  48//                APPLET_ODDNAME:name   main  location    suid_type     help
  49//applet:IF_EGREP(APPLET_ODDNAME(egrep, grep, BB_DIR_BIN, BB_SUID_DROP, egrep))
  50//applet:IF_FGREP(APPLET_ODDNAME(fgrep, grep, BB_DIR_BIN, BB_SUID_DROP, fgrep))
  51
  52//kbuild:lib-$(CONFIG_GREP) += grep.o
  53//kbuild:lib-$(CONFIG_EGREP) += grep.o
  54//kbuild:lib-$(CONFIG_FGREP) += grep.o
  55
  56#include "libbb.h"
  57#include "common_bufsiz.h"
  58#include "xregex.h"
  59
  60
  61/* options */
  62//usage:#define grep_trivial_usage
  63//usage:       "[-HhnlLoqvsriwFE"
  64//usage:        IF_EXTRA_COMPAT("z")
  65//usage:       "] [-m N] "
  66//usage:        IF_FEATURE_GREP_CONTEXT("[-A/B/C N] ")
  67//usage:       "PATTERN/-e PATTERN.../-f FILE [FILE]..."
  68//usage:#define grep_full_usage "\n\n"
  69//usage:       "Search for PATTERN in FILEs (or stdin)\n"
  70//usage:     "\n        -H      Add 'filename:' prefix"
  71//usage:     "\n        -h      Do not add 'filename:' prefix"
  72//usage:     "\n        -n      Add 'line_no:' prefix"
  73//usage:     "\n        -l      Show only names of files that match"
  74//usage:     "\n        -L      Show only names of files that don't match"
  75//usage:     "\n        -c      Show only count of matching lines"
  76//usage:     "\n        -o      Show only the matching part of line"
  77//usage:     "\n        -q      Quiet. Return 0 if PATTERN is found, 1 otherwise"
  78//usage:     "\n        -v      Select non-matching lines"
  79//usage:     "\n        -s      Suppress open and read errors"
  80//usage:     "\n        -r      Recurse"
  81//usage:     "\n        -i      Ignore case"
  82//usage:     "\n        -w      Match whole words only"
  83//usage:     "\n        -x      Match whole lines only"
  84//usage:     "\n        -F      PATTERN is a literal (not regexp)"
  85//usage:     "\n        -E      PATTERN is an extended regexp"
  86//usage:        IF_EXTRA_COMPAT(
  87//usage:     "\n        -z      Input is NUL terminated"
  88//usage:        )
  89//usage:     "\n        -m N    Match up to N times per file"
  90//usage:        IF_FEATURE_GREP_CONTEXT(
  91//usage:     "\n        -A N    Print N lines of trailing context"
  92//usage:     "\n        -B N    Print N lines of leading context"
  93//usage:     "\n        -C N    Same as '-A N -B N'"
  94//usage:        )
  95//usage:     "\n        -e PTRN Pattern to match"
  96//usage:     "\n        -f FILE Read pattern from file"
  97//usage:
  98//usage:#define grep_example_usage
  99//usage:       "$ grep root /etc/passwd\n"
 100//usage:       "root:x:0:0:root:/root:/bin/bash\n"
 101//usage:       "$ grep ^[rR]oo. /etc/passwd\n"
 102//usage:       "root:x:0:0:root:/root:/bin/bash\n"
 103//usage:
 104//usage:#define egrep_trivial_usage NOUSAGE_STR
 105//usage:#define egrep_full_usage ""
 106//usage:#define fgrep_trivial_usage NOUSAGE_STR
 107//usage:#define fgrep_full_usage ""
 108
 109/* -e,-f are lists; -m,-A,-B,-C have numeric param */
 110#define OPTSTR_GREP \
 111        "lnqvscFiHhe:*f:*Lorm:+wx" \
 112        IF_FEATURE_GREP_CONTEXT("A:+B:+C:+") \
 113        "E" \
 114        IF_EXTRA_COMPAT("z") \
 115        "aI"
 116/* ignored: -a "assume all files to be text" */
 117/* ignored: -I "assume binary files have no matches" */
 118enum {
 119        OPTBIT_l, /* list matched file names only */
 120        OPTBIT_n, /* print line# */
 121        OPTBIT_q, /* quiet - exit(EXIT_SUCCESS) of first match */
 122        OPTBIT_v, /* invert the match, to select non-matching lines */
 123        OPTBIT_s, /* suppress errors about file open errors */
 124        OPTBIT_c, /* count matches per file (suppresses normal output) */
 125        OPTBIT_F, /* literal match */
 126        OPTBIT_i, /* case-insensitive */
 127        OPTBIT_H, /* force filename display */
 128        OPTBIT_h, /* inhibit filename display */
 129        OPTBIT_e, /* -e PATTERN */
 130        OPTBIT_f, /* -f FILE_WITH_PATTERNS */
 131        OPTBIT_L, /* list unmatched file names only */
 132        OPTBIT_o, /* show only matching parts of lines */
 133        OPTBIT_r, /* recurse dirs */
 134        OPTBIT_m, /* -m MAX_MATCHES */
 135        OPTBIT_w, /* -w whole word match */
 136        OPTBIT_x, /* -x whole line match */
 137        IF_FEATURE_GREP_CONTEXT(    OPTBIT_A ,) /* -A NUM: after-match context */
 138        IF_FEATURE_GREP_CONTEXT(    OPTBIT_B ,) /* -B NUM: before-match context */
 139        IF_FEATURE_GREP_CONTEXT(    OPTBIT_C ,) /* -C NUM: -A and -B combined */
 140        OPTBIT_E, /* extended regexp */
 141        IF_EXTRA_COMPAT(            OPTBIT_z ,) /* input is NUL terminated */
 142        OPT_l = 1 << OPTBIT_l,
 143        OPT_n = 1 << OPTBIT_n,
 144        OPT_q = 1 << OPTBIT_q,
 145        OPT_v = 1 << OPTBIT_v,
 146        OPT_s = 1 << OPTBIT_s,
 147        OPT_c = 1 << OPTBIT_c,
 148        OPT_F = 1 << OPTBIT_F,
 149        OPT_i = 1 << OPTBIT_i,
 150        OPT_H = 1 << OPTBIT_H,
 151        OPT_h = 1 << OPTBIT_h,
 152        OPT_e = 1 << OPTBIT_e,
 153        OPT_f = 1 << OPTBIT_f,
 154        OPT_L = 1 << OPTBIT_L,
 155        OPT_o = 1 << OPTBIT_o,
 156        OPT_r = 1 << OPTBIT_r,
 157        OPT_m = 1 << OPTBIT_m,
 158        OPT_w = 1 << OPTBIT_w,
 159        OPT_x = 1 << OPTBIT_x,
 160        OPT_A = IF_FEATURE_GREP_CONTEXT(    (1 << OPTBIT_A)) + 0,
 161        OPT_B = IF_FEATURE_GREP_CONTEXT(    (1 << OPTBIT_B)) + 0,
 162        OPT_C = IF_FEATURE_GREP_CONTEXT(    (1 << OPTBIT_C)) + 0,
 163        OPT_E = 1 << OPTBIT_E,
 164        OPT_z = IF_EXTRA_COMPAT(            (1 << OPTBIT_z)) + 0,
 165};
 166
 167#define PRINT_FILES_WITH_MATCHES    (option_mask32 & OPT_l)
 168#define PRINT_LINE_NUM              (option_mask32 & OPT_n)
 169#define BE_QUIET                    (option_mask32 & OPT_q)
 170#define SUPPRESS_ERR_MSGS           (option_mask32 & OPT_s)
 171#define PRINT_MATCH_COUNTS          (option_mask32 & OPT_c)
 172#define FGREP_FLAG                  (option_mask32 & OPT_F)
 173#define PRINT_FILES_WITHOUT_MATCHES (option_mask32 & OPT_L)
 174#define NUL_DELIMITED               (option_mask32 & OPT_z)
 175
 176struct globals {
 177        int max_matches;
 178#if !ENABLE_EXTRA_COMPAT
 179        int reflags;
 180#else
 181        RE_TRANSLATE_TYPE case_fold; /* RE_TRANSLATE_TYPE is [[un]signed] char* */
 182#endif
 183        smalluint invert_search;
 184        smalluint print_filename;
 185        smalluint open_errors;
 186#if ENABLE_FEATURE_GREP_CONTEXT
 187        smalluint did_print_line;
 188        int lines_before;
 189        int lines_after;
 190        char **before_buf;
 191        IF_EXTRA_COMPAT(size_t *before_buf_size;)
 192        int last_line_printed;
 193#endif
 194        /* globals used internally */
 195        llist_t *pattern_head;   /* growable list of patterns to match */
 196        const char *cur_file;    /* the current file we are reading */
 197} FIX_ALIASING;
 198#define G (*(struct globals*)bb_common_bufsiz1)
 199#define INIT_G() do { \
 200        setup_common_bufsiz(); \
 201        BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
 202} while (0)
 203#define max_matches       (G.max_matches         )
 204#if !ENABLE_EXTRA_COMPAT
 205# define reflags          (G.reflags             )
 206#else
 207# define case_fold        (G.case_fold           )
 208/* http://www.delorie.com/gnu/docs/regex/regex_46.html */
 209# define reflags           re_syntax_options
 210# undef REG_NOSUB
 211# undef REG_EXTENDED
 212# undef REG_ICASE
 213# define REG_NOSUB    bug:is:here /* should not be used */
 214/* Just RE_SYNTAX_EGREP is not enough, need to enable {n[,[m]]} too */
 215# define REG_EXTENDED (RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES)
 216# define REG_ICASE    bug:is:here /* should not be used */
 217#endif
 218#define invert_search     (G.invert_search       )
 219#define print_filename    (G.print_filename      )
 220#define open_errors       (G.open_errors         )
 221#define did_print_line    (G.did_print_line      )
 222#define lines_before      (G.lines_before        )
 223#define lines_after       (G.lines_after         )
 224#define before_buf        (G.before_buf          )
 225#define before_buf_size   (G.before_buf_size     )
 226#define last_line_printed (G.last_line_printed   )
 227#define pattern_head      (G.pattern_head        )
 228#define cur_file          (G.cur_file            )
 229
 230
 231typedef struct grep_list_data_t {
 232        char *pattern;
 233/* for GNU regex, matched_range must be persistent across grep_file() calls */
 234#if !ENABLE_EXTRA_COMPAT
 235        regex_t compiled_regex;
 236        regmatch_t matched_range;
 237#else
 238        struct re_pattern_buffer compiled_regex;
 239        struct re_registers matched_range;
 240#endif
 241#define ALLOCATED 1
 242#define COMPILED 2
 243        int flg_mem_allocated_compiled;
 244} grep_list_data_t;
 245
 246#if !ENABLE_EXTRA_COMPAT
 247#define print_line(line, line_len, linenum, decoration) \
 248        print_line(line, linenum, decoration)
 249#endif
 250static void print_line(const char *line, size_t line_len, int linenum, char decoration)
 251{
 252#if ENABLE_FEATURE_GREP_CONTEXT
 253        /* Happens when we go to next file, immediately hit match
 254         * and try to print prev context... from prev file! Don't do it */
 255        if (linenum < 1)
 256                return;
 257        /* possibly print the little '--' separator */
 258        if ((lines_before || lines_after) && did_print_line
 259         && last_line_printed != linenum - 1
 260        ) {
 261                puts("--");
 262        }
 263        /* guard against printing "--" before first line of first file */
 264        did_print_line = 1;
 265        last_line_printed = linenum;
 266#endif
 267        if (print_filename)
 268                printf("%s%c", cur_file, decoration);
 269        if (PRINT_LINE_NUM)
 270                printf("%i%c", linenum, decoration);
 271        /* Emulate weird GNU grep behavior with -ov */
 272        if ((option_mask32 & (OPT_v|OPT_o)) != (OPT_v|OPT_o)) {
 273#if !ENABLE_EXTRA_COMPAT
 274                puts(line);
 275#else
 276                fwrite(line, 1, line_len, stdout);
 277                putchar(NUL_DELIMITED ? '\0' : '\n');
 278#endif
 279        }
 280}
 281
 282#if ENABLE_EXTRA_COMPAT
 283/* Unlike getline, this one removes trailing '\n' */
 284static ssize_t FAST_FUNC bb_getline(char **line_ptr, size_t *line_alloc_len, FILE *file)
 285{
 286        ssize_t res_sz;
 287        char *line;
 288        int delim = (NUL_DELIMITED ? '\0' : '\n');
 289
 290        res_sz = getdelim(line_ptr, line_alloc_len, delim, file);
 291        line = *line_ptr;
 292
 293        if (res_sz > 0) {
 294                if (line[res_sz - 1] == delim)
 295                        line[--res_sz] = '\0';
 296        } else {
 297                free(line); /* uclibc allocates a buffer even on EOF. WTF? */
 298        }
 299        return res_sz;
 300}
 301#endif
 302
 303static int grep_file(FILE *file)
 304{
 305        smalluint found;
 306        int linenum = 0;
 307        int nmatches = 0;
 308#if !ENABLE_EXTRA_COMPAT
 309        char *line;
 310#else
 311        char *line = NULL;
 312        ssize_t line_len;
 313        size_t line_alloc_len;
 314# define rm_so start[0]
 315# define rm_eo end[0]
 316#endif
 317#if ENABLE_FEATURE_GREP_CONTEXT
 318        int print_n_lines_after = 0;
 319        int curpos = 0; /* track where we are in the circular 'before' buffer */
 320        int idx = 0; /* used for iteration through the circular buffer */
 321#else
 322        enum { print_n_lines_after = 0 };
 323#endif
 324
 325        while (
 326#if !ENABLE_EXTRA_COMPAT
 327                (line = xmalloc_fgetline(file)) != NULL
 328#else
 329                (line_len = bb_getline(&line, &line_alloc_len, file)) >= 0
 330#endif
 331        ) {
 332                llist_t *pattern_ptr = pattern_head;
 333                grep_list_data_t *gl = gl; /* for gcc */
 334
 335                linenum++;
 336                found = 0;
 337                while (pattern_ptr) {
 338                        gl = (grep_list_data_t *)pattern_ptr->data;
 339                        if (FGREP_FLAG) {
 340                                char *match;
 341                                char *str = line;
 342 opt_f_again:
 343                                match = ((option_mask32 & OPT_i)
 344                                        ? strcasestr(str, gl->pattern)
 345                                        : strstr(str, gl->pattern)
 346                                        );
 347                                if (match) {
 348                                        if (option_mask32 & OPT_x) {
 349                                                if (match != str)
 350                                                        goto opt_f_not_found;
 351                                                if (str[strlen(gl->pattern)] != '\0')
 352                                                        goto opt_f_not_found;
 353                                        } else
 354                                        if (option_mask32 & OPT_w) {
 355                                                char c = (match != line) ? match[-1] : ' ';
 356                                                if (!isalnum(c) && c != '_') {
 357                                                        c = match[strlen(gl->pattern)];
 358                                                        if (!c || (!isalnum(c) && c != '_'))
 359                                                                goto opt_f_found;
 360                                                }
 361                                                str = match + 1;
 362                                                goto opt_f_again;
 363                                        }
 364 opt_f_found:
 365                                        found = 1;
 366 opt_f_not_found: ;
 367                                }
 368                        } else {
 369#if ENABLE_EXTRA_COMPAT
 370                                unsigned start_pos;
 371#else
 372                                int match_flg;
 373#endif
 374                                char *match_at;
 375
 376                                if (!(gl->flg_mem_allocated_compiled & COMPILED)) {
 377                                        gl->flg_mem_allocated_compiled |= COMPILED;
 378#if !ENABLE_EXTRA_COMPAT
 379                                        xregcomp(&gl->compiled_regex, gl->pattern, reflags);
 380#else
 381                                        memset(&gl->compiled_regex, 0, sizeof(gl->compiled_regex));
 382                                        gl->compiled_regex.translate = case_fold; /* for -i */
 383                                        if (re_compile_pattern(gl->pattern, strlen(gl->pattern), &gl->compiled_regex))
 384                                                bb_error_msg_and_die("bad regex '%s'", gl->pattern);
 385#endif
 386                                }
 387#if !ENABLE_EXTRA_COMPAT
 388                                gl->matched_range.rm_so = 0;
 389                                gl->matched_range.rm_eo = 0;
 390                                match_flg = 0;
 391#else
 392                                start_pos = 0;
 393#endif
 394                                match_at = line;
 395 opt_w_again:
 396//bb_error_msg("'%s' start_pos:%d line_len:%d", match_at, start_pos, line_len);
 397                                if (
 398#if !ENABLE_EXTRA_COMPAT
 399                                        regexec(&gl->compiled_regex, match_at, 1, &gl->matched_range, match_flg) == 0
 400#else
 401                                        re_search(&gl->compiled_regex, match_at, line_len,
 402                                                        start_pos, /*range:*/ line_len,
 403                                                        &gl->matched_range) >= 0
 404#endif
 405                                ) {
 406                                        if (option_mask32 & OPT_x) {
 407                                                found = (gl->matched_range.rm_so == 0
 408                                                         && match_at[gl->matched_range.rm_eo] == '\0');
 409                                        } else
 410                                        if (!(option_mask32 & OPT_w)) {
 411                                                found = 1;
 412                                        } else {
 413                                                char c = ' ';
 414                                                if (match_at > line || gl->matched_range.rm_so != 0) {
 415                                                        c = match_at[gl->matched_range.rm_so - 1];
 416                                                }
 417                                                if (!isalnum(c) && c != '_') {
 418                                                        c = match_at[gl->matched_range.rm_eo];
 419                                                }
 420                                                if (!isalnum(c) && c != '_') {
 421                                                        found = 1;
 422                                                } else {
 423                        /*
 424                         * Why check gl->matched_range.rm_eo?
 425                         * Zero-length match makes -w skip the line:
 426                         * "echo foo | grep ^" prints "foo",
 427                         * "echo foo | grep -w ^" prints nothing.
 428                         * Without such check, we can loop forever.
 429                         */
 430#if !ENABLE_EXTRA_COMPAT
 431                                                        if (gl->matched_range.rm_eo != 0) {
 432                                                                match_at += gl->matched_range.rm_eo;
 433                                                                match_flg |= REG_NOTBOL;
 434                                                                goto opt_w_again;
 435                                                        }
 436#else
 437                                                        if (gl->matched_range.rm_eo > start_pos) {
 438                                                                start_pos = gl->matched_range.rm_eo;
 439                                                                goto opt_w_again;
 440                                                        }
 441#endif
 442                                                }
 443                                        }
 444                                }
 445                        }
 446                        /* If it's non-inverted search, we can stop
 447                         * at first match */
 448                        if (found && !invert_search)
 449                                goto do_found;
 450                        pattern_ptr = pattern_ptr->link;
 451                } /* while (pattern_ptr) */
 452
 453                if (found ^ invert_search) {
 454 do_found:
 455                        /* keep track of matches */
 456                        nmatches++;
 457
 458                        /* quiet/print (non)matching file names only? */
 459                        if (option_mask32 & (OPT_q|OPT_l|OPT_L)) {
 460                                free(line); /* we don't need line anymore */
 461                                if (BE_QUIET) {
 462                                        /* manpage says about -q:
 463                                         * "exit immediately with zero status
 464                                         * if any match is found,
 465                                         * even if errors were detected" */
 466                                        exit(EXIT_SUCCESS);
 467                                }
 468                                /* if we're just printing filenames, we stop after the first match */
 469                                if (PRINT_FILES_WITH_MATCHES) {
 470                                        puts(cur_file);
 471                                        /* fall through to "return 1" */
 472                                }
 473                                /* OPT_L aka PRINT_FILES_WITHOUT_MATCHES: return early */
 474                                return 1; /* one match */
 475                        }
 476
 477#if ENABLE_FEATURE_GREP_CONTEXT
 478                        /* Were we printing context and saw next (unwanted) match? */
 479                        if ((option_mask32 & OPT_m) && nmatches > max_matches)
 480                                break;
 481#endif
 482
 483                        /* print the matched line */
 484                        if (PRINT_MATCH_COUNTS == 0) {
 485#if ENABLE_FEATURE_GREP_CONTEXT
 486                                int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1;
 487
 488                                /* if we were told to print 'before' lines and there is at least
 489                                 * one line in the circular buffer, print them */
 490                                if (lines_before && before_buf[prevpos] != NULL) {
 491                                        int first_buf_entry_line_num = linenum - lines_before;
 492
 493                                        /* advance to the first entry in the circular buffer, and
 494                                         * figure out the line number is of the first line in the
 495                                         * buffer */
 496                                        idx = curpos;
 497                                        while (before_buf[idx] == NULL) {
 498                                                idx = (idx + 1) % lines_before;
 499                                                first_buf_entry_line_num++;
 500                                        }
 501
 502                                        /* now print each line in the buffer, clearing them as we go */
 503                                        while (before_buf[idx] != NULL) {
 504                                                print_line(before_buf[idx], before_buf_size[idx], first_buf_entry_line_num, '-');
 505                                                free(before_buf[idx]);
 506                                                before_buf[idx] = NULL;
 507                                                idx = (idx + 1) % lines_before;
 508                                                first_buf_entry_line_num++;
 509                                        }
 510                                }
 511
 512                                /* make a note that we need to print 'after' lines */
 513                                print_n_lines_after = lines_after;
 514#endif
 515                                if (option_mask32 & OPT_o) {
 516                                        if (FGREP_FLAG) {
 517                                                /* -Fo just prints the pattern
 518                                                 * (unless -v: -Fov doesn't print anything at all) */
 519                                                if (found)
 520                                                        print_line(gl->pattern, strlen(gl->pattern), linenum, ':');
 521                                        } else while (1) {
 522                                                unsigned start = gl->matched_range.rm_so;
 523                                                unsigned end = gl->matched_range.rm_eo;
 524                                                unsigned len = end - start;
 525                                                char old = line[end];
 526                                                line[end] = '\0';
 527                                                /* Empty match is not printed: try "echo test | grep -o ''" */
 528                                                if (len != 0)
 529                                                        print_line(line + start, len, linenum, ':');
 530                                                if (old == '\0')
 531                                                        break;
 532                                                line[end] = old;
 533                                                if (len == 0)
 534                                                        end++;
 535#if !ENABLE_EXTRA_COMPAT
 536                                                if (regexec(&gl->compiled_regex, line + end,
 537                                                                1, &gl->matched_range, REG_NOTBOL) != 0)
 538                                                        break;
 539                                                gl->matched_range.rm_so += end;
 540                                                gl->matched_range.rm_eo += end;
 541#else
 542                                                if (re_search(&gl->compiled_regex, line, line_len,
 543                                                                end, line_len - end,
 544                                                                &gl->matched_range) < 0)
 545                                                        break;
 546#endif
 547                                        }
 548                                } else {
 549                                        print_line(line, line_len, linenum, ':');
 550                                }
 551                        }
 552                }
 553#if ENABLE_FEATURE_GREP_CONTEXT
 554                else { /* no match */
 555                        /* if we need to print some context lines after the last match, do so */
 556                        if (print_n_lines_after) {
 557                                print_line(line, strlen(line), linenum, '-');
 558                                print_n_lines_after--;
 559                        } else if (lines_before) {
 560                                /* Add the line to the circular 'before' buffer */
 561                                free(before_buf[curpos]);
 562                                before_buf[curpos] = line;
 563                                IF_EXTRA_COMPAT(before_buf_size[curpos] = line_len;)
 564                                curpos = (curpos + 1) % lines_before;
 565                                /* avoid free(line) - we took the line */
 566                                line = NULL;
 567                        }
 568                }
 569
 570#endif /* ENABLE_FEATURE_GREP_CONTEXT */
 571#if !ENABLE_EXTRA_COMPAT
 572                free(line);
 573#endif
 574                /* Did we print all context after last requested match? */
 575                if ((option_mask32 & OPT_m)
 576                 && !print_n_lines_after
 577                 && nmatches == max_matches
 578                ) {
 579                        break;
 580                }
 581        } /* while (read line) */
 582
 583        /* special-case file post-processing for options where we don't print line
 584         * matches, just filenames and possibly match counts */
 585
 586        /* grep -c: print [filename:]count, even if count is zero */
 587        if (PRINT_MATCH_COUNTS) {
 588                if (print_filename)
 589                        printf("%s:", cur_file);
 590                printf("%d\n", nmatches);
 591        }
 592
 593        /* grep -L: print just the filename */
 594        if (PRINT_FILES_WITHOUT_MATCHES) {
 595                /* nmatches is zero, no need to check it:
 596                 * we return 1 early if we detected a match
 597                 * and PRINT_FILES_WITHOUT_MATCHES is set */
 598                puts(cur_file);
 599        }
 600
 601        return nmatches;
 602}
 603
 604#if ENABLE_FEATURE_CLEAN_UP
 605#define new_grep_list_data(p, m) add_grep_list_data(p, m)
 606static char *add_grep_list_data(char *pattern, int flg_used_mem)
 607#else
 608#define new_grep_list_data(p, m) add_grep_list_data(p)
 609static char *add_grep_list_data(char *pattern)
 610#endif
 611{
 612        grep_list_data_t *gl = xzalloc(sizeof(*gl));
 613        gl->pattern = pattern;
 614#if ENABLE_FEATURE_CLEAN_UP
 615        gl->flg_mem_allocated_compiled = flg_used_mem;
 616#else
 617        /*gl->flg_mem_allocated_compiled = 0;*/
 618#endif
 619        return (char *)gl;
 620}
 621
 622static void load_regexes_from_file(llist_t *fopt)
 623{
 624        while (fopt) {
 625                char *line;
 626                FILE *fp;
 627                llist_t *cur = fopt;
 628                char *ffile = cur->data;
 629
 630                fopt = cur->link;
 631                free(cur);
 632                fp = xfopen_stdin(ffile);
 633                while ((line = xmalloc_fgetline(fp)) != NULL) {
 634                        llist_add_to(&pattern_head,
 635                                new_grep_list_data(line, ALLOCATED));
 636                }
 637                fclose_if_not_stdin(fp);
 638        }
 639}
 640
 641static int FAST_FUNC file_action_grep(const char *filename,
 642                        struct stat *statbuf,
 643                        void* matched,
 644                        int depth UNUSED_PARAM)
 645{
 646        FILE *file;
 647
 648        /* If we are given a link to a directory, we should bail out now, rather
 649         * than trying to open the "file" and hoping getline gives us nothing,
 650         * since that is not portable across operating systems (FreeBSD for
 651         * example will return the raw directory contents). */
 652        if (S_ISLNK(statbuf->st_mode)) {
 653                struct stat sb;
 654                if (stat(filename, &sb) != 0) {
 655                        if (!SUPPRESS_ERR_MSGS)
 656                                bb_simple_perror_msg(filename);
 657                        return 0;
 658                }
 659                if (S_ISDIR(sb.st_mode))
 660                        return 1;
 661        }
 662
 663        file = fopen_for_read(filename);
 664        if (file == NULL) {
 665                if (!SUPPRESS_ERR_MSGS)
 666                        bb_simple_perror_msg(filename);
 667                open_errors = 1;
 668                return 0;
 669        }
 670        cur_file = filename;
 671        *(int*)matched += grep_file(file);
 672        fclose(file);
 673        return 1;
 674}
 675
 676static int grep_dir(const char *dir)
 677{
 678        int matched = 0;
 679        recursive_action(dir,
 680                /* recurse=yes */ ACTION_RECURSE |
 681                /* followLinks=command line only */ ACTION_FOLLOWLINKS_L0 |
 682                /* depthFirst=yes */ ACTION_DEPTHFIRST,
 683                /* fileAction= */ file_action_grep,
 684                /* dirAction= */ NULL,
 685                /* userData= */ &matched,
 686                /* depth= */ 0);
 687        return matched;
 688}
 689
 690int grep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 691int grep_main(int argc UNUSED_PARAM, char **argv)
 692{
 693        FILE *file;
 694        int matched;
 695        llist_t *fopt = NULL;
 696#if ENABLE_FEATURE_GREP_CONTEXT
 697        int Copt, opts;
 698#endif
 699        INIT_G();
 700
 701        /* For grep, exitcode of 1 is "not found". Other errors are 2: */
 702        xfunc_error_retval = 2;
 703
 704        /* do normal option parsing */
 705#if ENABLE_FEATURE_GREP_CONTEXT
 706        /* -H unsets -h; -C unsets -A,-B */
 707        opts = getopt32(argv,
 708                "^" OPTSTR_GREP "\0" "H-h:C-AB",
 709                &pattern_head, &fopt, &max_matches,
 710                &lines_after, &lines_before, &Copt);
 711
 712        if (opts & OPT_C) {
 713                /* -C unsets prev -A and -B, but following -A or -B
 714                 * may override it */
 715                if (!(opts & OPT_A)) /* not overridden */
 716                        lines_after = Copt;
 717                if (!(opts & OPT_B)) /* not overridden */
 718                        lines_before = Copt;
 719        }
 720        /* sanity checks */
 721        if (opts & (OPT_c|OPT_q|OPT_l|OPT_L)) {
 722                option_mask32 &= ~OPT_n;
 723                lines_before = 0;
 724                lines_after = 0;
 725        } else if (lines_before > 0) {
 726                if (lines_before > INT_MAX / sizeof(long long))
 727                        lines_before = INT_MAX / sizeof(long long);
 728                /* overflow in (lines_before * sizeof(x)) is prevented (above) */
 729                before_buf = xzalloc(lines_before * sizeof(before_buf[0]));
 730                IF_EXTRA_COMPAT(before_buf_size = xzalloc(lines_before * sizeof(before_buf_size[0]));)
 731        }
 732#else
 733        /* with auto sanity checks */
 734        getopt32(argv, "^" OPTSTR_GREP "\0" "H-h:c-n:q-n:l-n:", // why trailing ":"?
 735                &pattern_head, &fopt, &max_matches);
 736#endif
 737        invert_search = ((option_mask32 & OPT_v) != 0); /* 0 | 1 */
 738
 739        {       /* convert char **argv to grep_list_data_t */
 740                llist_t *cur;
 741                for (cur = pattern_head; cur; cur = cur->link)
 742                        cur->data = new_grep_list_data(cur->data, 0);
 743        }
 744        if (option_mask32 & OPT_f) {
 745                load_regexes_from_file(fopt);
 746                if (!pattern_head) { /* -f EMPTY_FILE? */
 747                        /* GNU grep treats it as "nothing matches" */
 748                        llist_add_to(&pattern_head, new_grep_list_data((char*) "", 0));
 749                        invert_search ^= 1;
 750                }
 751        }
 752
 753        if (ENABLE_FGREP && applet_name[0] == 'f')
 754                option_mask32 |= OPT_F;
 755
 756#if !ENABLE_EXTRA_COMPAT
 757        if (!(option_mask32 & (OPT_o | OPT_w | OPT_x)))
 758                reflags = REG_NOSUB;
 759#endif
 760
 761        if ((ENABLE_EGREP && applet_name[0] == 'e')
 762         || (option_mask32 & OPT_E)
 763        ) {
 764                reflags |= REG_EXTENDED;
 765        }
 766#if ENABLE_EXTRA_COMPAT
 767        else {
 768                reflags = RE_SYNTAX_GREP;
 769        }
 770#endif
 771
 772        if (option_mask32 & OPT_i) {
 773#if !ENABLE_EXTRA_COMPAT
 774                reflags |= REG_ICASE;
 775#else
 776                int i;
 777                case_fold = xmalloc(256);
 778                for (i = 0; i < 256; i++)
 779                        case_fold[i] = (unsigned char)i;
 780                for (i = 'a'; i <= 'z'; i++)
 781                        case_fold[i] = (unsigned char)(i - ('a' - 'A'));
 782#endif
 783        }
 784
 785        argv += optind;
 786
 787        /* if we didn't get a pattern from -e and no command file was specified,
 788         * first parameter should be the pattern. no pattern, no worky */
 789        if (pattern_head == NULL) {
 790                char *pattern;
 791                if (*argv == NULL)
 792                        bb_show_usage();
 793                pattern = new_grep_list_data(*argv++, 0);
 794                llist_add_to(&pattern_head, pattern);
 795        }
 796
 797        /* argv[0..(argc-1)] should be names of file to grep through. If
 798         * there is more than one file to grep, we will print the filenames. */
 799        if (argv[0] && argv[1])
 800                print_filename = 1;
 801        /* -H / -h of course override */
 802        if (option_mask32 & OPT_H)
 803                print_filename = 1;
 804        if (option_mask32 & OPT_h)
 805                print_filename = 0;
 806
 807        /* If no files were specified, or '-' was specified, take input from
 808         * stdin. Otherwise, we grep through all the files specified. */
 809        matched = 0;
 810        do {
 811                cur_file = *argv;
 812                file = stdin;
 813                if (!cur_file || LONE_DASH(cur_file)) {
 814                        cur_file = "(standard input)";
 815                } else {
 816                        if (option_mask32 & OPT_r) {
 817                                struct stat st;
 818                                if (stat(cur_file, &st) == 0 && S_ISDIR(st.st_mode)) {
 819                                        if (!(option_mask32 & OPT_h))
 820                                                print_filename = 1;
 821                                        matched += grep_dir(cur_file);
 822                                        goto grep_done;
 823                                }
 824                        }
 825                        /* else: fopen(dir) will succeed, but reading won't */
 826                        file = fopen_for_read(cur_file);
 827                        if (file == NULL) {
 828                                if (!SUPPRESS_ERR_MSGS)
 829                                        bb_simple_perror_msg(cur_file);
 830                                open_errors = 1;
 831                                continue;
 832                        }
 833                }
 834                matched += grep_file(file);
 835                fclose_if_not_stdin(file);
 836 grep_done: ;
 837        } while (*argv && *++argv);
 838
 839        /* destroy all the elements in the pattern list */
 840        if (ENABLE_FEATURE_CLEAN_UP) {
 841                while (pattern_head) {
 842                        llist_t *pattern_head_ptr = pattern_head;
 843                        grep_list_data_t *gl = (grep_list_data_t *)pattern_head_ptr->data;
 844
 845                        pattern_head = pattern_head->link;
 846                        if (gl->flg_mem_allocated_compiled & ALLOCATED)
 847                                free(gl->pattern);
 848                        if (gl->flg_mem_allocated_compiled & COMPILED)
 849                                regfree(&gl->compiled_regex);
 850                        free(gl);
 851                        free(pattern_head_ptr);
 852                }
 853        }
 854        /* 0 = success, 1 = failed, 2 = error */
 855        if (open_errors)
 856                return 2;
 857        return !matched; /* invert return value: 0 = success, 1 = failed */
 858}
 859