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.

2589 lines
54 KiB

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