134
return fn(name, value);
135
}
136
137
+static int get_extended_base_var(char *name, int baselen, int c)
138
+{
139
+ do {
140
+ if (c == '\n')
141
+ return -1;
142
+ c = get_next_char();
143
+ } while (isspace(c));
144
+
145
+ /* We require the format to be '[base "extension"]' */
146
+ if (c != '"')
147
+ return -1;
148
+ name[baselen++] = '.';
149
+
150
+ for (;;) {
151
+ int c = get_next_char();
152
+ if (c == '\n')
153
+ return -1;
154
+ if (c == '"')
155
+ break;
156
+ if (c == '\\') {
157
+ c = get_next_char();
158
+ if (c == '\n')
159
+ return -1;
160
+ }
161
+ name[baselen++] = c;
162
+ if (baselen > MAXNAME / 2)
163
+ return -1;
164
+ }
165
+
166
+ /* Final ']' */
167
+ if (get_next_char() != ']')
168
+ return -1;
169
+ return baselen;
170
+}
171
+
172
static int get_base_var(char *name)
173
{
174
int baselen = 0;
179
return -1;
180
if (c == ']')
181
return baselen;
182
+ if (isspace(c))
183
+ return get_extended_base_var(name, baselen, c);
184
if (!isalnum(c) && c != '.')
185
return -1;
186
if (baselen > MAXNAME / 2)
372
store.offset[store.seen] = ftell(config_file);
373
store.state = KEY_SEEN;
374
store.seen++;
338
- } else if (strrchr(key, '.') - key == store.baselen &&
375
+ } else {
376
+ if (strrchr(key, '.') - key == store.baselen &&
377
!strncmp(key, store.key, store.baselen)) {
378
store.state = SECTION_SEEN;
379
store.offset[store.seen] = ftell(config_file);
380
+ }
381
}
382
}
383
return 0;
385
386
static void store_write_section(int fd, const char* key)
387
{
388
+ const char *dot = strchr(key, '.');
389
+ int len1 = store.baselen, len2 = -1;
390
+
391
+ dot = strchr(key, '.');
392
+ if (dot) {
393
+ int dotlen = dot - key;
394
+ if (dotlen < len1) {
395
+ len2 = len1 - dotlen - 1;
396
+ len1 = dotlen;
397
+ }
398
+ }
399
+
400
write(fd, "[", 1);
350
- write(fd, key, store.baselen);
401
+ write(fd, key, len1);
402
+ if (len2 >= 0) {
403
+ write(fd, " \"", 2);
404
+ while (--len2 >= 0) {
405
+ unsigned char c = *++dot;
406
+ if (c == '"')
407
+ write(fd, "\\", 1);
408
+ write(fd, &c, 1);
409
+ }
410
+ write(fd, "\"", 1);
411
+ }
412
write(fd, "]\n", 2);
413
}
414
482
int git_config_set_multivar(const char* key, const char* value,
483
const char* value_regex, int multi_replace)
484
{
424
- int i;
485
+ int i, dot;
486
int fd = -1, in_fd;
487
int ret;
488
char* config_filename = strdup(git_path("config"));
507
* Validate the key and while at it, lower case it for matching.
508
*/
509
store.key = (char*)malloc(strlen(key)+1);
449
- for (i = 0; key[i]; i++)
450
- if (i != store.baselen &&
451
- ((!isalnum(key[i]) && key[i] != '.') ||
452
- (i == store.baselen+1 && !isalpha(key[i])))) {
453
- fprintf(stderr, "invalid key: %s\n", key);
454
- free(store.key);
455
- ret = 1;
456
- goto out_free;
457
- } else
458
- store.key[i] = tolower(key[i]);
510
+ dot = 0;
511
+ for (i = 0; key[i]; i++) {
512
+ unsigned char c = key[i];
513
+ if (c == '.')
514
+ dot = 1;
515
+ /* Leave the extended basename untouched.. */
516
+ if (!dot || i > store.baselen) {
517
+ if (!isalnum(c) || (i == store.baselen+1 && !isalpha(c))) {
518
+ fprintf(stderr, "invalid key: %s\n", key);
519
+ free(store.key);
520
+ ret = 1;
521
+ goto out_free;
522
+ }
523
+ c = tolower(c);
524
+ }
525
+ store.key[i] = c;
526
+ }
527
store.key[i] = 0;
528
529
/*