summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@gnugeneration.com>2016-12-13 07:26:39 -0800
committerVito Caputo <vcaputo@gnugeneration.com>2016-12-13 07:26:39 -0800
commit349ae02d9201dffcca98cdba270091d6dfe2d114 (patch)
tree3a70be22c67a3bf1a5f74bd98df5f7565cdbdf92 /util.c
parente195a20a3af0b2c34db115574e00894cf55bf0fa (diff)
util: introduce ask_(string,num) helpers
quick and dirty stdio dialog helpers
Diffstat (limited to 'util.c')
-rw-r--r--util.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/util.c b/util.c
index c07589e..0e5825a 100644
--- a/util.c
+++ b/util.c
@@ -1,5 +1,6 @@
#include <limits.h>
#include <stdio.h>
+#include <string.h>
#include <unistd.h>
#include "util.h"
@@ -20,3 +21,40 @@ unsigned get_ncpus(void)
return n == 0 ? 1 : n;
}
+
+
+static void query(const char *prompt, const char *def, char *buf, int len)
+{
+ buf[0] = '\0';
+
+ printf("%s [%s]: ", prompt, def);
+ fflush(stdout);
+
+ fgets(buf, len, stdin);
+ if (buf[0] == '\0' || buf[0] == '\n') {
+ snprintf(buf, len, "%s", def);
+ } else if(strchr(buf, '\n')) {
+ *strchr(buf, '\n') = '\0';
+ }
+}
+
+
+void ask_string(char *buf, int len, const char *prompt, const char *def)
+{
+ query(prompt, def, buf, len);
+}
+
+
+void ask_num(int *res, int max, const char *prompt, int def)
+{
+ char buf[21], buf2[256];
+ int num;
+
+ snprintf(buf, sizeof(buf), "%i", def);
+ do {
+ query(prompt, buf, buf2, sizeof(buf2));
+ num = atoi(buf2); /* TODO: errors (strtol)*/
+ } while (num > max);
+
+ *res = num;
+}
© All Rights Reserved