X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=config.c;h=4183f80262ea9f24e286295a2295f93459548b78;hb=957450054cfbbe0d4ae8d33f7b74eace6e290f16;hp=9d368488893687a7b6986b5ab8eb1154f3ad7df9;hpb=5cfe4256d98af22a570c78c5e3048391a90f5f98;p=git.git diff --git a/config.c b/config.c index 9d3684888..4183f8026 100644 --- a/config.c +++ b/config.c @@ -12,22 +12,22 @@ #define MAXNAME (256) -static FILE *config_file; -static const char *config_file_name; -static int config_linenr; -static int config_file_eof; +typedef struct config_file { + struct config_file *prev; + FILE *f; + const char *name; + int linenr; + int eof; + struct strbuf value; + char var[MAXNAME]; +} config_file; + +static config_file *cf; + static int zlib_compression_seen; const char *config_exclusive_filename = NULL; -struct config_item { - struct config_item *next; - char *name; - char *value; -}; -static struct config_item *config_parameters; -static struct config_item **config_parameters_tail = &config_parameters; - static void lowercase(char *p) { for (; *p; p++) @@ -47,34 +47,31 @@ void git_config_push_parameter(const char *text) strbuf_release(&env); } -int git_config_parse_parameter(const char *text) +int git_config_parse_parameter(const char *text, + config_fn_t fn, void *data) { - struct config_item *ct; - struct strbuf tmp = STRBUF_INIT; struct strbuf **pair; - strbuf_addstr(&tmp, text); - pair = strbuf_split(&tmp, '='); + pair = strbuf_split_str(text, '=', 2); + if (!pair[0]) + return error("bogus config parameter: %s", text); if (pair[0]->len && pair[0]->buf[pair[0]->len - 1] == '=') strbuf_setlen(pair[0], pair[0]->len - 1); strbuf_trim(pair[0]); if (!pair[0]->len) { strbuf_list_free(pair); - return -1; + return error("bogus config parameter: %s", text); } - ct = xcalloc(1, sizeof(struct config_item)); - ct->name = strbuf_detach(pair[0], NULL); - if (pair[1]) { - strbuf_trim(pair[1]); - ct->value = strbuf_detach(pair[1], NULL); + lowercase(pair[0]->buf); + if (fn(pair[0]->buf, pair[1] ? pair[1]->buf : NULL, data) < 0) { + strbuf_list_free(pair); + return -1; } strbuf_list_free(pair); - lowercase(ct->name); - *config_parameters_tail = ct; - config_parameters_tail = &ct->next; return 0; } -int git_config_parse_environment(void) { +int git_config_from_parameters(config_fn_t fn, void *data) +{ const char *env = getenv(CONFIG_DATA_ENVIRONMENT); char *envw; const char **argv = NULL; @@ -92,8 +89,7 @@ int git_config_parse_environment(void) { } for (i = 0; i < nr; i++) { - if (git_config_parse_parameter(argv[i]) < 0) { - error("bogus config parameter: %s", argv[i]); + if (git_config_parse_parameter(argv[i], fn, data) < 0) { free(argv); free(envw); return -1; @@ -102,7 +98,7 @@ int git_config_parse_environment(void) { free(argv); free(envw); - return 0; + return nr > 0; } static int get_next_char(void) @@ -111,7 +107,7 @@ static int get_next_char(void) FILE *f; c = '\n'; - if ((f = config_file) != NULL) { + if (cf && ((f = cf->f) != NULL)) { c = fgetc(f); if (c == '\r') { /* DOS like systems */ @@ -122,9 +118,9 @@ static int get_next_char(void) } } if (c == '\n') - config_linenr++; + cf->linenr++; if (c == EOF) { - config_file_eof = 1; + cf->eof = 1; c = '\n'; } } @@ -133,21 +129,20 @@ static int get_next_char(void) static char *parse_value(void) { - static struct strbuf value = STRBUF_INIT; int quote = 0, comment = 0, space = 0; - strbuf_reset(&value); + strbuf_reset(&cf->value); for (;;) { int c = get_next_char(); if (c == '\n') { if (quote) return NULL; - return value.buf; + return cf->value.buf; } if (comment) continue; if (isspace(c) && !quote) { - if (value.len) + if (cf->value.len) space++; continue; } @@ -158,7 +153,7 @@ static char *parse_value(void) } } for (; space; space--) - strbuf_addch(&value, ' '); + strbuf_addch(&cf->value, ' '); if (c == '\\') { c = get_next_char(); switch (c) { @@ -180,14 +175,14 @@ static char *parse_value(void) default: return NULL; } - strbuf_addch(&value, c); + strbuf_addch(&cf->value, c); continue; } if (c == '"') { quote = 1-quote; continue; } - strbuf_addch(&value, c); + strbuf_addch(&cf->value, c); } } @@ -204,7 +199,7 @@ static int get_value(config_fn_t fn, void *data, char *name, unsigned int len) /* Get the full name */ for (;;) { c = get_next_char(); - if (config_file_eof) + if (cf->eof) break; if (!iskeychar(c)) break; @@ -268,7 +263,7 @@ static int get_base_var(char *name) for (;;) { int c = get_next_char(); - if (config_file_eof) + if (cf->eof) return -1; if (c == ']') return baselen; @@ -286,7 +281,7 @@ static int git_parse_file(config_fn_t fn, void *data) { int comment = 0; int baselen = 0; - static char var[MAXNAME]; + char *var = cf->var; /* U+FEFF Byte Order Mark in UTF8 */ static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf"; @@ -310,7 +305,7 @@ static int git_parse_file(config_fn_t fn, void *data) } } if (c == '\n') { - if (config_file_eof) + if (cf->eof) return 0; comment = 0; continue; @@ -335,7 +330,7 @@ static int git_parse_file(config_fn_t fn, void *data) if (get_value(fn, data, var, baselen+1) < 0) break; } - die("bad config file line %d in %s", config_linenr, config_file_name); + die("bad config file line %d in %s", cf->linenr, cf->name); } static int parse_unit_factor(const char *end, unsigned long *val) @@ -386,8 +381,8 @@ int git_parse_ulong(const char *value, unsigned long *ret) static void die_bad_config(const char *name) { - if (config_file_name) - die("bad config value for '%s' in %s", name, config_file_name); + if (cf && cf->name) + die("bad config value for '%s' in %s", name, cf->name); die("bad config value for '%s'", name); } @@ -581,6 +576,9 @@ static int git_default_core_config(const char *var, const char *value) return 0; } + if (!strcmp(var, "core.logpackaccess")) + return git_config_string(&log_pack_access, var, value); + if (!strcmp(var, "core.autocrlf")) { if (value && !strcasecmp(value, "input")) { if (core_eol == EOL_CRLF) @@ -807,13 +805,24 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data) ret = -1; if (f) { - config_file = f; - config_file_name = filename; - config_linenr = 1; - config_file_eof = 0; + config_file top; + + /* push config-file parsing state stack */ + top.prev = cf; + top.f = f; + top.name = filename; + top.linenr = 1; + top.eof = 0; + strbuf_init(&top.value, 1024); + cf = ⊤ + ret = git_parse_file(fn, data); + + /* pop config-file parsing state stack */ + strbuf_release(&top.value); + cf = top.prev; + fclose(f); - config_file_name = NULL; } return ret; } @@ -837,22 +846,6 @@ int git_config_system(void) return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0); } -int git_config_from_parameters(config_fn_t fn, void *data) -{ - static int loaded_environment; - const struct config_item *ct; - - if (!loaded_environment) { - if (git_config_parse_environment() < 0) - return -1; - loaded_environment = 1; - } - for (ct = config_parameters; ct; ct = ct->next) - if (fn(ct->name, ct->value, data) < 0) - return -1; - return 0; -} - int git_config_early(config_fn_t fn, void *data, const char *repo_config) { int ret = 0, found = 0; @@ -882,9 +875,16 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config) found += 1; } - ret += git_config_from_parameters(fn, data); - if (config_parameters) - found += 1; + switch (git_config_from_parameters(fn, data)) { + case -1: /* error */ + die("unable to parse command-line config"); + break; + case 0: /* found nothing */ + break; + default: /* found at least one item */ + found++; + break; + } return ret == 0 ? found : ret; } @@ -930,6 +930,7 @@ static int store_aux(const char *key, const char *value, void *cb) { const char *ep; size_t section_len; + FILE *f = cf->f; switch (store.state) { case KEY_SEEN: @@ -941,7 +942,7 @@ static int store_aux(const char *key, const char *value, void *cb) return 1; } - store.offset[store.seen] = ftell(config_file); + store.offset[store.seen] = ftell(f); store.seen++; } break; @@ -968,19 +969,19 @@ static int store_aux(const char *key, const char *value, void *cb) * Do not increment matches: this is no match, but we * just made sure we are in the desired section. */ - store.offset[store.seen] = ftell(config_file); + store.offset[store.seen] = ftell(f); /* fallthru */ case SECTION_END_SEEN: case START: if (matches(key, value)) { - store.offset[store.seen] = ftell(config_file); + store.offset[store.seen] = ftell(f); store.state = KEY_SEEN; store.seen++; } else { if (strrchr(key, '.') - key == store.baselen && !strncmp(key, store.key, store.baselen)) { store.state = SECTION_SEEN; - store.offset[store.seen] = ftell(config_file); + store.offset[store.seen] = ftell(f); } } } @@ -1436,6 +1437,7 @@ int git_config_rename_section(const char *old_name, const char *new_name) struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1); int out_fd; char buf[1024]; + FILE *config_file; if (config_exclusive_filename) config_filename = xstrdup(config_exclusive_filename); @@ -1500,10 +1502,10 @@ int git_config_rename_section(const char *old_name, const char *new_name) } } fclose(config_file); - unlock_and_out: +unlock_and_out: if (commit_lock_file(lock) < 0) ret = error("could not commit config file %s", config_filename); - out: +out: free(config_filename); return ret; }