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
17 #define DEBUG_CACHE_DIR ".debug"
20 char buildid_dir
[MAXPATHLEN
]; /* root dir for buildid, binary cache */
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
) {
38 /* DOS like systems */
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)
74 if (isspace(c
) && !quote
) {
79 if (c
== ';' || c
== '#') {
103 /* Some characters escape as themselves */
106 /* Reject unknown escape sequences */
121 static inline int iskeychar(int c
)
123 return isalnum(c
) || c
== '-' || c
== '_';
126 static int get_value(config_fn_t fn
, void *data
, char *name
, unsigned int len
)
131 /* Get the full name */
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
)
163 } while (isspace(c
));
165 /* We require the format to be '[base "extension"]' */
168 name
[baselen
++] = '.';
171 int ch
= get_next_char();
178 ch
= get_next_char();
182 name
[baselen
++] = ch
;
183 if (baselen
> MAXNAME
/ 2)
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
!= '.')
207 if (baselen
> MAXNAME
/ 2)
209 name
[baselen
++] = tolower(c
);
213 static int perf_parse_file(config_fn_t fn
, void *data
)
217 static char var
[MAXNAME
];
219 /* U+FEFF Byte Order Mark in UTF8 */
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
) {
226 /* We are at the file beginning; skip UTF8-encoded BOM
227 * if present. Sane editors won't put this in on their
228 * own, but e.g. Windows Notepad will do it happily. */
229 if ((unsigned char) c
== *bomptr
) {
233 /* Do not tolerate partial BOM. */
234 if (bomptr
!= utf8_bom
)
236 /* No BOM at file beginning. Cool. */
246 if (comment
|| isspace(c
))
248 if (c
== '#' || c
== ';') {
253 baselen
= get_base_var(var
);
256 var
[baselen
++] = '.';
262 var
[baselen
] = tolower(c
);
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
)
273 else if (!strcasecmp(end
, "k")) {
277 else if (!strcasecmp(end
, "m")) {
281 else if (!strcasecmp(end
, "g")) {
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
);
309 int perf_config_int(const char *name
, const char *value
)
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
)
324 if (!strcasecmp(value
, "true") || !strcasecmp(value
, "yes") || !strcasecmp(value
, "on"))
326 if (!strcasecmp(value
, "false") || !strcasecmp(value
, "no") || !strcasecmp(value
, "off"))
329 return perf_config_int(name
, value
);
332 int perf_config_bool(const char *name
, const char *value
)
335 return !!perf_config_bool_or_int(name
, value
, &discard
);
338 const char *perf_config_dirname(const char *name
, const char *value
)
345 static int perf_default_core_config(const char *var __maybe_unused
,
346 const char *value __maybe_unused
)
348 /* Add other config variables here. */
352 int perf_default_config(const char *var
, const char *value
,
353 void *dummy __maybe_unused
)
355 if (!prefixcmp(var
, "core."))
356 return perf_default_core_config(var
, value
);
358 /* Add other config variables here. */
362 static int perf_config_from_file(config_fn_t fn
, const char *filename
, void *data
)
365 FILE *f
= fopen(filename
, "r");
370 config_file_name
= filename
;
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
;
384 system_wide
= system_path(ETC_PERFCONFIG
);
388 static int perf_env_bool(const char *k
, int def
)
390 const char *v
= getenv(k
);
391 return v
? perf_config_bool(k
, v
) : def
;
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);
404 int perf_config(config_fn_t fn
, void *data
)
406 int ret
= 0, found
= 0;
407 const char *home
= NULL
;
409 /* Setting $PERF_CONFIG makes perf read _only_ the given config file. */
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)
432 if (st
.st_uid
&& (st
.st_uid
!= geteuid())) {
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
);
453 * Call this to report error for your variable that should not
454 * get a boolean value (i.e. "[my] var" means "true").
456 int config_error_nonbool(const char *var
)
458 return error("Missing value for '%s'", var
);
461 struct buildid_dir_config
{
465 static int buildid_dir_command_config(const char *var
, const char *value
,
468 struct buildid_dir_config
*c
= data
;
471 /* same dir for all commands */
472 if (!prefixcmp(var
, "buildid.") && !strcmp(var
+ 8, "dir")) {
473 v
= perf_config_dirname(var
, value
);
476 strncpy(c
->dir
, v
, MAXPATHLEN
-1);
477 c
->dir
[MAXPATHLEN
-1] = '\0';
482 static void check_buildid_dir_config(void)
484 struct buildid_dir_config c
;
486 perf_config(buildid_dir_command_config
, &c
);
489 void set_buildid_dir(void)
491 buildid_dir
[0] = '\0';
493 /* try config file */
494 check_buildid_dir_config();
496 /* default to $HOME/.debug */
497 if (buildid_dir
[0] == '\0') {
498 char *v
= getenv("HOME");
500 snprintf(buildid_dir
, MAXPATHLEN
-1, "%s/%s",
503 strncpy(buildid_dir
, DEBUG_CACHE_DIR
, MAXPATHLEN
-1);
505 buildid_dir
[MAXPATHLEN
-1] = '\0';
507 /* for communicating with external commands */
508 setenv("PERF_BUILDID_DIR", buildid_dir
, 1);