From: Trent Huber Date: Sat, 15 Nov 2025 08:45:09 +0000 (-0500) Subject: Clean up user input code X-Git-Url: https://trenthuber.com/code?a=commitdiff_plain;h=bb56a72b7c72670c2440dfe279b5aabce390032f;p=thus.git Clean up user input code --- diff --git a/src/input.c b/src/input.c index e839978..640c9be 100644 --- a/src/input.c +++ b/src/input.c @@ -14,17 +14,12 @@ #include "signals.h" #include "utils.h" -#define OFFSET(x) ((promptlen + (x) - start) % window.ws_col) +#define OFFSET(x) ((promptlen + x - start) % window.ws_col) +static char *end, *cursor, *start; static struct winsize window; -static char *start, *cursor, *end; static size_t promptlen; -void getcolumns(void) { - if (ioctl(STDIN_FILENO, TIOCGWINSZ, &window) == -1 && window.ws_col == 0) - window.ws_col = 80; -} - int stringinput(struct context *c) { size_t l; @@ -121,15 +116,17 @@ static void newline(void) { putchar('\r'); } +void getcolumns(void) { + if (ioctl(STDIN_FILENO, TIOCGWINSZ, &window) == -1 && window.ws_col == 0) + window.ws_col = 80; +} + int userinput(struct context *c) { enum { CTRLD = '\004', CLEAR = '\014', ESCAPE = '\033', - - /* See `ESCAPE' case */ ALT = '2' + 1, - UP = 'A', DOWN, RIGHT, @@ -161,6 +158,10 @@ int userinput(struct context *c) { } break; case EOF: + if (sigwinch) { + sigwinch = 0; + getcolumns(); + } if (sigquit) { case CTRLD: newline(); @@ -168,28 +169,24 @@ int userinput(struct context *c) { } if (sigint) { sigint = 0; - - newline(); - prompt(); - end = cursor = start; *start = '\0'; addhistory(NULL); - } - if (sigwinch) { - sigwinch = 0; - getcolumns(); + newline(); + prompt(); } break; case CLEAR: - fputs("\033[H\033[J", stdout); - prompt(); oldcursor = cursor; cursor = start; + + fputs("\033[H\033[J", stdout); + prompt(); while (cursor != end) moveright(); while (cursor != oldcursor) moveleft(); + break; /* This is a very minimal way to handle arrow keys. All modifiers except for @@ -213,17 +210,15 @@ int userinput(struct context *c) { switch (current) { case UP: case DOWN: - oldend = end; + if (!gethistory(current == UP, c->buffer)) break; oldcursor = cursor; - if (!gethistory(current == UP, c->buffer)) break; + oldend = end; end = cursor = start + strlen(start); while (cursor < oldend) *cursor++ = ' '; cursor = oldcursor; while (cursor != start) moveleft(); - putchar('\r'); - prompt(); while (cursor < end || cursor < oldend) moveright(); while (cursor != end) moveleft(); diff --git a/src/input.h b/src/input.h index 663d679..19ab26f 100644 --- a/src/input.h +++ b/src/input.h @@ -1,4 +1,4 @@ -void getcolumns(void); int stringinput(struct context *c); int scriptinput(struct context *c); +void getcolumns(void); int userinput(struct context *c); diff --git a/src/signals.c b/src/signals.c index 2d22a13..c1e2b76 100644 --- a/src/signals.c +++ b/src/signals.c @@ -5,7 +5,7 @@ #include "utils.h" -int sigquit, sigint, sigwinch; +int sigwinch, sigquit, sigint; sigset_t shellsigmask, childsigmask; struct sigaction defaultaction; @@ -14,22 +14,22 @@ void setsig(int sig, struct sigaction *act) { fatal("Unable to install %s handler", strsignal(sig)); } -static void sigquithandler(int sig) { +static void sigwinchhandler(int sig) { (void)sig; - sigquit = 1; + sigwinch = 1; } -static void siginthandler(int sig) { +static void sigquithandler(int sig) { (void)sig; - sigint = 1; + sigquit = 1; } -static void sigwinchhandler(int sig) { +static void siginthandler(int sig) { (void)sig; - sigwinch = 1; + sigint = 1; } void initsignals(void) { @@ -40,6 +40,9 @@ void initsignals(void) { sigaddset(&shellsigmask, SIGTTIN); sigaddset(&shellsigmask, SIGTTOU); + action = (struct sigaction){.sa_handler = sigwinchhandler}; + setsig(SIGWINCH, &action); + action = (struct sigaction){.sa_handler = sigquithandler}; setsig(SIGHUP, &action); setsig(SIGQUIT, &action); @@ -48,9 +51,6 @@ void initsignals(void) { action = (struct sigaction){.sa_handler = siginthandler}; setsig(SIGINT, &action); - action = (struct sigaction){.sa_handler = sigwinchhandler}; - setsig(SIGWINCH, &action); - defaultaction = (struct sigaction){.sa_handler = SIG_DFL}; setsig(SIGTSTP, &defaultaction); setsig(SIGTTOU, &defaultaction); diff --git a/src/signals.h b/src/signals.h index a23a08a..14961b6 100644 --- a/src/signals.h +++ b/src/signals.h @@ -1,4 +1,4 @@ -extern int sigquit, sigint, sigwinch; +extern int sigwinch, sigquit, sigint; extern sigset_t shellsigmask, childsigmask; extern struct sigaction defaultaction;