grep: move the configuration parsing logic to grep.[ch]

The configuration handling is a library-ish part of this program, that is not specific to "git grep" command. It should be reusable by "log" and others. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Oct 9, 2012 at 16:17 UTC 7687a0541e0a6d86c5005d84057321368181c1b5
3 files changed +134 -131
builtin/grep.c
-131
@@ -261,21 +261,6 @@ static int wait_all(void)
261 }
262 #endif
263
264 -static int parse_pattern_type_arg(const char *opt, const char *arg)
265 -{
266 - if (!strcmp(arg, "default"))
267 - return GREP_PATTERN_TYPE_UNSPECIFIED;
268 - else if (!strcmp(arg, "basic"))
269 - return GREP_PATTERN_TYPE_BRE;
270 - else if (!strcmp(arg, "extended"))
271 - return GREP_PATTERN_TYPE_ERE;
272 - else if (!strcmp(arg, "fixed"))
273 - return GREP_PATTERN_TYPE_FIXED;
274 - else if (!strcmp(arg, "perl"))
275 - return GREP_PATTERN_TYPE_PCRE;
276 - die("bad %s argument: %s", opt, arg);
277 -}
278 -
264 static void grep_pattern_type_options(const int pattern_type, struct grep_opt *opt)
265 {
266 switch (pattern_type) {
@@ -308,122 +293,6 @@ static void grep_pattern_type_options(const int pattern_type, struct grep_opt *o
293 }
294 }
295
311 -static struct grep_opt grep_defaults;
312 -
313 -/*
314 - * Initialize the grep_defaults template with hardcoded defaults.
315 - * We could let the compiler do this, but without C99 initializers
316 - * the code gets unwieldy and unreadable, so...
317 - */
318 -static void init_grep_defaults(void)
319 -{
320 - struct grep_opt *opt = &grep_defaults;
321 -
322 - memset(opt, 0, sizeof(*opt));
323 - opt->relative = 1;
324 - opt->pathname = 1;
325 - opt->regflags = REG_NEWLINE;
326 - opt->max_depth = -1;
327 - opt->pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED;
328 - opt->extended_regexp_option = 0;
329 - strcpy(opt->color_context, "");
330 - strcpy(opt->color_filename, "");
331 - strcpy(opt->color_function, "");
332 - strcpy(opt->color_lineno, "");
333 - strcpy(opt->color_match, GIT_COLOR_BOLD_RED);
334 - strcpy(opt->color_selected, "");
335 - strcpy(opt->color_sep, GIT_COLOR_CYAN);
336 - opt->color = -1;
337 -}
338 -
339 -/*
340 - * Read the configuration file once and store it in
341 - * the grep_defaults template.
342 - */
343 -static int grep_config(const char *var, const char *value, void *cb)
344 -{
345 - struct grep_opt *opt = &grep_defaults;
346 - char *color = NULL;
347 -
348 - if (userdiff_config(var, value) < 0)
349 - return -1;
350 -
351 - if (!strcmp(var, "grep.extendedregexp")) {
352 - if (git_config_bool(var, value))
353 - opt->extended_regexp_option = 1;
354 - else
355 - opt->extended_regexp_option = 0;
356 - return 0;
357 - }
358 -
359 - if (!strcmp(var, "grep.patterntype")) {
360 - opt->pattern_type_option = parse_pattern_type_arg(var, value);
361 - return 0;
362 - }
363 -
364 - if (!strcmp(var, "grep.linenumber")) {
365 - opt->linenum = git_config_bool(var, value);
366 - return 0;
367 - }
368 -
369 - if (!strcmp(var, "color.grep"))
370 - opt->color = git_config_colorbool(var, value);
371 - else if (!strcmp(var, "color.grep.context"))
372 - color = opt->color_context;
373 - else if (!strcmp(var, "color.grep.filename"))
374 - color = opt->color_filename;
375 - else if (!strcmp(var, "color.grep.function"))
376 - color = opt->color_function;
377 - else if (!strcmp(var, "color.grep.linenumber"))
378 - color = opt->color_lineno;
379 - else if (!strcmp(var, "color.grep.match"))
380 - color = opt->color_match;
381 - else if (!strcmp(var, "color.grep.selected"))
382 - color = opt->color_selected;
383 - else if (!strcmp(var, "color.grep.separator"))
384 - color = opt->color_sep;
385 -
386 - if (color) {
387 - if (!value)
388 - return config_error_nonbool(var);
389 - color_parse(value, var, color);
390 - }
391 - return 0;
392 -}
393 -
394 -/*
395 - * Initialize one instance of grep_opt and copy the
396 - * default values from the template we read the configuration
397 - * information in an earlier call to git_config(grep_config).
398 - */
399 -static void grep_init(struct grep_opt *opt, const char *prefix)
400 -{
401 - struct grep_opt *def = &grep_defaults;
402 -
403 - memset(opt, 0, sizeof(*opt));
404 - opt->prefix = prefix;
405 - opt->prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
406 - opt->pattern_tail = &opt->pattern_list;
407 - opt->header_tail = &opt->header_list;
408 -
409 - opt->color = def->color;
410 - opt->extended_regexp_option = def->extended_regexp_option;
411 - opt->pattern_type_option = def->pattern_type_option;
412 - opt->linenum = def->linenum;
413 - opt->max_depth = def->max_depth;
414 - opt->pathname = def->pathname;
415 - opt->regflags = def->regflags;
416 - opt->relative = def->relative;
417 -
418 - strcpy(opt->color_context, def->color_context);
419 - strcpy(opt->color_filename, def->color_filename);
420 - strcpy(opt->color_function, def->color_function);
421 - strcpy(opt->color_lineno, def->color_lineno);
422 - strcpy(opt->color_match, def->color_match);
423 - strcpy(opt->color_selected, def->color_selected);
424 - strcpy(opt->color_sep, def->color_sep);
425 -}
426 -
296 static int grep_cmd_config(const char *var, const char *value, void *cb)
297 {
298 int st = grep_config(var, value, cb);
grep.c
+130
@@ -6,6 +6,136 @@
6 static int grep_source_load(struct grep_source *gs);
7 static int grep_source_is_binary(struct grep_source *gs);
8
9 +static struct grep_opt grep_defaults;
10 +
11 +/*
12 + * Initialize the grep_defaults template with hardcoded defaults.
13 + * We could let the compiler do this, but without C99 initializers
14 + * the code gets unwieldy and unreadable, so...
15 + */
16 +void init_grep_defaults(void)
17 +{
18 + struct grep_opt *opt = &grep_defaults;
19 +
20 + memset(opt, 0, sizeof(*opt));
21 + opt->relative = 1;
22 + opt->pathname = 1;
23 + opt->regflags = REG_NEWLINE;
24 + opt->max_depth = -1;
25 + opt->pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED;
26 + opt->extended_regexp_option = 0;
27 + strcpy(opt->color_context, "");
28 + strcpy(opt->color_filename, "");
29 + strcpy(opt->color_function, "");
30 + strcpy(opt->color_lineno, "");
31 + strcpy(opt->color_match, GIT_COLOR_BOLD_RED);
32 + strcpy(opt->color_selected, "");
33 + strcpy(opt->color_sep, GIT_COLOR_CYAN);
34 + opt->color = -1;
35 +}
36 +
37 +static int parse_pattern_type_arg(const char *opt, const char *arg)
38 +{
39 + if (!strcmp(arg, "default"))
40 + return GREP_PATTERN_TYPE_UNSPECIFIED;
41 + else if (!strcmp(arg, "basic"))
42 + return GREP_PATTERN_TYPE_BRE;
43 + else if (!strcmp(arg, "extended"))
44 + return GREP_PATTERN_TYPE_ERE;
45 + else if (!strcmp(arg, "fixed"))
46 + return GREP_PATTERN_TYPE_FIXED;
47 + else if (!strcmp(arg, "perl"))
48 + return GREP_PATTERN_TYPE_PCRE;
49 + die("bad %s argument: %s", opt, arg);
50 +}
51 +
52 +/*
53 + * Read the configuration file once and store it in
54 + * the grep_defaults template.
55 + */
56 +int grep_config(const char *var, const char *value, void *cb)
57 +{
58 + struct grep_opt *opt = &grep_defaults;
59 + char *color = NULL;
60 +
61 + if (userdiff_config(var, value) < 0)
62 + return -1;
63 +
64 + if (!strcmp(var, "grep.extendedregexp")) {
65 + if (git_config_bool(var, value))
66 + opt->extended_regexp_option = 1;
67 + else
68 + opt->extended_regexp_option = 0;
69 + return 0;
70 + }
71 +
72 + if (!strcmp(var, "grep.patterntype")) {
73 + opt->pattern_type_option = parse_pattern_type_arg(var, value);
74 + return 0;
75 + }
76 +
77 + if (!strcmp(var, "grep.linenumber")) {
78 + opt->linenum = git_config_bool(var, value);
79 + return 0;
80 + }
81 +
82 + if (!strcmp(var, "color.grep"))
83 + opt->color = git_config_colorbool(var, value);
84 + else if (!strcmp(var, "color.grep.context"))
85 + color = opt->color_context;
86 + else if (!strcmp(var, "color.grep.filename"))
87 + color = opt->color_filename;
88 + else if (!strcmp(var, "color.grep.function"))
89 + color = opt->color_function;
90 + else if (!strcmp(var, "color.grep.linenumber"))
91 + color = opt->color_lineno;
92 + else if (!strcmp(var, "color.grep.match"))
93 + color = opt->color_match;
94 + else if (!strcmp(var, "color.grep.selected"))
95 + color = opt->color_selected;
96 + else if (!strcmp(var, "color.grep.separator"))
97 + color = opt->color_sep;
98 +
99 + if (color) {
100 + if (!value)
101 + return config_error_nonbool(var);
102 + color_parse(value, var, color);
103 + }
104 + return 0;
105 +}
106 +
107 +/*
108 + * Initialize one instance of grep_opt and copy the
109 + * default values from the template we read the configuration
110 + * information in an earlier call to git_config(grep_config).
111 + */
112 +void grep_init(struct grep_opt *opt, const char *prefix)
113 +{
114 + struct grep_opt *def = &grep_defaults;
115 +
116 + memset(opt, 0, sizeof(*opt));
117 + opt->prefix = prefix;
118 + opt->prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
119 + opt->pattern_tail = &opt->pattern_list;
120 + opt->header_tail = &opt->header_list;
121 +
122 + opt->color = def->color;
123 + opt->extended_regexp_option = def->extended_regexp_option;
124 + opt->pattern_type_option = def->pattern_type_option;
125 + opt->linenum = def->linenum;
126 + opt->max_depth = def->max_depth;
127 + opt->pathname = def->pathname;
128 + opt->regflags = def->regflags;
129 + opt->relative = def->relative;
130 +
131 + strcpy(opt->color_context, def->color_context);
132 + strcpy(opt->color_filename, def->color_filename);
133 + strcpy(opt->color_function, def->color_function);
134 + strcpy(opt->color_lineno, def->color_lineno);
135 + strcpy(opt->color_match, def->color_match);
136 + strcpy(opt->color_selected, def->color_selected);
137 + strcpy(opt->color_sep, def->color_sep);
138 +}
139
140 static struct grep_pat *create_grep_pat(const char *pat, size_t patlen,
141 const char *origin, int no,
grep.h
+4
@@ -138,6 +138,10 @@ struct grep_opt {
138 void *output_priv;
139 };
140
141 +extern void init_grep_defaults(void);
142 +extern int grep_config(const char *var, const char *value, void *);
143 +extern void grep_init(struct grep_opt *, const char *prefix);
144 +
145 extern void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);
146 extern void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
147 extern void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *);