strings: implement -t radix
[oweals/busybox.git] / findutils / grep.c
index b808ad92b2c50545a8e9b2893b74d02a390bfe67..1fa605f6f518ba4be5a76694746c8728af282d6a 100644 (file)
@@ -58,6 +58,7 @@
 //config:        Print the specified number of context lines (-C).
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include "xregex.h"
 
 
 //usage:#define fgrep_full_usage ""
 
 #define OPTSTR_GREP \
-       "lnqvscFiHhe:f:Lorm:wx" \
-       IF_FEATURE_GREP_CONTEXT("A:B:C:") \
+       "lnqvscFiHhe:*f:*Lorm:+wx" \
+       IF_FEATURE_GREP_CONTEXT("A:+B:+C:+") \
        IF_FEATURE_GREP_EGREP_ALIAS("E") \
        IF_EXTRA_COMPAT("z") \
        "aI"
@@ -201,11 +202,10 @@ struct globals {
        llist_t *pattern_head;   /* growable list of patterns to match */
        const char *cur_file;    /* the current file we are reading */
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { \
-       struct G_sizecheck { \
-               char G_sizecheck[sizeof(G) > COMMON_BUFSIZE ? -1 : 1]; \
-       }; \
+       setup_common_bufsiz(); \
+       BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
 } while (0)
 #define max_matches       (G.max_matches         )
 #if !ENABLE_EXTRA_COMPAT
@@ -247,7 +247,7 @@ typedef struct grep_list_data_t {
 #endif
 #define ALLOCATED 1
 #define COMPILED 2
-       int flg_mem_alocated_compiled;
+       int flg_mem_allocated_compiled;
 } grep_list_data_t;
 
 #if !ENABLE_EXTRA_COMPAT
@@ -373,10 +373,15 @@ static int grep_file(FILE *file)
  opt_f_not_found: ;
                                }
                        } else {
+#if ENABLE_EXTRA_COMPAT
+                               unsigned start_pos;
+#else
+                               int match_flg;
+#endif
                                char *match_at;
 
-                               if (!(gl->flg_mem_alocated_compiled & COMPILED)) {
-                                       gl->flg_mem_alocated_compiled |= COMPILED;
+                               if (!(gl->flg_mem_allocated_compiled & COMPILED)) {
+                                       gl->flg_mem_allocated_compiled |= COMPILED;
 #if !ENABLE_EXTRA_COMPAT
                                        xregcomp(&gl->compiled_regex, gl->pattern, reflags);
 #else
@@ -389,15 +394,19 @@ static int grep_file(FILE *file)
 #if !ENABLE_EXTRA_COMPAT
                                gl->matched_range.rm_so = 0;
                                gl->matched_range.rm_eo = 0;
+                               match_flg = 0;
+#else
+                               start_pos = 0;
 #endif
                                match_at = line;
  opt_w_again:
+//bb_error_msg("'%s' start_pos:%d line_len:%d", match_at, start_pos, line_len);
                                if (
 #if !ENABLE_EXTRA_COMPAT
-                                       regexec(&gl->compiled_regex, match_at, 1, &gl->matched_range, 0) == 0
+                                       regexec(&gl->compiled_regex, match_at, 1, &gl->matched_range, match_flg) == 0
 #else
                                        re_search(&gl->compiled_regex, match_at, line_len,
-                                                       /*start:*/ 0, /*range:*/ line_len,
+                                                       start_pos, /*range:*/ line_len,
                                                        &gl->matched_range) >= 0
 #endif
                                ) {
@@ -409,16 +418,34 @@ static int grep_file(FILE *file)
                                                found = 1;
                                        } else {
                                                char c = ' ';
-                                               if (gl->matched_range.rm_so)
+                                               if (match_at > line || gl->matched_range.rm_so != 0) {
                                                        c = match_at[gl->matched_range.rm_so - 1];
+                                               }
                                                if (!isalnum(c) && c != '_') {
                                                        c = match_at[gl->matched_range.rm_eo];
-                                                       if (!c || (!isalnum(c) && c != '_')) {
-                                                               found = 1;
-                                                       } else {
+                                               }
+                                               if (!isalnum(c) && c != '_') {
+                                                       found = 1;
+                                               } else {
+                       /*
+                        * Why check gl->matched_range.rm_eo?
+                        * Zero-length match makes -w skip the line:
+                        * "echo foo | grep ^" prints "foo",
+                        * "echo foo | grep -w ^" prints nothing.
+                        * Without such check, we can loop forever.
+                        */
+#if !ENABLE_EXTRA_COMPAT
+                                                       if (gl->matched_range.rm_eo != 0) {
                                                                match_at += gl->matched_range.rm_eo;
+                                                               match_flg |= REG_NOTBOL;
+                                                               goto opt_w_again;
+                                                       }
+#else
+                                                       if (gl->matched_range.rm_eo > start_pos) {
+                                                               start_pos = gl->matched_range.rm_eo;
                                                                goto opt_w_again;
                                                        }
+#endif
                                                }
                                        }
                                }
@@ -592,9 +619,9 @@ static char *add_grep_list_data(char *pattern)
        grep_list_data_t *gl = xzalloc(sizeof(*gl));
        gl->pattern = pattern;
 #if ENABLE_FEATURE_CLEAN_UP
-       gl->flg_mem_alocated_compiled = flg_used_mem;
+       gl->flg_mem_allocated_compiled = flg_used_mem;
 #else
-       /*gl->flg_mem_alocated_compiled = 0;*/
+       /*gl->flg_mem_allocated_compiled = 0;*/
 #endif
        return (char *)gl;
 }
@@ -641,7 +668,7 @@ static int grep_dir(const char *dir)
        int matched = 0;
        recursive_action(dir,
                /* recurse=yes */ ACTION_RECURSE |
-               /* followLinks=no */
+               /* followLinks=command line only */ ACTION_FOLLOWLINKS_L0 |
                /* depthFirst=yes */ ACTION_DEPTHFIRST,
                /* fileAction= */ file_action_grep,
                /* dirAction= */ NULL,
@@ -656,14 +683,19 @@ int grep_main(int argc UNUSED_PARAM, char **argv)
        FILE *file;
        int matched;
        llist_t *fopt = NULL;
-
-       /* do normal option parsing */
 #if ENABLE_FEATURE_GREP_CONTEXT
        int Copt, opts;
+#endif
+       INIT_G();
+
+       /* For grep, exitcode of 1 is "not found". Other errors are 2: */
+       xfunc_error_retval = 2;
 
+       /* do normal option parsing */
+#if ENABLE_FEATURE_GREP_CONTEXT
        /* -H unsets -h; -C unsets -A,-B; -e,-f are lists;
         * -m,-A,-B,-C have numeric param */
-       opt_complementary = "H-h:C-AB:e::f::m+:A+:B+:C+";
+       opt_complementary = "H-h:C-AB";
        opts = getopt32(argv,
                OPTSTR_GREP,
                &pattern_head, &fopt, &max_matches,
@@ -692,7 +724,7 @@ int grep_main(int argc UNUSED_PARAM, char **argv)
 #else
        /* with auto sanity checks */
        /* -H unsets -h; -c,-q or -l unset -n; -e,-f are lists; -m N */
-       opt_complementary = "H-h:c-n:q-n:l-n:e::f::m+";
+       opt_complementary = "H-h:c-n:q-n:l-n:";
        getopt32(argv, OPTSTR_GREP,
                &pattern_head, &fopt, &max_matches);
 #endif
@@ -716,7 +748,7 @@ int grep_main(int argc UNUSED_PARAM, char **argv)
                option_mask32 |= OPT_F;
 
 #if !ENABLE_EXTRA_COMPAT
-       if (!(option_mask32 & (OPT_o | OPT_w)))
+       if (!(option_mask32 & (OPT_o | OPT_w | OPT_x)))
                reflags = REG_NOSUB;
 #endif
 
@@ -805,9 +837,9 @@ int grep_main(int argc UNUSED_PARAM, char **argv)
                        grep_list_data_t *gl = (grep_list_data_t *)pattern_head_ptr->data;
 
                        pattern_head = pattern_head->link;
-                       if (gl->flg_mem_alocated_compiled & ALLOCATED)
+                       if (gl->flg_mem_allocated_compiled & ALLOCATED)
                                free(gl->pattern);
-                       if (gl->flg_mem_alocated_compiled & COMPILED)
+                       if (gl->flg_mem_allocated_compiled & COMPILED)
                                regfree(&gl->compiled_regex);
                        free(gl);
                        free(pattern_head_ptr);