grep.c 25 KB

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