#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;
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,
}
break;
case EOF:
+ if (sigwinch) {
+ sigwinch = 0;
+ getcolumns();
+ }
if (sigquit) {
case CTRLD:
newline();
}
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
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();
-void getcolumns(void);
int stringinput(struct context *c);
int scriptinput(struct context *c);
+void getcolumns(void);
int userinput(struct context *c);
#include "utils.h"
-int sigquit, sigint, sigwinch;
+int sigwinch, sigquit, sigint;
sigset_t shellsigmask, childsigmask;
struct sigaction defaultaction;
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) {
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);
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);
-extern int sigquit, sigint, sigwinch;
+extern int sigwinch, sigquit, sigint;
extern sigset_t shellsigmask, childsigmask;
extern struct sigaction defaultaction;