grep.c 26 KB

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