config_parse: provide parser init and dispose functions Right now, all configuration file backends are expected to directly mess with the configuration parser's internals in order to set it up. Let's avoid doing that by implementing both a `git_config_parser_init` and `git_config_parser_dispose` function to clearly define the interface between configuration backends and the parser. Ideally, we would make the `git_config_parser` structure definition private to its implementation. But as that would require an additional memory allocation that was not required before we just live with it being visible to others.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
diff --git a/src/config_file.c b/src/config_file.c
index d2238e8..51a3e93 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -1176,9 +1176,9 @@ static int config_write(diskfile_backend *cfg, const char *orig_key, const char
{
char *orig_section = NULL, *section = NULL, *orig_name, *name, *ldot;
git_buf buf = GIT_BUF_INIT, contents = GIT_BUF_INIT;
+ git_config_parser parser = GIT_CONFIG_PARSER_INIT;
git_filebuf file = GIT_FILEBUF_INIT;
struct write_data write_data;
- git_config_parser reader;
int error;
memset(&write_data, 0, sizeof(write_data));
@@ -1196,8 +1196,8 @@ static int config_write(diskfile_backend *cfg, const char *orig_key, const char
if (error < 0 && error != GIT_ENOTFOUND)
goto done;
- reader.path = cfg->file.path;
- git_parse_ctx_init(&reader.ctx, contents.ptr, contents.size);
+ if ((git_config_parser_init(&parser, cfg->file.path, contents.ptr, contents.size)) < 0)
+ goto done;
ldot = strrchr(key, '.');
name = ldot + 1;
@@ -1217,7 +1217,7 @@ static int config_write(diskfile_backend *cfg, const char *orig_key, const char
write_data.preg = preg;
write_data.value = value;
- if ((error = git_config_parse(&reader, write_on_section, write_on_variable,
+ if ((error = git_config_parse(&parser, write_on_section, write_on_variable,
write_on_comment, write_on_eof, &write_data)) < 0)
goto done;
@@ -1243,7 +1243,7 @@ done:
git_buf_dispose(&buf);
git_buf_dispose(&contents);
git_filebuf_cleanup(&file);
- git_parse_ctx_clear(&reader.ctx);
+ git_config_parser_dispose(&parser);
return error;
}
diff --git a/src/config_mem.c b/src/config_mem.c
index b563a97..e4006db 100644
--- a/src/config_mem.c
+++ b/src/config_mem.c
@@ -78,20 +78,24 @@ static int read_variable_cb(
static int config_memory_open(git_config_backend *backend, git_config_level_t level, const git_repository *repo)
{
config_memory_backend *memory_backend = (config_memory_backend *) backend;
+ git_config_parser parser = GIT_PARSE_CTX_INIT;
config_memory_parse_data parse_data;
- git_config_parser reader;
+ int error;
GIT_UNUSED(repo);
- if (memory_backend->cfg.size == 0)
- return 0;
-
- git_parse_ctx_init(&reader.ctx, memory_backend->cfg.ptr, memory_backend->cfg.size);
- reader.path = "in-memory";
+ if ((error = git_config_parser_init(&parser, "in-memory", memory_backend->cfg.ptr,
+ memory_backend->cfg.size)) < 0)
+ goto out;
parse_data.entries = memory_backend->entries;
parse_data.level = level;
- return git_config_parse(&reader, NULL, read_variable_cb, NULL, NULL, &parse_data);
+ if ((error = git_config_parse(&parser, NULL, read_variable_cb, NULL, NULL, &parse_data)) < 0)
+ goto out;
+
+out:
+ git_config_parser_dispose(&parser);
+ return error;
}
static int config_memory_get(git_config_backend *backend, const char *key, git_config_entry **out)
diff --git a/src/config_parse.c b/src/config_parse.c
index bda1840..48ad116 100644
--- a/src/config_parse.c
+++ b/src/config_parse.c
@@ -474,6 +474,17 @@ out:
return error;
}
+int git_config_parser_init(git_config_parser *out, const char *path, const char *data, size_t datalen)
+{
+ out->path = path;
+ return git_parse_ctx_init(&out->ctx, data, datalen);
+}
+
+void git_config_parser_dispose(git_config_parser *parser)
+{
+ git_parse_ctx_clear(&parser->ctx);
+}
+
int git_config_parse(
git_config_parser *parser,
git_config_parser_section_cb on_section,
diff --git a/src/config_parse.h b/src/config_parse.h
index a8657fd..0129ee3 100644
--- a/src/config_parse.h
+++ b/src/config_parse.h
@@ -22,6 +22,8 @@ typedef struct {
git_parse_ctx ctx;
} git_config_parser;
+#define GIT_CONFIG_PARSER_INIT { NULL, GIT_PARSE_CTX_INIT }
+
typedef int (*git_config_parser_section_cb)(
git_config_parser *parser,
const char *current_section,
@@ -49,6 +51,9 @@ typedef int (*git_config_parser_eof_cb)(
const char *current_section,
void *payload);
+int git_config_parser_init(git_config_parser *out, const char *path, const char *data, size_t datalen);
+void git_config_parser_dispose(git_config_parser *parser);
+
int git_config_parse(
git_config_parser *parser,
git_config_parser_section_cb on_section,
diff --git a/src/parse.h b/src/parse.h
index 21dcf9b..42a2aff 100644
--- a/src/parse.h
+++ b/src/parse.h
@@ -23,6 +23,8 @@ typedef struct {
size_t line_num;
} git_parse_ctx;
+#define GIT_PARSE_CTX_INIT { 0 }
+
int git_parse_ctx_init(git_parse_ctx *ctx, const char *content, size_t content_len);
void git_parse_ctx_clear(git_parse_ctx *ctx);