Merge branch 'es/pretty-describe-more'
[git/debian.git] / trace2 / tr2_cfg.c
blobec9ac1a6efd3f62b4ccae9db8a9d3ac3ceb1c64a
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;
10 static struct strbuf **tr2_cfg_env_vars;
11 static int tr2_cfg_env_vars_count;
12 static int tr2_cfg_env_vars_loaded;
15 * Parse a string containing a comma-delimited list of config keys
16 * or wildcard patterns into a list of strbufs.
18 static int tr2_cfg_load_patterns(void)
20 struct strbuf **s;
21 const char *envvar;
23 if (tr2_cfg_loaded)
24 return tr2_cfg_count_patterns;
25 tr2_cfg_loaded = 1;
27 envvar = tr2_sysenv_get(TR2_SYSENV_CFG_PARAM);
28 if (!envvar || !*envvar)
29 return tr2_cfg_count_patterns;
31 tr2_cfg_patterns = strbuf_split_buf(envvar, strlen(envvar), ',', -1);
32 for (s = tr2_cfg_patterns; *s; s++) {
33 struct strbuf *buf = *s;
35 if (buf->len && buf->buf[buf->len - 1] == ',')
36 strbuf_setlen(buf, buf->len - 1);
37 strbuf_trim_trailing_newline(*s);
38 strbuf_trim(*s);
41 tr2_cfg_count_patterns = s - tr2_cfg_patterns;
42 return tr2_cfg_count_patterns;
45 void tr2_cfg_free_patterns(void)
47 if (tr2_cfg_patterns)
48 strbuf_list_free(tr2_cfg_patterns);
49 tr2_cfg_count_patterns = 0;
50 tr2_cfg_loaded = 0;
54 * Parse a string containing a comma-delimited list of environment variable
55 * names into a list of strbufs.
57 static int tr2_load_env_vars(void)
59 struct strbuf **s;
60 const char *varlist;
62 if (tr2_cfg_env_vars_loaded)
63 return tr2_cfg_env_vars_count;
64 tr2_cfg_env_vars_loaded = 1;
66 varlist = tr2_sysenv_get(TR2_SYSENV_ENV_VARS);
67 if (!varlist || !*varlist)
68 return tr2_cfg_env_vars_count;
70 tr2_cfg_env_vars = strbuf_split_buf(varlist, strlen(varlist), ',', -1);
71 for (s = tr2_cfg_env_vars; *s; s++) {
72 struct strbuf *buf = *s;
74 if (buf->len && buf->buf[buf->len - 1] == ',')
75 strbuf_setlen(buf, buf->len - 1);
76 strbuf_trim_trailing_newline(*s);
77 strbuf_trim(*s);
80 tr2_cfg_env_vars_count = s - tr2_cfg_env_vars;
81 return tr2_cfg_env_vars_count;
84 void tr2_cfg_free_env_vars(void)
86 if (tr2_cfg_env_vars)
87 strbuf_list_free(tr2_cfg_env_vars);
88 tr2_cfg_env_vars_count = 0;
89 tr2_cfg_env_vars_loaded = 0;
92 struct tr2_cfg_data {
93 const char *file;
94 int line;
98 * See if the given config key matches any of our patterns of interest.
100 static int tr2_cfg_cb(const char *key, const char *value, void *d)
102 struct strbuf **s;
103 struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
105 for (s = tr2_cfg_patterns; *s; s++) {
106 struct strbuf *buf = *s;
107 int wm = wildmatch(buf->buf, key, WM_CASEFOLD);
108 if (wm == WM_MATCH) {
109 trace2_def_param_fl(data->file, data->line, key, value);
110 return 0;
114 return 0;
117 void tr2_cfg_list_config_fl(const char *file, int line)
119 struct tr2_cfg_data data = { file, line };
121 if (tr2_cfg_load_patterns() > 0)
122 read_early_config(tr2_cfg_cb, &data);
125 void tr2_list_env_vars_fl(const char *file, int line)
127 struct strbuf **s;
129 if (tr2_load_env_vars() <= 0)
130 return;
132 for (s = tr2_cfg_env_vars; *s; s++) {
133 struct strbuf *buf = *s;
134 const char *val = getenv(buf->buf);
135 if (val && *val)
136 trace2_def_param_fl(file, line, buf->buf, val);
140 void tr2_cfg_set_fl(const char *file, int line, const char *key,
141 const char *value)
143 struct tr2_cfg_data data = { file, line };
145 if (tr2_cfg_load_patterns() > 0)
146 tr2_cfg_cb(key, value, &data);