//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"
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
#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
} 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
#if !ENABLE_EXTRA_COMPAT
gl->matched_range.rm_so = 0;
gl->matched_range.rm_eo = 0;
+ match_flg = 0;
#else
start_pos = 0;
#endif
//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_pos, /*range:*/ line_len,
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:
* 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;
- goto opt_w_again;
- }
+ 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
+ if (gl->matched_range.rm_eo > start_pos) {
+ start_pos = gl->matched_range.rm_eo;
+ goto opt_w_again;
}
+#endif
}
}
}
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;
}
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,
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,
#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
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);