summaryrefslogtreecommitdiff
path: root/grep.c
diff options
context:
space:
mode:
authorMartin Ågren <martin.agren@gmail.com>2020-11-21 18:31:08 (GMT)
committerJunio C Hamano <gitster@pobox.com>2020-11-21 22:50:33 (GMT)
commit96313423a75fa8d88b6ecd5a15c21a7fbaf9e9be (patch)
tree19f731ed362d59f349c88f420ac5d41c0f9acc5e /grep.c
parent1d3878799f8260968ea9f6a75a92c4daca1da133 (diff)
downloadgit-96313423a75fa8d88b6ecd5a15c21a7fbaf9e9be.zip
git-96313423a75fa8d88b6ecd5a15c21a7fbaf9e9be.tar.gz
git-96313423a75fa8d88b6ecd5a15c21a7fbaf9e9be.tar.bz2
grep: use designated initializers for `grep_defaults`
In 15fabd1bbd ("builtin/grep.c: make configuration callback more reusable", 2012-10-09), we learned to fill a `static struct grep_opt grep_defaults` which we can use as a blueprint for other such structs. At the time, we didn't consider designated initializers to be widely useable, but these days, we do. (See, e.g., cbc0f81d96 ("strbuf: use designated initializers in STRBUF_INIT", 2017-07-10).) Use designated initializers to let the compiler set up the struct and so that we don't need to remember to call `init_grep_defaults()`. Signed-off-by: Martin Ågren <martin.agren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'grep.c')
-rw-r--r--grep.c64
1 files changed, 25 insertions, 39 deletions
diff --git a/grep.c b/grep.c
index b351449..8f2009e 100644
--- a/grep.c
+++ b/grep.c
@@ -14,7 +14,31 @@ static int grep_source_load(struct grep_source *gs);
static int grep_source_is_binary(struct grep_source *gs,
struct index_state *istate);
-static struct grep_opt grep_defaults;
+static void std_output(struct grep_opt *opt, const void *buf, size_t size)
+{
+ fwrite(buf, size, 1, stdout);
+}
+
+static struct grep_opt grep_defaults = {
+ .relative = 1,
+ .pathname = 1,
+ .max_depth = -1,
+ .pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED,
+ .colors = {
+ [GREP_COLOR_CONTEXT] = "",
+ [GREP_COLOR_FILENAME] = "",
+ [GREP_COLOR_FUNCTION] = "",
+ [GREP_COLOR_LINENO] = "",
+ [GREP_COLOR_COLUMNNO] = "",
+ [GREP_COLOR_MATCH_CONTEXT] = GIT_COLOR_BOLD_RED,
+ [GREP_COLOR_MATCH_SELECTED] = GIT_COLOR_BOLD_RED,
+ [GREP_COLOR_SELECTED] = "",
+ [GREP_COLOR_SEP] = GIT_COLOR_CYAN,
+ },
+ .only_matching = 0,
+ .color = -1,
+ .output = std_output,
+};
#ifdef USE_LIBPCRE2
static pcre2_general_context *pcre2_global_context;
@@ -42,49 +66,11 @@ static const char *color_grep_slots[] = {
[GREP_COLOR_SEP] = "separator",
};
-static void std_output(struct grep_opt *opt, const void *buf, size_t size)
-{
- fwrite(buf, size, 1, stdout);
-}
-
static void color_set(char *dst, const char *color_bytes)
{
xsnprintf(dst, COLOR_MAXLEN, "%s", color_bytes);
}
-/*
- * Initialize the grep_defaults template with hardcoded defaults.
- * We could let the compiler do this, but without C99 initializers
- * the code gets unwieldy and unreadable, so...
- */
-void init_grep_defaults(void)
-{
- struct grep_opt *opt = &grep_defaults;
- static int run_once;
-
- if (run_once)
- return;
- run_once++;
-
- memset(opt, 0, sizeof(*opt));
- opt->relative = 1;
- opt->pathname = 1;
- opt->max_depth = -1;
- opt->pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED;
- color_set(opt->colors[GREP_COLOR_CONTEXT], "");
- color_set(opt->colors[GREP_COLOR_FILENAME], "");
- color_set(opt->colors[GREP_COLOR_FUNCTION], "");
- color_set(opt->colors[GREP_COLOR_LINENO], "");
- color_set(opt->colors[GREP_COLOR_COLUMNNO], "");
- color_set(opt->colors[GREP_COLOR_MATCH_CONTEXT], GIT_COLOR_BOLD_RED);
- color_set(opt->colors[GREP_COLOR_MATCH_SELECTED], GIT_COLOR_BOLD_RED);
- color_set(opt->colors[GREP_COLOR_SELECTED], "");
- color_set(opt->colors[GREP_COLOR_SEP], GIT_COLOR_CYAN);
- opt->only_matching = 0;
- opt->color = -1;
- opt->output = std_output;
-}
-
static int parse_pattern_type_arg(const char *opt, const char *arg)
{
if (!strcmp(arg, "default"))