trace2: plumb config kvi
[git.git] / trace2 / tr2_cfg.c
blob733d9d2872ad8645e2a07f86149c4459a8bafe70
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "strbuf.h"
4 #include "trace2.h"
5 #include "trace2/tr2_cfg.h"
6 #include "trace2/tr2_sysenv.h"
8 static struct strbuf **tr2_cfg_patterns;
9 static int tr2_cfg_count_patterns;
10 static int tr2_cfg_loaded;
12 static struct strbuf **tr2_cfg_env_vars;
13 static int tr2_cfg_env_vars_count;
14 static int tr2_cfg_env_vars_loaded;
17 * Parse a string containing a comma-delimited list of config keys
18 * or wildcard patterns into a list of strbufs.
20 static int tr2_cfg_load_patterns(void)
22 struct strbuf **s;
23 const char *envvar;
25 if (tr2_cfg_loaded)
26 return tr2_cfg_count_patterns;
27 tr2_cfg_loaded = 1;
29 envvar = tr2_sysenv_get(TR2_SYSENV_CFG_PARAM);
30 if (!envvar || !*envvar)
31 return tr2_cfg_count_patterns;
33 tr2_cfg_patterns = strbuf_split_buf(envvar, strlen(envvar), ',', -1);
34 for (s = tr2_cfg_patterns; *s; s++) {
35 struct strbuf *buf = *s;
37 if (buf->len && buf->buf[buf->len - 1] == ',')
38 strbuf_setlen(buf, buf->len - 1);
39 strbuf_trim_trailing_newline(*s);
40 strbuf_trim(*s);
43 tr2_cfg_count_patterns = s - tr2_cfg_patterns;
44 return tr2_cfg_count_patterns;
47 void tr2_cfg_free_patterns(void)
49 if (tr2_cfg_patterns)
50 strbuf_list_free(tr2_cfg_patterns);
51 tr2_cfg_count_patterns = 0;
52 tr2_cfg_loaded = 0;
56 * Parse a string containing a comma-delimited list of environment variable
57 * names into a list of strbufs.
59 static int tr2_load_env_vars(void)
61 struct strbuf **s;
62 const char *varlist;
64 if (tr2_cfg_env_vars_loaded)
65 return tr2_cfg_env_vars_count;
66 tr2_cfg_env_vars_loaded = 1;
68 varlist = tr2_sysenv_get(TR2_SYSENV_ENV_VARS);
69 if (!varlist || !*varlist)
70 return tr2_cfg_env_vars_count;
72 tr2_cfg_env_vars = strbuf_split_buf(varlist, strlen(varlist), ',', -1);
73 for (s = tr2_cfg_env_vars; *s; s++) {
74 struct strbuf *buf = *s;
76 if (buf->len && buf->buf[buf->len - 1] == ',')
77 strbuf_setlen(buf, buf->len - 1);
78 strbuf_trim_trailing_newline(*s);
79 strbuf_trim(*s);
82 tr2_cfg_env_vars_count = s - tr2_cfg_env_vars;
83 return tr2_cfg_env_vars_count;
86 void tr2_cfg_free_env_vars(void)
88 if (tr2_cfg_env_vars)
89 strbuf_list_free(tr2_cfg_env_vars);
90 tr2_cfg_env_vars_count = 0;
91 tr2_cfg_env_vars_loaded = 0;
94 struct tr2_cfg_data {
95 const char *file;
96 int line;
100 * See if the given config key matches any of our patterns of interest.
102 static int tr2_cfg_cb(const char *key, const char *value,
103 const struct config_context *ctx, void *d)
105 struct strbuf **s;
106 struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
108 for (s = tr2_cfg_patterns; *s; s++) {
109 struct strbuf *buf = *s;
110 int wm = wildmatch(buf->buf, key, WM_CASEFOLD);
111 if (wm == WM_MATCH) {
112 trace2_def_param_fl(data->file, data->line, key, value,
113 ctx->kvi);
114 return 0;
118 return 0;
121 void tr2_cfg_list_config_fl(const char *file, int line)
123 struct tr2_cfg_data data = { file, line };
125 if (tr2_cfg_load_patterns() > 0)
126 read_early_config(tr2_cfg_cb, &data);
129 void tr2_list_env_vars_fl(const char *file, int line)
131 struct key_value_info kvi = KVI_INIT;
132 struct strbuf **s;
134 kvi_from_param(&kvi);
135 if (tr2_load_env_vars() <= 0)
136 return;
138 for (s = tr2_cfg_env_vars; *s; s++) {
139 struct strbuf *buf = *s;
140 const char *val = getenv(buf->buf);
141 if (val && *val)
142 trace2_def_param_fl(file, line, buf->buf, val, &kvi);
146 void tr2_cfg_set_fl(const char *file, int line, const char *key,
147 const char *value)
149 struct key_value_info kvi = KVI_INIT;
150 struct config_context ctx = {
151 .kvi = &kvi,
153 struct tr2_cfg_data data = { file, line };
155 if (tr2_cfg_load_patterns() > 0)
156 tr2_cfg_cb(key, value, &ctx, &data);