st/st.c

2772 lines
58 KiB
C
Raw Permalink Normal View History

/* See LICENSE for license details. */
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <pwd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <termios.h>
#include <unistd.h>
#include <wchar.h>
2012-11-11 19:38:41 +01:00
2017-01-20 09:06:39 +01:00
#include "st.h"
#include "win.h"
2017-01-20 09:06:39 +01:00
#if defined(__linux)
#include <pty.h>
#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
#include <util.h>
#elif defined(__FreeBSD__) || defined(__DragonFly__)
#include <libutil.h>
#endif
/* Arbitrary sizes */
#define UTF_INVALID 0xFFFD
#define UTF_SIZ 4
#define ESC_BUF_SIZ (128*UTF_SIZ)
#define ESC_ARG_SIZ 16
2024-09-02 15:04:04 +02:00
#define CAR_PER_ARG 4
#define STR_BUF_SIZ ESC_BUF_SIZ
#define STR_ARG_SIZ ESC_ARG_SIZ
/* macros */
#define IS_SET(flag) ((term.mode & (flag)) != 0)
#define ISCONTROLC0(c) (BETWEEN(c, 0, 0x1f) || (c) == 0x7f)
2015-07-10 14:21:52 +02:00
#define ISCONTROLC1(c) (BETWEEN(c, 0x80, 0x9f))
#define ISCONTROL(c) (ISCONTROLC0(c) || ISCONTROLC1(c))
#define ISDELIM(u) (u && wcschr(worddelimiters, u))
enum term_mode {
MODE_WRAP = 1 << 0,
MODE_INSERT = 1 << 1,
MODE_ALTSCREEN = 1 << 2,
MODE_CRLF = 1 << 3,
MODE_ECHO = 1 << 4,
MODE_PRINT = 1 << 5,
MODE_UTF8 = 1 << 6,
};
2012-02-16 00:33:11 +01:00
enum cursor_movement {
CURSOR_SAVE,
CURSOR_LOAD
};
enum cursor_state {
CURSOR_DEFAULT = 0,
CURSOR_WRAPNEXT = 1,
2013-06-22 23:07:00 +02:00
CURSOR_ORIGIN = 2
2012-02-16 00:33:11 +01:00
};
enum charset {
CS_GRAPHIC0,
CS_GRAPHIC1,
CS_UK,
CS_USA,
CS_MULTI,
CS_GER,
CS_FIN
};
2012-02-16 00:33:11 +01:00
enum escape_state {
ESC_START = 1,
2013-06-22 23:07:00 +02:00
ESC_CSI = 2,
ESC_STR = 4, /* DCS, OSC, PM, APC */
2012-08-29 23:14:20 +02:00
ESC_ALTCHARSET = 8,
ESC_STR_END = 16, /* a final string was encountered */
ESC_TEST = 32, /* Enter in test mode */
ESC_UTF8 = 64,
2012-02-16 00:33:11 +01:00
};
typedef struct {
Glyph attr; /* current char attributes */
int x;
int y;
char state;
} TCursor;
typedef struct {
int mode;
int type;
int snap;
/*
* Selection variables:
* nb normalized coordinates of the beginning of the selection
* ne normalized coordinates of the end of the selection
* ob original coordinates of the beginning of the selection
* oe original coordinates of the end of the selection
*/
struct {
int x, y;
} nb, ne, ob, oe;
int alt;
} Selection;
/* Internal representation of the screen */
typedef struct {
int row; /* nb row */
int col; /* nb col */
Line *line; /* screen */
Line *alt; /* alternate screen */
int *dirty; /* dirtyness of lines */
TCursor c; /* cursor */
int ocx; /* old cursor col */
int ocy; /* old cursor row */
int top; /* top scroll limit */
int bot; /* bottom scroll limit */
int mode; /* terminal mode flags */
int esc; /* escape state flags */
char trantbl[4]; /* charset table translation */
int charset; /* current charset */
int icharset; /* selected charset for sequence */
int *tabs;
Rune lastc; /* last printed char outside of sequence, 0 if control */
} Term;
/* CSI Escape sequence structs */
/* ESC '[' [[ [<priv>] <arg> [;]] <mode> [<mode>]] */
typedef struct {
char buf[ESC_BUF_SIZ]; /* raw string */
size_t len; /* raw string length */
char priv;
int arg[ESC_ARG_SIZ];
2013-06-22 23:07:00 +02:00
int narg; /* nb of args */
char mode[2];
2024-09-02 15:04:04 +02:00
int carg[ESC_ARG_SIZ][CAR_PER_ARG]; /* colon args */
} CSIEscape;
2012-08-29 23:14:20 +02:00
/* STR Escape sequence structs */
/* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
typedef struct {
2013-06-22 23:07:00 +02:00
char type; /* ESC type ... */
char *buf; /* allocated raw string */
size_t siz; /* allocation size */
size_t len; /* raw string length */
2012-08-29 23:14:20 +02:00
char *args[STR_ARG_SIZ];
2013-06-22 23:07:00 +02:00
int narg; /* nb of args */
2012-08-29 23:14:20 +02:00
} STREscape;
static void execsh(char *, char **);
static void stty(char **);
static void sigchld(int);
static void ttywriteraw(const char *, size_t);
static void csidump(void);
static void csihandle(void);
2024-09-02 15:04:04 +02:00
static void readcolonargs(char **, int, int[][CAR_PER_ARG]);
static void csiparse(void);
static void csireset(void);
static void osc_color_response(int, int, int);
static int eschandle(uchar);
2012-08-29 23:14:20 +02:00
static void strdump(void);
static void strhandle(void);
static void strparse(void);
static void strreset(void);
static void tprinter(char *, size_t);
static void tdumpsel(void);
static void tdumpline(int);
static void tdump(void);
2013-04-14 18:30:10 +02:00
static void tclearregion(int, int, int, int);
static void tcursor(int);
static void tdeletechar(int);
static void tdeleteline(int);
static void tinsertblank(int);
static void tinsertblankline(int);
static int tlinelen(int);
static void tmoveto(int, int);
static void tmoveato(int, int);
static void tnewline(int);
static void tputtab(int);
static void tputc(Rune);
static void treset(void);
static void tscrollup(int, int);
static void tscrolldown(int, int);
static void tsetattr(const int *, int);
static void tsetchar(Rune, const Glyph *, int, int);
static void tsetdirt(int, int);
static void tsetscroll(int, int);
2010-08-30 16:48:18 +02:00
static void tswapscreen(void);
static void tsetmode(int, int, const int *, int);
static int twrite(const char *, int, int);
2011-10-20 23:20:59 +02:00
static void tfulldirt(void);
static void tcontrolcode(uchar );
static void tdectest(char );
static void tdefutf8(char);
static int32_t tdefcolor(const int *, int *, int);
static void tdeftran(char);
static void tstrsequence(uchar);
static void drawregion(int, int, int, int);
static void selnormalize(void);
static void selscroll(int, int);
2015-04-30 22:51:35 +02:00
static void selsnap(int *, int *, int);
static size_t utf8decode(const char *, Rune *, size_t);
static Rune utf8decodebyte(char, size_t *);
static char utf8encodebyte(Rune, size_t);
static size_t utf8validate(Rune *, size_t);
static char *base64dec(const char *);
static char base64dec_getc(const char **);
2014-04-25 16:27:26 +02:00
static ssize_t xwrite(int, const char *, size_t);
2009-05-10 14:17:09 +02:00
/* Globals */
static Term term;
static Selection sel;
2012-08-29 23:14:20 +02:00
static CSIEscape csiescseq;
static STREscape strescseq;
static int iofd = 1;
static int cmdfd;
static pid_t pid;
2017-01-20 09:06:39 +01:00
static const uchar utfbyte[UTF_SIZ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
static const uchar utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
static const Rune utfmin[UTF_SIZ + 1] = { 0, 0, 0x80, 0x800, 0x10000};
static const Rune utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
2024-09-02 14:59:57 +02:00
#include <time.h>
static int su = 0;
struct timespec sutv;
static void
tsync_begin()
{
clock_gettime(CLOCK_MONOTONIC, &sutv);
su = 1;
}
static void
tsync_end()
{
su = 0;
}
int
tinsync(uint timeout)
{
struct timespec now;
if (su && !clock_gettime(CLOCK_MONOTONIC, &now)
&& TIMEDIFF(now, sutv) >= timeout)
su = 0;
return su;
}
ssize_t
xwrite(int fd, const char *s, size_t len)
{
size_t aux = len;
ssize_t r;
while (len > 0) {
2015-07-10 14:19:31 +02:00
r = write(fd, s, len);
if (r < 0)
return r;
len -= r;
s += r;
}
2015-07-10 14:30:37 +02:00
return aux;
}
void *
xmalloc(size_t len)
{
void *p;
if (!(p = malloc(len)))
die("malloc: %s\n", strerror(errno));
return p;
}
void *
xrealloc(void *p, size_t len)
{
if ((p = realloc(p, len)) == NULL)
die("realloc: %s\n", strerror(errno));
return p;
}
char *
xstrdup(const char *s)
{
char *p;
if ((p = strdup(s)) == NULL)
die("strdup: %s\n", strerror(errno));
return p;
}
size_t
utf8decode(const char *c, Rune *u, size_t clen)
{
size_t i, j, len, type;
Rune udecoded;
*u = UTF_INVALID;
if (!clen)
return 0;
udecoded = utf8decodebyte(c[0], &len);
if (!BETWEEN(len, 1, UTF_SIZ))
return 1;
for (i = 1, j = 1; i < clen && j < len; ++i, ++j) {
udecoded = (udecoded << 6) | utf8decodebyte(c[i], &type);
if (type != 0)
return j;
}
if (j < len)
return 0;
*u = udecoded;
utf8validate(u, len);
2015-07-10 14:30:37 +02:00
return len;
}
Rune
utf8decodebyte(char c, size_t *i)
{
for (*i = 0; *i < LEN(utfmask); ++(*i))
if (((uchar)c & utfmask[*i]) == utfbyte[*i])
return (uchar)c & ~utfmask[*i];
2015-07-10 14:30:37 +02:00
return 0;
}
size_t
utf8encode(Rune u, char *c)
{
size_t len, i;
len = utf8validate(&u, 0);
if (len > UTF_SIZ)
return 0;
2015-07-10 14:30:37 +02:00
for (i = len - 1; i != 0; --i) {
c[i] = utf8encodebyte(u, 0);
u >>= 6;
}
c[0] = utf8encodebyte(u, len);
2015-07-10 14:30:37 +02:00
return len;
}
char
utf8encodebyte(Rune u, size_t i)
{
return utfbyte[i] | (u & ~utfmask[i]);
}
size_t
utf8validate(Rune *u, size_t i)
{
if (!BETWEEN(*u, utfmin[i], utfmax[i]) || BETWEEN(*u, 0xD800, 0xDFFF))
*u = UTF_INVALID;
for (i = 1; *u > utfmax[i]; ++i)
;
2015-07-10 14:30:37 +02:00
return i;
}
char
base64dec_getc(const char **src)
{
while (**src && !isprint((unsigned char)**src))
2020-04-10 22:26:12 +02:00
(*src)++;
return **src ? *((*src)++) : '='; /* emulate padding if string ends */
}
char *
base64dec(const char *src)
{
size_t in_len = strlen(src);
char *result, *dst;
static const char base64_digits[256] = {
[43] = 62, 0, 0, 0, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
0, 0, 0, -1, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0,
0, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
};
if (in_len % 4)
in_len += 4 - (in_len % 4);
result = dst = xmalloc(in_len / 4 * 3 + 1);
while (*src) {
int a = base64_digits[(unsigned char) base64dec_getc(&src)];
int b = base64_digits[(unsigned char) base64dec_getc(&src)];
int c = base64_digits[(unsigned char) base64dec_getc(&src)];
int d = base64_digits[(unsigned char) base64dec_getc(&src)];
/* invalid input. 'a' can be -1, e.g. if src is "\n" (c-str) */
if (a == -1 || b == -1)
break;
*dst++ = (a << 2) | ((b & 0x30) >> 4);
if (c == -1)
break;
*dst++ = ((b & 0x0f) << 4) | ((c & 0x3c) >> 2);
if (d == -1)
break;
*dst++ = ((c & 0x03) << 6) | d;
}
*dst = '\0';
return result;
}
void
selinit(void)
{
sel.mode = SEL_IDLE;
sel.snap = 0;
sel.ob.x = -1;
}
2015-04-18 18:45:21 +02:00
int
tlinelen(int y)
{
int i = term.col;
if (term.line[y][i - 1].mode & ATTR_WRAP)
return i;
while (i > 0 && term.line[y][i - 1].u == ' ')
--i;
return i;
}
void
selstart(int col, int row, int snap)
{
selclear();
sel.mode = SEL_EMPTY;
sel.type = SEL_REGULAR;
sel.alt = IS_SET(MODE_ALTSCREEN);
sel.snap = snap;
sel.oe.x = sel.ob.x = col;
sel.oe.y = sel.ob.y = row;
selnormalize();
if (sel.snap != 0)
sel.mode = SEL_READY;
tsetdirt(sel.nb.y, sel.ne.y);
}
void
selextend(int col, int row, int type, int done)
{
int oldey, oldex, oldsby, oldsey, oldtype;
if (sel.mode == SEL_IDLE)
return;
if (done && sel.mode == SEL_EMPTY) {
selclear();
return;
}
oldey = sel.oe.y;
oldex = sel.oe.x;
oldsby = sel.nb.y;
oldsey = sel.ne.y;
oldtype = sel.type;
sel.oe.x = col;
sel.oe.y = row;
selnormalize();
sel.type = type;
if (oldey != sel.oe.y || oldex != sel.oe.x || oldtype != sel.type || sel.mode == SEL_EMPTY)
tsetdirt(MIN(sel.nb.y, oldsby), MAX(sel.ne.y, oldsey));
sel.mode = done ? SEL_IDLE : SEL_READY;
}
void
selnormalize(void)
{
int i;
if (sel.type == SEL_REGULAR && sel.ob.y != sel.oe.y) {
sel.nb.x = sel.ob.y < sel.oe.y ? sel.ob.x : sel.oe.x;
sel.ne.x = sel.ob.y < sel.oe.y ? sel.oe.x : sel.ob.x;
} else {
sel.nb.x = MIN(sel.ob.x, sel.oe.x);
sel.ne.x = MAX(sel.ob.x, sel.oe.x);
}
sel.nb.y = MIN(sel.ob.y, sel.oe.y);
sel.ne.y = MAX(sel.ob.y, sel.oe.y);
2015-04-30 22:51:35 +02:00
selsnap(&sel.nb.x, &sel.nb.y, -1);
selsnap(&sel.ne.x, &sel.ne.y, +1);
/* expand selection over line breaks */
if (sel.type == SEL_RECTANGULAR)
return;
i = tlinelen(sel.nb.y);
if (i < sel.nb.x)
sel.nb.x = i;
if (tlinelen(sel.ne.y) <= sel.ne.x)
sel.ne.x = term.col - 1;
}
int
selected(int x, int y)
{
if (sel.mode == SEL_EMPTY || sel.ob.x == -1 ||
sel.alt != IS_SET(MODE_ALTSCREEN))
return 0;
if (sel.type == SEL_RECTANGULAR)
2014-04-27 13:16:41 +02:00
return BETWEEN(y, sel.nb.y, sel.ne.y)
&& BETWEEN(x, sel.nb.x, sel.ne.x);
2014-04-27 13:16:41 +02:00
return BETWEEN(y, sel.nb.y, sel.ne.y)
&& (y != sel.nb.y || x >= sel.nb.x)
&& (y != sel.ne.y || x <= sel.ne.x);
}
void
selsnap(int *x, int *y, int direction)
{
int newx, newy, xt, yt;
int delim, prevdelim;
const Glyph *gp, *prevgp;