]> git.ipfire.org Git - people/stevee/pakfire.git/commitdiff
ui: Don't ask any questions if the shell isn't interactive
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 28 Feb 2023 14:45:58 +0000 (14:45 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 28 Feb 2023 14:45:58 +0000 (14:45 +0000)
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 <michael.tremer@ipfire.org>
src/libpakfire/ui.c

index 61d794387ade3b245f99e92ec2ceb83227f69df5..ca9223189cf10da449f615fc11f9846a52cab6f8 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <errno.h>
 #include <stdlib.h>
+#include <unistd.h>
 
 #include <pakfire/i18n.h>
 #include <pakfire/pakfire.h>
 #include <pakfire/request.h>
 #include <pakfire/ui.h>
 
+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)