Changeset 3232 in MondoRescue for branches/3.2/mindi-busybox/findutils/grep.c


Ignore:
Timestamp:
Jan 1, 2014, 12:47:38 AM (10 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.21.1
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3.2/mindi-busybox/findutils/grep.c

    r2725 r3232  
    1919 */
    2020
    21 //applet:IF_GREP(APPLET(grep, _BB_DIR_BIN, _BB_SUID_DROP))
    22 //applet:IF_FEATURE_GREP_EGREP_ALIAS(APPLET_ODDNAME(egrep, grep, _BB_DIR_BIN, _BB_SUID_DROP, egrep))
    23 //applet:IF_FEATURE_GREP_FGREP_ALIAS(APPLET_ODDNAME(fgrep, grep, _BB_DIR_BIN, _BB_SUID_DROP, fgrep))
     21//applet:IF_GREP(APPLET(grep, BB_DIR_BIN, BB_SUID_DROP))
     22//applet:IF_FEATURE_GREP_EGREP_ALIAS(APPLET_ODDNAME(egrep, grep, BB_DIR_BIN, BB_SUID_DROP, egrep))
     23//applet:IF_FEATURE_GREP_FGREP_ALIAS(APPLET_ODDNAME(fgrep, grep, BB_DIR_BIN, BB_SUID_DROP, fgrep))
    2424
    2525//kbuild:lib-$(CONFIG_GREP) += grep.o
     
    7373//usage:#define grep_full_usage "\n\n"
    7474//usage:       "Search for PATTERN in FILEs (or stdin)\n"
    75 //usage:     "\nOptions:"
    7675//usage:     "\n    -H  Add 'filename:' prefix"
    7776//usage:     "\n    -h  Do not add 'filename:' prefix"
     
    8786//usage:     "\n    -i  Ignore case"
    8887//usage:     "\n    -w  Match whole words only"
     88//usage:     "\n    -x  Match whole lines only"
    8989//usage:     "\n    -F  PATTERN is a literal (not regexp)"
    9090//usage:    IF_FEATURE_GREP_EGREP_ALIAS(
     
    115115
    116116#define OPTSTR_GREP \
    117     "lnqvscFiHhe:f:Lorm:w" \
     117    "lnqvscFiHhe:f:Lorm:wx" \
    118118    IF_FEATURE_GREP_CONTEXT("A:B:C:") \
    119119    IF_FEATURE_GREP_EGREP_ALIAS("E") \
     
    140140    OPTBIT_m, /* -m MAX_MATCHES */
    141141    OPTBIT_w, /* -w whole word match */
     142    OPTBIT_x, /* -x whole line match */
    142143    IF_FEATURE_GREP_CONTEXT(    OPTBIT_A ,) /* -A NUM: after-match context */
    143144    IF_FEATURE_GREP_CONTEXT(    OPTBIT_B ,) /* -B NUM: before-match context */
     
    162163    OPT_m = 1 << OPTBIT_m,
    163164    OPT_w = 1 << OPTBIT_w,
     165    OPT_x = 1 << OPTBIT_x,
    164166    OPT_A = IF_FEATURE_GREP_CONTEXT(    (1 << OPTBIT_A)) + 0,
    165167    OPT_B = IF_FEATURE_GREP_CONTEXT(    (1 << OPTBIT_B)) + 0,
     
    343345            gl = (grep_list_data_t *)pattern_ptr->data;
    344346            if (FGREP_FLAG) {
    345                 found |= (((option_mask32 & OPT_i)
    346                     ? strcasestr(line, gl->pattern)
    347                     : strstr(line, gl->pattern)
    348                     ) != NULL);
     347                char *match;
     348                char *str = line;
     349 opt_f_again:
     350                match = ((option_mask32 & OPT_i)
     351                    ? strcasestr(str, gl->pattern)
     352                    : strstr(str, gl->pattern)
     353                    );
     354                if (match) {
     355                    if (option_mask32 & OPT_x) {
     356                        if (match != str)
     357                            goto opt_f_not_found;
     358                        if (str[strlen(gl->pattern)] != '\0')
     359                            goto opt_f_not_found;
     360                    } else
     361                    if (option_mask32 & OPT_w) {
     362                        char c = (match != str) ? match[-1] : ' ';
     363                        if (!isalnum(c) && c != '_') {
     364                            c = match[strlen(gl->pattern)];
     365                            if (!c || (!isalnum(c) && c != '_'))
     366                                goto opt_f_found;
     367                        }
     368                        str = match + 1;
     369                        goto opt_f_again;
     370                    }
     371 opt_f_found:
     372                    found = 1;
     373 opt_f_not_found: ;
     374                }
    349375            } else {
    350376                if (!(gl->flg_mem_alocated_compiled & COMPILED)) {
     
    372398#endif
    373399                ) {
    374                     if (!(option_mask32 & OPT_w))
     400                    if (option_mask32 & OPT_x) {
     401                        found = (gl->matched_range.rm_so == 0
     402                                 && line[gl->matched_range.rm_eo] == '\0');
     403                    } else
     404                    if (!(option_mask32 & OPT_w)) {
    375405                        found = 1;
    376                     else {
     406                    } else {
    377407                        char c = ' ';
    378408                        if (gl->matched_range.rm_so)
     
    383413                                found = 1;
    384414                        }
     415//BUG: "echo foop foo | grep -w foo" should match, but doesn't:
     416//we bail out on first "mismatch" because it's not a word.
    385417                    }
    386418                }
     
    564596static void load_regexes_from_file(llist_t *fopt)
    565597{
    566     char *line;
    567     FILE *f;
    568 
    569598    while (fopt) {
     599        char *line;
     600        FILE *fp;
    570601        llist_t *cur = fopt;
    571602        char *ffile = cur->data;
     
    573604        fopt = cur->link;
    574605        free(cur);
    575         f = xfopen_stdin(ffile);
    576         while ((line = xmalloc_fgetline(f)) != NULL) {
     606        fp = xfopen_stdin(ffile);
     607        while ((line = xmalloc_fgetline(fp)) != NULL) {
    577608            llist_add_to(&pattern_head,
    578609                new_grep_list_data(line, ALLOCATED));
    579610        }
     611        fclose_if_not_stdin(fp);
    580612    }
    581613}
     
    634666    if (opts & OPT_C) {
    635667        /* -C unsets prev -A and -B, but following -A or -B
    636            may override it */
     668         * may override it */
    637669        if (!(opts & OPT_A)) /* not overridden */
    638670            lines_after = Copt;
     
    661693    invert_search = ((option_mask32 & OPT_v) != 0); /* 0 | 1 */
    662694
    663     if (pattern_head != NULL) {
    664         /* convert char **argv to grep_list_data_t */
     695    {   /* convert char **argv to grep_list_data_t */
    665696        llist_t *cur;
    666 
    667697        for (cur = pattern_head; cur; cur = cur->link)
    668698            cur->data = new_grep_list_data(cur->data, 0);
    669699    }
    670     if (option_mask32 & OPT_f)
     700    if (option_mask32 & OPT_f) {
    671701        load_regexes_from_file(fopt);
     702        if (!pattern_head) { /* -f EMPTY_FILE? */
     703            /* GNU grep treats it as "nothing matches" */
     704            llist_add_to(&pattern_head, new_grep_list_data((char*) "", 0));
     705            invert_search ^= 1;
     706        }
     707    }
    672708
    673709    if (ENABLE_FEATURE_GREP_FGREP_ALIAS && applet_name[0] == 'f')
Note: See TracChangeset for help on using the changeset viewer.