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

2611 lines
55 KiB

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