From: Michael Tremer Date: Wed, 3 Mar 2021 17:04:32 +0000 (+0000) Subject: parser: Capture the output of commands X-Git-Tag: 0.9.28~1285^2~647 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0a1b68b3e35ed31af1bdec521e1aec937a82b9a5;p=pakfire.git parser: Capture the output of commands Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/parser.c b/src/libpakfire/parser.c index 01e699e39..21ebb631b 100644 --- a/src/libpakfire/parser.c +++ b/src/libpakfire/parser.c @@ -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: