[pacman-dev] [PATCH 2/3] print yesno questions on /dev/tty if available
Attempt to print yesno questions on /dev/tty so that ideally the user can't miss them, especially when redirecting stdout/stderr to file. Signed-off-by: Simon Gomizelj <simongmzlj@gmail.com> --- src/pacman/util.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/pacman/util.c b/src/pacman/util.c index 3270c74..4532f84 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -1449,11 +1449,11 @@ static int question(short preset, char *fmt, va_list args) char response[32]; FILE *stream; int fd_in = fileno(stdin); + int rc = preset; - if(config->noconfirm) { - stream = stdout; - } else { - /* Use stderr so questions are always displayed when redirecting output */ + stream = fopen("/dev/tty", "w"); + if(stream == NULL) { + /* if we can't get at the underlying tty, fallback on stderr */ stream = stderr; } @@ -1471,7 +1471,7 @@ static int question(short preset, char *fmt, va_list args) if(config->noconfirm) { fprintf(stream, "\n"); - return preset; + goto cleanup; } fflush(stream); @@ -1480,7 +1480,7 @@ static int question(short preset, char *fmt, va_list args) if(fgets(response, sizeof(response), stdin)) { size_t len = strtrim(response); if(len == 0) { - return preset; + goto cleanup; } /* if stdin is piped, response does not get printed out, and as a result @@ -1490,12 +1490,18 @@ static int question(short preset, char *fmt, va_list args) } if(strcasecmp(response, _("Y")) == 0 || strcasecmp(response, _("YES")) == 0) { - return 1; + rc = 1; } else if(strcasecmp(response, _("N")) == 0 || strcasecmp(response, _("NO")) == 0) { - return 0; + rc = 0; } } - return 0; + +cleanup: + if(stream != stdout) { + fclose(stream); + } + + return rc; } int yesno(char *fmt, ...) -- 1.8.1.4
Since the prompt to proceed with the installation/download will now get printed to the underlying tty directly, they won't show up when stdout has been redirected. Add some messages to provide context instead. Signed-off-by: Simon Gomizelj <simongmzlj@gmail.com> --- src/pacman/sync.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/pacman/sync.c b/src/pacman/sync.c index b1cc3fe..99a7397 100644 --- a/src/pacman/sync.c +++ b/src/pacman/sync.c @@ -804,9 +804,24 @@ int sync_prepare_execute(void) } if(!confirm) { retval = 1; + if(!isatty(fileno(stdout))) { + if(config->op_s_downloadonly) { + printf(_("Download aborted.\n")); + } else { + printf(_("Installation aborted.\n")); + } + } goto cleanup; } + if(!isatty(fileno(stdout))) { + if(config->op_s_downloadonly) { + printf(_("Proceeding with download...\n")); + } else { + printf(_("Proceeding with installation...\n")); + } + } + if(alpm_trans_commit(config->handle, &data) == -1) { alpm_errno_t err = alpm_errno(config->handle); pm_printf(ALPM_LOG_ERROR, _("failed to commit transaction (%s)\n"), -- 1.8.1.4
On 02/28/13 at 02:54pm, Simon Gomizelj wrote:
Attempt to print yesno questions on /dev/tty so that ideally the user can't miss them, especially when redirecting stdout/stderr to file.
Signed-off-by: Simon Gomizelj <simongmzlj@gmail.com> --- src/pacman/util.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-)
<snip>
+cleanup: + if(stream != stdout) {
Shouldn't that be stderr?
+ fclose(stream); + } + + return rc; }
int yesno(char *fmt, ...) -- 1.8.1.4
apg
On 01/03/13 05:54, Simon Gomizelj wrote:
Attempt to print yesno questions on /dev/tty so that ideally the user can't miss them, especially when redirecting stdout/stderr to file.
Signed-off-by: Simon Gomizelj <simongmzlj@gmail.com> --- src/pacman/util.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-)
diff --git a/src/pacman/util.c b/src/pacman/util.c index 3270c74..4532f84 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -1449,11 +1449,11 @@ static int question(short preset, char *fmt, va_list args) char response[32]; FILE *stream; int fd_in = fileno(stdin); + int rc = preset;
User "ret" for the return value like everywhere else in the code base.
- if(config->noconfirm) { - stream = stdout; - } else {
I'm guessing this was stdout previously so you could --noconfirm and just have the output stderr emailed to you if non-blank. Now output gets put to stderr always. Whhat I would like to see... with --noconfirm, do not try to get the tty. Why do we need to make sure the question is seen if it is not being answered. Without --noconfirm, try getting the tty and fall back to stderr.
- /* Use stderr so questions are always displayed when redirecting output */ + stream = fopen("/dev/tty", "w"); + if(stream == NULL) { + /* if we can't get at the underlying tty, fallback on stderr */ stream = stderr; }
@@ -1471,7 +1471,7 @@ static int question(short preset, char *fmt, va_list args)
if(config->noconfirm) { fprintf(stream, "\n"); - return preset; + goto cleanup; }
fflush(stream); @@ -1480,7 +1480,7 @@ static int question(short preset, char *fmt, va_list args) if(fgets(response, sizeof(response), stdin)) { size_t len = strtrim(response); if(len == 0) { - return preset; + goto cleanup; }
/* if stdin is piped, response does not get printed out, and as a result @@ -1490,12 +1490,18 @@ static int question(short preset, char *fmt, va_list args) }
if(strcasecmp(response, _("Y")) == 0 || strcasecmp(response, _("YES")) == 0) { - return 1; + rc = 1; } else if(strcasecmp(response, _("N")) == 0 || strcasecmp(response, _("NO")) == 0) { - return 0; + rc = 0; } } - return 0; + +cleanup: + if(stream != stdout) { + fclose(stream); + } + + return rc; }
int yesno(char *fmt, ...)
participants (3)
-
Allan McRae
-
andrew.gregory.8@gmail.com
-
Simon Gomizelj