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 */
15 #include "util/llvm-utils.h" /* perf_llvm_config */
19 #define DEBUG_CACHE_DIR ".debug"
22 char buildid_dir
[MAXPATHLEN
]; /* root dir for buildid, binary cache */
24 static FILE *config_file
;
25 static const char *config_file_name
;
26 static int config_linenr
;
27 static int config_file_eof
;
29 static const char *config_exclusive_filename
;
31 static int get_next_char(void)
37 if ((f
= config_file
) != NULL
) {
40 /* DOS like systems */
57 static char *parse_value(void)
59 static char value
[1024];
60 int quote
= 0, comment
= 0, space
= 0;
64 int c
= get_next_char();
66 if (len
>= sizeof(value
) - 1)
76 if (isspace(c
) && !quote
) {
81 if (c
== ';' || c
== '#') {
105 /* Some characters escape as themselves */
108 /* Reject unknown escape sequences */
123 static inline int iskeychar(int c
)
125 return isalnum(c
) || c
== '-' || c
== '_';
128 static int get_value(config_fn_t fn
, void *data
, char *name
, unsigned int len
)
133 /* Get the full name */
145 while (c
== ' ' || c
== '\t')
152 value
= parse_value();
156 return fn(name
, value
, data
);
159 static int get_extended_base_var(char *name
, int baselen
, int c
)
165 } while (isspace(c
));
167 /* We require the format to be '[base "extension"]' */
170 name
[baselen
++] = '.';
173 int ch
= get_next_char();
180 ch
= get_next_char();
184 name
[baselen
++] = ch
;
185 if (baselen
> MAXNAME
/ 2)
190 if (get_next_char() != ']')
195 static int get_base_var(char *name
)
200 int c
= get_next_char();
206 return get_extended_base_var(name
, baselen
, c
);
207 if (!iskeychar(c
) && c
!= '.')
209 if (baselen
> MAXNAME
/ 2)
211 name
[baselen
++] = tolower(c
);
215 static int perf_parse_file(config_fn_t fn
, void *data
)
219 static char var
[MAXNAME
];
221 /* U+FEFF Byte Order Mark in UTF8 */
222 static const unsigned char *utf8_bom
= (unsigned char *) "\xef\xbb\xbf";
223 const unsigned char *bomptr
= utf8_bom
;
226 int line
, c
= get_next_char();
228 if (bomptr
&& *bomptr
) {
229 /* We are at the file beginning; skip UTF8-encoded BOM
230 * if present. Sane editors won't put this in on their
231 * own, but e.g. Windows Notepad will do it happily. */
232 if ((unsigned char) c
== *bomptr
) {
236 /* Do not tolerate partial BOM. */
237 if (bomptr
!= utf8_bom
)
239 /* No BOM at file beginning. Cool. */
249 if (comment
|| isspace(c
))
251 if (c
== '#' || c
== ';') {
256 baselen
= get_base_var(var
);
259 var
[baselen
++] = '.';
265 var
[baselen
] = tolower(c
);
268 * The get_value function might or might not reach the '\n',
269 * so saving the current line number for error reporting.
271 line
= config_linenr
;
272 if (get_value(fn
, data
, var
, baselen
+1) < 0) {
273 config_linenr
= line
;
277 die("bad config file line %d in %s", config_linenr
, config_file_name
);
280 static int parse_unit_factor(const char *end
, unsigned long *val
)
284 else if (!strcasecmp(end
, "k")) {
288 else if (!strcasecmp(end
, "m")) {
292 else if (!strcasecmp(end
, "g")) {
293 *val
*= 1024 * 1024 * 1024;
299 static int perf_parse_llong(const char *value
, long long *ret
)
301 if (value
&& *value
) {
303 long long val
= strtoll(value
, &end
, 0);
304 unsigned long factor
= 1;
306 if (!parse_unit_factor(end
, &factor
))
314 static int perf_parse_long(const char *value
, long *ret
)
316 if (value
&& *value
) {
318 long val
= strtol(value
, &end
, 0);
319 unsigned long factor
= 1;
320 if (!parse_unit_factor(end
, &factor
))
328 static void die_bad_config(const char *name
)
330 if (config_file_name
)
331 die("bad config value for '%s' in %s", name
, config_file_name
);
332 die("bad config value for '%s'", name
);
335 u64
perf_config_u64(const char *name
, const char *value
)
339 if (!perf_parse_llong(value
, &ret
))
340 die_bad_config(name
);
344 int perf_config_int(const char *name
, const char *value
)
347 if (!perf_parse_long(value
, &ret
))
348 die_bad_config(name
);
352 static int perf_config_bool_or_int(const char *name
, const char *value
, int *is_bool
)
359 if (!strcasecmp(value
, "true") || !strcasecmp(value
, "yes") || !strcasecmp(value
, "on"))
361 if (!strcasecmp(value
, "false") || !strcasecmp(value
, "no") || !strcasecmp(value
, "off"))
364 return perf_config_int(name
, value
);
367 int perf_config_bool(const char *name
, const char *value
)
370 return !!perf_config_bool_or_int(name
, value
, &discard
);
373 const char *perf_config_dirname(const char *name
, const char *value
)
380 static int perf_default_core_config(const char *var __maybe_unused
,
381 const char *value __maybe_unused
)
383 /* Add other config variables here. */
387 static int perf_ui_config(const char *var
, const char *value
)
389 /* Add other config variables here. */
390 if (!strcmp(var
, "ui.show-headers")) {
391 symbol_conf
.show_hist_headers
= perf_config_bool(var
, value
);
397 int perf_default_config(const char *var
, const char *value
,
398 void *dummy __maybe_unused
)
400 if (!prefixcmp(var
, "core."))
401 return perf_default_core_config(var
, value
);
403 if (!prefixcmp(var
, "hist."))
404 return perf_hist_config(var
, value
);
406 if (!prefixcmp(var
, "ui."))
407 return perf_ui_config(var
, value
);
409 if (!prefixcmp(var
, "call-graph."))
410 return perf_callchain_config(var
, value
);
412 if (!prefixcmp(var
, "llvm."))
413 return perf_llvm_config(var
, value
);
415 /* Add other config variables here. */
419 static int perf_config_from_file(config_fn_t fn
, const char *filename
, void *data
)
422 FILE *f
= fopen(filename
, "r");
427 config_file_name
= filename
;
430 ret
= perf_parse_file(fn
, data
);
432 config_file_name
= NULL
;
437 static const char *perf_etc_perfconfig(void)
439 static const char *system_wide
;
441 system_wide
= system_path(ETC_PERFCONFIG
);
445 static int perf_env_bool(const char *k
, int def
)
447 const char *v
= getenv(k
);
448 return v
? perf_config_bool(k
, v
) : def
;
451 static int perf_config_system(void)
453 return !perf_env_bool("PERF_CONFIG_NOSYSTEM", 0);
456 static int perf_config_global(void)
458 return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0);
461 int perf_config(config_fn_t fn
, void *data
)
463 int ret
= 0, found
= 0;
464 const char *home
= NULL
;
466 /* Setting $PERF_CONFIG makes perf read _only_ the given config file. */
467 if (config_exclusive_filename
)
468 return perf_config_from_file(fn
, config_exclusive_filename
, data
);
469 if (perf_config_system() && !access(perf_etc_perfconfig(), R_OK
)) {
470 ret
+= perf_config_from_file(fn
, perf_etc_perfconfig(),
475 home
= getenv("HOME");
476 if (perf_config_global() && home
) {
477 char *user_config
= strdup(mkpath("%s/.perfconfig", home
));
480 if (user_config
== NULL
) {
481 warning("Not enough memory to process %s/.perfconfig, "
482 "ignoring it.", home
);
486 if (stat(user_config
, &st
) < 0)
489 if (st
.st_uid
&& (st
.st_uid
!= geteuid())) {
490 warning("File %s not owned by current user or root, "
491 "ignoring it.", user_config
);
498 ret
+= perf_config_from_file(fn
, user_config
, data
);
510 * Call this to report error for your variable that should not
511 * get a boolean value (i.e. "[my] var" means "true").
513 int config_error_nonbool(const char *var
)
515 return error("Missing value for '%s'", var
);
518 struct buildid_dir_config
{
522 static int buildid_dir_command_config(const char *var
, const char *value
,
525 struct buildid_dir_config
*c
= data
;
528 /* same dir for all commands */
529 if (!strcmp(var
, "buildid.dir")) {
530 v
= perf_config_dirname(var
, value
);
533 strncpy(c
->dir
, v
, MAXPATHLEN
-1);
534 c
->dir
[MAXPATHLEN
-1] = '\0';
539 static void check_buildid_dir_config(void)
541 struct buildid_dir_config c
;
543 perf_config(buildid_dir_command_config
, &c
);
546 void set_buildid_dir(const char *dir
)
549 scnprintf(buildid_dir
, MAXPATHLEN
-1, "%s", dir
);
551 /* try config file */
552 if (buildid_dir
[0] == '\0')
553 check_buildid_dir_config();
555 /* default to $HOME/.debug */
556 if (buildid_dir
[0] == '\0') {
557 char *v
= getenv("HOME");
559 snprintf(buildid_dir
, MAXPATHLEN
-1, "%s/%s",
562 strncpy(buildid_dir
, DEBUG_CACHE_DIR
, MAXPATHLEN
-1);
564 buildid_dir
[MAXPATHLEN
-1] = '\0';
566 /* for communicating with external commands */
567 setenv("PERF_BUILDID_DIR", buildid_dir
, 1);