eel

formatter for ii(1) logs
Log | Files | Refs | README

commit 5bebd119d058b1cf66cd83179c0fba79d19fa111
parent a84ec938ac870a97c12c1ef24878233ff0657845
Author: Josuah Demangeon <mail@josuah.net>
Date:   Thu, 14 Jun 2018 00:10:14 +0200

refactor functions to make the big "get_input" function smaller

Diffstat:
Mshuntty.c | 85++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
1 file changed, 52 insertions(+), 33 deletions(-)

diff --git a/shuntty.c b/shuntty.c @@ -20,34 +20,35 @@ static struct termios termios; ** Take terminal out of raw mode. */ static void -reset_terminal(void) +term_reset(void) { if (tcsetattr(STDERR_FILENO, TCSANOW, &termios)) perror("resetting terminal"); } -int -die(char *msg) -{ - perror(msg); - reset_terminal(); - exit(1); -} - /* ** Set terminal in raw mode. */ static void -set_terminal(void) +term_set(void) { struct termios new; if (tcgetattr(STDERR_FILENO, &termios) == -1 || tcgetattr(STDERR_FILENO, &new) == -1) - die("setting terminal"); + perror("getting terminal attributes"), exit(1); new.c_lflag &= ~(ICANON | ECHO | IEXTEN | IGNBRK | ISIG); if (tcsetattr(STDERR_FILENO, TCSANOW, &new) == -1) - die("tcsetattr"); + perror("setting terminal attributes"), exit(1); +} + +void +die(char *msg) +{ + term_reset(); + fputs(msg, stderr); + fputc('\n', stderr); + exit(1); } char @@ -70,19 +71,43 @@ display_prompt(char *prompt, char *input) die("flush tty"); } +void +cmd_enter(char *input, ssize_t *end) +{ + puts(input); + fflush(stdout); + *input = '\0'; + *end = 0; +} + +void +cmd_backspace(char *input, ssize_t *end) +{ + if (*end > 0) + input[--*end] = '\0'; +} + +void +cmd_addkey(char *input, ssize_t *end, ssize_t len, int k) +{ + if (*end < len - 1 && isprint(k)) { + input[(*end)++] = k; + input[(*end)] = '\0'; + } +} + ssize_t -get_input(char *input, ssize_t pos, ssize_t len) +get_input(char *input, ssize_t end, ssize_t len) { int k; -top: switch (k = getkey()) { +top: case CTL('C'): return -1; case 127: case CTL('H'): /* backspace */ - if (pos > 0) - input[--pos] = '\0'; + cmd_backspace(input, &end); break; case CSI('A'): /* up */ case CSI('B'): /* down */ @@ -94,10 +119,7 @@ top: break; case CTL('J'): /* enter */ case CTL('M'): - puts(input); - fflush(stdout); - *input = '\0'; - pos = 0; + cmd_enter(input, &end); break; case ALT('['): k = CSI(getkey()); @@ -106,14 +128,11 @@ top: k = ALT(getkey()); goto top; default: - if (pos < len - 1 && isprint(k)) { - input[pos++] = k; - input[pos] = '\0'; - } + cmd_addkey(input, &end, len, k); break; } - return pos; + return end; } void @@ -143,26 +162,26 @@ int main(int argc, char **argv) { struct pollfd pfd[2]; - ssize_t pos; + ssize_t end; char input[1024], *prompt; if (argc > 2) usage(argv[0]); prompt = (argc == 2) ? argv[1] : "> "; + term_set(); + if (isatty(STDIN_FILENO)) - return fputs("stdin should not be a tty\n", stderr), 1; + die("stdin should not be a tty"); if (!isatty(STDERR_FILENO)) - return fputs("stderr should be a tyy\n", stderr), 1; - - set_terminal(); + die("stderr should be a tyy"); pfd[0].fd = STDIN_FILENO; pfd[1].fd = STDERR_FILENO; pfd[0].events = pfd[1].events = POLLIN | POLLHUP; *input = '\0'; - pos = 0; + end = 0; for (;;) { display_prompt(prompt, input); @@ -174,11 +193,11 @@ main(int argc, char **argv) if ((pfd[0].revents | pfd[1].revents) & POLLHUP) break; if (pfd[1].revents & POLLIN) - if ((pos = get_input(input, pos, sizeof(input))) == -1) + if ((end = get_input(input, end, sizeof(input))) == -1) break; } fputc('\n', stderr); - reset_terminal(); + term_reset(); return 0; }