config.c 9.7 KB

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