e386ca4c7357cb52b8375fb3acfd872d57eee382
   1#ifndef GREP_H
   2#define GREP_H
   3#include "color.h"
   4#ifdef USE_LIBPCRE
   5#include <pcre.h>
   6#else
   7typedef int pcre;
   8typedef int pcre_extra;
   9#endif
  10#include "kwset.h"
  11#include "thread-utils.h"
  12
  13enum grep_pat_token {
  14        GREP_PATTERN,
  15        GREP_PATTERN_HEAD,
  16        GREP_PATTERN_BODY,
  17        GREP_AND,
  18        GREP_OPEN_PAREN,
  19        GREP_CLOSE_PAREN,
  20        GREP_NOT,
  21        GREP_OR
  22};
  23
  24enum grep_context {
  25        GREP_CONTEXT_HEAD,
  26        GREP_CONTEXT_BODY
  27};
  28
  29enum grep_header_field {
  30        GREP_HEADER_AUTHOR = 0,
  31        GREP_HEADER_COMMITTER
  32};
  33#define GREP_HEADER_FIELD_MAX (GREP_HEADER_COMMITTER + 1)
  34
  35struct grep_pat {
  36        struct grep_pat *next;
  37        const char *origin;
  38        int no;
  39        enum grep_pat_token token;
  40        const char *pattern;
  41        size_t patternlen;
  42        enum grep_header_field field;
  43        regex_t regexp;
  44        pcre *pcre_regexp;
  45        pcre_extra *pcre_extra_info;
  46        kwset_t kws;
  47        unsigned fixed:1;
  48        unsigned ignore_case:1;
  49        unsigned word_regexp:1;
  50};
  51
  52enum grep_expr_node {
  53        GREP_NODE_ATOM,
  54        GREP_NODE_NOT,
  55        GREP_NODE_AND,
  56        GREP_NODE_TRUE,
  57        GREP_NODE_OR
  58};
  59
  60struct grep_expr {
  61        enum grep_expr_node node;
  62        unsigned hit;
  63        union {
  64                struct grep_pat *atom;
  65                struct grep_expr *unary;
  66                struct {
  67                        struct grep_expr *left;
  68                        struct grep_expr *right;
  69                } binary;
  70        } u;
  71};
  72
  73struct grep_opt {
  74        struct grep_pat *pattern_list;
  75        struct grep_pat **pattern_tail;
  76        struct grep_pat *header_list;
  77        struct grep_pat **header_tail;
  78        struct grep_expr *pattern_expression;
  79        const char *prefix;
  80        int prefix_length;
  81        regex_t regexp;
  82        int linenum;
  83        int invert;
  84        int ignore_case;
  85        int status_only;
  86        int name_only;
  87        int unmatch_name_only;
  88        int count;
  89        int word_regexp;
  90        int fixed;
  91        int all_match;
  92#define GREP_BINARY_DEFAULT     0
  93#define GREP_BINARY_NOMATCH     1
  94#define GREP_BINARY_TEXT        2
  95        int binary;
  96        int extended;
  97        int pcre;
  98        int relative;
  99        int pathname;
 100        int null_following_name;
 101        int color;
 102        int max_depth;
 103        int funcname;
 104        int funcbody;
 105        char color_context[COLOR_MAXLEN];
 106        char color_filename[COLOR_MAXLEN];
 107        char color_function[COLOR_MAXLEN];
 108        char color_lineno[COLOR_MAXLEN];
 109        char color_match[COLOR_MAXLEN];
 110        char color_selected[COLOR_MAXLEN];
 111        char color_sep[COLOR_MAXLEN];
 112        int regflags;
 113        unsigned pre_context;
 114        unsigned post_context;
 115        unsigned last_shown;
 116        int show_hunk_mark;
 117        int file_break;
 118        int heading;
 119        void *priv;
 120
 121        void (*output)(struct grep_opt *opt, const void *data, size_t size);
 122        void *output_priv;
 123};
 124
 125extern void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);
 126extern void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
 127extern void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *);
 128extern void compile_grep_patterns(struct grep_opt *opt);
 129extern void free_grep_patterns(struct grep_opt *opt);
 130extern int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size);
 131
 132struct grep_source {
 133        char *name;
 134
 135        enum grep_source_type {
 136                GREP_SOURCE_SHA1,
 137                GREP_SOURCE_FILE,
 138                GREP_SOURCE_BUF,
 139        } type;
 140        void *identifier;
 141
 142        char *buf;
 143        unsigned long size;
 144};
 145
 146void grep_source_init(struct grep_source *gs, enum grep_source_type type,
 147                      const char *name, const void *identifier);
 148int grep_source_load(struct grep_source *gs);
 149void grep_source_clear_data(struct grep_source *gs);
 150void grep_source_clear(struct grep_source *gs);
 151
 152int grep_source(struct grep_opt *opt, struct grep_source *gs);
 153
 154extern struct grep_opt *grep_opt_dup(const struct grep_opt *opt);
 155extern int grep_threads_ok(const struct grep_opt *opt);
 156
 157#ifndef NO_PTHREADS
 158/*
 159 * Mutex used around access to the attributes machinery if
 160 * opt->use_threads.  Must be initialized/destroyed by callers!
 161 */
 162extern int grep_use_locks;
 163extern pthread_mutex_t grep_attr_mutex;
 164extern pthread_mutex_t grep_read_mutex;
 165
 166static inline void grep_read_lock(void)
 167{
 168        if (grep_use_locks)
 169                pthread_mutex_lock(&grep_read_mutex);
 170}
 171
 172static inline void grep_read_unlock(void)
 173{
 174        if (grep_use_locks)
 175                pthread_mutex_unlock(&grep_read_mutex);
 176}
 177
 178#else
 179#define grep_read_lock()
 180#define grep_read_unlock()
 181#endif
 182
 183#endif