From 231881110362221e4200241e90b368d6d17ead69 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sun, 10 Mar 2019 18:05:20 +0000 Subject: [PATCH] libpakfire: parser: Store declared variables in a long list Signed-off-by: Michael Tremer --- src/libpakfire/include/pakfire/parser.h | 5 +++ src/libpakfire/parser/grammar.y | 44 +++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/libpakfire/include/pakfire/parser.h b/src/libpakfire/include/pakfire/parser.h index b3a9247a6..300c9e682 100644 --- a/src/libpakfire/include/pakfire/parser.h +++ b/src/libpakfire/include/pakfire/parser.h @@ -25,6 +25,11 @@ #include +struct pakfire_parser_declaration { + char* name; + char* value; +}; + int pakfire_parser_parse_metadata(Pakfire pakfire, const char* data, size_t len); #endif /* PAKFIRE_PRIVATE */ diff --git a/src/libpakfire/parser/grammar.y b/src/libpakfire/parser/grammar.y index cafcb5e33..8119859a4 100644 --- a/src/libpakfire/parser/grammar.y +++ b/src/libpakfire/parser/grammar.y @@ -22,7 +22,9 @@ #include #include +#include #include +#include #define YYERROR_VERBOSE 1 @@ -40,6 +42,9 @@ static void yyerror(const char* s); static void cleanup(void); #define ABORT do { cleanup(); YYABORT; } while (0); +#define NUM_DECLARATIONS 128 +static int pakfire_parser_add_declaration(const char* name, const char* value); +static struct pakfire_parser_declaration* declarations[NUM_DECLARATIONS]; %} %token APPEND @@ -152,12 +157,16 @@ assignments : assignments assignment assignment : whitespace variable whitespace ASSIGN whitespace value whitespace NEWLINE { - printf("ASSIGNMENT FOUND: %s = %s\n", $2, $6); + int r = pakfire_parser_add_declaration($2, $6); + if (r < 0) + ABORT; }; block_assignment : whitespace DEFINE WHITESPACE variable NEWLINE text whitespace END NEWLINE { - printf("BLOCK ASSIGNMENT: %s: %s\n", $4, $6); + int r = pakfire_parser_add_declaration($4, $6); + if (r < 0) + ABORT; } %% @@ -165,6 +174,37 @@ block_assignment : whitespace DEFINE WHITESPACE variable NEWLINE text whitespa static void cleanup(void) { // Reset Pakfire pointer pakfire = NULL; + + // Free all declarations + for (unsigned int i = 0; i < NUM_DECLARATIONS; i++) { + pakfire_free(declarations[i]); + } +} + +static int pakfire_parser_add_declaration(const char* name, const char* value) { + struct pakfire_parser_declaration* d; + unsigned int i = 0; + + while (i++ < NUM_DECLARATIONS && declarations[i]) + i++; + + if (i == NUM_DECLARATIONS) { + ERROR(pakfire, "No free declarations left\n"); + return -1; + } + + // Allocate a new declaration + declarations[i] = d = pakfire_calloc(1, sizeof(*d)); + if (!d) + return -1; + + // Import name & value + d->name = pakfire_strdup(name); + d->value = pakfire_strdup(value); + + DEBUG(pakfire, "New declaration: %s = %s\n", d->name, d->value); + + return 0; } int pakfire_parser_parse_metadata(Pakfire _pakfire, const char* data, size_t len) { -- 2.39.5