3 #include "trace2/tr2_cfg.h"
4 #include "trace2/tr2_sysenv.h"
6 static struct strbuf
**tr2_cfg_patterns
;
7 static int tr2_cfg_count_patterns
;
8 static int tr2_cfg_loaded
;
10 static struct strbuf
**tr2_cfg_env_vars
;
11 static int tr2_cfg_env_vars_count
;
12 static int tr2_cfg_env_vars_loaded
;
15 * Parse a string containing a comma-delimited list of config keys
16 * or wildcard patterns into a list of strbufs.
18 static int tr2_cfg_load_patterns(void)
24 return tr2_cfg_count_patterns
;
27 envvar
= tr2_sysenv_get(TR2_SYSENV_CFG_PARAM
);
28 if (!envvar
|| !*envvar
)
29 return tr2_cfg_count_patterns
;
31 tr2_cfg_patterns
= strbuf_split_buf(envvar
, strlen(envvar
), ',', -1);
32 for (s
= tr2_cfg_patterns
; *s
; s
++) {
33 struct strbuf
*buf
= *s
;
35 if (buf
->len
&& buf
->buf
[buf
->len
- 1] == ',')
36 strbuf_setlen(buf
, buf
->len
- 1);
37 strbuf_trim_trailing_newline(*s
);
41 tr2_cfg_count_patterns
= s
- tr2_cfg_patterns
;
42 return tr2_cfg_count_patterns
;
45 void tr2_cfg_free_patterns(void)
48 strbuf_list_free(tr2_cfg_patterns
);
49 tr2_cfg_count_patterns
= 0;
54 * Parse a string containing a comma-delimited list of environment variable
55 * names into a list of strbufs.
57 static int tr2_load_env_vars(void)
62 if (tr2_cfg_env_vars_loaded
)
63 return tr2_cfg_env_vars_count
;
64 tr2_cfg_env_vars_loaded
= 1;
66 varlist
= tr2_sysenv_get(TR2_SYSENV_ENV_VARS
);
67 if (!varlist
|| !*varlist
)
68 return tr2_cfg_env_vars_count
;
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
;
74 if (buf
->len
&& buf
->buf
[buf
->len
- 1] == ',')
75 strbuf_setlen(buf
, buf
->len
- 1);
76 strbuf_trim_trailing_newline(*s
);
80 tr2_cfg_env_vars_count
= s
- tr2_cfg_env_vars
;
81 return tr2_cfg_env_vars_count
;
84 void tr2_cfg_free_env_vars(void)
87 strbuf_list_free(tr2_cfg_env_vars
);
88 tr2_cfg_env_vars_count
= 0;
89 tr2_cfg_env_vars_loaded
= 0;
98 * See if the given config key matches any of our patterns of interest.
100 static int tr2_cfg_cb(const char *key
, const char *value
, void *d
)
103 struct tr2_cfg_data
*data
= (struct tr2_cfg_data
*)d
;
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
);
117 void tr2_cfg_list_config_fl(const char *file
, int line
)
119 struct tr2_cfg_data data
= { file
, line
};
121 if (tr2_cfg_load_patterns() > 0)
122 read_early_config(tr2_cfg_cb
, &data
);
125 void tr2_list_env_vars_fl(const char *file
, int line
)
129 if (tr2_load_env_vars() <= 0)
132 for (s
= tr2_cfg_env_vars
; *s
; s
++) {
133 struct strbuf
*buf
= *s
;
134 const char *val
= getenv(buf
->buf
);
136 trace2_def_param_fl(file
, line
, buf
->buf
, val
);
140 void tr2_cfg_set_fl(const char *file
, int line
, const char *key
,
143 struct tr2_cfg_data data
= { file
, line
};
145 if (tr2_cfg_load_patterns() > 0)
146 tr2_cfg_cb(key
, value
, &data
);