]> git.ipfire.org Git - thirdparty/git.git/blame - config.c
Add a new option 'core.askpass'.
[thirdparty/git.git] / config.c
CommitLineData
10bea152
JS
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Johannes Schindelin, 2005
6 *
7 */
17712991 8#include "cache.h"
7f0e39fa 9#include "exec_cmd.h"
572e4f6a 10#include "strbuf.h"
17712991
LT
11
12#define MAXNAME (256)
13
14static FILE *config_file;
0dccc7dc 15static const char *config_file_name;
17712991 16static int config_linenr;
02e5ba4a 17static int config_file_eof;
960ccca6
DH
18static int zlib_compression_seen;
19
dc871831
DB
20const char *config_exclusive_filename = NULL;
21
8b1fa778
AR
22struct config_item
23{
24 struct config_item *next;
572e4f6a 25 char *name;
8b1fa778 26 char *value;
8b1fa778
AR
27};
28static struct config_item *config_parameters;
29static struct config_item **config_parameters_tail = &config_parameters;
30
31static void lowercase(char *p)
32{
33 for (; *p; p++)
34 *p = tolower(*p);
35}
8b1fa778
AR
36
37int git_config_parse_parameter(const char *text)
38{
39 struct config_item *ct;
572e4f6a
AR
40 struct strbuf tmp = STRBUF_INIT;
41 struct strbuf **pair;
42 strbuf_addstr(&tmp, text);
43 pair = strbuf_split(&tmp, '=');
44 if (pair[0]->len && pair[0]->buf[pair[0]->len - 1] == '=')
45 strbuf_setlen(pair[0], pair[0]->len - 1);
46 strbuf_trim(pair[0]);
47 if (!pair[0]->len) {
48 strbuf_list_free(pair);
8b1fa778 49 return -1;
8b1fa778 50 }
572e4f6a
AR
51 ct = xcalloc(1, sizeof(struct config_item));
52 ct->name = strbuf_detach(pair[0], NULL);
53 if (pair[1]) {
54 strbuf_trim(pair[1]);
55 ct->value = strbuf_detach(pair[1], NULL);
56 }
57 strbuf_list_free(pair);
58 lowercase(ct->name);
8b1fa778
AR
59 *config_parameters_tail = ct;
60 config_parameters_tail = &ct->next;
61 return 0;
62}
63
17712991
LT
64static int get_next_char(void)
65{
66 int c;
67 FILE *f;
68
69 c = '\n';
70 if ((f = config_file) != NULL) {
71 c = fgetc(f);
db2c075d
JH
72 if (c == '\r') {
73 /* DOS like systems */
74 c = fgetc(f);
75 if (c != '\n') {
76 ungetc(c, f);
77 c = '\r';
78 }
79 }
17712991
LT
80 if (c == '\n')
81 config_linenr++;
82 if (c == EOF) {
02e5ba4a 83 config_file_eof = 1;
17712991
LT
84 c = '\n';
85 }
86 }
87 return c;
88}
89
90static char *parse_value(void)
91{
92 static char value[1024];
93 int quote = 0, comment = 0, len = 0, space = 0;
94
95 for (;;) {
96 int c = get_next_char();
e0b3cc0d 97 if (len >= sizeof(value) - 1)
17712991
LT
98 return NULL;
99 if (c == '\n') {
100 if (quote)
101 return NULL;
102 value[len] = 0;
103 return value;
104 }
105 if (comment)
106 continue;
107 if (isspace(c) && !quote) {
ebdaae37
BS
108 if (len)
109 space++;
17712991
LT
110 continue;
111 }
7ebdba61
JS
112 if (!quote) {
113 if (c == ';' || c == '#') {
114 comment = 1;
115 continue;
116 }
117 }
ebdaae37
BS
118 for (; space; space--)
119 value[len++] = ' ';
17712991
LT
120 if (c == '\\') {
121 c = get_next_char();
122 switch (c) {
123 case '\n':
124 continue;
125 case 't':
126 c = '\t';
127 break;
128 case 'b':
129 c = '\b';
130 break;
131 case 'n':
132 c = '\n';
133 break;
5cbb401d
LT
134 /* Some characters escape as themselves */
135 case '\\': case '"':
136 break;
137 /* Reject unknown escape sequences */
138 default:
139 return NULL;
17712991
LT
140 }
141 value[len++] = c;
142 continue;
143 }
144 if (c == '"') {
145 quote = 1-quote;
146 continue;
147 }
17712991
LT
148 value[len++] = c;
149 }
150}
151
38c5afa8
LT
152static inline int iskeychar(int c)
153{
154 return isalnum(c) || c == '-';
155}
156
ef90d6d4 157static int get_value(config_fn_t fn, void *data, char *name, unsigned int len)
17712991
LT
158{
159 int c;
160 char *value;
161
162 /* Get the full name */
163 for (;;) {
164 c = get_next_char();
02e5ba4a 165 if (config_file_eof)
17712991 166 break;
38c5afa8 167 if (!iskeychar(c))
17712991
LT
168 break;
169 name[len++] = tolower(c);
170 if (len >= MAXNAME)
171 return -1;
172 }
173 name[len] = 0;
174 while (c == ' ' || c == '\t')
175 c = get_next_char();
176
177 value = NULL;
178 if (c != '\n') {
179 if (c != '=')
180 return -1;
181 value = parse_value();
182 if (!value)
183 return -1;
184 }
ef90d6d4 185 return fn(name, value, data);
17712991
LT
186}
187
d14f7764
LT
188static int get_extended_base_var(char *name, int baselen, int c)
189{
190 do {
191 if (c == '\n')
192 return -1;
193 c = get_next_char();
194 } while (isspace(c));
195
196 /* We require the format to be '[base "extension"]' */
197 if (c != '"')
198 return -1;
199 name[baselen++] = '.';
200
201 for (;;) {
202 int c = get_next_char();
203 if (c == '\n')
204 return -1;
205 if (c == '"')
206 break;
207 if (c == '\\') {
208 c = get_next_char();
209 if (c == '\n')
210 return -1;
211 }
212 name[baselen++] = c;
213 if (baselen > MAXNAME / 2)
214 return -1;
215 }
216
217 /* Final ']' */
218 if (get_next_char() != ']')
219 return -1;
220 return baselen;
221}
222
17712991
LT
223static int get_base_var(char *name)
224{
225 int baselen = 0;
226
227 for (;;) {
228 int c = get_next_char();
02e5ba4a 229 if (config_file_eof)
17712991
LT
230 return -1;
231 if (c == ']')
232 return baselen;
d14f7764
LT
233 if (isspace(c))
234 return get_extended_base_var(name, baselen, c);
38c5afa8 235 if (!iskeychar(c) && c != '.')
17712991
LT
236 return -1;
237 if (baselen > MAXNAME / 2)
238 return -1;
239 name[baselen++] = tolower(c);
240 }
241}
242
ef90d6d4 243static int git_parse_file(config_fn_t fn, void *data)
17712991
LT
244{
245 int comment = 0;
246 int baselen = 0;
247 static char var[MAXNAME];
248
de056402
PB
249 /* U+FEFF Byte Order Mark in UTF8 */
250 static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf";
251 const unsigned char *bomptr = utf8_bom;
252
17712991
LT
253 for (;;) {
254 int c = get_next_char();
de056402
PB
255 if (bomptr && *bomptr) {
256 /* We are at the file beginning; skip UTF8-encoded BOM
257 * if present. Sane editors won't put this in on their
258 * own, but e.g. Windows Notepad will do it happily. */
259 if ((unsigned char) c == *bomptr) {
260 bomptr++;
261 continue;
262 } else {
263 /* Do not tolerate partial BOM. */
264 if (bomptr != utf8_bom)
265 break;
266 /* No BOM at file beginning. Cool. */
267 bomptr = NULL;
268 }
269 }
17712991 270 if (c == '\n') {
02e5ba4a 271 if (config_file_eof)
17712991
LT
272 return 0;
273 comment = 0;
274 continue;
275 }
276 if (comment || isspace(c))
277 continue;
278 if (c == '#' || c == ';') {
279 comment = 1;
280 continue;
281 }
282 if (c == '[') {
283 baselen = get_base_var(var);
284 if (baselen <= 0)
285 break;
286 var[baselen++] = '.';
287 var[baselen] = 0;
288 continue;
289 }
290 if (!isalpha(c))
291 break;
128af9d1 292 var[baselen] = tolower(c);
ef90d6d4 293 if (get_value(fn, data, var, baselen+1) < 0)
17712991
LT
294 break;
295 }
4f629539 296 die("bad config file line %d in %s", config_linenr, config_file_name);
17712991
LT
297}
298
c8deb5a1 299static int parse_unit_factor(const char *end, unsigned long *val)
0b87b6e0
BD
300{
301 if (!*end)
302 return 1;
c8deb5a1
SP
303 else if (!strcasecmp(end, "k")) {
304 *val *= 1024;
305 return 1;
306 }
307 else if (!strcasecmp(end, "m")) {
308 *val *= 1024 * 1024;
309 return 1;
310 }
311 else if (!strcasecmp(end, "g")) {
312 *val *= 1024 * 1024 * 1024;
313 return 1;
314 }
315 return 0;
0b87b6e0
BD
316}
317
0433bcd9 318static int git_parse_long(const char *value, long *ret)
0b87b6e0
BD
319{
320 if (value && *value) {
321 char *end;
322 long val = strtol(value, &end, 0);
c8deb5a1
SP
323 unsigned long factor = 1;
324 if (!parse_unit_factor(end, &factor))
325 return 0;
326 *ret = val * factor;
0b87b6e0
BD
327 return 1;
328 }
329 return 0;
330}
331
332int git_parse_ulong(const char *value, unsigned long *ret)
17712991
LT
333{
334 if (value && *value) {
335 char *end;
0b87b6e0 336 unsigned long val = strtoul(value, &end, 0);
c8deb5a1
SP
337 if (!parse_unit_factor(end, &val))
338 return 0;
339 *ret = val;
0b87b6e0 340 return 1;
17712991 341 }
0b87b6e0
BD
342 return 0;
343}
344
c1867cea
JK
345static void die_bad_config(const char *name)
346{
347 if (config_file_name)
348 die("bad config value for '%s' in %s", name, config_file_name);
349 die("bad config value for '%s'", name);
350}
351
0b87b6e0
BD
352int git_config_int(const char *name, const char *value)
353{
0433bcd9 354 long ret = 0;
0b87b6e0 355 if (!git_parse_long(value, &ret))
c1867cea 356 die_bad_config(name);
0b87b6e0
BD
357 return ret;
358}
359
360unsigned long git_config_ulong(const char *name, const char *value)
361{
362 unsigned long ret;
363 if (!git_parse_ulong(value, &ret))
c1867cea 364 die_bad_config(name);
0b87b6e0 365 return ret;
17712991
LT
366}
367
8420ccd8 368int git_config_maybe_bool(const char *name, const char *value)
17712991
LT
369{
370 if (!value)
371 return 1;
372 if (!*value)
373 return 0;
8420ccd8
JH
374 if (!strcasecmp(value, "true")
375 || !strcasecmp(value, "yes")
376 || !strcasecmp(value, "on"))
17712991 377 return 1;
8420ccd8
JH
378 if (!strcasecmp(value, "false")
379 || !strcasecmp(value, "no")
380 || !strcasecmp(value, "off"))
17712991 381 return 0;
8420ccd8
JH
382 return -1;
383}
384
385int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
386{
387 int v = git_config_maybe_bool(name, value);
388 if (0 <= v) {
389 *is_bool = 1;
390 return v;
391 }
a53f2ec6 392 *is_bool = 0;
c35b0b58 393 return git_config_int(name, value);
17712991
LT
394}
395
a53f2ec6
JH
396int git_config_bool(const char *name, const char *value)
397{
398 int discard;
c35b0b58 399 return !!git_config_bool_or_int(name, value, &discard);
a53f2ec6
JH
400}
401
ea5105a5
CC
402int git_config_string(const char **dest, const char *var, const char *value)
403{
404 if (!value)
405 return config_error_nonbool(var);
406 *dest = xstrdup(value);
407 return 0;
408}
409
395de250
MM
410int git_config_pathname(const char **dest, const char *var, const char *value)
411{
412 if (!value)
413 return config_error_nonbool(var);
414 *dest = expand_user_path(value);
415 if (!*dest)
416 die("Failed to expand user dir in: '%s'", value);
417 return 0;
418}
419
806e2ad7 420static int git_default_core_config(const char *var, const char *value)
17712991
LT
421{
422 /* This needs a better name */
423 if (!strcmp(var, "core.filemode")) {
424 trust_executable_bit = git_config_bool(var, value);
425 return 0;
426 }
1ce4790b
AR
427 if (!strcmp(var, "core.trustctime")) {
428 trust_ctime = git_config_bool(var, value);
429 return 0;
430 }
17712991 431
9378c161
JH
432 if (!strcmp(var, "core.quotepath")) {
433 quote_path_fully = git_config_bool(var, value);
434 return 0;
435 }
436
78a8d641
JS
437 if (!strcmp(var, "core.symlinks")) {
438 has_symlinks = git_config_bool(var, value);
439 return 0;
440 }
441
0a9b88b7
LT
442 if (!strcmp(var, "core.ignorecase")) {
443 ignore_case = git_config_bool(var, value);
444 return 0;
445 }
446
7d1864ce
JH
447 if (!strcmp(var, "core.bare")) {
448 is_bare_repository_cfg = git_config_bool(var, value);
449 return 0;
450 }
451
5f73076c
JH
452 if (!strcmp(var, "core.ignorestat")) {
453 assume_unchanged = git_config_bool(var, value);
454 return 0;
455 }
456
e388c738
JH
457 if (!strcmp(var, "core.prefersymlinkrefs")) {
458 prefer_symlink_refs = git_config_bool(var, value);
f8348be3
JS
459 return 0;
460 }
461
6de08ae6
SP
462 if (!strcmp(var, "core.logallrefupdates")) {
463 log_all_ref_updates = git_config_bool(var, value);
464 return 0;
465 }
466
2f8acdb3
JH
467 if (!strcmp(var, "core.warnambiguousrefs")) {
468 warn_ambiguous_refs = git_config_bool(var, value);
469 return 0;
470 }
471
960ccca6 472 if (!strcmp(var, "core.loosecompression")) {
12f6c308
JBH
473 int level = git_config_int(var, value);
474 if (level == -1)
475 level = Z_DEFAULT_COMPRESSION;
476 else if (level < 0 || level > Z_BEST_COMPRESSION)
477 die("bad zlib compression level %d", level);
478 zlib_compression_level = level;
960ccca6
DH
479 zlib_compression_seen = 1;
480 return 0;
481 }
482
483 if (!strcmp(var, "core.compression")) {
484 int level = git_config_int(var, value);
485 if (level == -1)
486 level = Z_DEFAULT_COMPRESSION;
487 else if (level < 0 || level > Z_BEST_COMPRESSION)
488 die("bad zlib compression level %d", level);
489 core_compression_level = level;
490 core_compression_seen = 1;
491 if (!zlib_compression_seen)
492 zlib_compression_level = level;
12f6c308
JBH
493 return 0;
494 }
495
60bb8b14 496 if (!strcmp(var, "core.packedgitwindowsize")) {
5faaf246 497 int pgsz_x2 = getpagesize() * 2;
60bb8b14 498 packed_git_window_size = git_config_int(var, value);
5faaf246
JH
499
500 /* This value must be multiple of (pagesize * 2) */
501 packed_git_window_size /= pgsz_x2;
502 if (packed_git_window_size < 1)
503 packed_git_window_size = 1;
504 packed_git_window_size *= pgsz_x2;
60bb8b14
SP
505 return 0;
506 }
507
77ccc5bb
SP
508 if (!strcmp(var, "core.packedgitlimit")) {
509 packed_git_limit = git_config_int(var, value);
510 return 0;
511 }
512
18bdec11
SP
513 if (!strcmp(var, "core.deltabasecachelimit")) {
514 delta_base_cache_limit = git_config_int(var, value);
515 return 0;
516 }
517
6c510bee 518 if (!strcmp(var, "core.autocrlf")) {
d7f46334 519 if (value && !strcasecmp(value, "input")) {
942e7747
EB
520 if (eol == EOL_CRLF)
521 return error("core.autocrlf=input conflicts with core.eol=crlf");
fd6cce9e 522 auto_crlf = AUTO_CRLF_INPUT;
d7f46334
LT
523 return 0;
524 }
6c510bee
LT
525 auto_crlf = git_config_bool(var, value);
526 return 0;
527 }
528
21e5ad50
SP
529 if (!strcmp(var, "core.safecrlf")) {
530 if (value && !strcasecmp(value, "warn")) {
531 safe_crlf = SAFE_CRLF_WARN;
532 return 0;
533 }
534 safe_crlf = git_config_bool(var, value);
535 return 0;
536 }
537
942e7747
EB
538 if (!strcmp(var, "core.eol")) {
539 if (value && !strcasecmp(value, "lf"))
540 eol = EOL_LF;
541 else if (value && !strcasecmp(value, "crlf"))
542 eol = EOL_CRLF;
543 else if (value && !strcasecmp(value, "native"))
544 eol = EOL_NATIVE;
545 else
546 eol = EOL_UNSET;
547 if (eol == EOL_CRLF && auto_crlf == AUTO_CRLF_INPUT)
548 return error("core.autocrlf=input conflicts with core.eol=crlf");
549 return 0;
550 }
551
a97a7468
JS
552 if (!strcmp(var, "core.notesref")) {
553 notes_ref_name = xstrdup(value);
554 return 0;
555 }
556
806e2ad7
LT
557 if (!strcmp(var, "core.pager"))
558 return git_config_string(&pager_program, var, value);
559
560 if (!strcmp(var, "core.editor"))
561 return git_config_string(&editor_program, var, value);
562
d3e7da89
AK
563 if (!strcmp(var, "core.askpass"))
564 return git_config_string(&askpass_program, var, value);
565
806e2ad7 566 if (!strcmp(var, "core.excludesfile"))
395de250 567 return git_config_pathname(&excludes_file, var, value);
806e2ad7
LT
568
569 if (!strcmp(var, "core.whitespace")) {
570 if (!value)
571 return config_error_nonbool(var);
572 whitespace_rule_cfg = parse_whitespace_rule(value);
573 return 0;
574 }
575
aafe9fba
LT
576 if (!strcmp(var, "core.fsyncobjectfiles")) {
577 fsync_object_files = git_config_bool(var, value);
578 return 0;
579 }
580
671c9b7e
LT
581 if (!strcmp(var, "core.preloadindex")) {
582 core_preload_index = git_config_bool(var, value);
583 return 0;
584 }
585
348df166
JS
586 if (!strcmp(var, "core.createobject")) {
587 if (!strcmp(value, "rename"))
588 object_creation_mode = OBJECT_CREATION_USES_RENAMES;
589 else if (!strcmp(value, "link"))
590 object_creation_mode = OBJECT_CREATION_USES_HARDLINKS;
591 else
592 die("Invalid mode for object creation: %s", value);
be66a6c4
JS
593 return 0;
594 }
595
08aefc9e
NTND
596 if (!strcmp(var, "core.sparsecheckout")) {
597 core_apply_sparse_checkout = git_config_bool(var, value);
598 return 0;
599 }
600
806e2ad7
LT
601 /* Add other config variables here and to Documentation/config.txt. */
602 return 0;
603}
604
d1364529 605static int git_default_user_config(const char *var, const char *value)
806e2ad7 606{
e1b10391 607 if (!strcmp(var, "user.name")) {
6c47d0e8
JH
608 if (!value)
609 return config_error_nonbool(var);
817151e6 610 strlcpy(git_default_name, value, sizeof(git_default_name));
91c38a21 611 user_ident_explicitly_given |= IDENT_NAME_GIVEN;
e1b10391
LT
612 return 0;
613 }
614
615 if (!strcmp(var, "user.email")) {
6c47d0e8
JH
616 if (!value)
617 return config_error_nonbool(var);
817151e6 618 strlcpy(git_default_email, value, sizeof(git_default_email));
91c38a21 619 user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
e1b10391
LT
620 return 0;
621 }
622
d1364529
LT
623 /* Add other config variables here and to Documentation/config.txt. */
624 return 0;
625}
626
1141f492 627static int git_default_i18n_config(const char *var, const char *value)
d1364529 628{
ea5105a5
CC
629 if (!strcmp(var, "i18n.commitencoding"))
630 return git_config_string(&git_commit_encoding, var, value);
d2c11a38 631
ea5105a5
CC
632 if (!strcmp(var, "i18n.logoutputencoding"))
633 return git_config_string(&git_log_output_encoding, var, value);
d2c11a38 634
1141f492
LT
635 /* Add other config variables here and to Documentation/config.txt. */
636 return 0;
637}
039bc64e 638
1141f492
LT
639static int git_default_branch_config(const char *var, const char *value)
640{
9ed36cfa
JS
641 if (!strcmp(var, "branch.autosetupmerge")) {
642 if (value && !strcasecmp(value, "always")) {
643 git_branch_track = BRANCH_TRACK_ALWAYS;
644 return 0;
645 }
646 git_branch_track = git_config_bool(var, value);
647 return 0;
648 }
c998ae9b
DS
649 if (!strcmp(var, "branch.autosetuprebase")) {
650 if (!value)
651 return config_error_nonbool(var);
652 else if (!strcmp(value, "never"))
653 autorebase = AUTOREBASE_NEVER;
654 else if (!strcmp(value, "local"))
655 autorebase = AUTOREBASE_LOCAL;
656 else if (!strcmp(value, "remote"))
657 autorebase = AUTOREBASE_REMOTE;
658 else if (!strcmp(value, "always"))
659 autorebase = AUTOREBASE_ALWAYS;
660 else
661 return error("Malformed value for %s", var);
662 return 0;
663 }
a9cc857a 664
1ab661dd 665 /* Add other config variables here and to Documentation/config.txt. */
17712991
LT
666 return 0;
667}
668
52153747
FAG
669static int git_default_push_config(const char *var, const char *value)
670{
671 if (!strcmp(var, "push.default")) {
672 if (!value)
673 return config_error_nonbool(var);
674 else if (!strcmp(value, "nothing"))
675 push_default = PUSH_DEFAULT_NOTHING;
676 else if (!strcmp(value, "matching"))
677 push_default = PUSH_DEFAULT_MATCHING;
678 else if (!strcmp(value, "tracking"))
679 push_default = PUSH_DEFAULT_TRACKING;
680 else if (!strcmp(value, "current"))
681 push_default = PUSH_DEFAULT_CURRENT;
682 else {
683 error("Malformed value for %s: %s", var, value);
684 return error("Must be one of nothing, matching, "
685 "tracking or current.");
686 }
687 return 0;
688 }
689
690 /* Add other config variables here and to Documentation/config.txt. */
691 return 0;
692}
693
d551a488
MSO
694static int git_default_mailmap_config(const char *var, const char *value)
695{
696 if (!strcmp(var, "mailmap.file"))
697 return git_config_string(&git_mailmap_file, var, value);
698
699 /* Add other config variables here and to Documentation/config.txt. */
700 return 0;
701}
702
1141f492
LT
703int git_default_config(const char *var, const char *value, void *dummy)
704{
705 if (!prefixcmp(var, "core."))
706 return git_default_core_config(var, value);
707
708 if (!prefixcmp(var, "user."))
709 return git_default_user_config(var, value);
710
711 if (!prefixcmp(var, "i18n."))
712 return git_default_i18n_config(var, value);
713
714 if (!prefixcmp(var, "branch."))
715 return git_default_branch_config(var, value);
716
52153747
FAG
717 if (!prefixcmp(var, "push."))
718 return git_default_push_config(var, value);
719
d551a488
MSO
720 if (!prefixcmp(var, "mailmap."))
721 return git_default_mailmap_config(var, value);
722
75194438
JK
723 if (!prefixcmp(var, "advice."))
724 return git_default_advice_config(var, value);
725
1141f492
LT
726 if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) {
727 pager_use_color = git_config_bool(var,value);
728 return 0;
729 }
730
731 /* Add other config variables here and to Documentation/config.txt. */
732 return 0;
733}
734
ef90d6d4 735int git_config_from_file(config_fn_t fn, const char *filename, void *data)
17712991
LT
736{
737 int ret;
4f629539 738 FILE *f = fopen(filename, "r");
17712991
LT
739
740 ret = -1;
741 if (f) {
742 config_file = f;
4f629539 743 config_file_name = filename;
17712991 744 config_linenr = 1;
02e5ba4a 745 config_file_eof = 0;
ef90d6d4 746 ret = git_parse_file(fn, data);
17712991 747 fclose(f);
4f629539 748 config_file_name = NULL;
17712991
LT
749 }
750 return ret;
751}
10bea152 752
506b17b1
JS
753const char *git_etc_gitconfig(void)
754{
7f0e39fa 755 static const char *system_wide;
2de9de5e
SP
756 if (!system_wide)
757 system_wide = system_path(ETC_GITCONFIG);
7f0e39fa 758 return system_wide;
506b17b1
JS
759}
760
0ef37164 761int git_env_bool(const char *k, int def)
ab88c363
JK
762{
763 const char *v = getenv(k);
764 return v ? git_config_bool(k, v) : def;
765}
766
767int git_config_system(void)
768{
769 return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
770}
771
772int git_config_global(void)
773{
774 return !git_env_bool("GIT_CONFIG_NOGLOBAL", 0);
775}
776
8b1fa778
AR
777int git_config_from_parameters(config_fn_t fn, void *data)
778{
779 const struct config_item *ct;
780 for (ct = config_parameters; ct; ct = ct->next)
781 if (fn(ct->name, ct->value, data) < 0)
782 return -1;
783 return 0;
784}
785
ef90d6d4 786int git_config(config_fn_t fn, void *data)
4f629539 787{
aa387407 788 int ret = 0, found = 0;
5f1a63e0 789 char *repo_config = NULL;
dc871831 790 const char *home = NULL;
5f1a63e0 791
8befc50c 792 /* Setting $GIT_CONFIG makes git read _only_ the given config file. */
dc871831
DB
793 if (config_exclusive_filename)
794 return git_config_from_file(fn, config_exclusive_filename, data);
aa387407 795 if (git_config_system() && !access(git_etc_gitconfig(), R_OK)) {
dc871831
DB
796 ret += git_config_from_file(fn, git_etc_gitconfig(),
797 data);
aa387407
FC
798 found += 1;
799 }
5f1a63e0 800
dc871831 801 home = getenv("HOME");
ab88c363 802 if (git_config_global() && home) {
9befac47 803 char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
aa387407 804 if (!access(user_config, R_OK)) {
dc871831 805 ret += git_config_from_file(fn, user_config, data);
aa387407
FC
806 found += 1;
807 }
5f1a63e0
JS
808 free(user_config);
809 }
810
a4f34cbb 811 repo_config = git_pathdup("config");
aa387407
FC
812 if (!access(repo_config, R_OK)) {
813 ret += git_config_from_file(fn, repo_config, data);
814 found += 1;
815 }
4cac42b1 816 free(repo_config);
8b1fa778
AR
817
818 if (config_parameters) {
819 ret += git_config_from_parameters(fn, data);
820 found += 1;
821 }
822
aa387407
FC
823 if (found == 0)
824 return -1;
5f1a63e0 825 return ret;
4f629539
JH
826}
827
10bea152
JS
828/*
829 * Find all the stuff for git_config_set() below.
830 */
4ddba79d
JS
831
832#define MAX_MATCHES 512
833
10bea152
JS
834static struct {
835 int baselen;
4b25d091 836 char *key;
f98d863d 837 int do_not_match;
4b25d091 838 regex_t *value_regex;
4ddba79d 839 int multi_replace;
dc49cd76 840 size_t offset[MAX_MATCHES];
10bea152
JS
841 enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
842 int seen;
843} store;
844
4b25d091 845static int matches(const char *key, const char *value)
f98d863d
JS
846{
847 return !strcmp(key, store.key) &&
848 (store.value_regex == NULL ||
849 (store.do_not_match ^
850 !regexec(store.value_regex, value, 0, NULL, 0)));
851}
852
4b25d091 853static int store_aux(const char *key, const char *value, void *cb)
10bea152 854{
ae9ee41d
JH
855 const char *ep;
856 size_t section_len;
857
10bea152
JS
858 switch (store.state) {
859 case KEY_SEEN:
f98d863d 860 if (matches(key, value)) {
4ddba79d 861 if (store.seen == 1 && store.multi_replace == 0) {
64c0d71c 862 warning("%s has multiple values", key);
4ddba79d 863 } else if (store.seen >= MAX_MATCHES) {
64c0d71c 864 error("too many matches for %s", key);
4ddba79d 865 return 1;
10bea152 866 }
4ddba79d
JS
867
868 store.offset[store.seen] = ftell(config_file);
10bea152
JS
869 store.seen++;
870 }
871 break;
872 case SECTION_SEEN:
ae9ee41d
JH
873 /*
874 * What we are looking for is in store.key (both
875 * section and var), and its section part is baselen
876 * long. We found key (again, both section and var).
877 * We would want to know if this key is in the same
878 * section as what we are looking for. We already
879 * know we are in the same section as what should
880 * hold store.key.
881 */
882 ep = strrchr(key, '.');
883 section_len = ep - key;
884
885 if ((section_len != store.baselen) ||
886 memcmp(key, store.key, section_len+1)) {
10bea152
JS
887 store.state = SECTION_END_SEEN;
888 break;
ae9ee41d
JH
889 }
890
891 /*
892 * Do not increment matches: this is no match, but we
893 * just made sure we are in the desired section.
894 */
895 store.offset[store.seen] = ftell(config_file);
10bea152
JS
896 /* fallthru */
897 case SECTION_END_SEEN:
898 case START:
f98d863d 899 if (matches(key, value)) {
4ddba79d 900 store.offset[store.seen] = ftell(config_file);
10bea152
JS
901 store.state = KEY_SEEN;
902 store.seen++;
d14f7764
LT
903 } else {
904 if (strrchr(key, '.') - key == store.baselen &&
bdf0ef08 905 !strncmp(key, store.key, store.baselen)) {
93ddef3e 906 store.state = SECTION_SEEN;
bdf0ef08 907 store.offset[store.seen] = ftell(config_file);
d14f7764 908 }
bdf0ef08 909 }
10bea152
JS
910 }
911 return 0;
912}
913
64c0d71c 914static int write_error(const char *filename)
480c9e52 915{
64c0d71c 916 error("failed to write new configuration file %s", filename);
480c9e52
AW
917
918 /* Same error code as "failed to rename". */
919 return 4;
920}
921
4b25d091 922static int store_write_section(int fd, const char *key)
10bea152 923{
cb891a59
KH
924 const char *dot;
925 int i, success;
f285a2d7 926 struct strbuf sb = STRBUF_INIT;
d14f7764 927
cb891a59 928 dot = memchr(key, '.', store.baselen);
d14f7764 929 if (dot) {
cb891a59
KH
930 strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key);
931 for (i = dot - key + 1; i < store.baselen; i++) {
e5c349ba 932 if (key[i] == '"' || key[i] == '\\')
cb891a59
KH
933 strbuf_addch(&sb, '\\');
934 strbuf_addch(&sb, key[i]);
d14f7764 935 }
cb891a59
KH
936 strbuf_addstr(&sb, "\"]\n");
937 } else {
938 strbuf_addf(&sb, "[%.*s]\n", store.baselen, key);
d14f7764
LT
939 }
940
cb891a59
KH
941 success = write_in_full(fd, sb.buf, sb.len) == sb.len;
942 strbuf_release(&sb);
480c9e52 943
cb891a59 944 return success;
10bea152
JS
945}
946
4b25d091 947static int store_write_pair(int fd, const char *key, const char *value)
10bea152 948{
cb891a59
KH
949 int i, success;
950 int length = strlen(key + store.baselen + 1);
951 const char *quote = "";
f285a2d7 952 struct strbuf sb = STRBUF_INIT;
cdd4fb15 953
6281f394
JM
954 /*
955 * Check to see if the value needs to be surrounded with a dq pair.
956 * Note that problematic characters are always backslash-quoted; this
957 * check is about not losing leading or trailing SP and strings that
958 * follow beginning-of-comment characters (i.e. ';' and '#') by the
959 * configuration parser.
960 */
cdd4fb15 961 if (value[0] == ' ')
cb891a59 962 quote = "\"";
cdd4fb15
BG
963 for (i = 0; value[i]; i++)
964 if (value[i] == ';' || value[i] == '#')
cb891a59
KH
965 quote = "\"";
966 if (i && value[i - 1] == ' ')
967 quote = "\"";
968
cb891a59
KH
969 strbuf_addf(&sb, "\t%.*s = %s",
970 length, key + store.baselen + 1, quote);
10bea152 971
10bea152
JS
972 for (i = 0; value[i]; i++)
973 switch (value[i]) {
480c9e52 974 case '\n':
cb891a59 975 strbuf_addstr(&sb, "\\n");
480c9e52
AW
976 break;
977 case '\t':
cb891a59 978 strbuf_addstr(&sb, "\\t");
480c9e52
AW
979 break;
980 case '"':
981 case '\\':
cb891a59 982 strbuf_addch(&sb, '\\');
480c9e52 983 default:
cb891a59 984 strbuf_addch(&sb, value[i]);
480c9e52
AW
985 break;
986 }
cb891a59
KH
987 strbuf_addf(&sb, "%s\n", quote);
988
989 success = write_in_full(fd, sb.buf, sb.len) == sb.len;
990 strbuf_release(&sb);
991
992 return success;
10bea152
JS
993}
994
4b25d091
FC
995static ssize_t find_beginning_of_line(const char *contents, size_t size,
996 size_t offset_, int *found_bracket)
4ddba79d 997{
dc49cd76
SP
998 size_t equal_offset = size, bracket_offset = size;
999 ssize_t offset;
4ddba79d 1000
7a31cc0f 1001contline:
a6080a0a 1002 for (offset = offset_-2; offset > 0
4ddba79d
JS
1003 && contents[offset] != '\n'; offset--)
1004 switch (contents[offset]) {
1005 case '=': equal_offset = offset; break;
1006 case ']': bracket_offset = offset; break;
1007 }
7a31cc0f
FL
1008 if (offset > 0 && contents[offset-1] == '\\') {
1009 offset_ = offset;
1010 goto contline;
1011 }
4ddba79d
JS
1012 if (bracket_offset < equal_offset) {
1013 *found_bracket = 1;
1014 offset = bracket_offset+1;
1015 } else
1016 offset++;
1017
1018 return offset;
1019}
1020
4b25d091 1021int git_config_set(const char *key, const char *value)
10bea152 1022{
4ddba79d 1023 return git_config_set_multivar(key, value, NULL, 0);
10bea152
JS
1024}
1025
1026/*
1027 * If value==NULL, unset in (remove from) config,
1028 * if value_regex!=NULL, disregard key/value pairs where value does not match.
4ddba79d
JS
1029 * if multi_replace==0, nothing, or only one matching key/value is replaced,
1030 * else all matching key/values (regardless how many) are removed,
1031 * before the new pair is written.
10bea152
JS
1032 *
1033 * Returns 0 on success.
1034 *
1035 * This function does this:
1036 *
1037 * - it locks the config file by creating ".git/config.lock"
1038 *
1039 * - it then parses the config using store_aux() as validator to find
1040 * the position on the key/value pair to replace. If it is to be unset,
1041 * it must be found exactly once.
1042 *
1043 * - the config file is mmap()ed and the part before the match (if any) is
1044 * written to the lock file, then the changed part and the rest.
1045 *
1046 * - the config file is removed and the lock file rename()d to it.
1047 *
1048 */
4b25d091
FC
1049int git_config_set_multivar(const char *key, const char *value,
1050 const char *value_regex, int multi_replace)
10bea152 1051{
d14f7764 1052 int i, dot;
f8ba655e 1053 int fd = -1, in_fd;
dafc88b1 1054 int ret;
4b25d091 1055 char *config_filename;
6cbf973c 1056 struct lock_file *lock = NULL;
4b25d091 1057 const char *last_dot = strrchr(key, '.');
4ddba79d 1058
dc871831
DB
1059 if (config_exclusive_filename)
1060 config_filename = xstrdup(config_exclusive_filename);
1061 else
a4f34cbb 1062 config_filename = git_pathdup("config");
9c3796fc 1063
10bea152
JS
1064 /*
1065 * Since "key" actually contains the section name and the real
1066 * key name separated by a dot, we have to know where the dot is.
1067 */
b17e659d 1068
dafc88b1 1069 if (last_dot == NULL) {
64c0d71c 1070 error("key does not contain a section: %s", key);
dafc88b1
SH
1071 ret = 2;
1072 goto out_free;
10bea152 1073 }
b17e659d
JS
1074 store.baselen = last_dot - key;
1075
1076 store.multi_replace = multi_replace;
10bea152
JS
1077
1078 /*
1079 * Validate the key and while at it, lower case it for matching.
1080 */
2d7320d0 1081 store.key = xmalloc(strlen(key) + 1);
d14f7764
LT
1082 dot = 0;
1083 for (i = 0; key[i]; i++) {
1084 unsigned char c = key[i];
1085 if (c == '.')
1086 dot = 1;
1087 /* Leave the extended basename untouched.. */
1088 if (!dot || i > store.baselen) {
38c5afa8 1089 if (!iskeychar(c) || (i == store.baselen+1 && !isalpha(c))) {
64c0d71c 1090 error("invalid key: %s", key);
d14f7764
LT
1091 free(store.key);
1092 ret = 1;
1093 goto out_free;
1094 }
1095 c = tolower(c);
6f71686e 1096 } else if (c == '\n') {
64c0d71c 1097 error("invalid key (newline): %s", key);
6f71686e
JS
1098 free(store.key);
1099 ret = 1;
1100 goto out_free;
d14f7764
LT
1101 }
1102 store.key[i] = c;
1103 }
3dd94e3b 1104 store.key[i] = 0;
10bea152
JS
1105
1106 /*
6cbf973c 1107 * The lock serves a purpose in addition to locking: the new
10bea152
JS
1108 * contents of .git/config will be written into it.
1109 */
6cbf973c
BS
1110 lock = xcalloc(sizeof(struct lock_file), 1);
1111 fd = hold_lock_file_for_update(lock, config_filename, 0);
1112 if (fd < 0) {
6ffd567b 1113 error("could not lock config file %s: %s", config_filename, strerror(errno));
10bea152 1114 free(store.key);
dafc88b1
SH
1115 ret = -1;
1116 goto out_free;
10bea152
JS
1117 }
1118
1119 /*
1120 * If .git/config does not exist yet, write a minimal version.
1121 */
88fb958b
AR
1122 in_fd = open(config_filename, O_RDONLY);
1123 if ( in_fd < 0 ) {
10bea152
JS
1124 free(store.key);
1125
88fb958b
AR
1126 if ( ENOENT != errno ) {
1127 error("opening %s: %s", config_filename,
1128 strerror(errno));
dafc88b1
SH
1129 ret = 3; /* same as "invalid config file" */
1130 goto out_free;
88fb958b 1131 }
10bea152
JS
1132 /* if nothing to unset, error out */
1133 if (value == NULL) {
dafc88b1
SH
1134 ret = 5;
1135 goto out_free;
10bea152
JS
1136 }
1137
4b25d091 1138 store.key = (char *)key;
480c9e52 1139 if (!store_write_section(fd, key) ||
93c1e079
JH
1140 !store_write_pair(fd, key, value))
1141 goto write_err_out;
1142 } else {
88fb958b 1143 struct stat st;
4b25d091 1144 char *contents;
dc49cd76
SP
1145 size_t contents_sz, copy_begin, copy_end;
1146 int i, new_line = 0;
10bea152
JS
1147
1148 if (value_regex == NULL)
1149 store.value_regex = NULL;
1150 else {
f98d863d
JS
1151 if (value_regex[0] == '!') {
1152 store.do_not_match = 1;
1153 value_regex++;
1154 } else
1155 store.do_not_match = 0;
1156
2d7320d0 1157 store.value_regex = (regex_t*)xmalloc(sizeof(regex_t));
10bea152
JS
1158 if (regcomp(store.value_regex, value_regex,
1159 REG_EXTENDED)) {
64c0d71c 1160 error("invalid pattern: %s", value_regex);
10bea152 1161 free(store.value_regex);
dafc88b1
SH
1162 ret = 6;
1163 goto out_free;
10bea152
JS
1164 }
1165 }
1166
4ddba79d 1167 store.offset[0] = 0;
10bea152
JS
1168 store.state = START;
1169 store.seen = 0;
1170
1171 /*
1172 * After this, store.offset will contain the *end* offset
1173 * of the last match, or remain at 0 if no match was found.
1174 * As a side effect, we make sure to transform only a valid
1175 * existing config file.
1176 */
ef90d6d4 1177 if (git_config_from_file(store_aux, config_filename, NULL)) {
64c0d71c 1178 error("invalid config file %s", config_filename);
10bea152
JS
1179 free(store.key);
1180 if (store.value_regex != NULL) {
1181 regfree(store.value_regex);
1182 free(store.value_regex);
1183 }
dafc88b1
SH
1184 ret = 3;
1185 goto out_free;
10bea152
JS
1186 }
1187
1188 free(store.key);
1189 if (store.value_regex != NULL) {
1190 regfree(store.value_regex);
1191 free(store.value_regex);
1192 }
1193
4ddba79d
JS
1194 /* if nothing to unset, or too many matches, error out */
1195 if ((store.seen == 0 && value == NULL) ||
1196 (store.seen > 1 && multi_replace == 0)) {
dafc88b1
SH
1197 ret = 5;
1198 goto out_free;
10bea152
JS
1199 }
1200
88fb958b 1201 fstat(in_fd, &st);
dc49cd76
SP
1202 contents_sz = xsize_t(st.st_size);
1203 contents = xmmap(NULL, contents_sz, PROT_READ,
10bea152
JS
1204 MAP_PRIVATE, in_fd, 0);
1205 close(in_fd);
1206
4ddba79d
JS
1207 if (store.seen == 0)
1208 store.seen = 1;
1209
1210 for (i = 0, copy_begin = 0; i < store.seen; i++) {
1211 if (store.offset[i] == 0) {
dc49cd76 1212 store.offset[i] = copy_end = contents_sz;
4ddba79d
JS
1213 } else if (store.state != KEY_SEEN) {
1214 copy_end = store.offset[i];
10bea152 1215 } else
4ddba79d 1216 copy_end = find_beginning_of_line(
dc49cd76 1217 contents, contents_sz,
4ddba79d
JS
1218 store.offset[i]-2, &new_line);
1219
02e5ba4a
JK
1220 if (copy_end > 0 && contents[copy_end-1] != '\n')
1221 new_line = 1;
1222
4ddba79d
JS
1223 /* write the first part of the config */
1224 if (copy_end > copy_begin) {
93c1e079
JH
1225 if (write_in_full(fd, contents + copy_begin,
1226 copy_end - copy_begin) <
1227 copy_end - copy_begin)
1228 goto write_err_out;
1229 if (new_line &&
2b7ca830 1230 write_str_in_full(fd, "\n") != 1)
93c1e079 1231 goto write_err_out;
4ddba79d
JS
1232 }
1233 copy_begin = store.offset[i];
10bea152
JS
1234 }
1235
10bea152
JS
1236 /* write the pair (value == NULL means unset) */
1237 if (value != NULL) {
93c1e079
JH
1238 if (store.state == START) {
1239 if (!store_write_section(fd, key))
1240 goto write_err_out;
480c9e52 1241 }
93c1e079
JH
1242 if (!store_write_pair(fd, key, value))
1243 goto write_err_out;
10bea152
JS
1244 }
1245
1246 /* write the rest of the config */
dc49cd76 1247 if (copy_begin < contents_sz)
93c1e079 1248 if (write_in_full(fd, contents + copy_begin,
dc49cd76
SP
1249 contents_sz - copy_begin) <
1250 contents_sz - copy_begin)
93c1e079 1251 goto write_err_out;
10bea152 1252
dc49cd76 1253 munmap(contents, contents_sz);
10bea152
JS
1254 }
1255
4ed7cd3a 1256 if (commit_lock_file(lock) < 0) {
64c0d71c 1257 error("could not commit config file %s", config_filename);
dafc88b1
SH
1258 ret = 4;
1259 goto out_free;
10bea152
JS
1260 }
1261
6cbf973c
BS
1262 /*
1263 * lock is committed, so don't try to roll it back below.
1264 * NOTE: Since lockfile.c keeps a linked list of all created
1265 * lock_file structures, it isn't safe to free(lock). It's
1266 * better to just leave it hanging around.
1267 */
1268 lock = NULL;
dafc88b1
SH
1269 ret = 0;
1270
1271out_free:
6cbf973c
BS
1272 if (lock)
1273 rollback_lock_file(lock);
4cac42b1 1274 free(config_filename);
dafc88b1 1275 return ret;
93c1e079
JH
1276
1277write_err_out:
64c0d71c 1278 ret = write_error(lock->filename);
93c1e079
JH
1279 goto out_free;
1280
10bea152
JS
1281}
1282
118f8b24
PB
1283static int section_name_match (const char *buf, const char *name)
1284{
1285 int i = 0, j = 0, dot = 0;
a4c0d463
AV
1286 if (buf[i] != '[')
1287 return 0;
1288 for (i = 1; buf[i] && buf[i] != ']'; i++) {
118f8b24
PB
1289 if (!dot && isspace(buf[i])) {
1290 dot = 1;
1291 if (name[j++] != '.')
1292 break;
1293 for (i++; isspace(buf[i]); i++)
1294 ; /* do nothing */
1295 if (buf[i] != '"')
1296 break;
1297 continue;
1298 }
1299 if (buf[i] == '\\' && dot)
1300 i++;
1301 else if (buf[i] == '"' && dot) {
1302 for (i++; isspace(buf[i]); i++)
1303 ; /* do_nothing */
1304 break;
1305 }
1306 if (buf[i] != name[j++])
1307 break;
1308 }
a4c0d463
AV
1309 if (buf[i] == ']' && name[j] == 0) {
1310 /*
1311 * We match, now just find the right length offset by
1312 * gobbling up any whitespace after it, as well
1313 */
1314 i++;
1315 for (; buf[i] && isspace(buf[i]); i++)
1316 ; /* do nothing */
1317 return i;
1318 }
1319 return 0;
118f8b24
PB
1320}
1321
1322/* if new_name == NULL, the section is removed instead */
0667fcfb
JS
1323int git_config_rename_section(const char *old_name, const char *new_name)
1324{
118f8b24 1325 int ret = 0, remove = 0;
fc1905bb 1326 char *config_filename;
0667fcfb
JS
1327 struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
1328 int out_fd;
1329 char buf[1024];
1330
dc871831
DB
1331 if (config_exclusive_filename)
1332 config_filename = xstrdup(config_exclusive_filename);
1333 else
a4f34cbb 1334 config_filename = git_pathdup("config");
0667fcfb 1335 out_fd = hold_lock_file_for_update(lock, config_filename, 0);
fc1905bb 1336 if (out_fd < 0) {
64c0d71c 1337 ret = error("could not lock config file %s", config_filename);
fc1905bb
JH
1338 goto out;
1339 }
0667fcfb 1340
fc1905bb 1341 if (!(config_file = fopen(config_filename, "rb"))) {
01ebb9dc
GB
1342 /* no config file means nothing to rename, no error */
1343 goto unlock_and_out;
fc1905bb 1344 }
0667fcfb
JS
1345
1346 while (fgets(buf, sizeof(buf), config_file)) {
1347 int i;
480c9e52 1348 int length;
9a5abfc7 1349 char *output = buf;
0667fcfb
JS
1350 for (i = 0; buf[i] && isspace(buf[i]); i++)
1351 ; /* do nothing */
1352 if (buf[i] == '[') {
1353 /* it's a section */
a4c0d463
AV
1354 int offset = section_name_match(&buf[i], old_name);
1355 if (offset > 0) {
118f8b24
PB
1356 ret++;
1357 if (new_name == NULL) {
1358 remove = 1;
0667fcfb
JS
1359 continue;
1360 }
0667fcfb 1361 store.baselen = strlen(new_name);
480c9e52 1362 if (!store_write_section(out_fd, new_name)) {
64c0d71c 1363 ret = write_error(lock->filename);
480c9e52
AW
1364 goto out;
1365 }
9a5abfc7
AV
1366 /*
1367 * We wrote out the new section, with
1368 * a newline, now skip the old
1369 * section's length
1370 */
1371 output += offset + i;
1372 if (strlen(output) > 0) {
1373 /*
1374 * More content means there's
1375 * a declaration to put on the
1376 * next line; indent with a
1377 * tab
1378 */
1379 output -= 1;
1380 output[0] = '\t';
1381 }
0667fcfb 1382 }
118f8b24 1383 remove = 0;
0667fcfb 1384 }
118f8b24
PB
1385 if (remove)
1386 continue;
9a5abfc7
AV
1387 length = strlen(output);
1388 if (write_in_full(out_fd, output, length) != length) {
64c0d71c 1389 ret = write_error(lock->filename);
480c9e52
AW
1390 goto out;
1391 }
0667fcfb 1392 }
fc1905bb 1393 fclose(config_file);
01ebb9dc 1394 unlock_and_out:
4ed7cd3a 1395 if (commit_lock_file(lock) < 0)
64c0d71c 1396 ret = error("could not commit config file %s", config_filename);
fc1905bb
JH
1397 out:
1398 free(config_filename);
0667fcfb
JS
1399 return ret;
1400}
40ea4ed9
JH
1401
1402/*
1403 * Call this to report error for your variable that should not
1404 * get a boolean value (i.e. "[my] var" means "true").
1405 */
1406int config_error_nonbool(const char *var)
1407{
1408 return error("Missing value for '%s'", var);
1409}