diff options
author | Chantry Xavier <shiningxc@gmail.com> | 2008-02-19 00:44:09 +0100 |
---|---|---|
committer | Dan McGee <dan@archlinux.org> | 2008-02-25 01:17:49 +0100 |
commit | 143135e666f5564240868810f0b8f4bc46ff87b5 (patch) | |
tree | d70bb426c06e9ee0dc94481ed5144aedca0bc54c /src/pacman/util.c | |
parent | ca1a187131d95604ff2c33df3c1ec80ef4af789b (diff) | |
download | pacman-143135e666f5564240868810f0b8f4bc46ff87b5.tar.gz pacman-143135e666f5564240868810f0b8f4bc46ff87b5.tar.xz |
Improve yesno function.
Add a preset paramater to yesno function saying which answer should be the
default. Ref:
http://www.archlinux.org/pipermail/pacman-dev/2007-June/008470.html
This allows us to answer no by default to some questions, like the -Scc one
mentioned in the above thread, and implemented by this patch.
Another advantage is that we don't have to repeat the [Y/n] in every
questions. It's only put once in yesno function. This highly reduces the
chances that YES and NO strings are translated, but not some questions,
which lead to obvious confusions.
Finally, the noconfirm variable only needs to be used in that yesno
function. So all other usages of it were removed.
Signed-off-by: Chantry Xavier <shiningxc@gmail.com>
Diffstat (limited to 'src/pacman/util.c')
-rw-r--r-- | src/pacman/util.c | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/src/pacman/util.c b/src/pacman/util.c index 229f65a2..2623dbdb 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -527,28 +527,44 @@ void display_targets(const alpm_list_t *syncpkgs, pmdb_t *db_local) } /* presents a prompt and gets a Y/N answer */ -/* TODO there must be a better way */ -int yesno(char *fmt, ...) +int yesno(short preset, char *fmt, ...) { char response[32]; va_list args; + FILE *stream; if(config->noconfirm) { - return(1); + stream = stdout; + } else { + /* Use stderr so questions are always displayed when redirecting output */ + stream = stderr; } va_start(args, fmt); - /* Use stderr so questions are always displayed when redirecting output */ - vfprintf(stderr, fmt, args); + vfprintf(stream, fmt, args); va_end(args); + if(preset) { + fprintf(stream, " %s ", _("[Y/n]")); + } else { + fprintf(stream, " %s ", _("[y/N]")); + } + + if(config->noconfirm) { + fprintf(stream, "\n"); + return(preset); + } + if(fgets(response, 32, stdin)) { - if(strlen(response) != 0) { - strtrim(response); + strtrim(response); + if(strlen(response) == 0) { + return(preset); } - if(!strcasecmp(response, _("Y")) || !strcasecmp(response, _("YES")) || strlen(response) == 0) { + if(!strcasecmp(response, _("Y")) || !strcasecmp(response, _("YES"))) { return(1); + } else if (!strcasecmp(response, _("N")) || !strcasecmp(response, _("NO"))) { + return(0); } } return(0); |