config.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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. const char *config_exclusive_filename = NULL;
  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, len = 0, space = 0;
  45. for (;;) {
  46. int c = get_next_char();
  47. if (len >= sizeof(value) - 1)
  48. return NULL;
  49. if (c == '\n') {
  50. if (quote)
  51. return NULL;
  52. value[len] = 0;
  53. return value;
  54. }
  55. if (comment)
  56. continue;
  57. if (isspace(c) && !quote) {
  58. space = 1;
  59. continue;
  60. }
  61. if (!quote) {
  62. if (c == ';' || c == '#') {
  63. comment = 1;
  64. continue;
  65. }
  66. }
  67. if (space) {
  68. if (len)
  69. value[len++] = ' ';
  70. space = 0;
  71. }
  72. if (c == '\\') {
  73. c = get_next_char();
  74. switch (c) {
  75. case '\n':
  76. continue;
  77. case 't':
  78. c = '\t';
  79. break;
  80. case 'b':
  81. c = '\b';
  82. break;
  83. case 'n':
  84. c = '\n';
  85. break;
  86. /* Some characters escape as themselves */
  87. case '\\': case '"':
  88. break;
  89. /* Reject unknown escape sequences */
  90. default:
  91. return NULL;
  92. }
  93. value[len++] = c;
  94. continue;
  95. }
  96. if (c == '"') {
  97. quote = 1-quote;
  98. continue;
  99. }
  100. value[len++] = c;
  101. }
  102. }
  103. static inline int iskeychar(int c)
  104. {
  105. return isalnum(c) || c == '-';
  106. }
  107. static int get_value(config_fn_t fn, void *data, char *name, unsigned int len)
  108. {
  109. int c;
  110. char *value;
  111. /* Get the full name */
  112. for (;;) {
  113. c = get_next_char();
  114. if (config_file_eof)
  115. break;
  116. if (!iskeychar(c))
  117. break;
  118. name[len++] = tolower(c);
  119. if (len >= MAXNAME)
  120. return -1;
  121. }
  122. name[len] = 0;
  123. while (c == ' ' || c == '\t')
  124. c = get_next_char();
  125. value = NULL;
  126. if (c != '\n') {
  127. if (c != '=')
  128. return -1;
  129. value = parse_value();
  130. if (!value)
  131. return -1;
  132. }
  133. return fn(name, value, data);
  134. }
  135. static int get_extended_base_var(char *name, int baselen, int c)
  136. {
  137. do {
  138. if (c == '\n')
  139. return -1;
  140. c = get_next_char();
  141. } while (isspace(c));
  142. /* We require the format to be '[base "extension"]' */
  143. if (c != '"')
  144. return -1;
  145. name[baselen++] = '.';
  146. for (;;) {
  147. int c = get_next_char();
  148. if (c == '\n')
  149. return -1;
  150. if (c == '"')
  151. break;
  152. if (c == '\\') {
  153. c = get_next_char();
  154. if (c == '\n')
  155. return -1;
  156. }
  157. name[baselen++] = c;
  158. if (baselen > MAXNAME / 2)
  159. return -1;
  160. }
  161. /* Final ']' */
  162. if (get_next_char() != ']')
  163. return -1;
  164. return baselen;
  165. }
  166. static int get_base_var(char *name)
  167. {
  168. int baselen = 0;
  169. for (;;) {
  170. int c = get_next_char();
  171. if (config_file_eof)
  172. return -1;
  173. if (c == ']')
  174. return baselen;
  175. if (isspace(c))
  176. return get_extended_base_var(name, baselen, c);
  177. if (!iskeychar(c) && c != '.')
  178. return -1;
  179. if (baselen > MAXNAME / 2)
  180. return -1;
  181. name[baselen++] = tolower(c);
  182. }
  183. }
  184. static int perf_parse_file(config_fn_t fn, void *data)
  185. {
  186. int comment = 0;
  187. int baselen = 0;
  188. static char var[MAXNAME];
  189. /* U+FEFF Byte Order Mark in UTF8 */
  190. static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf";
  191. const unsigned char *bomptr = utf8_bom;
  192. for (;;) {
  193. int c = get_next_char();
  194. if (bomptr && *bomptr) {
  195. /* We are at the file beginning; skip UTF8-encoded BOM
  196. * if present. Sane editors won't put this in on their
  197. * own, but e.g. Windows Notepad will do it happily. */
  198. if ((unsigned char) c == *bomptr) {
  199. bomptr++;
  200. continue;
  201. } else {
  202. /* Do not tolerate partial BOM. */
  203. if (bomptr != utf8_bom)
  204. break;
  205. /* No BOM at file beginning. Cool. */
  206. bomptr = NULL;
  207. }
  208. }
  209. if (c == '\n') {
  210. if (config_file_eof)
  211. return 0;
  212. comment = 0;
  213. continue;
  214. }
  215. if (comment || isspace(c))
  216. continue;
  217. if (c == '#' || c == ';') {
  218. comment = 1;
  219. continue;
  220. }
  221. if (c == '[') {
  222. baselen = get_base_var(var);
  223. if (baselen <= 0)
  224. break;
  225. var[baselen++] = '.';
  226. var[baselen] = 0;
  227. continue;
  228. }
  229. if (!isalpha(c))
  230. break;
  231. var[baselen] = tolower(c);
  232. if (get_value(fn, data, var, baselen+1) < 0)
  233. break;
  234. }
  235. die("bad config file line %d in %s", config_linenr, config_file_name);
  236. }
  237. static int parse_unit_factor(const char *end, unsigned long *val)
  238. {
  239. if (!*end)
  240. return 1;
  241. else if (!strcasecmp(end, "k")) {
  242. *val *= 1024;
  243. return 1;
  244. }
  245. else if (!strcasecmp(end, "m")) {
  246. *val *= 1024 * 1024;
  247. return 1;
  248. }
  249. else if (!strcasecmp(end, "g")) {
  250. *val *= 1024 * 1024 * 1024;
  251. return 1;
  252. }
  253. return 0;
  254. }
  255. static int perf_parse_long(const char *value, long *ret)
  256. {
  257. if (value && *value) {
  258. char *end;
  259. long val = strtol(value, &end, 0);
  260. unsigned long factor = 1;
  261. if (!parse_unit_factor(end, &factor))
  262. return 0;
  263. *ret = val * factor;
  264. return 1;
  265. }
  266. return 0;
  267. }
  268. int perf_parse_ulong(const char *value, unsigned long *ret)
  269. {
  270. if (value && *value) {
  271. char *end;
  272. unsigned long val = strtoul(value, &end, 0);
  273. if (!parse_unit_factor(end, &val))
  274. return 0;
  275. *ret = val;
  276. return 1;
  277. }
  278. return 0;
  279. }
  280. static void die_bad_config(const char *name)
  281. {
  282. if (config_file_name)
  283. die("bad config value for '%s' in %s", name, config_file_name);
  284. die("bad config value for '%s'", name);
  285. }
  286. int perf_config_int(const char *name, const char *value)
  287. {
  288. long ret = 0;
  289. if (!perf_parse_long(value, &ret))
  290. die_bad_config(name);
  291. return ret;
  292. }
  293. unsigned long perf_config_ulong(const char *name, const char *value)
  294. {
  295. unsigned long ret;
  296. if (!perf_parse_ulong(value, &ret))
  297. die_bad_config(name);
  298. return ret;
  299. }
  300. int perf_config_bool_or_int(const char *name, const char *value, int *is_bool)
  301. {
  302. *is_bool = 1;
  303. if (!value)
  304. return 1;
  305. if (!*value)
  306. return 0;
  307. if (!strcasecmp(value, "true") || !strcasecmp(value, "yes") || !strcasecmp(value, "on"))
  308. return 1;
  309. if (!strcasecmp(value, "false") || !strcasecmp(value, "no") || !strcasecmp(value, "off"))
  310. return 0;
  311. *is_bool = 0;
  312. return perf_config_int(name, value);
  313. }
  314. int perf_config_bool(const char *name, const char *value)
  315. {
  316. int discard;
  317. return !!perf_config_bool_or_int(name, value, &discard);
  318. }
  319. int perf_config_string(const char **dest, const char *var, const char *value)
  320. {
  321. if (!value)
  322. return config_error_nonbool(var);
  323. *dest = strdup(value);
  324. return 0;
  325. }
  326. static int perf_default_core_config(const char *var, const char *value)
  327. {
  328. /* Add other config variables here and to Documentation/config.txt. */
  329. return 0;
  330. }
  331. int perf_default_config(const char *var, const char *value, void *dummy)
  332. {
  333. if (!prefixcmp(var, "core."))
  334. return perf_default_core_config(var, value);
  335. /* Add other config variables here and to Documentation/config.txt. */
  336. return 0;
  337. }
  338. int perf_config_from_file(config_fn_t fn, const char *filename, void *data)
  339. {
  340. int ret;
  341. FILE *f = fopen(filename, "r");
  342. ret = -1;
  343. if (f) {
  344. config_file = f;
  345. config_file_name = filename;
  346. config_linenr = 1;
  347. config_file_eof = 0;
  348. ret = perf_parse_file(fn, data);
  349. fclose(f);
  350. config_file_name = NULL;
  351. }
  352. return ret;
  353. }
  354. const char *perf_etc_perfconfig(void)
  355. {
  356. static const char *system_wide;
  357. if (!system_wide)
  358. system_wide = system_path(ETC_PERFCONFIG);
  359. return system_wide;
  360. }
  361. static int perf_env_bool(const char *k, int def)
  362. {
  363. const char *v = getenv(k);
  364. return v ? perf_config_bool(k, v) : def;
  365. }
  366. int perf_config_system(void)
  367. {
  368. return !perf_env_bool("PERF_CONFIG_NOSYSTEM", 0);
  369. }
  370. int perf_config_global(void)
  371. {
  372. return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0);
  373. }
  374. int perf_config(config_fn_t fn, void *data)
  375. {
  376. int ret = 0, found = 0;
  377. char *repo_config = NULL;
  378. const char *home = NULL;
  379. /* Setting $PERF_CONFIG makes perf read _only_ the given config file. */
  380. if (config_exclusive_filename)
  381. return perf_config_from_file(fn, config_exclusive_filename, data);
  382. if (perf_config_system() && !access(perf_etc_perfconfig(), R_OK)) {
  383. ret += perf_config_from_file(fn, perf_etc_perfconfig(),
  384. data);
  385. found += 1;
  386. }
  387. home = getenv("HOME");
  388. if (perf_config_global() && home) {
  389. char *user_config = strdup(mkpath("%s/.perfconfig", home));
  390. if (!access(user_config, R_OK)) {
  391. ret += perf_config_from_file(fn, user_config, data);
  392. found += 1;
  393. }
  394. free(user_config);
  395. }
  396. repo_config = perf_pathdup("config");
  397. if (!access(repo_config, R_OK)) {
  398. ret += perf_config_from_file(fn, repo_config, data);
  399. found += 1;
  400. }
  401. free(repo_config);
  402. if (found == 0)
  403. return -1;
  404. return ret;
  405. }
  406. /*
  407. * Find all the stuff for perf_config_set() below.
  408. */
  409. #define MAX_MATCHES 512
  410. static struct {
  411. int baselen;
  412. char* key;
  413. int do_not_match;
  414. regex_t* value_regex;
  415. int multi_replace;
  416. size_t offset[MAX_MATCHES];
  417. enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
  418. int seen;
  419. } store;
  420. static int matches(const char* key, const char* value)
  421. {
  422. return !strcmp(key, store.key) &&
  423. (store.value_regex == NULL ||
  424. (store.do_not_match ^
  425. !regexec(store.value_regex, value, 0, NULL, 0)));
  426. }
  427. static int store_aux(const char* key, const char* value, void *cb)
  428. {
  429. const char *ep;
  430. size_t section_len;
  431. switch (store.state) {
  432. case KEY_SEEN:
  433. if (matches(key, value)) {
  434. if (store.seen == 1 && store.multi_replace == 0) {
  435. warning("%s has multiple values", key);
  436. } else if (store.seen >= MAX_MATCHES) {
  437. error("too many matches for %s", key);
  438. return 1;
  439. }
  440. store.offset[store.seen] = ftell(config_file);
  441. store.seen++;
  442. }
  443. break;
  444. case SECTION_SEEN:
  445. /*
  446. * What we are looking for is in store.key (both
  447. * section and var), and its section part is baselen
  448. * long. We found key (again, both section and var).
  449. * We would want to know if this key is in the same
  450. * section as what we are looking for. We already
  451. * know we are in the same section as what should
  452. * hold store.key.
  453. */
  454. ep = strrchr(key, '.');
  455. section_len = ep - key;
  456. if ((section_len != store.baselen) ||
  457. memcmp(key, store.key, section_len+1)) {
  458. store.state = SECTION_END_SEEN;
  459. break;
  460. }
  461. /*
  462. * Do not increment matches: this is no match, but we
  463. * just made sure we are in the desired section.
  464. */
  465. store.offset[store.seen] = ftell(config_file);
  466. /* fallthru */
  467. case SECTION_END_SEEN:
  468. case START:
  469. if (matches(key, value)) {
  470. store.offset[store.seen] = ftell(config_file);
  471. store.state = KEY_SEEN;
  472. store.seen++;
  473. } else {
  474. if (strrchr(key, '.') - key == store.baselen &&
  475. !strncmp(key, store.key, store.baselen)) {
  476. store.state = SECTION_SEEN;
  477. store.offset[store.seen] = ftell(config_file);
  478. }
  479. }
  480. }
  481. return 0;
  482. }
  483. static int store_write_section(int fd, const char* key)
  484. {
  485. const char *dot;
  486. int i, success;
  487. struct strbuf sb = STRBUF_INIT;
  488. dot = memchr(key, '.', store.baselen);
  489. if (dot) {
  490. strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key);
  491. for (i = dot - key + 1; i < store.baselen; i++) {
  492. if (key[i] == '"' || key[i] == '\\')
  493. strbuf_addch(&sb, '\\');
  494. strbuf_addch(&sb, key[i]);
  495. }
  496. strbuf_addstr(&sb, "\"]\n");
  497. } else {
  498. strbuf_addf(&sb, "[%.*s]\n", store.baselen, key);
  499. }
  500. success = write_in_full(fd, sb.buf, sb.len) == sb.len;
  501. strbuf_release(&sb);
  502. return success;
  503. }
  504. static int store_write_pair(int fd, const char* key, const char* value)
  505. {
  506. int i, success;
  507. int length = strlen(key + store.baselen + 1);
  508. const char *quote = "";
  509. struct strbuf sb = STRBUF_INIT;
  510. /*
  511. * Check to see if the value needs to be surrounded with a dq pair.
  512. * Note that problematic characters are always backslash-quoted; this
  513. * check is about not losing leading or trailing SP and strings that
  514. * follow beginning-of-comment characters (i.e. ';' and '#') by the
  515. * configuration parser.
  516. */
  517. if (value[0] == ' ')
  518. quote = "\"";
  519. for (i = 0; value[i]; i++)
  520. if (value[i] == ';' || value[i] == '#')
  521. quote = "\"";
  522. if (i && value[i - 1] == ' ')
  523. quote = "\"";
  524. strbuf_addf(&sb, "\t%.*s = %s",
  525. length, key + store.baselen + 1, quote);
  526. for (i = 0; value[i]; i++)
  527. switch (value[i]) {
  528. case '\n':
  529. strbuf_addstr(&sb, "\\n");
  530. break;
  531. case '\t':
  532. strbuf_addstr(&sb, "\\t");
  533. break;
  534. case '"':
  535. case '\\':
  536. strbuf_addch(&sb, '\\');
  537. default:
  538. strbuf_addch(&sb, value[i]);
  539. break;
  540. }
  541. strbuf_addf(&sb, "%s\n", quote);
  542. success = write_in_full(fd, sb.buf, sb.len) == sb.len;
  543. strbuf_release(&sb);
  544. return success;
  545. }
  546. static ssize_t find_beginning_of_line(const char* contents, size_t size,
  547. size_t offset_, int* found_bracket)
  548. {
  549. size_t equal_offset = size, bracket_offset = size;
  550. ssize_t offset;
  551. contline:
  552. for (offset = offset_-2; offset > 0
  553. && contents[offset] != '\n'; offset--)
  554. switch (contents[offset]) {
  555. case '=': equal_offset = offset; break;
  556. case ']': bracket_offset = offset; break;
  557. }
  558. if (offset > 0 && contents[offset-1] == '\\') {
  559. offset_ = offset;
  560. goto contline;
  561. }
  562. if (bracket_offset < equal_offset) {
  563. *found_bracket = 1;
  564. offset = bracket_offset+1;
  565. } else
  566. offset++;
  567. return offset;
  568. }
  569. int perf_config_set(const char* key, const char* value)
  570. {
  571. return perf_config_set_multivar(key, value, NULL, 0);
  572. }
  573. /*
  574. * If value==NULL, unset in (remove from) config,
  575. * if value_regex!=NULL, disregard key/value pairs where value does not match.
  576. * if multi_replace==0, nothing, or only one matching key/value is replaced,
  577. * else all matching key/values (regardless how many) are removed,
  578. * before the new pair is written.
  579. *
  580. * Returns 0 on success.
  581. *
  582. * This function does this:
  583. *
  584. * - it locks the config file by creating ".perf/config.lock"
  585. *
  586. * - it then parses the config using store_aux() as validator to find
  587. * the position on the key/value pair to replace. If it is to be unset,
  588. * it must be found exactly once.
  589. *
  590. * - the config file is mmap()ed and the part before the match (if any) is
  591. * written to the lock file, then the changed part and the rest.
  592. *
  593. * - the config file is removed and the lock file rename()d to it.
  594. *
  595. */
  596. int perf_config_set_multivar(const char* key, const char* value,
  597. const char* value_regex, int multi_replace)
  598. {
  599. int i, dot;
  600. int fd = -1, in_fd;
  601. int ret = 0;
  602. char* config_filename;
  603. const char* last_dot = strrchr(key, '.');
  604. if (config_exclusive_filename)
  605. config_filename = strdup(config_exclusive_filename);
  606. else
  607. config_filename = perf_pathdup("config");
  608. /*
  609. * Since "key" actually contains the section name and the real
  610. * key name separated by a dot, we have to know where the dot is.
  611. */
  612. if (last_dot == NULL) {
  613. error("key does not contain a section: %s", key);
  614. ret = 2;
  615. goto out_free;
  616. }
  617. store.baselen = last_dot - key;
  618. store.multi_replace = multi_replace;
  619. /*
  620. * Validate the key and while at it, lower case it for matching.
  621. */
  622. store.key = malloc(strlen(key) + 1);
  623. dot = 0;
  624. for (i = 0; key[i]; i++) {
  625. unsigned char c = key[i];
  626. if (c == '.')
  627. dot = 1;
  628. /* Leave the extended basename untouched.. */
  629. if (!dot || i > store.baselen) {
  630. if (!iskeychar(c) || (i == store.baselen+1 && !isalpha(c))) {
  631. error("invalid key: %s", key);
  632. free(store.key);
  633. ret = 1;
  634. goto out_free;
  635. }
  636. c = tolower(c);
  637. } else if (c == '\n') {
  638. error("invalid key (newline): %s", key);
  639. free(store.key);
  640. ret = 1;
  641. goto out_free;
  642. }
  643. store.key[i] = c;
  644. }
  645. store.key[i] = 0;
  646. /*
  647. * If .perf/config does not exist yet, write a minimal version.
  648. */
  649. in_fd = open(config_filename, O_RDONLY);
  650. if ( in_fd < 0 ) {
  651. free(store.key);
  652. if ( ENOENT != errno ) {
  653. error("opening %s: %s", config_filename,
  654. strerror(errno));
  655. ret = 3; /* same as "invalid config file" */
  656. goto out_free;
  657. }
  658. /* if nothing to unset, error out */
  659. if (value == NULL) {
  660. ret = 5;
  661. goto out_free;
  662. }
  663. store.key = (char*)key;
  664. if (!store_write_section(fd, key) ||
  665. !store_write_pair(fd, key, value))
  666. goto write_err_out;
  667. } else {
  668. struct stat st;
  669. char* contents;
  670. size_t contents_sz, copy_begin, copy_end;
  671. int i, new_line = 0;
  672. if (value_regex == NULL)
  673. store.value_regex = NULL;
  674. else {
  675. if (value_regex[0] == '!') {
  676. store.do_not_match = 1;
  677. value_regex++;
  678. } else
  679. store.do_not_match = 0;
  680. store.value_regex = (regex_t*)malloc(sizeof(regex_t));
  681. if (regcomp(store.value_regex, value_regex,
  682. REG_EXTENDED)) {
  683. error("invalid pattern: %s", value_regex);
  684. free(store.value_regex);
  685. ret = 6;
  686. goto out_free;
  687. }
  688. }
  689. store.offset[0] = 0;
  690. store.state = START;
  691. store.seen = 0;
  692. /*
  693. * After this, store.offset will contain the *end* offset
  694. * of the last match, or remain at 0 if no match was found.
  695. * As a side effect, we make sure to transform only a valid
  696. * existing config file.
  697. */
  698. if (perf_config_from_file(store_aux, config_filename, NULL)) {
  699. error("invalid config file %s", config_filename);
  700. free(store.key);
  701. if (store.value_regex != NULL) {
  702. regfree(store.value_regex);
  703. free(store.value_regex);
  704. }
  705. ret = 3;
  706. goto out_free;
  707. }
  708. free(store.key);
  709. if (store.value_regex != NULL) {
  710. regfree(store.value_regex);
  711. free(store.value_regex);
  712. }
  713. /* if nothing to unset, or too many matches, error out */
  714. if ((store.seen == 0 && value == NULL) ||
  715. (store.seen > 1 && multi_replace == 0)) {
  716. ret = 5;
  717. goto out_free;
  718. }
  719. fstat(in_fd, &st);
  720. contents_sz = xsize_t(st.st_size);
  721. contents = mmap(NULL, contents_sz, PROT_READ,
  722. MAP_PRIVATE, in_fd, 0);
  723. close(in_fd);
  724. if (store.seen == 0)
  725. store.seen = 1;
  726. for (i = 0, copy_begin = 0; i < store.seen; i++) {
  727. if (store.offset[i] == 0) {
  728. store.offset[i] = copy_end = contents_sz;
  729. } else if (store.state != KEY_SEEN) {
  730. copy_end = store.offset[i];
  731. } else
  732. copy_end = find_beginning_of_line(
  733. contents, contents_sz,
  734. store.offset[i]-2, &new_line);
  735. if (copy_end > 0 && contents[copy_end-1] != '\n')
  736. new_line = 1;
  737. /* write the first part of the config */
  738. if (copy_end > copy_begin) {
  739. if (write_in_full(fd, contents + copy_begin,
  740. copy_end - copy_begin) <
  741. copy_end - copy_begin)
  742. goto write_err_out;
  743. if (new_line &&
  744. write_in_full(fd, "\n", 1) != 1)
  745. goto write_err_out;
  746. }
  747. copy_begin = store.offset[i];
  748. }
  749. /* write the pair (value == NULL means unset) */
  750. if (value != NULL) {
  751. if (store.state == START) {
  752. if (!store_write_section(fd, key))
  753. goto write_err_out;
  754. }
  755. if (!store_write_pair(fd, key, value))
  756. goto write_err_out;
  757. }
  758. /* write the rest of the config */
  759. if (copy_begin < contents_sz)
  760. if (write_in_full(fd, contents + copy_begin,
  761. contents_sz - copy_begin) <
  762. contents_sz - copy_begin)
  763. goto write_err_out;
  764. munmap(contents, contents_sz);
  765. }
  766. ret = 0;
  767. out_free:
  768. free(config_filename);
  769. return ret;
  770. write_err_out:
  771. goto out_free;
  772. }
  773. /*
  774. * Call this to report error for your variable that should not
  775. * get a boolean value (i.e. "[my] var" means "true").
  776. */
  777. int config_error_nonbool(const char *var)
  778. {
  779. return error("Missing value for '%s'", var);
  780. }