Commit | Line | Data |
---|---|---|
07800601 | 1 | /* |
5f9273d6 NK |
2 | * config.c |
3 | * | |
4 | * Helper functions for parsing config items. | |
5 | * Originally copied from GIT source. | |
07800601 IM |
6 | * |
7 | * Copyright (C) Linus Torvalds, 2005 | |
8 | * Copyright (C) Johannes Schindelin, 2005 | |
9 | * | |
10 | */ | |
11 | #include "util.h" | |
12 | #include "cache.h" | |
13 | #include "exec_cmd.h" | |
0b93da17 | 14 | #include "util/hist.h" /* perf_hist_config */ |
07800601 IM |
15 | |
16 | #define MAXNAME (256) | |
17 | ||
45de34bb SE |
18 | #define DEBUG_CACHE_DIR ".debug" |
19 | ||
20 | ||
21 | char buildid_dir[MAXPATHLEN]; /* root dir for buildid, binary cache */ | |
22 | ||
07800601 IM |
23 | static FILE *config_file; |
24 | static const char *config_file_name; | |
25 | static int config_linenr; | |
26 | static int config_file_eof; | |
07800601 | 27 | |
a41794cd | 28 | static const char *config_exclusive_filename; |
07800601 IM |
29 | |
30 | static int get_next_char(void) | |
31 | { | |
32 | int c; | |
33 | FILE *f; | |
34 | ||
35 | c = '\n'; | |
36 | if ((f = config_file) != NULL) { | |
37 | c = fgetc(f); | |
38 | if (c == '\r') { | |
39 | /* DOS like systems */ | |
40 | c = fgetc(f); | |
41 | if (c != '\n') { | |
42 | ungetc(c, f); | |
43 | c = '\r'; | |
44 | } | |
45 | } | |
46 | if (c == '\n') | |
47 | config_linenr++; | |
48 | if (c == EOF) { | |
49 | config_file_eof = 1; | |
50 | c = '\n'; | |
51 | } | |
52 | } | |
53 | return c; | |
54 | } | |
55 | ||
56 | static char *parse_value(void) | |
57 | { | |
58 | static char value[1024]; | |
f37a291c IM |
59 | int quote = 0, comment = 0, space = 0; |
60 | size_t len = 0; | |
07800601 IM |
61 | |
62 | for (;;) { | |
63 | int c = get_next_char(); | |
f37a291c | 64 | |
07800601 IM |
65 | if (len >= sizeof(value) - 1) |
66 | return NULL; | |
67 | if (c == '\n') { | |
68 | if (quote) | |
69 | return NULL; | |
70 | value[len] = 0; | |
71 | return value; | |
72 | } | |
73 | if (comment) | |
74 | continue; | |
75 | if (isspace(c) && !quote) { | |
76 | space = 1; | |
77 | continue; | |
78 | } | |
79 | if (!quote) { | |
80 | if (c == ';' || c == '#') { | |
81 | comment = 1; | |
82 | continue; | |
83 | } | |
84 | } | |
85 | if (space) { | |
86 | if (len) | |
87 | value[len++] = ' '; | |
88 | space = 0; | |
89 | } | |
90 | if (c == '\\') { | |
91 | c = get_next_char(); | |
92 | switch (c) { | |
93 | case '\n': | |
94 | continue; | |
95 | case 't': | |
96 | c = '\t'; | |
97 | break; | |
98 | case 'b': | |
99 | c = '\b'; | |
100 | break; | |
101 | case 'n': | |
102 | c = '\n'; | |
103 | break; | |
104 | /* Some characters escape as themselves */ | |
105 | case '\\': case '"': | |
106 | break; | |
107 | /* Reject unknown escape sequences */ | |
108 | default: | |
109 | return NULL; | |
110 | } | |
111 | value[len++] = c; | |
112 | continue; | |
113 | } | |
114 | if (c == '"') { | |
115 | quote = 1-quote; | |
116 | continue; | |
117 | } | |
118 | value[len++] = c; | |
119 | } | |
120 | } | |
121 | ||
122 | static inline int iskeychar(int c) | |
123 | { | |
8dc7c651 | 124 | return isalnum(c) || c == '-' || c == '_'; |
07800601 IM |
125 | } |
126 | ||
127 | static int get_value(config_fn_t fn, void *data, char *name, unsigned int len) | |
128 | { | |
129 | int c; | |
130 | char *value; | |
131 | ||
132 | /* Get the full name */ | |
133 | for (;;) { | |
134 | c = get_next_char(); | |
135 | if (config_file_eof) | |
136 | break; | |
137 | if (!iskeychar(c)) | |
138 | break; | |
45de34bb | 139 | name[len++] = c; |
07800601 IM |
140 | if (len >= MAXNAME) |
141 | return -1; | |
142 | } | |
143 | name[len] = 0; | |
144 | while (c == ' ' || c == '\t') | |
145 | c = get_next_char(); | |
146 | ||
147 | value = NULL; | |
148 | if (c != '\n') { | |
149 | if (c != '=') | |
150 | return -1; | |
151 | value = parse_value(); | |
152 | if (!value) | |
153 | return -1; | |
154 | } | |
155 | return fn(name, value, data); | |
156 | } | |
157 | ||
158 | static int get_extended_base_var(char *name, int baselen, int c) | |
159 | { | |
160 | do { | |
161 | if (c == '\n') | |
162 | return -1; | |
163 | c = get_next_char(); | |
164 | } while (isspace(c)); | |
165 | ||
166 | /* We require the format to be '[base "extension"]' */ | |
167 | if (c != '"') | |
168 | return -1; | |
169 | name[baselen++] = '.'; | |
170 | ||
171 | for (;;) { | |
83a0944f IM |
172 | int ch = get_next_char(); |
173 | ||
174 | if (ch == '\n') | |
07800601 | 175 | return -1; |
83a0944f | 176 | if (ch == '"') |
07800601 | 177 | break; |
83a0944f IM |
178 | if (ch == '\\') { |
179 | ch = get_next_char(); | |
180 | if (ch == '\n') | |
07800601 IM |
181 | return -1; |
182 | } | |
83a0944f | 183 | name[baselen++] = ch; |
07800601 IM |
184 | if (baselen > MAXNAME / 2) |
185 | return -1; | |
186 | } | |
187 | ||
188 | /* Final ']' */ | |
189 | if (get_next_char() != ']') | |
190 | return -1; | |
191 | return baselen; | |
192 | } | |
193 | ||
194 | static int get_base_var(char *name) | |
195 | { | |
196 | int baselen = 0; | |
197 | ||
198 | for (;;) { | |
199 | int c = get_next_char(); | |
200 | if (config_file_eof) | |
201 | return -1; | |
202 | if (c == ']') | |
203 | return baselen; | |
204 | if (isspace(c)) | |
205 | return get_extended_base_var(name, baselen, c); | |
206 | if (!iskeychar(c) && c != '.') | |
207 | return -1; | |
208 | if (baselen > MAXNAME / 2) | |
209 | return -1; | |
210 | name[baselen++] = tolower(c); | |
211 | } | |
212 | } | |
213 | ||
214 | static int perf_parse_file(config_fn_t fn, void *data) | |
215 | { | |
216 | int comment = 0; | |
217 | int baselen = 0; | |
218 | static char var[MAXNAME]; | |
219 | ||
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; | |
223 | ||
224 | for (;;) { | |
225 | int c = get_next_char(); | |
226 | if (bomptr && *bomptr) { | |
227 | /* We are at the file beginning; skip UTF8-encoded BOM | |
228 | * if present. Sane editors won't put this in on their | |
229 | * own, but e.g. Windows Notepad will do it happily. */ | |
230 | if ((unsigned char) c == *bomptr) { | |
231 | bomptr++; | |
232 | continue; | |
233 | } else { | |
234 | /* Do not tolerate partial BOM. */ | |
235 | if (bomptr != utf8_bom) | |
236 | break; | |
237 | /* No BOM at file beginning. Cool. */ | |
238 | bomptr = NULL; | |
239 | } | |
240 | } | |
241 | if (c == '\n') { | |
242 | if (config_file_eof) | |
243 | return 0; | |
244 | comment = 0; | |
245 | continue; | |
246 | } | |
247 | if (comment || isspace(c)) | |
248 | continue; | |
249 | if (c == '#' || c == ';') { | |
250 | comment = 1; | |
251 | continue; | |
252 | } | |
253 | if (c == '[') { | |
254 | baselen = get_base_var(var); | |
255 | if (baselen <= 0) | |
256 | break; | |
257 | var[baselen++] = '.'; | |
258 | var[baselen] = 0; | |
259 | continue; | |
260 | } | |
261 | if (!isalpha(c)) | |
262 | break; | |
263 | var[baselen] = tolower(c); | |
264 | if (get_value(fn, data, var, baselen+1) < 0) | |
265 | break; | |
266 | } | |
267 | die("bad config file line %d in %s", config_linenr, config_file_name); | |
268 | } | |
269 | ||
270 | static int parse_unit_factor(const char *end, unsigned long *val) | |
271 | { | |
272 | if (!*end) | |
273 | return 1; | |
274 | else if (!strcasecmp(end, "k")) { | |
275 | *val *= 1024; | |
276 | return 1; | |
277 | } | |
278 | else if (!strcasecmp(end, "m")) { | |
279 | *val *= 1024 * 1024; | |
280 | return 1; | |
281 | } | |
282 | else if (!strcasecmp(end, "g")) { | |
283 | *val *= 1024 * 1024 * 1024; | |
284 | return 1; | |
285 | } | |
286 | return 0; | |
287 | } | |
288 | ||
94c0655f JO |
289 | static int perf_parse_llong(const char *value, long long *ret) |
290 | { | |
291 | if (value && *value) { | |
292 | char *end; | |
293 | long long val = strtoll(value, &end, 0); | |
294 | unsigned long factor = 1; | |
295 | ||
296 | if (!parse_unit_factor(end, &factor)) | |
297 | return 0; | |
298 | *ret = val * factor; | |
299 | return 1; | |
300 | } | |
301 | return 0; | |
302 | } | |
303 | ||
07800601 IM |
304 | static int perf_parse_long(const char *value, long *ret) |
305 | { | |
306 | if (value && *value) { | |
307 | char *end; | |
308 | long val = strtol(value, &end, 0); | |
309 | unsigned long factor = 1; | |
310 | if (!parse_unit_factor(end, &factor)) | |
311 | return 0; | |
312 | *ret = val * factor; | |
313 | return 1; | |
314 | } | |
315 | return 0; | |
316 | } | |
317 | ||
07800601 IM |
318 | static void die_bad_config(const char *name) |
319 | { | |
320 | if (config_file_name) | |
321 | die("bad config value for '%s' in %s", name, config_file_name); | |
322 | die("bad config value for '%s'", name); | |
323 | } | |
324 | ||
94c0655f JO |
325 | u64 perf_config_u64(const char *name, const char *value) |
326 | { | |
327 | long long ret = 0; | |
328 | ||
329 | if (!perf_parse_llong(value, &ret)) | |
330 | die_bad_config(name); | |
331 | return (u64) ret; | |
332 | } | |
333 | ||
07800601 IM |
334 | int perf_config_int(const char *name, const char *value) |
335 | { | |
336 | long ret = 0; | |
337 | if (!perf_parse_long(value, &ret)) | |
338 | die_bad_config(name); | |
339 | return ret; | |
340 | } | |
341 | ||
a41794cd | 342 | static int perf_config_bool_or_int(const char *name, const char *value, int *is_bool) |
07800601 IM |
343 | { |
344 | *is_bool = 1; | |
345 | if (!value) | |
346 | return 1; | |
347 | if (!*value) | |
348 | return 0; | |
349 | if (!strcasecmp(value, "true") || !strcasecmp(value, "yes") || !strcasecmp(value, "on")) | |
350 | return 1; | |
351 | if (!strcasecmp(value, "false") || !strcasecmp(value, "no") || !strcasecmp(value, "off")) | |
352 | return 0; | |
353 | *is_bool = 0; | |
354 | return perf_config_int(name, value); | |
355 | } | |
356 | ||
357 | int perf_config_bool(const char *name, const char *value) | |
358 | { | |
359 | int discard; | |
360 | return !!perf_config_bool_or_int(name, value, &discard); | |
361 | } | |
362 | ||
45de34bb SE |
363 | const char *perf_config_dirname(const char *name, const char *value) |
364 | { | |
365 | if (!name) | |
366 | return NULL; | |
367 | return value; | |
368 | } | |
369 | ||
1d037ca1 IT |
370 | static int perf_default_core_config(const char *var __maybe_unused, |
371 | const char *value __maybe_unused) | |
07800601 | 372 | { |
395cf969 | 373 | /* Add other config variables here. */ |
07800601 IM |
374 | return 0; |
375 | } | |
376 | ||
c8302367 JO |
377 | static int perf_ui_config(const char *var, const char *value) |
378 | { | |
379 | /* Add other config variables here. */ | |
380 | if (!strcmp(var, "ui.show-headers")) { | |
381 | symbol_conf.show_hist_headers = perf_config_bool(var, value); | |
382 | return 0; | |
383 | } | |
384 | return 0; | |
385 | } | |
386 | ||
1d037ca1 IT |
387 | int perf_default_config(const char *var, const char *value, |
388 | void *dummy __maybe_unused) | |
07800601 IM |
389 | { |
390 | if (!prefixcmp(var, "core.")) | |
391 | return perf_default_core_config(var, value); | |
392 | ||
0b93da17 NK |
393 | if (!prefixcmp(var, "hist.")) |
394 | return perf_hist_config(var, value); | |
395 | ||
c8302367 JO |
396 | if (!prefixcmp(var, "ui.")) |
397 | return perf_ui_config(var, value); | |
398 | ||
395cf969 | 399 | /* Add other config variables here. */ |
07800601 IM |
400 | return 0; |
401 | } | |
402 | ||
a41794cd | 403 | static int perf_config_from_file(config_fn_t fn, const char *filename, void *data) |
07800601 IM |
404 | { |
405 | int ret; | |
406 | FILE *f = fopen(filename, "r"); | |
407 | ||
408 | ret = -1; | |
409 | if (f) { | |
410 | config_file = f; | |
411 | config_file_name = filename; | |
412 | config_linenr = 1; | |
413 | config_file_eof = 0; | |
414 | ret = perf_parse_file(fn, data); | |
415 | fclose(f); | |
416 | config_file_name = NULL; | |
417 | } | |
418 | return ret; | |
419 | } | |
420 | ||
a41794cd | 421 | static const char *perf_etc_perfconfig(void) |
07800601 IM |
422 | { |
423 | static const char *system_wide; | |
424 | if (!system_wide) | |
425 | system_wide = system_path(ETC_PERFCONFIG); | |
426 | return system_wide; | |
427 | } | |
428 | ||
429 | static int perf_env_bool(const char *k, int def) | |
430 | { | |
431 | const char *v = getenv(k); | |
432 | return v ? perf_config_bool(k, v) : def; | |
433 | } | |
434 | ||
a41794cd | 435 | static int perf_config_system(void) |
07800601 IM |
436 | { |
437 | return !perf_env_bool("PERF_CONFIG_NOSYSTEM", 0); | |
438 | } | |
439 | ||
a41794cd | 440 | static int perf_config_global(void) |
07800601 IM |
441 | { |
442 | return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0); | |
443 | } | |
444 | ||
445 | int perf_config(config_fn_t fn, void *data) | |
446 | { | |
447 | int ret = 0, found = 0; | |
07800601 IM |
448 | const char *home = NULL; |
449 | ||
450 | /* Setting $PERF_CONFIG makes perf read _only_ the given config file. */ | |
451 | if (config_exclusive_filename) | |
452 | return perf_config_from_file(fn, config_exclusive_filename, data); | |
453 | if (perf_config_system() && !access(perf_etc_perfconfig(), R_OK)) { | |
454 | ret += perf_config_from_file(fn, perf_etc_perfconfig(), | |
455 | data); | |
456 | found += 1; | |
457 | } | |
458 | ||
459 | home = getenv("HOME"); | |
460 | if (perf_config_global() && home) { | |
461 | char *user_config = strdup(mkpath("%s/.perfconfig", home)); | |
069e3725 ACM |
462 | struct stat st; |
463 | ||
464 | if (user_config == NULL) { | |
465 | warning("Not enough memory to process %s/.perfconfig, " | |
466 | "ignoring it.", home); | |
467 | goto out; | |
468 | } | |
469 | ||
470 | if (stat(user_config, &st) < 0) | |
471 | goto out_free; | |
472 | ||
473 | if (st.st_uid && (st.st_uid != geteuid())) { | |
474 | warning("File %s not owned by current user or root, " | |
475 | "ignoring it.", user_config); | |
476 | goto out_free; | |
07800601 | 477 | } |
069e3725 ACM |
478 | |
479 | if (!st.st_size) | |
480 | goto out_free; | |
481 | ||
482 | ret += perf_config_from_file(fn, user_config, data); | |
483 | found += 1; | |
484 | out_free: | |
07800601 IM |
485 | free(user_config); |
486 | } | |
069e3725 | 487 | out: |
07800601 IM |
488 | if (found == 0) |
489 | return -1; | |
490 | return ret; | |
491 | } | |
492 | ||
07800601 IM |
493 | /* |
494 | * Call this to report error for your variable that should not | |
495 | * get a boolean value (i.e. "[my] var" means "true"). | |
496 | */ | |
497 | int config_error_nonbool(const char *var) | |
498 | { | |
499 | return error("Missing value for '%s'", var); | |
500 | } | |
45de34bb SE |
501 | |
502 | struct buildid_dir_config { | |
503 | char *dir; | |
504 | }; | |
505 | ||
506 | static int buildid_dir_command_config(const char *var, const char *value, | |
507 | void *data) | |
508 | { | |
509 | struct buildid_dir_config *c = data; | |
510 | const char *v; | |
511 | ||
512 | /* same dir for all commands */ | |
513 | if (!prefixcmp(var, "buildid.") && !strcmp(var + 8, "dir")) { | |
514 | v = perf_config_dirname(var, value); | |
515 | if (!v) | |
516 | return -1; | |
517 | strncpy(c->dir, v, MAXPATHLEN-1); | |
518 | c->dir[MAXPATHLEN-1] = '\0'; | |
519 | } | |
520 | return 0; | |
521 | } | |
522 | ||
523 | static void check_buildid_dir_config(void) | |
524 | { | |
525 | struct buildid_dir_config c; | |
526 | c.dir = buildid_dir; | |
527 | perf_config(buildid_dir_command_config, &c); | |
528 | } | |
529 | ||
530 | void set_buildid_dir(void) | |
531 | { | |
532 | buildid_dir[0] = '\0'; | |
533 | ||
534 | /* try config file */ | |
535 | check_buildid_dir_config(); | |
536 | ||
537 | /* default to $HOME/.debug */ | |
538 | if (buildid_dir[0] == '\0') { | |
539 | char *v = getenv("HOME"); | |
540 | if (v) { | |
541 | snprintf(buildid_dir, MAXPATHLEN-1, "%s/%s", | |
542 | v, DEBUG_CACHE_DIR); | |
543 | } else { | |
544 | strncpy(buildid_dir, DEBUG_CACHE_DIR, MAXPATHLEN-1); | |
545 | } | |
546 | buildid_dir[MAXPATHLEN-1] = '\0'; | |
547 | } | |
548 | /* for communicating with external commands */ | |
549 | setenv("PERF_BUILDID_DIR", buildid_dir, 1); | |
550 | } |