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
== '-';
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 __used
, const char *value __used
)
347 /* Add other config variables here. */
351 int perf_default_config(const char *var
, const char *value
, void *dummy __used
)
353 if (!prefixcmp(var
, "core."))
354 return perf_default_core_config(var
, value
);
356 /* Add other config variables here. */
360 static int perf_config_from_file(config_fn_t fn
, const char *filename
, void *data
)
363 FILE *f
= fopen(filename
, "r");
368 config_file_name
= filename
;
371 ret
= perf_parse_file(fn
, data
);
373 config_file_name
= NULL
;
378 static const char *perf_etc_perfconfig(void)
380 static const char *system_wide
;
382 system_wide
= system_path(ETC_PERFCONFIG
);
386 static int perf_env_bool(const char *k
, int def
)
388 const char *v
= getenv(k
);
389 return v
? perf_config_bool(k
, v
) : def
;
392 static int perf_config_system(void)
394 return !perf_env_bool("PERF_CONFIG_NOSYSTEM", 0);
397 static int perf_config_global(void)
399 return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0);
402 int perf_config(config_fn_t fn
, void *data
)
404 int ret
= 0, found
= 0;
405 const char *home
= NULL
;
407 /* Setting $PERF_CONFIG makes perf read _only_ the given config file. */
408 if (config_exclusive_filename
)
409 return perf_config_from_file(fn
, config_exclusive_filename
, data
);
410 if (perf_config_system() && !access(perf_etc_perfconfig(), R_OK
)) {
411 ret
+= perf_config_from_file(fn
, perf_etc_perfconfig(),
416 home
= getenv("HOME");
417 if (perf_config_global() && home
) {
418 char *user_config
= strdup(mkpath("%s/.perfconfig", home
));
421 if (user_config
== NULL
) {
422 warning("Not enough memory to process %s/.perfconfig, "
423 "ignoring it.", home
);
427 if (stat(user_config
, &st
) < 0)
430 if (st
.st_uid
&& (st
.st_uid
!= geteuid())) {
431 warning("File %s not owned by current user or root, "
432 "ignoring it.", user_config
);
439 ret
+= perf_config_from_file(fn
, user_config
, data
);
451 * Call this to report error for your variable that should not
452 * get a boolean value (i.e. "[my] var" means "true").
454 int config_error_nonbool(const char *var
)
456 return error("Missing value for '%s'", var
);
459 struct buildid_dir_config
{
463 static int buildid_dir_command_config(const char *var
, const char *value
,
466 struct buildid_dir_config
*c
= data
;
469 /* same dir for all commands */
470 if (!prefixcmp(var
, "buildid.") && !strcmp(var
+ 8, "dir")) {
471 v
= perf_config_dirname(var
, value
);
474 strncpy(c
->dir
, v
, MAXPATHLEN
-1);
475 c
->dir
[MAXPATHLEN
-1] = '\0';
480 static void check_buildid_dir_config(void)
482 struct buildid_dir_config c
;
484 perf_config(buildid_dir_command_config
, &c
);
487 void set_buildid_dir(void)
489 buildid_dir
[0] = '\0';
491 /* try config file */
492 check_buildid_dir_config();
494 /* default to $HOME/.debug */
495 if (buildid_dir
[0] == '\0') {
496 char *v
= getenv("HOME");
498 snprintf(buildid_dir
, MAXPATHLEN
-1, "%s/%s",
501 strncpy(buildid_dir
, DEBUG_CACHE_DIR
, MAXPATHLEN
-1);
503 buildid_dir
[MAXPATHLEN
-1] = '\0';
505 /* for communicating with external commands */
506 setenv("PERF_BUILDID_DIR", buildid_dir
, 1);