From: Michael Tremer Date: Fri, 25 Oct 2024 13:45:59 +0000 (+0000) Subject: parser: Remove the legacy logger X-Git-Tag: 0.9.30~890 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e45fdc28732d736c0d5e680192aab9a9d0ea0be6;p=pakfire.git parser: Remove the legacy logger Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/parser.c b/src/libpakfire/parser.c index dbe7702f9..9f2d79928 100644 --- a/src/libpakfire/parser.c +++ b/src/libpakfire/parser.c @@ -30,9 +30,6 @@ #define PCRE2_CODE_UNIT_WIDTH 8 #include -// Enable legacy logging -#define PAKFIRE_LEGACY_LOGGING - #include #include #include @@ -185,9 +182,9 @@ static struct pakfire_parser_declaration* pakfire_parser_get_declaration( #ifdef PAKFIRE_DEBUG_PARSER if (*namespace) - DEBUG(parser->pakfire, "%p: Looking up %s.%s\n", parser, namespace, name); + CTX_DEBUG(parser->ctx, "%p: Looking up %s.%s\n", parser, namespace, name); else - DEBUG(parser->pakfire, "%p: Looking up %s\n", parser, name); + CTX_DEBUG(parser->ctx, "%p: Looking up %s\n", parser, name); #endif /* PAKFIRE_DEBUG_PARSER */ struct pakfire_parser_declaration* d; @@ -199,7 +196,7 @@ static struct pakfire_parser_declaration* pakfire_parser_get_declaration( // Return if namespace and name match if ((strcmp(d->namespace, namespace) == 0) && (strcmp(d->name, name) == 0)) { #ifdef PAKFIRE_DEBUG_PARSER - DEBUG(parser->pakfire, "%p: Found result = %s\n", parser, d->value); + CTX_DEBUG(parser->ctx, "%p: Found result = %s\n", parser, d->value); #endif /* PAKFIRE_DEBUG_PARSER */ return d; @@ -207,7 +204,7 @@ static struct pakfire_parser_declaration* pakfire_parser_get_declaration( } #ifdef PAKFIRE_DEBUG_PARSER - DEBUG(parser->pakfire, "%p: Nothing found\n", parser); + CTX_DEBUG(parser->ctx, "%p: Nothing found\n", parser); #endif /* PAKFIRE_DEBUG_PARSER */ return NULL; @@ -311,7 +308,7 @@ int pakfire_parser_set(struct pakfire_parser* parser, if (flags) d->flags = flags; - DEBUG(parser->pakfire, "%p: Updated declaration: %s.%s = %s\n", + CTX_DEBUG(parser->ctx, "%p: Updated declaration: %s.%s = %s\n", parser, d->namespace, d->name, d->value); // All done @@ -334,7 +331,7 @@ int pakfire_parser_set(struct pakfire_parser* parser, // Import flags d->flags = flags; - DEBUG(parser->pakfire, "%p: New declaration (%u): %s.%s %s= %s\n", + CTX_DEBUG(parser->ctx, "%p: New declaration (%u): %s.%s %s= %s\n", parser, d->flags, d->namespace, @@ -358,7 +355,7 @@ int pakfire_parser_apply_declaration(struct pakfire_parser* parser, static int pakfire_parser_find_template(struct pakfire_parser* parser, char* template, const size_t length, const char* namespace) { - DEBUG(parser->pakfire, "Looking up template in namespace '%s'\n", namespace); + CTX_DEBUG(parser->ctx, "Looking up template in namespace '%s'\n", namespace); struct pakfire_parser_declaration* d = pakfire_parser_get_declaration( parser, namespace, "template"); @@ -413,7 +410,7 @@ int pakfire_parser_append(struct pakfire_parser* parser, // Fetch the value of the current declaration const char* old_value = pakfire_parser_get_raw(parser, namespace, name); - DEBUG(parser->pakfire, "%p: Old value for %s.%s = %s\n", + CTX_DEBUG(parser->ctx, "%p: Old value for %s.%s = %s\n", parser, namespace, name, old_value); // Set the new value when there is no old one @@ -439,7 +436,7 @@ static int pakfire_parser_expand_commands(struct pakfire_parser* parser, char** PCRE2_UCHAR* pattern = NULL; PCRE2_SIZE pattern_length; - DEBUG(parser->pakfire, "Searching for commands in:\n%s\n", *buffer); + CTX_DEBUG(parser->ctx, "Searching for commands in:\n%s\n", *buffer); // Allocate memory for results pcre2_match_data* match = pcre2_match_data_create_from_pattern( @@ -457,7 +454,7 @@ static int pakfire_parser_expand_commands(struct pakfire_parser* parser, char** // End loop when we have expanded all variables if (r == PCRE2_ERROR_NOMATCH) { - DEBUG(parser->pakfire, "No (more) matches found\n"); + CTX_DEBUG(parser->ctx, "No (more) matches found\n"); r = 0; break; } @@ -468,7 +465,7 @@ static int pakfire_parser_expand_commands(struct pakfire_parser* parser, char** goto ERROR; #ifdef PAKFIRE_DEBUG_PARSER - DEBUG(parser->pakfire, "Expanding command: %s\n", command); + CTX_DEBUG(parser->ctx, "Expanding command: %s\n", command); #endif /* PAKFIRE_DEBUG_PARSER */ // Update argv @@ -482,7 +479,7 @@ static int pakfire_parser_expand_commands(struct pakfire_parser* parser, char** r = pakfire_jail_run(parser->pakfire, argv, 0, &output, &length); if (r) { // Just log this and continue - DEBUG(parser->pakfire, "Command '%s' failed with return code %d\n", command, r); + CTX_DEBUG(parser->ctx, "Command '%s' failed with return code %d\n", command, r); } // Strip newline from output @@ -561,7 +558,7 @@ static int pakfire_parser_expand_variables(struct pakfire_parser* parser, // End loop when we have expanded all variables if (r == PCRE2_ERROR_NOMATCH) { - DEBUG(parser->pakfire, "No (more) matches found in: %s\n", *buffer); + CTX_DEBUG(parser->ctx, "No (more) matches found in: %s\n", *buffer); r = 0; break; } @@ -577,7 +574,7 @@ static int pakfire_parser_expand_variables(struct pakfire_parser* parser, goto ERROR; #ifdef PAKFIRE_DEBUG_PARSER - DEBUG(parser->pakfire, "Expanding variable: %s\n", variable); + CTX_DEBUG(parser->ctx, "Expanding variable: %s\n", variable); #endif /* PAKFIRE_DEBUG_PARSER */ // Search for a declaration of this variable @@ -585,7 +582,7 @@ static int pakfire_parser_expand_variables(struct pakfire_parser* parser, // Is this a recursive pattern? if (repl && pakfire_string_matches(repl, (const char*)pattern)) { - DEBUG(parser->pakfire, "Recursion detected in %s\n", pattern); + CTX_DEBUG(parser->ctx, "Recursion detected in %s\n", pattern); // Move up one step and lookup there if (namespace && *namespace) { @@ -603,9 +600,9 @@ static int pakfire_parser_expand_variables(struct pakfire_parser* parser, #ifdef PAKFIRE_DEBUG_PARSER // What is its value? if (repl) { - DEBUG(parser->pakfire, "Replacing %%{%s} with '%s'\n", variable, repl); + CTX_DEBUG(parser->ctx, "Replacing %%{%s} with '%s'\n", variable, repl); } else { - DEBUG(parser->pakfire, "Replacing %%{%s} with an empty string\n", variable); + CTX_DEBUG(parser->ctx, "Replacing %%{%s} with an empty string\n", variable); } #endif /* PAKFIRE_DEBUG_PARSER */ @@ -656,14 +653,14 @@ char* pakfire_parser_expand(struct pakfire_parser* parser, // Compile all regular expressions int r = pakfire_parser_compile_regexes(parser); if (r) { - DEBUG(parser->pakfire, "Could not compile regular expressions: %m\n"); + CTX_DEBUG(parser->ctx, "Could not compile regular expressions: %m\n"); goto ERROR; } // Expand all variables r = pakfire_parser_expand_variables(parser, namespace, &buffer); if (r) { - DEBUG(parser->pakfire, "Failed to expand variables in '%s': %m\n", value); + CTX_DEBUG(parser->ctx, "Failed to expand variables in '%s': %m\n", value); goto ERROR; } @@ -671,7 +668,7 @@ char* pakfire_parser_expand(struct pakfire_parser* parser, if (parser->flags & PAKFIRE_PARSER_FLAGS_EXPAND_COMMANDS) { r = pakfire_parser_expand_commands(parser, &buffer); if (r) { - DEBUG(parser->pakfire, "Failed to expand commands in '%s': %m\n", value); + CTX_DEBUG(parser->ctx, "Failed to expand commands in '%s': %m\n", value); goto ERROR; } } @@ -776,7 +773,7 @@ char** pakfire_parser_list_namespaces(struct pakfire_parser* parser, // Check for any errors else if (r > 0) { - ERROR(parser->pakfire, "fnmatch failed: %m\n"); + CTX_ERROR(parser->ctx, "fnmatch failed: %m\n"); if (namespaces) free(namespaces); @@ -818,7 +815,7 @@ int pakfire_parser_merge(struct pakfire_parser* parser1, struct pakfire_parser* char* value = NULL; int r; - DEBUG(parser1->pakfire, "Merging parsers %p and %p\n", parser1, parser2); + CTX_DEBUG(parser1->ctx, "Merging parsers %p and %p\n", parser1, parser2); if (!parser2) { errno = EINVAL; @@ -898,7 +895,7 @@ int pakfire_parser_read(struct pakfire_parser* parser, FILE* f, int pakfire_parser_read_file(struct pakfire_parser* parser, const char* path, struct pakfire_parser_error** error) { - DEBUG(parser->pakfire, "Parsing %s...\n", path); + CTX_DEBUG(parser->ctx, "Parsing %s...\n", path); FILE* f = fopen(path, "r"); if (!f) @@ -951,7 +948,7 @@ int pakfire_parser_set_namespace(struct pakfire_parser* parser, const char* name else parser->namespace = NULL; - DEBUG(parser->pakfire, "%p: Set namespace to: %s\n", parser, parser->namespace); + CTX_DEBUG(parser->ctx, "%p: Set namespace to: %s\n", parser, parser->namespace); return 0; } @@ -1011,19 +1008,19 @@ int pakfire_parser_create_package(struct pakfire_parser* parser, char* deps = NULL; char* build_arches = NULL; - DEBUG(parser->pakfire, "Building package from namespace '%s'\n", namespace); + CTX_DEBUG(parser->ctx, "Building package from namespace '%s'\n", namespace); // Fetch name name = pakfire_parser_get(parser, namespace, "name"); if (!name || !*name) { - ERROR(parser->pakfire, "Name is empty\n"); + CTX_ERROR(parser->ctx, "Name is empty\n"); goto CLEANUP; } // Fetch EVR evr = pakfire_parser_get(parser, namespace, "evr"); if (!evr || !*evr) { - ERROR(parser->pakfire, "EVR is empty\n"); + CTX_ERROR(parser->ctx, "EVR is empty\n"); goto CLEANUP; } @@ -1035,7 +1032,7 @@ int pakfire_parser_create_package(struct pakfire_parser* parser, if (!arch) goto CLEANUP; } else { - ERROR(parser->pakfire, "Arch is empty\n"); + CTX_ERROR(parser->ctx, "Arch is empty\n"); goto CLEANUP; } } @@ -1043,7 +1040,7 @@ int pakfire_parser_create_package(struct pakfire_parser* parser, // Create a new package object r = pakfire_package_create(pkg, parser->pakfire, repo, name, evr, arch); if (r) { - ERROR(parser->pakfire, "Could not create package: %m\n"); + CTX_ERROR(parser->ctx, "Could not create package: %m\n"); goto CLEANUP; } @@ -1053,7 +1050,7 @@ int pakfire_parser_create_package(struct pakfire_parser* parser, // Assign a new UUID to this package char* uuid = pakfire_generate_uuid(); if (!uuid) { - ERROR(parser->pakfire, "Generating a UUID failed: %m\n"); + CTX_ERROR(parser->ctx, "Generating a UUID failed: %m\n"); goto CLEANUP; } @@ -1089,7 +1086,7 @@ int pakfire_parser_create_package(struct pakfire_parser* parser, free(value); if (r) { - ERROR(parser->pakfire, "Could not set %s: %m\n", a->name); + CTX_ERROR(parser->ctx, "Could not set %s: %m\n", a->name); goto CLEANUP; } } @@ -1128,7 +1125,7 @@ int pakfire_parser_create_package(struct pakfire_parser* parser, CLEANUP: if (r) - ERROR(parser->pakfire, "Could not create package: %m\n"); + CTX_ERROR(parser->ctx, "Could not create package: %m\n"); if (name) free(name); @@ -1188,14 +1185,12 @@ struct pakfire_parser_error* pakfire_parser_error_ref( } static void pakfire_parser_error_free(struct pakfire_parser_error* error) { - pakfire_parser_unref(error->parser); - + if (error->parser) + pakfire_parser_unref(error->parser); if (error->filename) free(error->filename); - if (error->message) free(error->message); - free(error); } @@ -1205,7 +1200,6 @@ struct pakfire_parser_error* pakfire_parser_error_unref( return error; pakfire_parser_error_free(error); - return NULL; } diff --git a/src/libpakfire/parser/grammar.y b/src/libpakfire/parser/grammar.y index a5a490f0b..2826f3a58 100644 --- a/src/libpakfire/parser/grammar.y +++ b/src/libpakfire/parser/grammar.y @@ -22,6 +22,7 @@ %parse-param {yyscan_t* scanner} + {struct pakfire_ctx* ctx} {struct pakfire* pakfire} {struct pakfire_parser** parser} {struct pakfire_parser* parent} @@ -40,9 +41,6 @@ #include #include -// Enable legacy logging -#define PAKFIRE_LEGACY_LOGGING - #include #include #include @@ -79,22 +77,23 @@ enum operator { OP_EQUALS = 0, }; -static void yyerror(yyscan_t* scanner, struct pakfire* pakfire, struct pakfire_parser** parser, - struct pakfire_parser* parent, struct pakfire_parser_error** error, const char* s) { +static void yyerror(yyscan_t* scanner, struct pakfire_ctx* ctx, struct pakfire* pakfire, + struct pakfire_parser** parser, struct pakfire_parser* parent, + struct pakfire_parser_error** error, const char* s) { const struct pakfire_parser_state* state = yyget_extra(scanner); - ERROR(pakfire, "Error (line %u): %s\n", state->lineno, s); + CTX_ERROR(ctx, "Error (line %u): %s\n", state->lineno, s); // Create a new error object if (error) { int r = pakfire_parser_error_create(error, *parser, NULL, state->lineno, s); if (r) { - ERROR(pakfire, "Could not create error object: %s\n", strerror(errno)); + CTX_ERROR(ctx, "Could not create error object: %s\n", strerror(errno)); } } } -static struct pakfire_parser* make_if_stmt(struct pakfire* pakfire, const enum operator op, +static struct pakfire_parser* make_if_stmt(struct pakfire_ctx* ctx, const enum operator op, const char* val1, const char* val2, struct pakfire_parser* if_block, struct pakfire_parser* else_block); static int pakfire_parser_new_declaration( @@ -386,7 +385,7 @@ subparser_name : T_SUBPARSER if_stmt : T_IF T_STRING T_EQUALS T_STRING T_EOL block else_stmt T_END T_EOL { - $$ = make_if_stmt(pakfire, OP_EQUALS, $2, $4, $6, $7); + $$ = make_if_stmt(ctx, OP_EQUALS, $2, $4, $6, $7); if ($6) pakfire_parser_unref($6); @@ -421,9 +420,11 @@ int pakfire_parser_parse_data(struct pakfire_parser* parent, const char* data, s .readline_indent = 0, }; + // Fetch context + struct pakfire_ctx* ctx = pakfire_ctx(pakfire); + #ifdef ENABLE_DEBUG - DEBUG(pakfire, "Parsing the following data (%zu):\n%.*s\n", - len, (int)len, data); + CTX_DEBUG(ctx, "Parsing the following data (%zu):\n%.*s\n", len, (int)len, data); // Save start time clock_t t_start = clock(); @@ -436,7 +437,7 @@ int pakfire_parser_parse_data(struct pakfire_parser* parent, const char* data, s struct pakfire_parser* parser = NULL; YY_BUFFER_STATE buffer = yy_scan_bytes(data, len, scanner); - int r = yyparse(scanner, pakfire, &parser, parent, error); + int r = yyparse(scanner, ctx, pakfire, &parser, parent, error); yy_delete_buffer(buffer, scanner); // If everything was parsed successfully, we merge the sub-parser into @@ -458,33 +459,36 @@ int pakfire_parser_parse_data(struct pakfire_parser* parent, const char* data, s // Log what we have in the parent parser now dump = pakfire_parser_dump(parent); if (dump) - DEBUG(pakfire, "Status of the parser %p:\n%s\n", parent, dump); + CTX_DEBUG(ctx, "Status of the parser %p:\n%s\n", parent, dump); // Log time we needed to parse data - DEBUG(pakfire, "Parser finished in %.4fms\n", + CTX_DEBUG(ctx, "Parser finished in %.4fms\n", (double)(t_end - t_start) * 1000 / CLOCKS_PER_SEC); #endif // Cleanup if (dump) free(dump); - pakfire_unref(pakfire); + if (pakfire) + pakfire_unref(pakfire); + if (ctx) + pakfire_ctx_unref(ctx); yylex_destroy(scanner); return r; } -static struct pakfire_parser* make_if_stmt(struct pakfire* pakfire, const enum operator op, +static struct pakfire_parser* make_if_stmt(struct pakfire_ctx* ctx, const enum operator op, const char* val1, const char* val2, struct pakfire_parser* if_block, struct pakfire_parser* else_block) { switch (op) { case OP_EQUALS: - DEBUG(pakfire, "Evaluating if statement: %s == %s?\n", val1, val2); + CTX_DEBUG(ctx, "Evaluating if statement: %s == %s?\n", val1, val2); break; } struct pakfire_parser* parent = pakfire_parser_get_parent(if_block); - DEBUG(pakfire, " parent = %p, if = %p, else = %p\n", parent, if_block, else_block); + CTX_DEBUG(ctx, " parent = %p, if = %p, else = %p\n", parent, if_block, else_block); // Expand values char* v1 = pakfire_parser_expand(parent, NULL, val1); @@ -494,7 +498,7 @@ static struct pakfire_parser* make_if_stmt(struct pakfire* pakfire, const enum o switch (op) { case OP_EQUALS: - DEBUG(pakfire, " '%s' == '%s'?\n", v1, v2); + CTX_DEBUG(ctx, " '%s' == '%s'?\n", v1, v2); if (strcmp(v1, v2) == 0) result = if_block;