]> git.ipfire.org Git - pakfire.git/commitdiff
parser: Split expanding variables into separate function
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 3 Mar 2021 14:46:54 +0000 (14:46 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 3 Mar 2021 14:46:54 +0000 (14:46 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/parser.c

index addc44ad69506721c6964bf30721cf7c708e604e..f0a952b2217c188236b7f68b3a61e93d6ad68993 100644 (file)
@@ -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;
 }