config.c 9.7 KB

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