17 #define DEBUG_CACHE_DIR ".debug"
22 static FILE *config_file;
23 static const char *config_file_name;
24 static int config_linenr;
25 static int config_file_eof;
27 static const char *config_exclusive_filename;
29 static int get_next_char(
void)
35 if ((f = config_file) !=
NULL) {
55 static char *parse_value(
void)
57 static char value[1024];
58 int quote = 0, comment = 0, space = 0;
62 int c = get_next_char();
64 if (len >=
sizeof(value) - 1)
79 if (c ==
';' || c ==
'#') {
121 static inline int iskeychar(
int c)
123 return isalnum(c) || c ==
'-' || c ==
'_';
143 while (c ==
' ' || c ==
'\t')
150 value = parse_value();
154 return fn(name, value, data);
157 static int get_extended_base_var(
char *name,
int baselen,
int c)
168 name[baselen++] =
'.';
171 int ch = get_next_char();
178 ch = get_next_char();
182 name[baselen++] = ch;
188 if (get_next_char() !=
']')
193 static int get_base_var(
char *name)
198 int c = get_next_char();
204 return get_extended_base_var(name, baselen, c);
205 if (!iskeychar(c) && c !=
'.')
213 static int perf_parse_file(
config_fn_t fn,
void *data)
220 static const unsigned char *utf8_bom = (
unsigned char *)
"\xef\xbb\xbf";
221 const unsigned char *bomptr = utf8_bom;
224 int c = get_next_char();
225 if (bomptr && *bomptr) {
229 if ((
unsigned char) c == *bomptr) {
234 if (bomptr != utf8_bom)
248 if (c ==
'#' || c ==
';') {
253 baselen = get_base_var(var);
256 var[baselen++] =
'.';
263 if (get_value(fn, data, var, baselen+1) < 0)
266 die(
"bad config file line %d in %s", config_linenr, config_file_name);
269 static int parse_unit_factor(
const char *
end,
unsigned long *
val)
282 *val *= 1024 * 1024 * 1024;
288 static int perf_parse_long(
const char *value,
long *
ret)
290 if (value && *value) {
292 long val = strtol(value, &end, 0);
293 unsigned long factor = 1;
294 if (!parse_unit_factor(end, &factor))
302 static void die_bad_config(
const char *name)
304 if (config_file_name)
305 die(
"bad config value for '%s' in %s", name, config_file_name);
306 die(
"bad config value for '%s'", name);
312 if (!perf_parse_long(value, &ret))
313 die_bad_config(name);
317 static int perf_config_bool_or_int(
const char *name,
const char *value,
int *is_bool)
335 return !!perf_config_bool_or_int(name, value, &discard);
345 static int perf_default_core_config(
const char *var
__maybe_unused,
346 const char *value __maybe_unused)
356 return perf_default_core_config(var, value);
365 FILE *
f = fopen(filename,
"r");
373 ret = perf_parse_file(fn, data);
375 config_file_name =
NULL;
380 static const char *perf_etc_perfconfig(
void)
382 static const char *system_wide;
388 static int perf_env_bool(
const char *
k,
int def)
390 const char *
v = getenv(k);
394 static int perf_config_system(
void)
396 return !perf_env_bool(
"PERF_CONFIG_NOSYSTEM", 0);
399 static int perf_config_global(
void)
401 return !perf_env_bool(
"PERF_CONFIG_NOGLOBAL", 0);
406 int ret = 0, found = 0;
407 const char *home =
NULL;
410 if (config_exclusive_filename)
411 return perf_config_from_file(fn, config_exclusive_filename, data);
412 if (perf_config_system() && !
access(perf_etc_perfconfig(), R_OK)) {
413 ret += perf_config_from_file(fn, perf_etc_perfconfig(),
418 home = getenv(
"HOME");
419 if (perf_config_global() && home) {
420 char *user_config = strdup(
mkpath(
"%s/.perfconfig", home));
423 if (user_config ==
NULL) {
424 warning(
"Not enough memory to process %s/.perfconfig, "
425 "ignoring it.", home);
429 if (
stat(user_config, &st) < 0)
433 warning(
"File %s not owned by current user or root, "
434 "ignoring it.", user_config);
441 ret += perf_config_from_file(fn, user_config, data);
458 return error(
"Missing value for '%s'", var);
465 static int buildid_dir_command_config(
const char *var,
const char *value,
482 static void check_buildid_dir_config(
void)
494 check_buildid_dir_config();
498 char *v = getenv(
"HOME");