The eighth batch for 2.26
[alt-git.git] / trace2 / tr2_cfg.c
blobcaa7f06948ab97c09ee9adf82a9588fcc07d6e52
1 #include "cache.h"
2 #include "config.h"
3 #include "trace2/tr2_cfg.h"
4 #include "trace2/tr2_sysenv.h"
6 static struct strbuf **tr2_cfg_patterns;
7 static int tr2_cfg_count_patterns;
8 static int tr2_cfg_loaded;
11 * Parse a string containing a comma-delimited list of config keys
12 * or wildcard patterns into a list of strbufs.
14 static int tr2_cfg_load_patterns(void)
16 struct strbuf **s;
17 const char *envvar;
19 if (tr2_cfg_loaded)
20 return tr2_cfg_count_patterns;
21 tr2_cfg_loaded = 1;
23 envvar = tr2_sysenv_get(TR2_SYSENV_CFG_PARAM);
24 if (!envvar || !*envvar)
25 return tr2_cfg_count_patterns;
27 tr2_cfg_patterns = strbuf_split_buf(envvar, strlen(envvar), ',', -1);
28 for (s = tr2_cfg_patterns; *s; s++) {
29 struct strbuf *buf = *s;
31 if (buf->len && buf->buf[buf->len - 1] == ',')
32 strbuf_setlen(buf, buf->len - 1);
33 strbuf_trim_trailing_newline(*s);
34 strbuf_trim(*s);
37 tr2_cfg_count_patterns = s - tr2_cfg_patterns;
38 return tr2_cfg_count_patterns;
41 void tr2_cfg_free_patterns(void)
43 if (tr2_cfg_patterns)
44 strbuf_list_free(tr2_cfg_patterns);
45 tr2_cfg_count_patterns = 0;
46 tr2_cfg_loaded = 0;
49 struct tr2_cfg_data {
50 const char *file;
51 int line;
55 * See if the given config key matches any of our patterns of interest.
57 static int tr2_cfg_cb(const char *key, const char *value, void *d)
59 struct strbuf **s;
60 struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
62 for (s = tr2_cfg_patterns; *s; s++) {
63 struct strbuf *buf = *s;
64 int wm = wildmatch(buf->buf, key, WM_CASEFOLD);
65 if (wm == WM_MATCH) {
66 trace2_def_param_fl(data->file, data->line, key, value);
67 return 0;
71 return 0;
74 void tr2_cfg_list_config_fl(const char *file, int line)
76 struct tr2_cfg_data data = { file, line };
78 if (tr2_cfg_load_patterns() > 0)
79 read_early_config(tr2_cfg_cb, &data);
82 void tr2_cfg_set_fl(const char *file, int line, const char *key,
83 const char *value)
85 struct tr2_cfg_data data = { file, line };
87 if (tr2_cfg_load_patterns() > 0)
88 tr2_cfg_cb(key, value, &data);