From: Michael Tremer Date: Sat, 11 Jan 2025 12:21:41 +0000 (+0000) Subject: parser: Refactor creating a new parser X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e4a2d978a6b23f5efb7980bc45fba13bad7de664;p=people%2Fric9%2Fpakfire.git parser: Refactor creating a new parser Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/dist.c b/src/pakfire/dist.c index 0397958c6..6e251ffef 100644 --- a/src/pakfire/dist.c +++ b/src/pakfire/dist.c @@ -156,11 +156,10 @@ int pakfire_read_makefile(struct pakfire_parser** parser, struct pakfire* pakfir // Fetch context struct pakfire_ctx* ctx = pakfire_ctx(pakfire); - *parser = pakfire_parser_create(pakfire, NULL, NULL, PAKFIRE_PARSER_FLAGS_EXPAND_COMMANDS); - if (!*parser) { - r = 1; + // Create a new parser + r = pakfire_parser_create(parser, pakfire, NULL, NULL, PAKFIRE_PARSER_FLAGS_EXPAND_COMMANDS); + if (r < 0) goto ERROR; - } // Set defaults r = pakfire_makefile_set_defaults(pakfire, *parser, path); diff --git a/src/pakfire/parser.c b/src/pakfire/parser.c index 17cc8f407..74a3f6c6b 100644 --- a/src/pakfire/parser.c +++ b/src/pakfire/parser.c @@ -83,42 +83,6 @@ static int pakfire_parser_compile_regexes(struct pakfire_parser* parser) { return 0; } -struct pakfire_parser* pakfire_parser_create(struct pakfire* pakfire, - struct pakfire_parser* parent, const char* namespace, int flags) { - struct pakfire_parser* parser = calloc(1, sizeof(*parser)); - if (parser) { - parser->ctx = pakfire_ctx(pakfire); - parser->pakfire = pakfire_ref(pakfire); - parser->nrefs = 1; - - // Store a reference to the parent parser if we have one - if (parent) - parser->parent = pakfire_parser_ref(parent); - - // Store flags - parser->flags = flags; - - // Make namespace - pakfire_parser_set_namespace(parser, namespace); - } - - return parser; -} - -struct pakfire_parser* pakfire_parser_create_child(struct pakfire_parser* parser, const char* namespace) { - return pakfire_parser_create(parser->pakfire, parser, namespace, parser->flags); -} - -struct pakfire_parser* pakfire_parser_ref(struct pakfire_parser* parser) { - ++parser->nrefs; - - return parser; -} - -struct pakfire* pakfire_parser_get_pakfire(struct pakfire_parser* parser) { - return pakfire_ref(parser->pakfire); -} - static void pakfire_parser_free_declarations(struct pakfire_parser* parser) { if (!parser->declarations) return; @@ -156,6 +120,69 @@ static void pakfire_parser_free(struct pakfire_parser* parser) { free(parser); } +int pakfire_parser_create(struct pakfire_parser** parser, + struct pakfire* pakfire, struct pakfire_parser* parent, const char* namespace, int flags) { + struct pakfire_parser* self = NULL; + int r; + + // Allocate some memory + self = calloc(1, sizeof(*self)); + if (!self) + return -errno; + + // Store a reference to the context + self->ctx = pakfire_ctx(pakfire); + + // Store a reference to Pakfire + self->pakfire = pakfire_ref(pakfire); + + // Initialize the reference counter + self->nrefs = 1; + + // Store a reference to the parent parser if we have one + if (parent) + self->parent = pakfire_parser_ref(parent); + + // Store flags + self->flags = flags; + + // Make namespace + r = pakfire_parser_set_namespace(self, namespace); + if (r < 0) + goto ERROR; + + // Return the pointer + *parser = self; + return 0; + +ERROR: + pakfire_parser_free(self); + + return r; +} + +struct pakfire_parser* pakfire_parser_create_child(struct pakfire_parser* parser, const char* namespace) { + struct pakfire_parser* child = NULL; + int r; + + // Create the parser + r = pakfire_parser_create(&child, parser->pakfire, parser, namespace, parser->flags); + if (r < 0) + return NULL; + + return child; +} + +struct pakfire_parser* pakfire_parser_ref(struct pakfire_parser* parser) { + ++parser->nrefs; + + return parser; +} + +struct pakfire* pakfire_parser_get_pakfire(struct pakfire_parser* parser) { + return pakfire_ref(parser->pakfire); +} + struct pakfire_parser* pakfire_parser_unref(struct pakfire_parser* parser) { if (--parser->nrefs > 0) return parser; diff --git a/src/pakfire/parser.h b/src/pakfire/parser.h index 54a0fb186..d0cf9275a 100644 --- a/src/pakfire/parser.h +++ b/src/pakfire/parser.h @@ -38,8 +38,8 @@ enum pakfire_parser_flags { struct pakfire_parser_error; -struct pakfire_parser* pakfire_parser_create(struct pakfire* pakfire, struct pakfire_parser* parser, - const char* namespace, int flags); +int pakfire_parser_create(struct pakfire_parser** parser, struct pakfire* pakfire, + struct pakfire_parser* parent, const char* namespace, int flags); struct pakfire_parser* pakfire_parser_create_child(struct pakfire_parser* parser, const char* namespace); struct pakfire_parser* pakfire_parser_ref(struct pakfire_parser* parser); diff --git a/src/pakfire/parser/grammar.y b/src/pakfire/parser/grammar.y index 52a58d5a4..9d94539e2 100644 --- a/src/pakfire/parser/grammar.y +++ b/src/pakfire/parser/grammar.y @@ -171,7 +171,9 @@ static void pakfire_parser_free_declaration(struct pakfire_parser_declaration* d } %initial-action { - *parser = pakfire_parser_create(pakfire, parent, NULL, 0); + int r = pakfire_parser_create(parser, pakfire, parent, NULL, 0); + if (r < 0) + ABORT; }; %destructor { if ($$) pakfire_parser_unref($$); } @@ -183,8 +185,8 @@ static void pakfire_parser_free_declaration(struct pakfire_parser_declaration* d grammar : %empty { - $$ = pakfire_parser_create(pakfire, *parser, NULL, 0); - if (!$$) + int r = pakfire_parser_create(&$$, pakfire, *parser, NULL, 0); + if (r < 0) ABORT; // This becomes the new top parser @@ -342,13 +344,14 @@ subparser : subparser_name T_EOL block T_END T_EOL { char* key = NULL; char* value = NULL; + int r; // Create a new parser - $$ = pakfire_parser_create(pakfire, *parser, NULL, 0); - if (!$$) + r = pakfire_parser_create(&$$, pakfire, *parser, NULL, 0); + if (r < 0) ABORT; - int r = pakfire_string_partition($1, ":", &key, &value); + r = pakfire_string_partition($1, ":", &key, &value); if (r) ABORT; diff --git a/tests/libpakfire/makefile.c b/tests/libpakfire/makefile.c index f5adc6bfc..b7b7b5d63 100644 --- a/tests/libpakfire/makefile.c +++ b/tests/libpakfire/makefile.c @@ -70,7 +70,7 @@ static int test_parse(const struct test* t) { FILE* f = fopen(path, "r"); ASSERT(f); - parser = pakfire_parser_create(t->pakfire, NULL, NULL, 0); + ASSERT_SUCCESS(pakfire_parser_create(&parser, t->pakfire, NULL, NULL, 0)); ASSERT_SUCCESS(pakfire_parser_read(parser, f, NULL)); @@ -93,10 +93,10 @@ FAIL: } static int test_macros(const struct test* t) { + struct pakfire_parser* parser = NULL; int r = EXIT_FAILURE; - struct pakfire_parser* parser = pakfire_parser_create(t->pakfire, NULL, NULL, 0); - ASSERT(parser); + ASSERT_SUCCESS(pakfire_parser_create(&parser, t->pakfire, NULL, NULL, 0)); // Load 'em all ASSERT_SUCCESS(load_macros(parser)); @@ -120,9 +120,8 @@ static int test_packages(const struct test* t) { ASSERT_SUCCESS(pakfire_repo_create(&repo, t->pakfire, "test")); ASSERT(repo); - parser = pakfire_parser_create(t->pakfire, NULL, NULL, - PAKFIRE_PARSER_FLAGS_EXPAND_COMMANDS); - ASSERT(parser); + ASSERT_SUCCESS(pakfire_parser_create(&parser, t->pakfire, NULL, NULL, + PAKFIRE_PARSER_FLAGS_EXPAND_COMMANDS)); // Set some architecture pakfire_parser_set(parser, NULL, "DISTRO_ARCH", "x86_64", 0); diff --git a/tests/libpakfire/parser.c b/tests/libpakfire/parser.c index ff27fe647..618ed2ea7 100644 --- a/tests/libpakfire/parser.c +++ b/tests/libpakfire/parser.c @@ -29,12 +29,13 @@ #include "../testsuite.h" static int test_parser(const struct test* t) { + struct pakfire_parser* parser = NULL; struct pakfire_parser* subparser = NULL; char* value = NULL; int r = EXIT_FAILURE; // Create a new parser - struct pakfire_parser* parser = pakfire_parser_create(t->pakfire, NULL, NULL, 0); + ASSERT_SUCCESS(pakfire_parser_create(&parser, t->pakfire, NULL, NULL, 0)); // Retrieve a value that does not exist value = pakfire_parser_get(parser, NULL, "null"); @@ -116,6 +117,7 @@ static const char* files[] = { }; static int test_parser_files(const struct test* t) { + struct pakfire_parser* parser = NULL; const char** file = files; char path[PATH_MAX]; int r = EXIT_FAILURE; @@ -124,7 +126,7 @@ static int test_parser_files(const struct test* t) { ASSERT_SUCCESS(pakfire_string_format(path, "%s/%s", TEST_SRC_PATH, *file)); // Create a new parser - struct pakfire_parser* parser = pakfire_parser_create(t->pakfire, NULL, NULL, 0); + ASSERT_SUCCESS(pakfire_parser_create(&parser, t->pakfire, NULL, NULL, 0)); FILE* f = fopen(path, "r"); ASSERT(f); @@ -150,10 +152,11 @@ FAIL: } static int test_append(const struct test* t) { + struct pakfire_parser* parser = NULL; int r = EXIT_FAILURE; // Create a new parser - struct pakfire_parser* parser = pakfire_parser_create(t->pakfire, NULL, NULL, 0); + ASSERT_SUCCESS(pakfire_parser_create(&parser, t->pakfire, NULL, NULL, 0)); const char* str1 = "a = 1\n" @@ -203,9 +206,7 @@ static int test_parser_command(const struct test* t) { struct pakfire_parser* parser = NULL; int r = EXIT_FAILURE; - parser = pakfire_parser_create(t->pakfire, NULL, NULL, - PAKFIRE_PARSER_FLAGS_EXPAND_COMMANDS); - ASSERT(parser); + ASSERT_SUCCESS(pakfire_parser_create(&parser, t->pakfire, NULL, NULL, PAKFIRE_PARSER_FLAGS_EXPAND_COMMANDS)); ASSERT_SUCCESS(pakfire_parser_set(parser, NULL, "command", command, 0)); diff --git a/tests/parser/test.c b/tests/parser/test.c index cc55aa1a6..99aa11037 100644 --- a/tests/parser/test.c +++ b/tests/parser/test.c @@ -72,9 +72,9 @@ int main(int argc, const char* argv[]) { } // Create a new parser - parser = pakfire_parser_create(pakfire, NULL, NULL, 0); - if (!parser) { - fprintf(stderr, "Could not create a parser: %m\n"); + r = pakfire_parser_create(&parser, pakfire, NULL, NULL, 0); + if (r < 0) { + fprintf(stderr, "Could not create a parser: %s\n", strerror(-r)); goto ERROR; }