aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorMike Frysinger <vapier@gentoo.org>2013-05-08 12:27:25 -0400
committerArtem Bityutskiy <artem.bityutskiy@linux.intel.com>2013-07-01 08:55:55 +0300
commitdbe0fd17f2323f108715db0bd0f734e9563e40d8 (patch)
tree0e2342e773045ee2e7f937c1eec5250dbc0810f1 /include
parent8b4786830174e06bc27810f15c76f72cb3e951d9 (diff)
mtd-utils: new prompt() helper for talking to the user
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@gentoo.org> Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
Diffstat (limited to 'include')
-rw-r--r--include/common.h39
1 files changed, 38 insertions, 1 deletions
diff --git a/include/common.h b/include/common.h
index d0c4146..4ffccea 100644
--- a/include/common.h
+++ b/include/common.h
@@ -19,6 +19,7 @@
#ifndef __MTD_UTILS_COMMON_H__
#define __MTD_UTILS_COMMON_H__
+#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.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)) {
+ case 0: ret = false; break;
+ case 1: ret = true; break;
+ case -1:
+ puts("unknown response; please try again");
+ continue;
+ }
+ }
+ break;
+ } while (1);
+
+ free(line);
+
+ return ret;
+}
+
static inline int is_power_of_2(unsigned long long n)
{
- return (n != 0 && ((n & (n - 1)) == 0));
+ return (n != 0 && ((n & (n - 1)) == 0));
}
/**