// 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);
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;
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;
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);
}
%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($$); } <parser>
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
{
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;
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));
}
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));
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);
#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");
};
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;
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);
}
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"
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));
}
// 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;
}