config.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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, 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 c = get_next_char();
  149. if (c == '\n')
  150. return -1;
  151. if (c == '"')
  152. break;
  153. if (c == '\\') {
  154. c = get_next_char();
  155. if (c == '\n')
  156. return -1;
  157. }
  158. name[baselen++] = c;
  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. int perf_parse_ulong(const char *value, unsigned long *ret)
  270. {
  271. if (value && *value) {
  272. char *end;
  273. unsigned long val = strtoul(value, &end, 0);
  274. if (!parse_unit_factor(end, &val))
  275. return 0;
  276. *ret = val;
  277. return 1;
  278. }
  279. return 0;
  280. }
  281. static void die_bad_config(const char *name)
  282. {
  283. if (config_file_name)
  284. die("bad config value for '%s' in %s", name, config_file_name);
  285. die("bad config value for '%s'", name);
  286. }
  287. int perf_config_int(const char *name, const char *value)
  288. {
  289. long ret = 0;
  290. if (!perf_parse_long(value, &ret))
  291. die_bad_config(name);
  292. return ret;
  293. }
  294. unsigned long perf_config_ulong(const char *name, const char *value)
  295. {
  296. unsigned long ret;
  297. if (!perf_parse_ulong(value, &ret))
  298. die_bad_config(name);
  299. return ret;
  300. }
  301. int perf_config_bool_or_int(const char *name, const char *value, int *is_bool)
  302. {
  303. *is_bool = 1;
  304. if (!value)
  305. return 1;
  306. if (!*value)
  307. return 0;
  308. if (!strcasecmp(value, "true") || !strcasecmp(value, "yes") || !strcasecmp(value, "on"))
  309. return 1;
  310. if (!strcasecmp(value, "false") || !strcasecmp(value, "no") || !strcasecmp(value, "off"))
  311. return 0;
  312. *is_bool = 0;
  313. return perf_config_int(name, value);
  314. }
  315. int perf_config_bool(const char *name, const char *value)
  316. {
  317. int discard;
  318. return !!perf_config_bool_or_int(name, value, &discard);
  319. }
  320. int perf_config_string(const char **dest, const char *var, const char *value)
  321. {
  322. if (!value)
  323. return config_error_nonbool(var);
  324. *dest = strdup(value);
  325. return 0;
  326. }
  327. static int perf_default_core_config(const char *var __used, const char *value __used)
  328. {
  329. /* Add other config variables here and to Documentation/config.txt. */
  330. return 0;
  331. }
  332. int perf_default_config(const char *var, const char *value, void *dummy __used)
  333. {
  334. if (!prefixcmp(var, "core."))
  335. return perf_default_core_config(var, value);
  336. /* Add other config variables here and to Documentation/config.txt. */
  337. return 0;
  338. }
  339. int perf_config_from_file(config_fn_t fn, const char *filename, void *data)
  340. {
  341. int ret;
  342. FILE *f = fopen(filename, "r");
  343. ret = -1;
  344. if (f) {
  345. config_file = f;
  346. config_file_name = filename;
  347. config_linenr = 1;
  348. config_file_eof = 0;
  349. ret = perf_parse_file(fn, data);
  350. fclose(f);
  351. config_file_name = NULL;
  352. }
  353. return ret;
  354. }
  355. const char *perf_etc_perfconfig(void)
  356. {
  357. static const char *system_wide;
  358. if (!system_wide)
  359. system_wide = system_path(ETC_PERFCONFIG);
  360. return system_wide;
  361. }
  362. static int perf_env_bool(const char *k, int def)
  363. {
  364. const char *v = getenv(k);
  365. return v ? perf_config_bool(k, v) : def;
  366. }
  367. int perf_config_system(void)
  368. {
  369. return !perf_env_bool("PERF_CONFIG_NOSYSTEM", 0);
  370. }
  371. int perf_config_global(void)
  372. {
  373. return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0);
  374. }
  375. int perf_config(config_fn_t fn, void *data)
  376. {
  377. int ret = 0, found = 0;
  378. char *repo_config = NULL;
  379. const char *home = NULL;
  380. /* Setting $PERF_CONFIG makes perf read _only_ the given config file. */
  381. if (config_exclusive_filename)
  382. return perf_config_from_file(fn, config_exclusive_filename, data);
  383. if (perf_config_system() && !access(perf_etc_perfconfig(), R_OK)) {
  384. ret += perf_config_from_file(fn, perf_etc_perfconfig(),
  385. data);
  386. found += 1;
  387. }
  388. home = getenv("HOME");
  389. if (perf_config_global() && home) {
  390. char *user_config = strdup(mkpath("%s/.perfconfig", home));
  391. if (!access(user_config, R_OK)) {
  392. ret += perf_config_from_file(fn, user_config, data);
  393. found += 1;
  394. }
  395. free(user_config);
  396. }
  397. repo_config = perf_pathdup("config");
  398. if (!access(repo_config, R_OK)) {
  399. ret += perf_config_from_file(fn, repo_config, data);
  400. found += 1;
  401. }
  402. free(repo_config);
  403. if (found == 0)
  404. return -1;
  405. return ret;
  406. }
  407. /*
  408. * Find all the stuff for perf_config_set() below.
  409. */
  410. #define MAX_MATCHES 512
  411. static struct {
  412. int baselen;
  413. char* key;
  414. int do_not_match;
  415. regex_t* value_regex;
  416. int multi_replace;
  417. size_t offset[MAX_MATCHES];
  418. enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
  419. int seen;
  420. } store;
  421. static int matches(const char* key, const char* value)
  422. {
  423. return !strcmp(key, store.key) &&
  424. (store.value_regex == NULL ||
  425. (store.do_not_match ^
  426. !regexec(store.value_regex, value, 0, NULL, 0)));
  427. }
  428. static int store_aux(const char* key, const char* value, void *cb __used)
  429. {
  430. int section_len;
  431. const char *ep;
  432. switch (store.state) {
  433. case KEY_SEEN:
  434. if (matches(key, value)) {
  435. if (store.seen == 1 && store.multi_replace == 0) {
  436. warning("%s has multiple values", key);
  437. } else if (store.seen >= MAX_MATCHES) {
  438. error("too many matches for %s", key);
  439. return 1;
  440. }
  441. store.offset[store.seen] = ftell(config_file);
  442. store.seen++;
  443. }
  444. break;
  445. case SECTION_SEEN:
  446. /*
  447. * What we are looking for is in store.key (both
  448. * section and var), and its section part is baselen
  449. * long. We found key (again, both section and var).
  450. * We would want to know if this key is in the same
  451. * section as what we are looking for. We already
  452. * know we are in the same section as what should
  453. * hold store.key.
  454. */
  455. ep = strrchr(key, '.');
  456. section_len = ep - key;
  457. if ((section_len != store.baselen) ||
  458. memcmp(key, store.key, section_len+1)) {
  459. store.state = SECTION_END_SEEN;
  460. break;
  461. }
  462. /*
  463. * Do not increment matches: this is no match, but we
  464. * just made sure we are in the desired section.
  465. */
  466. store.offset[store.seen] = ftell(config_file);
  467. /* fallthru */
  468. case SECTION_END_SEEN:
  469. case START:
  470. if (matches(key, value)) {
  471. store.offset[store.seen] = ftell(config_file);
  472. store.state = KEY_SEEN;
  473. store.seen++;
  474. } else {
  475. if (strrchr(key, '.') - key == store.baselen &&
  476. !strncmp(key, store.key, store.baselen)) {
  477. store.state = SECTION_SEEN;
  478. store.offset[store.seen] = ftell(config_file);
  479. }
  480. }
  481. }
  482. return 0;
  483. }
  484. static int store_write_section(int fd, const char* key)
  485. {
  486. const char *dot;
  487. int i, success;
  488. struct strbuf sb = STRBUF_INIT;
  489. dot = memchr(key, '.', store.baselen);
  490. if (dot) {
  491. strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key);
  492. for (i = dot - key + 1; i < store.baselen; i++) {
  493. if (key[i] == '"' || key[i] == '\\')
  494. strbuf_addch(&sb, '\\');
  495. strbuf_addch(&sb, key[i]);
  496. }
  497. strbuf_addstr(&sb, "\"]\n");
  498. } else {
  499. strbuf_addf(&sb, "[%.*s]\n", store.baselen, key);
  500. }
  501. success = (write_in_full(fd, sb.buf, sb.len) == (ssize_t)sb.len);
  502. strbuf_release(&sb);
  503. return success;
  504. }
  505. static int store_write_pair(int fd, const char* key, const char* value)
  506. {
  507. int i, success;
  508. int length = strlen(key + store.baselen + 1);
  509. const char *quote = "";
  510. struct strbuf sb = STRBUF_INIT;
  511. /*
  512. * Check to see if the value needs to be surrounded with a dq pair.
  513. * Note that problematic characters are always backslash-quoted; this
  514. * check is about not losing leading or trailing SP and strings that
  515. * follow beginning-of-comment characters (i.e. ';' and '#') by the
  516. * configuration parser.
  517. */
  518. if (value[0] == ' ')
  519. quote = "\"";
  520. for (i = 0; value[i]; i++)
  521. if (value[i] == ';' || value[i] == '#')
  522. quote = "\"";
  523. if (i && value[i - 1] == ' ')
  524. quote = "\"";
  525. strbuf_addf(&sb, "\t%.*s = %s",
  526. length, key + store.baselen + 1, quote);
  527. for (i = 0; value[i]; i++)
  528. switch (value[i]) {
  529. case '\n':
  530. strbuf_addstr(&sb, "\\n");
  531. break;
  532. case '\t':
  533. strbuf_addstr(&sb, "\\t");
  534. break;
  535. case '"':
  536. case '\\':
  537. strbuf_addch(&sb, '\\');
  538. default:
  539. strbuf_addch(&sb, value[i]);
  540. break;
  541. }
  542. strbuf_addf(&sb, "%s\n", quote);
  543. success = (write_in_full(fd, sb.buf, sb.len) == (ssize_t)sb.len);
  544. strbuf_release(&sb);
  545. return success;
  546. }
  547. static ssize_t find_beginning_of_line(const char* contents, size_t size,
  548. size_t offset_, int* found_bracket)
  549. {
  550. size_t equal_offset = size, bracket_offset = size;
  551. ssize_t offset;
  552. contline:
  553. for (offset = offset_-2; offset > 0
  554. && contents[offset] != '\n'; offset--)
  555. switch (contents[offset]) {
  556. case '=': equal_offset = offset; break;
  557. case ']': bracket_offset = offset; break;
  558. }
  559. if (offset > 0 && contents[offset-1] == '\\') {
  560. offset_ = offset;
  561. goto contline;
  562. }
  563. if (bracket_offset < equal_offset) {
  564. *found_bracket = 1;
  565. offset = bracket_offset+1;
  566. } else
  567. offset++;
  568. return offset;
  569. }
  570. int perf_config_set(const char* key, const char* value)
  571. {
  572. return perf_config_set_multivar(key, value, NULL, 0);
  573. }
  574. /*
  575. * If value==NULL, unset in (remove from) config,
  576. * if value_regex!=NULL, disregard key/value pairs where value does not match.
  577. * if multi_replace==0, nothing, or only one matching key/value is replaced,
  578. * else all matching key/values (regardless how many) are removed,
  579. * before the new pair is written.
  580. *
  581. * Returns 0 on success.
  582. *
  583. * This function does this:
  584. *
  585. * - it locks the config file by creating ".perf/config.lock"
  586. *
  587. * - it then parses the config using store_aux() as validator to find
  588. * the position on the key/value pair to replace. If it is to be unset,
  589. * it must be found exactly once.
  590. *
  591. * - the config file is mmap()ed and the part before the match (if any) is
  592. * written to the lock file, then the changed part and the rest.
  593. *
  594. * - the config file is removed and the lock file rename()d to it.
  595. *
  596. */
  597. int perf_config_set_multivar(const char* key, const char* value,
  598. const char* value_regex, int multi_replace)
  599. {
  600. int i, dot;
  601. int fd = -1, in_fd;
  602. int ret = 0;
  603. char* config_filename;
  604. const char* last_dot = strrchr(key, '.');
  605. if (config_exclusive_filename)
  606. config_filename = strdup(config_exclusive_filename);
  607. else
  608. config_filename = perf_pathdup("config");
  609. /*
  610. * Since "key" actually contains the section name and the real
  611. * key name separated by a dot, we have to know where the dot is.
  612. */
  613. if (last_dot == NULL) {
  614. error("key does not contain a section: %s", key);
  615. ret = 2;
  616. goto out_free;
  617. }
  618. store.baselen = last_dot - key;
  619. store.multi_replace = multi_replace;
  620. /*
  621. * Validate the key and while at it, lower case it for matching.
  622. */
  623. store.key = malloc(strlen(key) + 1);
  624. dot = 0;
  625. for (i = 0; key[i]; i++) {
  626. unsigned char c = key[i];
  627. if (c == '.')
  628. dot = 1;
  629. /* Leave the extended basename untouched.. */
  630. if (!dot || i > store.baselen) {
  631. if (!iskeychar(c) || (i == store.baselen+1 && !isalpha(c))) {
  632. error("invalid key: %s", key);
  633. free(store.key);
  634. ret = 1;
  635. goto out_free;
  636. }
  637. c = tolower(c);
  638. } else if (c == '\n') {
  639. error("invalid key (newline): %s", key);
  640. free(store.key);
  641. ret = 1;
  642. goto out_free;
  643. }
  644. store.key[i] = c;
  645. }
  646. store.key[i] = 0;
  647. /*
  648. * If .perf/config does not exist yet, write a minimal version.
  649. */
  650. in_fd = open(config_filename, O_RDONLY);
  651. if ( in_fd < 0 ) {
  652. free(store.key);
  653. if ( ENOENT != errno ) {
  654. error("opening %s: %s", config_filename,
  655. strerror(errno));
  656. ret = 3; /* same as "invalid config file" */
  657. goto out_free;
  658. }
  659. /* if nothing to unset, error out */
  660. if (value == NULL) {
  661. ret = 5;
  662. goto out_free;
  663. }
  664. store.key = (char*)key;
  665. if (!store_write_section(fd, key) ||
  666. !store_write_pair(fd, key, value))
  667. goto write_err_out;
  668. } else {
  669. struct stat st;
  670. char* contents;
  671. ssize_t contents_sz, copy_begin, copy_end;
  672. int i, new_line = 0;
  673. if (value_regex == NULL)
  674. store.value_regex = NULL;
  675. else {
  676. if (value_regex[0] == '!') {
  677. store.do_not_match = 1;
  678. value_regex++;
  679. } else
  680. store.do_not_match = 0;
  681. store.value_regex = (regex_t*)malloc(sizeof(regex_t));
  682. if (regcomp(store.value_regex, value_regex,
  683. REG_EXTENDED)) {
  684. error("invalid pattern: %s", value_regex);
  685. free(store.value_regex);
  686. ret = 6;
  687. goto out_free;
  688. }
  689. }
  690. store.offset[0] = 0;
  691. store.state = START;
  692. store.seen = 0;
  693. /*
  694. * After this, store.offset will contain the *end* offset
  695. * of the last match, or remain at 0 if no match was found.
  696. * As a side effect, we make sure to transform only a valid
  697. * existing config file.
  698. */
  699. if (perf_config_from_file(store_aux, config_filename, NULL)) {
  700. error("invalid config file %s", config_filename);
  701. free(store.key);
  702. if (store.value_regex != NULL) {
  703. regfree(store.value_regex);
  704. free(store.value_regex);
  705. }
  706. ret = 3;
  707. goto out_free;
  708. }
  709. free(store.key);
  710. if (store.value_regex != NULL) {
  711. regfree(store.value_regex);
  712. free(store.value_regex);
  713. }
  714. /* if nothing to unset, or too many matches, error out */
  715. if ((store.seen == 0 && value == NULL) ||
  716. (store.seen > 1 && multi_replace == 0)) {
  717. ret = 5;
  718. goto out_free;
  719. }
  720. fstat(in_fd, &st);
  721. contents_sz = xsize_t(st.st_size);
  722. contents = mmap(NULL, contents_sz, PROT_READ,
  723. MAP_PRIVATE, in_fd, 0);
  724. close(in_fd);
  725. if (store.seen == 0)
  726. store.seen = 1;
  727. for (i = 0, copy_begin = 0; i < store.seen; i++) {
  728. if (store.offset[i] == 0) {
  729. store.offset[i] = copy_end = contents_sz;
  730. } else if (store.state != KEY_SEEN) {
  731. copy_end = store.offset[i];
  732. } else
  733. copy_end = find_beginning_of_line(
  734. contents, contents_sz,
  735. store.offset[i]-2, &new_line);
  736. if (copy_end > 0 && contents[copy_end-1] != '\n')
  737. new_line = 1;
  738. /* write the first part of the config */
  739. if (copy_end > copy_begin) {
  740. if (write_in_full(fd, contents + copy_begin,
  741. copy_end - copy_begin) <
  742. copy_end - copy_begin)
  743. goto write_err_out;
  744. if (new_line &&
  745. write_in_full(fd, "\n", 1) != 1)
  746. goto write_err_out;
  747. }
  748. copy_begin = store.offset[i];
  749. }
  750. /* write the pair (value == NULL means unset) */
  751. if (value != NULL) {
  752. if (store.state == START) {
  753. if (!store_write_section(fd, key))
  754. goto write_err_out;
  755. }
  756. if (!store_write_pair(fd, key, value))
  757. goto write_err_out;
  758. }
  759. /* write the rest of the config */
  760. if (copy_begin < contents_sz)
  761. if (write_in_full(fd, contents + copy_begin,
  762. contents_sz - copy_begin) <
  763. contents_sz - copy_begin)
  764. goto write_err_out;
  765. munmap(contents, contents_sz);
  766. }
  767. ret = 0;
  768. out_free:
  769. free(config_filename);
  770. return ret;
  771. write_err_out:
  772. goto out_free;
  773. }
  774. /*
  775. * Call this to report error for your variable that should not
  776. * get a boolean value (i.e. "[my] var" means "true").
  777. */
  778. int config_error_nonbool(const char *var)
  779. {
  780. return error("Missing value for '%s'", var);
  781. }