aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2016-12-25 00:43:42 +0100
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2016-12-25 00:43:42 +0100
commit6636bd99d6767a99546b1b82ce69ade6df867b42 (patch)
tree9855c1668ffe7ba367ed0f976bc50b4cc048e8a6
parentccb429435e162915917f2492217c4e206b9b2a96 (diff)
downloadblogc-6636bd99d6767a99546b1b82ce69ade6df867b42.tar.gz
blogc-6636bd99d6767a99546b1b82ce69ade6df867b42.tar.bz2
blogc-6636bd99d6767a99546b1b82ce69ade6df867b42.zip
config-parser: added support to parse section as list of lines
-rw-r--r--src/blogc-git-receiver/post-receive.c2
-rw-r--r--src/common/config-parser.c113
-rw-r--r--src/common/config-parser.h4
-rw-r--r--tests/blogc-git-receiver/check_post_receive.c6
-rw-r--r--tests/common/check_config_parser.c163
5 files changed, 258 insertions, 30 deletions
diff --git a/src/blogc-git-receiver/post-receive.c b/src/blogc-git-receiver/post-receive.c
index e722d60..415cb0c 100644
--- a/src/blogc-git-receiver/post-receive.c
+++ b/src/blogc-git-receiver/post-receive.c
@@ -102,7 +102,7 @@ bgr_post_receive_hook(int argc, char *argv[])
goto cleanup;
}
- bc_config_t *config = bc_config_parse(config_content, len, &err);
+ bc_config_t *config = bc_config_parse(config_content, len, NULL, &err);
free(config_content);
if (err != NULL) {
fprintf(stderr, "warning: failed to parse configuration file (%s), "
diff --git a/src/common/config-parser.c b/src/common/config-parser.c
index bfea2d4..19ca192 100644
--- a/src/common/config-parser.c
+++ b/src/common/config-parser.c
@@ -8,6 +8,7 @@
#include <stdbool.h>
#include <stdlib.h>
+#include <string.h>
#include "error.h"
#include "utils.h"
#include "config-parser.h"
@@ -20,11 +21,41 @@ typedef enum {
CONFIG_SECTION_KEY,
CONFIG_SECTION_VALUE_START,
CONFIG_SECTION_VALUE,
+ CONFIG_SECTION_LIST,
} bc_configparser_state_t;
+typedef enum {
+ CONFIG_SECTION_TYPE_MAP = 1,
+ CONFIG_SECTION_TYPE_LIST,
+} bc_configparser_section_type_t;
+
+typedef struct {
+ bc_configparser_section_type_t type;
+ void *data;
+} bc_configparser_section_t;
+
+
+static void
+free_section(bc_configparser_section_t *section)
+{
+ if (section == NULL)
+ return;
+
+ switch (section->type) {
+ case CONFIG_SECTION_TYPE_MAP:
+ bc_trie_free(section->data);
+ break;
+ case CONFIG_SECTION_TYPE_LIST:
+ bc_slist_free_full(section->data, free);
+ break;
+ }
+ free(section);
+}
+
bc_config_t*
-bc_config_parse(const char *src, size_t src_len, bc_error_t **err)
+bc_config_parse(const char *src, size_t src_len, const char *list_sections[],
+ bc_error_t **err)
{
if (err == NULL || *err != NULL)
return NULL;
@@ -32,14 +63,14 @@ bc_config_parse(const char *src, size_t src_len, bc_error_t **err)
size_t current = 0;
size_t start = 0;
- bc_trie_t *section = NULL;
+ bc_configparser_section_t *section = NULL;
char *section_name = NULL;
char *key = NULL;
char *value = NULL;
bc_config_t *rv = bc_malloc(sizeof(bc_config_t));
- rv->root = bc_trie_new((bc_free_func_t) bc_trie_free);
+ rv->root = bc_trie_new((bc_free_func_t) free_section);
bc_configparser_state_t state = CONFIG_START;
@@ -66,7 +97,14 @@ bc_config_parse(const char *src, size_t src_len, bc_error_t **err)
}
if (section != NULL) {
start = current;
- state = CONFIG_SECTION_KEY;
+ switch (section->type) {
+ case CONFIG_SECTION_TYPE_MAP:
+ state = CONFIG_SECTION_KEY;
+ break;
+ case CONFIG_SECTION_TYPE_LIST:
+ state = CONFIG_SECTION_LIST;
+ break;
+ }
continue;
}
*err = bc_error_parser(BC_ERROR_CONFIG_PARSER, src, src_len,
@@ -81,7 +119,24 @@ bc_config_parse(const char *src, size_t src_len, bc_error_t **err)
case CONFIG_SECTION:
if (c == ']') {
section_name = bc_strndup(src + start, current - start);
- section = bc_trie_new(free);
+ section = bc_malloc(sizeof(bc_configparser_section_t));
+ section->type = CONFIG_SECTION_TYPE_MAP;
+ if (list_sections != NULL) {
+ for (size_t i = 0; list_sections[i] != NULL; i++) {
+ if (0 == strcmp(section_name, list_sections[i])) {
+ section->type = CONFIG_SECTION_TYPE_LIST;
+ break;
+ }
+ }
+ }
+ switch (section->type) {
+ case CONFIG_SECTION_TYPE_MAP:
+ section->data = bc_trie_new(free);
+ break;
+ case CONFIG_SECTION_TYPE_LIST:
+ section->data = NULL;
+ break;
+ }
bc_trie_insert(rv->root, section_name, section);
free(section_name);
section_name = NULL;
@@ -122,7 +177,7 @@ bc_config_parse(const char *src, size_t src_len, bc_error_t **err)
size_t end = is_last && c != '\n' && c != '\r' ? src_len :
current;
value = bc_strndup(src + start, end - start);
- bc_trie_insert(section, bc_str_strip(key),
+ bc_trie_insert(section->data, bc_str_strip(key),
bc_strdup(bc_str_strip(value)));
free(key);
key = NULL;
@@ -133,6 +188,21 @@ bc_config_parse(const char *src, size_t src_len, bc_error_t **err)
}
break;
+ case CONFIG_SECTION_LIST:
+ if (c == '\r' || c == '\n' || is_last) {
+ size_t end = is_last && c != '\n' && c != '\r' ? src_len :
+ current;
+ value = bc_strndup(src + start, end - start);
+ section->data = bc_slist_append(section->data,
+ bc_strdup(bc_str_strip(value)));
+ free(value);
+ value = NULL;
+ state = CONFIG_START;
+ break;
+
+ }
+ break;
+
}
if (*err != NULL) {
@@ -187,12 +257,15 @@ bc_config_list_keys(bc_config_t *config, const char *section)
if (config == NULL)
return NULL;
- bc_trie_t *s = bc_trie_lookup(config->root, section);
+ bc_configparser_section_t *s = bc_trie_lookup(config->root, section);
if (s == NULL)
return NULL;
+ if (s->type != CONFIG_SECTION_TYPE_MAP)
+ return NULL;
+
bc_slist_t *l = NULL;
- bc_trie_foreach(s, (bc_trie_foreach_func_t) list_keys, &l);
+ bc_trie_foreach(s->data, (bc_trie_foreach_func_t) list_keys, &l);
char **rv = bc_malloc(sizeof(char*) * (bc_slist_length(l) + 1));
@@ -213,11 +286,14 @@ bc_config_get(bc_config_t *config, const char *section, const char *key)
if (config == NULL)
return NULL;
- bc_trie_t *s = bc_trie_lookup(config->root, section);
+ bc_configparser_section_t *s = bc_trie_lookup(config->root, section);
if (s == NULL)
return NULL;
- return bc_trie_lookup(s, key);
+ if (s->type != CONFIG_SECTION_TYPE_MAP)
+ return NULL;
+
+ return bc_trie_lookup(s->data, key);
}
@@ -232,6 +308,23 @@ bc_config_get_with_default(bc_config_t *config, const char *section, const char
}
+bc_slist_t*
+bc_config_get_list(bc_config_t *config, const char *section)
+{
+ if (config == NULL)
+ return NULL;
+
+ bc_configparser_section_t *s = bc_trie_lookup(config->root, section);
+ if (s == NULL)
+ return NULL;
+
+ if (s->type != CONFIG_SECTION_TYPE_LIST)
+ return NULL;
+
+ return s->data;
+}
+
+
void
bc_config_free(bc_config_t *config)
{
diff --git a/src/common/config-parser.h b/src/common/config-parser.h
index e249c37..0bf1706 100644
--- a/src/common/config-parser.h
+++ b/src/common/config-parser.h
@@ -17,13 +17,15 @@ typedef struct {
bc_trie_t *root;
} bc_config_t;
-bc_config_t* bc_config_parse(const char *src, size_t src_len, bc_error_t **err);
+bc_config_t* bc_config_parse(const char *src, size_t src_len,
+ const char *list_sections[], bc_error_t **err);
char** bc_config_list_sections(bc_config_t *config);
char** bc_config_list_keys(bc_config_t *config, const char *section);
const char* bc_config_get(bc_config_t *config, const char *section,
const char *key);
const char* bc_config_get_with_default(bc_config_t *config, const char *section,
const char *key, const char *default_);
+bc_slist_t* bc_config_get_list(bc_config_t *config, const char *section);
void bc_config_free(bc_config_t *config);
#endif /* _CONFIG_PARSER_H */
diff --git a/tests/blogc-git-receiver/check_post_receive.c b/tests/blogc-git-receiver/check_post_receive.c
index 8e56f98..cf003e0 100644
--- a/tests/blogc-git-receiver/check_post_receive.c
+++ b/tests/blogc-git-receiver/check_post_receive.c
@@ -33,7 +33,7 @@ test_post_receive_get_config_section(void **state)
{
bc_error_t *err = NULL;
- bc_config_t *config = bc_config_parse("", 0, &err);
+ bc_config_t *config = bc_config_parse("", 0, NULL, &err);
assert_null(err);
assert_null(bgr_post_receive_get_config_section(config,
"/home/blogc/repos/foo.git", "/home/blogc"));
@@ -51,7 +51,7 @@ test_post_receive_get_config_section(void **state)
"[repo:baz.git]\n"
"mirror = baz\n"
"\n";
- config = bc_config_parse(conf, strlen(conf), &err);
+ config = bc_config_parse(conf, strlen(conf), NULL, &err);
assert_null(err);
char *s = bgr_post_receive_get_config_section(config,
"/home/blogc/repos/bar.git", "/home/blogc");
@@ -71,7 +71,7 @@ test_post_receive_get_config_section(void **state)
"[repo:asd/baz.git]\n"
"mirror = baz\n"
"\n";
- config = bc_config_parse(conf, strlen(conf), &err);
+ config = bc_config_parse(conf, strlen(conf), NULL, &err);
assert_null(err);
s = bgr_post_receive_get_config_section(config,
"/home/blogc/repos/asd/bar.git", "/home/blogc");
diff --git a/tests/common/check_config_parser.c b/tests/common/check_config_parser.c
index 66003e0..b542c88 100644
--- a/tests/common/check_config_parser.c
+++ b/tests/common/check_config_parser.c
@@ -24,7 +24,7 @@ test_config_empty(void **state)
{
const char *a = "";
bc_error_t *err = NULL;
- bc_config_t *c = bc_config_parse(a, strlen(a), &err);
+ bc_config_t *c = bc_config_parse(a, strlen(a), NULL, &err);
assert_null(err);
assert_non_null(c);
assert_non_null(c->root);
@@ -39,7 +39,7 @@ test_config_section_empty(void **state)
{
const char *a = "[foo]";
bc_error_t *err = NULL;
- bc_config_t *c = bc_config_parse(a, strlen(a), &err);
+ bc_config_t *c = bc_config_parse(a, strlen(a), NULL, &err);
assert_null(err);
assert_non_null(c);
assert_non_null(c->root);
@@ -66,7 +66,7 @@ test_config_section(void **state)
"[foo]\n"
"asd = zxc";
bc_error_t *err = NULL;
- bc_config_t *c = bc_config_parse(a, strlen(a), &err);
+ bc_config_t *c = bc_config_parse(a, strlen(a), NULL, &err);
assert_null(err);
assert_non_null(c);
assert_non_null(c->root);
@@ -90,7 +90,7 @@ test_config_section(void **state)
"[foo]\n"
"asd = zxc\n";
err = NULL;
- c = bc_config_parse(a, strlen(a), &err);
+ c = bc_config_parse(a, strlen(a), NULL, &err);
assert_null(err);
assert_non_null(c);
assert_non_null(c->root);
@@ -114,7 +114,7 @@ test_config_section(void **state)
"[foo]\r\n"
"asd = zxc\r\n";
err = NULL;
- c = bc_config_parse(a, strlen(a), &err);
+ c = bc_config_parse(a, strlen(a), NULL, &err);
assert_null(err);
assert_non_null(c);
assert_non_null(c->root);
@@ -145,7 +145,7 @@ test_config_section_multiple_keys(void **state)
"qwe = rty\n"
"zxc = vbn";
bc_error_t *err = NULL;
- bc_config_t *c = bc_config_parse(a, strlen(a), &err);
+ bc_config_t *c = bc_config_parse(a, strlen(a), NULL, &err);
assert_null(err);
assert_non_null(c);
assert_non_null(c->root);
@@ -175,7 +175,7 @@ test_config_section_multiple_keys(void **state)
"qwe = rty\n"
"zxc = vbn\n";
err = NULL;
- c = bc_config_parse(a, strlen(a), &err);
+ c = bc_config_parse(a, strlen(a), NULL, &err);
assert_null(err);
assert_non_null(c);
assert_non_null(c->root);
@@ -205,7 +205,7 @@ test_config_section_multiple_keys(void **state)
"qwe = rty\r\n"
"zxc = vbn\r\n";
err = NULL;
- c = bc_config_parse(a, strlen(a), &err);
+ c = bc_config_parse(a, strlen(a), NULL, &err);
assert_null(err);
assert_non_null(c);
assert_non_null(c->root);
@@ -243,7 +243,7 @@ test_config_section_multiple_sections(void **state)
"[bar]\n"
"lol = hehe";
bc_error_t *err = NULL;
- bc_config_t *c = bc_config_parse(a, strlen(a), &err);
+ bc_config_t *c = bc_config_parse(a, strlen(a), NULL, &err);
assert_null(err);
assert_non_null(c);
assert_non_null(c->root);
@@ -284,7 +284,7 @@ test_config_section_multiple_sections(void **state)
"[bar]\n"
"lol = hehe\n";
err = NULL;
- c = bc_config_parse(a, strlen(a), &err);
+ c = bc_config_parse(a, strlen(a), NULL, &err);
assert_null(err);
assert_non_null(c);
assert_non_null(c->root);
@@ -325,7 +325,7 @@ test_config_section_multiple_sections(void **state)
"[bar]\r\n"
"lol = hehe\r\n";
err = NULL;
- c = bc_config_parse(a, strlen(a), &err);
+ c = bc_config_parse(a, strlen(a), NULL, &err);
assert_null(err);
assert_non_null(c);
assert_non_null(c->root);
@@ -360,13 +360,145 @@ test_config_section_multiple_sections(void **state)
static void
+test_config_section_list(void **state)
+{
+ const char *a =
+ "[foo]\n"
+ "asd = zxc\n"
+ "qwe = rty\n"
+ "zxc = vbn\n"
+ "\n"
+ "[bar]\n"
+ "lol = hehe\n"
+ "asdasdadssad";
+ bc_error_t *err = NULL;
+ const char *sections[] = {"bar", NULL};
+ bc_config_t *c = bc_config_parse(a, strlen(a), sections, &err);
+ assert_null(err);
+ assert_non_null(c);
+ assert_non_null(c->root);
+ assert_int_equal(bc_trie_size(c->root), 2);
+ char **s = bc_config_list_sections(c);
+ assert_non_null(s);
+ assert_int_equal(bc_strv_length(s), 2);
+ assert_string_equal(s[0], "foo");
+ assert_string_equal(s[1], "bar");
+ assert_null(s[2]);
+ bc_strv_free(s);
+ assert_string_equal(bc_config_get(c, "foo", "asd"), "zxc");
+ assert_string_equal(bc_config_get(c, "foo", "qwe"), "rty");
+ assert_string_equal(bc_config_get(c, "foo", "zxc"), "vbn");
+ bc_slist_t *foo = bc_config_get_list(c, "bar");
+ assert_non_null(foo);
+ assert_string_equal(foo->data, "lol = hehe");
+ assert_string_equal(foo->next->data, "asdasdadssad");
+ assert_null(foo->next->next);
+ char **k = bc_config_list_keys(c, "foo");
+ assert_non_null(k);
+ assert_int_equal(bc_strv_length(k), 3);
+ assert_string_equal(k[0], "asd");
+ assert_string_equal(k[1], "qwe");
+ assert_string_equal(k[2], "zxc");
+ assert_null(k[3]);
+ bc_strv_free(k);
+ k = bc_config_list_keys(c, "bar");
+ assert_null(k);
+ bc_config_free(c);
+
+ a =
+ "[foo]\n"
+ "asd = zxc\n"
+ "qwe = rty\n"
+ "zxc = vbn\n"
+ "\n"
+ "[bar]\n"
+ "lol = hehe\n"
+ "asdasdadssad\n";
+ err = NULL;
+ c = bc_config_parse(a, strlen(a), sections, &err);
+ assert_null(err);
+ assert_non_null(c);
+ assert_non_null(c->root);
+ assert_int_equal(bc_trie_size(c->root), 2);
+ s = bc_config_list_sections(c);
+ assert_non_null(s);
+ assert_int_equal(bc_strv_length(s), 2);
+ assert_string_equal(s[0], "foo");
+ assert_string_equal(s[1], "bar");
+ assert_null(s[2]);
+ bc_strv_free(s);
+ assert_string_equal(bc_config_get(c, "foo", "asd"), "zxc");
+ assert_string_equal(bc_config_get(c, "foo", "qwe"), "rty");
+ assert_string_equal(bc_config_get(c, "foo", "zxc"), "vbn");
+ foo = bc_config_get_list(c, "bar");
+ assert_non_null(foo);
+ assert_string_equal(foo->data, "lol = hehe");
+ assert_string_equal(foo->next->data, "asdasdadssad");
+ assert_null(foo->next->next);
+ k = bc_config_list_keys(c, "foo");
+ assert_non_null(k);
+ assert_int_equal(bc_strv_length(k), 3);
+ assert_string_equal(k[0], "asd");
+ assert_string_equal(k[1], "qwe");
+ assert_string_equal(k[2], "zxc");
+ assert_null(k[3]);
+ bc_strv_free(k);
+ k = bc_config_list_keys(c, "bar");
+ assert_null(k);
+ bc_config_free(c);
+
+ a =
+ "[foo]\r\n"
+ "asd = zxc\r\n"
+ "qwe = rty\r\n"
+ "zxc = vbn\r\n"
+ "\r\n"
+ "[bar]\r\n"
+ "lol = hehe\r\n"
+ "asdasdadssad\r\n";
+ err = NULL;
+ c = bc_config_parse(a, strlen(a), sections, &err);
+ assert_null(err);
+ assert_non_null(c);
+ assert_non_null(c->root);
+ assert_int_equal(bc_trie_size(c->root), 2);
+ s = bc_config_list_sections(c);
+ assert_non_null(s);
+ assert_int_equal(bc_strv_length(s), 2);
+ assert_string_equal(s[0], "foo");
+ assert_string_equal(s[1], "bar");
+ assert_null(s[2]);
+ bc_strv_free(s);
+ assert_string_equal(bc_config_get(c, "foo", "asd"), "zxc");
+ assert_string_equal(bc_config_get(c, "foo", "qwe"), "rty");
+ assert_string_equal(bc_config_get(c, "foo", "zxc"), "vbn");
+ foo = bc_config_get_list(c, "bar");
+ assert_non_null(foo);
+ assert_string_equal(foo->data, "lol = hehe");
+ assert_string_equal(foo->next->data, "asdasdadssad");
+ assert_null(foo->next->next);
+ k = bc_config_list_keys(c, "foo");
+ assert_non_null(k);
+ assert_int_equal(bc_strv_length(k), 3);
+ assert_string_equal(k[0], "asd");
+ assert_string_equal(k[1], "qwe");
+ assert_string_equal(k[2], "zxc");
+ assert_null(k[3]);
+ bc_strv_free(k);
+ k = bc_config_list_keys(c, "bar");
+ assert_null(k);
+ bc_config_free(c);
+}
+
+
+static void
test_config_error_start(void **state)
{
const char *a =
"asd\n"
"[foo]";
bc_error_t *err = NULL;
- bc_config_t *c = bc_config_parse(a, strlen(a), &err);
+ bc_config_t *c = bc_config_parse(a, strlen(a), NULL, &err);
assert_non_null(err);
assert_null(c);
assert_int_equal(err->type, BC_ERROR_CONFIG_PARSER);
@@ -383,7 +515,7 @@ test_config_error_section_with_newline(void **state)
const char *a =
"[foo\nbar]";
bc_error_t *err = NULL;
- bc_config_t *c = bc_config_parse(a, strlen(a), &err);
+ bc_config_t *c = bc_config_parse(a, strlen(a), NULL, &err);
assert_non_null(err);
assert_null(c);
assert_int_equal(err->type, BC_ERROR_CONFIG_PARSER);
@@ -402,7 +534,7 @@ test_config_error_key_without_value(void **state)
"asd = 12\n"
"foo";
bc_error_t *err = NULL;
- bc_config_t *c = bc_config_parse(a, strlen(a), &err);
+ bc_config_t *c = bc_config_parse(a, strlen(a), NULL, &err);
assert_non_null(err);
assert_null(c);
assert_int_equal(err->type, BC_ERROR_CONFIG_PARSER);
@@ -415,7 +547,7 @@ test_config_error_key_without_value(void **state)
"asd = 12\n"
"foo\n";
err = NULL;
- c = bc_config_parse(a, strlen(a), &err);
+ c = bc_config_parse(a, strlen(a), NULL, &err);
assert_non_null(err);
assert_null(c);
assert_int_equal(err->type, BC_ERROR_CONFIG_PARSER);
@@ -435,6 +567,7 @@ main(void)
unit_test(test_config_section),
unit_test(test_config_section_multiple_keys),
unit_test(test_config_section_multiple_sections),
+ unit_test(test_config_section_list),
unit_test(test_config_error_start),
unit_test(test_config_error_section_with_newline),
unit_test(test_config_error_key_without_value),