From: Michael Tremer Date: Tue, 26 Sep 2023 18:48:11 +0000 (+0000) Subject: cli: Implement the confirm callback X-Git-Tag: 0.9.30~1629 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b82d0b7f76ea6ae712cfe2a660a7c2446d43ac3e;p=pakfire.git cli: Implement the confirm callback Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 5e884e8b8..2915e6135 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 index 000000000..049a5744d --- /dev/null +++ b/src/cli/lib/terminal.c @@ -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 . # +# # +#############################################################################*/ + +#include +#include +#include + +#include + +#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 index 000000000..b805b4105 --- /dev/null +++ b/src/cli/lib/terminal.h @@ -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 . # +# # +#############################################################################*/ + +#ifndef PAKFIRE_CLI_TERMINAL_H +#define PAKFIRE_CLI_TERMINAL_H + +#include + +int cli_term_confirm(struct pakfire* pakfire, + void* data, const char* message, const char* question); + +#endif /* PAKFIRE_CLI_TERMINAL_H */ diff --git a/src/cli/pakfire/main.c b/src/cli/pakfire/main.c index e4a67d484..8ecf53036 100644 --- a/src/cli/pakfire/main.c +++ b/src/cli/pakfire/main.c @@ -28,6 +28,8 @@ #include #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); diff --git a/src/cli/pakfire/transaction.c b/src/cli/pakfire/transaction.c index 38cffe835..414f89267 100644 --- a/src/cli/pakfire/transaction.c +++ b/src/cli/pakfire/transaction.c @@ -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; }