]> git.ipfire.org Git - pakfire.git/commitdiff
cli: Implement the confirm callback
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 26 Sep 2023 18:48:11 +0000 (18:48 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 26 Sep 2023 18:48:11 +0000 (18:48 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/cli/lib/terminal.c [new file with mode: 0644]
src/cli/lib/terminal.h [new file with mode: 0644]
src/cli/pakfire/main.c
src/cli/pakfire/transaction.c

index 5e884e8b893f7f0fffa62c20ba20894d4d76aa36..2915e6135be94ccbff338f41d4ca9808c691463e 100644 (file)
@@ -436,7 +436,9 @@ libcli_la_SOURCES = \
        src/cli/lib/command.c \
        src/cli/lib/command.h \
        src/cli/lib/dump.c \
-       src/cli/lib/dump.h
+       src/cli/lib/dump.h \
+       src/cli/lib/terminal.c \
+       src/cli/lib/terminal.h
 
 libcli_la_CPPFLAGS = \
        $(AM_CPPFLAGS) \
diff --git a/src/cli/lib/terminal.c b/src/cli/lib/terminal.c
new file mode 100644 (file)
index 0000000..049a574
--- /dev/null
@@ -0,0 +1,86 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2023 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <pakfire/pakfire.h>
+
+#include "terminal.h"
+
+static int cli_term_is_interactive(void) {
+       return isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) && isatty(STDERR_FILENO);
+}
+
+int cli_term_confirm(struct pakfire* pakfire,
+               void* data, const char* message, const char* question) {
+       char* line = NULL;
+       size_t length = 0;
+       int r = 1;
+
+       // Show the message if any
+       if (message)
+               printf("%s\n", message);
+
+       for (;;) {
+               // Print question
+               printf("%s ", question);
+
+               // Do not wait for any input if the terminal isn't interactive
+               if (!cli_term_is_interactive())
+                       break;
+
+               // Wait for the user to enter something
+               ssize_t bytes_read = getline(&line, &length, stdin);
+               if (bytes_read < 0) {
+                       r = bytes_read;
+                       goto END;
+
+               // If we have read more than one character + newline, we will ask again
+               } else if (bytes_read > 2)
+                       continue;
+
+               // Check the response
+               switch (*line) {
+                       // Yes!
+                       case 'Y':
+                       case 'y':
+                               r = 0;
+                               goto END;
+
+                       // No!
+                       case 'N':
+                       case 'n':
+                               r = 1;
+                               goto END;
+
+                       // Ask again for any other inputs
+                       default:
+                               continue;
+               }
+       }
+
+END:
+       if (line)
+               free(line);
+
+       return r;
+}
diff --git a/src/cli/lib/terminal.h b/src/cli/lib/terminal.h
new file mode 100644 (file)
index 0000000..b805b41
--- /dev/null
@@ -0,0 +1,29 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2023 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef PAKFIRE_CLI_TERMINAL_H
+#define PAKFIRE_CLI_TERMINAL_H
+
+#include <pakfire/pakfire.h>
+
+int cli_term_confirm(struct pakfire* pakfire,
+       void* data, const char* message, const char* question);
+
+#endif /* PAKFIRE_CLI_TERMINAL_H */
index e4a67d48455389724f30c7abffe9ba27a7c595d1..8ecf5303603c9cc18e817417ac6ba3065a6c5d6c 100644 (file)
@@ -28,6 +28,8 @@
 #include <pakfire/pakfire.h>
 
 #include "command.h"
+#include "terminal.h"
+
 #include "clean.h"
 #include "info.h"
 #include "install.h"
@@ -249,6 +251,9 @@ int main(int argc, char* argv[]) {
        if (r)
                goto ERROR;
 
+       // Configure callbacks
+       pakfire_set_confirm_callback(pakfire, cli_term_confirm, NULL);
+
        // Enable repositories
        for (unsigned int i = 0; i < config.num_enable_repos; i++)
                cli_set_repo_enabled(pakfire, config.enable_repos[i], 1);
index 38cffe835c6dea35315f6f49f1b6bf4851de53d5..414f89267e305bcaea97a92030ca817bd2cc6da1 100644 (file)
@@ -36,7 +36,6 @@ int cli_transaction(struct pakfire* pakfire, int argc, char* argv[], int flags,
                cli_transaction_callback callback, void* data) {
        struct pakfire_transaction* transaction = NULL;
        char* problems = NULL;
-       char* dump = NULL;
        int r;
 
        // Create a new transaction
@@ -58,11 +57,6 @@ int cli_transaction(struct pakfire* pakfire, int argc, char* argv[], int flags,
                goto ERROR;
        }
 
-       // Dump the transaction
-       dump = pakfire_transaction_dump(transaction, 0);
-       if (dump)
-               printf("%s\n", dump);
-
        // Run the transaction
        r = pakfire_transaction_run(transaction);
        if (r)
@@ -73,8 +67,6 @@ ERROR:
                pakfire_transaction_unref(transaction);
        if (problems)
                free(problems);
-       if (dump)
-               free(dump);
 
        return r;
 }