My patched version of suckless' terminal - st.
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.

2599 lines
54 KiB

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