aboutsummaryrefslogtreecommitdiff
path: root/ask.c
diff options
context:
space:
mode:
Diffstat (limited to 'ask.c')
-rw-r--r--ask.c36
1 files changed, 21 insertions, 15 deletions
diff --git a/ask.c b/ask.c
index 87b4ac9..b3cad99 100644
--- a/ask.c
+++ b/ask.c
@@ -2,11 +2,12 @@
* Copyright 2019 Bruce Hill
* Released under the MIT License (see LICENSE for details)
* Usage: ask [-Q|--quickpick] [-y|--yes] [-n|--no] [-P|--password] [[-H |--history=]name]
- * [-v|--version] [-h|--help] [-q |--query=initial] [[-p |--prompt=]prompt [options...]]
+ * [-v|--version] [-h|--help] [-0|--read0] [-q |--query=initial] [[-p |--prompt=]prompt [options...]]
* --password: password mode
* --quickpick: quickpick mode (exit when only one option remains)
* --version: print version and exit
* --help: print usage and exit
+ * --read0: read input delimited by NULL bytes instead of newlines
* --query: initial value to pre-populate user input
* --prompt: use the given prompt (displayed in bold)
* --yes: append " [Y/n]" to the prompt, use quickpick mode,
@@ -28,7 +29,7 @@
#include "bterm.h"
-#define ASK_VERSION "0.3"
+#define ASK_VERSION "0.4"
#define LOWERCASE(c) ('A' <= (c) && (c) <= 'Z' ? ((c) + 'a' - 'A') : (c))
#define EQ(a, b) (case_sensitive ? (a) == (b) : LOWERCASE(a) == LOWERCASE(b))
#define PASSWORD "-\\|/"
@@ -373,20 +374,8 @@ int main(int argc, char *argv[])
int yes = 0, no = 0;
char *prompt = NULL, *query = NULL, *histname = NULL;
char **opts = NULL;
+ char delim = '\n';
size_t linescap = 0, linecap = 0;
- int nopts = 0;
- char *line = NULL;
- struct pollfd pfd = {STDIN_FILENO, POLLIN, 0};
- if (poll(&pfd, 1, 50) > 0) {
- while ((getline(&line, &linecap, stdin)) >= 0) {
- if ((size_t)nopts >= linescap)
- opts = memcheck(realloc(opts, (linescap += 100)*sizeof(char*)));
- if (!line[0]) continue;
- if (line[strlen(line)-1] == '\n')
- line[strlen(line)-1] = '\0';
- opts[nopts++] = memcheck(strdup(line));
- }
- }
int a;
for (a = 1; a < argc; a++) {
if (strcmp(argv[a], "-H") == 0) {
@@ -399,6 +388,7 @@ int main(int argc, char *argv[])
case 'Q': quickpick = 1; break;
case 'h': goto help_flag;
case 'v': goto version_flag;
+ case '0': delim = '\0'; break;
case 'y': yes = 1; quickpick = 1; break;
case 'n': no = 1; quickpick = 1; break;
}
@@ -407,6 +397,8 @@ int main(int argc, char *argv[])
prompt = argv[++a];
} else if (strcmp(argv[a], "-q") == 0) {
query = argv[++a];
+ } else if (strncmp(argv[a], "--read0", strlen("--read0")) == 0) {
+ delim = '\0';
} else if (strncmp(argv[a], "--prompt=", strlen("--prompt=")) == 0) {
prompt = &argv[a][strlen("--prompt=")];
} else if (strncmp(argv[a], "--query=", strlen("--query=")) == 0) {
@@ -437,6 +429,20 @@ int main(int argc, char *argv[])
}
if (!prompt && a < argc) prompt = argv[a++];
+ int nopts = 0;
+ char *line = NULL;
+ struct pollfd pfd = {STDIN_FILENO, POLLIN, 0};
+ if (poll(&pfd, 1, 50) > 0) {
+ while ((getdelim(&line, &linecap, delim, stdin)) >= 0) {
+ if ((size_t)nopts >= linescap)
+ opts = memcheck(realloc(opts, (linescap += 100)*sizeof(char*)));
+ if (!line[0]) continue;
+ if (line[strlen(line)-1] == '\n')
+ line[strlen(line)-1] = '\0';
+ opts[nopts++] = memcheck(strdup(line));
+ }
+ }
+
while (a < argc) {
if ((size_t)nopts >= linescap)
opts = memcheck(realloc(opts, (linescap += 100)*sizeof(char*)));