iomenu

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

commit 0502e73cca4f319ac484f66983cf6d40bdf843da
parent 2743c784dbce7910cbec895c42a723f06fa7b620
Author: Josuah Demangeon <mail@josuah.net>
Date:   Sun, 29 Oct 2017 22:17:52 +0100

*.c: included each other

Diffstat:
MMakefile | 5++++-
Mbuffer.c | 21++++++++++++++++++---
Mcontrol.c | 20++++++++++++++++----
Mdisplay.c | 10+++++++++-
Diomenu.core | 0
Aiomenu.h | 21+++++++++++++++++++++
Mmain.c | 31++++++++++++++++++++++++++++++-
Mmain.h | 30+-----------------------------
Mutf8.c | 2++
9 files changed, 101 insertions(+), 39 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,12 +1,15 @@ -CFLAGS = -std=c89 -Wpedantic -Wall -Wextra -g -D_POSIX_C_SOURCE=200809L +CFLAGS = -std=c89 -pedantic -Wall -Wextra -g -D_POSIX_C_SOURCE=200809L OBJ = buffer.o control.o display.o main.o utf8.o +INC = buffer.h control.h display.h main.h utf8.h iomenu.h all: iomenu iomenu: $(OBJ) $(CC) $(LDFLAGS) $(OBJ) -o $@ +$(OBJ): $(INC) + clean: rm -f *.o iomenu diff --git a/buffer.c b/buffer.c @@ -1,4 +1,14 @@ +#include <sys/ioctl.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <limits.h> + +#include "iomenu.h" #include "buffer.h" +#include "main.h" +#include "control.h" static char * read_line(FILE *fp) @@ -34,6 +44,8 @@ match_line(char *line, char **tokv, int tokc) void free_lines(void) { + extern char **linev; + if (linev) { for (; linec > 0; linec--) free(linev[linec - 1]); @@ -46,12 +58,13 @@ free_lines(void) void read_stdin(void) { - int size = 0; + int size = 0; + extern char **linev; while (1) { if (linec >= size) { size += BUFSIZ; - linev = realloc(linev, sizeof (char **) * size); + linev = realloc(linev, sizeof (char **) * size); matchv = realloc(matchv, sizeof (char **) * size); if (!linev || !matchv) die("realloc"); @@ -67,11 +80,13 @@ void filter(void) { int tokc = 0; - int n = 0; + int n = 0; int i; char **tokv = NULL; char *s; char buffer[sizeof (input)]; + extern char **linev; + extern current; current = offset = next = 0; strcpy(buffer, input); diff --git a/control.c b/control.c @@ -1,4 +1,15 @@ +#include <sys/ioctl.h> + +#include <stddef.h> +#include <limits.h> +#include <string.h> +#include <ctype.h> +#include <stdio.h> + +#include "iomenu.h" +#include "buffer.h" #include "control.h" +#include "display.h" #define CTL(char) ((char) ^ 0x40) #define ALT(char) ((char) + 0x80) @@ -22,7 +33,8 @@ width(char *s) int prev_page(int pos) { - int col, cols = ws.ws_col - MARGIN - 4; + int col; + int cols = ws.ws_col - MARGIN - 4; pos -= pos > 0 ? 1 : 0; for (col = 0; pos > 0; pos--) @@ -111,7 +123,7 @@ key(int k) top: switch (k) { case CTL('C'): - return EXIT_FAILURE; + return -1; case CTL('U'): input[0] = '\0'; filter(); @@ -154,7 +166,7 @@ top: case CTL('J'): /* enter */ case CTL('M'): print_selection(); - return EXIT_SUCCESS; + return 0; case ALT('['): k = CSI(fgetc(stdin)); goto top; @@ -165,5 +177,5 @@ top: add_char((char) k); } - return CONTINUE; + return 1; } diff --git a/display.c b/display.c @@ -1,3 +1,11 @@ +#include <sys/ioctl.h> +#include <string.h> +#include <stdio.h> +#include <limits.h> + +#include "iomenu.h" +#include "utf8.h" +#include "control.h" #include "display.h" static char * @@ -15,7 +23,7 @@ format(char *str, int cols) col++; } str++; - } else if (utf8_to_rune(&rune, str) && rune_is_print(rune)) { + } else if (utf8_to_rune(&rune, str) && utf8_is_print(rune)) { int i = utf8_len(str); while (i--) *fmt++ = *str++; diff --git a/iomenu.core b/iomenu.core Binary files differ. diff --git a/iomenu.h b/iomenu.h @@ -0,0 +1,21 @@ +#ifndef SIGWINCH +#define SIGWINCH 28 +#endif + +#define MARGIN 30 + +#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) + +extern struct winsize ws; +extern char **linev; +extern int linec; +extern char **matchv; +extern int matchc; +extern char *prompt; +extern char input[LINE_MAX]; +extern char formatted[LINE_MAX * 8]; +extern int current; +extern int offset; +extern int next; +extern int opt[128]; +extern int rows; diff --git a/main.c b/main.c @@ -1,8 +1,37 @@ +#include <sys/ioctl.h> + +#include <stdio.h> +#include <stdlib.h> +#include <termios.h> +#include <signal.h> +#include <string.h> +#include <unistd.h> +#include <fcntl.h> +#include <limits.h> + +#include "iomenu.h" #include "main.h" +#include "buffer.h" +#include "control.h" +#include "display.h" static struct termios termios; static int ttyfd; +struct winsize ws; +char **linev = NULL; +int linec = 0; +char **matchv = NULL; +int matchc = 0; +char *prompt = ""; +char input[LINE_MAX]; +char formatted[LINE_MAX * 8]; +int current = 0; +int offset = 0; +int rows = 0; +int next = 0; +int opt[128]; + void die(const char *s) { @@ -112,7 +141,7 @@ main(int argc, char *argv[]) set_terminal(); sigwinch(); input[0] = '\0'; - while ((exit_code = key(fgetc(stdin))) == CONTINUE) + while ((exit_code = key(fgetc(stdin))) > 0) print_screen(); print_screen(); reset_terminal(); diff --git a/main.h b/main.h @@ -1,29 +1 @@ -#ifndef SIGWINCH -#define SIGWINCH 28 -#endif - -#define CONTINUE (EXIT_SUCCESS + EXIT_FAILURE + 1) -#define MARGIN 30 - -#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) - -winsize ws; -char **linev = NULL; -int linec = 0; -char **matchv = NULL; -int matchc = 0; -char *prompt = ""; -char input[LINE_MAX]; -char formatted[LINE_MAX * 8]; -int current = 0; -int offset = 0; -int next = 0; -int opt[128]; -int rows = 0; - -size_t utf8_len (char *); -size_t rune_len (long); -size_t utf8_to_rune (long *, char *); -int utf8_is_unicode (long); -int utf8_check (char *); -int utf8_is_print (long); +void die (const char *); diff --git a/utf8.c b/utf8.c @@ -1,3 +1,5 @@ +#include <stddef.h> + #include "utf8.h" /*