1 #include "git-compat-util.h"
5 #include "trace2/tr2_cfg.h"
6 #include "trace2/tr2_sysenv.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)
27 return tr2_cfg_count_patterns
;
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
);
44 tr2_cfg_count_patterns
= s
- tr2_cfg_patterns
;
45 return tr2_cfg_count_patterns
;
48 void tr2_cfg_free_patterns(void)
51 strbuf_list_free(tr2_cfg_patterns
);
52 tr2_cfg_count_patterns
= 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)
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
);
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)
90 strbuf_list_free(tr2_cfg_env_vars
);
91 tr2_cfg_env_vars_count
= 0;
92 tr2_cfg_env_vars_loaded
= 0;
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
)
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
,
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
;
135 kvi_from_param(&kvi
);
136 if (tr2_load_env_vars() <= 0)
139 for (s
= tr2_cfg_env_vars
; *s
; s
++) {
140 struct strbuf
*buf
= *s
;
141 const char *val
= getenv(buf
->buf
);
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
,
150 struct key_value_info kvi
= KVI_INIT
;
151 struct config_context ctx
= {
154 struct tr2_cfg_data data
= { file
, line
};
156 if (tr2_cfg_load_patterns() > 0)
157 tr2_cfg_cb(key
, value
, &ctx
, &data
);