1 #include "git-compat-util.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)
26 return tr2_cfg_count_patterns
;
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
);
43 tr2_cfg_count_patterns
= s
- tr2_cfg_patterns
;
44 return tr2_cfg_count_patterns
;
47 void tr2_cfg_free_patterns(void)
50 strbuf_list_free(tr2_cfg_patterns
);
51 tr2_cfg_count_patterns
= 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)
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
);
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)
89 strbuf_list_free(tr2_cfg_env_vars
);
90 tr2_cfg_env_vars_count
= 0;
91 tr2_cfg_env_vars_loaded
= 0;
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
, void *d
)
105 struct tr2_cfg_data
*data
= (struct tr2_cfg_data
*)d
;
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
);
119 void tr2_cfg_list_config_fl(const char *file
, int line
)
121 struct tr2_cfg_data data
= { file
, line
};
123 if (tr2_cfg_load_patterns() > 0)
124 read_early_config(tr2_cfg_cb
, &data
);
127 void tr2_list_env_vars_fl(const char *file
, int line
)
131 if (tr2_load_env_vars() <= 0)
134 for (s
= tr2_cfg_env_vars
; *s
; s
++) {
135 struct strbuf
*buf
= *s
;
136 const char *val
= getenv(buf
->buf
);
138 trace2_def_param_fl(file
, line
, buf
->buf
, val
);
142 void tr2_cfg_set_fl(const char *file
, int line
, const char *key
,
145 struct tr2_cfg_data data
= { file
, line
};
147 if (tr2_cfg_load_patterns() > 0)
148 tr2_cfg_cb(key
, value
, &data
);