]> git.ipfire.org Git - pakfire.git/commitdiff
parser: Capture the output of commands
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 3 Mar 2021 17:04:32 +0000 (17:04 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 3 Mar 2021 17:04:32 +0000 (17:04 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/parser.c

index 01e699e3906972459fbcdaf06dac14f76fb984b9..21ebb631beb150e4764edaef2a48dc56908bc34c 100644 (file)
@@ -382,6 +382,16 @@ static struct pakfire_parser_declaration* pakfire_parser_find_declaration(
        return d;
 }
 
+static int pakfire_parser_command_logger(Pakfire pakfire, void* data,
+               int priority, const char* line) {
+       char** output = (char**)data;
+
+       // Append output
+       asprintf(output, "%s%s", (output && *output) ? *output : "", line);
+
+       return 0;
+}
+
 static int pakfire_parser_expand_commands(PakfireParser parser, char** buffer) {
        int r = 0;
        PCRE2_UCHAR* command = NULL;
@@ -420,24 +430,37 @@ static int pakfire_parser_expand_commands(PakfireParser parser, char** buffer) {
                argv[2] = (const char*)command;
 
                // The output of the command
-               const char* output = "XXX";
+               char* output = NULL;
 
                // Execute the command inside the Pakfire environment
-               r = pakfire_execute(parser->pakfire, argv, NULL, 0, NULL, NULL);
+               r = pakfire_execute(parser->pakfire, argv, NULL, 0,
+                               pakfire_parser_command_logger, &output);
                if (r) {
                        // Just log this and continue
                        DEBUG(parser->pakfire, "Command '%s' failed with return code %d\n", command, r);
                }
 
+               // Strip newline from output
+               if (output)
+                       pakfire_remove_trailing_newline(output);
+
                // Find the entire matched pattern
                r = pcre2_substring_get_bynumber(match, 0, &pattern, &pattern_length);
-               if (r)
+               if (r) {
+                       if (output)
+                               free(output);
+
                        goto ERROR;
+               }
 
                // Replace all occurrences
                char* tmp = pakfire_string_replace(*buffer, (const char*)pattern, output);
-               if (!tmp)
+               if (!tmp) {
+                       if (output)
+                               free(output);
+
                        goto ERROR;
+               }
 
                // Replace buffer
                free(*buffer);
@@ -449,6 +472,9 @@ static int pakfire_parser_expand_commands(PakfireParser parser, char** buffer) {
 
                pcre2_substring_free(pattern);
                pattern = NULL;
+
+               if (output)
+                       free(output);
        }
 
 ERROR: