From: Michael Tremer Date: Sat, 1 Jun 2019 14:22:15 +0000 (+0100) Subject: libpakfire: parser: Fix behaviour of append X-Git-Tag: 0.9.28~1285^2~985 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f16c81ca3a9ed65f598d98d2886ca30d7f388c5b;p=pakfire.git libpakfire: parser: Fix behaviour of append We updated the old declaration but that could have been in a parent parser. Therefore we get the value (where ever it is from) and update it. Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/parser.c b/src/libpakfire/parser.c index 617d49e64..970a977f7 100644 --- a/src/libpakfire/parser.c +++ b/src/libpakfire/parser.c @@ -224,31 +224,41 @@ PAKFIRE_EXPORT int pakfire_parser_set(PakfireParser parser, const char* name, co return r; } -PAKFIRE_EXPORT int pakfire_parser_append(PakfireParser parser, - const char* name, const char* value) { +static const char* pakfire_parser_get_raw(PakfireParser parser, const char* name) { struct pakfire_parser_declaration* d = pakfire_parser_get_declaration(parser, name); - // Add the declaration if we could not find it - if (!d) - return pakfire_parser_set_declaration(parser, name, value); + // Return a match + if (d) + return d->value; + // Search in parent parser if available + if (parser->parent) + return pakfire_parser_get_raw(parser->parent, name); + + return NULL; +} + +PAKFIRE_EXPORT int pakfire_parser_append(PakfireParser parser, + const char* name, const char* value) { char* buffer = NULL; + // Fetch the value of the current declaration + const char* old_value = pakfire_parser_get_raw(parser, name); + + // Set the new value when there is no old one + if (!old_value) + return pakfire_parser_set_declaration(parser, name, value); + // Concat value - int r = asprintf(&buffer, "%s %s", d->value, value); + int r = asprintf(&buffer, "%s %s", old_value, value); if (r < 0) return r; - DEBUG(parser->pakfire, "Appended declaration: %s = %s (was: %s)\n", - d->name, buffer, d->value); - - // Replace value in declaration - if (d->value) - pakfire_free(d->value); - - d->value = buffer; + // Set the new value + r = pakfire_parser_set_declaration(parser, name, buffer); + pakfire_free(buffer); - return 0; + return r; } static void pakfire_parser_strip_namespace(char* s) { @@ -401,19 +411,6 @@ PAKFIRE_EXPORT char* pakfire_parser_expand(PakfireParser parser, const char* val return buffer; } -static const char* pakfire_parser_get_raw(PakfireParser parser, const char* name) { - struct pakfire_parser_declaration* d = pakfire_parser_get_declaration(parser, name); - - if (d) - return d->value; - - // Search in parent parser if available - if (parser->parent) - return pakfire_parser_get_raw(parser->parent, name); - - return NULL; -} - PAKFIRE_EXPORT char* pakfire_parser_get(PakfireParser parser, const char* name) { const char* value = pakfire_parser_get_raw(parser, name);