Merge branch 'mh/doc-credential-helpers'
[git/debian.git] / trace2 / tr2_cfg.c
blobd96d908bb9df6fddb22e4dbecfa07882049bdf78
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"
7 #include "wildmatch.h"
9 static struct strbuf **tr2_cfg_patterns;
10 static int tr2_cfg_count_patterns;
11 static int tr2_cfg_loaded;
13 static struct strbuf **tr2_cfg_env_vars;
14 static int tr2_cfg_env_vars_count;
15 static int tr2_cfg_env_vars_loaded;
18 * Parse a string containing a comma-delimited list of config keys
19 * or wildcard patterns into a list of strbufs.
21 static int tr2_cfg_load_patterns(void)
23 struct strbuf **s;
24 const char *envvar;
26 if (tr2_cfg_loaded)
27 return tr2_cfg_count_patterns;
28 tr2_cfg_loaded = 1;
30 envvar = tr2_sysenv_get(TR2_SYSENV_CFG_PARAM);
31 if (!envvar || !*envvar)
32 return tr2_cfg_count_patterns;
34 tr2_cfg_patterns = strbuf_split_buf(envvar, strlen(envvar), ',', -1);
35 for (s = tr2_cfg_patterns; *s; s++) {
36 struct strbuf *buf = *s;
38 if (buf->len && buf->buf[buf->len - 1] == ',')
39 strbuf_setlen(buf, buf->len - 1);
40 strbuf_trim_trailing_newline(*s);
41 strbuf_trim(*s);
44 tr2_cfg_count_patterns = s - tr2_cfg_patterns;
45 return tr2_cfg_count_patterns;
48 void tr2_cfg_free_patterns(void)
50 if (tr2_cfg_patterns)
51 strbuf_list_free(tr2_cfg_patterns);
52 tr2_cfg_count_patterns = 0;
53 tr2_cfg_loaded = 0;
57 * Parse a string containing a comma-delimited list of environment variable
58 * names into a list of strbufs.
60 static int tr2_load_env_vars(void)
62 struct strbuf **s;
63 const char *varlist;
65 if (tr2_cfg_env_vars_loaded)
66 return tr2_cfg_env_vars_count;
67 tr2_cfg_env_vars_loaded = 1;
69 varlist = tr2_sysenv_get(TR2_SYSENV_ENV_VARS);
70 if (!varlist || !*varlist)
71 return tr2_cfg_env_vars_count;
73 tr2_cfg_env_vars = strbuf_split_buf(varlist, strlen(varlist), ',', -1);
74 for (s = tr2_cfg_env_vars; *s; s++) {
75 struct strbuf *buf = *s;
77 if (buf->len && buf->buf[buf->len - 1] == ',')
78 strbuf_setlen(buf, buf->len - 1);
79 strbuf_trim_trailing_newline(*s);
80 strbuf_trim(*s);
83 tr2_cfg_env_vars_count = s - tr2_cfg_env_vars;
84 return tr2_cfg_env_vars_count;
87 void tr2_cfg_free_env_vars(void)
89 if (tr2_cfg_env_vars)
90 strbuf_list_free(tr2_cfg_env_vars);
91 tr2_cfg_env_vars_count = 0;
92 tr2_cfg_env_vars_loaded = 0;
95 struct tr2_cfg_data {
96 const char *file;
97 int line;
101 * See if the given config key matches any of our patterns of interest.
103 static int tr2_cfg_cb(const char *key, const char *value,
104 const struct config_context *ctx, void *d)
106 struct strbuf **s;
107 struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
109 for (s = tr2_cfg_patterns; *s; s++) {
110 struct strbuf *buf = *s;
111 int wm = wildmatch(buf->buf, key, WM_CASEFOLD);
112 if (wm == WM_MATCH) {
113 trace2_def_param_fl(data->file, data->line, key, value,
114 ctx->kvi);
115 return 0;
119 return 0;
122 void tr2_cfg_list_config_fl(const char *file, int line)
124 struct tr2_cfg_data data = { file, line };
126 if (tr2_cfg_load_patterns() > 0)
127 read_early_config(tr2_cfg_cb, &data);
130 void tr2_list_env_vars_fl(const char *file, int line)
132 struct key_value_info kvi = KVI_INIT;
133 struct strbuf **s;
135 kvi_from_param(&kvi);
136 if (tr2_load_env_vars() <= 0)
137 return;
139 for (s = tr2_cfg_env_vars; *s; s++) {
140 struct strbuf *buf = *s;
141 const char *val = getenv(buf->buf);
142 if (val && *val)
143 trace2_def_param_fl(file, line, buf->buf, val, &kvi);
147 void tr2_cfg_set_fl(const char *file, int line, const char *key,
148 const char *value)
150 struct key_value_info kvi = KVI_INIT;
151 struct config_context ctx = {
152 .kvi = &kvi,
154 struct tr2_cfg_data data = { file, line };
156 if (tr2_cfg_load_patterns() > 0)
157 tr2_cfg_cb(key, value, &ctx, &data);