From: Michael Tremer Date: Wed, 3 Mar 2021 14:46:54 +0000 (+0000) Subject: parser: Split expanding variables into separate function X-Git-Tag: 0.9.28~1285^2~652 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6accaf4332b4270ed49e6389f939ecfd9851681b;p=pakfire.git parser: Split expanding variables into separate function Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/parser.c b/src/libpakfire/parser.c index addc44ad6..f0a952b22 100644 --- a/src/libpakfire/parser.c +++ b/src/libpakfire/parser.c @@ -349,25 +349,13 @@ static struct pakfire_parser_declaration* pakfire_parser_find_declaration( return d; } -PAKFIRE_EXPORT char* pakfire_parser_expand(PakfireParser parser, - const char* namespace, const char* value) { - // Return NULL when the value is NULL - if (!value) - return NULL; - +static int pakfire_parser_expand_variables(PakfireParser parser, + const char* namespace, char** buffer) { PCRE2_UCHAR* variable = NULL; PCRE2_SIZE variable_length; PCRE2_UCHAR* pattern = NULL; PCRE2_SIZE pattern_length; - // Create a working copy of the string we are expanding - char* buffer = strdup(value); - - // Fast path to check if there are any variables in here whatsoever - char* pos = strchr(value, '%'); - if (!pos) - return buffer; - // Compile all regular expressions int r = pakfire_parser_compile_regex(parser); if (r) @@ -379,11 +367,12 @@ PAKFIRE_EXPORT char* pakfire_parser_expand(PakfireParser parser, // Search for any variables while (1) { // Perform matching - int r = pcre2_match(parser->regex_variable, (PCRE2_UCHAR*)buffer, strlen(buffer), 0, 0, match, NULL); + int r = pcre2_match(parser->regex_variable, (PCRE2_UCHAR*)*buffer, strlen(*buffer), + 0, 0, match, NULL); // End loop when we have expanded all variables if (r == PCRE2_ERROR_NOMATCH) { - DEBUG(parser->pakfire, "No (more) matches found in: %s\n", buffer); + DEBUG(parser->pakfire, "No (more) matches found in: %s\n", *buffer); break; } @@ -417,31 +406,23 @@ PAKFIRE_EXPORT char* pakfire_parser_expand(PakfireParser parser, goto ERROR; // Replace all occurrences - char* tmp = pakfire_string_replace(buffer, (const char*)pattern, repl); + char* tmp = pakfire_string_replace(*buffer, (const char*)pattern, repl); if (!tmp) goto ERROR; // Replace buffer - free(buffer); - buffer = tmp; + free(*buffer); + *buffer = tmp; // Free resources pcre2_substring_free(variable); - pcre2_substring_free(pattern); - variable = pattern = NULL; + variable = NULL; - DEBUG(parser->pakfire, "New buffer: %s\n", buffer); + pcre2_substring_free(pattern); + pattern = NULL; } - goto OUT; - ERROR: - if (buffer) { - free(buffer); - buffer = NULL; - } - -OUT: pcre2_match_data_free(match); if (variable) @@ -449,6 +430,32 @@ OUT: if (pattern) pcre2_substring_free(pattern); + return r; +} + +PAKFIRE_EXPORT char* pakfire_parser_expand(PakfireParser parser, + const char* namespace, const char* value) { + // Return NULL when the value is NULL + if (!value) + return NULL; + + // Create a working copy of the string we are expanding + char* buffer = strdup(value); + + // Fast path to check if there are any variables in here whatsoever + char* pos = strchr(value, '%'); + if (!pos) + return buffer; + + // Expand all variables + int r = pakfire_parser_expand_variables(parser, namespace, &buffer); + if (r) { + if (buffer) + free(buffer); + + return NULL; + } + return buffer; }