[PATCH [mtd-utils] 2/3] mtd-utils: new prompt() helper for talking to the user
Brian Norris
computersforpeace at gmail.com
Wed Jul 3 22:01:52 EDT 2013
Hi Mike, Artem,
I notice Artem just pushed this.
On Wed, May 8, 2013 at 9:27 AM, Mike Frysinger <vapier at gentoo.org> wrote:
> We've got a few tools that prompt the user for "yes/no" questions.
> Add a common helper to simplify the various implementations.
>
> Signed-off-by: Mike Frysinger <vapier at gentoo.org>
> ---
...
> diff --git a/include/common.h b/include/common.h
> index d0c4146..4ffccea 100644
> --- a/include/common.h
> +++ b/include/common.h
> @@ -101,9 +102,45 @@ extern "C" {
> fprintf(stderr, "%s: warning!: " fmt "\n", PROGRAM_NAME, ##__VA_ARGS__); \
> } while(0)
>
> +/**
> + * prompt the user for confirmation
> + */
> +static inline bool prompt(const char *msg, bool def)
> +{
> + char *line = NULL;
> + size_t len;
> + bool ret = def;
> +
> + do {
> + normsg_cont("%s (%c/%c) ", msg, def ? 'Y' : 'y', def ? 'n' : 'N');
> + fflush(stdout);
> +
> + while (getline(&line, &len, stdin) == -1) {
> + printf("failed to read prompt; assuming '%s'\n",
> + def ? "yes" : "no");
> + break;
> + }
> +
> + if (strcmp("\n", line) != 0) {
> + switch (rpmatch(line)) {
rpmatch() is not POSIX-compliant and is not available on my uclibc, so
this breaks my compile.
> + case 0: ret = false; break;
> + case 1: ret = true; break;
> + case -1:
> + puts("unknown response; please try again");
> + continue;
> + }
...
Brian
More information about the linux-mtd
mailing list