From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ve0-x22e.google.com ([2607:f8b0:400c:c01::22e]) by merlin.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1UuYsE-0004Ca-8c for linux-mtd@lists.infradead.org; Thu, 04 Jul 2013 02:02:18 +0000 Received: by mail-ve0-f174.google.com with SMTP id oz10so657795veb.5 for ; Wed, 03 Jul 2013 19:01:52 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <1368030446-343-2-git-send-email-vapier@gentoo.org> References: <1368030446-343-1-git-send-email-vapier@gentoo.org> <1368030446-343-2-git-send-email-vapier@gentoo.org> Date: Wed, 3 Jul 2013 19:01:52 -0700 Message-ID: Subject: Re: [PATCH [mtd-utils] 2/3] mtd-utils: new prompt() helper for talking to the user From: Brian Norris To: Mike Frysinger Content-Type: text/plain; charset=ISO-8859-1 Cc: linux-mtd@lists.infradead.org, Artem Bityutskiy List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Hi Mike, Artem, I notice Artem just pushed this. On Wed, May 8, 2013 at 9:27 AM, Mike Frysinger 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 > --- ... > 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