grep.c 26 KB

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