Simple Terminal from SuckLess
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2605 lines
55 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
13 years ago
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
15 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
15 years ago
6 years ago
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
15 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
15 years ago
​
​
15 years ago
15 years ago
​
​
​
​
​
​
​
15 years ago
15 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
15 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12 years ago
12 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
16 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
16 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /* See LICENSE for license details. */
  2. #include <ctype.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <limits.h>
  6. #include <pwd.h>
  7. #include <stdarg.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <signal.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/select.h>
  14. #include <sys/types.h>
  15. #include <sys/wait.h>
  16. #include <termios.h>
  17. #include <unistd.h>
  18. #include <wchar.h>
  19. #include "st.h"
  20. #include "win.h"
  21. #if defined(__linux)
  22. #include <pty.h>
  23. #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  24. #include <util.h>
  25. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  26. #include <libutil.h>
  27. #endif
  28. /* Arbitrary sizes */
  29. #define UTF_INVALID 0xFFFD
  30. #define UTF_SIZ 4
  31. #define ESC_BUF_SIZ (128*UTF_SIZ)
  32. #define ESC_ARG_SIZ 16
  33. #define STR_BUF_SIZ ESC_BUF_SIZ
  34. #define STR_ARG_SIZ ESC_ARG_SIZ
  35. /* macros */
  36. #define IS_SET(flag) ((term.mode & (flag)) != 0)
  37. #define ISCONTROLC0(c) (BETWEEN(c, 0, 0x1f) || (c) == 0x7f)
  38. #define ISCONTROLC1(c) (BETWEEN(c, 0x80, 0x9f))
  39. #define ISCONTROL(c) (ISCONTROLC0(c) || ISCONTROLC1(c))
  40. #define ISDELIM(u) (u && wcschr(worddelimiters, u))
  41. enum term_mode {
  42. MODE_WRAP = 1 << 0,
  43. MODE_INSERT = 1 << 1,
  44. MODE_ALTSCREEN = 1 << 2,
  45. MODE_CRLF = 1 << 3,
  46. MODE_ECHO = 1 << 4,
  47. MODE_PRINT = 1 << 5,
  48. MODE_UTF8 = 1 << 6,
  49. MODE_SIXEL = 1 << 7,
  50. };
  51. enum cursor_movement {
  52. CURSOR_SAVE,
  53. CURSOR_LOAD
  54. };
  55. enum cursor_state {
  56. CURSOR_DEFAULT = 0,
  57. CURSOR_WRAPNEXT = 1,
  58. CURSOR_ORIGIN = 2
  59. };
  60. enum charset {
  61. CS_GRAPHIC0,
  62. CS_GRAPHIC1,
  63. CS_UK,
  64. CS_USA,
  65. CS_MULTI,
  66. CS_GER,
  67. CS_FIN
  68. };
  69. enum escape_state {
  70. ESC_START = 1,
  71. ESC_CSI = 2,
  72. ESC_STR = 4, /* OSC, PM, APC */
  73. ESC_ALTCHARSET = 8,
  74. ESC_STR_END = 16, /* a final string was encountered */
  75. ESC_TEST = 32, /* Enter in test mode */
  76. ESC_UTF8 = 64,
  77. ESC_DCS =128,
  78. };
  79. typedef struct {
  80. Glyph attr; /* current char attributes */
  81. int x;
  82. int y;
  83. char state;
  84. } TCursor;
  85. typedef struct {
  86. int mode;
  87. int type;
  88. int snap;
  89. /*
  90. * Selection variables:
  91. * nb – normalized coordinates of the beginning of the selection
  92. * ne – normalized coordinates of the end of the selection
  93. * ob – original coordinates of the beginning of the selection
  94. * oe – original coordinates of the end of the selection
  95. */
  96. struct {
  97. int x, y;
  98. } nb, ne, ob, oe;
  99. int alt;
  100. } Selection;
  101. /* Internal representation of the screen */
  102. typedef struct {
  103. int row; /* nb row */
  104. int col; /* nb col */
  105. Line *line; /* screen */
  106. Line *alt; /* alternate screen */
  107. int *dirty; /* dirtyness of lines */
  108. TCursor c; /* cursor */
  109. int ocx; /* old cursor col */
  110. int ocy; /* old cursor row */
  111. int top; /* top scroll limit */
  112. int bot; /* bottom scroll limit */
  113. int mode; /* terminal mode flags */
  114. int esc; /* escape state flags */
  115. char trantbl[4]; /* charset table translation */
  116. int charset; /* current charset */
  117. int icharset; /* selected charset for sequence */
  118. int *tabs;
  119. } Term;
  120. /* CSI Escape sequence structs */
  121. /* ESC '[' [[ [<priv>] <arg> [;]] <mode> [<mode>]] */
  122. typedef struct {
  123. char buf[ESC_BUF_SIZ]; /* raw string */
  124. size_t len; /* raw string length */
  125. char priv;
  126. int arg[ESC_ARG_SIZ];
  127. int narg; /* nb of args */
  128. char mode[2];
  129. } CSIEscape;
  130. /* STR Escape sequence structs */
  131. /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
  132. typedef struct {
  133. char type; /* ESC type ... */
  134. char *buf; /* allocated raw string */
  135. size_t siz; /* allocation size */
  136. size_t len; /* raw string length */
  137. char *args[STR_ARG_SIZ];
  138. int narg; /* nb of args */
  139. } STREscape;
  140. static void execsh(char *, char **);
  141. static void stty(char **);
  142. static void sigchld(int);
  143. static void ttywriteraw(const char *, size_t);
  144. static void csidump(void);
  145. static void csihandle(void);
  146. static void csiparse(void);
  147. static void csireset(void);
  148. static int eschandle(uchar);
  149. static void strdump(void);
  150. static void strhandle(void);
  151. static void strparse(void);
  152. static void strreset(void);
  153. static void tprinter(char *, size_t);
  154. static void tdumpsel(void);
  155. static void tdumpline(int);
  156. static void tdump(void);
  157. static void tclearregion(int, int, int, int);
  158. static void tcursor(int);
  159. static void tdeletechar(int);
  160. static void tdeleteline(int);
  161. static void tinsertblank(int);
  162. static void tinsertblankline(int);
  163. static int tlinelen(int);
  164. static void tmoveto(int, int);
  165. static void tmoveato(int, int);
  166. static void tnewline(int);
  167. static void tputtab(int);
  168. static void tputc(Rune);
  169. static void treset(void);
  170. static void tscrollup(int, int);
  171. static void tscrolldown(int, int);
  172. static void tsetattr(int *, int);
  173. static void tsetchar(Rune, Glyph *, int, int);
  174. static void tsetdirt(int, int);
  175. static void tsetscroll(int, int);
  176. static void tswapscreen(void);
  177. static void tsetmode(int, int, int *, int);
  178. static int twrite(const char *, int, int);
  179. static void tfulldirt(void);
  180. static void tcontrolcode(uchar );
  181. static void tdectest(char );
  182. static void tdefutf8(char);
  183. static int32_t tdefcolor(int *, int *, int);
  184. static void tdeftran(char);
  185. static void tstrsequence(uchar);
  186. static void drawregion(int, int, int, int);
  187. static void selnormalize(void);
  188. static void selscroll(int, int);
  189. static void selsnap(int *, int *, int);
  190. static size_t utf8decode(const char *, Rune *, size_t);
  191. static Rune utf8decodebyte(char, size_t *);
  192. static char utf8encodebyte(Rune, size_t);
  193. static size_t utf8validate(Rune *, size_t);
  194. static char *base64dec(const char *);
  195. static char base64dec_getc(const char **);
  196. static ssize_t xwrite(int, const char *, size_t);
  197. /* Globals */
  198. static Term term;
  199. static Selection sel;
  200. static CSIEscape csiescseq;
  201. static STREscape strescseq;
  202. static int iofd = 1;
  203. static int cmdfd;
  204. static pid_t pid;
  205. static uchar utfbyte[UTF_SIZ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
  206. static uchar utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
  207. static Rune utfmin[UTF_SIZ + 1] = { 0, 0, 0x80, 0x800, 0x10000};
  208. static Rune utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
  209. ssize_t
  210. xwrite(int fd, const char *s, size_t len)
  211. {
  212. size_t aux = len;
  213. ssize_t r;
  214. while (len > 0) {
  215. r = write(fd, s, len);
  216. if (r < 0)
  217. return r;
  218. len -= r;
  219. s += r;
  220. }
  221. return aux;
  222. }
  223. void *
  224. xmalloc(size_t len)
  225. {
  226. void *p;
  227. if (!(p = malloc(len)))
  228. die("malloc: %s\n", strerror(errno));
  229. return p;
  230. }
  231. void *
  232. xrealloc(void *p, size_t len)
  233. {
  234. if ((p = realloc(p, len)) == NULL)
  235. die("realloc: %s\n", strerror(errno));
  236. return p;
  237. }
  238. char *
  239. xstrdup(char *s)
  240. {
  241. if ((s = strdup(s)) == NULL)
  242. die("strdup: %s\n", strerror(errno));
  243. return s;
  244. }
  245. size_t
  246. utf8decode(const char *c, Rune *u, size_t clen)
  247. {
  248. size_t i, j, len, type;
  249. Rune udecoded;
  250. *u = UTF_INVALID;
  251. if (!clen)
  252. return 0;
  253. udecoded = utf8decodebyte(c[0], &len);
  254. if (!BETWEEN(len, 1, UTF_SIZ))
  255. return 1;
  256. for (i = 1, j = 1; i < clen && j < len; ++i, ++j) {
  257. udecoded = (udecoded << 6) | utf8decodebyte(c[i], &type);
  258. if (type != 0)
  259. return j;
  260. }
  261. if (j < len)
  262. return 0;
  263. *u = udecoded;
  264. utf8validate(u, len);
  265. return len;
  266. }
  267. Rune
  268. utf8decodebyte(char c, size_t *i)
  269. {
  270. for (*i = 0; *i < LEN(utfmask); ++(*i))
  271. if (((uchar)c & utfmask[*i]) == utfbyte[*i])
  272. return (uchar)c & ~utfmask[*i];
  273. return 0;
  274. }
  275. size_t
  276. utf8encode(Rune u, char *c)
  277. {
  278. size_t len, i;
  279. len = utf8validate(&u, 0);
  280. if (len > UTF_SIZ)
  281. return 0;
  282. for (i = len - 1; i != 0; --i) {
  283. c[i] = utf8encodebyte(u, 0);
  284. u >>= 6;
  285. }
  286. c[0] = utf8encodebyte(u, len);
  287. return len;
  288. }
  289. char
  290. utf8encodebyte(Rune u, size_t i)
  291. {
  292. return utfbyte[i] | (u & ~utfmask[i]);
  293. }
  294. size_t
  295. utf8validate(Rune *u, size_t i)
  296. {
  297. if (!BETWEEN(*u, utfmin[i], utfmax[i]) || BETWEEN(*u, 0xD800, 0xDFFF))
  298. *u = UTF_INVALID;
  299. for (i = 1; *u > utfmax[i]; ++i)
  300. ;
  301. return i;
  302. }
  303. static const char base64_digits[] = {
  304. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  305. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0,
  306. 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, -1, 0, 0, 0, 0, 1,
  307. 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
  308. 22, 23, 24, 25, 0, 0, 0, 0, 0, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34,
  309. 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0,
  310. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  311. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  312. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  313. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  314. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  315. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  316. };
  317. char
  318. base64dec_getc(const char **src)
  319. {
  320. while (**src && !isprint(**src))
  321. (*src)++;
  322. return **src ? *((*src)++) : '='; /* emulate padding if string ends */
  323. }
  324. char *
  325. base64dec(const char *src)
  326. {
  327. size_t in_len = strlen(src);
  328. char *result, *dst;
  329. if (in_len % 4)
  330. in_len += 4 - (in_len % 4);
  331. result = dst = xmalloc(in_len / 4 * 3 + 1);
  332. while (*src) {
  333. int a = base64_digits[(unsigned char) base64dec_getc(&src)];
  334. int b = base64_digits[(unsigned char) base64dec_getc(&src)];
  335. int c = base64_digits[(unsigned char) base64dec_getc(&src)];
  336. int d = base64_digits[(unsigned char) base64dec_getc(&src)];
  337. /* invalid input. 'a' can be -1, e.g. if src is "\n" (c-str) */
  338. if (a == -1 || b == -1)
  339. break;
  340. *dst++ = (a << 2) | ((b & 0x30) >> 4);
  341. if (c == -1)
  342. break;
  343. *dst++ = ((b & 0x0f) << 4) | ((c & 0x3c) >> 2);
  344. if (d == -1)
  345. break;
  346. *dst++ = ((c & 0x03) << 6) | d;
  347. }
  348. *dst = '\0';
  349. return result;
  350. }
  351. void
  352. selinit(void)
  353. {
  354. sel.mode = SEL_IDLE;
  355. sel.snap = 0;
  356. sel.ob.x = -1;
  357. }
  358. int
  359. tlinelen(int y)
  360. {
  361. int i = term.col;
  362. if (term.line[y][i - 1].mode & ATTR_WRAP)
  363. return i;
  364. while (i > 0 && term.line[y][i - 1].u == ' ')
  365. --i;
  366. return i;
  367. }
  368. void
  369. selstart(int col, int row, int snap)
  370. {
  371. selclear();
  372. sel.mode = SEL_EMPTY;
  373. sel.type = SEL_REGULAR;
  374. sel.alt = IS_SET(MODE_ALTSCREEN);
  375. sel.snap = snap;
  376. sel.oe.x = sel.ob.x = col;
  377. sel.oe.y = sel.ob.y = row;
  378. selnormalize();
  379. if (sel.snap != 0)
  380. sel.mode = SEL_READY;
  381. tsetdirt(sel.nb.y, sel.ne.y);
  382. }
  383. void
  384. selextend(int col, int row, int type, int done)
  385. {
  386. int oldey, oldex, oldsby, oldsey, oldtype;
  387. if (sel.mode == SEL_IDLE)
  388. return;
  389. if (done && sel.mode == SEL_EMPTY) {
  390. selclear();
  391. return;
  392. }
  393. oldey = sel.oe.y;
  394. oldex = sel.oe.x;
  395. oldsby = sel.nb.y;
  396. oldsey = sel.ne.y;
  397. oldtype = sel.type;
  398. sel.oe.x = col;
  399. sel.oe.y = row;
  400. selnormalize();
  401. sel.type = type;
  402. if (oldey != sel.oe.y || oldex != sel.oe.x || oldtype != sel.type || sel.mode == SEL_EMPTY)
  403. tsetdirt(MIN(sel.nb.y, oldsby), MAX(sel.ne.y, oldsey));
  404. sel.mode = done ? SEL_IDLE : SEL_READY;
  405. }
  406. void
  407. selnormalize(void)
  408. {
  409. int i;
  410. if (sel.type == SEL_REGULAR && sel.ob.y != sel.oe.y) {
  411. sel.nb.x = sel.ob.y < sel.oe.y ? sel.ob.x : sel.oe.x;
  412. sel.ne.x = sel.ob.y < sel.oe.y ? sel.oe.x : sel.ob.x;
  413. } else {
  414. sel.nb.x = MIN(sel.ob.x, sel.oe.x);
  415. sel.ne.x = MAX(sel.ob.x, sel.oe.x);
  416. }
  417. sel.nb.y = MIN(sel.ob.y, sel.oe.y);
  418. sel.ne.y = MAX(sel.ob.y, sel.oe.y);
  419. selsnap(&sel.nb.x, &sel.nb.y, -1);
  420. selsnap(&sel.ne.x, &sel.ne.y, +1);
  421. /* expand selection over line breaks */
  422. if (sel.type == SEL_RECTANGULAR)
  423. return;
  424. i = tlinelen(sel.nb.y);
  425. if (i < sel.nb.x)
  426. sel.nb.x = i;
  427. if (tlinelen(sel.ne.y) <= sel.ne.x)
  428. sel.ne.x = term.col - 1;
  429. }
  430. int
  431. selected(int x, int y)
  432. {
  433. if (sel.mode == SEL_EMPTY || sel.ob.x == -1 ||
  434. sel.alt != IS_SET(MODE_ALTSCREEN))
  435. return 0;
  436. if (sel.type == SEL_RECTANGULAR)
  437. return BETWEEN(y, sel.nb.y, sel.ne.y)
  438. && BETWEEN(x, sel.nb.x, sel.ne.x);
  439. return BETWEEN(y, sel.nb.y, sel.ne.y)
  440. && (y != sel.nb.y || x >= sel.nb.x)
  441. && (y != sel.ne.y || x <= sel.ne.x);
  442. }
  443. void
  444. selsnap(int *x, int *y, int direction)
  445. {
  446. int newx, newy, xt, yt;
  447. int delim, prevdelim;
  448. Glyph *gp, *prevgp;
  449. switch (sel.snap) {
  450. case SNAP_WORD:
  451. /*
  452. * Snap around if the word wraps around at the end or
  453. * beginning of a line.
  454. */
  455. prevgp = &term.line[*y][*x];
  456. prevdelim = ISDELIM(prevgp->u);
  457. for (;;) {
  458. newx = *x + direction;
  459. newy = *y;
  460. if (!BETWEEN(newx, 0, term.col - 1)) {
  461. newy += direction;
  462. newx = (newx + term.col) % term.col;
  463. if (!BETWEEN(newy, 0, term.row - 1))
  464. break;
  465. if (direction > 0)
  466. yt = *y, xt = *x;
  467. else
  468. yt = newy, xt = newx;
  469. if (!(term.line[yt][xt].mode & ATTR_WRAP))
  470. break;
  471. }
  472. if (newx >= tlinelen(newy))
  473. break;
  474. gp = &term.line[newy][newx];
  475. delim = ISDELIM(gp->u);
  476. if (!(gp->mode & ATTR_WDUMMY) && (delim != prevdelim
  477. || (delim && gp->u != prevgp->u)))
  478. break;
  479. *x = newx;
  480. *y = newy;
  481. prevgp = gp;
  482. prevdelim = delim;
  483. }
  484. break;
  485. case SNAP_LINE:
  486. /*
  487. * Snap around if the the previous line or the current one
  488. * has set ATTR_WRAP at its end. Then the whole next or
  489. * previous line will be selected.
  490. */
  491. *x = (direction < 0) ? 0 : term.col - 1;
  492. if (direction < 0) {
  493. for (; *y > 0; *y += direction) {
  494. if (!(term.line[*y-1][term.col-1].mode
  495. & ATTR_WRAP)) {
  496. break;
  497. }
  498. }
  499. } else if (direction > 0) {
  500. for (; *y < term.row-1; *y += direction) {
  501. if (!(term.line[*y][term.col-1].mode
  502. & ATTR_WRAP)) {
  503. break;
  504. }
  505. }
  506. }
  507. break;
  508. }
  509. }
  510. char *
  511. getsel(void)
  512. {
  513. char *str, *ptr;
  514. int y, bufsize, lastx, linelen;
  515. Glyph *gp, *last;
  516. if (sel.ob.x == -1)
  517. return NULL;
  518. bufsize = (term.col+1) * (sel.ne.y-sel.nb.y+1) * UTF_SIZ;
  519. ptr = str = xmalloc(bufsize);
  520. /* append every set & selected glyph to the selection */
  521. for (y = sel.nb.y; y <= sel.ne.y; y++) {
  522. if ((linelen = tlinelen(y)) == 0) {
  523. *ptr++ = '\n';
  524. continue;
  525. }
  526. if (sel.type == SEL_RECTANGULAR) {
  527. gp = &term.line[y][sel.nb.x];
  528. lastx = sel.ne.x;
  529. } else {
  530. gp = &term.line[y][sel.nb.y == y ? sel.nb.x : 0];
  531. lastx = (sel.ne.y == y) ? sel.ne.x : term.col-1;
  532. }
  533. last = &term.line[y][MIN(lastx, linelen-1)];
  534. while (last >= gp && last->u == ' ')
  535. --last;
  536. for ( ; gp <= last; ++gp) {
  537. if (gp->mode & ATTR_WDUMMY)
  538. continue;
  539. ptr += utf8encode(gp->u, ptr);
  540. }
  541. /*
  542. * Copy and pasting of line endings is inconsistent
  543. * in the inconsistent terminal and GUI world.
  544. * The best solution seems like to produce '\n' when
  545. * something is copied from st and convert '\n' to
  546. * '\r', when something to be pasted is received by
  547. * st.
  548. * FIXME: Fix the computer world.
  549. */
  550. if ((y < sel.ne.y || lastx >= linelen) &&
  551. (!(last->mode & ATTR_WRAP) || sel.type == SEL_RECTANGULAR))
  552. *ptr++ = '\n';
  553. }
  554. *ptr = 0;
  555. return str;
  556. }
  557. void
  558. selclear(void)
  559. {
  560. if (sel.ob.x == -1)
  561. return;
  562. sel.mode = SEL_IDLE;
  563. sel.ob.x = -1;
  564. tsetdirt(sel.nb.y, sel.ne.y);
  565. }
  566. void
  567. die(const char *errstr, ...)
  568. {
  569. va_list ap;
  570. va_start(ap, errstr);
  571. vfprintf(stderr, errstr, ap);
  572. va_end(ap);
  573. exit(1);
  574. }
  575. void
  576. execsh(char *cmd, char **args)
  577. {
  578. char *sh, *prog, *arg;
  579. const struct passwd *pw;
  580. errno = 0;
  581. if ((pw = getpwuid(getuid())) == NULL) {
  582. if (errno)
  583. die("getpwuid: %s\n", strerror(errno));
  584. else
  585. die("who are you?\n");
  586. }
  587. if ((sh = getenv("SHELL")) == NULL)
  588. sh = (pw->pw_shell[0]) ? pw->pw_shell : cmd;
  589. if (args) {
  590. prog = args[0];
  591. arg = NULL;
  592. } else if (scroll) {
  593. prog = scroll;
  594. arg = utmp ? utmp : sh;
  595. } else if (utmp) {
  596. prog = utmp;
  597. arg = NULL;
  598. } else {
  599. prog = sh;
  600. arg = NULL;
  601. }
  602. DEFAULT(args, ((char *[]) {prog, arg, NULL}));
  603. unsetenv("COLUMNS");
  604. unsetenv("LINES");
  605. unsetenv("TERMCAP");
  606. setenv("LOGNAME", pw->pw_name, 1);
  607. setenv("USER", pw->pw_name, 1);
  608. setenv("SHELL", sh, 1);
  609. setenv("HOME", pw->pw_dir, 1);
  610. setenv("TERM", termname, 1);
  611. signal(SIGCHLD, SIG_DFL);
  612. signal(SIGHUP, SIG_DFL);
  613. signal(SIGINT, SIG_DFL);
  614. signal(SIGQUIT, SIG_DFL);
  615. signal(SIGTERM, SIG_DFL);
  616. signal(SIGALRM, SIG_DFL);
  617. execvp(prog, args);
  618. _exit(1);
  619. }
  620. void
  621. sigchld(int a)
  622. {
  623. int stat;
  624. pid_t p;
  625. if ((p = waitpid(pid, &stat, WNOHANG)) < 0)
  626. die("waiting for pid %hd failed: %s\n", pid, strerror(errno));
  627. if (pid != p)
  628. return;
  629. if (WIFEXITED(stat) && WEXITSTATUS(stat))
  630. die("child exited with status %d\n", WEXITSTATUS(stat));
  631. else if (WIFSIGNALED(stat))
  632. die("child terminated due to signal %d\n", WTERMSIG(stat));
  633. _exit(0);
  634. }
  635. void
  636. stty(char **args)
  637. {
  638. char cmd[_POSIX_ARG_MAX], **p, *q, *s;
  639. size_t n, siz;
  640. if ((n = strlen(stty_args)) > sizeof(cmd)-1)
  641. die("incorrect stty parameters\n");
  642. memcpy(cmd, stty_args, n);
  643. q = cmd + n;
  644. siz = sizeof(cmd) - n;
  645. for (p = args; p && (s = *p); ++p) {
  646. if ((n = strlen(s)) > siz-1)
  647. die("stty parameter length too long\n");
  648. *q++ = ' ';
  649. memcpy(q, s, n);
  650. q += n;
  651. siz -= n + 1;
  652. }
  653. *q = '\0';
  654. if (system(cmd) != 0)
  655. perror("Couldn't call stty");
  656. }
  657. int
  658. ttynew(char *line, char *cmd, char *out, char **args)
  659. {
  660. int m, s;
  661. if (out) {
  662. term.mode |= MODE_PRINT;
  663. iofd = (!strcmp(out, "-")) ?
  664. 1 : open(out, O_WRONLY | O_CREAT, 0666);
  665. if (iofd < 0) {
  666. fprintf(stderr, "Error opening %s:%s\n",
  667. out, strerror(errno));
  668. }
  669. }
  670. if (line) {
  671. if ((cmdfd = open(line, O_RDWR)) < 0)
  672. die("open line '%s' failed: %s\n",
  673. line, strerror(errno));
  674. dup2(cmdfd, 0);
  675. stty(args);
  676. return cmdfd;
  677. }
  678. /* seems to work fine on linux, openbsd and freebsd */
  679. if (openpty(&m, &s, NULL, NULL, NULL) < 0)
  680. die("openpty failed: %s\n", strerror(errno));
  681. switch (pid = fork()) {
  682. case -1:
  683. die("fork failed: %s\n", strerror(errno));
  684. break;
  685. case 0:
  686. close(iofd);
  687. setsid(); /* create a new process group */
  688. dup2(s, 0);
  689. dup2(s, 1);
  690. dup2(s, 2);
  691. if (ioctl(s, TIOCSCTTY, NULL) < 0)
  692. die("ioctl TIOCSCTTY failed: %s\n", strerror(errno));
  693. close(s);
  694. close(m);
  695. #ifdef __OpenBSD__
  696. if (pledge("stdio getpw proc exec", NULL) == -1)
  697. die("pledge\n");
  698. #endif
  699. execsh(cmd, args);
  700. break;
  701. default:
  702. #ifdef __OpenBSD__
  703. if (pledge("stdio rpath tty proc", NULL) == -1)
  704. die("pledge\n");
  705. #endif
  706. close(s);
  707. cmdfd = m;
  708. signal(SIGCHLD, sigchld);
  709. break;
  710. }
  711. return cmdfd;
  712. }
  713. size_t
  714. ttyread(void)
  715. {
  716. static char buf[BUFSIZ];
  717. static int buflen = 0;
  718. int ret, written;
  719. /* append read bytes to unprocessed bytes */
  720. ret = read(cmdfd, buf+buflen, LEN(buf)-buflen);
  721. switch (ret) {
  722. case 0:
  723. exit(0);
  724. case -1:
  725. die("couldn't read from shell: %s\n", strerror(errno));
  726. default:
  727. buflen += ret;
  728. written = twrite(buf, buflen, 0);
  729. buflen -= written;
  730. /* keep any incomplete UTF-8 byte sequence for the next call */
  731. if (buflen > 0)
  732. memmove(buf, buf + written, buflen);
  733. return ret;
  734. }
  735. }
  736. void
  737. ttywrite(const char *s, size_t n, int may_echo)
  738. {
  739. const char *next;
  740. if (may_echo && IS_SET(MODE_ECHO))
  741. twrite(s, n, 1);
  742. if (!IS_SET(MODE_CRLF)) {
  743. ttywriteraw(s, n);
  744. return;
  745. }
  746. /* This is similar to how the kernel handles ONLCR for ttys */
  747. while (n > 0) {
  748. if (*s == '\r') {
  749. next = s + 1;
  750. ttywriteraw("\r\n", 2);
  751. } else {
  752. next = memchr(s, '\r', n);
  753. DEFAULT(next, s + n);
  754. ttywriteraw(s, next - s);
  755. }
  756. n -= next - s;
  757. s = next;
  758. }
  759. }
  760. void
  761. ttywriteraw(const char *s, size_t n)
  762. {
  763. fd_set wfd, rfd;
  764. ssize_t r;
  765. size_t lim = 256;
  766. /*
  767. * Remember that we are using a pty, which might be a modem line.
  768. * Writing too much will clog the line. That's why we are doing this
  769. * dance.
  770. * FIXME: Migrate the world to Plan 9.
  771. */
  772. while (n > 0) {
  773. FD_ZERO(&wfd);
  774. FD_ZERO(&rfd);
  775. FD_SET(cmdfd, &wfd);
  776. FD_SET(cmdfd, &rfd);
  777. /* Check if we can write. */
  778. if (pselect(cmdfd+1, &rfd, &wfd, NULL, NULL, NULL) < 0) {
  779. if (errno == EINTR)
  780. continue;
  781. die("select failed: %s\n", strerror(errno));
  782. }
  783. if (FD_ISSET(cmdfd, &wfd)) {
  784. /*
  785. * Only write the bytes written by ttywrite() or the
  786. * default of 256. This seems to be a reasonable value
  787. * for a serial line. Bigger values might clog the I/O.
  788. */
  789. if ((r = write(cmdfd, s, (n < lim)? n : lim)) < 0)
  790. goto write_error;
  791. if (r < n) {
  792. /*
  793. * We weren't able to write out everything.
  794. * This means the buffer is getting full
  795. * again. Empty it.
  796. */
  797. if (n < lim)
  798. lim = ttyread();
  799. n -= r;
  800. s += r;
  801. } else {
  802. /* All bytes have been written. */
  803. break;
  804. }
  805. }
  806. if (FD_ISSET(cmdfd, &rfd))
  807. lim = ttyread();
  808. }
  809. return;
  810. write_error:
  811. die("write error on tty: %s\n", strerror(errno));
  812. }
  813. void
  814. ttyresize(int tw, int th)
  815. {
  816. struct winsize w;
  817. w.ws_row = term.row;
  818. w.ws_col = term.col;
  819. w.ws_xpixel = tw;
  820. w.ws_ypixel = th;
  821. if (ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
  822. fprintf(stderr, "Couldn't set window size: %s\n", strerror(errno));
  823. }
  824. void
  825. ttyhangup()
  826. {
  827. /* Send SIGHUP to shell */
  828. kill(pid, SIGHUP);
  829. }
  830. int
  831. tattrset(int attr)
  832. {
  833. int i, j;
  834. for (i = 0; i < term.row-1; i++) {
  835. for (j = 0; j < term.col-1; j++) {
  836. if (term.line[i][j].mode & attr)
  837. return 1;
  838. }
  839. }
  840. return 0;
  841. }
  842. void
  843. tsetdirt(int top, int bot)
  844. {
  845. int i;
  846. LIMIT(top, 0, term.row-1);
  847. LIMIT(bot, 0, term.row-1);
  848. for (i = top; i <= bot; i++)
  849. term.dirty[i] = 1;
  850. }
  851. void
  852. tsetdirtattr(int attr)
  853. {
  854. int i, j;
  855. for (i = 0; i < term.row-1; i++) {
  856. for (j = 0; j < term.col-1; j++) {
  857. if (term.line[i][j].mode & attr) {
  858. tsetdirt(i, i);
  859. break;
  860. }
  861. }
  862. }
  863. }
  864. void
  865. tfulldirt(void)
  866. {
  867. tsetdirt(0, term.row-1);
  868. }
  869. void
  870. tcursor(int mode)
  871. {
  872. static TCursor c[2];
  873. int alt = IS_SET(MODE_ALTSCREEN);
  874. if (mode == CURSOR_SAVE) {
  875. c[alt] = term.c;
  876. } else if (mode == CURSOR_LOAD) {
  877. term.c = c[alt];
  878. tmoveto(c[alt].x, c[alt].y);
  879. }
  880. }
  881. void
  882. treset(void)
  883. {
  884. uint i;
  885. term.c = (TCursor){{
  886. .mode = ATTR_NULL,
  887. .fg = defaultfg,
  888. .bg = defaultbg
  889. }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
  890. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  891. for (i = tabspaces; i < term.col; i += tabspaces)
  892. term.tabs[i] = 1;
  893. term.top = 0;
  894. term.bot = term.row - 1;
  895. term.mode = MODE_WRAP|MODE_UTF8;
  896. memset(term.trantbl, CS_USA, sizeof(term.trantbl));
  897. term.charset = 0;
  898. for (i = 0; i < 2; i++) {
  899. tmoveto(0, 0);
  900. tcursor(CURSOR_SAVE);
  901. tclearregion(0, 0, term.col-1, term.row-1);
  902. tswapscreen();
  903. }
  904. }
  905. void
  906. tnew(int col, int row)
  907. {
  908. term = (Term){ .c = { .attr = { .fg = defaultfg, .bg = defaultbg } } };
  909. tresize(col, row);
  910. treset();
  911. }
  912. void
  913. tswapscreen(void)
  914. {
  915. Line *tmp = term.line;
  916. term.line = term.alt;
  917. term.alt = tmp;
  918. term.mode ^= MODE_ALTSCREEN;
  919. tfulldirt();
  920. }
  921. void
  922. tscrolldown(int orig, int n)
  923. {
  924. int i;
  925. Line temp;
  926. LIMIT(n, 0, term.bot-orig+1);
  927. tsetdirt(orig, term.bot-n);
  928. tclearregion(0, term.bot-n+1, term.col-1, term.bot);
  929. for (i = term.bot; i >= orig+n; i--) {
  930. temp = term.line[i];
  931. term.line[i] = term.line[i-n];
  932. term.line[i-n] = temp;
  933. }
  934. selscroll(orig, n);
  935. }
  936. void
  937. tscrollup(int orig, int n)
  938. {
  939. int i;
  940. Line temp;
  941. LIMIT(n, 0, term.bot-orig+1);
  942. tclearregion(0, orig, term.col-1, orig+n-1);
  943. tsetdirt(orig+n, term.bot);
  944. for (i = orig; i <= term.bot-n; i++) {
  945. temp = term.line[i];
  946. term.line[i] = term.line[i+n];
  947. term.line[i+n] = temp;
  948. }
  949. selscroll(orig, -n);
  950. }
  951. void
  952. selscroll(int orig, int n)
  953. {
  954. if (sel.ob.x == -1)
  955. return;
  956. if (BETWEEN(sel.nb.y, orig, term.bot) != BETWEEN(sel.ne.y, orig, term.bot)) {
  957. selclear();
  958. } else if (BETWEEN(sel.nb.y, orig, term.bot)) {
  959. sel.ob.y += n;
  960. sel.oe.y += n;
  961. if (sel.ob.y < term.top || sel.ob.y > term.bot ||
  962. sel.oe.y < term.top || sel.oe.y > term.bot) {
  963. selclear();
  964. } else {
  965. selnormalize();
  966. }
  967. }
  968. }
  969. void
  970. tnewline(int first_col)
  971. {
  972. int y = term.c.y;
  973. if (y == term.bot) {
  974. tscrollup(term.top, 1);
  975. } else {
  976. y++;
  977. }
  978. tmoveto(first_col ? 0 : term.c.x, y);
  979. }
  980. void
  981. csiparse(void)
  982. {
  983. char *p = csiescseq.buf, *np;
  984. long int v;
  985. csiescseq.narg = 0;
  986. if (*p == '?') {
  987. csiescseq.priv = 1;
  988. p++;
  989. }
  990. csiescseq.buf[csiescseq.len] = '\0';
  991. while (p < csiescseq.buf+csiescseq.len) {
  992. np = NULL;
  993. v = strtol(p, &np, 10);
  994. if (np == p)
  995. v = 0;
  996. if (v == LONG_MAX || v == LONG_MIN)
  997. v = -1;
  998. csiescseq.arg[csiescseq.narg++] = v;
  999. p = np;
  1000. if (*p != ';' || csiescseq.narg == ESC_ARG_SIZ)
  1001. break;
  1002. p++;
  1003. }
  1004. csiescseq.mode[0] = *p++;
  1005. csiescseq.mode[1] = (p < csiescseq.buf+csiescseq.len) ? *p : '\0';
  1006. }
  1007. /* for absolute user moves, when decom is set */
  1008. void
  1009. tmoveato(int x, int y)
  1010. {
  1011. tmoveto(x, y + ((term.c.state & CURSOR_ORIGIN) ? term.top: 0));
  1012. }
  1013. void
  1014. tmoveto(int x, int y)
  1015. {
  1016. int miny, maxy;
  1017. if (term.c.state & CURSOR_ORIGIN) {
  1018. miny = term.top;
  1019. maxy = term.bot;
  1020. } else {
  1021. miny = 0;
  1022. maxy = term.row - 1;
  1023. }
  1024. term.c.state &= ~CURSOR_WRAPNEXT;
  1025. term.c.x = LIMIT(x, 0, term.col-1);
  1026. term.c.y = LIMIT(y, miny, maxy);
  1027. }
  1028. void
  1029. tsetchar(Rune u, Glyph *attr, int x, int y)
  1030. {
  1031. static char *vt100_0[62] = { /* 0x41 - 0x7e */
  1032. "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
  1033. 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
  1034. 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
  1035. 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
  1036. "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
  1037. "␤", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
  1038. "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
  1039. "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
  1040. };
  1041. /*
  1042. * The table is proudly stolen from rxvt.
  1043. */
  1044. if (term.trantbl[term.charset] == CS_GRAPHIC0 &&
  1045. BETWEEN(u, 0x41, 0x7e) && vt100_0[u - 0x41])
  1046. utf8decode(vt100_0[u - 0x41], &u, UTF_SIZ);
  1047. if (term.line[y][x].mode & ATTR_WIDE) {
  1048. if (x+1 < term.col) {
  1049. term.line[y][x+1].u = ' ';
  1050. term.line[y][x+1].mode &= ~ATTR_WDUMMY;
  1051. }
  1052. } else if (term.line[y][x].mode & ATTR_WDUMMY) {
  1053. term.line[y][x-1].u = ' ';
  1054. term.line[y][x-1].mode &= ~ATTR_WIDE;
  1055. }
  1056. term.dirty[y] = 1;
  1057. term.line[y][x] = *attr;
  1058. term.line[y][x].u = u;
  1059. }
  1060. void
  1061. tclearregion(int x1, int y1, int x2, int y2)
  1062. {
  1063. int x, y, temp;
  1064. Glyph *gp;
  1065. if (x1 > x2)
  1066. temp = x1, x1 = x2, x2 = temp;
  1067. if (y1 > y2)
  1068. temp = y1, y1 = y2, y2 = temp;
  1069. LIMIT(x1, 0, term.col-1);
  1070. LIMIT(x2, 0, term.col-1);
  1071. LIMIT(y1, 0, term.row-1);
  1072. LIMIT(y2, 0, term.row-1);
  1073. for (y = y1; y <= y2; y++) {
  1074. term.dirty[y] = 1;
  1075. for (x = x1; x <= x2; x++) {
  1076. gp = &term.line[y][x];
  1077. if (selected(x, y))
  1078. selclear();
  1079. gp->fg = term.c.attr.fg;
  1080. gp->bg = term.c.attr.bg;
  1081. gp->mode = 0;
  1082. gp->u = ' ';
  1083. }
  1084. }
  1085. }
  1086. void
  1087. tdeletechar(int n)
  1088. {
  1089. int dst, src, size;
  1090. Glyph *line;
  1091. LIMIT(n, 0, term.col - term.c.x);
  1092. dst = term.c.x;
  1093. src = term.c.x + n;
  1094. size = term.col - src;
  1095. line = term.line[term.c.y];
  1096. memmove(&line[dst], &line[src], size * sizeof(Glyph));
  1097. tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
  1098. }
  1099. void
  1100. tinsertblank(int n)
  1101. {
  1102. int dst, src, size;
  1103. Glyph *line;
  1104. LIMIT(n, 0, term.col - term.c.x);
  1105. dst = term.c.x + n;
  1106. src = term.c.x;
  1107. size = term.col - dst;
  1108. line = term.line[term.c.y];
  1109. memmove(&line[dst], &line[src], size * sizeof(Glyph));
  1110. tclearregion(src, term.c.y, dst - 1, term.c.y);
  1111. }
  1112. void
  1113. tinsertblankline(int n)
  1114. {
  1115. if (BETWEEN(term.c.y, term.top, term.bot))
  1116. tscrolldown(term.c.y, n);
  1117. }
  1118. void
  1119. tdeleteline(int n)
  1120. {
  1121. if (BETWEEN(term.c.y, term.top, term.bot))
  1122. tscrollup(term.c.y, n);
  1123. }
  1124. int32_t
  1125. tdefcolor(int *attr, int *npar, int l)
  1126. {
  1127. int32_t idx = -1;
  1128. uint r, g, b;
  1129. switch (attr[*npar + 1]) {
  1130. case 2: /* direct color in RGB space */
  1131. if (*npar + 4 >= l) {
  1132. fprintf(stderr,
  1133. "erresc(38): Incorrect number of parameters (%d)\n",
  1134. *npar);
  1135. break;
  1136. }
  1137. r = attr[*npar + 2];
  1138. g = attr[*npar + 3];
  1139. b = attr[*npar + 4];
  1140. *npar += 4;
  1141. if (!BETWEEN(r, 0, 255) || !BETWEEN(g, 0, 255) || !BETWEEN(b, 0, 255))
  1142. fprintf(stderr, "erresc: bad rgb color (%u,%u,%u)\n",
  1143. r, g, b);
  1144. else
  1145. idx = TRUECOLOR(r, g, b);
  1146. break;
  1147. case 5: /* indexed color */
  1148. if (*npar + 2 >= l) {
  1149. fprintf(stderr,
  1150. "erresc(38): Incorrect number of parameters (%d)\n",
  1151. *npar);
  1152. break;
  1153. }
  1154. *npar += 2;
  1155. if (!BETWEEN(attr[*npar], 0, 255))
  1156. fprintf(stderr, "erresc: bad fgcolor %d\n", attr[*npar]);
  1157. else
  1158. idx = attr[*npar];
  1159. break;
  1160. case 0: /* implemented defined (only foreground) */
  1161. case 1: /* transparent */
  1162. case 3: /* direct color in CMY space */
  1163. case 4: /* direct color in CMYK space */
  1164. default:
  1165. fprintf(stderr,
  1166. "erresc(38): gfx attr %d unknown\n", attr[*npar]);
  1167. break;
  1168. }
  1169. return idx;
  1170. }
  1171. void
  1172. tsetattr(int *attr, int l)
  1173. {
  1174. int i;
  1175. int32_t idx;
  1176. for (i = 0; i < l; i++) {
  1177. switch (attr[i]) {
  1178. case 0:
  1179. term.c.attr.mode &= ~(
  1180. ATTR_BOLD |
  1181. ATTR_FAINT |
  1182. ATTR_ITALIC |
  1183. ATTR_UNDERLINE |
  1184. ATTR_BLINK |
  1185. ATTR_REVERSE |
  1186. ATTR_INVISIBLE |
  1187. ATTR_STRUCK );
  1188. term.c.attr.fg = defaultfg;
  1189. term.c.attr.bg = defaultbg;
  1190. break;
  1191. case 1:
  1192. term.c.attr.mode |= ATTR_BOLD;
  1193. break;
  1194. case 2:
  1195. term.c.attr.mode |= ATTR_FAINT;
  1196. break;
  1197. case 3:
  1198. term.c.attr.mode |= ATTR_ITALIC;
  1199. break;
  1200. case 4:
  1201. term.c.attr.mode |= ATTR_UNDERLINE;
  1202. break;
  1203. case 5: /* slow blink */
  1204. /* FALLTHROUGH */
  1205. case 6: /* rapid blink */
  1206. term.c.attr.mode |= ATTR_BLINK;
  1207. break;
  1208. case 7:
  1209. term.c.attr.mode |= ATTR_REVERSE;
  1210. break;
  1211. case 8:
  1212. term.c.attr.mode |= ATTR_INVISIBLE;
  1213. break;
  1214. case 9:
  1215. term.c.attr.mode |= ATTR_STRUCK;
  1216. break;
  1217. case 22:
  1218. term.c.attr.mode &= ~(ATTR_BOLD | ATTR_FAINT);
  1219. break;
  1220. case 23:
  1221. term.c.attr.mode &= ~ATTR_ITALIC;
  1222. break;
  1223. case 24:
  1224. term.c.attr.mode &= ~ATTR_UNDERLINE;
  1225. break;
  1226. case 25:
  1227. term.c.attr.mode &= ~ATTR_BLINK;
  1228. break;
  1229. case 27:
  1230. term.c.attr.mode &= ~ATTR_REVERSE;
  1231. break;
  1232. case 28:
  1233. term.c.attr.mode &= ~ATTR_INVISIBLE;
  1234. break;
  1235. case 29:
  1236. term.c.attr.mode &= ~ATTR_STRUCK;
  1237. break;
  1238. case 38:
  1239. if ((idx = tdefcolor(attr, &i, l)) >= 0)
  1240. term.c.attr.fg = idx;
  1241. break;
  1242. case 39:
  1243. term.c.attr.fg = defaultfg;
  1244. break;
  1245. case 48:
  1246. if ((idx = tdefcolor(attr, &i, l)) >= 0)
  1247. term.c.attr.bg = idx;
  1248. break;
  1249. case 49:
  1250. term.c.attr.bg = defaultbg;
  1251. break;
  1252. default:
  1253. if (BETWEEN(attr[i], 30, 37)) {
  1254. term.c.attr.fg = attr[i] - 30;
  1255. } else if (BETWEEN(attr[i], 40, 47)) {
  1256. term.c.attr.bg = attr[i] - 40;
  1257. } else if (BETWEEN(attr[i], 90, 97)) {
  1258. term.c.attr.fg = attr[i] - 90 + 8;
  1259. } else if (BETWEEN(attr[i], 100, 107)) {
  1260. term.c.attr.bg = attr[i] - 100 + 8;
  1261. } else {
  1262. fprintf(stderr,
  1263. "erresc(default): gfx attr %d unknown\n",
  1264. attr[i]);
  1265. csidump();
  1266. }
  1267. break;
  1268. }
  1269. }
  1270. }
  1271. void
  1272. tsetscroll(int t, int b)
  1273. {
  1274. int temp;
  1275. LIMIT(t, 0, term.row-1);
  1276. LIMIT(b, 0, term.row-1);
  1277. if (t > b) {
  1278. temp = t;
  1279. t = b;
  1280. b = temp;
  1281. }
  1282. term.top = t;
  1283. term.bot = b;
  1284. }
  1285. void
  1286. tsetmode(int priv, int set, int *args, int narg)
  1287. {
  1288. int alt, *lim;
  1289. for (lim = args + narg; args < lim; ++args) {
  1290. if (priv) {
  1291. switch (*args) {
  1292. case 1: /* DECCKM -- Cursor key */
  1293. xsetmode(set, MODE_APPCURSOR);
  1294. break;
  1295. case 5: /* DECSCNM -- Reverse video */
  1296. xsetmode(set, MODE_REVERSE);
  1297. break;
  1298. case 6: /* DECOM -- Origin */
  1299. MODBIT(term.c.state, set, CURSOR_ORIGIN);
  1300. tmoveato(0, 0);
  1301. break;
  1302. case 7: /* DECAWM -- Auto wrap */
  1303. MODBIT(term.mode, set, MODE_WRAP);
  1304. break;
  1305. case 0: /* Error (IGNORED) */
  1306. case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
  1307. case 3: /* DECCOLM -- Column (IGNORED) */
  1308. case 4: /* DECSCLM -- Scroll (IGNORED) */
  1309. case 8: /* DECARM -- Auto repeat (IGNORED) */
  1310. case 18: /* DECPFF -- Printer feed (IGNORED) */
  1311. case 19: /* DECPEX -- Printer extent (IGNORED) */
  1312. case 42: /* DECNRCM -- National characters (IGNORED) */
  1313. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  1314. break;
  1315. case 25: /* DECTCEM -- Text Cursor Enable Mode */
  1316. xsetmode(!set, MODE_HIDE);
  1317. break;
  1318. case 9: /* X10 mouse compatibility mode */
  1319. xsetpointermotion(0);
  1320. xsetmode(0, MODE_MOUSE);
  1321. xsetmode(set, MODE_MOUSEX10);
  1322. break;
  1323. case 1000: /* 1000: report button press */
  1324. xsetpointermotion(0);
  1325. xsetmode(0, MODE_MOUSE);
  1326. xsetmode(set, MODE_MOUSEBTN);
  1327. break;
  1328. case 1002: /* 1002: report motion on button press */
  1329. xsetpointermotion(0);
  1330. xsetmode(0, MODE_MOUSE);
  1331. xsetmode(set, MODE_MOUSEMOTION);
  1332. break;
  1333. case 1003: /* 1003: enable all mouse motions */
  1334. xsetpointermotion(set);
  1335. xsetmode(0, MODE_MOUSE);
  1336. xsetmode(set, MODE_MOUSEMANY);
  1337. break;
  1338. case 1004: /* 1004: send focus events to tty */
  1339. xsetmode(set, MODE_FOCUS);
  1340. break;
  1341. case 1006: /* 1006: extended reporting mode */
  1342. xsetmode(set, MODE_MOUSESGR);
  1343. break;
  1344. case 1034:
  1345. xsetmode(set, MODE_8BIT);
  1346. break;
  1347. case 1049: /* swap screen & set/restore cursor as xterm */
  1348. if (!allowaltscreen)
  1349. break;
  1350. tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
  1351. /* FALLTHROUGH */
  1352. case 47: /* swap screen */
  1353. case 1047:
  1354. if (!allowaltscreen)
  1355. break;
  1356. alt = IS_SET(MODE_ALTSCREEN);
  1357. if (alt) {
  1358. tclearregion(0, 0, term.col-1,
  1359. term.row-1);
  1360. }
  1361. if (set ^ alt) /* set is always 1 or 0 */
  1362. tswapscreen();
  1363. if (*args != 1049)
  1364. break;
  1365. /* FALLTHROUGH */
  1366. case 1048:
  1367. tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
  1368. break;
  1369. case 2004: /* 2004: bracketed paste mode */
  1370. xsetmode(set, MODE_BRCKTPASTE);
  1371. break;
  1372. /* Not implemented mouse modes. See comments there. */
  1373. case 1001: /* mouse highlight mode; can hang the
  1374. terminal by design when implemented. */
  1375. case 1005: /* UTF-8 mouse mode; will confuse
  1376. applications not supporting UTF-8
  1377. and luit. */
  1378. case 1015: /* urxvt mangled mouse mode; incompatible
  1379. and can be mistaken for other control
  1380. codes. */
  1381. break;
  1382. default:
  1383. fprintf(stderr,
  1384. "erresc: unknown private set/reset mode %d\n",
  1385. *args);
  1386. break;
  1387. }
  1388. } else {
  1389. switch (*args) {
  1390. case 0: /* Error (IGNORED) */
  1391. break;
  1392. case 2:
  1393. xsetmode(set, MODE_KBDLOCK);
  1394. break;
  1395. case 4: /* IRM -- Insertion-replacement */
  1396. MODBIT(term.mode, set, MODE_INSERT);
  1397. break;
  1398. case 12: /* SRM -- Send/Receive */
  1399. MODBIT(term.mode, !set, MODE_ECHO);
  1400. break;
  1401. case 20: /* LNM -- Linefeed/new line */
  1402. MODBIT(term.mode, set, MODE_CRLF);
  1403. break;
  1404. default:
  1405. fprintf(stderr,
  1406. "erresc: unknown set/reset mode %d\n",
  1407. *args);
  1408. break;
  1409. }
  1410. }
  1411. }
  1412. }
  1413. void
  1414. csihandle(void)
  1415. {
  1416. char buf[40];
  1417. int len;
  1418. switch (csiescseq.mode[0]) {
  1419. default:
  1420. unknown:
  1421. fprintf(stderr, "erresc: unknown csi ");
  1422. csidump();
  1423. /* die(""); */
  1424. break;
  1425. case '@': /* ICH -- Insert <n> blank char */
  1426. DEFAULT(csiescseq.arg[0], 1);
  1427. tinsertblank(csiescseq.arg[0]);
  1428. break;
  1429. case 'A': /* CUU -- Cursor <n> Up */
  1430. DEFAULT(csiescseq.arg[0], 1);
  1431. tmoveto(term.c.x, term.c.y-csiescseq.arg[0]);
  1432. break;
  1433. case 'B': /* CUD -- Cursor <n> Down */
  1434. case 'e': /* VPR --Cursor <n> Down */
  1435. DEFAULT(csiescseq.arg[0], 1);
  1436. tmoveto(term.c.x, term.c.y+csiescseq.arg[0]);
  1437. break;
  1438. case 'i': /* MC -- Media Copy */
  1439. switch (csiescseq.arg[0]) {
  1440. case 0:
  1441. tdump();
  1442. break;
  1443. case 1:
  1444. tdumpline(term.c.y);
  1445. break;
  1446. case 2:
  1447. tdumpsel();
  1448. break;
  1449. case 4:
  1450. term.mode &= ~MODE_PRINT;
  1451. break;
  1452. case 5:
  1453. term.mode |= MODE_PRINT;
  1454. break;
  1455. }
  1456. break;
  1457. case 'c': /* DA -- Device Attributes */
  1458. if (csiescseq.arg[0] == 0)
  1459. ttywrite(vtiden, strlen(vtiden), 0);
  1460. break;
  1461. case 'C': /* CUF -- Cursor <n> Forward */
  1462. case 'a': /* HPR -- Cursor <n> Forward */
  1463. DEFAULT(csiescseq.arg[0], 1);
  1464. tmoveto(term.c.x+csiescseq.arg[0], term.c.y);
  1465. break;
  1466. case 'D': /* CUB -- Cursor <n> Backward */
  1467. DEFAULT(csiescseq.arg[0], 1);
  1468. tmoveto(term.c.x-csiescseq.arg[0], term.c.y);
  1469. break;
  1470. case 'E': /* CNL -- Cursor <n> Down and first col */
  1471. DEFAULT(csiescseq.arg[0], 1);
  1472. tmoveto(0, term.c.y+csiescseq.arg[0]);
  1473. break;
  1474. case 'F': /* CPL -- Cursor <n> Up and first col */
  1475. DEFAULT(csiescseq.arg[0], 1);
  1476. tmoveto(0, term.c.y-csiescseq.arg[0]);
  1477. break;
  1478. case 'g': /* TBC -- Tabulation clear */
  1479. switch (csiescseq.arg[0]) {
  1480. case 0: /* clear current tab stop */
  1481. term.tabs[term.c.x] = 0;
  1482. break;
  1483. case 3: /* clear all the tabs */
  1484. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  1485. break;
  1486. default:
  1487. goto unknown;
  1488. }
  1489. break;
  1490. case 'G': /* CHA -- Move to <col> */
  1491. case '`': /* HPA */
  1492. DEFAULT(csiescseq.arg[0], 1);
  1493. tmoveto(csiescseq.arg[0]-1, term.c.y);
  1494. break;
  1495. case 'H': /* CUP -- Move to <row> <col> */
  1496. case 'f': /* HVP */
  1497. DEFAULT(csiescseq.arg[0], 1);
  1498. DEFAULT(csiescseq.arg[1], 1);
  1499. tmoveato(csiescseq.arg[1]-1, csiescseq.arg[0]-1);
  1500. break;
  1501. case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
  1502. DEFAULT(csiescseq.arg[0], 1);
  1503. tputtab(csiescseq.arg[0]);
  1504. break;
  1505. case 'J': /* ED -- Clear screen */
  1506. switch (csiescseq.arg[0]) {
  1507. case 0: /* below */
  1508. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1509. if (term.c.y < term.row-1) {
  1510. tclearregion(0, term.c.y+1, term.col-1,
  1511. term.row-1);
  1512. }
  1513. break;
  1514. case 1: /* above */
  1515. if (term.c.y > 1)
  1516. tclearregion(0, 0, term.col-1, term.c.y-1);
  1517. tclearregion(0, term.c.y, term.c.x, term.c.y);
  1518. break;
  1519. case 2: /* all */
  1520. tclearregion(0, 0, term.col-1, term.row-1);
  1521. break;
  1522. default:
  1523. goto unknown;
  1524. }
  1525. break;
  1526. case 'K': /* EL -- Clear line */
  1527. switch (csiescseq.arg[0]) {
  1528. case 0: /* right */
  1529. tclearregion(term.c.x, term.c.y, term.col-1,
  1530. term.c.y);
  1531. break;
  1532. case 1: /* left */
  1533. tclearregion(0, term.c.y, term.c.x, term.c.y);
  1534. break;
  1535. case 2: /* all */
  1536. tclearregion(0, term.c.y, term.col-1, term.c.y);
  1537. break;
  1538. }
  1539. break;
  1540. case 'S': /* SU -- Scroll <n> line up */
  1541. DEFAULT(csiescseq.arg[0], 1);
  1542. tscrollup(term.top, csiescseq.arg[0]);
  1543. break;
  1544. case 'T': /* SD -- Scroll <n> line down */
  1545. DEFAULT(csiescseq.arg[0], 1);
  1546. tscrolldown(term.top, csiescseq.arg[0]);
  1547. break;
  1548. case 'L': /* IL -- Insert <n> blank lines */
  1549. DEFAULT(csiescseq.arg[0], 1);
  1550. tinsertblankline(csiescseq.arg[0]);
  1551. break;
  1552. case 'l': /* RM -- Reset Mode */
  1553. tsetmode(csiescseq.priv, 0, csiescseq.arg, csiescseq.narg);
  1554. break;
  1555. case 'M': /* DL -- Delete <n> lines */
  1556. DEFAULT(csiescseq.arg[0], 1);
  1557. tdeleteline(csiescseq.arg[0]);
  1558. break;
  1559. case 'X': /* ECH -- Erase <n> char */
  1560. DEFAULT(csiescseq.arg[0], 1);
  1561. tclearregion(term.c.x, term.c.y,
  1562. term.c.x + csiescseq.arg[0] - 1, term.c.y);
  1563. break;
  1564. case 'P': /* DCH -- Delete <n> char */
  1565. DEFAULT(csiescseq.arg[0], 1);
  1566. tdeletechar(csiescseq.arg[0]);
  1567. break;
  1568. case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
  1569. DEFAULT(csiescseq.arg[0], 1);
  1570. tputtab(-csiescseq.arg[0]);
  1571. break;
  1572. case 'd': /* VPA -- Move to <row> */
  1573. DEFAULT(csiescseq.arg[0], 1);
  1574. tmoveato(term.c.x, csiescseq.arg[0]-1);
  1575. break;
  1576. case 'h': /* SM -- Set terminal mode */
  1577. tsetmode(csiescseq.priv, 1, csiescseq.arg, csiescseq.narg);
  1578. break;
  1579. case 'm': /* SGR -- Terminal attribute (color) */
  1580. tsetattr(csiescseq.arg, csiescseq.narg);
  1581. break;
  1582. case 'n': /* DSR – Device Status Report (cursor position) */
  1583. if (csiescseq.arg[0] == 6) {
  1584. len = snprintf(buf, sizeof(buf),"\033[%i;%iR",
  1585. term.c.y+1, term.c.x+1);
  1586. ttywrite(buf, len, 0);
  1587. }
  1588. break;
  1589. case 'r': /* DECSTBM -- Set Scrolling Region */
  1590. if (csiescseq.priv) {
  1591. goto unknown;
  1592. } else {
  1593. DEFAULT(csiescseq.arg[0], 1);
  1594. DEFAULT(csiescseq.arg[1], term.row);
  1595. tsetscroll(csiescseq.arg[0]-1, csiescseq.arg[1]-1);
  1596. tmoveato(0, 0);
  1597. }
  1598. break;
  1599. case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
  1600. tcursor(CURSOR_SAVE);
  1601. break;
  1602. case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
  1603. tcursor(CURSOR_LOAD);
  1604. break;
  1605. case ' ':
  1606. switch (csiescseq.mode[1]) {
  1607. case 'q': /* DECSCUSR -- Set Cursor Style */
  1608. if (xsetcursor(csiescseq.arg[0]))
  1609. goto unknown;
  1610. break;
  1611. default:
  1612. goto unknown;
  1613. }
  1614. break;
  1615. }
  1616. }
  1617. void
  1618. csidump(void)
  1619. {
  1620. size_t i;
  1621. uint c;
  1622. fprintf(stderr, "ESC[");
  1623. for (i = 0; i < csiescseq.len; i++) {
  1624. c = csiescseq.buf[i] & 0xff;
  1625. if (isprint(c)) {
  1626. putc(c, stderr);
  1627. } else if (c == '\n') {
  1628. fprintf(stderr, "(\\n)");
  1629. } else if (c == '\r') {
  1630. fprintf(stderr, "(\\r)");
  1631. } else if (c == 0x1b) {
  1632. fprintf(stderr, "(\\e)");
  1633. } else {
  1634. fprintf(stderr, "(%02x)", c);
  1635. }
  1636. }
  1637. putc('\n', stderr);
  1638. }
  1639. void
  1640. csireset(void)
  1641. {
  1642. memset(&csiescseq, 0, sizeof(csiescseq));
  1643. }
  1644. void
  1645. strhandle(void)
  1646. {
  1647. char *p = NULL, *dec;
  1648. int j, narg, par;
  1649. term.esc &= ~(ESC_STR_END|ESC_STR);
  1650. strparse();
  1651. par = (narg = strescseq.narg) ? atoi(strescseq.args[0]) : 0;
  1652. switch (strescseq.type) {
  1653. case ']': /* OSC -- Operating System Command */
  1654. switch (par) {
  1655. case 0:
  1656. case 1:
  1657. case 2:
  1658. if (narg > 1)
  1659. xsettitle(strescseq.args[1]);
  1660. return;
  1661. case 52:
  1662. if (narg > 2) {
  1663. dec = base64dec(strescseq.args[2]);
  1664. if (dec) {
  1665. xsetsel(dec);
  1666. xclipcopy();
  1667. } else {
  1668. fprintf(stderr, "erresc: invalid base64\n");
  1669. }
  1670. }
  1671. return;
  1672. case 4: /* color set */
  1673. if (narg < 3)
  1674. break;
  1675. p = strescseq.args[2];
  1676. /* FALLTHROUGH */
  1677. case 104: /* color reset, here p = NULL */
  1678. j = (narg > 1) ? atoi(strescseq.args[1]) : -1;
  1679. if (xsetcolorname(j, p)) {
  1680. if (par == 104 && narg <= 1)
  1681. return; /* color reset without parameter */
  1682. fprintf(stderr, "erresc: invalid color j=%d, p=%s\n",
  1683. j, p ? p : "(null)");
  1684. } else {
  1685. /*
  1686. * TODO if defaultbg color is changed, borders
  1687. * are dirty
  1688. */
  1689. redraw();
  1690. }
  1691. return;
  1692. }
  1693. break;
  1694. case 'k': /* old title set compatibility */
  1695. xsettitle(strescseq.args[0]);
  1696. return;
  1697. case 'P': /* DCS -- Device Control String */
  1698. term.mode |= ESC_DCS;
  1699. case '_': /* APC -- Application Program Command */
  1700. case '^': /* PM -- Privacy Message */
  1701. return;
  1702. }
  1703. fprintf(stderr, "erresc: unknown str ");
  1704. strdump();
  1705. }
  1706. void
  1707. strparse(void)
  1708. {
  1709. int c;
  1710. char *p = strescseq.buf;
  1711. strescseq.narg = 0;
  1712. strescseq.buf[strescseq.len] = '\0';
  1713. if (*p == '\0')
  1714. return;
  1715. while (strescseq.narg < STR_ARG_SIZ) {
  1716. strescseq.args[strescseq.narg++] = p;
  1717. while ((c = *p) != ';' && c != '\0')
  1718. ++p;
  1719. if (c == '\0')
  1720. return;
  1721. *p++ = '\0';
  1722. }
  1723. }
  1724. void
  1725. strdump(void)
  1726. {
  1727. size_t i;
  1728. uint c;
  1729. fprintf(stderr, "ESC%c", strescseq.type);
  1730. for (i = 0; i < strescseq.len; i++) {
  1731. c = strescseq.buf[i] & 0xff;
  1732. if (c == '\0') {
  1733. putc('\n', stderr);
  1734. return;
  1735. } else if (isprint(c)) {
  1736. putc(c, stderr);
  1737. } else if (c == '\n') {
  1738. fprintf(stderr, "(\\n)");
  1739. } else if (c == '\r') {
  1740. fprintf(stderr, "(\\r)");
  1741. } else if (c == 0x1b) {
  1742. fprintf(stderr, "(\\e)");
  1743. } else {
  1744. fprintf(stderr, "(%02x)", c);
  1745. }
  1746. }
  1747. fprintf(stderr, "ESC\\\n");
  1748. }
  1749. void
  1750. strreset(void)
  1751. {
  1752. strescseq = (STREscape){
  1753. .buf = xrealloc(strescseq.buf, STR_BUF_SIZ),
  1754. .siz = STR_BUF_SIZ,
  1755. };
  1756. }
  1757. void
  1758. sendbreak(const Arg *arg)
  1759. {
  1760. if (tcsendbreak(cmdfd, 0))
  1761. perror("Error sending break");
  1762. }
  1763. void
  1764. tprinter(char *s, size_t len)
  1765. {
  1766. if (iofd != -1 && xwrite(iofd, s, len) < 0) {
  1767. perror("Error writing to output file");
  1768. close(iofd);
  1769. iofd = -1;
  1770. }
  1771. }
  1772. void
  1773. toggleprinter(const Arg *arg)
  1774. {
  1775. term.mode ^= MODE_PRINT;
  1776. }
  1777. void
  1778. printscreen(const Arg *arg)
  1779. {
  1780. tdump();
  1781. }
  1782. void
  1783. printsel(const Arg *arg)
  1784. {
  1785. tdumpsel();
  1786. }
  1787. void
  1788. tdumpsel(void)
  1789. {
  1790. char *ptr;
  1791. if ((ptr = getsel())) {
  1792. tprinter(ptr, strlen(ptr));
  1793. free(ptr);
  1794. }
  1795. }
  1796. void
  1797. tdumpline(int n)
  1798. {
  1799. char buf[UTF_SIZ];
  1800. Glyph *bp, *end;
  1801. bp = &term.line[n][0];
  1802. end = &bp[MIN(tlinelen(n), term.col) - 1];
  1803. if (bp != end || bp->u != ' ') {
  1804. for ( ; bp <= end; ++bp)
  1805. tprinter(buf, utf8encode(bp->u, buf));
  1806. }
  1807. tprinter("\n", 1);
  1808. }
  1809. void
  1810. tdump(void)
  1811. {
  1812. int i;
  1813. for (i = 0; i < term.row; ++i)
  1814. tdumpline(i);
  1815. }
  1816. void
  1817. tputtab(int n)
  1818. {
  1819. uint x = term.c.x;
  1820. if (n > 0) {
  1821. while (x < term.col && n--)
  1822. for (++x; x < term.col && !term.tabs[x]; ++x)
  1823. /* nothing */ ;
  1824. } else if (n < 0) {
  1825. while (x > 0 && n++)
  1826. for (--x; x > 0 && !term.tabs[x]; --x)
  1827. /* nothing */ ;
  1828. }
  1829. term.c.x = LIMIT(x, 0, term.col-1);
  1830. }
  1831. void
  1832. tdefutf8(char ascii)
  1833. {
  1834. if (ascii == 'G')
  1835. term.mode |= MODE_UTF8;
  1836. else if (ascii == '@')
  1837. term.mode &= ~MODE_UTF8;
  1838. }
  1839. void
  1840. tdeftran(char ascii)
  1841. {
  1842. static char cs[] = "0B";
  1843. static int vcs[] = {CS_GRAPHIC0, CS_USA};
  1844. char *p;
  1845. if ((p = strchr(cs, ascii)) == NULL) {
  1846. fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
  1847. } else {
  1848. term.trantbl[term.icharset] = vcs[p - cs];
  1849. }
  1850. }
  1851. void
  1852. tdectest(char c)
  1853. {
  1854. int x, y;
  1855. if (c == '8') { /* DEC screen alignment test. */
  1856. for (x = 0; x < term.col; ++x) {
  1857. for (y = 0; y < term.row; ++y)
  1858. tsetchar('E', &term.c.attr, x, y);
  1859. }
  1860. }
  1861. }
  1862. void
  1863. tstrsequence(uchar c)
  1864. {
  1865. strreset();
  1866. switch (c) {
  1867. case 0x90: /* DCS -- Device Control String */
  1868. c = 'P';
  1869. term.esc |= ESC_DCS;
  1870. break;
  1871. case 0x9f: /* APC -- Application Program Command */
  1872. c = '_';
  1873. break;
  1874. case 0x9e: /* PM -- Privacy Message */
  1875. c = '^';
  1876. break;
  1877. case 0x9d: /* OSC -- Operating System Command */
  1878. c = ']';
  1879. break;
  1880. }
  1881. strescseq.type = c;
  1882. term.esc |= ESC_STR;
  1883. }
  1884. void
  1885. tcontrolcode(uchar ascii)
  1886. {
  1887. switch (ascii) {
  1888. case '\t': /* HT */
  1889. tputtab(1);
  1890. return;
  1891. case '\b': /* BS */
  1892. tmoveto(term.c.x-1, term.c.y);
  1893. return;
  1894. case '\r': /* CR */
  1895. tmoveto(0, term.c.y);
  1896. return;
  1897. case '\f': /* LF */
  1898. case '\v': /* VT */
  1899. case '\n': /* LF */
  1900. /* go to first col if the mode is set */
  1901. tnewline(IS_SET(MODE_CRLF));
  1902. return;
  1903. case '\a': /* BEL */
  1904. if (term.esc & ESC_STR_END) {
  1905. /* backwards compatibility to xterm */
  1906. strhandle();
  1907. } else {
  1908. xbell();
  1909. }
  1910. break;
  1911. case '\033': /* ESC */
  1912. csireset();
  1913. term.esc &= ~(ESC_CSI|ESC_ALTCHARSET|ESC_TEST);
  1914. term.esc |= ESC_START;
  1915. return;
  1916. case '\016': /* SO (LS1 -- Locking shift 1) */
  1917. case '\017': /* SI (LS0 -- Locking shift 0) */
  1918. term.charset = 1 - (ascii - '\016');
  1919. return;
  1920. case '\032': /* SUB */
  1921. tsetchar('?', &term.c.attr, term.c.x, term.c.y);
  1922. /* FALLTHROUGH */
  1923. case '\030': /* CAN */
  1924. csireset();
  1925. break;
  1926. case '\005': /* ENQ (IGNORED) */
  1927. case '\000': /* NUL (IGNORED) */
  1928. case '\021': /* XON (IGNORED) */
  1929. case '\023': /* XOFF (IGNORED) */
  1930. case 0177: /* DEL (IGNORED) */
  1931. return;
  1932. case 0x80: /* TODO: PAD */
  1933. case 0x81: /* TODO: HOP */
  1934. case 0x82: /* TODO: BPH */
  1935. case 0x83: /* TODO: NBH */
  1936. case 0x84: /* TODO: IND */
  1937. break;
  1938. case 0x85: /* NEL -- Next line */
  1939. tnewline(1); /* always go to first col */
  1940. break;
  1941. case 0x86: /* TODO: SSA */
  1942. case 0x87: /* TODO: ESA */
  1943. break;
  1944. case 0x88: /* HTS -- Horizontal tab stop */
  1945. term.tabs[term.c.x] = 1;
  1946. break;
  1947. case 0x89: /* TODO: HTJ */
  1948. case 0x8a: /* TODO: VTS */
  1949. case 0x8b: /* TODO: PLD */
  1950. case 0x8c: /* TODO: PLU */
  1951. case 0x8d: /* TODO: RI */
  1952. case 0x8e: /* TODO: SS2 */
  1953. case 0x8f: /* TODO: SS3 */
  1954. case 0x91: /* TODO: PU1 */
  1955. case 0x92: /* TODO: PU2 */
  1956. case 0x93: /* TODO: STS */
  1957. case 0x94: /* TODO: CCH */
  1958. case 0x95: /* TODO: MW */
  1959. case 0x96: /* TODO: SPA */
  1960. case 0x97: /* TODO: EPA */
  1961. case 0x98: /* TODO: SOS */
  1962. case 0x99: /* TODO: SGCI */
  1963. break;
  1964. case 0x9a: /* DECID -- Identify Terminal */
  1965. ttywrite(vtiden, strlen(vtiden), 0);
  1966. break;
  1967. case 0x9b: /* TODO: CSI */
  1968. case 0x9c: /* TODO: ST */
  1969. break;
  1970. case 0x90: /* DCS -- Device Control String */
  1971. case 0x9d: /* OSC -- Operating System Command */
  1972. case 0x9e: /* PM -- Privacy Message */
  1973. case 0x9f: /* APC -- Application Program Command */
  1974. tstrsequence(ascii);
  1975. return;
  1976. }
  1977. /* only CAN, SUB, \a and C1 chars interrupt a sequence */
  1978. term.esc &= ~(ESC_STR_END|ESC_STR);
  1979. }
  1980. /*
  1981. * returns 1 when the sequence is finished and it hasn't to read
  1982. * more characters for this sequence, otherwise 0
  1983. */
  1984. int
  1985. eschandle(uchar ascii)
  1986. {
  1987. switch (ascii) {
  1988. case '[':
  1989. term.esc |= ESC_CSI;
  1990. return 0;
  1991. case '#':
  1992. term.esc |= ESC_TEST;
  1993. return 0;
  1994. case '%':
  1995. term.esc |= ESC_UTF8;
  1996. return 0;
  1997. case 'P': /* DCS -- Device Control String */
  1998. case '_': /* APC -- Application Program Command */
  1999. case '^': /* PM -- Privacy Message */
  2000. case ']': /* OSC -- Operating System Command */
  2001. case 'k': /* old title set compatibility */
  2002. tstrsequence(ascii);
  2003. return 0;
  2004. case 'n': /* LS2 -- Locking shift 2 */
  2005. case 'o': /* LS3 -- Locking shift 3 */
  2006. term.charset = 2 + (ascii - 'n');
  2007. break;
  2008. case '(': /* GZD4 -- set primary charset G0 */
  2009. case ')': /* G1D4 -- set secondary charset G1 */
  2010. case '*': /* G2D4 -- set tertiary charset G2 */
  2011. case '+': /* G3D4 -- set quaternary charset G3 */
  2012. term.icharset = ascii - '(';
  2013. term.esc |= ESC_ALTCHARSET;
  2014. return 0;
  2015. case 'D': /* IND -- Linefeed */
  2016. if (term.c.y == term.bot) {
  2017. tscrollup(term.top, 1);
  2018. } else {
  2019. tmoveto(term.c.x, term.c.y+1);
  2020. }
  2021. break;
  2022. case 'E': /* NEL -- Next line */
  2023. tnewline(1); /* always go to first col */
  2024. break;
  2025. case 'H': /* HTS -- Horizontal tab stop */
  2026. term.tabs[term.c.x] = 1;
  2027. break;
  2028. case 'M': /* RI -- Reverse index */
  2029. if (term.c.y == term.top) {
  2030. tscrolldown(term.top, 1);
  2031. } else {
  2032. tmoveto(term.c.x, term.c.y-1);
  2033. }
  2034. break;
  2035. case 'Z': /* DECID -- Identify Terminal */
  2036. ttywrite(vtiden, strlen(vtiden), 0);
  2037. break;
  2038. case 'c': /* RIS -- Reset to initial state */
  2039. treset();
  2040. resettitle();
  2041. xloadcols();
  2042. break;
  2043. case '=': /* DECPAM -- Application keypad */
  2044. xsetmode(1, MODE_APPKEYPAD);
  2045. break;
  2046. case '>': /* DECPNM -- Normal keypad */
  2047. xsetmode(0, MODE_APPKEYPAD);
  2048. break;
  2049. case '7': /* DECSC -- Save Cursor */
  2050. tcursor(CURSOR_SAVE);
  2051. break;
  2052. case '8': /* DECRC -- Restore Cursor */
  2053. tcursor(CURSOR_LOAD);
  2054. break;
  2055. case '\\': /* ST -- String Terminator */
  2056. if (term.esc & ESC_STR_END)
  2057. strhandle();
  2058. break;
  2059. default:
  2060. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
  2061. (uchar) ascii, isprint(ascii)? ascii:'.');
  2062. break;
  2063. }
  2064. return 1;
  2065. }
  2066. void
  2067. tputc(Rune u)
  2068. {
  2069. char c[UTF_SIZ];
  2070. int control;
  2071. int width, len;
  2072. Glyph *gp;
  2073. control = ISCONTROL(u);
  2074. if (u < 127 || !IS_SET(MODE_UTF8 | MODE_SIXEL)) {
  2075. c[0] = u;
  2076. width = len = 1;
  2077. } else {
  2078. len = utf8encode(u, c);
  2079. if (!control && (width = wcwidth(u)) == -1)
  2080. width = 1;
  2081. }
  2082. if (IS_SET(MODE_PRINT))
  2083. tprinter(c, len);
  2084. /*
  2085. * STR sequence must be checked before anything else
  2086. * because it uses all following characters until it
  2087. * receives a ESC, a SUB, a ST or any other C1 control
  2088. * character.
  2089. */
  2090. if (term.esc & ESC_STR) {
  2091. if (u == '\a' || u == 030 || u == 032 || u == 033 ||
  2092. ISCONTROLC1(u)) {
  2093. term.esc &= ~(ESC_START|ESC_STR|ESC_DCS);
  2094. if (IS_SET(MODE_SIXEL)) {
  2095. /* TODO: render sixel */;
  2096. term.mode &= ~MODE_SIXEL;
  2097. return;
  2098. }
  2099. term.esc |= ESC_STR_END;
  2100. goto check_control_code;
  2101. }
  2102. if (IS_SET(MODE_SIXEL)) {
  2103. /* TODO: implement sixel mode */
  2104. return;
  2105. }
  2106. if (term.esc&ESC_DCS && strescseq.len == 0 && u == 'q')
  2107. term.mode |= MODE_SIXEL;
  2108. if (strescseq.len+len >= strescseq.siz) {
  2109. /*
  2110. * Here is a bug in terminals. If the user never sends
  2111. * some code to stop the str or esc command, then st
  2112. * will stop responding. But this is better than
  2113. * silently failing with unknown characters. At least
  2114. * then users will report back.
  2115. *
  2116. * In the case users ever get fixed, here is the code:
  2117. */
  2118. /*
  2119. * term.esc = 0;
  2120. * strhandle();
  2121. */
  2122. if (strescseq.siz > (SIZE_MAX - UTF_SIZ) / 2)
  2123. return;
  2124. strescseq.siz *= 2;
  2125. strescseq.buf = xrealloc(strescseq.buf, strescseq.siz);
  2126. }
  2127. memmove(&strescseq.buf[strescseq.len], c, len);
  2128. strescseq.len += len;
  2129. return;
  2130. }
  2131. check_control_code:
  2132. /*
  2133. * Actions of control codes must be performed as soon they arrive
  2134. * because they can be embedded inside a control sequence, and
  2135. * they must not cause conflicts with sequences.
  2136. */
  2137. if (control) {
  2138. tcontrolcode(u);
  2139. /*
  2140. * control codes are not shown ever
  2141. */
  2142. return;
  2143. } else if (term.esc & ESC_START) {
  2144. if (term.esc & ESC_CSI) {
  2145. csiescseq.buf[csiescseq.len++] = u;
  2146. if (BETWEEN(u, 0x40, 0x7E)
  2147. || csiescseq.len >= \
  2148. sizeof(csiescseq.buf)-1) {
  2149. term.esc = 0;
  2150. csiparse();
  2151. csihandle();
  2152. }
  2153. return;
  2154. } else if (term.esc & ESC_UTF8) {
  2155. tdefutf8(u);
  2156. } else if (term.esc & ESC_ALTCHARSET) {
  2157. tdeftran(u);
  2158. } else if (term.esc & ESC_TEST) {
  2159. tdectest(u);
  2160. } else {
  2161. if (!eschandle(u))
  2162. return;
  2163. /* sequence already finished */
  2164. }
  2165. term.esc = 0;
  2166. /*
  2167. * All characters which form part of a sequence are not
  2168. * printed
  2169. */
  2170. return;
  2171. }
  2172. if (selected(term.c.x, term.c.y))
  2173. selclear();
  2174. gp = &term.line[term.c.y][term.c.x];
  2175. if (IS_SET(MODE_WRAP) && (term.c.state & CURSOR_WRAPNEXT)) {
  2176. gp->mode |= ATTR_WRAP;
  2177. tnewline(1);
  2178. gp = &term.line[term.c.y][term.c.x];
  2179. }
  2180. if (IS_SET(MODE_INSERT) && term.c.x+width < term.col)
  2181. memmove(gp+width, gp, (term.col - term.c.x - width) * sizeof(Glyph));
  2182. if (term.c.x+width > term.col) {
  2183. tnewline(1);
  2184. gp = &term.line[term.c.y][term.c.x];
  2185. }
  2186. tsetchar(u, &term.c.attr, term.c.x, term.c.y);
  2187. if (width == 2) {
  2188. gp->mode |= ATTR_WIDE;
  2189. if (term.c.x+1 < term.col) {
  2190. gp[1].u = '\0';
  2191. gp[1].mode = ATTR_WDUMMY;
  2192. }
  2193. }
  2194. if (term.c.x+width < term.col) {
  2195. tmoveto(term.c.x+width, term.c.y);
  2196. } else {
  2197. term.c.state |= CURSOR_WRAPNEXT;
  2198. }
  2199. }
  2200. int
  2201. twrite(const char *buf, int buflen, int show_ctrl)
  2202. {
  2203. int charsize;
  2204. Rune u;
  2205. int n;
  2206. for (n = 0; n < buflen; n += charsize) {
  2207. if (IS_SET(MODE_UTF8) && !IS_SET(MODE_SIXEL)) {
  2208. /* process a complete utf8 char */
  2209. charsize = utf8decode(buf + n, &u, buflen - n);
  2210. if (charsize == 0)
  2211. break;
  2212. } else {
  2213. u = buf[n] & 0xFF;
  2214. charsize = 1;
  2215. }
  2216. if (show_ctrl && ISCONTROL(u)) {
  2217. if (u & 0x80) {
  2218. u &= 0x7f;
  2219. tputc('^');
  2220. tputc('[');
  2221. } else if (u != '\n' && u != '\r' && u != '\t') {
  2222. u ^= 0x40;
  2223. tputc('^');
  2224. }
  2225. }
  2226. tputc(u);
  2227. }
  2228. return n;
  2229. }
  2230. void
  2231. tresize(int col, int row)
  2232. {
  2233. int i;
  2234. int minrow = MIN(row, term.row);
  2235. int mincol = MIN(col, term.col);
  2236. int *bp;
  2237. TCursor c;
  2238. if (col < 1 || row < 1) {
  2239. fprintf(stderr,
  2240. "tresize: error resizing to %dx%d\n", col, row);
  2241. return;
  2242. }
  2243. /*
  2244. * slide screen to keep cursor where we expect it -
  2245. * tscrollup would work here, but we can optimize to
  2246. * memmove because we're freeing the earlier lines
  2247. */
  2248. for (i = 0; i <= term.c.y - row; i++) {
  2249. free(term.line[i]);
  2250. free(term.alt[i]);
  2251. }
  2252. /* ensure that both src and dst are not NULL */
  2253. if (i > 0) {
  2254. memmove(term.line, term.line + i, row * sizeof(Line));
  2255. memmove(term.alt, term.alt + i, row * sizeof(Line));
  2256. }
  2257. for (i += row; i < term.row; i++) {
  2258. free(term.line[i]);
  2259. free(term.alt[i]);
  2260. }
  2261. /* resize to new height */
  2262. term.line = xrealloc(term.line, row * sizeof(Line));
  2263. term.alt = xrealloc(term.alt, row * sizeof(Line));
  2264. term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
  2265. term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
  2266. /* resize each row to new width, zero-pad if needed */
  2267. for (i = 0; i < minrow; i++) {
  2268. term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph));
  2269. term.alt[i] = xrealloc(term.alt[i], col * sizeof(Glyph));
  2270. }
  2271. /* allocate any new rows */
  2272. for (/* i = minrow */; i < row; i++) {
  2273. term.line[i] = xmalloc(col * sizeof(Glyph));
  2274. term.alt[i] = xmalloc(col * sizeof(Glyph));
  2275. }
  2276. if (col > term.col) {
  2277. bp = term.tabs + term.col;
  2278. memset(bp, 0, sizeof(*term.tabs) * (col - term.col));
  2279. while (--bp > term.tabs && !*bp)
  2280. /* nothing */ ;
  2281. for (bp += tabspaces; bp < term.tabs + col; bp += tabspaces)
  2282. *bp = 1;
  2283. }
  2284. /* update terminal size */
  2285. term.col = col;
  2286. term.row = row;
  2287. /* reset scrolling region */
  2288. tsetscroll(0, row-1);
  2289. /* make use of the LIMIT in tmoveto */
  2290. tmoveto(term.c.x, term.c.y);
  2291. /* Clearing both screens (it makes dirty all lines) */
  2292. c = term.c;
  2293. for (i = 0; i < 2; i++) {
  2294. if (mincol < col && 0 < minrow) {
  2295. tclearregion(mincol, 0, col - 1, minrow - 1);
  2296. }
  2297. if (0 < col && minrow < row) {
  2298. tclearregion(0, minrow, col - 1, row - 1);
  2299. }
  2300. tswapscreen();
  2301. tcursor(CURSOR_LOAD);
  2302. }
  2303. term.c = c;
  2304. }
  2305. void
  2306. resettitle(void)
  2307. {
  2308. xsettitle(NULL);
  2309. }
  2310. void
  2311. drawregion(int x1, int y1, int x2, int y2)
  2312. {
  2313. int y;
  2314. for (y = y1; y < y2; y++) {
  2315. if (!term.dirty[y])
  2316. continue;
  2317. term.dirty[y] = 0;
  2318. xdrawline(term.line[y], x1, y, x2);
  2319. }
  2320. }
  2321. void
  2322. draw(void)
  2323. {
  2324. int cx = term.c.x, ocx = term.ocx, ocy = term.ocy;
  2325. if (!xstartdraw())
  2326. return;
  2327. /* adjust cursor position */
  2328. LIMIT(term.ocx, 0, term.col-1);
  2329. LIMIT(term.ocy, 0, term.row-1);
  2330. if (term.line[term.ocy][term.ocx].mode & ATTR_WDUMMY)
  2331. term.ocx--;
  2332. if (term.line[term.c.y][cx].mode & ATTR_WDUMMY)
  2333. cx--;
  2334. drawregion(0, 0, term.col, term.row);
  2335. xdrawcursor(cx, term.c.y, term.line[term.c.y][cx],
  2336. term.ocx, term.ocy, term.line[term.ocy][term.ocx]);
  2337. term.ocx = cx;
  2338. term.ocy = term.c.y;
  2339. xfinishdraw();
  2340. if (ocx != term.ocx || ocy != term.ocy)
  2341. xximspot(term.ocx, term.ocy);
  2342. }
  2343. void
  2344. redraw(void)
  2345. {
  2346. tfulldirt();
  2347. draw();
  2348. }