2015-04-13 14:03:12 +02:00
|
|
|
|
/* See LICENSE for license details. */
|
2009-06-16 02:23:46 +02:00
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <fcntl.h>
|
2009-10-28 14:34:22 +01:00
|
|
|
|
#include <limits.h>
|
2012-10-28 06:54:08 +01:00
|
|
|
|
#include <pwd.h>
|
2009-06-16 02:23:46 +02:00
|
|
|
|
#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>
|
2018-03-09 15:35:34 +01:00
|
|
|
|
#include <termios.h>
|
2009-06-16 02:23:46 +02:00
|
|
|
|
#include <unistd.h>
|
2013-09-07 12:41:36 +02:00
|
|
|
|
#include <wchar.h>
|
2012-11-11 19:38:41 +01:00
|
|
|
|
|
2017-01-20 09:06:39 +01:00
|
|
|
|
#include "st.h"
|
2017-10-10 18:30:36 +02:00
|
|
|
|
#include "win.h"
|
2017-01-20 09:06:39 +01:00
|
|
|
|
|
2010-08-30 13:04:19 +02:00
|
|
|
|
#if defined(__linux)
|
2010-08-28 03:18:22 +02:00
|
|
|
|
#include <pty.h>
|
2010-11-24 14:27:44 +01:00
|
|
|
|
#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
|
2010-08-28 03:18:22 +02:00
|
|
|
|
#include <util.h>
|
2010-08-30 13:04:19 +02:00
|
|
|
|
#elif defined(__FreeBSD__) || defined(__DragonFly__)
|
2010-08-28 03:18:22 +02:00
|
|
|
|
#include <libutil.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2009-06-16 02:23:46 +02:00
|
|
|
|
/* Arbitrary sizes */
|
2014-03-25 20:20:26 +01:00
|
|
|
|
#define UTF_INVALID 0xFFFD
|
2018-02-24 23:32:20 +01:00
|
|
|
|
#define UTF_SIZ 4
|
2013-01-18 19:11:25 +01:00
|
|
|
|
#define ESC_BUF_SIZ (128*UTF_SIZ)
|
2010-02-08 23:16:55 +01:00
|
|
|
|
#define ESC_ARG_SIZ 16
|
2024-09-02 15:04:04 +02:00
|
|
|
|
#define CAR_PER_ARG 4
|
2013-01-18 19:11:25 +01:00
|
|
|
|
#define STR_BUF_SIZ ESC_BUF_SIZ
|
|
|
|
|
#define STR_ARG_SIZ ESC_ARG_SIZ
|
2009-06-16 02:23:46 +02:00
|
|
|
|
|
2012-10-28 13:25:53 +01:00
|
|
|
|
/* macros */
|
2018-02-23 21:16:52 +01:00
|
|
|
|
#define IS_SET(flag) ((term.mode & (flag)) != 0)
|
2020-05-09 13:55:34 +02:00
|
|
|
|
#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))
|
2019-03-15 20:40:16 +01:00
|
|
|
|
#define ISDELIM(u) (u && wcschr(worddelimiters, u))
|
2013-07-19 20:34:36 +02:00
|
|
|
|
|
2018-02-23 21:16:52 +01:00
|
|
|
|
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,
|
2012-11-08 17:15:26 +01:00
|
|
|
|
CURSOR_WRAPNEXT = 1,
|
2013-06-22 23:07:00 +02:00
|
|
|
|
CURSOR_ORIGIN = 2
|
2012-02-16 00:33:11 +01:00
|
|
|
|
};
|
|
|
|
|
|
Add support for multiple charset definitions
vt100 has support for two defined charset, G0 and G1. Each charset
can be defined, but in each moment is selected only one of both
charset. This is usually used selecting a national charset in G0
and graphic charset in G1, so you can switch between graphic
charset and text charset without losing the national charset
already defined.
st hasn't support for national charsets, because it is an utf8
based terminal emulator, but it has support for graphic
charset because it is heavily used, but it only supports G0,
without understanding G1 selection sequences, which causes some
programs in some moments can print some garbage in the screen.
This patch adds a fake support for multiple charset definitions,
where we only support graphic charset and us-ascii charset, but
we allow more of one charset definition.
This patch allow define G0 until G3 charsets, but only accepts
select G0 or G1, and it accepts some national charset definitions
but all of them are mapped to us-ascii.
2013-10-01 21:23:11 +02: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,
|
2020-06-17 22:05:48 +02:00
|
|
|
|
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 */
|
2012-10-07 11:06:17 +02:00
|
|
|
|
ESC_TEST = 32, /* Enter in test mode */
|
2016-09-13 14:01:18 +02:00
|
|
|
|
ESC_UTF8 = 64,
|
2012-02-16 00:33:11 +01:00
|
|
|
|
};
|
|
|
|
|
|
2018-02-24 23:32:20 +01:00
|
|
|
|
typedef struct {
|
|
|
|
|
Glyph attr; /* current char attributes */
|
2024-03-30 12:30:49 +01:00
|
|
|
|
int x;
|
|
|
|
|
int y;
|
2018-02-24 23:32:20 +01:00
|
|
|
|
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;
|
|
|
|
|
|
2018-02-24 22:32:48 +01:00
|
|
|
|
/* 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;
|
2020-05-30 21:34:57 +02:00
|
|
|
|
Rune lastc; /* last printed char outside of sequence, 0 if control */
|
2018-02-24 22:32:48 +01:00
|
|
|
|
} Term;
|
|
|
|
|
|
2010-02-03 03:25:35 +01:00
|
|
|
|
/* CSI Escape sequence structs */
|
2015-03-18 21:12:47 +01:00
|
|
|
|
/* ESC '[' [[ [<priv>] <arg> [;]] <mode> [<mode>]] */
|
2009-06-16 02:23:46 +02:00
|
|
|
|
typedef struct {
|
2010-02-08 23:16:55 +01:00
|
|
|
|
char buf[ESC_BUF_SIZ]; /* raw string */
|
2019-10-16 11:38:43 +02:00
|
|
|
|
size_t len; /* raw string length */
|
2009-06-16 02:23:46 +02:00
|
|
|
|
char priv;
|
2010-02-08 23:16:55 +01:00
|
|
|
|
int arg[ESC_ARG_SIZ];
|
2013-06-22 23:07:00 +02:00
|
|
|
|
int narg; /* nb of args */
|
2015-03-18 21:12:47 +01:00
|
|
|
|
char mode[2];
|
2024-09-02 15:04:04 +02:00
|
|
|
|
int carg[ESC_ARG_SIZ][CAR_PER_ARG]; /* colon args */
|
2010-02-03 03:25:35 +01:00
|
|
|
|
} CSIEscape;
|
2009-06-16 02:23:46 +02:00
|
|
|
|
|
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 ... */
|
OSC 52 - copy to clipboard: don't limit to 382 bytes
Strings which an application sends to the terminal in OSC, DCS, etc
are typically small (title, colors, etc) but one exception is OSC 52
which copies text to the clipboard, and is used for instance by tmux.
Previously st cropped these strings at 512 bytes, which for OSC 52
limited the copied text to 382 bytes (remaining buffer space before
base64). This made it less useful than it can be.
Now it's a dynamic growing buffer. It remains allocated after use,
resets to 512 when a new string starts, or leaked on exit.
Resetting/deallocating the buffer right after use (at strhandle) is
possible with some more code, however, it doesn't always end up used,
and to cover those cases too will require even more code, so resetting
only on new string is good enough for now.
2019-10-16 11:55:53 +02:00
|
|
|
|
char *buf; /* allocated raw string */
|
|
|
|
|
size_t siz; /* allocation size */
|
2019-10-16 11:38:43 +02:00
|
|
|
|
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;
|
|
|
|
|
|
2018-02-24 23:16:12 +01:00
|
|
|
|
static void execsh(char *, char **);
|
2017-10-13 05:25:49 +02:00
|
|
|
|
static void stty(char **);
|
2009-07-10 17:23:11 +02:00
|
|
|
|
static void sigchld(int);
|
2018-02-22 08:05:12 +01:00
|
|
|
|
static void ttywriteraw(const char *, size_t);
|
2009-07-10 17:23:11 +02:00
|
|
|
|
|
2010-02-03 03:25:35 +01:00
|
|
|
|
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]);
|
2010-02-03 03:25:35 +01:00
|
|
|
|
static void csiparse(void);
|
|
|
|
|
static void csireset(void);
|
2022-01-07 18:21:04 +01:00
|
|
|
|
static void osc_color_response(int, int, int);
|
2015-04-09 01:16:57 +02:00
|
|
|
|
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);
|
2009-07-10 17:23:11 +02:00
|
|
|
|
|
2014-04-22 19:59:21 +02:00
|
|
|
|
static void tprinter(char *, size_t);
|
2014-02-01 13:41:58 +01:00
|
|
|
|
static void tdumpsel(void);
|
2014-01-31 21:53:53 +01:00
|
|
|
|
static void tdumpline(int);
|
2014-01-31 22:25:51 +01:00
|
|
|
|
static void tdump(void);
|
2013-04-14 18:30:10 +02:00
|
|
|
|
static void tclearregion(int, int, int, int);
|
2009-07-10 17:23:11 +02:00
|
|
|
|
static void tcursor(int);
|
|
|
|
|
static void tdeletechar(int);
|
|
|
|
|
static void tdeleteline(int);
|
|
|
|
|
static void tinsertblank(int);
|
|
|
|
|
static void tinsertblankline(int);
|
2014-06-27 09:15:56 +02:00
|
|
|
|
static int tlinelen(int);
|
2009-07-10 17:23:11 +02:00
|
|
|
|
static void tmoveto(int, int);
|
2014-04-22 19:59:21 +02:00
|
|
|
|
static void tmoveato(int, int);
|
2010-10-16 20:50:29 +02:00
|
|
|
|
static void tnewline(int);
|
2014-04-23 21:12:45 +02:00
|
|
|
|
static void tputtab(int);
|
2015-05-05 22:13:21 +02:00
|
|
|
|
static void tputc(Rune);
|
2010-03-11 23:50:50 +01:00
|
|
|
|
static void treset(void);
|
2010-09-01 23:20:54 +02:00
|
|
|
|
static void tscrollup(int, int);
|
|
|
|
|
static void tscrolldown(int, int);
|
2021-03-28 21:16:59 +02:00
|
|
|
|
static void tsetattr(const int *, int);
|
|
|
|
|
static void tsetchar(Rune, const Glyph *, int, int);
|
2018-02-22 06:29:41 +01:00
|
|
|
|
static void tsetdirt(int, int);
|
2009-07-10 17:23:11 +02:00
|
|
|
|
static void tsetscroll(int, int);
|
2010-08-30 16:48:18 +02:00
|
|
|
|
static void tswapscreen(void);
|
2021-03-28 21:16:59 +02:00
|
|
|
|
static void tsetmode(int, int, const int *, int);
|
2017-10-16 03:35:48 +02:00
|
|
|
|
static int twrite(const char *, int, int);
|
2011-10-20 23:20:59 +02:00
|
|
|
|
static void tfulldirt(void);
|
2014-04-29 09:58:55 +02:00
|
|
|
|
static void tcontrolcode(uchar );
|
2014-04-26 09:24:04 +02:00
|
|
|
|
static void tdectest(char );
|
2016-09-13 14:01:18 +02:00
|
|
|
|
static void tdefutf8(char);
|
2021-03-28 21:16:59 +02:00
|
|
|
|
static int32_t tdefcolor(const int *, int *, int);
|
Add support for multiple charset definitions
vt100 has support for two defined charset, G0 and G1. Each charset
can be defined, but in each moment is selected only one of both
charset. This is usually used selecting a national charset in G0
and graphic charset in G1, so you can switch between graphic
charset and text charset without losing the national charset
already defined.
st hasn't support for national charsets, because it is an utf8
based terminal emulator, but it has support for graphic
charset because it is heavily used, but it only supports G0,
without understanding G1 selection sequences, which causes some
programs in some moments can print some garbage in the screen.
This patch adds a fake support for multiple charset definitions,
where we only support graphic charset and us-ascii charset, but
we allow more of one charset definition.
This patch allow define G0 until G3 charsets, but only accepts
select G0 or G1, and it accepts some national charset definitions
but all of them are mapped to us-ascii.
2013-10-01 21:23:11 +02:00
|
|
|
|
static void tdeftran(char);
|
2015-04-09 01:16:57 +02:00
|
|
|
|
static void tstrsequence(uchar);
|
2009-07-10 17:23:11 +02:00
|
|
|
|
|
2018-02-24 21:53:23 +01:00
|
|
|
|
static void drawregion(int, int, int, int);
|
|
|
|
|
|
2018-02-24 23:32:20 +01:00
|
|
|
|
static void selnormalize(void);
|
2011-09-16 18:48:16 +02:00
|
|
|
|
static void selscroll(int, int);
|
2015-04-30 22:51:35 +02:00
|
|
|
|
static void selsnap(int *, int *, int);
|
2009-07-10 17:23:11 +02:00
|
|
|
|
|
2018-02-24 23:32:20 +01:00
|
|
|
|
static size_t utf8decode(const char *, Rune *, size_t);
|
2015-05-05 22:13:21 +02:00
|
|
|
|
static Rune utf8decodebyte(char, size_t *);
|
|
|
|
|
static char utf8encodebyte(Rune, size_t);
|
|
|
|
|
static size_t utf8validate(Rune *, size_t);
|
2010-11-18 01:00:04 +01:00
|
|
|
|
|
2017-03-18 11:55:04 +01:00
|
|
|
|
static char *base64dec(const char *);
|
2018-02-24 23:32:20 +01:00
|
|
|
|
static char base64dec_getc(const char **);
|
2017-03-18 11:55:04 +01:00
|
|
|
|
|
2014-04-25 16:27:26 +02:00
|
|
|
|
static ssize_t xwrite(int, const char *, size_t);
|
2009-06-16 02:23:46 +02:00
|
|
|
|
|
2009-05-10 14:17:09 +02:00
|
|
|
|
/* Globals */
|
2018-02-24 22:32:48 +01:00
|
|
|
|
static Term term;
|
2018-02-22 06:54:29 +01:00
|
|
|
|
static Selection sel;
|
2012-08-29 23:14:20 +02:00
|
|
|
|
static CSIEscape csiescseq;
|
|
|
|
|
static STREscape strescseq;
|
2015-07-08 23:49:25 +02:00
|
|
|
|
static int iofd = 1;
|
2018-02-24 23:16:12 +01:00
|
|
|
|
static int cmdfd;
|
|
|
|
|
static pid_t pid;
|
2017-01-20 09:06:39 +01:00
|
|
|
|
|
2021-03-28 21:16:59 +02: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};
|
2014-03-25 20:20:26 +01:00
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-28 06:27:42 +01:00
|
|
|
|
ssize_t
|
2015-07-09 23:59:50 +02:00
|
|
|
|
xwrite(int fd, const char *s, size_t len)
|
|
|
|
|
{
|
2015-07-24 11:52:17 +02:00
|
|
|
|
size_t aux = len;
|
|
|
|
|
ssize_t r;
|
2012-10-28 06:27:42 +01:00
|
|
|
|
|
2015-07-10 10:29:53 +02:00
|
|
|
|
while (len > 0) {
|
2015-07-10 14:19:31 +02:00
|
|
|
|
r = write(fd, s, len);
|
2015-07-10 10:29:53 +02:00
|
|
|
|
if (r < 0)
|
2012-10-28 06:27:42 +01:00
|
|
|
|
return r;
|
|
|
|
|
len -= r;
|
|
|
|
|
s += r;
|
|
|
|
|
}
|
2015-07-10 14:30:37 +02:00
|
|
|
|
|
2012-10-28 06:27:42 +01:00
|
|
|
|
return aux;
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-12 21:25:35 +02:00
|
|
|
|
void *
|
2015-07-09 23:59:50 +02:00
|
|
|
|
xmalloc(size_t len)
|
|
|
|
|
{
|
2018-03-29 18:30:05 +02:00
|
|
|
|
void *p;
|
2012-10-06 09:58:45 +02:00
|
|
|
|
|
2018-03-29 18:30:05 +02:00
|
|
|
|
if (!(p = malloc(len)))
|
|
|
|
|
die("malloc: %s\n", strerror(errno));
|
2012-10-06 09:58:45 +02:00
|
|
|
|
|
2012-09-12 21:25:35 +02:00
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void *
|
2015-07-09 23:59:50 +02:00
|
|
|
|
xrealloc(void *p, size_t len)
|
|
|
|
|
{
|
2015-07-10 10:29:53 +02:00
|
|
|
|
if ((p = realloc(p, len)) == NULL)
|
2018-03-29 18:30:05 +02:00
|
|
|
|
die("realloc: %s\n", strerror(errno));
|
2012-10-06 09:58:45 +02:00
|
|
|
|
|
2012-09-12 21:25:35 +02:00
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-01 11:12:47 +01:00
|
|
|
|
char *
|
2021-03-28 21:16:59 +02:00
|
|
|
|
xstrdup(const char *s)
|
2015-07-09 23:59:50 +02:00
|
|
|
|
{
|
2021-03-28 21:16:59 +02:00
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
|
|
if ((p = strdup(s)) == NULL)
|
2018-03-29 18:30:05 +02:00
|
|
|
|
die("strdup: %s\n", strerror(errno));
|
2014-02-01 11:12:47 +01:00
|
|
|
|
|
2021-03-28 21:16:59 +02:00
|
|
|
|
return p;
|
2014-02-01 11:12:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-25 20:20:26 +01:00
|
|
|
|
size_t
|
2017-10-16 03:35:48 +02:00
|
|
|
|
utf8decode(const char *c, Rune *u, size_t clen)
|
2015-07-09 23:59:50 +02:00
|
|
|
|
{
|
2014-03-25 20:20:26 +01:00
|
|
|
|
size_t i, j, len, type;
|
2015-05-05 22:13:21 +02:00
|
|
|
|
Rune udecoded;
|
2010-11-18 01:00:04 +01:00
|
|
|
|
|
2014-03-25 20:20:26 +01:00
|
|
|
|
*u = UTF_INVALID;
|
2015-07-10 10:29:53 +02:00
|
|
|
|
if (!clen)
|
2014-03-25 20:20:26 +01:00
|
|
|
|
return 0;
|
|
|
|
|
udecoded = utf8decodebyte(c[0], &len);
|
2015-07-10 10:29:53 +02:00
|
|
|
|
if (!BETWEEN(len, 1, UTF_SIZ))
|
2010-11-18 01:00:04 +01:00
|
|
|
|
return 1;
|
2015-07-10 10:29:53 +02:00
|
|
|
|
for (i = 1, j = 1; i < clen && j < len; ++i, ++j) {
|
2014-03-25 20:20:26 +01:00
|
|
|
|
udecoded = (udecoded << 6) | utf8decodebyte(c[i], &type);
|
2015-07-10 10:29:53 +02:00
|
|
|
|
if (type != 0)
|
2014-03-25 20:20:26 +01:00
|
|
|
|
return j;
|
2010-11-18 01:00:04 +01:00
|
|
|
|
}
|
2015-07-10 10:29:53 +02:00
|
|
|
|
if (j < len)
|
2014-03-25 20:20:26 +01:00
|
|
|
|
return 0;
|
|
|
|
|
*u = udecoded;
|
|
|
|
|
utf8validate(u, len);
|
2015-07-10 14:30:37 +02:00
|
|
|
|
|
2014-03-25 20:20:26 +01:00
|
|
|
|
return len;
|
|
|
|
|
}
|
2012-10-06 09:58:45 +02:00
|
|
|
|
|
2015-05-05 22:13:21 +02:00
|
|
|
|
Rune
|
2015-07-09 23:59:50 +02:00
|
|
|
|
utf8decodebyte(char c, size_t *i)
|
|
|
|
|
{
|
2015-07-10 10:29:53 +02:00
|
|
|
|
for (*i = 0; *i < LEN(utfmask); ++(*i))
|
|
|
|
|
if (((uchar)c & utfmask[*i]) == utfbyte[*i])
|
2014-03-25 20:20:26 +01:00
|
|
|
|
return (uchar)c & ~utfmask[*i];
|
2015-07-10 14:30:37 +02:00
|
|
|
|
|
2014-03-25 20:20:26 +01:00
|
|
|
|
return 0;
|
2010-11-18 01:00:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-25 20:20:26 +01:00
|
|
|
|
size_t
|
2015-07-09 23:59:50 +02:00
|
|
|
|
utf8encode(Rune u, char *c)
|
|
|
|
|
{
|
2014-03-25 20:20:26 +01:00
|
|
|
|
size_t len, i;
|
2010-11-18 01:00:04 +01:00
|
|
|
|
|
2014-03-25 20:20:26 +01:00
|
|
|
|
len = utf8validate(&u, 0);
|
2015-07-10 10:29:53 +02:00
|
|
|
|
if (len > UTF_SIZ)
|
2010-11-18 01:00:04 +01:00
|
|
|
|
return 0;
|
2015-07-10 14:30:37 +02:00
|
|
|
|
|
2015-07-10 10:29:53 +02:00
|
|
|
|
for (i = len - 1; i != 0; --i) {
|
2014-03-25 20:20:26 +01:00
|
|
|
|
c[i] = utf8encodebyte(u, 0);
|
|
|
|
|
u >>= 6;
|
2012-10-06 09:58:45 +02:00
|
|
|
|
}
|
2014-03-25 20:20:26 +01:00
|
|
|
|
c[0] = utf8encodebyte(u, len);
|
2015-07-10 14:30:37 +02:00
|
|
|
|
|
2014-03-25 20:20:26 +01:00
|
|
|
|
return len;
|
2010-11-18 01:00:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-25 20:20:26 +01:00
|
|
|
|
char
|
2015-07-09 23:59:50 +02:00
|
|
|
|
utf8encodebyte(Rune u, size_t i)
|
|
|
|
|
{
|
2014-03-25 20:20:26 +01:00
|
|
|
|
return utfbyte[i] | (u & ~utfmask[i]);
|
|
|
|
|
}
|
2010-11-18 01:00:04 +01:00
|
|
|
|
|
2014-03-25 20:20:26 +01:00
|
|
|
|
size_t
|
2015-07-09 23:59:50 +02:00
|
|
|
|
utf8validate(Rune *u, size_t i)
|
|
|
|
|
{
|
2015-07-10 10:29:53 +02:00
|
|
|
|
if (!BETWEEN(*u, utfmin[i], utfmax[i]) || BETWEEN(*u, 0xD800, 0xDFFF))
|
2014-03-25 20:20:26 +01:00
|
|
|
|
*u = UTF_INVALID;
|
2015-07-10 10:29:53 +02:00
|
|
|
|
for (i = 1; *u > utfmax[i]; ++i)
|
2014-03-25 20:20:26 +01:00
|
|
|
|
;
|
2015-07-10 14:30:37 +02:00
|
|
|
|
|
2014-03-25 20:20:26 +01:00
|
|
|
|
return i;
|
2010-11-18 01:00:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-18 08:00:10 +02:00
|
|
|
|
char
|
|
|
|
|
base64dec_getc(const char **src)
|
|
|
|
|
{
|
2022-03-18 11:20:54 +01:00
|
|
|
|
while (**src && !isprint((unsigned char)**src))
|
2020-04-10 22:26:12 +02:00
|
|
|
|
(*src)++;
|
base64dec: don't read out of bounds
Previously, base64dec checked terminating input '\0' every 4 calls to
base64dec_getc, where the latter progressed one or more chars on each
call, and could read past '\0' in the way it was used.
The input to base64dec currently comes only from OSC 52 escape seq
(copy to clipboard), and reading past '\0' or even past the buffer
boundary was easy to trigger.
Also, even if we could trust external input to be valid base64, there
are different base64 standards, and not all of them require padding
to 4 bytes blocks (using trailing '=' chars).
It didn't affect short OSC 52 strings because the buffer is initialized
to 0's, so typically it did stop within the buffer, but if the string
was trimmed to fit (the buffer is 512 bytes) then it did also read past
the end of the buffer, and the decoded suffix ended up arbitrary.
This patch makes base64dec_getc not progress past '\0', and instead
produce fake trailing padding of '='.
Additionally, at base64dec, if padding is detected at the first or
second byte of a quartet, then we identify it as invalid and abort
(a valid quartet has at least two leading non-padding bytes).
2019-10-16 10:17:23 +02:00
|
|
|
|
return **src ? *((*src)++) : '='; /* emulate padding if string ends */
|
2017-08-18 08:00:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-18 11:55:04 +01:00
|
|
|
|
char *
|
|
|
|
|
base64dec(const char *src)
|
|
|
|
|
{
|
|
|
|
|
size_t in_len = strlen(src);
|
|
|
|
|
char *result, *dst;
|
2022-03-18 12:03:34 +01:00
|
|
|
|
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
|
|
|
|
|
};
|
2017-03-18 11:55:04 +01:00
|
|
|
|
|
|
|
|
|
if (in_len % 4)
|
2017-08-18 08:00:10 +02:00
|
|
|
|
in_len += 4 - (in_len % 4);
|
2017-03-18 11:55:04 +01:00
|
|
|
|
result = dst = xmalloc(in_len / 4 * 3 + 1);
|
|
|
|
|
while (*src) {
|
2017-08-18 08:00:10 +02:00
|
|
|
|
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)];
|
2017-03-18 11:55:04 +01:00
|
|
|
|
|
base64dec: don't read out of bounds
Previously, base64dec checked terminating input '\0' every 4 calls to
base64dec_getc, where the latter progressed one or more chars on each
call, and could read past '\0' in the way it was used.
The input to base64dec currently comes only from OSC 52 escape seq
(copy to clipboard), and reading past '\0' or even past the buffer
boundary was easy to trigger.
Also, even if we could trust external input to be valid base64, there
are different base64 standards, and not all of them require padding
to 4 bytes blocks (using trailing '=' chars).
It didn't affect short OSC 52 strings because the buffer is initialized
to 0's, so typically it did stop within the buffer, but if the string
was trimmed to fit (the buffer is 512 bytes) then it did also read past
the end of the buffer, and the decoded suffix ended up arbitrary.
This patch makes base64dec_getc not progress past '\0', and instead
produce fake trailing padding of '='.
Additionally, at base64dec, if padding is detected at the first or
second byte of a quartet, then we identify it as invalid and abort
(a valid quartet has at least two leading non-padding bytes).
2019-10-16 10:17:23 +02:00
|
|
|
|
/* invalid input. 'a' can be -1, e.g. if src is "\n" (c-str) */
|
|
|
|
|
if (a == -1 || b == -1)
|
|
|
|
|
break;
|
|
|
|
|
|
2017-03-18 11:55:04 +01:00
|
|
|
|
*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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-09 01:17:33 +02:00
|
|
|
|
void
|
2015-07-09 23:59:50 +02:00
|
|
|
|
selinit(void)
|
|
|
|
|
{
|
2015-05-01 19:13:13 +02:00
|
|
|
|
sel.mode = SEL_IDLE;
|
2015-10-11 11:44:34 +02:00
|
|
|
|
sel.snap = 0;
|
2013-05-26 13:07:26 +02:00
|
|
|
|
sel.ob.x = -1;
|
2010-08-31 16:53:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-18 18:45:21 +02:00
|
|
|
|
int
|
2015-07-09 23:59:50 +02:00
|
|
|
|
tlinelen(int y)
|
|
|
|
|
{
|
2014-06-03 04:34:58 +02:00
|
|
|
|
int i = term.col;
|
|
|
|
|
|
2015-07-10 10:29:53 +02:00
|
|
|
|
if (term.line[y][i - 1].mode & ATTR_WRAP)
|
2014-08-22 18:25:04 +02:00
|
|
|
|
return i;
|
|
|
|
|
|
2015-07-10 10:29:53 +02:00
|
|
|
|
while (i > 0 && term.line[y][i - 1].u == ' ')
|
2014-06-03 04:34:58 +02:00
|
|
|
|
--i;
|
|
|
|
|
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-22 06:29:41 +01:00
|
|
|
|
void
|
|
|
|
|
selstart(int col, int row, int snap)
|
|
|
|
|
{
|
|
|
|
|
selclear();
|
|
|
|
|
sel.mode = SEL_EMPTY;
|
|
|
|
|
sel.type = SEL_REGULAR;
|
2018-03-28 21:27:58 +02:00
|
|
|
|
sel.alt = IS_SET(MODE_ALTSCREEN);
|
2018-02-22 06:29:41 +01:00
|
|
|
|
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
|
2018-02-22 06:54:29 +01:00
|
|
|
|
selextend(int col, int row, int type, int done)
|
2018-02-22 06:29:41 +01:00
|
|
|
|
{
|
|
|
|
|
int oldey, oldex, oldsby, oldsey, oldtype;
|
2018-02-22 06:54:29 +01:00
|
|
|
|
|
2018-03-17 13:48:29 +01:00
|
|
|
|
if (sel.mode == SEL_IDLE)
|
2018-02-22 06:54:29 +01:00
|
|
|
|
return;
|
|
|
|
|
if (done && sel.mode == SEL_EMPTY) {
|
|
|
|
|
selclear();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-22 06:29:41 +01:00
|
|
|
|
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;
|
|
|
|
|
|
2019-04-10 00:54:43 +02:00
|
|
|
|
if (oldey != sel.oe.y || oldex != sel.oe.x || oldtype != sel.type || sel.mode == SEL_EMPTY)
|
2018-02-22 06:29:41 +01:00
|
|
|
|
tsetdirt(MIN(sel.nb.y, oldsby), MAX(sel.ne.y, oldsey));
|
2018-02-22 06:54:29 +01:00
|
|
|
|
|
|
|
|
|
sel.mode = done ? SEL_IDLE : SEL_READY;
|
2018-02-22 06:29:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-09 01:17:33 +02:00
|
|
|
|
void
|
2015-07-09 23:59:50 +02:00
|
|
|
|
selnormalize(void)
|
|
|
|
|
{
|
2014-06-03 04:34:58 +02:00
|
|
|
|
int i;
|
|
|
|
|
|
2015-07-10 10:29:53 +02:00
|
|
|
|
if (sel.type == SEL_REGULAR && sel.ob.y != sel.oe.y) {
|
2013-05-26 16:10:22 +02:00
|
|
|
|
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;
|
2015-04-30 21:59:24 +02:00
|
|
|
|
} else {
|
|
|
|
|
sel.nb.x = MIN(sel.ob.x, sel.oe.x);
|
|
|
|
|
sel.ne.x = MAX(sel.ob.x, sel.oe.x);
|
2013-05-26 16:10:22 +02:00
|
|
|
|
}
|
2013-05-26 13:07:26 +02:00
|
|
|
|
sel.nb.y = MIN(sel.ob.y, sel.oe.y);
|
|
|
|
|
sel.ne.y = MAX(sel.ob.y, sel.oe.y);
|
2014-06-03 04:34:58 +02:00
|
|
|
|
|
2015-04-30 22:51:35 +02:00
|
|
|
|
selsnap(&sel.nb.x, &sel.nb.y, -1);
|
|
|
|
|
selsnap(&sel.ne.x, &sel.ne.y, +1);
|
2014-08-20 21:14:08 +02:00
|
|
|
|
|
2014-06-03 04:34:58 +02:00
|
|
|
|
/* 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;
|
2013-05-26 13:07:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-08 23:56:55 +02:00
|
|
|
|
int
|
2015-07-09 23:59:50 +02:00
|
|
|
|
selected(int x, int y)
|
|
|
|
|
{
|
2018-02-22 05:48:28 +01:00
|
|
|
|
if (sel.mode == SEL_EMPTY || sel.ob.x == -1 ||
|
|
|
|
|
sel.alt != IS_SET(MODE_ALTSCREEN))
|
2015-07-08 23:56:55 +02:00
|
|
|
|
return 0;
|
2015-05-03 21:28:10 +02:00
|
|
|
|
|
2015-07-10 10:29:53 +02:00
|
|
|
|
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);
|
2012-10-06 09:58:45 +02:00
|
|
|
|
|
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);
|
2010-08-30 23:41:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-04-15 08:28:31 +02:00
|
|
|
|
void
|
2015-07-09 23:59:50 +02:00
|
|
|
|
selsnap(int *x, int *y, int direction)
|
|
|
|
|
{
|
2014-06-05 06:32:01 +02:00
|
|
|
|
int newx, newy, xt, yt;
|
2015-07-08 23:56:55 +02:00
|
|
|
|
int delim, prevdelim;
|
2021-03-28 21:16:59 +02:00
|
|
|
|
const Glyph *gp, *prevgp;
|
2014-06-05 06:32:01 +02:00
|
|