1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
7 #include "trace2/tr2_cfg.h"
8 #include "trace2/tr2_sysenv.h"
11 static struct strbuf
**tr2_cfg_patterns
;
12 static int tr2_cfg_count_patterns
;
13 static int tr2_cfg_loaded
;
15 static struct strbuf
**tr2_cfg_env_vars
;
16 static int tr2_cfg_env_vars_count
;
17 static int tr2_cfg_env_vars_loaded
;
20 * Parse a string containing a comma-delimited list of config keys
21 * or wildcard patterns into a list of strbufs.
23 static int tr2_cfg_load_patterns(void)
29 return tr2_cfg_count_patterns
;
32 envvar
= tr2_sysenv_get(TR2_SYSENV_CFG_PARAM
);
33 if (!envvar
|| !*envvar
)
34 return tr2_cfg_count_patterns
;
36 tr2_cfg_patterns
= strbuf_split_buf(envvar
, strlen(envvar
), ',', -1);
37 for (s
= tr2_cfg_patterns
; *s
; s
++) {
38 struct strbuf
*buf
= *s
;
40 if (buf
->len
&& buf
->buf
[buf
->len
- 1] == ',')
41 strbuf_setlen(buf
, buf
->len
- 1);
42 strbuf_trim_trailing_newline(*s
);
46 tr2_cfg_count_patterns
= s
- tr2_cfg_patterns
;
47 return tr2_cfg_count_patterns
;
50 void tr2_cfg_free_patterns(void)
53 strbuf_list_free(tr2_cfg_patterns
);
54 tr2_cfg_count_patterns
= 0;
59 * Parse a string containing a comma-delimited list of environment variable
60 * names into a list of strbufs.
62 static int tr2_load_env_vars(void)
67 if (tr2_cfg_env_vars_loaded
)
68 return tr2_cfg_env_vars_count
;
69 tr2_cfg_env_vars_loaded
= 1;
71 varlist
= tr2_sysenv_get(TR2_SYSENV_ENV_VARS
);
72 if (!varlist
|| !*varlist
)
73 return tr2_cfg_env_vars_count
;
75 tr2_cfg_env_vars
= strbuf_split_buf(varlist
, strlen(varlist
), ',', -1);
76 for (s
= tr2_cfg_env_vars
; *s
; s
++) {
77 struct strbuf
*buf
= *s
;
79 if (buf
->len
&& buf
->buf
[buf
->len
- 1] == ',')
80 strbuf_setlen(buf
, buf
->len
- 1);
81 strbuf_trim_trailing_newline(*s
);
85 tr2_cfg_env_vars_count
= s
- tr2_cfg_env_vars
;
86 return tr2_cfg_env_vars_count
;
89 void tr2_cfg_free_env_vars(void)
92 strbuf_list_free(tr2_cfg_env_vars
);
93 tr2_cfg_env_vars_count
= 0;
94 tr2_cfg_env_vars_loaded
= 0;
103 * See if the given config key matches any of our patterns of interest.
105 static int tr2_cfg_cb(const char *key
, const char *value
,
106 const struct config_context
*ctx
, void *d
)
109 struct tr2_cfg_data
*data
= (struct tr2_cfg_data
*)d
;
111 for (s
= tr2_cfg_patterns
; *s
; s
++) {
112 struct strbuf
*buf
= *s
;
113 int wm
= wildmatch(buf
->buf
, key
, WM_CASEFOLD
);
114 if (wm
== WM_MATCH
) {
115 trace2_def_param_fl(data
->file
, data
->line
, key
, value
,
124 void tr2_cfg_list_config_fl(const char *file
, int line
)
126 struct tr2_cfg_data data
= { file
, line
};
128 if (tr2_cfg_load_patterns() > 0)
129 read_early_config(the_repository
, tr2_cfg_cb
, &data
);
132 void tr2_list_env_vars_fl(const char *file
, int line
)
134 struct key_value_info kvi
= KVI_INIT
;
137 kvi_from_param(&kvi
);
138 if (tr2_load_env_vars() <= 0)
141 for (s
= tr2_cfg_env_vars
; *s
; s
++) {
142 struct strbuf
*buf
= *s
;
143 const char *val
= getenv(buf
->buf
);
145 trace2_def_param_fl(file
, line
, buf
->buf
, val
, &kvi
);
149 void tr2_cfg_set_fl(const char *file
, int line
, const char *key
,
152 struct key_value_info kvi
= KVI_INIT
;
153 struct config_context ctx
= {
156 struct tr2_cfg_data data
= { file
, line
};
158 if (tr2_cfg_load_patterns() > 0)
159 tr2_cfg_cb(key
, value
, &ctx
, &data
);