config.c 9.5 KB

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