iomenu

interactive text selection menu
Log | Files | Refs | README | LICENSE

commit 972969f0568ff78ac41d72a942440859af004ad2
parent 17183e9024bb8a1fa1e2152819ac29a3e82047b4
Author: Josuah Demangeon <mail@josuah.net>
Date:   Wed,  1 Nov 2017 17:11:20 +0100

fixing empty input and removing not used -l option

Diffstat:
Mbuffer.c | 12+++++++++---
Mcontrol.c | 1+
Mdisplay.c | 1+
Miomenu.1 | 6------
Miomenu.h | 1-
Mmain.c | 14+-------------
6 files changed, 12 insertions(+), 23 deletions(-)

diff --git a/buffer.c b/buffer.c @@ -19,7 +19,6 @@ match_line(char *line, char **tokv, int tokc) while (tokc-- > 0) if (strstr(line, tokv[tokc]) == NULL) return 0; - return 1; } @@ -49,6 +48,8 @@ split_lines(char *buf) b = buf; while ((b = strchr(b + 1, '\n'))) linec++; + if (!linec) + linec = 1; if (!(lv = linev = calloc(linec, sizeof (char **)))) die("calloc"); if (!(mv = matchv = calloc(linec, sizeof (char **)))) { @@ -70,14 +71,19 @@ read_stdin(void) size_t len; size_t off; char *buf; + char *b; off = 0; buf = malloc(size); while ((len = read(STDIN_FILENO, buf + off, size - off)) > 0) { off += len; - if (off > size >> 1) { + if (off >= size >> 1) { size <<= 1; - buf = realloc(buf, size); + if (!(b = realloc(buf, size + 1))) { + free(buf); + die("realloc"); + } + buf = b; } } buf[off] = '\0'; diff --git a/control.c b/control.c @@ -32,6 +32,7 @@ static void move_page(signed int sign) { int i; + int rows = ws.ws_row - 1; i = current - current % rows + rows * sign; if (!(0 <= i && i < matchc)) diff --git a/display.c b/display.c @@ -61,6 +61,7 @@ print_screen(void) int p; int i; int cols = ws.ws_col - 1; + int rows = ws.ws_row - 1; p = 0; i = current - current % rows; diff --git a/iomenu.1 b/iomenu.1 @@ -15,7 +15,6 @@ . .Nm .Op Fl # -.Op Fl l Ar lines .Op Fl p Ar prompt . . @@ -32,11 +31,6 @@ The selected line is printed to standard output. . .Bl -tag -width XXXXXXXXXXXXXXXX . -.It Fl l Ar lines -If -.Ar lines -Total amount of lines to display on the screen. -. .It Fl p Ar prompt Set the prompt to display at the beginning of the input to .Ar prompt . diff --git a/iomenu.h b/iomenu.h @@ -16,4 +16,3 @@ extern char input[LINE_MAX]; extern char formatted[LINE_MAX * 8]; extern int current; extern int opt[128]; -extern int rows; diff --git a/main.c b/main.c @@ -27,7 +27,6 @@ char *prompt = ""; char input[LINE_MAX]; char formatted[LINE_MAX * 8]; int current = 0; -int rows = 0; int opt[128]; void @@ -66,7 +65,6 @@ sigwinch() { if (ioctl(ttyfd, TIOCGWINSZ, &ws) < 0) die("ioctl"); - rows = MIN(opt['l'], ws.ws_row - 1); print_screen(); signal(SIGWINCH, sigwinch); } @@ -74,7 +72,7 @@ sigwinch() static void usage(void) { - fputs("iomenu [-#] [-l lines] [-p prompt]\n", stderr); + fputs("iomenu [-#] [-p prompt]\n", stderr); exit(EXIT_FAILURE); } @@ -82,15 +80,10 @@ static void parse_opt(int argc, char *argv[]) { memset(opt, 0, 128 * sizeof (int)); - opt['l'] = 255; for (argv++, argc--; argc > 0; argv++, argc--) { if (argv[0][0] != '-') usage(); switch ((*argv)[1]) { - case 'l': - if (!--argc || (opt['l'] = atoi(*++argv)) <= 0) - usage(); - break; case 'p': if (!--argc) usage(); @@ -99,11 +92,6 @@ parse_opt(int argc, char *argv[]) case '#': opt['#'] = 1; break; - case 's': - if (!--argc) - usage(); - opt['s'] = (int) **++argv; - break; default: usage(); }