]> git.ipfire.org Git - pakfire.git/commitdiff
parser: Correctly skip parantheses in commands
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 20 May 2021 14:05:24 +0000 (14:05 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 20 May 2021 14:05:24 +0000 (14:05 +0000)
This regular expression handles balanced parantheses correctly so that
we will find all commands that use ().

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/parser.c

index 83e216312b4d61bf8ecb1bd4b09f135e907d1bca..164a25a1bd6e26e1f9a72e58cc373632d04b85a9 100644 (file)
@@ -85,9 +85,9 @@ static int pakfire_parser_compile_regexes(PakfireParser parser) {
        int r;
 
        // Commands
-       if (!parser->regex_command && parser->flags & PAKFIRE_PARSER_FLAGS_EXPAND_COMMANDS) {
+       if (!parser->regex_command && (parser->flags & PAKFIRE_PARSER_FLAGS_EXPAND_COMMANDS)) {
                r = pakfire_parser_compile_regex(parser,
-                       &parser->regex_command, "%\\((.*?)\\)");
+                       &parser->regex_command, "%(\\(((?>[^()]|(?1))*)\\))");
                if (r)
                        return r;
        }
@@ -394,11 +394,13 @@ static int pakfire_parser_expand_commands(PakfireParser parser, char** buffer) {
                        (PCRE2_UCHAR*)*buffer, strlen(*buffer), 0, 0, match, NULL);
 
                // End loop when we have expanded all variables
-               if (r == PCRE2_ERROR_NOMATCH)
+               if (r == PCRE2_ERROR_NOMATCH) {
+                       DEBUG(parser->pakfire, "No (more) matches found\n");
                        break;
+               }
 
                // Extract the command
-               r = pcre2_substring_get_bynumber(match, 1, &command, &command_length);
+               r = pcre2_substring_get_bynumber(match, 2, &command, &command_length);
                if (r)
                        goto ERROR;