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.

2588 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;
  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. char *dec;
  1657. dec = base64dec(strescseq.args[2]);
  1658. if (dec) {
  1659. xsetsel(dec);
  1660. xclipcopy();
  1661. } else {
  1662. fprintf(stderr, "erresc: invalid base64\n");
  1663. }
  1664. }
  1665. return;
  1666. case 4: /* color set */
  1667. if (narg < 3)
  1668. break;
  1669. p = strescseq.args[2];
  1670. /* FALLTHROUGH */
  1671. case 104: /* color reset, here p = NULL */
  1672. j = (narg > 1) ? atoi(strescseq.args[1]) : -1;
  1673. if (xsetcolorname(j, p)) {
  1674. fprintf(stderr, "erresc: invalid color %s\n", p);
  1675. } else {
  1676. /*
  1677. * TODO if defaultbg color is changed, borders
  1678. * are dirty
  1679. */
  1680. redraw();
  1681. }
  1682. return;
  1683. }
  1684. break;
  1685. case 'k': /* old title set compatibility */
  1686. xsettitle(strescseq.args[0]);
  1687. return;
  1688. case 'P': /* DCS -- Device Control String */
  1689. term.mode |= ESC_DCS;
  1690. case '_': /* APC -- Application Program Command */
  1691. case '^': /* PM -- Privacy Message */
  1692. return;
  1693. }
  1694. fprintf(stderr, "erresc: unknown str ");
  1695. strdump();
  1696. }
  1697. void
  1698. strparse(void)
  1699. {
  1700. int c;
  1701. char *p = strescseq.buf;
  1702. strescseq.narg = 0;
  1703. strescseq.buf[strescseq.len] = '\0';
  1704. if (*p == '\0')
  1705. return;
  1706. while (strescseq.narg < STR_ARG_SIZ) {
  1707. strescseq.args[strescseq.narg++] = p;
  1708. while ((c = *p) != ';' && c != '\0')
  1709. ++p;
  1710. if (c == '\0')
  1711. return;
  1712. *p++ = '\0';
  1713. }
  1714. }
  1715. void
  1716. strdump(void)
  1717. {
  1718. int i;
  1719. uint c;
  1720. fprintf(stderr, "ESC%c", strescseq.type);
  1721. for (i = 0; i < strescseq.len; i++) {
  1722. c = strescseq.buf[i] & 0xff;
  1723. if (c == '\0') {
  1724. putc('\n', stderr);
  1725. return;
  1726. } else if (isprint(c)) {
  1727. putc(c, stderr);
  1728. } else if (c == '\n') {
  1729. fprintf(stderr, "(\\n)");
  1730. } else if (c == '\r') {
  1731. fprintf(stderr, "(\\r)");
  1732. } else if (c == 0x1b) {
  1733. fprintf(stderr, "(\\e)");
  1734. } else {
  1735. fprintf(stderr, "(%02x)", c);
  1736. }
  1737. }
  1738. fprintf(stderr, "ESC\\\n");
  1739. }
  1740. void
  1741. strreset(void)
  1742. {
  1743. memset(&strescseq, 0, sizeof(strescseq));
  1744. }
  1745. void
  1746. sendbreak(const Arg *arg)
  1747. {
  1748. if (tcsendbreak(cmdfd, 0))
  1749. perror("Error sending break");
  1750. }
  1751. void
  1752. tprinter(char *s, size_t len)
  1753. {
  1754. if (iofd != -1 && xwrite(iofd, s, len) < 0) {
  1755. perror("Error writing to output file");
  1756. close(iofd);
  1757. iofd = -1;
  1758. }
  1759. }
  1760. void
  1761. toggleprinter(const Arg *arg)
  1762. {
  1763. term.mode ^= MODE_PRINT;
  1764. }
  1765. void
  1766. printscreen(const Arg *arg)
  1767. {
  1768. tdump();
  1769. }
  1770. void
  1771. printsel(const Arg *arg)
  1772. {
  1773. tdumpsel();
  1774. }
  1775. void
  1776. tdumpsel(void)
  1777. {
  1778. char *ptr;
  1779. if ((ptr = getsel())) {
  1780. tprinter(ptr, strlen(ptr));
  1781. free(ptr);
  1782. }
  1783. }
  1784. void
  1785. tdumpline(int n)
  1786. {
  1787. char buf[UTF_SIZ];
  1788. Glyph *bp, *end;
  1789. bp = &term.line[n][0];
  1790. end = &bp[MIN(tlinelen(n), term.col) - 1];
  1791. if (bp != end || bp->u != ' ') {
  1792. for ( ;bp <= end; ++bp)
  1793. tprinter(buf, utf8encode(bp->u, buf));
  1794. }
  1795. tprinter("\n", 1);
  1796. }
  1797. void
  1798. tdump(void)
  1799. {
  1800. int i;
  1801. for (i = 0; i < term.row; ++i)
  1802. tdumpline(i);
  1803. }
  1804. void
  1805. tputtab(int n)
  1806. {
  1807. uint x = term.c.x;
  1808. if (n > 0) {
  1809. while (x < term.col && n--)
  1810. for (++x; x < term.col && !term.tabs[x]; ++x)
  1811. /* nothing */ ;
  1812. } else if (n < 0) {
  1813. while (x > 0 && n++)
  1814. for (--x; x > 0 && !term.tabs[x]; --x)
  1815. /* nothing */ ;
  1816. }
  1817. term.c.x = LIMIT(x, 0, term.col-1);
  1818. }
  1819. void
  1820. tdefutf8(char ascii)
  1821. {
  1822. if (ascii == 'G')
  1823. term.mode |= MODE_UTF8;
  1824. else if (ascii == '@')
  1825. term.mode &= ~MODE_UTF8;
  1826. }
  1827. void
  1828. tdeftran(char ascii)
  1829. {
  1830. static char cs[] = "0B";
  1831. static int vcs[] = {CS_GRAPHIC0, CS_USA};
  1832. char *p;
  1833. if ((p = strchr(cs, ascii)) == NULL) {
  1834. fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
  1835. } else {
  1836. term.trantbl[term.icharset] = vcs[p - cs];
  1837. }
  1838. }
  1839. void
  1840. tdectest(char c)
  1841. {
  1842. int x, y;
  1843. if (c == '8') { /* DEC screen alignment test. */
  1844. for (x = 0; x < term.col; ++x) {
  1845. for (y = 0; y < term.row; ++y)
  1846. tsetchar('E', &term.c.attr, x, y);
  1847. }
  1848. }
  1849. }
  1850. void
  1851. tstrsequence(uchar c)
  1852. {
  1853. strreset();
  1854. switch (c) {
  1855. case 0x90: /* DCS -- Device Control String */
  1856. c = 'P';
  1857. term.esc |= ESC_DCS;
  1858. break;
  1859. case 0x9f: /* APC -- Application Program Command */
  1860. c = '_';
  1861. break;
  1862. case 0x9e: /* PM -- Privacy Message */
  1863. c = '^';
  1864. break;
  1865. case 0x9d: /* OSC -- Operating System Command */
  1866. c = ']';
  1867. break;
  1868. }
  1869. strescseq.type = c;
  1870. term.esc |= ESC_STR;
  1871. }
  1872. void
  1873. tcontrolcode(uchar ascii)
  1874. {
  1875. switch (ascii) {
  1876. case '\t': /* HT */
  1877. tputtab(1);
  1878. return;
  1879. case '\b': /* BS */
  1880. tmoveto(term.c.x-1, term.c.y);
  1881. return;
  1882. case '\r': /* CR */
  1883. tmoveto(0, term.c.y);
  1884. return;
  1885. case '\f': /* LF */
  1886. case '\v': /* VT */
  1887. case '\n': /* LF */
  1888. /* go to first col if the mode is set */
  1889. tnewline(IS_SET(MODE_CRLF));
  1890. return;
  1891. case '\a': /* BEL */
  1892. if (term.esc & ESC_STR_END) {
  1893. /* backwards compatibility to xterm */
  1894. strhandle();
  1895. } else {
  1896. xbell();
  1897. }
  1898. break;
  1899. case '\033': /* ESC */
  1900. csireset();
  1901. term.esc &= ~(ESC_CSI|ESC_ALTCHARSET|ESC_TEST);
  1902. term.esc |= ESC_START;
  1903. return;
  1904. case '\016': /* SO (LS1 -- Locking shift 1) */
  1905. case '\017': /* SI (LS0 -- Locking shift 0) */
  1906. term.charset = 1 - (ascii - '\016');
  1907. return;
  1908. case '\032': /* SUB */
  1909. tsetchar('?', &term.c.attr, term.c.x, term.c.y);
  1910. case '\030': /* CAN */
  1911. csireset();
  1912. break;
  1913. case '\005': /* ENQ (IGNORED) */
  1914. case '\000': /* NUL (IGNORED) */
  1915. case '\021': /* XON (IGNORED) */
  1916. case '\023': /* XOFF (IGNORED) */
  1917. case 0177: /* DEL (IGNORED) */
  1918. return;
  1919. case 0x80: /* TODO: PAD */
  1920. case 0x81: /* TODO: HOP */
  1921. case 0x82: /* TODO: BPH */
  1922. case 0x83: /* TODO: NBH */
  1923. case 0x84: /* TODO: IND */
  1924. break;
  1925. case 0x85: /* NEL -- Next line */
  1926. tnewline(1); /* always go to first col */
  1927. break;
  1928. case 0x86: /* TODO: SSA */
  1929. case 0x87: /* TODO: ESA */
  1930. break;
  1931. case 0x88: /* HTS -- Horizontal tab stop */
  1932. term.tabs[term.c.x] = 1;
  1933. break;
  1934. case 0x89: /* TODO: HTJ */
  1935. case 0x8a: /* TODO: VTS */
  1936. case 0x8b: /* TODO: PLD */
  1937. case 0x8c: /* TODO: PLU */
  1938. case 0x8d: /* TODO: RI */
  1939. case 0x8e: /* TODO: SS2 */
  1940. case 0x8f: /* TODO: SS3 */
  1941. case 0x91: /* TODO: PU1 */
  1942. case 0x92: /* TODO: PU2 */
  1943. case 0x93: /* TODO: STS */
  1944. case 0x94: /* TODO: CCH */
  1945. case 0x95: /* TODO: MW */
  1946. case 0x96: /* TODO: SPA */
  1947. case 0x97: /* TODO: EPA */
  1948. case 0x98: /* TODO: SOS */
  1949. case 0x99: /* TODO: SGCI */
  1950. break;
  1951. case 0x9a: /* DECID -- Identify Terminal */
  1952. ttywrite(vtiden, strlen(vtiden), 0);
  1953. break;
  1954. case 0x9b: /* TODO: CSI */
  1955. case 0x9c: /* TODO: ST */
  1956. break;
  1957. case 0x90: /* DCS -- Device Control String */
  1958. case 0x9d: /* OSC -- Operating System Command */
  1959. case 0x9e: /* PM -- Privacy Message */
  1960. case 0x9f: /* APC -- Application Program Command */
  1961. tstrsequence(ascii);
  1962. return;
  1963. }
  1964. /* only CAN, SUB, \a and C1 chars interrupt a sequence */
  1965. term.esc &= ~(ESC_STR_END|ESC_STR);
  1966. }
  1967. /*
  1968. * returns 1 when the sequence is finished and it hasn't to read
  1969. * more characters for this sequence, otherwise 0
  1970. */
  1971. int
  1972. eschandle(uchar ascii)
  1973. {
  1974. switch (ascii) {
  1975. case '[':
  1976. term.esc |= ESC_CSI;
  1977. return 0;
  1978. case '#':
  1979. term.esc |= ESC_TEST;
  1980. return 0;
  1981. case '%':
  1982. term.esc |= ESC_UTF8;
  1983. return 0;
  1984. case 'P': /* DCS -- Device Control String */
  1985. case '_': /* APC -- Application Program Command */
  1986. case '^': /* PM -- Privacy Message */
  1987. case ']': /* OSC -- Operating System Command */
  1988. case 'k': /* old title set compatibility */
  1989. tstrsequence(ascii);
  1990. return 0;
  1991. case 'n': /* LS2 -- Locking shift 2 */
  1992. case 'o': /* LS3 -- Locking shift 3 */
  1993. term.charset = 2 + (ascii - 'n');
  1994. break;
  1995. case '(': /* GZD4 -- set primary charset G0 */
  1996. case ')': /* G1D4 -- set secondary charset G1 */
  1997. case '*': /* G2D4 -- set tertiary charset G2 */
  1998. case '+': /* G3D4 -- set quaternary charset G3 */
  1999. term.icharset = ascii - '(';
  2000. term.esc |= ESC_ALTCHARSET;
  2001. return 0;
  2002. case 'D': /* IND -- Linefeed */
  2003. if (term.c.y == term.bot) {
  2004. tscrollup(term.top, 1);
  2005. } else {
  2006. tmoveto(term.c.x, term.c.y+1);
  2007. }
  2008. break;
  2009. case 'E': /* NEL -- Next line */
  2010. tnewline(1); /* always go to first col */
  2011. break;
  2012. case 'H': /* HTS -- Horizontal tab stop */
  2013. term.tabs[term.c.x] = 1;
  2014. break;
  2015. case 'M': /* RI -- Reverse index */
  2016. if (term.c.y == term.top) {
  2017. tscrolldown(term.top, 1);
  2018. } else {
  2019. tmoveto(term.c.x, term.c.y-1);
  2020. }
  2021. break;
  2022. case 'Z': /* DECID -- Identify Terminal */
  2023. ttywrite(vtiden, strlen(vtiden), 0);
  2024. break;
  2025. case 'c': /* RIS -- Reset to initial state */
  2026. treset();
  2027. resettitle();
  2028. xloadcols();
  2029. break;
  2030. case '=': /* DECPAM -- Application keypad */
  2031. xsetmode(1, MODE_APPKEYPAD);
  2032. break;
  2033. case '>': /* DECPNM -- Normal keypad */
  2034. xsetmode(0, MODE_APPKEYPAD);
  2035. break;
  2036. case '7': /* DECSC -- Save Cursor */
  2037. tcursor(CURSOR_SAVE);
  2038. break;
  2039. case '8': /* DECRC -- Restore Cursor */
  2040. tcursor(CURSOR_LOAD);
  2041. break;
  2042. case '\\': /* ST -- String Terminator */
  2043. if (term.esc & ESC_STR_END)
  2044. strhandle();
  2045. break;
  2046. default:
  2047. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
  2048. (uchar) ascii, isprint(ascii)? ascii:'.');
  2049. break;
  2050. }
  2051. return 1;
  2052. }
  2053. void
  2054. tputc(Rune u)
  2055. {
  2056. char c[UTF_SIZ];
  2057. int control;
  2058. int width, len;
  2059. Glyph *gp;
  2060. control = ISCONTROL(u);
  2061. if (!IS_SET(MODE_UTF8) && !IS_SET(MODE_SIXEL)) {
  2062. c[0] = u;
  2063. width = len = 1;
  2064. } else {
  2065. len = utf8encode(u, c);
  2066. if (!control && (width = wcwidth(u)) == -1) {
  2067. memcpy(c, "\357\277\275", 4); /* UTF_INVALID */
  2068. width = 1;
  2069. }
  2070. }
  2071. if (IS_SET(MODE_PRINT))
  2072. tprinter(c, len);
  2073. /*
  2074. * STR sequence must be checked before anything else
  2075. * because it uses all following characters until it
  2076. * receives a ESC, a SUB, a ST or any other C1 control
  2077. * character.
  2078. */
  2079. if (term.esc & ESC_STR) {
  2080. if (u == '\a' || u == 030 || u == 032 || u == 033 ||
  2081. ISCONTROLC1(u)) {
  2082. term.esc &= ~(ESC_START|ESC_STR|ESC_DCS);
  2083. if (IS_SET(MODE_SIXEL)) {
  2084. /* TODO: render sixel */;
  2085. term.mode &= ~MODE_SIXEL;
  2086. return;
  2087. }
  2088. term.esc |= ESC_STR_END;
  2089. goto check_control_code;
  2090. }
  2091. if (IS_SET(MODE_SIXEL)) {
  2092. /* TODO: implement sixel mode */
  2093. return;
  2094. }
  2095. if (term.esc&ESC_DCS && strescseq.len == 0 && u == 'q')
  2096. term.mode |= MODE_SIXEL;
  2097. if (strescseq.len+len >= sizeof(strescseq.buf)-1) {
  2098. /*
  2099. * Here is a bug in terminals. If the user never sends
  2100. * some code to stop the str or esc command, then st
  2101. * will stop responding. But this is better than
  2102. * silently failing with unknown characters. At least
  2103. * then users will report back.
  2104. *
  2105. * In the case users ever get fixed, here is the code:
  2106. */
  2107. /*
  2108. * term.esc = 0;
  2109. * strhandle();
  2110. */
  2111. return;
  2112. }
  2113. memmove(&strescseq.buf[strescseq.len], c, len);
  2114. strescseq.len += len;
  2115. return;
  2116. }
  2117. check_control_code:
  2118. /*
  2119. * Actions of control codes must be performed as soon they arrive
  2120. * because they can be embedded inside a control sequence, and
  2121. * they must not cause conflicts with sequences.
  2122. */
  2123. if (control) {
  2124. tcontrolcode(u);
  2125. /*
  2126. * control codes are not shown ever
  2127. */
  2128. return;
  2129. } else if (term.esc & ESC_START) {
  2130. if (term.esc & ESC_CSI) {
  2131. csiescseq.buf[csiescseq.len++] = u;
  2132. if (BETWEEN(u, 0x40, 0x7E)
  2133. || csiescseq.len >= \
  2134. sizeof(csiescseq.buf)-1) {
  2135. term.esc = 0;
  2136. csiparse();
  2137. csihandle();
  2138. }
  2139. return;
  2140. } else if (term.esc & ESC_UTF8) {
  2141. tdefutf8(u);
  2142. } else if (term.esc & ESC_ALTCHARSET) {
  2143. tdeftran(u);
  2144. } else if (term.esc & ESC_TEST) {
  2145. tdectest(u);
  2146. } else {
  2147. if (!eschandle(u))
  2148. return;
  2149. /* sequence already finished */
  2150. }
  2151. term.esc = 0;
  2152. /*
  2153. * All characters which form part of a sequence are not
  2154. * printed
  2155. */
  2156. return;
  2157. }
  2158. if (sel.ob.x != -1 && BETWEEN(term.c.y, sel.ob.y, sel.oe.y))
  2159. selclear();
  2160. gp = &term.line[term.c.y][term.c.x];
  2161. if (IS_SET(MODE_WRAP) && (term.c.state & CURSOR_WRAPNEXT)) {
  2162. gp->mode |= ATTR_WRAP;
  2163. tnewline(1);
  2164. gp = &term.line[term.c.y][term.c.x];
  2165. }
  2166. if (IS_SET(MODE_INSERT) && term.c.x+width < term.col)
  2167. memmove(gp+width, gp, (term.col - term.c.x - width) * sizeof(Glyph));
  2168. if (term.c.x+width > term.col) {
  2169. tnewline(1);
  2170. gp = &term.line[term.c.y][term.c.x];
  2171. }
  2172. tsetchar(u, &term.c.attr, term.c.x, term.c.y);
  2173. if (width == 2) {
  2174. gp->mode |= ATTR_WIDE;
  2175. if (term.c.x+1 < term.col) {
  2176. gp[1].u = '\0';
  2177. gp[1].mode = ATTR_WDUMMY;
  2178. }
  2179. }
  2180. if (term.c.x+width < term.col) {
  2181. tmoveto(term.c.x+width, term.c.y);
  2182. } else {
  2183. term.c.state |= CURSOR_WRAPNEXT;
  2184. }
  2185. }
  2186. int
  2187. twrite(const char *buf, int buflen, int show_ctrl)
  2188. {
  2189. int charsize;
  2190. Rune u;
  2191. int n;
  2192. for (n = 0; n < buflen; n += charsize) {
  2193. if (IS_SET(MODE_UTF8) && !IS_SET(MODE_SIXEL)) {
  2194. /* process a complete utf8 char */
  2195. charsize = utf8decode(buf + n, &u, buflen - n);
  2196. if (charsize == 0)
  2197. break;
  2198. } else {
  2199. u = buf[n] & 0xFF;
  2200. charsize = 1;
  2201. }
  2202. if (show_ctrl && ISCONTROL(u)) {
  2203. if (u & 0x80) {
  2204. u &= 0x7f;
  2205. tputc('^');
  2206. tputc('[');
  2207. } else if (u != '\n' && u != '\r' && u != '\t') {
  2208. u ^= 0x40;
  2209. tputc('^');
  2210. }
  2211. }
  2212. tputc(u);
  2213. }
  2214. return n;
  2215. }
  2216. void
  2217. tresize(int col, int row)
  2218. {
  2219. int i;
  2220. int minrow = MIN(row, term.row);
  2221. int mincol = MIN(col, term.col);
  2222. int *bp;
  2223. TCursor c;
  2224. if (col < 1 || row < 1) {
  2225. fprintf(stderr,
  2226. "tresize: error resizing to %dx%d\n", col, row);
  2227. return;
  2228. }
  2229. /*
  2230. * slide screen to keep cursor where we expect it -
  2231. * tscrollup would work here, but we can optimize to
  2232. * memmove because we're freeing the earlier lines
  2233. */
  2234. for (i = 0; i <= term.c.y - row; i++) {
  2235. free(term.line[i]);
  2236. free(term.alt[i]);
  2237. }
  2238. /* ensure that both src and dst are not NULL */
  2239. if (i > 0) {
  2240. memmove(term.line, term.line + i, row * sizeof(Line));
  2241. memmove(term.alt, term.alt + i, row * sizeof(Line));
  2242. }
  2243. for (i += row; i < term.row; i++) {
  2244. free(term.line[i]);
  2245. free(term.alt[i]);
  2246. }
  2247. /* resize to new height */
  2248. term.line = xrealloc(term.line, row * sizeof(Line));
  2249. term.alt = xrealloc(term.alt, row * sizeof(Line));
  2250. term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
  2251. term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
  2252. /* resize each row to new width, zero-pad if needed */
  2253. for (i = 0; i < minrow; i++) {
  2254. term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph));
  2255. term.alt[i] = xrealloc(term.alt[i], col * sizeof(Glyph));
  2256. }
  2257. /* allocate any new rows */
  2258. for (/* i = minrow */; i < row; i++) {
  2259. term.line[i] = xmalloc(col * sizeof(Glyph));
  2260. term.alt[i] = xmalloc(col * sizeof(Glyph));
  2261. }
  2262. if (col > term.col) {
  2263. bp = term.tabs + term.col;
  2264. memset(bp, 0, sizeof(*term.tabs) * (col - term.col));
  2265. while (--bp > term.tabs && !*bp)
  2266. /* nothing */ ;
  2267. for (bp += tabspaces; bp < term.tabs + col; bp += tabspaces)
  2268. *bp = 1;
  2269. }
  2270. /* update terminal size */
  2271. term.col = col;
  2272. term.row = row;
  2273. /* reset scrolling region */
  2274. tsetscroll(0, row-1);
  2275. /* make use of the LIMIT in tmoveto */
  2276. tmoveto(term.c.x, term.c.y);
  2277. /* Clearing both screens (it makes dirty all lines) */
  2278. c = term.c;
  2279. for (i = 0; i < 2; i++) {
  2280. if (mincol < col && 0 < minrow) {
  2281. tclearregion(mincol, 0, col - 1, minrow - 1);
  2282. }
  2283. if (0 < col && minrow < row) {
  2284. tclearregion(0, minrow, col - 1, row - 1);
  2285. }
  2286. tswapscreen();
  2287. tcursor(CURSOR_LOAD);
  2288. }
  2289. term.c = c;
  2290. }
  2291. void
  2292. resettitle(void)
  2293. {
  2294. xsettitle(NULL);
  2295. }
  2296. void
  2297. drawregion(int x1, int y1, int x2, int y2)
  2298. {
  2299. int y;
  2300. for (y = y1; y < y2; y++) {
  2301. if (!term.dirty[y])
  2302. continue;
  2303. term.dirty[y] = 0;
  2304. xdrawline(term.line[y], x1, y, x2);
  2305. }
  2306. }
  2307. void
  2308. draw(void)
  2309. {
  2310. int cx = term.c.x;
  2311. if (!xstartdraw())
  2312. return;
  2313. /* adjust cursor position */
  2314. LIMIT(term.ocx, 0, term.col-1);
  2315. LIMIT(term.ocy, 0, term.row-1);
  2316. if (term.line[term.ocy][term.ocx].mode & ATTR_WDUMMY)
  2317. term.ocx--;
  2318. if (term.line[term.c.y][cx].mode & ATTR_WDUMMY)
  2319. cx--;
  2320. drawregion(0, 0, term.col, term.row);
  2321. xdrawcursor(cx, term.c.y, term.line[term.c.y][cx],
  2322. term.ocx, term.ocy, term.line[term.ocy][term.ocx]);
  2323. term.ocx = cx, term.ocy = term.c.y;
  2324. xfinishdraw();
  2325. xximspot(term.ocx, term.ocy);
  2326. }
  2327. void
  2328. redraw(void)
  2329. {
  2330. tfulldirt();
  2331. draw();
  2332. }