t: add tests for switch
[git.git] / trace2 / tr2_cfg.c
blobb329921ac5a583567485f03d69dbfa70ab8d680a
1 #include "cache.h"
2 #include "config.h"
3 #include "tr2_cfg.h"
5 #define TR2_ENVVAR_CFG_PARAM "GIT_TR2_CONFIG_PARAMS"
7 static struct strbuf **tr2_cfg_patterns;
8 static int tr2_cfg_count_patterns;
9 static int tr2_cfg_loaded;
12 * Parse a string containing a comma-delimited list of config keys
13 * or wildcard patterns into a list of strbufs.
15 static int tr2_cfg_load_patterns(void)
17 struct strbuf **s;
18 const char *envvar;
20 if (tr2_cfg_loaded)
21 return tr2_cfg_count_patterns;
22 tr2_cfg_loaded = 1;
24 envvar = getenv(TR2_ENVVAR_CFG_PARAM);
25 if (!envvar || !*envvar)
26 return tr2_cfg_count_patterns;
28 tr2_cfg_patterns = strbuf_split_buf(envvar, strlen(envvar), ',', -1);
29 for (s = tr2_cfg_patterns; *s; s++) {
30 struct strbuf *buf = *s;
32 if (buf->len && buf->buf[buf->len - 1] == ',')
33 strbuf_setlen(buf, buf->len - 1);
34 strbuf_trim_trailing_newline(*s);
35 strbuf_trim(*s);
38 tr2_cfg_count_patterns = s - tr2_cfg_patterns;
39 return tr2_cfg_count_patterns;
42 void tr2_cfg_free_patterns(void)
44 if (tr2_cfg_patterns)
45 strbuf_list_free(tr2_cfg_patterns);
46 tr2_cfg_count_patterns = 0;
47 tr2_cfg_loaded = 0;
50 struct tr2_cfg_data {
51 const char *file;
52 int line;
56 * See if the given config key matches any of our patterns of interest.
58 static int tr2_cfg_cb(const char *key, const char *value, void *d)
60 struct strbuf **s;
61 struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
63 for (s = tr2_cfg_patterns; *s; s++) {
64 struct strbuf *buf = *s;
65 int wm = wildmatch(buf->buf, key, WM_CASEFOLD);
66 if (wm == WM_MATCH) {
67 trace2_def_param_fl(data->file, data->line, key, value);
68 return 0;
72 return 0;
75 void tr2_cfg_list_config_fl(const char *file, int line)
77 struct tr2_cfg_data data = { file, line };
79 if (tr2_cfg_load_patterns() > 0)
80 read_early_config(tr2_cfg_cb, &data);
83 void tr2_cfg_set_fl(const char *file, int line, const char *key,
84 const char *value)
86 struct tr2_cfg_data data = { file, line };
88 if (tr2_cfg_load_patterns() > 0)
89 tr2_cfg_cb(key, value, &data);