summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2019-07-11 10:56:05 +0200
committerPatrick Steinhardt <ps@pks.im>2019-07-11 11:10:04 +0200
commitdbeadf8a9e9cb66f65b894e4dfd1fb23f9f31d5b (patch)
tree5b60f577ce6de9f6e371684f707b1ef095f55529
parent3215752653885cbd33abb22ae9c356434d9f9dce (diff)
downloadlibgit2-dbeadf8a9e9cb66f65b894e4dfd1fb23f9f31d5b.tar.gz
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.
-rw-r--r--src/config_file.c10
-rw-r--r--src/config_mem.c18
-rw-r--r--src/config_parse.c11
-rw-r--r--src/config_parse.h5
-rw-r--r--src/parse.h2
5 files changed, 34 insertions, 12 deletions
diff --git a/src/config_file.c b/src/config_file.c
index d2238e811..51a3e93e2 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 b563a972e..e4006db32 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 bda1840fe..48ad1164f 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 a8657fd4a..0129ee309 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 21dcf9bd1..42a2aff1a 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);