grep.hon commit Merge branch 'pw/p4-various-fixes' (15470c6)
   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#include "userdiff.h"
  13
  14enum grep_pat_token {
  15        GREP_PATTERN,
  16        GREP_PATTERN_HEAD,
  17        GREP_PATTERN_BODY,
  18        GREP_AND,
  19        GREP_OPEN_PAREN,
  20        GREP_CLOSE_PAREN,
  21        GREP_NOT,
  22        GREP_OR
  23};
  24
  25enum grep_context {
  26        GREP_CONTEXT_HEAD,
  27        GREP_CONTEXT_BODY
  28};
  29
  30enum grep_header_field {
  31        GREP_HEADER_AUTHOR = 0,
  32        GREP_HEADER_COMMITTER,
  33        GREP_HEADER_REFLOG,
  34
  35        /* Must be at the end of the enum */
  36        GREP_HEADER_FIELD_MAX
  37};
  38
  39struct grep_pat {
  40        struct grep_pat *next;
  41        const char *origin;
  42        int no;
  43        enum grep_pat_token token;
  44        char *pattern;
  45        size_t patternlen;
  46        enum grep_header_field field;
  47        regex_t regexp;
  48        pcre *pcre_regexp;
  49        pcre_extra *pcre_extra_info;
  50        kwset_t kws;
  51        unsigned fixed:1;
  52        unsigned ignore_case:1;
  53        unsigned word_regexp:1;
  54};
  55
  56enum grep_expr_node {
  57        GREP_NODE_ATOM,
  58        GREP_NODE_NOT,
  59        GREP_NODE_AND,
  60        GREP_NODE_TRUE,
  61        GREP_NODE_OR
  62};
  63
  64enum grep_pattern_type {
  65        GREP_PATTERN_TYPE_UNSPECIFIED = 0,
  66        GREP_PATTERN_TYPE_BRE,
  67        GREP_PATTERN_TYPE_ERE,
  68        GREP_PATTERN_TYPE_FIXED,
  69        GREP_PATTERN_TYPE_PCRE
  70};
  71
  72struct grep_expr {
  73        enum grep_expr_node node;
  74        unsigned hit;
  75        union {
  76                struct grep_pat *atom;
  77                struct grep_expr *unary;
  78                struct {
  79                        struct grep_expr *left;
  80                        struct grep_expr *right;
  81                } binary;
  82        } u;
  83};
  84
  85struct grep_opt {
  86        struct grep_pat *pattern_list;
  87        struct grep_pat **pattern_tail;
  88        struct grep_pat *header_list;
  89        struct grep_pat **header_tail;
  90        struct grep_expr *pattern_expression;
  91        const char *prefix;
  92        int prefix_length;
  93        regex_t regexp;
  94        int linenum;
  95        int invert;
  96        int ignore_case;
  97        int status_only;
  98        int name_only;
  99        int unmatch_name_only;
 100        int count;
 101        int word_regexp;
 102        int fixed;
 103        int all_match;
 104        int debug;
 105#define GREP_BINARY_DEFAULT     0
 106#define GREP_BINARY_NOMATCH     1
 107#define GREP_BINARY_TEXT        2
 108        int binary;
 109        int extended;
 110        int use_reflog_filter;
 111        int pcre;
 112        int relative;
 113        int pathname;
 114        int null_following_name;
 115        int color;
 116        int max_depth;
 117        int funcname;
 118        int funcbody;
 119        int extended_regexp_option;
 120        int pattern_type_option;
 121        char color_context[COLOR_MAXLEN];
 122        char color_filename[COLOR_MAXLEN];
 123        char color_function[COLOR_MAXLEN];
 124        char color_lineno[COLOR_MAXLEN];
 125        char color_match[COLOR_MAXLEN];
 126        char color_selected[COLOR_MAXLEN];
 127        char color_sep[COLOR_MAXLEN];
 128        int regflags;
 129        unsigned pre_context;
 130        unsigned post_context;
 131        unsigned last_shown;
 132        int show_hunk_mark;
 133        int file_break;
 134        int heading;
 135        void *priv;
 136
 137        void (*output)(struct grep_opt *opt, const void *data, size_t size);
 138        void *output_priv;
 139};
 140
 141extern void init_grep_defaults(void);
 142extern int grep_config(const char *var, const char *value, void *);
 143extern void grep_init(struct grep_opt *, const char *prefix);
 144void grep_set_pattern_type_option(enum grep_pattern_type, struct grep_opt *opt);
 145void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt);
 146
 147extern void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);
 148extern void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
 149extern void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *);
 150extern void compile_grep_patterns(struct grep_opt *opt);
 151extern void free_grep_patterns(struct grep_opt *opt);
 152extern int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size);
 153
 154struct grep_source {
 155        char *name;
 156
 157        enum grep_source_type {
 158                GREP_SOURCE_SHA1,
 159                GREP_SOURCE_FILE,
 160                GREP_SOURCE_BUF,
 161        } type;
 162        void *identifier;
 163
 164        char *buf;
 165        unsigned long size;
 166
 167        char *path; /* for attribute lookups */
 168        struct userdiff_driver *driver;
 169};
 170
 171void grep_source_init(struct grep_source *gs, enum grep_source_type type,
 172                      const char *name, const char *path,
 173                      const void *identifier);
 174void grep_source_clear_data(struct grep_source *gs);
 175void grep_source_clear(struct grep_source *gs);
 176void grep_source_load_driver(struct grep_source *gs);
 177
 178
 179int grep_source(struct grep_opt *opt, struct grep_source *gs);
 180
 181extern struct grep_opt *grep_opt_dup(const struct grep_opt *opt);
 182extern int grep_threads_ok(const struct grep_opt *opt);
 183
 184#ifndef NO_PTHREADS
 185/*
 186 * Mutex used around access to the attributes machinery if
 187 * opt->use_threads.  Must be initialized/destroyed by callers!
 188 */
 189extern int grep_use_locks;
 190extern pthread_mutex_t grep_attr_mutex;
 191extern pthread_mutex_t grep_read_mutex;
 192
 193static inline void grep_read_lock(void)
 194{
 195        if (grep_use_locks)
 196                pthread_mutex_lock(&grep_read_mutex);
 197}
 198
 199static inline void grep_read_unlock(void)
 200{
 201        if (grep_use_locks)
 202                pthread_mutex_unlock(&grep_read_mutex);
 203}
 204
 205#else
 206#define grep_read_lock()
 207#define grep_read_unlock()
 208#endif
 209
 210#endif