My build of suckless st terminal
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.

2592 lines
54 KiB

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