]> git.ipfire.org Git - thirdparty/git.git/blob - trace2/tr2_cfg.c
mv: fix error for moving directory to another
[thirdparty/git.git] / trace2 / tr2_cfg.c
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
8 static struct strbuf **tr2_cfg_patterns;
9 static int tr2_cfg_count_patterns;
10 static int tr2_cfg_loaded;
11
12 static struct strbuf **tr2_cfg_env_vars;
13 static int tr2_cfg_env_vars_count;
14 static int tr2_cfg_env_vars_loaded;
15
16 /*
17 * Parse a string containing a comma-delimited list of config keys
18 * or wildcard patterns into a list of strbufs.
19 */
20 static int tr2_cfg_load_patterns(void)
21 {
22 struct strbuf **s;
23 const char *envvar;
24
25 if (tr2_cfg_loaded)
26 return tr2_cfg_count_patterns;
27 tr2_cfg_loaded = 1;
28
29 envvar = tr2_sysenv_get(TR2_SYSENV_CFG_PARAM);
30 if (!envvar || !*envvar)
31 return tr2_cfg_count_patterns;
32
33 tr2_cfg_patterns = strbuf_split_buf(envvar, strlen(envvar), ',', -1);
34 for (s = tr2_cfg_patterns; *s; s++) {
35 struct strbuf *buf = *s;
36
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);
41 }
42
43 tr2_cfg_count_patterns = s - tr2_cfg_patterns;
44 return tr2_cfg_count_patterns;
45 }
46
47 void tr2_cfg_free_patterns(void)
48 {
49 if (tr2_cfg_patterns)
50 strbuf_list_free(tr2_cfg_patterns);
51 tr2_cfg_count_patterns = 0;
52 tr2_cfg_loaded = 0;
53 }
54
55 /*
56 * Parse a string containing a comma-delimited list of environment variable
57 * names into a list of strbufs.
58 */
59 static int tr2_load_env_vars(void)
60 {
61 struct strbuf **s;
62 const char *varlist;
63
64 if (tr2_cfg_env_vars_loaded)
65 return tr2_cfg_env_vars_count;
66 tr2_cfg_env_vars_loaded = 1;
67
68 varlist = tr2_sysenv_get(TR2_SYSENV_ENV_VARS);
69 if (!varlist || !*varlist)
70 return tr2_cfg_env_vars_count;
71
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;
75
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);
80 }
81
82 tr2_cfg_env_vars_count = s - tr2_cfg_env_vars;
83 return tr2_cfg_env_vars_count;
84 }
85
86 void tr2_cfg_free_env_vars(void)
87 {
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;
92 }
93
94 struct tr2_cfg_data {
95 const char *file;
96 int line;
97 };
98
99 /*
100 * See if the given config key matches any of our patterns of interest.
101 */
102 static int tr2_cfg_cb(const char *key, const char *value, void *d)
103 {
104 struct strbuf **s;
105 struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
106
107 for (s = tr2_cfg_patterns; *s; s++) {
108 struct strbuf *buf = *s;
109 int wm = wildmatch(buf->buf, key, WM_CASEFOLD);
110 if (wm == WM_MATCH) {
111 trace2_def_param_fl(data->file, data->line, key, value);
112 return 0;
113 }
114 }
115
116 return 0;
117 }
118
119 void tr2_cfg_list_config_fl(const char *file, int line)
120 {
121 struct tr2_cfg_data data = { file, line };
122
123 if (tr2_cfg_load_patterns() > 0)
124 read_early_config(tr2_cfg_cb, &data);
125 }
126
127 void tr2_list_env_vars_fl(const char *file, int line)
128 {
129 struct strbuf **s;
130
131 if (tr2_load_env_vars() <= 0)
132 return;
133
134 for (s = tr2_cfg_env_vars; *s; s++) {
135 struct strbuf *buf = *s;
136 const char *val = getenv(buf->buf);
137 if (val && *val)
138 trace2_def_param_fl(file, line, buf->buf, val);
139 }
140 }
141
142 void tr2_cfg_set_fl(const char *file, int line, const char *key,
143 const char *value)
144 {
145 struct tr2_cfg_data data = { file, line };
146
147 if (tr2_cfg_load_patterns() > 0)
148 tr2_cfg_cb(key, value, &data);
149 }