[pacman-dev] [PATCH 2/3] print yesno questions on /dev/tty if available

Allan McRae allan at archlinux.org
Sun Mar 3 16:35:53 EST 2013


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 at 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, ...)
> 



More information about the pacman-dev mailing list