2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Johannes Schindelin, 2005
14 #define DEBUG_CACHE_DIR ".debug"
17 char buildid_dir[MAXPATHLEN]; /* root dir for buildid, binary cache */
19 static FILE *config_file;
20 static const char *config_file_name;
21 static int config_linenr;
22 static int config_file_eof;
24 static const char *config_exclusive_filename;
26 static int get_next_char(void)
32 if ((f = config_file) != NULL) {
35 /* DOS like systems */
52 static char *parse_value(void)
54 static char value[1024];
55 int quote = 0, comment = 0, space = 0;
59 int c = get_next_char();
61 if (len >= sizeof(value) - 1)
71 if (isspace(c) && !quote) {
76 if (c == ';' || c == '#') {
100 /* Some characters escape as themselves */
103 /* Reject unknown escape sequences */
118 static inline int iskeychar(int c)
120 return isalnum(c) || c == '-';
123 static int get_value(config_fn_t fn, void *data, char *name, unsigned int len)
128 /* Get the full name */
140 while (c == ' ' || c == '\t')
147 value = parse_value();
151 return fn(name, value, data);
154 static int get_extended_base_var(char *name, int baselen, int c)
160 } while (isspace(c));
162 /* We require the format to be '[base "extension"]' */
165 name[baselen++] = '.';
168 int ch = get_next_char();
175 ch = get_next_char();
179 name[baselen++] = ch;
180 if (baselen > MAXNAME / 2)
185 if (get_next_char() != ']')
190 static int get_base_var(char *name)
195 int c = get_next_char();
201 return get_extended_base_var(name, baselen, c);
202 if (!iskeychar(c) && c != '.')
204 if (baselen > MAXNAME / 2)
206 name[baselen++] = tolower(c);
210 static int perf_parse_file(config_fn_t fn, void *data)
214 static char var[MAXNAME];
216 /* U+FEFF Byte Order Mark in UTF8 */
217 static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf";
218 const unsigned char *bomptr = utf8_bom;
221 int c = get_next_char();
222 if (bomptr && *bomptr) {
223 /* We are at the file beginning; skip UTF8-encoded BOM
224 * if present. Sane editors won't put this in on their
225 * own, but e.g. Windows Notepad will do it happily. */
226 if ((unsigned char) c == *bomptr) {
230 /* Do not tolerate partial BOM. */
231 if (bomptr != utf8_bom)
233 /* No BOM at file beginning. Cool. */
243 if (comment || isspace(c))
245 if (c == '#' || c == ';') {
250 baselen = get_base_var(var);
253 var[baselen++] = '.';
259 var[baselen] = tolower(c);
260 if (get_value(fn, data, var, baselen+1) < 0)
263 die("bad config file line %d in %s", config_linenr, config_file_name);
266 static int parse_unit_factor(const char *end, unsigned long *val)
270 else if (!strcasecmp(end, "k")) {
274 else if (!strcasecmp(end, "m")) {
278 else if (!strcasecmp(end, "g")) {
279 *val *= 1024 * 1024 * 1024;
285 static int perf_parse_long(const char *value, long *ret)
287 if (value && *value) {
289 long val = strtol(value, &end, 0);
290 unsigned long factor = 1;
291 if (!parse_unit_factor(end, &factor))
299 static void die_bad_config(const char *name)
301 if (config_file_name)
302 die("bad config value for '%s' in %s", name, config_file_name);
303 die("bad config value for '%s'", name);
306 int perf_config_int(const char *name, const char *value)
309 if (!perf_parse_long(value, &ret))
310 die_bad_config(name);
314 static int perf_config_bool_or_int(const char *name, const char *value, int *is_bool)
321 if (!strcasecmp(value, "true") || !strcasecmp(value, "yes") || !strcasecmp(value, "on"))
323 if (!strcasecmp(value, "false") || !strcasecmp(value, "no") || !strcasecmp(value, "off"))
326 return perf_config_int(name, value);
329 int perf_config_bool(const char *name, const char *value)
332 return !!perf_config_bool_or_int(name, value, &discard);
335 const char *perf_config_dirname(const char *name, const char *value)
342 static int perf_default_core_config(const char *var __used, const char *value __used)
344 /* Add other config variables here and to Documentation/config.txt. */
348 int perf_default_config(const char *var, const char *value, void *dummy __used)
350 if (!prefixcmp(var, "core."))
351 return perf_default_core_config(var, value);
353 /* Add other config variables here and to Documentation/config.txt. */
357 static int perf_config_from_file(config_fn_t fn, const char *filename, void *data)
360 FILE *f = fopen(filename, "r");
365 config_file_name = filename;
368 ret = perf_parse_file(fn, data);
370 config_file_name = NULL;
375 static const char *perf_etc_perfconfig(void)
377 static const char *system_wide;
379 system_wide = system_path(ETC_PERFCONFIG);
383 static int perf_env_bool(const char *k, int def)
385 const char *v = getenv(k);
386 return v ? perf_config_bool(k, v) : def;
389 static int perf_config_system(void)
391 return !perf_env_bool("PERF_CONFIG_NOSYSTEM", 0);
394 static int perf_config_global(void)
396 return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0);
399 int perf_config(config_fn_t fn, void *data)
401 int ret = 0, found = 0;
402 char *repo_config = NULL;
403 const char *home = NULL;
405 /* Setting $PERF_CONFIG makes perf read _only_ the given config file. */
406 if (config_exclusive_filename)
407 return perf_config_from_file(fn, config_exclusive_filename, data);
408 if (perf_config_system() && !access(perf_etc_perfconfig(), R_OK)) {
409 ret += perf_config_from_file(fn, perf_etc_perfconfig(),
414 home = getenv("HOME");
415 if (perf_config_global() && home) {
416 char *user_config = strdup(mkpath("%s/.perfconfig", home));
417 if (!access(user_config, R_OK)) {
418 ret += perf_config_from_file(fn, user_config, data);
424 repo_config = perf_pathdup("config");
425 if (!access(repo_config, R_OK)) {
426 ret += perf_config_from_file(fn, repo_config, data);
436 * Call this to report error for your variable that should not
437 * get a boolean value (i.e. "[my] var" means "true").
439 int config_error_nonbool(const char *var)
441 return error("Missing value for '%s'", var);
444 struct buildid_dir_config {
448 static int buildid_dir_command_config(const char *var, const char *value,
451 struct buildid_dir_config *c = data;
454 /* same dir for all commands */
455 if (!prefixcmp(var, "buildid.") && !strcmp(var + 8, "dir")) {
456 v = perf_config_dirname(var, value);
459 strncpy(c->dir, v, MAXPATHLEN-1);
460 c->dir[MAXPATHLEN-1] = '\0';
465 static void check_buildid_dir_config(void)
467 struct buildid_dir_config c;
469 perf_config(buildid_dir_command_config, &c);
472 void set_buildid_dir(void)
474 buildid_dir[0] = '\0';
476 /* try config file */
477 check_buildid_dir_config();
479 /* default to $HOME/.debug */
480 if (buildid_dir[0] == '\0') {
481 char *v = getenv("HOME");
483 snprintf(buildid_dir, MAXPATHLEN-1, "%s/%s",
486 strncpy(buildid_dir, DEBUG_CACHE_DIR, MAXPATHLEN-1);
488 buildid_dir[MAXPATHLEN-1] = '\0';
490 /* for communicating with external commands */
491 setenv("PERF_BUILDID_DIR", buildid_dir, 1);