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.

2610 lines
55 KiB

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