config.c 10 KB

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