Merge branch 'gs/commit-graph-path-filter'
[git] / trace2 / tr2_cfg.c
1 #include "cache.h"
2 #include "config.h"
3 #include "trace2/tr2_cfg.h"
4 #include "trace2/tr2_sysenv.h"
5
6 static struct strbuf **tr2_cfg_patterns;
7 static int tr2_cfg_count_patterns;
8 static int tr2_cfg_loaded;
9
10 static struct strbuf **tr2_cfg_env_vars;
11 static int tr2_cfg_env_vars_count;
12 static int tr2_cfg_env_vars_loaded;
13
14 /*
15  * Parse a string containing a comma-delimited list of config keys
16  * or wildcard patterns into a list of strbufs.
17  */
18 static int tr2_cfg_load_patterns(void)
19 {
20         struct strbuf **s;
21         const char *envvar;
22
23         if (tr2_cfg_loaded)
24                 return tr2_cfg_count_patterns;
25         tr2_cfg_loaded = 1;
26
27         envvar = tr2_sysenv_get(TR2_SYSENV_CFG_PARAM);
28         if (!envvar || !*envvar)
29                 return tr2_cfg_count_patterns;
30
31         tr2_cfg_patterns = strbuf_split_buf(envvar, strlen(envvar), ',', -1);
32         for (s = tr2_cfg_patterns; *s; s++) {
33                 struct strbuf *buf = *s;
34
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);
39         }
40
41         tr2_cfg_count_patterns = s - tr2_cfg_patterns;
42         return tr2_cfg_count_patterns;
43 }
44
45 void tr2_cfg_free_patterns(void)
46 {
47         if (tr2_cfg_patterns)
48                 strbuf_list_free(tr2_cfg_patterns);
49         tr2_cfg_count_patterns = 0;
50         tr2_cfg_loaded = 0;
51 }
52
53 /*
54  * Parse a string containing a comma-delimited list of environment variable
55  * names into a list of strbufs.
56  */
57 static int tr2_load_env_vars(void)
58 {
59         struct strbuf **s;
60         const char *varlist;
61
62         if (tr2_cfg_env_vars_loaded)
63                 return tr2_cfg_env_vars_count;
64         tr2_cfg_env_vars_loaded = 1;
65
66         varlist = tr2_sysenv_get(TR2_SYSENV_ENV_VARS);
67         if (!varlist || !*varlist)
68                 return tr2_cfg_env_vars_count;
69
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;
73
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);
78         }
79
80         tr2_cfg_env_vars_count = s - tr2_cfg_env_vars;
81         return tr2_cfg_env_vars_count;
82 }
83
84 void tr2_cfg_free_env_vars(void)
85 {
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;
90 }
91
92 struct tr2_cfg_data {
93         const char *file;
94         int line;
95 };
96
97 /*
98  * See if the given config key matches any of our patterns of interest.
99  */
100 static int tr2_cfg_cb(const char *key, const char *value, void *d)
101 {
102         struct strbuf **s;
103         struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
104
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;
111                 }
112         }
113
114         return 0;
115 }
116
117 void tr2_cfg_list_config_fl(const char *file, int line)
118 {
119         struct tr2_cfg_data data = { file, line };
120
121         if (tr2_cfg_load_patterns() > 0)
122                 read_early_config(tr2_cfg_cb, &data);
123 }
124
125 void tr2_list_env_vars_fl(const char *file, int line)
126 {
127         struct strbuf **s;
128
129         if (tr2_load_env_vars() <= 0)
130                 return;
131
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);
137         }
138 }
139
140 void tr2_cfg_set_fl(const char *file, int line, const char *key,
141                     const char *value)
142 {
143         struct tr2_cfg_data data = { file, line };
144
145         if (tr2_cfg_load_patterns() > 0)
146                 tr2_cfg_cb(key, value, &data);
147 }