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