From 24ed834317f3fda8f0f55489f54a2df1aca5de17 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 30 Apr 2022 14:19:08 -0400 Subject: Simplified things by passing a def argument to next_match instead of chaining defs together. Also simplified `..` by just using a lookahead instead of retconning it. Immutability invariants are now enforced better. --- bp.c | 51 ++++++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 27 deletions(-) (limited to 'bp.c') diff --git a/bp.c b/bp.c index dbd72bd..b863e24 100644 --- a/bp.c +++ b/bp.c @@ -207,10 +207,10 @@ static int is_text_file(const char *filename) // // Print matches in JSON format. // -static int print_matches_as_json(file_t *f, pat_t *pattern) +static int print_matches_as_json(file_t *f, pat_t *pattern, pat_t *defs) { int nmatches = 0; - for (match_t *m = NULL; next_match(&m, f->start, f->end, pattern, options.skip, options.ignorecase); ) { + for (match_t *m = NULL; next_match(&m, f->start, f->end, pattern, defs, options.skip, options.ignorecase); ) { if (++nmatches > 1) printf(",\n"); printf("{\"filename\":\"%s\",\"match\":", f->filename); @@ -223,10 +223,10 @@ static int print_matches_as_json(file_t *f, pat_t *pattern) // // Print matches in a visual explanation style // -static int explain_matches(file_t *f, pat_t *pattern) +static int explain_matches(file_t *f, pat_t *pattern, pat_t *defs) { int nmatches = 0; - for (match_t *m = NULL; next_match(&m, f->start, f->end, pattern, options.skip, options.ignorecase); ) { + for (match_t *m = NULL; next_match(&m, f->start, f->end, pattern, defs, options.skip, options.ignorecase); ) { if (++nmatches == 1) { if (options.print_filenames) fprint_filename(stdout, f->filename); @@ -349,7 +349,7 @@ static void on_nl(FILE *out) // // Print all the matches in a file. // -static int print_matches(FILE *out, file_t *f, pat_t *pattern) +static int print_matches(FILE *out, file_t *f, pat_t *pattern, pat_t *defs) { static int printed_filenames = 0; int matches = 0; @@ -364,7 +364,7 @@ static int print_matches(FILE *out, file_t *f, pat_t *pattern) print_opts.replace_color = "\033[0;34;1m"; print_opts.normal_color = "\033[m"; } - for (match_t *m = NULL; next_match(&m, f->start, f->end, pattern, options.skip, options.ignorecase); ) { + for (match_t *m = NULL; next_match(&m, f->start, f->end, pattern, defs, options.skip, options.ignorecase); ) { if (++matches == 1 && options.print_filenames) { if (printed_filenames++ > 0) printf("\n"); fprint_filename(out, f->filename); @@ -394,7 +394,7 @@ static int print_matches(FILE *out, file_t *f, pat_t *pattern) // against it, printing any results according to the flags. // __attribute__((nonnull)) -static int process_file(const char *filename, pat_t *pattern) +static int process_file(const char *filename, pat_t *pattern, pat_t *defs) { file_t *f = load_file(NULL, filename); if (f == NULL) { @@ -404,19 +404,19 @@ static int process_file(const char *filename, pat_t *pattern) int matches = 0; if (options.mode == MODE_EXPLAIN) { - matches += explain_matches(f, pattern); + matches += explain_matches(f, pattern, defs); } else if (options.mode == MODE_LISTFILES) { match_t *m = NULL; - if (next_match(&m, f->start, f->end, pattern, options.skip, options.ignorecase)) { + if (next_match(&m, f->start, f->end, pattern, defs, options.skip, options.ignorecase)) { printf("%s\n", f->filename); matches += 1; } stop_matching(&m); } else if (options.mode == MODE_JSON) { - matches += print_matches_as_json(f, pattern); + matches += print_matches_as_json(f, pattern, defs); } else if (options.mode == MODE_INPLACE) { match_t *m = NULL; - bool found = next_match(&m, f->start, f->end, pattern, options.skip, options.ignorecase); + bool found = next_match(&m, f->start, f->end, pattern, defs, options.skip, options.ignorecase); stop_matching(&m); if (!found) return 0; @@ -432,12 +432,12 @@ static int process_file(const char *filename, pat_t *pattern) // are used to restore the original file contents. modifying_file = out; backup_file = f; { - matches += print_matches(out, f, pattern); + matches += print_matches(out, f, pattern, defs); } modifying_file = NULL; backup_file = NULL; fclose(out); } else { - matches += print_matches(stdout, f, pattern); + matches += print_matches(stdout, f, pattern, defs); } fflush(stdout); @@ -452,7 +452,7 @@ static int process_file(const char *filename, pat_t *pattern) // Recursively process all non-dotfile files in the given directory. // __attribute__((nonnull)) -static int process_dir(const char *dirname, pat_t *pattern) +static int process_dir(const char *dirname, pat_t *pattern, pat_t *defs) { int matches = 0; glob_t globbuf; @@ -469,9 +469,9 @@ static int process_dir(const char *dirname, pat_t *pattern) if (S_ISLNK(statbuf.st_mode)) continue; // Skip symbolic links else if (S_ISDIR(statbuf.st_mode)) - matches += process_dir(globbuf.gl_pathv[i], pattern); + matches += process_dir(globbuf.gl_pathv[i], pattern, defs); else if (is_text_file(globbuf.gl_pathv[i])) - matches += process_file(globbuf.gl_pathv[i], pattern); + matches += process_file(globbuf.gl_pathv[i], pattern, defs); } } globfree(&globbuf); @@ -482,7 +482,7 @@ static int process_dir(const char *dirname, pat_t *pattern) // Process git files using `git ls-files ...` // __attribute__((nonnull(1))) -static int process_git_files(pat_t *pattern, int argc, char *argv[]) +static int process_git_files(pat_t *pattern, pat_t *defs, int argc, char *argv[]) { int fds[2]; require(pipe(fds), "Failed to create pipe"); @@ -505,7 +505,7 @@ static int process_git_files(pat_t *pattern, int argc, char *argv[]) size_t path_size = 0; int found = 0; while (getdelim(&path, &path_size, '\0', fp) > 0) - found += process_file(path, pattern); + found += process_file(path, pattern, defs); if (path) delete(&path); require(fclose(fp), "Failed to close read end of pipe"); int status; @@ -521,7 +521,7 @@ static int process_git_files(pat_t *pattern, int argc, char *argv[]) // static pat_t *load_grammar(pat_t *defs, file_t *f) { - return chain_together(assert_pat(f->start, f->end, bp_pattern(f->start, f->end)), defs); + return chain_together(defs, assert_pat(f->start, f->end, bp_pattern(f->start, f->end))); } // @@ -636,9 +636,6 @@ int main(int argc, char *argv[]) if (pattern == NULL) errx(EXIT_FAILURE, "No pattern provided.\n\n%s", usage); - // Hook up definitions: - pattern = chain_together(defs, pattern); - for (argc = 0; argv[argc]; ++argc) ; // update argc if (options.context_before == USE_DEFAULT_CONTEXT) options.context_before = 0; @@ -660,7 +657,7 @@ int main(int argc, char *argv[]) int found = 0; if (options.mode == MODE_JSON) printf("["); if (options.git_mode) { // Get the list of files from `git --ls-files ...` - found = process_git_files(pattern, argc, argv); + found = process_git_files(pattern, defs, argc, argv); } else if (argv[0]) { // Files pass in as command line args: struct stat statbuf; @@ -668,17 +665,17 @@ int main(int argc, char *argv[]) options.print_filenames = false; for ( ; argv[0]; argv++) { if (stat(argv[0], &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) // Symlinks are okay if manually specified - found += process_dir(argv[0], pattern); + found += process_dir(argv[0], pattern, defs); else - found += process_file(argv[0], pattern); + found += process_file(argv[0], pattern, defs); } } else if (isatty(STDIN_FILENO)) { // No files, no piped in input, so use files in current dir, recursively - found += process_dir(".", pattern); + found += process_dir(".", pattern, defs); } else { // Piped in input: options.print_filenames = false; // Don't print filename on stdin - found += process_file("", pattern); + found += process_file("", pattern, defs); } if (options.mode == MODE_JSON) printf("]\n"); -- cgit v1.2.3