};
#define CONFIG_SOURCE_INIT { 0 }
+struct config_reader {
+ struct config_source *source;
+};
+/*
+ * Where possible, prefer to accept "struct config_reader" as an arg than to use
+ * "the_reader". "the_reader" should only be used if that is infeasible, e.g. in
+ * a public function.
+ */
+static struct config_reader the_reader;
+
/*
* These variables record the "current" config source, which
* can be accessed by parsing callbacks.
* at the variables, it's either a bug for it to be called in the first place,
* or it's a function which can be reused for non-config purposes, and should
* fall back to some sane behavior).
+ *
+ * FIXME "cf_global" has been replaced by "the_reader.source", remove
+ * "cf_global" once we plumb "the_reader" through all of the callback functions.
*/
static struct config_source *cf_global;
static struct key_value_info *current_config_kvi;
*/
static enum config_scope current_parsing_scope;
-static inline void config_reader_push_source(struct config_source *top)
+static inline void config_reader_push_source(struct config_reader *reader,
+ struct config_source *top)
{
- top->prev = cf_global;
- cf_global = top;
+ top->prev = reader->source;
+ reader->source = top;
+ /* FIXME remove this when cf_global is removed. */
+ cf_global = reader->source;
}
-static inline struct config_source *config_reader_pop_source()
+static inline struct config_source *config_reader_pop_source(struct config_reader *reader)
{
struct config_source *ret;
- if (!cf_global)
+ if (!reader->source)
BUG("tried to pop config source, but we weren't reading config");
- ret = cf_global;
- cf_global = cf_global->prev;
+ ret = reader->source;
+ reader->source = reader->source->prev;
+ /* FIXME remove this when cf_global is removed. */
+ cf_global = reader->source;
return ret;
}
struct config_source source = CONFIG_SOURCE_INIT;
source.origin_type = CONFIG_ORIGIN_CMDLINE;
- config_reader_push_source(&source);
+ config_reader_push_source(&the_reader, &source);
env = getenv(CONFIG_COUNT_ENVIRONMENT);
if (env) {
strbuf_release(&envvar);
strvec_clear(&to_free);
free(envw);
- config_reader_pop_source();
+ config_reader_pop_source(&the_reader);
return ret;
}
{
int ret;
if (!git_parse_int(value, &ret))
- die_bad_number(cf_global, name, value);
+ die_bad_number(the_reader.source, name, value);
return ret;
}
{
int64_t ret;
if (!git_parse_int64(value, &ret))
- die_bad_number(cf_global, name, value);
+ die_bad_number(the_reader.source, name, value);
return ret;
}
{
unsigned long ret;
if (!git_parse_ulong(value, &ret))
- die_bad_number(cf_global, name, value);
+ die_bad_number(the_reader.source, name, value);
return ret;
}
{
ssize_t ret;
if (!git_parse_ssize_t(value, &ret))
- die_bad_number(cf_global, name, value);
+ die_bad_number(the_reader.source, name, value);
return ret;
}
* fgetc, ungetc, ftell of top need to be initialized before calling
* this function.
*/
-static int do_config_from(struct config_source *top, config_fn_t fn, void *data,
+static int do_config_from(struct config_reader *reader,
+ struct config_source *top, config_fn_t fn, void *data,
const struct config_options *opts)
{
int ret;
top->total_len = 0;
strbuf_init(&top->value, 1024);
strbuf_init(&top->var, 1024);
- config_reader_push_source(top);
+ config_reader_push_source(reader, top);
ret = git_parse_source(top, fn, data, opts);
/* pop config-file parsing state stack */
strbuf_release(&top->value);
strbuf_release(&top->var);
- config_reader_pop_source();
+ config_reader_pop_source(reader);
return ret;
}
-static int do_config_from_file(config_fn_t fn,
- const enum config_origin_type origin_type,
- const char *name, const char *path, FILE *f,
- void *data, const struct config_options *opts)
+static int do_config_from_file(struct config_reader *reader,
+ config_fn_t fn,
+ const enum config_origin_type origin_type,
+ const char *name, const char *path, FILE *f,
+ void *data, const struct config_options *opts)
{
struct config_source top = CONFIG_SOURCE_INIT;
int ret;
top.do_ftell = config_file_ftell;
flockfile(f);
- ret = do_config_from(&top, fn, data, opts);
+ ret = do_config_from(reader, &top, fn, data, opts);
funlockfile(f);
return ret;
}
static int git_config_from_stdin(config_fn_t fn, void *data)
{
- return do_config_from_file(fn, CONFIG_ORIGIN_STDIN, "", NULL, stdin,
- data, NULL);
+ return do_config_from_file(&the_reader, fn, CONFIG_ORIGIN_STDIN, "",
+ NULL, stdin, data, NULL);
}
int git_config_from_file_with_options(config_fn_t fn, const char *filename,
BUG("filename cannot be NULL");
f = fopen_or_warn(filename, "r");
if (f) {
- ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename,
- filename, f, data, opts);
+ ret = do_config_from_file(&the_reader, fn, CONFIG_ORIGIN_FILE,
+ filename, filename, f, data, opts);
fclose(f);
}
return ret;
top.do_ungetc = config_buf_ungetc;
top.do_ftell = config_buf_ftell;
- return do_config_from(&top, fn, data, opts);
+ return do_config_from(&the_reader, &top, fn, data, opts);
}
int git_config_from_blob_oid(config_fn_t fn,
int type;
if (current_config_kvi)
type = current_config_kvi->origin_type;
- else if(cf_global)
- type = cf_global->origin_type;
+ else if(the_reader.source)
+ type = the_reader.source->origin_type;
else
BUG("current_config_origin_type called outside config callback");
const char *name;
if (current_config_kvi)
name = current_config_kvi->filename;
- else if (cf_global)
- name = cf_global->name;
+ else if (the_reader.source)
+ name = the_reader.source->name;
else
BUG("current_config_name called outside config callback");
return name ? name : "";
if (current_config_kvi)
return current_config_kvi->linenr;
else
- return cf_global->linenr;
+ return the_reader.source->linenr;
}
int lookup_config(const char **mapping, int nr_mapping, const char *var)