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)
// 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;
}
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)
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;
}