3
0

grep.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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.5 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.6 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.6 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 non-inverted search, we can stop
  429. * at first match */
  430. if (found && !invert_search)
  431. goto do_found;
  432. pattern_ptr = pattern_ptr->link;
  433. } /* while (pattern_ptr) */
  434. if (found ^ invert_search) {
  435. do_found:
  436. /* keep track of matches */
  437. nmatches++;
  438. /* quiet/print (non)matching file names only? */
  439. if (option_mask32 & (OPT_q|OPT_l|OPT_L)) {
  440. free(line); /* we don't need line anymore */
  441. if (BE_QUIET) {
  442. /* manpage says about -q:
  443. * "exit immediately with zero status
  444. * if any match is found,
  445. * even if errors were detected" */
  446. exit(EXIT_SUCCESS);
  447. }
  448. /* if we're just printing filenames, we stop after the first match */
  449. if (PRINT_FILES_WITH_MATCHES) {
  450. puts(cur_file);
  451. /* fall through to "return 1" */
  452. }
  453. /* OPT_L aka PRINT_FILES_WITHOUT_MATCHES: return early */
  454. return 1; /* one match */
  455. }
  456. #if ENABLE_FEATURE_GREP_CONTEXT
  457. /* Were we printing context and saw next (unwanted) match? */
  458. if ((option_mask32 & OPT_m) && nmatches > max_matches)
  459. break;
  460. #endif
  461. /* print the matched line */
  462. if (PRINT_MATCH_COUNTS == 0) {
  463. #if ENABLE_FEATURE_GREP_CONTEXT
  464. int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1;
  465. /* if we were told to print 'before' lines and there is at least
  466. * one line in the circular buffer, print them */
  467. if (lines_before && before_buf[prevpos] != NULL) {
  468. int first_buf_entry_line_num = linenum - lines_before;
  469. /* advance to the first entry in the circular buffer, and
  470. * figure out the line number is of the first line in the
  471. * buffer */
  472. idx = curpos;
  473. while (before_buf[idx] == NULL) {
  474. idx = (idx + 1) % lines_before;
  475. first_buf_entry_line_num++;
  476. }
  477. /* now print each line in the buffer, clearing them as we go */
  478. while (before_buf[idx] != NULL) {
  479. print_line(before_buf[idx], before_buf_size[idx], first_buf_entry_line_num, '-');
  480. free(before_buf[idx]);
  481. before_buf[idx] = NULL;
  482. idx = (idx + 1) % lines_before;
  483. first_buf_entry_line_num++;
  484. }
  485. }
  486. /* make a note that we need to print 'after' lines */
  487. print_n_lines_after = lines_after;
  488. #endif
  489. if (option_mask32 & OPT_o) {
  490. if (FGREP_FLAG) {
  491. /* -Fo just prints the pattern
  492. * (unless -v: -Fov doesn't print anything at all) */
  493. if (found)
  494. print_line(gl->pattern, strlen(gl->pattern), linenum, ':');
  495. } else while (1) {
  496. unsigned start = gl->matched_range.rm_so;
  497. unsigned end = gl->matched_range.rm_eo;
  498. unsigned len = end - start;
  499. char old = line[end];
  500. line[end] = '\0';
  501. /* Empty match is not printed: try "echo test | grep -o ''" */
  502. if (len != 0)
  503. print_line(line + start, len, linenum, ':');
  504. if (old == '\0')
  505. break;
  506. line[end] = old;
  507. if (len == 0)
  508. end++;
  509. #if !ENABLE_EXTRA_COMPAT
  510. if (regexec(&gl->compiled_regex, line + end,
  511. 1, &gl->matched_range, REG_NOTBOL) != 0)
  512. break;
  513. gl->matched_range.rm_so += end;
  514. gl->matched_range.rm_eo += end;
  515. #else
  516. if (re_search(&gl->compiled_regex, line, line_len,
  517. end, line_len - end,
  518. &gl->matched_range) < 0)
  519. break;
  520. #endif
  521. }
  522. } else {
  523. print_line(line, line_len, linenum, ':');
  524. }
  525. }
  526. }
  527. #if ENABLE_FEATURE_GREP_CONTEXT
  528. else { /* no match */
  529. /* if we need to print some context lines after the last match, do so */
  530. if (print_n_lines_after) {
  531. print_line(line, strlen(line), linenum, '-');
  532. print_n_lines_after--;
  533. } else if (lines_before) {
  534. /* Add the line to the circular 'before' buffer */
  535. free(before_buf[curpos]);
  536. before_buf[curpos] = line;
  537. IF_EXTRA_COMPAT(before_buf_size[curpos] = line_len;)
  538. curpos = (curpos + 1) % lines_before;
  539. /* avoid free(line) - we took the line */
  540. line = NULL;
  541. }
  542. }
  543. #endif /* ENABLE_FEATURE_GREP_CONTEXT */
  544. #if !ENABLE_EXTRA_COMPAT
  545. free(line);
  546. #endif
  547. /* Did we print all context after last requested match? */
  548. if ((option_mask32 & OPT_m)
  549. && !print_n_lines_after
  550. && nmatches == max_matches
  551. ) {
  552. break;
  553. }
  554. } /* while (read line) */
  555. /* special-case file post-processing for options where we don't print line
  556. * matches, just filenames and possibly match counts */
  557. /* grep -c: print [filename:]count, even if count is zero */
  558. if (PRINT_MATCH_COUNTS) {
  559. if (print_filename)
  560. printf("%s:", cur_file);
  561. printf("%d\n", nmatches);
  562. }
  563. /* grep -L: print just the filename */
  564. if (PRINT_FILES_WITHOUT_MATCHES) {
  565. /* nmatches is zero, no need to check it:
  566. * we return 1 early if we detected a match
  567. * and PRINT_FILES_WITHOUT_MATCHES is set */
  568. puts(cur_file);
  569. }
  570. return nmatches;
  571. }
  572. #if ENABLE_FEATURE_CLEAN_UP
  573. #define new_grep_list_data(p, m) add_grep_list_data(p, m)
  574. static char *add_grep_list_data(char *pattern, int flg_used_mem)
  575. #else
  576. #define new_grep_list_data(p, m) add_grep_list_data(p)
  577. static char *add_grep_list_data(char *pattern)
  578. #endif
  579. {
  580. grep_list_data_t *gl = xzalloc(sizeof(*gl));
  581. gl->pattern = pattern;
  582. #if ENABLE_FEATURE_CLEAN_UP
  583. gl->flg_mem_allocated_compiled = flg_used_mem;
  584. #else
  585. /*gl->flg_mem_allocated_compiled = 0;*/
  586. #endif
  587. return (char *)gl;
  588. }
  589. static void load_regexes_from_file(llist_t *fopt)
  590. {
  591. while (fopt) {
  592. char *line;
  593. FILE *fp;
  594. llist_t *cur = fopt;
  595. char *ffile = cur->data;
  596. fopt = cur->link;
  597. free(cur);
  598. fp = xfopen_stdin(ffile);
  599. while ((line = xmalloc_fgetline(fp)) != NULL) {
  600. llist_add_to(&pattern_head,
  601. new_grep_list_data(line, ALLOCATED));
  602. }
  603. fclose_if_not_stdin(fp);
  604. }
  605. }
  606. static int FAST_FUNC file_action_grep(const char *filename,
  607. struct stat *statbuf,
  608. void* matched,
  609. int depth UNUSED_PARAM)
  610. {
  611. FILE *file;
  612. /* If we are given a link to a directory, we should bail out now, rather
  613. * than trying to open the "file" and hoping getline gives us nothing,
  614. * since that is not portable across operating systems (FreeBSD for
  615. * example will return the raw directory contents). */
  616. if (S_ISLNK(statbuf->st_mode)) {
  617. struct stat sb;
  618. if (stat(filename, &sb) != 0) {
  619. if (!SUPPRESS_ERR_MSGS)
  620. bb_simple_perror_msg(filename);
  621. return 0;
  622. }
  623. if (S_ISDIR(sb.st_mode))
  624. return 1;
  625. }
  626. file = fopen_for_read(filename);
  627. if (file == NULL) {
  628. if (!SUPPRESS_ERR_MSGS)
  629. bb_simple_perror_msg(filename);
  630. open_errors = 1;
  631. return 0;
  632. }
  633. cur_file = filename;
  634. *(int*)matched += grep_file(file);
  635. fclose(file);
  636. return 1;
  637. }
  638. static int grep_dir(const char *dir)
  639. {
  640. int matched = 0;
  641. recursive_action(dir,
  642. /* recurse=yes */ ACTION_RECURSE |
  643. /* followLinks=command line only */ ACTION_FOLLOWLINKS_L0 |
  644. /* depthFirst=yes */ ACTION_DEPTHFIRST,
  645. /* fileAction= */ file_action_grep,
  646. /* dirAction= */ NULL,
  647. /* userData= */ &matched,
  648. /* depth= */ 0);
  649. return matched;
  650. }
  651. int grep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  652. int grep_main(int argc UNUSED_PARAM, char **argv)
  653. {
  654. FILE *file;
  655. int matched;
  656. llist_t *fopt = NULL;
  657. #if ENABLE_FEATURE_GREP_CONTEXT
  658. int Copt, opts;
  659. #endif
  660. INIT_G();
  661. /* For grep, exitcode of 1 is "not found". Other errors are 2: */
  662. xfunc_error_retval = 2;
  663. /* do normal option parsing */
  664. #if ENABLE_FEATURE_GREP_CONTEXT
  665. /* -H unsets -h; -C unsets -A,-B */
  666. opts = getopt32(argv,
  667. "^" OPTSTR_GREP "\0" "H-h:C-AB",
  668. &pattern_head, &fopt, &max_matches,
  669. &lines_after, &lines_before, &Copt);
  670. if (opts & OPT_C) {
  671. /* -C unsets prev -A and -B, but following -A or -B
  672. * may override it */
  673. if (!(opts & OPT_A)) /* not overridden */
  674. lines_after = Copt;
  675. if (!(opts & OPT_B)) /* not overridden */
  676. lines_before = Copt;
  677. }
  678. /* sanity checks */
  679. if (opts & (OPT_c|OPT_q|OPT_l|OPT_L)) {
  680. option_mask32 &= ~OPT_n;
  681. lines_before = 0;
  682. lines_after = 0;
  683. } else if (lines_before > 0) {
  684. if (lines_before > INT_MAX / sizeof(long long))
  685. lines_before = INT_MAX / sizeof(long long);
  686. /* overflow in (lines_before * sizeof(x)) is prevented (above) */
  687. before_buf = xzalloc(lines_before * sizeof(before_buf[0]));
  688. IF_EXTRA_COMPAT(before_buf_size = xzalloc(lines_before * sizeof(before_buf_size[0]));)
  689. }
  690. #else
  691. /* with auto sanity checks */
  692. getopt32(argv, "^" OPTSTR_GREP "\0" "H-h:c-n:q-n:l-n:", // why trailing ":"?
  693. &pattern_head, &fopt, &max_matches);
  694. #endif
  695. invert_search = ((option_mask32 & OPT_v) != 0); /* 0 | 1 */
  696. { /* convert char **argv to grep_list_data_t */
  697. llist_t *cur;
  698. for (cur = pattern_head; cur; cur = cur->link)
  699. cur->data = new_grep_list_data(cur->data, 0);
  700. }
  701. if (option_mask32 & OPT_f) {
  702. load_regexes_from_file(fopt);
  703. if (!pattern_head) { /* -f EMPTY_FILE? */
  704. /* GNU grep treats it as "nothing matches" */
  705. llist_add_to(&pattern_head, new_grep_list_data((char*) "", 0));
  706. invert_search ^= 1;
  707. }
  708. }
  709. if (ENABLE_FGREP && applet_name[0] == 'f')
  710. option_mask32 |= OPT_F;
  711. #if !ENABLE_EXTRA_COMPAT
  712. if (!(option_mask32 & (OPT_o | OPT_w | OPT_x)))
  713. reflags = REG_NOSUB;
  714. #endif
  715. if ((ENABLE_EGREP && applet_name[0] == 'e')
  716. || (option_mask32 & OPT_E)
  717. ) {
  718. reflags |= REG_EXTENDED;
  719. }
  720. #if ENABLE_EXTRA_COMPAT
  721. else {
  722. reflags = RE_SYNTAX_GREP;
  723. }
  724. #endif
  725. if (option_mask32 & OPT_i) {
  726. #if !ENABLE_EXTRA_COMPAT
  727. reflags |= REG_ICASE;
  728. #else
  729. int i;
  730. case_fold = xmalloc(256);
  731. for (i = 0; i < 256; i++)
  732. case_fold[i] = (unsigned char)i;
  733. for (i = 'a'; i <= 'z'; i++)
  734. case_fold[i] = (unsigned char)(i - ('a' - 'A'));
  735. #endif
  736. }
  737. argv += optind;
  738. /* if we didn't get a pattern from -e and no command file was specified,
  739. * first parameter should be the pattern. no pattern, no worky */
  740. if (pattern_head == NULL) {
  741. char *pattern;
  742. if (*argv == NULL)
  743. bb_show_usage();
  744. pattern = new_grep_list_data(*argv++, 0);
  745. llist_add_to(&pattern_head, pattern);
  746. }
  747. /* argv[0..(argc-1)] should be names of file to grep through. If
  748. * there is more than one file to grep, we will print the filenames. */
  749. if (argv[0] && argv[1])
  750. print_filename = 1;
  751. /* -H / -h of course override */
  752. if (option_mask32 & OPT_H)
  753. print_filename = 1;
  754. if (option_mask32 & OPT_h)
  755. print_filename = 0;
  756. /* If no files were specified, or '-' was specified, take input from
  757. * stdin. Otherwise, we grep through all the files specified. */
  758. matched = 0;
  759. do {
  760. cur_file = *argv;
  761. file = stdin;
  762. if (!cur_file || LONE_DASH(cur_file)) {
  763. cur_file = "(standard input)";
  764. } else {
  765. if (option_mask32 & OPT_r) {
  766. struct stat st;
  767. if (stat(cur_file, &st) == 0 && S_ISDIR(st.st_mode)) {
  768. if (!(option_mask32 & OPT_h))
  769. print_filename = 1;
  770. matched += grep_dir(cur_file);
  771. goto grep_done;
  772. }
  773. }
  774. /* else: fopen(dir) will succeed, but reading won't */
  775. file = fopen_for_read(cur_file);
  776. if (file == NULL) {
  777. if (!SUPPRESS_ERR_MSGS)
  778. bb_simple_perror_msg(cur_file);
  779. open_errors = 1;
  780. continue;
  781. }
  782. }
  783. matched += grep_file(file);
  784. fclose_if_not_stdin(file);
  785. grep_done: ;
  786. } while (*argv && *++argv);
  787. /* destroy all the elements in the pattern list */
  788. if (ENABLE_FEATURE_CLEAN_UP) {
  789. while (pattern_head) {
  790. llist_t *pattern_head_ptr = pattern_head;
  791. grep_list_data_t *gl = (grep_list_data_t *)pattern_head_ptr->data;
  792. pattern_head = pattern_head->link;
  793. if (gl->flg_mem_allocated_compiled & ALLOCATED)
  794. free(gl->pattern);
  795. if (gl->flg_mem_allocated_compiled & COMPILED)
  796. regfree(&gl->compiled_regex);
  797. free(gl);
  798. free(pattern_head_ptr);
  799. }
  800. }
  801. /* 0 = success, 1 = failed, 2 = error */
  802. if (open_errors)
  803. return 2;
  804. return !matched; /* invert return value: 0 = success, 1 = failed */
  805. }