]> git.ipfire.org Git - thirdparty/git.git/blame - config.c
git-config-set: Properly terminate strings with '\0'
[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"
10bea152 9#include <regex.h>
17712991
LT
10
11#define MAXNAME (256)
12
13static FILE *config_file;
14static int config_linenr;
15static int get_next_char(void)
16{
17 int c;
18 FILE *f;
19
20 c = '\n';
21 if ((f = config_file) != NULL) {
22 c = fgetc(f);
db2c075d
JH
23 if (c == '\r') {
24 /* DOS like systems */
25 c = fgetc(f);
26 if (c != '\n') {
27 ungetc(c, f);
28 c = '\r';
29 }
30 }
17712991
LT
31 if (c == '\n')
32 config_linenr++;
33 if (c == EOF) {
34 config_file = NULL;
35 c = '\n';
36 }
37 }
38 return c;
39}
40
41static char *parse_value(void)
42{
43 static char value[1024];
44 int quote = 0, comment = 0, len = 0, space = 0;
45
46 for (;;) {
47 int c = get_next_char();
48 if (len >= sizeof(value))
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 (space) {
63 if (len)
64 value[len++] = ' ';
65 space = 0;
66 }
67 if (c == '\\') {
68 c = get_next_char();
69 switch (c) {
70 case '\n':
71 continue;
72 case 't':
73 c = '\t';
74 break;
75 case 'b':
76 c = '\b';
77 break;
78 case 'n':
79 c = '\n';
80 break;
5cbb401d
LT
81 /* Some characters escape as themselves */
82 case '\\': case '"':
83 break;
84 /* Reject unknown escape sequences */
85 default:
86 return NULL;
17712991
LT
87 }
88 value[len++] = c;
89 continue;
90 }
91 if (c == '"') {
92 quote = 1-quote;
93 continue;
94 }
95 if (!quote) {
96 if (c == ';' || c == '#') {
97 comment = 1;
98 continue;
99 }
100 }
101 value[len++] = c;
102 }
103}
104
105static int get_value(config_fn_t fn, char *name, unsigned int len)
106{
107 int c;
108 char *value;
109
110 /* Get the full name */
111 for (;;) {
112 c = get_next_char();
113 if (c == EOF)
114 break;
115 if (!isalnum(c))
116 break;
117 name[len++] = tolower(c);
118 if (len >= MAXNAME)
119 return -1;
120 }
121 name[len] = 0;
122 while (c == ' ' || c == '\t')
123 c = get_next_char();
124
125 value = NULL;
126 if (c != '\n') {
127 if (c != '=')
128 return -1;
129 value = parse_value();
130 if (!value)
131 return -1;
132 }
133 return fn(name, value);
134}
135
136static int get_base_var(char *name)
137{
138 int baselen = 0;
139
140 for (;;) {
141 int c = get_next_char();
142 if (c == EOF)
143 return -1;
144 if (c == ']')
145 return baselen;
146 if (!isalnum(c))
147 return -1;
148 if (baselen > MAXNAME / 2)
149 return -1;
150 name[baselen++] = tolower(c);
151 }
152}
153
154static int git_parse_file(config_fn_t fn)
155{
156 int comment = 0;
157 int baselen = 0;
158 static char var[MAXNAME];
159
160 for (;;) {
161 int c = get_next_char();
162 if (c == '\n') {
163 /* EOF? */
164 if (!config_file)
165 return 0;
166 comment = 0;
167 continue;
168 }
169 if (comment || isspace(c))
170 continue;
171 if (c == '#' || c == ';') {
172 comment = 1;
173 continue;
174 }
175 if (c == '[') {
176 baselen = get_base_var(var);
177 if (baselen <= 0)
178 break;
179 var[baselen++] = '.';
180 var[baselen] = 0;
181 continue;
182 }
183 if (!isalpha(c))
184 break;
128af9d1 185 var[baselen] = tolower(c);
17712991
LT
186 if (get_value(fn, var, baselen+1) < 0)
187 break;
188 }
189 die("bad config file line %d", config_linenr);
190}
191
192int git_config_int(const char *name, const char *value)
193{
194 if (value && *value) {
195 char *end;
196 int val = strtol(value, &end, 0);
197 if (!*end)
198 return val;
199 }
200 die("bad config value for '%s'", name);
201}
202
203int git_config_bool(const char *name, const char *value)
204{
205 if (!value)
206 return 1;
207 if (!*value)
208 return 0;
209 if (!strcasecmp(value, "true"))
210 return 1;
211 if (!strcasecmp(value, "false"))
212 return 0;
213 return git_config_int(name, value) != 0;
214}
215
216int git_default_config(const char *var, const char *value)
217{
218 /* This needs a better name */
219 if (!strcmp(var, "core.filemode")) {
220 trust_executable_bit = git_config_bool(var, value);
221 return 0;
222 }
223
f8348be3
JS
224 if (!strcmp(var, "core.symrefsonly")) {
225 only_use_symrefs = git_config_bool(var, value);
226 return 0;
227 }
228
e1b10391
LT
229 if (!strcmp(var, "user.name")) {
230 strncpy(git_default_name, value, sizeof(git_default_name));
231 return 0;
232 }
233
234 if (!strcmp(var, "user.email")) {
235 strncpy(git_default_email, value, sizeof(git_default_email));
236 return 0;
237 }
238
3299c6f6
JH
239 if (!strcmp(var, "diff.renamelimit")) {
240 diff_rename_limit_default = git_config_int(var, value);
241 return 0;
242 }
243
17712991
LT
244 /* Add other config variables here.. */
245 return 0;
246}
247
248int git_config(config_fn_t fn)
249{
250 int ret;
251 FILE *f = fopen(git_path("config"), "r");
252
253 ret = -1;
254 if (f) {
255 config_file = f;
256 config_linenr = 1;
257 ret = git_parse_file(fn);
258 fclose(f);
259 }
260 return ret;
261}
10bea152
JS
262
263/*
264 * Find all the stuff for git_config_set() below.
265 */
4ddba79d
JS
266
267#define MAX_MATCHES 512
268
10bea152
JS
269static struct {
270 int baselen;
271 char* key;
f98d863d 272 int do_not_match;
10bea152 273 regex_t* value_regex;
4ddba79d
JS
274 int multi_replace;
275 off_t offset[MAX_MATCHES];
10bea152
JS
276 enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
277 int seen;
278} store;
279
f98d863d
JS
280static int matches(const char* key, const char* value)
281{
282 return !strcmp(key, store.key) &&
283 (store.value_regex == NULL ||
284 (store.do_not_match ^
285 !regexec(store.value_regex, value, 0, NULL, 0)));
286}
287
10bea152
JS
288static int store_aux(const char* key, const char* value)
289{
290 switch (store.state) {
291 case KEY_SEEN:
f98d863d 292 if (matches(key, value)) {
4ddba79d 293 if (store.seen == 1 && store.multi_replace == 0) {
10bea152
JS
294 fprintf(stderr,
295 "Warning: %s has multiple values\n",
296 key);
4ddba79d
JS
297 } else if (store.seen >= MAX_MATCHES) {
298 fprintf(stderr, "Too many matches\n");
299 return 1;
10bea152 300 }
4ddba79d
JS
301
302 store.offset[store.seen] = ftell(config_file);
10bea152
JS
303 store.seen++;
304 }
305 break;
306 case SECTION_SEEN:
307 if (strncmp(key, store.key, store.baselen+1)) {
308 store.state = SECTION_END_SEEN;
309 break;
310 } else
4ddba79d
JS
311 /* do not increment matches: this is no match */
312 store.offset[store.seen] = ftell(config_file);
10bea152
JS
313 /* fallthru */
314 case SECTION_END_SEEN:
315 case START:
f98d863d 316 if (matches(key, value)) {
4ddba79d 317 store.offset[store.seen] = ftell(config_file);
10bea152
JS
318 store.state = KEY_SEEN;
319 store.seen++;
320 } else if(!strncmp(key, store.key, store.baselen))
321 store.state = SECTION_SEEN;
322 }
323 return 0;
324}
325
326static void store_write_section(int fd, const char* key)
327{
328 write(fd, "[", 1);
329 write(fd, key, store.baselen);
330 write(fd, "]\n", 2);
331}
332
333static void store_write_pair(int fd, const char* key, const char* value)
334{
335 int i;
336
337 write(fd, "\t", 1);
338 write(fd, key+store.baselen+1,
339 strlen(key+store.baselen+1));
340 write(fd, " = ", 3);
341 for (i = 0; value[i]; i++)
342 switch (value[i]) {
343 case '\n': write(fd, "\\n", 2); break;
344 case '\t': write(fd, "\\t", 2); break;
345 case '"': case '\\': write(fd, "\\", 1);
346 default: write(fd, value+i, 1);
347 }
348 write(fd, "\n", 1);
349}
350
4ddba79d
JS
351static int find_beginning_of_line(const char* contents, int size,
352 int offset_, int* found_bracket)
353{
354 int equal_offset = size, bracket_offset = size;
355 int offset;
356
357 for (offset = offset_-2; offset > 0
358 && contents[offset] != '\n'; offset--)
359 switch (contents[offset]) {
360 case '=': equal_offset = offset; break;
361 case ']': bracket_offset = offset; break;
362 }
363 if (bracket_offset < equal_offset) {
364 *found_bracket = 1;
365 offset = bracket_offset+1;
366 } else
367 offset++;
368
369 return offset;
370}
371
10bea152
JS
372int git_config_set(const char* key, const char* value)
373{
4ddba79d 374 return git_config_set_multivar(key, value, NULL, 0);
10bea152
JS
375}
376
377/*
378 * If value==NULL, unset in (remove from) config,
379 * if value_regex!=NULL, disregard key/value pairs where value does not match.
4ddba79d
JS
380 * if multi_replace==0, nothing, or only one matching key/value is replaced,
381 * else all matching key/values (regardless how many) are removed,
382 * before the new pair is written.
10bea152
JS
383 *
384 * Returns 0 on success.
385 *
386 * This function does this:
387 *
388 * - it locks the config file by creating ".git/config.lock"
389 *
390 * - it then parses the config using store_aux() as validator to find
391 * the position on the key/value pair to replace. If it is to be unset,
392 * it must be found exactly once.
393 *
394 * - the config file is mmap()ed and the part before the match (if any) is
395 * written to the lock file, then the changed part and the rest.
396 *
397 * - the config file is removed and the lock file rename()d to it.
398 *
399 */
400int git_config_set_multivar(const char* key, const char* value,
4ddba79d 401 const char* value_regex, int multi_replace)
10bea152
JS
402{
403 int i;
404 struct stat st;
405 int fd;
406 char* config_file = strdup(git_path("config"));
407 char* lock_file = strdup(git_path("config.lock"));
408
4ddba79d
JS
409 store.multi_replace = multi_replace;
410
10bea152
JS
411 /*
412 * Since "key" actually contains the section name and the real
413 * key name separated by a dot, we have to know where the dot is.
414 */
415 for (store.baselen = 0;
416 key[store.baselen] != '.' && key[store.baselen];
417 store.baselen++);
418 if (!key[store.baselen] || !key[store.baselen+1]) {
419 fprintf(stderr, "key does not contain a section: %s\n", key);
420 return 2;
421 }
422
423 /*
424 * Validate the key and while at it, lower case it for matching.
425 */
426 store.key = (char*)malloc(strlen(key)+1);
427 for (i = 0; key[i]; i++)
428 if (i != store.baselen && (!isalnum(key[i]) ||
429 (i == store.baselen+1 && !isalpha(key[i])))) {
430 fprintf(stderr, "invalid key: %s\n", key);
431 free(store.key);
432 return 1;
433 } else
434 store.key[i] = tolower(key[i]);
3dd94e3b 435 store.key[i] = 0;
10bea152
JS
436
437 /*
438 * The lock_file serves a purpose in addition to locking: the new
439 * contents of .git/config will be written into it.
440 */
441 fd = open(lock_file, O_WRONLY | O_CREAT | O_EXCL, 0666);
442 if (fd < 0) {
443 fprintf(stderr, "could not lock config file\n");
444 free(store.key);
445 return -1;
446 }
447
448 /*
449 * If .git/config does not exist yet, write a minimal version.
450 */
451 if (stat(config_file, &st)) {
452 static const char contents[] =
453 "#\n"
454 "# This is the config file\n"
455 "#\n"
456 "\n";
457
458 free(store.key);
459
460 /* if nothing to unset, error out */
461 if (value == NULL) {
462 close(fd);
463 unlink(lock_file);
464 return 5;
465 }
466
467 store.key = (char*)key;
468
469 write(fd, contents, sizeof(contents)-1);
470 store_write_section(fd, key);
471 store_write_pair(fd, key, value);
472 } else{
473 int in_fd;
474 char* contents;
4ddba79d 475 int i, copy_begin, copy_end, new_line = 0;
10bea152
JS
476
477 if (value_regex == NULL)
478 store.value_regex = NULL;
479 else {
f98d863d
JS
480 if (value_regex[0] == '!') {
481 store.do_not_match = 1;
482 value_regex++;
483 } else
484 store.do_not_match = 0;
485
10bea152
JS
486 store.value_regex = (regex_t*)malloc(sizeof(regex_t));
487 if (regcomp(store.value_regex, value_regex,
488 REG_EXTENDED)) {
489 fprintf(stderr, "Invalid pattern: %s",
490 value_regex);
491 free(store.value_regex);
492 return 6;
493 }
494 }
495
4ddba79d 496 store.offset[0] = 0;
10bea152
JS
497 store.state = START;
498 store.seen = 0;
499
500 /*
501 * After this, store.offset will contain the *end* offset
502 * of the last match, or remain at 0 if no match was found.
503 * As a side effect, we make sure to transform only a valid
504 * existing config file.
505 */
506 if (git_config(store_aux)) {
507 fprintf(stderr, "invalid config file\n");
508 free(store.key);
509 if (store.value_regex != NULL) {
510 regfree(store.value_regex);
511 free(store.value_regex);
512 }
513 return 3;
514 }
515
516 free(store.key);
517 if (store.value_regex != NULL) {
518 regfree(store.value_regex);
519 free(store.value_regex);
520 }
521
4ddba79d
JS
522 /* if nothing to unset, or too many matches, error out */
523 if ((store.seen == 0 && value == NULL) ||
524 (store.seen > 1 && multi_replace == 0)) {
10bea152
JS
525 close(fd);
526 unlink(lock_file);
527 return 5;
528 }
529
10bea152
JS
530 in_fd = open(config_file, O_RDONLY, 0666);
531 contents = mmap(NULL, st.st_size, PROT_READ,
532 MAP_PRIVATE, in_fd, 0);
533 close(in_fd);
534
4ddba79d
JS
535 if (store.seen == 0)
536 store.seen = 1;
537
538 for (i = 0, copy_begin = 0; i < store.seen; i++) {
539 if (store.offset[i] == 0) {
540 store.offset[i] = copy_end = st.st_size;
541 } else if (store.state != KEY_SEEN) {
542 copy_end = store.offset[i];
10bea152 543 } else
4ddba79d
JS
544 copy_end = find_beginning_of_line(
545 contents, st.st_size,
546 store.offset[i]-2, &new_line);
547
548 /* write the first part of the config */
549 if (copy_end > copy_begin) {
550 write(fd, contents + copy_begin,
551 copy_end - copy_begin);
552 if (new_line)
553 write(fd, "\n", 1);
554 }
555 copy_begin = store.offset[i];
10bea152
JS
556 }
557
10bea152
JS
558 /* write the pair (value == NULL means unset) */
559 if (value != NULL) {
560 if (store.state == START)
561 store_write_section(fd, key);
562 store_write_pair(fd, key, value);
563 }
564
565 /* write the rest of the config */
4ddba79d
JS
566 if (copy_begin < st.st_size)
567 write(fd, contents + copy_begin,
568 st.st_size - copy_begin);
10bea152
JS
569
570 munmap(contents, st.st_size);
571 unlink(config_file);
572 }
573
574 close(fd);
575
576 if (rename(lock_file, config_file) < 0) {
577 fprintf(stderr, "Could not rename the lock file?\n");
578 return 4;
579 }
580
581 return 0;
582}
583
4ddba79d 584