From 83b5d2f5b0c95fe102bc3d1cc2947abbdf5e5c5b Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sun, 17 Sep 2006 16:02:52 -0700 Subject: builtin-grep: make pieces of it available as library. This makes three functions and associated option structures from builtin-grep available from other parts of the system. * options to drive built-in grep engine is stored in struct grep_opt; * pattern strings and extended grep expressions are added to struct grep_opt with append_grep_pattern(); * when finished calling append_grep_pattern(), call compile_grep_patterns() to prepare for execution; * call grep_buffer() to find matches in the in-core buffer. This also adds an internal option "status_only" to grep_opt, which suppresses any output from grep_buffer(). Callers of the function as library can use it to check if there is a match without producing any output. Signed-off-by: Junio C Hamano --- grep.h | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 grep.h (limited to 'grep.h') diff --git a/grep.h b/grep.h new file mode 100644 index 000000000..80122b05d --- /dev/null +++ b/grep.h @@ -0,0 +1,71 @@ +#ifndef GREP_H +#define GREP_H + +enum grep_pat_token { + GREP_PATTERN, + GREP_AND, + GREP_OPEN_PAREN, + GREP_CLOSE_PAREN, + GREP_NOT, + GREP_OR, +}; + +struct grep_pat { + struct grep_pat *next; + const char *origin; + int no; + enum grep_pat_token token; + const char *pattern; + regex_t regexp; +}; + +enum grep_expr_node { + GREP_NODE_ATOM, + GREP_NODE_NOT, + GREP_NODE_AND, + GREP_NODE_OR, +}; + +struct grep_expr { + enum grep_expr_node node; + union { + struct grep_pat *atom; + struct grep_expr *unary; + struct { + struct grep_expr *left; + struct grep_expr *right; + } binary; + } u; +}; + +struct grep_opt { + struct grep_pat *pattern_list; + struct grep_pat **pattern_tail; + struct grep_expr *pattern_expression; + int prefix_length; + regex_t regexp; + unsigned linenum:1; + unsigned invert:1; + unsigned status_only:1; + unsigned name_only:1; + unsigned unmatch_name_only:1; + unsigned count:1; + unsigned word_regexp:1; + unsigned fixed:1; +#define GREP_BINARY_DEFAULT 0 +#define GREP_BINARY_NOMATCH 1 +#define GREP_BINARY_TEXT 2 + unsigned binary:2; + unsigned extended:1; + unsigned relative:1; + unsigned pathname:1; + int regflags; + unsigned pre_context; + unsigned post_context; +}; + +extern void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t); +extern void compile_grep_patterns(struct grep_opt *opt); +extern int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size); + +#endif -- cgit v1.2.1 From 480c1ca6fd8df58a783e231648b489ed2bfd17f1 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 20 Sep 2006 12:39:46 -0700 Subject: Update grep internal for grepping only in head/body This further updates the built-in grep engine so that we can say something like "this pattern should match only in head". This can be used to simplify grepping in the log messages. Signed-off-by: Junio C Hamano --- grep.h | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'grep.h') diff --git a/grep.h b/grep.h index 80122b05d..0b503ea66 100644 --- a/grep.h +++ b/grep.h @@ -3,6 +3,8 @@ enum grep_pat_token { GREP_PATTERN, + GREP_PATTERN_HEAD, + GREP_PATTERN_BODY, GREP_AND, GREP_OPEN_PAREN, GREP_CLOSE_PAREN, @@ -10,6 +12,11 @@ enum grep_pat_token { GREP_OR, }; +enum grep_context { + GREP_CONTEXT_HEAD, + GREP_CONTEXT_BODY, +}; + struct grep_pat { struct grep_pat *next; const char *origin; -- cgit v1.2.1 From b48fb5b6a950a6757b790e9160967065a3e03978 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 27 Sep 2006 16:27:10 -0700 Subject: grep: free expressions and patterns when done. Signed-off-by: Junio C Hamano --- grep.h | 1 + 1 file changed, 1 insertion(+) (limited to 'grep.h') diff --git a/grep.h b/grep.h index 0b503ea66..af9098cfe 100644 --- a/grep.h +++ b/grep.h @@ -73,6 +73,7 @@ struct grep_opt { extern void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t); extern void compile_grep_patterns(struct grep_opt *opt); +extern void free_grep_patterns(struct grep_opt *opt); extern int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size); #endif -- cgit v1.2.1 From 0ab7befa31d07fe3ffb51a6cc626d4c09ded1c92 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 27 Sep 2006 17:50:52 -0700 Subject: grep --all-match This lets you say: git grep --all-match -e A -e B -e C to find lines that match A or B or C but limit the matches from the files that have all of A, B and C. This is different from git grep -e A --and -e B --and -e C in that the latter looks for a single line that has all of these at the same time. Signed-off-by: Junio C Hamano --- grep.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'grep.h') diff --git a/grep.h b/grep.h index af9098cfe..d252dd25f 100644 --- a/grep.h +++ b/grep.h @@ -35,6 +35,7 @@ enum grep_expr_node { struct grep_expr { enum grep_expr_node node; + unsigned hit; union { struct grep_pat *atom; struct grep_expr *unary; @@ -59,6 +60,7 @@ struct grep_opt { unsigned count:1; unsigned word_regexp:1; unsigned fixed:1; + unsigned all_match:1; #define GREP_BINARY_DEFAULT 0 #define GREP_BINARY_NOMATCH 1 #define GREP_BINARY_TEXT 2 -- cgit v1.2.1