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