grep.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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: "[-HhnlLoqvsriwFE"
  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 -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 Input is NUL terminated"
  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:*Lorm:+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_m, /* -m MAX_MATCHES */
  129. OPTBIT_w, /* -w whole word match */
  130. OPTBIT_x, /* -x whole line match */
  131. IF_FEATURE_GREP_CONTEXT( OPTBIT_A ,) /* -A NUM: after-match context */
  132. IF_FEATURE_GREP_CONTEXT( OPTBIT_B ,) /* -B NUM: before-match context */
  133. IF_FEATURE_GREP_CONTEXT( OPTBIT_C ,) /* -C NUM: -A and -B combined */
  134. OPTBIT_E, /* extended regexp */
  135. IF_EXTRA_COMPAT( OPTBIT_z ,) /* input is NUL terminated */
  136. OPT_l = 1 << OPTBIT_l,
  137. OPT_n = 1 << OPTBIT_n,
  138. OPT_q = 1 << OPTBIT_q,
  139. OPT_v = 1 << OPTBIT_v,
  140. OPT_s = 1 << OPTBIT_s,
  141. OPT_c = 1 << OPTBIT_c,
  142. OPT_F = 1 << OPTBIT_F,
  143. OPT_i = 1 << OPTBIT_i,
  144. OPT_H = 1 << OPTBIT_H,
  145. OPT_h = 1 << OPTBIT_h,
  146. OPT_e = 1 << OPTBIT_e,
  147. OPT_f = 1 << OPTBIT_f,
  148. OPT_L = 1 << OPTBIT_L,
  149. OPT_o = 1 << OPTBIT_o,
  150. OPT_r = 1 << OPTBIT_r,
  151. OPT_m = 1 << OPTBIT_m,
  152. OPT_w = 1 << OPTBIT_w,
  153. OPT_x = 1 << OPTBIT_x,
  154. OPT_A = IF_FEATURE_GREP_CONTEXT( (1 << OPTBIT_A)) + 0,
  155. OPT_B = IF_FEATURE_GREP_CONTEXT( (1 << OPTBIT_B)) + 0,
  156. OPT_C = IF_FEATURE_GREP_CONTEXT( (1 << OPTBIT_C)) + 0,
  157. OPT_E = 1 << OPTBIT_E,
  158. OPT_z = IF_EXTRA_COMPAT( (1 << OPTBIT_z)) + 0,
  159. };
  160. #define PRINT_FILES_WITH_MATCHES (option_mask32 & OPT_l)
  161. #define PRINT_LINE_NUM (option_mask32 & OPT_n)
  162. #define BE_QUIET (option_mask32 & OPT_q)
  163. #define SUPPRESS_ERR_MSGS (option_mask32 & OPT_s)
  164. #define PRINT_MATCH_COUNTS (option_mask32 & OPT_c)
  165. #define FGREP_FLAG (option_mask32 & OPT_F)
  166. #define PRINT_FILES_WITHOUT_MATCHES (option_mask32 & OPT_L)
  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(EXIT_SUCCESS);
  455. }
  456. /* if we're just printing filenames, we stop after the first match */
  457. if (PRINT_FILES_WITH_MATCHES) {
  458. puts(cur_file);
  459. /* fall through to "return 1" */
  460. }
  461. /* OPT_L aka PRINT_FILES_WITHOUT_MATCHES: return early */
  462. return 1; /* one 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 just the filename */
  573. if (PRINT_FILES_WITHOUT_MATCHES) {
  574. /* nmatches is zero, no need to check it:
  575. * we return 1 early if we detected a match
  576. * and PRINT_FILES_WITHOUT_MATCHES is set */
  577. puts(cur_file);
  578. }
  579. return nmatches;
  580. }
  581. #if ENABLE_FEATURE_CLEAN_UP
  582. #define new_grep_list_data(p, m) add_grep_list_data(p, m)
  583. static char *add_grep_list_data(char *pattern, int flg_used_mem)
  584. #else
  585. #define new_grep_list_data(p, m) add_grep_list_data(p)
  586. static char *add_grep_list_data(char *pattern)
  587. #endif
  588. {
  589. grep_list_data_t *gl = xzalloc(sizeof(*gl));
  590. gl->pattern = pattern;
  591. #if ENABLE_FEATURE_CLEAN_UP
  592. gl->flg_mem_allocated_compiled = flg_used_mem;
  593. #else
  594. /*gl->flg_mem_allocated_compiled = 0;*/
  595. #endif
  596. return (char *)gl;
  597. }
  598. static void load_regexes_from_file(llist_t *fopt)
  599. {
  600. while (fopt) {
  601. char *line;
  602. FILE *fp;
  603. llist_t *cur = fopt;
  604. char *ffile = cur->data;
  605. fopt = cur->link;
  606. free(cur);
  607. fp = xfopen_stdin(ffile);
  608. while ((line = xmalloc_fgetline(fp)) != NULL) {
  609. llist_add_to(&pattern_head,
  610. new_grep_list_data(line, ALLOCATED));
  611. }
  612. fclose_if_not_stdin(fp);
  613. }
  614. }
  615. static int FAST_FUNC file_action_grep(const char *filename,
  616. struct stat *statbuf,
  617. void* matched,
  618. int depth UNUSED_PARAM)
  619. {
  620. FILE *file;
  621. /* If we are given a link to a directory, we should bail out now, rather
  622. * than trying to open the "file" and hoping getline gives us nothing,
  623. * since that is not portable across operating systems (FreeBSD for
  624. * example will return the raw directory contents). */
  625. if (S_ISLNK(statbuf->st_mode)) {
  626. struct stat sb;
  627. if (stat(filename, &sb) != 0) {
  628. if (!SUPPRESS_ERR_MSGS)
  629. bb_simple_perror_msg(filename);
  630. return 0;
  631. }
  632. if (S_ISDIR(sb.st_mode))
  633. return 1;
  634. }
  635. file = fopen_for_read(filename);
  636. if (file == NULL) {
  637. if (!SUPPRESS_ERR_MSGS)
  638. bb_simple_perror_msg(filename);
  639. open_errors = 1;
  640. return 0;
  641. }
  642. cur_file = filename;
  643. *(int*)matched += grep_file(file);
  644. fclose(file);
  645. return 1;
  646. }
  647. static int grep_dir(const char *dir)
  648. {
  649. int matched = 0;
  650. recursive_action(dir,
  651. /* recurse=yes */ ACTION_RECURSE |
  652. /* followLinks=command line only */ ACTION_FOLLOWLINKS_L0 |
  653. /* depthFirst=yes */ ACTION_DEPTHFIRST,
  654. /* fileAction= */ file_action_grep,
  655. /* dirAction= */ NULL,
  656. /* userData= */ &matched,
  657. /* depth= */ 0);
  658. return matched;
  659. }
  660. int grep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  661. int grep_main(int argc UNUSED_PARAM, char **argv)
  662. {
  663. FILE *file;
  664. int matched;
  665. llist_t *fopt = NULL;
  666. #if ENABLE_FEATURE_GREP_CONTEXT
  667. int Copt, opts;
  668. #endif
  669. INIT_G();
  670. /* For grep, exitcode of 1 is "not found". Other errors are 2: */
  671. xfunc_error_retval = 2;
  672. /* do normal option parsing */
  673. #if ENABLE_FEATURE_GREP_CONTEXT
  674. /* -H unsets -h; -C unsets -A,-B */
  675. opts = getopt32long(argv, "^"
  676. OPTSTR_GREP
  677. "\0"
  678. "H-h:C-AB",
  679. "color\0" Optional_argument "\xff",
  680. &pattern_head, &fopt, &max_matches,
  681. &lines_after, &lines_before, &Copt
  682. , NULL
  683. );
  684. if (opts & OPT_C) {
  685. /* -C unsets prev -A and -B, but following -A or -B
  686. * may override it */
  687. if (!(opts & OPT_A)) /* not overridden */
  688. lines_after = Copt;
  689. if (!(opts & OPT_B)) /* not overridden */
  690. lines_before = Copt;
  691. }
  692. /* sanity checks */
  693. if (opts & (OPT_c|OPT_q|OPT_l|OPT_L)) {
  694. option_mask32 &= ~OPT_n;
  695. lines_before = 0;
  696. lines_after = 0;
  697. } else if (lines_before > 0) {
  698. if (lines_before > INT_MAX / sizeof(long long))
  699. lines_before = INT_MAX / sizeof(long long);
  700. /* overflow in (lines_before * sizeof(x)) is prevented (above) */
  701. before_buf = xzalloc(lines_before * sizeof(before_buf[0]));
  702. IF_EXTRA_COMPAT(before_buf_size = xzalloc(lines_before * sizeof(before_buf_size[0]));)
  703. }
  704. #else
  705. /* with auto sanity checks */
  706. getopt32(argv, "^" OPTSTR_GREP "\0" "H-h:c-n:q-n:l-n:", // why trailing ":"?
  707. &pattern_head, &fopt, &max_matches);
  708. #endif
  709. invert_search = ((option_mask32 & OPT_v) != 0); /* 0 | 1 */
  710. { /* convert char **argv to grep_list_data_t */
  711. llist_t *cur;
  712. for (cur = pattern_head; cur; cur = cur->link)
  713. cur->data = new_grep_list_data(cur->data, 0);
  714. }
  715. if (option_mask32 & OPT_f) {
  716. load_regexes_from_file(fopt);
  717. if (!pattern_head) { /* -f EMPTY_FILE? */
  718. /* GNU grep treats it as "nothing matches" */
  719. llist_add_to(&pattern_head, new_grep_list_data((char*) "", 0));
  720. invert_search ^= 1;
  721. }
  722. }
  723. if (ENABLE_FGREP && applet_name[0] == 'f')
  724. option_mask32 |= OPT_F;
  725. #if !ENABLE_EXTRA_COMPAT
  726. if (!(option_mask32 & (OPT_o | OPT_w | OPT_x)))
  727. reflags = REG_NOSUB;
  728. #endif
  729. if ((ENABLE_EGREP && applet_name[0] == 'e')
  730. || (option_mask32 & OPT_E)
  731. ) {
  732. reflags |= REG_EXTENDED;
  733. }
  734. #if ENABLE_EXTRA_COMPAT
  735. else {
  736. reflags = RE_SYNTAX_GREP;
  737. }
  738. #endif
  739. if (option_mask32 & OPT_i) {
  740. #if !ENABLE_EXTRA_COMPAT
  741. reflags |= REG_ICASE;
  742. #else
  743. int i;
  744. case_fold = xmalloc(256);
  745. for (i = 0; i < 256; i++)
  746. case_fold[i] = (unsigned char)i;
  747. for (i = 'a'; i <= 'z'; i++)
  748. case_fold[i] = (unsigned char)(i - ('a' - 'A'));
  749. #endif
  750. }
  751. argv += optind;
  752. /* if we didn't get a pattern from -e and no command file was specified,
  753. * first parameter should be the pattern. no pattern, no worky */
  754. if (pattern_head == NULL) {
  755. char *pattern;
  756. if (*argv == NULL)
  757. bb_show_usage();
  758. pattern = new_grep_list_data(*argv++, 0);
  759. llist_add_to(&pattern_head, pattern);
  760. }
  761. /* argv[0..(argc-1)] should be names of file to grep through. If
  762. * there is more than one file to grep, we will print the filenames. */
  763. if (argv[0] && argv[1])
  764. print_filename = 1;
  765. /* -H / -h of course override */
  766. if (option_mask32 & OPT_H)
  767. print_filename = 1;
  768. if (option_mask32 & OPT_h)
  769. print_filename = 0;
  770. /* If no files were specified, or '-' was specified, take input from
  771. * stdin. Otherwise, we grep through all the files specified. */
  772. matched = 0;
  773. do {
  774. cur_file = *argv;
  775. file = stdin;
  776. if (!cur_file || LONE_DASH(cur_file)) {
  777. cur_file = "(standard input)";
  778. } else {
  779. if (option_mask32 & OPT_r) {
  780. struct stat st;
  781. if (stat(cur_file, &st) == 0 && S_ISDIR(st.st_mode)) {
  782. if (!(option_mask32 & OPT_h))
  783. print_filename = 1;
  784. matched += grep_dir(cur_file);
  785. goto grep_done;
  786. }
  787. }
  788. /* else: fopen(dir) will succeed, but reading won't */
  789. file = fopen_for_read(cur_file);
  790. if (file == NULL) {
  791. if (!SUPPRESS_ERR_MSGS)
  792. bb_simple_perror_msg(cur_file);
  793. open_errors = 1;
  794. continue;
  795. }
  796. }
  797. matched += grep_file(file);
  798. fclose_if_not_stdin(file);
  799. grep_done: ;
  800. } while (*argv && *++argv);
  801. /* destroy all the elements in the pattern list */
  802. if (ENABLE_FEATURE_CLEAN_UP) {
  803. while (pattern_head) {
  804. llist_t *pattern_head_ptr = pattern_head;
  805. grep_list_data_t *gl = (grep_list_data_t *)pattern_head_ptr->data;
  806. pattern_head = pattern_head->link;
  807. if (gl->flg_mem_allocated_compiled & ALLOCATED)
  808. free(gl->pattern);
  809. if (gl->flg_mem_allocated_compiled & COMPILED)
  810. regfree(&gl->compiled_regex);
  811. free(gl);
  812. free(pattern_head_ptr);
  813. }
  814. }
  815. /* 0 = success, 1 = failed, 2 = error */
  816. if (open_errors)
  817. return 2;
  818. return !matched; /* invert return value: 0 = success, 1 = failed */
  819. }