static void do_require_eq_tests(const struct require_eq_test *tests) { const struct require_eq_test *t; for (t = &tests[0]; t->head != NULL; t++) { printf("Checking with %s, %s and expecting %s\n", t->v1, t->v2, t->ok ? "true" : "false"); init_and_run_h_tc("h_require", t->head, t->body); ATF_REQUIRE(exists("before")); if (t->ok) { ATF_REQUIRE(grep_file("result", "^passed")); ATF_REQUIRE(exists("after")); } else { ATF_REQUIRE(grep_file("result", "^failed: .*macros_test.c" ":[0-9]+: %s$", t->msg)); ATF_REQUIRE(!exists("after")); } ATF_REQUIRE(unlink("before") != -1); if (t->ok) ATF_REQUIRE(unlink("after") != -1); } }
static void check_file (const enum out_type type) { switch (type) { case stdout_type: ATF_CHECK (grep_file ("stdout", "stdout: msg")); ATF_CHECK (!grep_file ("stdout", "stderr: msg")); break; case stderr_type: ATF_CHECK (grep_file ("stderr", "stderr: msg")); ATF_CHECK (!grep_file ("stderr", "stdout: msg")); break; default: UNREACHABLE; } }
static int grep_cache(struct grep_opt *opt, struct repository *repo, const struct pathspec *pathspec, int cached) { int hit = 0; int nr; struct strbuf name = STRBUF_INIT; int name_base_len = 0; if (repo->submodule_prefix) { name_base_len = strlen(repo->submodule_prefix); strbuf_addstr(&name, repo->submodule_prefix); } if (repo_read_index(repo) < 0) die(_("index file corrupt")); for (nr = 0; nr < repo->index->cache_nr; nr++) { const struct cache_entry *ce = repo->index->cache[nr]; strbuf_setlen(&name, name_base_len); strbuf_addstr(&name, ce->name); if (S_ISREG(ce->ce_mode) && match_pathspec(repo->index, pathspec, name.buf, name.len, 0, NULL, S_ISDIR(ce->ce_mode) || S_ISGITLINK(ce->ce_mode))) { /* * If CE_VALID is on, we assume worktree file and its * cache entry are identical, even if worktree file has * been modified, so use cache version instead */ if (cached || (ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) { if (ce_stage(ce) || ce_intent_to_add(ce)) continue; hit |= grep_oid(opt, &ce->oid, name.buf, 0, name.buf); } else { hit |= grep_file(opt, name.buf); } } else if (recurse_submodules && S_ISGITLINK(ce->ce_mode) && submodule_path_match(repo->index, pathspec, name.buf, NULL)) { hit |= grep_submodule(opt, repo, pathspec, NULL, ce->name, ce->name); } else { continue; } if (ce_stage(ce)) { do { nr++; } while (nr < repo->index->cache_nr && !strcmp(ce->name, repo->index->cache[nr]->name)); nr--; /* compensate for loop control */ } if (hit && opt->status_only) break; } strbuf_release(&name); return hit; }
ATF_TC_BODY(require, tc) { struct test { void (*head)(atf_tc_t *); void (*body)(const atf_tc_t *); bool value; const char *msg; bool ok; } *t, tests[] = { { H_REQUIRE_HEAD_NAME(0), H_REQUIRE_BODY_NAME(0), 0, "0 not met", false }, { H_REQUIRE_HEAD_NAME(1), H_REQUIRE_BODY_NAME(1), 1, "1 not met", true }, { H_REQUIRE_MSG_HEAD_NAME(0), H_REQUIRE_MSG_BODY_NAME(0), 0, "expected a false value", false }, { H_REQUIRE_MSG_HEAD_NAME(1), H_REQUIRE_MSG_BODY_NAME(1), 1, "expected a true value", true }, { NULL, NULL, false, NULL, false } }; for (t = &tests[0]; t->head != NULL; t++) { printf("Checking with a %d value\n", t->value); init_and_run_h_tc("h_require", t->head, t->body); ATF_REQUIRE(exists("before")); if (t->ok) { ATF_REQUIRE(grep_file("result", "^passed")); ATF_REQUIRE(exists("after")); } else { ATF_REQUIRE(grep_file("result", "^failed: .*macros_test.c:[0-9]+: " "%s$", t->msg)); ATF_REQUIRE(!exists("after")); } ATF_REQUIRE(unlink("before") != -1); if (t->ok) ATF_REQUIRE(unlink("after") != -1); } }
ATF_TC_BODY(require_errno, tc) { struct test { void (*head)(atf_tc_t *); void (*body)(const atf_tc_t *); bool ok; const char *exp_regex; } *t, tests[] = { { H_REQUIRE_ERRNO_HEAD_NAME(no_error), H_REQUIRE_ERRNO_BODY_NAME(no_error), false, "Expected true value in errno_ok_stub\\(\\) == -1" }, { H_REQUIRE_ERRNO_HEAD_NAME(errno_ok), H_REQUIRE_ERRNO_BODY_NAME(errno_ok), true, NULL }, { H_REQUIRE_ERRNO_HEAD_NAME(errno_fail), H_REQUIRE_ERRNO_BODY_NAME(errno_fail), false, "Expected errno 3, got 4, in errno_fail_stub\\(4\\) == -1" }, { NULL, NULL, false, NULL } }; for (t = &tests[0]; t->head != NULL; t++) { init_and_run_h_tc("h_require_errno", t->head, t->body); ATF_REQUIRE(exists("before")); if (t->ok) { ATF_REQUIRE(grep_file("result", "^passed")); ATF_REQUIRE(exists("after")); } else { ATF_REQUIRE(grep_file("result", "^failed: .*macros_test.c:[0-9]+: " "%s$", t->exp_regex)); ATF_REQUIRE(!exists("after")); } ATF_REQUIRE(unlink("before") != -1); if (t->ok) ATF_REQUIRE(unlink("after") != -1); } }
static int file_action_grep(const char *filename, struct stat *statbuf, void* matched, int depth) { FILE *file = fopen(filename, "r"); if (file == NULL) { if (!SUPPRESS_ERR_MSGS) bb_perror_msg("%s", cur_file); open_errors = 1; return 0; } cur_file = filename; *(int*)matched += grep_file(file); fclose(file); return 1; }
ATF_TC_BODY(msg_embedded_fmt, tc) { struct test { void (*head)(atf_tc_t *); void (*body)(const atf_tc_t *); bool fatal; const char *msg; } *t, tests[] = { { H_CHECK_HEAD_NAME(msg), H_CHECK_BODY_NAME(msg), false, "aux_bool\\(\"%d\"\\) not met" }, { H_REQUIRE_HEAD_NAME(msg), H_REQUIRE_BODY_NAME(msg), true, "aux_bool\\(\"%d\"\\) not met" }, { H_CHECK_STREQ_HEAD_NAME(msg), H_CHECK_STREQ_BODY_NAME(msg), false, "aux_str\\(\"%d\"\\) != \"\" \\(foo != \\)" }, { H_REQUIRE_STREQ_HEAD_NAME(msg), H_REQUIRE_STREQ_BODY_NAME(msg), true, "aux_str\\(\"%d\"\\) != \"\" \\(foo != \\)" }, { NULL, NULL, false, NULL } }; for (t = &tests[0]; t->head != NULL; t++) { printf("Checking with an expected '%s' message\n", t->msg); init_and_run_h_tc("h_check", t->head, t->body); if (t->fatal) { bool matched = grep_file("result", "^failed: .*macros_test.c:[0-9]+: " "%s$", t->msg); ATF_CHECK_MSG(matched, "couldn't find error string in result"); } else { bool matched = grep_file("error", "Check failed: .*" "macros_test.c:[0-9]+: %s$", t->msg); ATF_CHECK_MSG(matched, "couldn't find error string in output"); } } }
static int grep_cache(struct grep_opt *opt, const char **paths, int cached) { int hit = 0; int nr; read_cache(); #if !NO_EXTERNAL_GREP /* * Use the external "grep" command for the case where * we grep through the checked-out files. It tends to * be a lot more optimized */ if (!cached && !builtin_grep) { hit = external_grep(opt, paths, cached); if (hit >= 0) return hit; } #endif for (nr = 0; nr < active_nr; nr++) { struct cache_entry *ce = active_cache[nr]; if (!S_ISREG(ce->ce_mode)) continue; if (!pathspec_matches(paths, ce->name)) continue; /* * If CE_VALID is on, we assume worktree file and its cache entry * are identical, even if worktree file has been modified, so use * cache version instead */ if (cached || (ce->ce_flags & CE_VALID)) { if (ce_stage(ce)) continue; hit |= grep_sha1(opt, ce->sha1, ce->name, 0); } else hit |= grep_file(opt, ce->name); if (ce_stage(ce)) { do { nr++; } while (nr < active_nr && !strcmp(ce->name, active_cache[nr]->name)); nr--; /* compensate for loop control */ } } free_grep_patterns(opt); return hit; }
static int grep_directory(struct grep_opt *opt, const char **paths) { struct dir_struct dir; int i, hit = 0; memset(&dir, 0, sizeof(dir)); setup_standard_excludes(&dir); fill_directory(&dir, paths); for (i = 0; i < dir.nr; i++) { hit |= grep_file(opt, dir.entries[i]->name); if (hit && opt->status_only) break; } return hit; }
ATF_TC_BODY(build_cpp, tc) { init_and_run_h_tc(&ATF_TC_NAME(h_build_cpp_ok), &ATF_TC_PACK_NAME(h_build_cpp_ok), "stdout", "stderr"); ATF_CHECK(grep_file("stdout", "-o.*test.p")); ATF_CHECK(grep_file("stdout", "test.c")); ATF_CHECK(grep_file("test.p", "foo bar")); init_and_run_h_tc(&ATF_TC_NAME(h_build_cpp_fail), &ATF_TC_PACK_NAME(h_build_cpp_fail), "stdout", "stderr"); ATF_CHECK(grep_file("stdout", "-o test.p")); ATF_CHECK(grep_file("stdout", "test.c")); ATF_CHECK(grep_file("stderr", "test.c")); ATF_CHECK(grep_file("stderr", "non-existent.h")); }
ATF_TC_BODY(build_cxx_o, tc) { init_and_run_h_tc(&ATF_TC_NAME(h_build_cxx_o_ok), &ATF_TC_PACK_NAME(h_build_cxx_o_ok), "stdout", "stderr"); ATF_CHECK(grep_file("stdout", "-o test.o")); ATF_CHECK(grep_file("stdout", "-c test.cpp")); init_and_run_h_tc(&ATF_TC_NAME(h_build_cxx_o_fail), &ATF_TC_PACK_NAME(h_build_cxx_o_fail), "stdout", "stderr"); ATF_CHECK(grep_file("stdout", "-o test.o")); ATF_CHECK(grep_file("stdout", "-c test.cpp")); ATF_CHECK(grep_file("stderr", "test.cpp")); ATF_CHECK(grep_file("stderr", "UNDEFINED_SYMBOL")); }
static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec, int exc_std) { struct dir_struct dir; int i, hit = 0; memset(&dir, 0, sizeof(dir)); if (exc_std) setup_standard_excludes(&dir); fill_directory(&dir, pathspec); for (i = 0; i < dir.nr; i++) { if (!dir_path_match(dir.entries[i], pathspec, 0, NULL)) continue; hit |= grep_file(opt, dir.entries[i]->name); if (hit && opt->status_only) break; } return hit; }
static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec) { struct dir_struct dir; int i, hit = 0; memset(&dir, 0, sizeof(dir)); setup_standard_excludes(&dir); fill_directory(&dir, pathspec->raw); for (i = 0; i < dir.nr; i++) { const char *name = dir.entries[i]->name; int namelen = strlen(name); if (!match_pathspec_depth(pathspec, name, namelen, 0, NULL)) continue; hit |= grep_file(opt, dir.entries[i]->name); if (hit && opt->status_only) break; } return hit; }
int main (int argc, const char *argv[]) { Filesystem::convert_native_arguments (argc, argv); ArgParse ap; ap.options ("igrep -- search images for matching metadata\n" OIIO_INTRO_STRING "\n" "Usage: igrep [options] pattern filename...", "%*", parse_files, "", "-i", &ignore_case, "Ignore upper/lower case distinctions", "-v", &invert_match, "Invert match (select non-matching files)", "-E", &extended_regex, "Pattern is an extended regular expression", "-f", &file_match, "Match against file name as well as metadata", "-l", &list_files, "List the matching files (no detail)", "-r", &recursive, "Recurse into directories", "-d", &print_dirs, "Print directories (when recursive)", "-a", &all_subimages, "Search all subimages of each file", "--help", &help, "Print help message", NULL); if (ap.parse(argc, argv) < 0 || pattern.empty() || filenames.empty()) { std::cerr << ap.geterror() << std::endl; ap.usage (); return EXIT_FAILURE; } if (help) { ap.usage (); exit (EXIT_FAILURE); } boost::regex_constants::syntax_option_type flag = boost::regex_constants::grep; if (extended_regex) flag = boost::regex::extended; if (ignore_case) flag |= boost::regex_constants::icase; boost::regex re (pattern, flag); for (auto&& s : filenames) { grep_file (s, re); } return 0; }
ATF_TC_BODY(grep_file, tc) { FILE *f; f = fopen("test.txt", "w"); ATF_CHECK(f != NULL); fprintf(f, "line1\n"); fprintf(f, "the second line\n"); fprintf(f, "aaaabbbb\n"); fclose(f); ATF_CHECK(grep_file("test.txt", "line1")); ATF_CHECK(grep_file("test.txt", "line%d", 1)); ATF_CHECK(grep_file("test.txt", "second line")); ATF_CHECK(grep_file("test.txt", "aa.*bb")); ATF_CHECK(!grep_file("test.txt", "foo")); ATF_CHECK(!grep_file("test.txt", "bar")); ATF_CHECK(!grep_file("test.txt", "aaaaa")); }
static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int cached) { int hit = 0; int nr; read_cache(); for (nr = 0; nr < active_nr; nr++) { struct cache_entry *ce = active_cache[nr]; if (!S_ISREG(ce->ce_mode)) continue; if (!match_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, NULL)) continue; if (skip_binary(opt, ce->name)) continue; /* * If CE_VALID is on, we assume worktree file and its cache entry * are identical, even if worktree file has been modified, so use * cache version instead */ if (cached || (ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) { if (ce_stage(ce)) continue; hit |= grep_sha1(opt, ce->sha1, ce->name, 0, ce->name); } else hit |= grep_file(opt, ce->name); if (ce_stage(ce)) { do { nr++; } while (nr < active_nr && !strcmp(ce->name, active_cache[nr]->name)); nr--; /* compensate for loop control */ } if (hit && opt->status_only) break; } return hit; }
int grep_main(int argc, char **argv) { FILE *file; int matched; llist_t *fopt = NULL; /* do normal option parsing */ #if ENABLE_FEATURE_GREP_CONTEXT char *slines_after; char *slines_before; char *Copt; opt_complementary = "H-h:e::f::C-AB"; getopt32(argc, argv, GREP_OPTS GREP_OPT_CONTEXT OPT_EGREP, &pattern_head, &fopt, &slines_after, &slines_before, &Copt); if (option_mask32 & GREP_OPT_C) { /* -C unsets prev -A and -B, but following -A or -B may override it */ if (!(option_mask32 & GREP_OPT_A)) /* not overridden */ slines_after = Copt; if (!(option_mask32 & GREP_OPT_B)) /* not overridden */ slines_before = Copt; option_mask32 |= GREP_OPT_A|GREP_OPT_B; /* for parser */ } if (option_mask32 & GREP_OPT_A) { lines_after = xatoi_u(slines_after); } if (option_mask32 & GREP_OPT_B) { lines_before = xatoi_u(slines_before); } /* sanity checks */ if (option_mask32 & (GREP_OPT_c|GREP_OPT_q|GREP_OPT_l|GREP_OPT_L)) { option_mask32 &= ~GREP_OPT_n; lines_before = 0; lines_after = 0; } else if (lines_before > 0) before_buf = xzalloc(lines_before * sizeof(char *)); #else /* with auto sanity checks */ opt_complementary = "H-h:e::f::c-n:q-n:l-n"; getopt32(argc, argv, GREP_OPTS OPT_EGREP, &pattern_head, &fopt); #endif invert_search = ((option_mask32 & GREP_OPT_v) != 0); /* 0 | 1 */ if (pattern_head != NULL) { /* convert char *argv[] to grep_list_data_t */ llist_t *cur; for (cur = pattern_head; cur; cur = cur->link) cur->data = new_grep_list_data(cur->data, 0); } if (option_mask32 & GREP_OPT_f) load_regexes_from_file(fopt); if (ENABLE_FEATURE_GREP_FGREP_ALIAS && applet_name[0] == 'f') option_mask32 |= GREP_OPT_F; if (!(option_mask32 & GREP_OPT_o)) reflags = REG_NOSUB; if (ENABLE_FEATURE_GREP_EGREP_ALIAS && (applet_name[0] == 'e' || (option_mask32 & GREP_OPT_E))) reflags |= REG_EXTENDED; if (option_mask32 & GREP_OPT_i) reflags |= REG_ICASE; argv += optind; argc -= optind; /* if we didn't get a pattern from a -e and no command file was specified, * argv[optind] should be the pattern. no pattern, no worky */ if (pattern_head == NULL) { if (*argv == NULL) bb_show_usage(); else { char *pattern = new_grep_list_data(*argv++, 0); llist_add_to(&pattern_head, pattern); argc--; } } /* argv[(optind)..(argc-1)] should be names of file to grep through. If * there is more than one file to grep, we will print the filenames. */ if (argc > 1) print_filename = 1; /* -H / -h of course override */ if (option_mask32 & GREP_OPT_H) print_filename = 1; if (option_mask32 & GREP_OPT_h) print_filename = 0; /* If no files were specified, or '-' was specified, take input from * stdin. Otherwise, we grep through all the files specified. */ if (argc == 0) argc++; matched = 0; while (argc--) { cur_file = *argv++; file = stdin; if (!cur_file || (*cur_file == '-' && !cur_file[1])) { cur_file = "(standard input)"; } else { if (option_mask32 & GREP_OPT_r) { struct stat st; if (stat(cur_file, &st) == 0 && S_ISDIR(st.st_mode)) { if (!(option_mask32 & GREP_OPT_h)) print_filename = 1; matched += grep_dir(cur_file); goto grep_done; } } /* else: fopen(dir) will succeed, but reading won't */ file = fopen(cur_file, "r"); if (file == NULL) { if (!SUPPRESS_ERR_MSGS) bb_perror_msg("%s", cur_file); open_errors = 1; continue; } } matched += grep_file(file); fclose_if_not_stdin(file); grep_done: if (matched < 0) { /* we found a match but were told to be quiet, stop here and * return success */ break; } } /* destroy all the elments in the pattern list */ if (ENABLE_FEATURE_CLEAN_UP) { while (pattern_head) { llist_t *pattern_head_ptr = pattern_head; grep_list_data_t *gl = (grep_list_data_t *)pattern_head_ptr->data; pattern_head = pattern_head->link; if ((gl->flg_mem_alocated_compiled & PATTERN_MEM_A)) free(gl->pattern); if ((gl->flg_mem_alocated_compiled & COMPILED)) regfree(&(gl->preg)); free(pattern_head_ptr); } } /* 0 = success, 1 = failed, 2 = error */ /* If the -q option is specified, the exit status shall be zero * if an input line is selected, even if an error was detected. */ if (BE_QUIET && matched) return 0; if (open_errors) return 2; return !matched; /* invert return value 0 = success, 1 = failed */ }
static bool grep_file (const std::string &filename, boost::regex &re, bool ignore_nonimage_files=false) { if (! Filesystem::exists (filename)) { std::cerr << "igrep: " << filename << ": No such file or directory\n"; return false; } if (Filesystem::is_directory (filename)) { if (! recursive) return false; if (print_dirs) { std::cout << "(" << filename << "/)\n"; std::cout.flush(); } bool r = false; std::vector<std::string> directory_entries; Filesystem::get_directory_entries (filename, directory_entries); for (size_t i = 0, e = directory_entries.size(); i < e; ++i) r |= grep_file (directory_entries[i], re, true); return r; } std::unique_ptr<ImageInput> in (ImageInput::open (filename.c_str())); if (! in.get()) { if (! ignore_nonimage_files) std::cerr << geterror() << "\n"; return false; } ImageSpec spec = in->spec(); if (file_match) { bool match = boost::regex_search (filename, re); if (match && ! invert_match) { std::cout << filename << "\n"; return true; } } bool found = false; int subimage = 0; do { if (!all_subimages && subimage > 0) break; for (auto&& p : spec.extra_attribs) { TypeDesc t = p.type(); if (t.elementtype() == TypeDesc::STRING) { int n = t.numelements(); for (int i = 0; i < n; ++i) { bool match = boost::regex_search (((const char **)p.data())[i], re); found |= match; if (match && ! invert_match) { if (list_files) { std::cout << filename << "\n"; return found; } std::cout << filename << ": " << p.name() << " = " << ((const char **)p.data())[i] << "\n"; } } } } } while (in->seek_subimage (++subimage, 0, spec)); if (invert_match) { found = !found; if (found) std::cout << filename << "\n"; } return found; }
extern int grep_main(int argc, char **argv) { FILE *file; int matched; unsigned long opt; llist_t *fopt = NULL; /* do normal option parsing */ #ifdef CONFIG_FEATURE_GREP_CONTEXT { char *junk; char *slines_after; char *slines_before; char *Copt; bb_opt_complementaly = "H-h:e*:f*:C-AB"; opt = bb_getopt_ulflags(argc, argv, GREP_OPTS GREP_OPT_CONTEXT OPT_EGREP, &pattern_head, &fopt, &slines_after, &slines_before, &Copt); if(opt & GREP_OPT_C) { /* C option unseted A and B options, but next -A or -B may be ovewrite own option */ if(!(opt & GREP_OPT_A)) /* not overwtited */ slines_after = Copt; if(!(opt & GREP_OPT_B)) /* not overwtited */ slines_before = Copt; opt |= GREP_OPT_A|GREP_OPT_B; /* set for parse now */ } if(opt & GREP_OPT_A) { lines_after = strtoul(slines_after, &junk, 10); if(*junk != '\0') bb_error_msg_and_die("invalid context length argument"); } if(opt & GREP_OPT_B) { lines_before = strtoul(slines_before, &junk, 10); if(*junk != '\0') bb_error_msg_and_die("invalid context length argument"); } /* sanity checks after parse may be invalid numbers ;-) */ if ((opt & (GREP_OPT_c|GREP_OPT_q|GREP_OPT_l|GREP_OPT_L))) { opt &= ~GREP_OPT_n; lines_before = 0; lines_after = 0; } else if(lines_before > 0) before_buf = (char **)xcalloc(lines_before, sizeof(char *)); } #else /* with auto sanity checks */ bb_opt_complementaly = "H-h:e*:f*:c-n:q-n:l-n"; opt = bb_getopt_ulflags(argc, argv, GREP_OPTS OPT_EGREP, &pattern_head, &fopt); #endif print_files_with_matches = opt & GREP_OPT_l; print_files_without_matches = (opt & GREP_OPT_L) != 0; print_line_num = opt & GREP_OPT_n; be_quiet = opt & GREP_OPT_q; invert_search = (opt & GREP_OPT_v) != 0; /* 0 | 1 */ suppress_err_msgs = opt & GREP_OPT_s; print_match_counts = opt & GREP_OPT_c; fgrep_flag = opt & GREP_OPT_F; if(opt & GREP_OPT_H) print_filename++; if(opt & GREP_OPT_h) print_filename--; if(opt & GREP_OPT_f) load_regexes_from_file(fopt); #ifdef CONFIG_FEATURE_GREP_FGREP_ALIAS if(bb_applet_name[0] == 'f') fgrep_flag = 1; #endif #ifdef CONFIG_FEATURE_GREP_EGREP_ALIAS if(bb_applet_name[0] == 'e' || (opt & GREP_OPT_E)) reflags = REG_EXTENDED | REG_NOSUB; else #endif reflags = REG_NOSUB; if(opt & GREP_OPT_i) reflags |= REG_ICASE; argv += optind; argc -= optind; /* if we didn't get a pattern from a -e and no command file was specified, * argv[optind] should be the pattern. no pattern, no worky */ if (pattern_head == NULL) { if (*argv == NULL) bb_show_usage(); else { pattern_head = llist_add_to(pattern_head, *argv++); argc--; } } /* argv[(optind)..(argc-1)] should be names of file to grep through. If * there is more than one file to grep, we will print the filenames */ if (argc > 1) { print_filename++; /* If no files were specified, or '-' was specified, take input from * stdin. Otherwise, we grep through all the files specified. */ } else if (argc == 0) { argc++; } matched = 0; while (argc--) { cur_file = *argv++; if(!cur_file || (*cur_file == '-' && !cur_file[1])) { cur_file = "-"; file = stdin; } else { file = fopen(cur_file, "r"); } if (file == NULL) { if (!suppress_err_msgs) bb_perror_msg("%s", cur_file); } else { matched += grep_file(file); if(matched < 0) { /* we found a match but were told to be quiet, stop here and * return success */ break; } fclose(file); } } #ifdef CONFIG_FEATURE_CLEAN_UP /* destroy all the elments in the pattern list */ while (pattern_head) { llist_t *pattern_head_ptr = pattern_head; pattern_head = pattern_head->link; free(pattern_head_ptr); } #endif return !matched; /* invert return value 0 = success, 1 = failed */ }
static bool run_output_check(const output_check oc, const atf::fs::path& path, const std::string& stdxxx) { bool result; if (oc.type == oc_empty) { const bool is_empty = file_empty(path); if (!oc.negated && !is_empty) { std::cerr << "Fail: " << stdxxx << " not empty\n"; print_diff(atf::fs::path("/dev/null"), path); result = false; } else if (oc.negated && is_empty) { std::cerr << "Fail: " << stdxxx << " is empty\n"; result = false; } else result = true; } else if (oc.type == oc_file) { const bool equals = compare_files(path, atf::fs::path(oc.value)); if (!oc.negated && !equals) { std::cerr << "Fail: " << stdxxx << " does not match golden " "output\n"; print_diff(atf::fs::path(oc.value), path); result = false; } else if (oc.negated && equals) { std::cerr << "Fail: " << stdxxx << " matches golden output\n"; cat_file(atf::fs::path(oc.value)); result = false; } else result = true; } else if (oc.type == oc_ignore) { result = true; } else if (oc.type == oc_inline) { atf::fs::path path2 = atf::fs::path(atf::config::get("atf_workdir")) / "inline.XXXXXX"; temp_file temp(path2); temp.write(decode(oc.value)); temp.close(); const bool equals = compare_files(path, temp.get_path()); if (!oc.negated && !equals) { std::cerr << "Fail: " << stdxxx << " does not match expected " "value\n"; print_diff(temp.get_path(), path); result = false; } else if (oc.negated && equals) { std::cerr << "Fail: " << stdxxx << " matches expected value\n"; cat_file(temp.get_path()); result = false; } else result = true; } else if (oc.type == oc_match) { const bool matches = grep_file(path, oc.value); if (!oc.negated && !matches) { std::cerr << "Fail: regexp " + oc.value + " not in " << stdxxx << "\n"; cat_file(path); result = false; } else if (oc.negated && matches) { std::cerr << "Fail: regexp " + oc.value + " is in " << stdxxx << "\n"; cat_file(path); result = false; } else result = true; } else if (oc.type == oc_save) { INV(!oc.negated); std::ifstream ifs(path.c_str(), std::fstream::binary); ifs >> std::noskipws; std::istream_iterator< char > begin(ifs), end; std::ofstream ofs(oc.value.c_str(), std::fstream::binary | std::fstream::trunc); std::ostream_iterator <char> obegin(ofs); std::copy(begin, end, obegin); result = true; } else {
extern int grep_main(int argc, char **argv) { int opt; #if defined BB_FEATURE_GREP_CONTEXT || defined BB_FEATURE_GREP_EGREP_ALIAS char *junk; #endif #ifdef BB_FEATURE_CLEAN_UP /* destroy command strings on exit */ atexit(destroy_regexes); #endif #ifdef BB_FEATURE_GREP_EGREP_ALIAS junk = get_last_path_component(argv[0]); if (junk && strcmp(junk, "egrep") == 0) reflags |= REG_EXTENDED; #endif /* do normal option parsing */ while ((opt = getopt(argc, argv, "iHhlnqvsce:f:" #ifdef BB_FEATURE_GREP_CONTEXT "A:B:C:" #endif #ifdef BB_FEATURE_GREP_EGREP_ALIAS "E" #endif )) > 0) { switch (opt) { case 'i': ignore_case++; break; case 'l': print_files_with_matches++; break; case 'H': print_filename++; break; case 'h': print_filename--; break; case 'n': print_line_num++; break; case 'q': be_quiet++; break; case 'v': invert_search++; break; case 's': suppress_err_msgs++; break; case 'c': print_match_counts++; break; case 'e': add_regex(optarg); break; #ifdef BB_FEATURE_GREP_EGREP_ALIAS case 'E': reflags |= REG_EXTENDED; break; #endif case 'f': load_regexes_from_file(optarg); break; #ifdef BB_FEATURE_GREP_CONTEXT case 'A': lines_after = strtoul(optarg, &junk, 10); if(*junk != '\0') error_msg_and_die("invalid context length argument"); break; case 'B': lines_before = strtoul(optarg, &junk, 10); if(*junk != '\0') error_msg_and_die("invalid context length argument"); before_buf = (char **)xcalloc(lines_before, sizeof(char *)); break; case 'C': lines_after = lines_before = strtoul(optarg, &junk, 10); if(*junk != '\0') error_msg_and_die("invalid context length argument"); before_buf = (char **)xcalloc(lines_before, sizeof(char *)); break; #endif /* BB_FEATURE_GREP_CONTEXT */ default: show_usage(); } } /* if we didn't get a pattern from a -e and no command file was specified, * argv[optind] should be the pattern. no pattern, no worky */ if (nregexes == 0) { if (argv[optind] == NULL) show_usage(); else { add_regex(argv[optind]); optind++; } } /* sanity checks */ if (print_match_counts || be_quiet || print_files_with_matches) { print_line_num = 0; #ifdef BB_FEATURE_GREP_CONTEXT lines_before = 0; lines_after = 0; #endif } /* argv[(optind)..(argc-1)] should be names of file to grep through. If * there is more than one file to grep, we will print the filenames */ if ((argc-1) - (optind) > 0) print_filename++; /* If no files were specified, or '-' was specified, take input from * stdin. Otherwise, we grep through all the files specified. */ if (argv[optind] == NULL || (strcmp(argv[optind], "-") == 0)) { grep_file(stdin); } else { int i; FILE *file; for (i = optind; i < argc; i++) { cur_file = argv[i]; file = fopen(cur_file, "r"); if (file == NULL) { if (!suppress_err_msgs) perror_msg("%s", cur_file); } else { grep_file(file); fclose(file); } } } return !matched; /* invert return value 0 = success, 1 = failed */ }