From: Michael Tremer Date: Tue, 28 Feb 2023 14:45:58 +0000 (+0000) Subject: ui: Don't ask any questions if the shell isn't interactive X-Git-Tag: 0.9.29~374 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6ac055a05b560e8b3addab1a19bd8cadc9708304;p=pakfire.git ui: Don't ask any questions if the shell isn't interactive This is a slight hack because we did not finish implementing a proper solution with callbacks that we would prefer. Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/ui.c b/src/libpakfire/ui.c index 61d794387..ca9223189 100644 --- a/src/libpakfire/ui.c +++ b/src/libpakfire/ui.c @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -27,6 +28,19 @@ #include #include +static int pakfire_ui_is_interactive(struct pakfire* pakfire) { + if (!isatty(STDIN_FILENO)) + return 1; + + if (!isatty(STDOUT_FILENO)) + return 1; + + if (!isatty(STDERR_FILENO)) + return 1; + + return 0; +} + static int pakfire_ui_enter_number(struct pakfire* pakfire, const char* question, unsigned int* choice, unsigned int min, unsigned int max) { char* line = NULL; @@ -38,6 +52,10 @@ static int pakfire_ui_enter_number(struct pakfire* pakfire, const char* question // Print question printf("%s ", question); + // Do not wait for any input if the terminal isn't interactive + if (!pakfire_ui_is_interactive(pakfire)) + break; + // Wait for the user to enter something ssize_t bytes_read = getline(&line, &length, stdin); if (bytes_read < 0)