config: make parsing stack struct independent from actual data source
[gitweb.git] / config.c
index aefd80b12a079d4a3c91d43c8a2c33ed6fbd0a38..fc2796771b689e85daf0a400488eeae3f7a48cd9 100644 (file)
--- a/config.c
+++ b/config.c
 #include "strbuf.h"
 #include "quote.h"
 
-typedef struct config_file {
-       struct config_file *prev;
-       FILE *f;
+struct config_source {
+       struct config_source *prev;
+       union {
+               FILE *file;
+       } u;
        const char *name;
        int linenr;
        int eof;
        struct strbuf value;
        struct strbuf var;
-} config_file;
 
-static config_file *cf;
+       int (*fgetc)(struct config_source *c);
+       int (*ungetc)(int c, struct config_source *conf);
+       long (*ftell)(struct config_source *c);
+};
+
+static struct config_source *cf;
 
 static int zlib_compression_seen;
 
+static int config_file_fgetc(struct config_source *conf)
+{
+       return fgetc(conf->u.file);
+}
+
+static int config_file_ungetc(int c, struct config_source *conf)
+{
+       return ungetc(c, conf->u.file);
+}
+
+static long config_file_ftell(struct config_source *conf)
+{
+       return ftell(conf->u.file);
+}
+
 #define MAX_INCLUDE_DEPTH 10
 static const char include_depth_advice[] =
 "exceeded maximum include depth (%d) while including\n"
@@ -168,27 +189,22 @@ int git_config_from_parameters(config_fn_t fn, void *data)
 
 static int get_next_char(void)
 {
-       int c;
-       FILE *f;
-
-       c = '\n';
-       if (cf && ((f = cf->f) != NULL)) {
-               c = fgetc(f);
-               if (c == '\r') {
-                       /* DOS like systems */
-                       c = fgetc(f);
-                       if (c != '\n') {
-                               ungetc(c, f);
-                               c = '\r';
-                       }
-               }
-               if (c == '\n')
-                       cf->linenr++;
-               if (c == EOF) {
-                       cf->eof = 1;
-                       c = '\n';
+       int c = cf->fgetc(cf);
+
+       if (c == '\r') {
+               /* DOS like systems */
+               c = cf->fgetc(cf);
+               if (c != '\n') {
+                       cf->ungetc(c, cf);
+                       c = '\r';
                }
        }
+       if (c == '\n')
+               cf->linenr++;
+       if (c == EOF) {
+               cf->eof = 1;
+               c = '\n';
+       }
        return c;
 }
 
@@ -339,7 +355,7 @@ static int get_base_var(struct strbuf *name)
        }
 }
 
-static int git_parse_file(config_fn_t fn, void *data)
+static int git_parse_source(config_fn_t fn, void *data)
 {
        int comment = 0;
        int baselen = 0;
@@ -896,6 +912,33 @@ int git_default_config(const char *var, const char *value, void *dummy)
        return 0;
 }
 
+/*
+ * All source specific fields in the union, name and the callbacks
+ * 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)
+{
+       int ret;
+
+       /* push config-file parsing state stack */
+       top->prev = cf;
+       top->linenr = 1;
+       top->eof = 0;
+       strbuf_init(&top->value, 1024);
+       strbuf_init(&top->var, 1024);
+       cf = top;
+
+       ret = git_parse_source(fn, data);
+
+       /* pop config-file parsing state stack */
+       strbuf_release(&top->value);
+       strbuf_release(&top->var);
+       cf = top->prev;
+
+       return ret;
+}
+
 int git_config_from_file(config_fn_t fn, const char *filename, void *data)
 {
        int ret;
@@ -903,24 +946,15 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data)
 
        ret = -1;
        if (f) {
-               config_file top;
+               struct config_source top;
 
-               /* push config-file parsing state stack */
-               top.prev = cf;
-               top.f = f;
+               top.u.file = f;
                top.name = filename;
-               top.linenr = 1;
-               top.eof = 0;
-               strbuf_init(&top.value, 1024);
-               strbuf_init(&top.var, 1024);
-               cf = ⊤
-
-               ret = git_parse_file(fn, data);
+               top.fgetc = config_file_fgetc;
+               top.ungetc = config_file_ungetc;
+               top.ftell = config_file_ftell;
 
-               /* pop config-file parsing state stack */
-               strbuf_release(&top.value);
-               strbuf_release(&top.var);
-               cf = top.prev;
+               ret = do_config_from(&top, fn, data);
 
                fclose(f);
        }
@@ -1053,7 +1087,6 @@ 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:
@@ -1065,7 +1098,7 @@ static int store_aux(const char *key, const char *value, void *cb)
                                return 1;
                        }
 
-                       store.offset[store.seen] = ftell(f);
+                       store.offset[store.seen] = cf->ftell(cf);
                        store.seen++;
                }
                break;
@@ -1092,19 +1125,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(f);
+               store.offset[store.seen] = cf->ftell(cf);
                /* fallthru */
        case SECTION_END_SEEN:
        case START:
                if (matches(key, value)) {
-                       store.offset[store.seen] = ftell(f);
+                       store.offset[store.seen] = cf->ftell(cf);
                        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(f);
+                                       store.offset[store.seen] = cf->ftell(cf);
                        }
                }
        }