]> Trent Huber's Code - thus.git/commitdiff
Clean up user input code
authorTrent Huber <trentmhuber@gmail.com>
Sat, 15 Nov 2025 08:45:09 +0000 (03:45 -0500)
committerTrent Huber <trentmhuber@gmail.com>
Sat, 15 Nov 2025 08:45:09 +0000 (03:45 -0500)
src/input.c
src/input.h
src/signals.c
src/signals.h

index e8399780026bc0823ec45c8becf30ed046df5142..640c9be0c110b437da94adaf33286962693f65de 100644 (file)
 #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();
 
index 663d6793912f4a575a5d8d8c95d4d01c433c7bbc..19ab26f4ee41e079f76399b137d787553e7f4429 100644 (file)
@@ -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);
index 2d22a135214a9cdce3b50c0e46b0b3f136f212d2..c1e2b76bbea6aedbaa05e555f969fca2f58b2035 100644 (file)
@@ -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);
index a23a08ac5e0bf6c0e7a270990649346419d27b7e..14961b6f59328dca26809c0bcec0b8264441a831 100644 (file)
@@ -1,4 +1,4 @@
-extern int sigquit, sigint, sigwinch;
+extern int sigwinch, sigquit, sigint;
 extern sigset_t shellsigmask, childsigmask;
 extern struct sigaction defaultaction;