4 * Helper functions for parsing config items.
5 * Originally copied from GIT source.
7 * Copyright (C) Linus Torvalds, 2005
8 * Copyright (C) Johannes Schindelin, 2005
14 #include "util/hist.h" /* perf_hist_config */
18 #define DEBUG_CACHE_DIR ".debug"
21 char buildid_dir[MAXPATHLEN]; /* root dir for buildid, binary cache */
23 static FILE *config_file;
24 static const char *config_file_name;
25 static int config_linenr;
26 static int config_file_eof;
28 static const char *config_exclusive_filename;
30 static int get_next_char(void)
36 if ((f = config_file) != NULL) {
39 /* DOS like systems */
56 static char *parse_value(void)
58 static char value[1024];
59 int quote = 0, comment = 0, space = 0;
63 int c = get_next_char();
65 if (len >= sizeof(value) - 1)
75 if (isspace(c) && !quote) {
80 if (c == ';' || c == '#') {
104 /* Some characters escape as themselves */
107 /* Reject unknown escape sequences */
122 static inline int iskeychar(int c)
124 return isalnum(c) || c == '-' || c == '_';
127 static int get_value(config_fn_t fn, void *data, char *name, unsigned int len)
132 /* Get the full name */
144 while (c == ' ' || c == '\t')
151 value = parse_value();
155 return fn(name, value, data);
158 static int get_extended_base_var(char *name, int baselen, int c)
164 } while (isspace(c));
166 /* We require the format to be '[base "extension"]' */
169 name[baselen++] = '.';
172 int ch = get_next_char();
179 ch = get_next_char();
183 name[baselen++] = ch;
184 if (baselen > MAXNAME / 2)
189 if (get_next_char() != ']')
194 static int get_base_var(char *name)
199 int c = get_next_char();
205 return get_extended_base_var(name, baselen, c);
206 if (!iskeychar(c) && c != '.')
208 if (baselen > MAXNAME / 2)
210 name[baselen++] = tolower(c);
214 static int perf_parse_file(config_fn_t fn, void *data)
218 static char var[MAXNAME];
220 /* U+FEFF Byte Order Mark in UTF8 */
221 static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf";
222 const unsigned char *bomptr = utf8_bom;
225 int line, c = get_next_char();
227 if (bomptr && *bomptr) {
228 /* We are at the file beginning; skip UTF8-encoded BOM
229 * if present. Sane editors won't put this in on their
230 * own, but e.g. Windows Notepad will do it happily. */
231 if ((unsigned char) c == *bomptr) {
235 /* Do not tolerate partial BOM. */
236 if (bomptr != utf8_bom)
238 /* No BOM at file beginning. Cool. */
248 if (comment || isspace(c))
250 if (c == '#' || c == ';') {
255 baselen = get_base_var(var);
258 var[baselen++] = '.';
264 var[baselen] = tolower(c);
267 * The get_value function might or might not reach the '\n',
268 * so saving the current line number for error reporting.
270 line = config_linenr;
271 if (get_value(fn, data, var, baselen+1) < 0) {
272 config_linenr = line;
276 die("bad config file line %d in %s", config_linenr, config_file_name);
279 static int parse_unit_factor(const char *end, unsigned long *val)
283 else if (!strcasecmp(end, "k")) {
287 else if (!strcasecmp(end, "m")) {
291 else if (!strcasecmp(end, "g")) {
292 *val *= 1024 * 1024 * 1024;
298 static int perf_parse_llong(const char *value, long long *ret)
300 if (value && *value) {
302 long long val = strtoll(value, &end, 0);
303 unsigned long factor = 1;
305 if (!parse_unit_factor(end, &factor))
313 static int perf_parse_long(const char *value, long *ret)
315 if (value && *value) {
317 long val = strtol(value, &end, 0);
318 unsigned long factor = 1;
319 if (!parse_unit_factor(end, &factor))
327 static void die_bad_config(const char *name)
329 if (config_file_name)
330 die("bad config value for '%s' in %s", name, config_file_name);
331 die("bad config value for '%s'", name);
334 u64 perf_config_u64(const char *name, const char *value)
338 if (!perf_parse_llong(value, &ret))
339 die_bad_config(name);
343 int perf_config_int(const char *name, const char *value)
346 if (!perf_parse_long(value, &ret))
347 die_bad_config(name);
351 static int perf_config_bool_or_int(const char *name, const char *value, int *is_bool)
358 if (!strcasecmp(value, "true") || !strcasecmp(value, "yes") || !strcasecmp(value, "on"))
360 if (!strcasecmp(value, "false") || !strcasecmp(value, "no") || !strcasecmp(value, "off"))
363 return perf_config_int(name, value);
366 int perf_config_bool(const char *name, const char *value)
369 return !!perf_config_bool_or_int(name, value, &discard);
372 const char *perf_config_dirname(const char *name, const char *value)
379 static int perf_default_core_config(const char *var __maybe_unused,
380 const char *value __maybe_unused)
382 /* Add other config variables here. */
386 static int perf_ui_config(const char *var, const char *value)
388 /* Add other config variables here. */
389 if (!strcmp(var, "ui.show-headers")) {
390 symbol_conf.show_hist_headers = perf_config_bool(var, value);
396 int perf_default_config(const char *var, const char *value,
397 void *dummy __maybe_unused)
399 if (!prefixcmp(var, "core."))
400 return perf_default_core_config(var, value);
402 if (!prefixcmp(var, "hist."))
403 return perf_hist_config(var, value);
405 if (!prefixcmp(var, "ui."))
406 return perf_ui_config(var, value);
408 if (!prefixcmp(var, "call-graph."))
409 return perf_callchain_config(var, value);
411 /* Add other config variables here. */
415 static int perf_config_from_file(config_fn_t fn, const char *filename, void *data)
418 FILE *f = fopen(filename, "r");
423 config_file_name = filename;
426 ret = perf_parse_file(fn, data);
428 config_file_name = NULL;
433 static const char *perf_etc_perfconfig(void)
435 static const char *system_wide;
437 system_wide = system_path(ETC_PERFCONFIG);
441 static int perf_env_bool(const char *k, int def)
443 const char *v = getenv(k);
444 return v ? perf_config_bool(k, v) : def;
447 static int perf_config_system(void)
449 return !perf_env_bool("PERF_CONFIG_NOSYSTEM", 0);
452 static int perf_config_global(void)
454 return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0);
457 int perf_config(config_fn_t fn, void *data)
459 int ret = 0, found = 0;
460 const char *home = NULL;
462 /* Setting $PERF_CONFIG makes perf read _only_ the given config file. */
463 if (config_exclusive_filename)
464 return perf_config_from_file(fn, config_exclusive_filename, data);
465 if (perf_config_system() && !access(perf_etc_perfconfig(), R_OK)) {
466 ret += perf_config_from_file(fn, perf_etc_perfconfig(),
471 home = getenv("HOME");
472 if (perf_config_global() && home) {
473 char *user_config = strdup(mkpath("%s/.perfconfig", home));
476 if (user_config == NULL) {
477 warning("Not enough memory to process %s/.perfconfig, "
478 "ignoring it.", home);
482 if (stat(user_config, &st) < 0)
485 if (st.st_uid && (st.st_uid != geteuid())) {
486 warning("File %s not owned by current user or root, "
487 "ignoring it.", user_config);
494 ret += perf_config_from_file(fn, user_config, data);
506 * Call this to report error for your variable that should not
507 * get a boolean value (i.e. "[my] var" means "true").
509 int config_error_nonbool(const char *var)
511 return error("Missing value for '%s'", var);
514 struct buildid_dir_config {
518 static int buildid_dir_command_config(const char *var, const char *value,
521 struct buildid_dir_config *c = data;
524 /* same dir for all commands */
525 if (!prefixcmp(var, "buildid.") && !strcmp(var + 8, "dir")) {
526 v = perf_config_dirname(var, value);
529 strncpy(c->dir, v, MAXPATHLEN-1);
530 c->dir[MAXPATHLEN-1] = '\0';
535 static void check_buildid_dir_config(void)
537 struct buildid_dir_config c;
539 perf_config(buildid_dir_command_config, &c);
542 void set_buildid_dir(void)
544 buildid_dir[0] = '\0';
546 /* try config file */
547 check_buildid_dir_config();
549 /* default to $HOME/.debug */
550 if (buildid_dir[0] == '\0') {
551 char *v = getenv("HOME");
553 snprintf(buildid_dir, MAXPATHLEN-1, "%s/%s",
556 strncpy(buildid_dir, DEBUG_CACHE_DIR, MAXPATHLEN-1);
558 buildid_dir[MAXPATHLEN-1] = '\0';
560 /* for communicating with external commands */
561 setenv("PERF_BUILDID_DIR", buildid_dir, 1);