config.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. int perf_default_config(const char *var, const char *value,
  320. void *dummy __maybe_unused)
  321. {
  322. if (!prefixcmp(var, "core."))
  323. return perf_default_core_config(var, value);
  324. if (!prefixcmp(var, "hist."))
  325. return perf_hist_config(var, value);
  326. /* Add other config variables here. */
  327. return 0;
  328. }
  329. static int perf_config_from_file(config_fn_t fn, const char *filename, void *data)
  330. {
  331. int ret;
  332. FILE *f = fopen(filename, "r");
  333. ret = -1;
  334. if (f) {
  335. config_file = f;
  336. config_file_name = filename;
  337. config_linenr = 1;
  338. config_file_eof = 0;
  339. ret = perf_parse_file(fn, data);
  340. fclose(f);
  341. config_file_name = NULL;
  342. }
  343. return ret;
  344. }
  345. static const char *perf_etc_perfconfig(void)
  346. {
  347. static const char *system_wide;
  348. if (!system_wide)
  349. system_wide = system_path(ETC_PERFCONFIG);
  350. return system_wide;
  351. }
  352. static int perf_env_bool(const char *k, int def)
  353. {
  354. const char *v = getenv(k);
  355. return v ? perf_config_bool(k, v) : def;
  356. }
  357. static int perf_config_system(void)
  358. {
  359. return !perf_env_bool("PERF_CONFIG_NOSYSTEM", 0);
  360. }
  361. static int perf_config_global(void)
  362. {
  363. return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0);
  364. }
  365. int perf_config(config_fn_t fn, void *data)
  366. {
  367. int ret = 0, found = 0;
  368. const char *home = NULL;
  369. /* Setting $PERF_CONFIG makes perf read _only_ the given config file. */
  370. if (config_exclusive_filename)
  371. return perf_config_from_file(fn, config_exclusive_filename, data);
  372. if (perf_config_system() && !access(perf_etc_perfconfig(), R_OK)) {
  373. ret += perf_config_from_file(fn, perf_etc_perfconfig(),
  374. data);
  375. found += 1;
  376. }
  377. home = getenv("HOME");
  378. if (perf_config_global() && home) {
  379. char *user_config = strdup(mkpath("%s/.perfconfig", home));
  380. struct stat st;
  381. if (user_config == NULL) {
  382. warning("Not enough memory to process %s/.perfconfig, "
  383. "ignoring it.", home);
  384. goto out;
  385. }
  386. if (stat(user_config, &st) < 0)
  387. goto out_free;
  388. if (st.st_uid && (st.st_uid != geteuid())) {
  389. warning("File %s not owned by current user or root, "
  390. "ignoring it.", user_config);
  391. goto out_free;
  392. }
  393. if (!st.st_size)
  394. goto out_free;
  395. ret += perf_config_from_file(fn, user_config, data);
  396. found += 1;
  397. out_free:
  398. free(user_config);
  399. }
  400. out:
  401. if (found == 0)
  402. return -1;
  403. return ret;
  404. }
  405. /*
  406. * Call this to report error for your variable that should not
  407. * get a boolean value (i.e. "[my] var" means "true").
  408. */
  409. int config_error_nonbool(const char *var)
  410. {
  411. return error("Missing value for '%s'", var);
  412. }
  413. struct buildid_dir_config {
  414. char *dir;
  415. };
  416. static int buildid_dir_command_config(const char *var, const char *value,
  417. void *data)
  418. {
  419. struct buildid_dir_config *c = data;
  420. const char *v;
  421. /* same dir for all commands */
  422. if (!prefixcmp(var, "buildid.") && !strcmp(var + 8, "dir")) {
  423. v = perf_config_dirname(var, value);
  424. if (!v)
  425. return -1;
  426. strncpy(c->dir, v, MAXPATHLEN-1);
  427. c->dir[MAXPATHLEN-1] = '\0';
  428. }
  429. return 0;
  430. }
  431. static void check_buildid_dir_config(void)
  432. {
  433. struct buildid_dir_config c;
  434. c.dir = buildid_dir;
  435. perf_config(buildid_dir_command_config, &c);
  436. }
  437. void set_buildid_dir(void)
  438. {
  439. buildid_dir[0] = '\0';
  440. /* try config file */
  441. check_buildid_dir_config();
  442. /* default to $HOME/.debug */
  443. if (buildid_dir[0] == '\0') {
  444. char *v = getenv("HOME");
  445. if (v) {
  446. snprintf(buildid_dir, MAXPATHLEN-1, "%s/%s",
  447. v, DEBUG_CACHE_DIR);
  448. } else {
  449. strncpy(buildid_dir, DEBUG_CACHE_DIR, MAXPATHLEN-1);
  450. }
  451. buildid_dir[MAXPATHLEN-1] = '\0';
  452. }
  453. /* for communicating with external commands */
  454. setenv("PERF_BUILDID_DIR", buildid_dir, 1);
  455. }