config.c 8.2 KB

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