My build of suckless st terminal
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2614 lines
55 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
5 years ago
14 years ago
14 years ago
5 years ago
5 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
5 years ago
11 years ago
11 years ago
14 years ago
14 years ago
  1. /* See LICENSE for license details. */
  2. #include <ctype.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <limits.h>
  6. #include <pwd.h>
  7. #include <stdarg.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <signal.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/select.h>
  14. #include <sys/types.h>
  15. #include <sys/wait.h>
  16. #include <termios.h>
  17. #include <unistd.h>
  18. #include <wchar.h>
  19. #include "st.h"
  20. #include "win.h"
  21. #if defined(__linux)
  22. #include <pty.h>
  23. #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  24. #include <util.h>
  25. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  26. #include <libutil.h>
  27. #endif
  28. /* Arbitrary sizes */
  29. #define UTF_INVALID 0xFFFD
  30. #define UTF_SIZ 4
  31. #define ESC_BUF_SIZ (128*UTF_SIZ)
  32. #define ESC_ARG_SIZ 16
  33. #define STR_BUF_SIZ ESC_BUF_SIZ
  34. #define STR_ARG_SIZ ESC_ARG_SIZ
  35. /* macros */
  36. #define IS_SET(flag) ((term.mode & (flag)) != 0)
  37. #define ISCONTROLC0(c) (BETWEEN(c, 0, 0x1f) || (c) == 0x7f)
  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. Rune lastc; /* last printed char outside of sequence, 0 if control */
  120. } Term;
  121. /* CSI Escape sequence structs */
  122. /* ESC '[' [[ [<priv>] <arg> [;]] <mode> [<mode>]] */
  123. typedef struct {
  124. char buf[ESC_BUF_SIZ]; /* raw string */
  125. size_t len; /* raw string length */
  126. char priv;
  127. int arg[ESC_ARG_SIZ];
  128. int narg; /* nb of args */
  129. char mode[2];
  130. } CSIEscape;
  131. /* STR Escape sequence structs */
  132. /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
  133. typedef struct {
  134. char type; /* ESC type ... */
  135. char *buf; /* allocated raw string */
  136. size_t siz; /* allocation size */
  137. size_t len; /* raw string length */
  138. char *args[STR_ARG_SIZ];
  139. int narg; /* nb of args */
  140. } STREscape;
  141. static void execsh(char *, char **);
  142. static void stty(char **);
  143. static void sigchld(int);
  144. static void ttywriteraw(const char *, size_t);
  145. static void csidump(void);
  146. static void csihandle(void);
  147. static void csiparse(void);
  148. static void csireset(void);
  149. static int eschandle(uchar);
  150. static void strdump(void);
  151. static void strhandle(void);
  152. static void strparse(void);
  153. static void strreset(void);
  154. static void tprinter(char *, size_t);
  155. static void tdumpsel(void);
  156. static void tdumpline(int);
  157. static void tdump(void);
  158. static void tclearregion(int, int, int, int);
  159. static void tcursor(int);
  160. static void tdeletechar(int);
  161. static void tdeleteline(int);
  162. static void tinsertblank(int);
  163. static void tinsertblankline(int);
  164. static int tlinelen(int);
  165. static void tmoveto(int, int);
  166. static void tmoveato(int, int);
  167. static void tnewline(int);
  168. static void tputtab(int);
  169. static void tputc(Rune);
  170. static void treset(void);
  171. static void tscrollup(int, int);
  172. static void tscrolldown(int, int);
  173. static void tsetattr(int *, int);
  174. static void tsetchar(Rune, Glyph *, int, int);
  175. static void tsetdirt(int, int);
  176. static void tsetscroll(int, int);
  177. static void tswapscreen(void);
  178. static void tsetmode(int, int, int *, int);
  179. static int twrite(const char *, int, int);
  180. static void tfulldirt(void);
  181. static void tcontrolcode(uchar );
  182. static void tdectest(char );
  183. static void tdefutf8(char);
  184. static int32_t tdefcolor(int *, int *, int);
  185. static void tdeftran(char);
  186. static void tstrsequence(uchar);
  187. static void drawregion(int, int, int, int);
  188. static void selnormalize(void);
  189. static void selscroll(int, int);
  190. static void selsnap(int *, int *, int);
  191. static size_t utf8decode(const char *, Rune *, size_t);
  192. static Rune utf8decodebyte(char, size_t *);
  193. static char utf8encodebyte(Rune, size_t);
  194. static size_t utf8validate(Rune *, size_t);
  195. static char *base64dec(const char *);
  196. static char base64dec_getc(const char **);
  197. static ssize_t xwrite(int, const char *, size_t);
  198. /* Globals */
  199. static Term term;
  200. static Selection sel;
  201. static CSIEscape csiescseq;
  202. static STREscape strescseq;
  203. static int iofd = 1;
  204. static int cmdfd;
  205. static pid_t pid;
  206. static uchar utfbyte[UTF_SIZ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
  207. static uchar utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
  208. static Rune utfmin[UTF_SIZ + 1] = { 0, 0, 0x80, 0x800, 0x10000};
  209. static Rune utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
  210. ssize_t
  211. xwrite(int fd, const char *s, size_t len)
  212. {
  213. size_t aux = len;
  214. ssize_t r;
  215. while (len > 0) {
  216. r = write(fd, s, len);
  217. if (r < 0)
  218. return r;
  219. len -= r;
  220. s += r;
  221. }
  222. return aux;
  223. }
  224. void *
  225. xmalloc(size_t len)
  226. {
  227. void *p;
  228. if (!(p = malloc(len)))
  229. die("malloc: %s\n", strerror(errno));
  230. return p;
  231. }
  232. void *
  233. xrealloc(void *p, size_t len)
  234. {
  235. if ((p = realloc(p, len)) == NULL)
  236. die("realloc: %s\n", strerror(errno));
  237. return p;
  238. }
  239. char *
  240. xstrdup(char *s)
  241. {
  242. if ((s = strdup(s)) == NULL)
  243. die("strdup: %s\n", strerror(errno));
  244. return s;
  245. }
  246. size_t
  247. utf8decode(const char *c, Rune *u, size_t clen)
  248. {
  249. size_t i, j, len, type;
  250. Rune udecoded;
  251. *u = UTF_INVALID;
  252. if (!clen)
  253. return 0;
  254. udecoded = utf8decodebyte(c[0], &len);
  255. if (!BETWEEN(len, 1, UTF_SIZ))
  256. return 1;
  257. for (i = 1, j = 1; i < clen && j < len; ++i, ++j) {
  258. udecoded = (udecoded << 6) | utf8decodebyte(c[i], &type);
  259. if (type != 0)
  260. return j;
  261. }
  262. if (j < len)
  263. return 0;
  264. *u = udecoded;
  265. utf8validate(u, len);
  266. return len;
  267. }
  268. Rune
  269. utf8decodebyte(char c, size_t *i)
  270. {
  271. for (*i = 0; *i < LEN(utfmask); ++(*i))
  272. if (((uchar)c & utfmask[*i]) == utfbyte[*i])
  273. return (uchar)c & ~utfmask[*i];
  274. return 0;
  275. }
  276. size_t
  277. utf8encode(Rune u, char *c)
  278. {
  279. size_t len, i;
  280. len = utf8validate(&u, 0);
  281. if (len > UTF_SIZ)
  282. return 0;
  283. for (i = len - 1; i != 0; --i) {
  284. c[i] = utf8encodebyte(u, 0);
  285. u >>= 6;
  286. }
  287. c[0] = utf8encodebyte(u, len);
  288. return len;
  289. }
  290. char
  291. utf8encodebyte(Rune u, size_t i)
  292. {
  293. return utfbyte[i] | (u & ~utfmask[i]);
  294. }
  295. size_t
  296. utf8validate(Rune *u, size_t i)
  297. {
  298. if (!BETWEEN(*u, utfmin[i], utfmax[i]) || BETWEEN(*u, 0xD800, 0xDFFF))
  299. *u = UTF_INVALID;
  300. for (i = 1; *u > utfmax[i]; ++i)
  301. ;
  302. return i;
  303. }
  304. static const char base64_digits[] = {
  305. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  306. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0,
  307. 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, -1, 0, 0, 0, 0, 1,
  308. 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
  309. 22, 23, 24, 25, 0, 0, 0, 0, 0, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34,
  310. 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  316. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  317. };
  318. char
  319. base64dec_getc(const char **src)
  320. {
  321. while (**src && !isprint(**src))
  322. (*src)++;
  323. return **src ? *((*src)++) : '='; /* emulate padding if string ends */
  324. }
  325. char *
  326. base64dec(const char *src)
  327. {
  328. size_t in_len = strlen(src);
  329. char *result, *dst;
  330. if (in_len % 4)
  331. in_len += 4 - (in_len % 4);
  332. result = dst = xmalloc(in_len / 4 * 3 + 1);
  333. while (*src) {
  334. int a = base64_digits[(unsigned char) base64dec_getc(&src)];
  335. int b = base64_digits[(unsigned char) base64dec_getc(&src)];
  336. int c = base64_digits[(unsigned char) base64dec_getc(&src)];
  337. int d = base64_digits[(unsigned char) base64dec_getc(&src)];
  338. /* invalid input. 'a' can be -1, e.g. if src is "\n" (c-str) */
  339. if (a == -1 || b == -1)
  340. break;
  341. *dst++ = (a << 2) | ((b & 0x30) >> 4);
  342. if (c == -1)
  343. break;
  344. *dst++ = ((b & 0x0f) << 4) | ((c & 0x3c) >> 2);
  345. if (d == -1)
  346. break;
  347. *dst++ = ((c & 0x03) << 6) | d;
  348. }
  349. *dst = '\0';
  350. return result;
  351. }
  352. void
  353. selinit(void)
  354. {
  355. sel.mode = SEL_IDLE;
  356. sel.snap = 0;
  357. sel.ob.x = -1;
  358. }
  359. int
  360. tlinelen(int y)
  361. {
  362. int i = term.col;
  363. if (term.line[y][i - 1].mode & ATTR_WRAP)
  364. return i;
  365. while (i > 0 && term.line[y][i - 1].u == ' ')
  366. --i;
  367. return i;
  368. }
  369. void
  370. selstart(int col, int row, int snap)
  371. {
  372. selclear();
  373. sel.mode = SEL_EMPTY;
  374. sel.type = SEL_REGULAR;
  375. sel.alt = IS_SET(MODE_ALTSCREEN);
  376. sel.snap = snap;
  377. sel.oe.x = sel.ob.x = col;
  378. sel.oe.y = sel.ob.y = row;
  379. selnormalize();
  380. if (sel.snap != 0)
  381. sel.mode = SEL_READY;
  382. tsetdirt(sel.nb.y, sel.ne.y);
  383. }
  384. void
  385. selextend(int col, int row, int type, int done)
  386. {
  387. int oldey, oldex, oldsby, oldsey, oldtype;
  388. if (sel.mode == SEL_IDLE)
  389. return;
  390. if (done && sel.mode == SEL_EMPTY) {
  391. selclear();
  392. return;
  393. }
  394. oldey = sel.oe.y;
  395. oldex = sel.oe.x;
  396. oldsby = sel.nb.y;
  397. oldsey = sel.ne.y;
  398. oldtype = sel.type;
  399. sel.oe.x = col;
  400. sel.oe.y = row;
  401. selnormalize();
  402. sel.type = type;
  403. if (oldey != sel.oe.y || oldex != sel.oe.x || oldtype != sel.type || sel.mode == SEL_EMPTY)
  404. tsetdirt(MIN(sel.nb.y, oldsby), MAX(sel.ne.y, oldsey));
  405. sel.mode = done ? SEL_IDLE : SEL_READY;
  406. }
  407. void
  408. selnormalize(void)
  409. {
  410. int i;
  411. if (sel.type == SEL_REGULAR && sel.ob.y != sel.oe.y) {
  412. sel.nb.x = sel.ob.y < sel.oe.y ? sel.ob.x : sel.oe.x;
  413. sel.ne.x = sel.ob.y < sel.oe.y ? sel.oe.x : sel.ob.x;
  414. } else {
  415. sel.nb.x = MIN(sel.ob.x, sel.oe.x);
  416. sel.ne.x = MAX(sel.ob.x, sel.oe.x);
  417. }
  418. sel.nb.y = MIN(sel.ob.y, sel.oe.y);
  419. sel.ne.y = MAX(sel.ob.y, sel.oe.y);
  420. selsnap(&sel.nb.x, &sel.nb.y, -1);
  421. selsnap(&sel.ne.x, &sel.ne.y, +1);
  422. /* expand selection over line breaks */
  423. if (sel.type == SEL_RECTANGULAR)
  424. return;
  425. i = tlinelen(sel.nb.y);
  426. if (i < sel.nb.x)
  427. sel.nb.x = i;
  428. if (tlinelen(sel.ne.y) <= sel.ne.x)
  429. sel.ne.x = term.col - 1;
  430. }
  431. int
  432. selected(int x, int y)
  433. {
  434. if (sel.mode == SEL_EMPTY || sel.ob.x == -1 ||
  435. sel.alt != IS_SET(MODE_ALTSCREEN))
  436. return 0;
  437. if (sel.type == SEL_RECTANGULAR)
  438. return BETWEEN(y, sel.nb.y, sel.ne.y)
  439. && BETWEEN(x, sel.nb.x, sel.ne.x);
  440. return BETWEEN(y, sel.nb.y, sel.ne.y)
  441. && (y != sel.nb.y || x >= sel.nb.x)
  442. && (y != sel.ne.y || x <= sel.ne.x);
  443. }
  444. void
  445. selsnap(int *x, int *y, int direction)
  446. {
  447. int newx, newy, xt, yt;
  448. int delim, prevdelim;
  449. Glyph *gp, *prevgp;
  450. switch (sel.snap) {
  451. case SNAP_WORD:
  452. /*
  453. * Snap around if the word wraps around at the end or
  454. * beginning of a line.
  455. */
  456. prevgp = &term.line[*y][*x];
  457. prevdelim = ISDELIM(prevgp->u);
  458. for (;;) {
  459. newx = *x + direction;
  460. newy = *y;
  461. if (!BETWEEN(newx, 0, term.col - 1)) {
  462. newy += direction;
  463. newx = (newx + term.col) % term.col;
  464. if (!BETWEEN(newy, 0, term.row - 1))
  465. break;
  466. if (direction > 0)
  467. yt = *y, xt = *x;
  468. else
  469. yt = newy, xt = newx;
  470. if (!(term.line[yt][xt].mode & ATTR_WRAP))
  471. break;
  472. }
  473. if (newx >= tlinelen(newy))
  474. break;
  475. gp = &term.line[newy][newx];
  476. delim = ISDELIM(gp->u);
  477. if (!(gp->mode & ATTR_WDUMMY) && (delim != prevdelim
  478. || (delim && gp->u != prevgp->u)))
  479. break;
  480. *x = newx;
  481. *y = newy;
  482. prevgp = gp;
  483. prevdelim = delim;
  484. }
  485. break;
  486. case SNAP_LINE:
  487. /*
  488. * Snap around if the the previous line or the current one
  489. * has set ATTR_WRAP at its end. Then the whole next or
  490. * previous line will be selected.
  491. */
  492. *x = (direction < 0) ? 0 : term.col - 1;
  493. if (direction < 0) {
  494. for (; *y > 0; *y += direction) {
  495. if (!(term.line[*y-1][term.col-1].mode
  496. & ATTR_WRAP)) {
  497. break;
  498. }
  499. }
  500. } else if (direction > 0) {
  501. for (; *y < term.row-1; *y += direction) {
  502. if (!(term.line[*y][term.col-1].mode
  503. & ATTR_WRAP)) {
  504. break;
  505. }
  506. }
  507. }
  508. break;
  509. }
  510. }
  511. char *
  512. getsel(void)
  513. {
  514. char *str, *ptr;
  515. int y, bufsize, lastx, linelen;
  516. Glyph *gp, *last;
  517. if (sel.ob.x == -1)
  518. return NULL;
  519. bufsize = (term.col+1) * (sel.ne.y-sel.nb.y+1) * UTF_SIZ;
  520. ptr = str = xmalloc(bufsize);
  521. /* append every set & selected glyph to the selection */
  522. for (y = sel.nb.y; y <= sel.ne.y; y++) {
  523. if ((linelen = tlinelen(y)) == 0) {
  524. *ptr++ = '\n';
  525. continue;
  526. }
  527. if (sel.type == SEL_RECTANGULAR) {
  528. gp = &term.line[y][sel.nb.x];
  529. lastx = sel.ne.x;
  530. } else {
  531. gp = &term.line[y][sel.nb.y == y ? sel.nb.x : 0];
  532. lastx = (sel.ne.y == y) ? sel.ne.x : term.col-1;
  533. }
  534. last = &term.line[y][MIN(lastx, linelen-1)];
  535. while (last >= gp && last->u == ' ')
  536. --last;
  537. for ( ; gp <= last; ++gp) {
  538. if (gp->mode & ATTR_WDUMMY)
  539. continue;
  540. ptr += utf8encode(gp->u, ptr);
  541. }
  542. /*
  543. * Copy and pasting of line endings is inconsistent
  544. * in the inconsistent terminal and GUI world.
  545. * The best solution seems like to produce '\n' when
  546. * something is copied from st and convert '\n' to
  547. * '\r', when something to be pasted is received by
  548. * st.
  549. * FIXME: Fix the computer world.
  550. */
  551. if ((y < sel.ne.y || lastx >= linelen) &&
  552. (!(last->mode & ATTR_WRAP) || sel.type == SEL_RECTANGULAR))
  553. *ptr++ = '\n';
  554. }
  555. *ptr = 0;
  556. return str;
  557. }
  558. void
  559. selclear(void)
  560. {
  561. if (sel.ob.x == -1)
  562. return;
  563. sel.mode = SEL_IDLE;
  564. sel.ob.x = -1;
  565. tsetdirt(sel.nb.y, sel.ne.y);
  566. }
  567. void
  568. die(const char *errstr, ...)
  569. {
  570. va_list ap;
  571. va_start(ap, errstr);
  572. vfprintf(stderr, errstr, ap);
  573. va_end(ap);
  574. exit(1);
  575. }
  576. void
  577. execsh(char *cmd, char **args)
  578. {
  579. char *sh, *prog, *arg;
  580. const struct passwd *pw;
  581. errno = 0;
  582. if ((pw = getpwuid(getuid())) == NULL) {
  583. if (errno)
  584. die("getpwuid: %s\n", strerror(errno));
  585. else
  586. die("who are you?\n");
  587. }
  588. if ((sh = getenv("SHELL")) == NULL)
  589. sh = (pw->pw_shell[0]) ? pw->pw_shell : cmd;
  590. if (args) {
  591. prog = args[0];
  592. arg = NULL;
  593. } else if (scroll) {
  594. prog = scroll;
  595. arg = utmp ? utmp : sh;
  596. } else if (utmp) {
  597. prog = utmp;
  598. arg = NULL;
  599. } else {
  600. prog = sh;
  601. arg = NULL;
  602. }
  603. DEFAULT(args, ((char *[]) {prog, arg, NULL}));
  604. unsetenv("COLUMNS");
  605. unsetenv("LINES");
  606. unsetenv("TERMCAP");
  607. setenv("LOGNAME", pw->pw_name, 1);
  608. setenv("USER", pw->pw_name, 1);
  609. setenv("SHELL", sh, 1);
  610. setenv("HOME", pw->pw_dir, 1);
  611. setenv("TERM", termname, 1);
  612. signal(SIGCHLD, SIG_DFL);
  613. signal(SIGHUP, SIG_DFL);
  614. signal(SIGINT, SIG_DFL);
  615. signal(SIGQUIT, SIG_DFL);
  616. signal(SIGTERM, SIG_DFL);
  617. signal(SIGALRM, SIG_DFL);
  618. execvp(prog, args);
  619. _exit(1);
  620. }
  621. void
  622. sigchld(int a)
  623. {
  624. int stat;
  625. pid_t p;
  626. if ((p = waitpid(pid, &stat, WNOHANG)) < 0)
  627. die("waiting for pid %hd failed: %s\n", pid, strerror(errno));
  628. if (pid != p)
  629. return;
  630. if (WIFEXITED(stat) && WEXITSTATUS(stat))
  631. die("child exited with status %d\n", WEXITSTATUS(stat));
  632. else if (WIFSIGNALED(stat))
  633. die("child terminated due to signal %d\n", WTERMSIG(stat));
  634. _exit(0);
  635. }
  636. void
  637. stty(char **args)
  638. {
  639. char cmd[_POSIX_ARG_MAX], **p, *q, *s;
  640. size_t n, siz;
  641. if ((n = strlen(stty_args)) > sizeof(cmd)-1)
  642. die("incorrect stty parameters\n");
  643. memcpy(cmd, stty_args, n);
  644. q = cmd + n;
  645. siz = sizeof(cmd) - n;
  646. for (p = args; p && (s = *p); ++p) {
  647. if ((n = strlen(s)) > siz-1)
  648. die("stty parameter length too long\n");
  649. *q++ = ' ';
  650. memcpy(q, s, n);
  651. q += n;
  652. siz -= n + 1;
  653. }
  654. *q = '\0';
  655. if (system(cmd) != 0)
  656. perror("Couldn't call stty");
  657. }
  658. int
  659. ttynew(char *line, char *cmd, char *out, char **args)
  660. {
  661. int m, s;
  662. if (out) {
  663. term.mode |= MODE_PRINT;
  664. iofd = (!strcmp(out, "-")) ?
  665. 1 : open(out, O_WRONLY | O_CREAT, 0666);
  666. if (iofd < 0) {
  667. fprintf(stderr, "Error opening %s:%s\n",
  668. out, strerror(errno));
  669. }
  670. }
  671. if (line) {
  672. if ((cmdfd = open(line, O_RDWR)) < 0)
  673. die("open line '%s' failed: %s\n",
  674. line, strerror(errno));
  675. dup2(cmdfd, 0);
  676. stty(args);
  677. return cmdfd;
  678. }
  679. /* seems to work fine on linux, openbsd and freebsd */
  680. if (openpty(&m, &s, NULL, NULL, NULL) < 0)
  681. die("openpty failed: %s\n", strerror(errno));
  682. switch (pid = fork()) {
  683. case -1:
  684. die("fork failed: %s\n", strerror(errno));
  685. break;
  686. case 0:
  687. close(iofd);
  688. setsid(); /* create a new process group */
  689. dup2(s, 0);
  690. dup2(s, 1);
  691. dup2(s, 2);
  692. if (ioctl(s, TIOCSCTTY, NULL) < 0)
  693. die("ioctl TIOCSCTTY failed: %s\n", strerror(errno));
  694. close(s);
  695. close(m);
  696. #ifdef __OpenBSD__
  697. if (pledge("stdio getpw proc exec", NULL) == -1)
  698. die("pledge\n");
  699. #endif
  700. execsh(cmd, args);
  701. break;
  702. default:
  703. #ifdef __OpenBSD__
  704. if (pledge("stdio rpath tty proc", NULL) == -1)
  705. die("pledge\n");
  706. #endif
  707. close(s);
  708. cmdfd = m;
  709. signal(SIGCHLD, sigchld);
  710. break;
  711. }
  712. return cmdfd;
  713. }
  714. size_t
  715. ttyread(void)
  716. {
  717. static char buf[BUFSIZ];
  718. static int buflen = 0;
  719. int ret, written;
  720. /* append read bytes to unprocessed bytes */
  721. ret = read(cmdfd, buf+buflen, LEN(buf)-buflen);
  722. switch (ret) {
  723. case 0:
  724. exit(0);
  725. case -1:
  726. die("couldn't read from shell: %s\n", strerror(errno));
  727. default:
  728. buflen += ret;
  729. written = twrite(buf, buflen, 0);
  730. buflen -= written;
  731. /* keep any incomplete UTF-8 byte sequence for the next call */
  732. if (buflen > 0)
  733. memmove(buf, buf + written, buflen);
  734. return ret;
  735. }
  736. }
  737. void
  738. ttywrite(const char *s, size_t n, int may_echo)
  739. {
  740. const char *next;
  741. if (may_echo && IS_SET(MODE_ECHO))
  742. twrite(s, n, 1);
  743. if (!IS_SET(MODE_CRLF)) {
  744. ttywriteraw(s, n);
  745. return;
  746. }
  747. /* This is similar to how the kernel handles ONLCR for ttys */
  748. while (n > 0) {
  749. if (*s == '\r') {
  750. next = s + 1;
  751. ttywriteraw("\r\n", 2);
  752. } else {
  753. next = memchr(s, '\r', n);
  754. DEFAULT(next, s + n);
  755. ttywriteraw(s, next - s);
  756. }
  757. n -= next - s;
  758. s = next;
  759. }
  760. }
  761. void
  762. ttywriteraw(const char *s, size_t n)
  763. {
  764. fd_set wfd, rfd;
  765. ssize_t r;
  766. size_t lim = 256;
  767. /*
  768. * Remember that we are using a pty, which might be a modem line.
  769. * Writing too much will clog the line. That's why we are doing this
  770. * dance.
  771. * FIXME: Migrate the world to Plan 9.
  772. */
  773. while (n > 0) {
  774. FD_ZERO(&wfd);
  775. FD_ZERO(&rfd);
  776. FD_SET(cmdfd, &wfd);
  777. FD_SET(cmdfd, &rfd);
  778. /* Check if we can write. */
  779. if (pselect(cmdfd+1, &rfd, &wfd, NULL, NULL, NULL) < 0) {
  780. if (errno == EINTR)
  781. continue;
  782. die("select failed: %s\n", strerror(errno));
  783. }
  784. if (FD_ISSET(cmdfd, &wfd)) {
  785. /*
  786. * Only write the bytes written by ttywrite() or the
  787. * default of 256. This seems to be a reasonable value
  788. * for a serial line. Bigger values might clog the I/O.
  789. */
  790. if ((r = write(cmdfd, s, (n < lim)? n : lim)) < 0)
  791. goto write_error;
  792. if (r < n) {
  793. /*
  794. * We weren't able to write out everything.
  795. * This means the buffer is getting full
  796. * again. Empty it.
  797. */
  798. if (n < lim)
  799. lim = ttyread();
  800. n -= r;
  801. s += r;
  802. } else {
  803. /* All bytes have been written. */
  804. break;
  805. }
  806. }
  807. if (FD_ISSET(cmdfd, &rfd))
  808. lim = ttyread();
  809. }
  810. return;
  811. write_error:
  812. die("write error on tty: %s\n", strerror(errno));
  813. }
  814. void
  815. ttyresize(int tw, int th)
  816. {
  817. struct winsize w;
  818. w.ws_row = term.row;
  819. w.ws_col = term.col;
  820. w.ws_xpixel = tw;
  821. w.ws_ypixel = th;
  822. if (ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
  823. fprintf(stderr, "Couldn't set window size: %s\n", strerror(errno));
  824. }
  825. void
  826. ttyhangup()
  827. {
  828. /* Send SIGHUP to shell */
  829. kill(pid, SIGHUP);
  830. }
  831. int
  832. tattrset(int attr)
  833. {
  834. int i, j;
  835. for (i = 0; i < term.row-1; i++) {
  836. for (j = 0; j < term.col-1; j++) {
  837. if (term.line[i][j].mode & attr)
  838. return 1;
  839. }
  840. }
  841. return 0;
  842. }
  843. void
  844. tsetdirt(int top, int bot)
  845. {
  846. int i;
  847. LIMIT(top, 0, term.row-1);
  848. LIMIT(bot, 0, term.row-1);
  849. for (i = top; i <= bot; i++)
  850. term.dirty[i] = 1;
  851. }
  852. void
  853. tsetdirtattr(int attr)
  854. {
  855. int i, j;
  856. for (i = 0; i < term.row-1; i++) {
  857. for (j = 0; j < term.col-1; j++) {
  858. if (term.line[i][j].mode & attr) {
  859. tsetdirt(i, i);
  860. break;
  861. }
  862. }
  863. }
  864. }
  865. void
  866. tfulldirt(void)
  867. {
  868. tsetdirt(0, term.row-1);
  869. }
  870. void
  871. tcursor(int mode)
  872. {
  873. static TCursor c[2];
  874. int alt = IS_SET(MODE_ALTSCREEN);
  875. if (mode == CURSOR_SAVE) {
  876. c[alt] = term.c;
  877. } else if (mode == CURSOR_LOAD) {
  878. term.c = c[alt];
  879. tmoveto(c[alt].x, c[alt].y);
  880. }
  881. }
  882. void
  883. treset(void)
  884. {
  885. uint i;
  886. term.c = (TCursor){{
  887. .mode = ATTR_NULL,
  888. .fg = defaultfg,
  889. .bg = defaultbg
  890. }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
  891. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  892. for (i = tabspaces; i < term.col; i += tabspaces)
  893. term.tabs[i] = 1;
  894. term.top = 0;
  895. term.bot = term.row - 1;
  896. term.mode = MODE_WRAP|MODE_UTF8;
  897. memset(term.trantbl, CS_USA, sizeof(term.trantbl));
  898. term.charset = 0;
  899. for (i = 0; i < 2; i++) {
  900. tmoveto(0, 0);
  901. tcursor(CURSOR_SAVE);
  902. tclearregion(0, 0, term.col-1, term.row-1);
  903. tswapscreen();
  904. }
  905. }
  906. void
  907. tnew(int col, int row)
  908. {
  909. term = (Term){ .c = { .attr = { .fg = defaultfg, .bg = defaultbg } } };
  910. tresize(col, row);
  911. treset();
  912. }
  913. void
  914. tswapscreen(void)
  915. {
  916. Line *tmp = term.line;
  917. term.line = term.alt;
  918. term.alt = tmp;
  919. term.mode ^= MODE_ALTSCREEN;
  920. tfulldirt();
  921. }
  922. void
  923. tscrolldown(int orig, int n)
  924. {
  925. int i;
  926. Line temp;
  927. LIMIT(n, 0, term.bot-orig+1);
  928. tsetdirt(orig, term.bot-n);
  929. tclearregion(0, term.bot-n+1, term.col-1, term.bot);
  930. for (i = term.bot; i >= orig+n; i--) {
  931. temp = term.line[i];
  932. term.line[i] = term.line[i-n];
  933. term.line[i-n] = temp;
  934. }
  935. selscroll(orig, n);
  936. }
  937. void
  938. tscrollup(int orig, int n)
  939. {
  940. int i;
  941. Line temp;
  942. LIMIT(n, 0, term.bot-orig+1);
  943. tclearregion(0, orig, term.col-1, orig+n-1);
  944. tsetdirt(orig+n, term.bot);
  945. for (i = orig; i <= term.bot-n; i++) {
  946. temp = term.line[i];
  947. term.line[i] = term.line[i+n];
  948. term.line[i+n] = temp;
  949. }
  950. selscroll(orig, -n);
  951. }
  952. void
  953. selscroll(int orig, int n)
  954. {
  955. if (sel.ob.x == -1)
  956. return;
  957. if (BETWEEN(sel.nb.y, orig, term.bot) != BETWEEN(sel.ne.y, orig, term.bot)) {
  958. selclear();
  959. } else if (BETWEEN(sel.nb.y, orig, term.bot)) {
  960. sel.ob.y += n;
  961. sel.oe.y += n;
  962. if (sel.ob.y < term.top || sel.ob.y > term.bot ||
  963. sel.oe.y < term.top || sel.oe.y > term.bot) {
  964. selclear();
  965. } else {
  966. selnormalize();
  967. }
  968. }
  969. }
  970. void
  971. tnewline(int first_col)
  972. {
  973. int y = term.c.y;
  974. if (y == term.bot) {
  975. tscrollup(term.top, 1);
  976. } else {
  977. y++;
  978. }
  979. tmoveto(first_col ? 0 : term.c.x, y);
  980. }
  981. void
  982. csiparse(void)
  983. {
  984. char *p = csiescseq.buf, *np;
  985. long int v;
  986. csiescseq.narg = 0;
  987. if (*p == '?') {
  988. csiescseq.priv = 1;
  989. p++;
  990. }
  991. csiescseq.buf[csiescseq.len] = '\0';
  992. while (p < csiescseq.buf+csiescseq.len) {
  993. np = NULL;
  994. v = strtol(p, &np, 10);
  995. if (np == p)
  996. v = 0;
  997. if (v == LONG_MAX || v == LONG_MIN)
  998. v = -1;
  999. csiescseq.arg[csiescseq.narg++] = v;
  1000. p = np;
  1001. if (*p != ';' || csiescseq.narg == ESC_ARG_SIZ)
  1002. break;
  1003. p++;
  1004. }
  1005. csiescseq.mode[0] = *p++;
  1006. csiescseq.mode[1] = (p < csiescseq.buf+csiescseq.len) ? *p : '\0';
  1007. }
  1008. /* for absolute user moves, when decom is set */
  1009. void
  1010. tmoveato(int x, int y)
  1011. {
  1012. tmoveto(x, y + ((term.c.state & CURSOR_ORIGIN) ? term.top: 0));
  1013. }
  1014. void
  1015. tmoveto(int x, int y)
  1016. {
  1017. int miny, maxy;
  1018. if (term.c.state & CURSOR_ORIGIN) {
  1019. miny = term.top;
  1020. maxy = term.bot;
  1021. } else {
  1022. miny = 0;
  1023. maxy = term.row - 1;
  1024. }
  1025. term.c.state &= ~CURSOR_WRAPNEXT;
  1026. term.c.x = LIMIT(x, 0, term.col-1);
  1027. term.c.y = LIMIT(y, miny, maxy);
  1028. }
  1029. void
  1030. tsetchar(Rune u, Glyph *attr, int x, int y)
  1031. {
  1032. static char *vt100_0[62] = { /* 0x41 - 0x7e */
  1033. "", "", "", "", "", "", "", /* A - G */
  1034. 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
  1035. 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
  1036. 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
  1037. "", "", "", "", "", "", "°", "±", /* ` - g */
  1038. "", "", "", "", "", "", "", "", /* h - o */
  1039. "", "", "", "", "", "", "", "", /* p - w */
  1040. "", "", "", "π", "", "£", "·", /* x - ~ */
  1041. };
  1042. /*
  1043. * The table is proudly stolen from rxvt.
  1044. */
  1045. if (term.trantbl[term.charset] == CS_GRAPHIC0 &&
  1046. BETWEEN(u, 0x41, 0x7e) && vt100_0[u - 0x41])
  1047. utf8decode(vt100_0[u - 0x41], &u, UTF_SIZ);
  1048. if (term.line[y][x].mode & ATTR_WIDE) {
  1049. if (x+1 < term.col) {
  1050. term.line[y][x+1].u = ' ';
  1051. term.line[y][x+1].mode &= ~ATTR_WDUMMY;
  1052. }
  1053. } else if (term.line[y][x].mode & ATTR_WDUMMY) {
  1054. term.line[y][x-1].u = ' ';
  1055. term.line[y][x-1].mode &= ~ATTR_WIDE;
  1056. }
  1057. term.dirty[y] = 1;
  1058. term.line[y][x] = *attr;
  1059. term.line[y][x].u = u;
  1060. }
  1061. void
  1062. tclearregion(int x1, int y1, int x2, int y2)
  1063. {
  1064. int x, y, temp;
  1065. Glyph *gp;
  1066. if (x1 > x2)
  1067. temp = x1, x1 = x2, x2 = temp;
  1068. if (y1 > y2)
  1069. temp = y1, y1 = y2, y2 = temp;
  1070. LIMIT(x1, 0, term.col-1);
  1071. LIMIT(x2, 0, term.col-1);
  1072. LIMIT(y1, 0, term.row-1);
  1073. LIMIT(y2, 0, term.row-1);
  1074. for (y = y1; y <= y2; y++) {
  1075. term.dirty[y] = 1;
  1076. for (x = x1; x <= x2; x++) {
  1077. gp = &term.line[y][x];
  1078. if (selected(x, y))
  1079. selclear();
  1080. gp->fg = term.c.attr.fg;
  1081. gp->bg = term.c.attr.bg;
  1082. gp->mode = 0;
  1083. gp->u = ' ';
  1084. }
  1085. }
  1086. }
  1087. void
  1088. tdeletechar(int n)
  1089. {
  1090. int dst, src, size;
  1091. Glyph *line;
  1092. LIMIT(n, 0, term.col - term.c.x);
  1093. dst = term.c.x;
  1094. src = term.c.x + n;
  1095. size = term.col - src;
  1096. line = term.line[term.c.y];
  1097. memmove(&line[dst], &line[src], size * sizeof(Glyph));
  1098. tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
  1099. }
  1100. void
  1101. tinsertblank(int n)
  1102. {
  1103. int dst, src, size;
  1104. Glyph *line;
  1105. LIMIT(n, 0, term.col - term.c.x);
  1106. dst = term.c.x + n;
  1107. src = term.c.x;
  1108. size = term.col - dst;
  1109. line = term.line[term.c.y];
  1110. memmove(&line[dst], &line[src], size * sizeof(Glyph));
  1111. tclearregion(src, term.c.y, dst - 1, term.c.y);
  1112. }
  1113. void
  1114. tinsertblankline(int n)
  1115. {
  1116. if (BETWEEN(term.c.y, term.top, term.bot))
  1117. tscrolldown(term.c.y, n);
  1118. }
  1119. void
  1120. tdeleteline(int n)
  1121. {
  1122. if (BETWEEN(term.c.y, term.top, term.bot))
  1123. tscrollup(term.c.y, n);
  1124. }
  1125. int32_t
  1126. tdefcolor(int *attr, int *npar, int l)
  1127. {
  1128. int32_t idx = -1;
  1129. uint r, g, b;
  1130. switch (attr[*npar + 1]) {
  1131. case 2: /* direct color in RGB space */
  1132. if (*npar + 4 >= l) {
  1133. fprintf(stderr,
  1134. "erresc(38): Incorrect number of parameters (%d)\n",
  1135. *npar);
  1136. break;
  1137. }
  1138. r = attr[*npar + 2];
  1139. g = attr[*npar + 3];
  1140. b = attr[*npar + 4];
  1141. *npar += 4;
  1142. if (!BETWEEN(r, 0, 255) || !BETWEEN(g, 0, 255) || !BETWEEN(b, 0, 255))
  1143. fprintf(stderr, "erresc: bad rgb color (%u,%u,%u)\n",
  1144. r, g, b);
  1145. else
  1146. idx = TRUECOLOR(r, g, b);
  1147. break;
  1148. case 5: /* indexed color */
  1149. if (*npar + 2 >= l) {
  1150. fprintf(stderr,
  1151. "erresc(38): Incorrect number of parameters (%d)\n",
  1152. *npar);
  1153. break;
  1154. }
  1155. *npar += 2;
  1156. if (!BETWEEN(attr[*npar], 0, 255))
  1157. fprintf(stderr, "erresc: bad fgcolor %d\n", attr[*npar]);
  1158. else
  1159. idx = attr[*npar];
  1160. break;
  1161. case 0: /* implemented defined (only foreground) */
  1162. case 1: /* transparent */
  1163. case 3: /* direct color in CMY space */
  1164. case 4: /* direct color in CMYK space */
  1165. default:
  1166. fprintf(stderr,
  1167. "erresc(38): gfx attr %d unknown\n", attr[*npar]);
  1168. break;
  1169. }
  1170. return idx;
  1171. }
  1172. void
  1173. tsetattr(int *attr, int l)
  1174. {
  1175. int i;
  1176. int32_t idx;
  1177. for (i = 0; i < l; i++) {
  1178. switch (attr[i]) {
  1179. case 0:
  1180. term.c.attr.mode &= ~(
  1181. ATTR_BOLD |
  1182. ATTR_FAINT |
  1183. ATTR_ITALIC |
  1184. ATTR_UNDERLINE |
  1185. ATTR_BLINK |
  1186. ATTR_REVERSE |
  1187. ATTR_INVISIBLE |
  1188. ATTR_STRUCK );
  1189. term.c.attr.fg = defaultfg;
  1190. term.c.attr.bg = defaultbg;
  1191. break;
  1192. case 1:
  1193. term.c.attr.mode |= ATTR_BOLD;
  1194. break;
  1195. case 2:
  1196. term.c.attr.mode |= ATTR_FAINT;
  1197. break;
  1198. case 3:
  1199. term.c.attr.mode |= ATTR_ITALIC;
  1200. break;
  1201. case 4:
  1202. term.c.attr.mode |= ATTR_UNDERLINE;
  1203. break;
  1204. case 5: /* slow blink */
  1205. /* FALLTHROUGH */
  1206. case 6: /* rapid blink */
  1207. term.c.attr.mode |= ATTR_BLINK;
  1208. break;
  1209. case 7:
  1210. term.c.attr.mode |= ATTR_REVERSE;
  1211. break;
  1212. case 8:
  1213. term.c.attr.mode |= ATTR_INVISIBLE;
  1214. break;
  1215. case 9:
  1216. term.c.attr.mode |= ATTR_STRUCK;
  1217. break;
  1218. case 22:
  1219. term.c.attr.mode &= ~(ATTR_BOLD | ATTR_FAINT);
  1220. break;
  1221. case 23:
  1222. term.c.attr.mode &= ~ATTR_ITALIC;
  1223. break;
  1224. case 24:
  1225. term.c.attr.mode &= ~ATTR_UNDERLINE;
  1226. break;
  1227. case 25:
  1228. term.c.attr.mode &= ~ATTR_BLINK;
  1229. break;
  1230. case 27:
  1231. term.c.attr.mode &= ~ATTR_REVERSE;
  1232. break;
  1233. case 28:
  1234. term.c.attr.mode &= ~ATTR_INVISIBLE;
  1235. break;
  1236. case 29:
  1237. term.c.attr.mode &= ~ATTR_STRUCK;
  1238. break;
  1239. case 38:
  1240. if ((idx = tdefcolor(attr, &i, l)) >= 0)
  1241. term.c.attr.fg = idx;
  1242. break;
  1243. case 39:
  1244. term.c.attr.fg = defaultfg;
  1245. break;
  1246. case 48:
  1247. if ((idx = tdefcolor(attr, &i, l)) >= 0)
  1248. term.c.attr.bg = idx;
  1249. break;
  1250. case 49:
  1251. term.c.attr.bg = defaultbg;
  1252. break;
  1253. default:
  1254. if (BETWEEN(attr[i], 30, 37)) {
  1255. term.c.attr.fg = attr[i] - 30;
  1256. } else if (BETWEEN(attr[i], 40, 47)) {
  1257. term.c.attr.bg = attr[i] - 40;
  1258. } else if (BETWEEN(attr[i], 90, 97)) {
  1259. term.c.attr.fg = attr[i] - 90 + 8;
  1260. } else if (BETWEEN(attr[i], 100, 107)) {
  1261. term.c.attr.bg = attr[i] - 100 + 8;
  1262. } else {
  1263. fprintf(stderr,
  1264. "erresc(default): gfx attr %d unknown\n",
  1265. attr[i]);
  1266. csidump();
  1267. }
  1268. break;
  1269. }
  1270. }
  1271. }
  1272. void
  1273. tsetscroll(int t, int b)
  1274. {
  1275. int temp;
  1276. LIMIT(t, 0, term.row-1);
  1277. LIMIT(b, 0, term.row-1);
  1278. if (t > b) {
  1279. temp = t;
  1280. t = b;
  1281. b = temp;
  1282. }
  1283. term.top = t;
  1284. term.bot = b;
  1285. }
  1286. void
  1287. tsetmode(int priv, int set, int *args, int narg)
  1288. {
  1289. int alt, *lim;
  1290. for (lim = args + narg; args < lim; ++args) {
  1291. if (priv) {
  1292. switch (*args) {
  1293. case 1: /* DECCKM -- Cursor key */
  1294. xsetmode(set, MODE_APPCURSOR);
  1295. break;
  1296. case 5: /* DECSCNM -- Reverse video */
  1297. xsetmode(set, MODE_REVERSE);
  1298. break;
  1299. case 6: /* DECOM -- Origin */
  1300. MODBIT(term.c.state, set, CURSOR_ORIGIN);
  1301. tmoveato(0, 0);
  1302. break;
  1303. case 7: /* DECAWM -- Auto wrap */
  1304. MODBIT(term.mode, set, MODE_WRAP);
  1305. break;
  1306. case 0: /* Error (IGNORED) */
  1307. case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
  1308. case 3: /* DECCOLM -- Column (IGNORED) */
  1309. case 4: /* DECSCLM -- Scroll (IGNORED) */
  1310. case 8: /* DECARM -- Auto repeat (IGNORED) */
  1311. case 18: /* DECPFF -- Printer feed (IGNORED) */
  1312. case 19: /* DECPEX -- Printer extent (IGNORED) */
  1313. case 42: /* DECNRCM -- National characters (IGNORED) */
  1314. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  1315. break;
  1316. case 25: /* DECTCEM -- Text Cursor Enable Mode */
  1317. xsetmode(!set, MODE_HIDE);
  1318. break;
  1319. case 9: /* X10 mouse compatibility mode */
  1320. xsetpointermotion(0);
  1321. xsetmode(0, MODE_MOUSE);
  1322. xsetmode(set, MODE_MOUSEX10);
  1323. break;
  1324. case 1000: /* 1000: report button press */
  1325. xsetpointermotion(0);
  1326. xsetmode(0, MODE_MOUSE);
  1327. xsetmode(set, MODE_MOUSEBTN);
  1328. break;
  1329. case 1002: /* 1002: report motion on button press */
  1330. xsetpointermotion(0);
  1331. xsetmode(0, MODE_MOUSE);
  1332. xsetmode(set, MODE_MOUSEMOTION);
  1333. break;
  1334. case 1003: /* 1003: enable all mouse motions */
  1335. xsetpointermotion(set);
  1336. xsetmode(0, MODE_MOUSE);
  1337. xsetmode(set, MODE_MOUSEMANY);
  1338. break;
  1339. case 1004: /* 1004: send focus events to tty */
  1340. xsetmode(set, MODE_FOCUS);
  1341. break;
  1342. case 1006: /* 1006: extended reporting mode */
  1343. xsetmode(set, MODE_MOUSESGR);
  1344. break;
  1345. case 1034:
  1346. xsetmode(set, MODE_8BIT);
  1347. break;
  1348. case 1049: /* swap screen & set/restore cursor as xterm */
  1349. if (!allowaltscreen)
  1350. break;
  1351. tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
  1352. /* FALLTHROUGH */
  1353. case 47: /* swap screen */
  1354. case 1047:
  1355. if (!allowaltscreen)
  1356. break;
  1357. alt = IS_SET(MODE_ALTSCREEN);
  1358. if (alt) {
  1359. tclearregion(0, 0, term.col-1,
  1360. term.row-1);
  1361. }
  1362. if (set ^ alt) /* set is always 1 or 0 */
  1363. tswapscreen();
  1364. if (*args != 1049)
  1365. break;
  1366. /* FALLTHROUGH */
  1367. case 1048:
  1368. tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
  1369. break;
  1370. case 2004: /* 2004: bracketed paste mode */
  1371. xsetmode(set, MODE_BRCKTPASTE);
  1372. break;
  1373. /* Not implemented mouse modes. See comments there. */
  1374. case 1001: /* mouse highlight mode; can hang the
  1375. terminal by design when implemented. */
  1376. case 1005: /* UTF-8 mouse mode; will confuse
  1377. applications not supporting UTF-8
  1378. and luit. */
  1379. case 1015: /* urxvt mangled mouse mode; incompatible
  1380. and can be mistaken for other control
  1381. codes. */
  1382. break;
  1383. default:
  1384. fprintf(stderr,
  1385. "erresc: unknown private set/reset mode %d\n",
  1386. *args);
  1387. break;
  1388. }
  1389. } else {
  1390. switch (*args) {
  1391. case 0: /* Error (IGNORED) */
  1392. break;
  1393. case 2:
  1394. xsetmode(set, MODE_KBDLOCK);
  1395. break;
  1396. case 4: /* IRM -- Insertion-replacement */
  1397. MODBIT(term.mode, set, MODE_INSERT);
  1398. break;
  1399. case 12: /* SRM -- Send/Receive */
  1400. MODBIT(term.mode, !set, MODE_ECHO);
  1401. break;
  1402. case 20: /* LNM -- Linefeed/new line */
  1403. MODBIT(term.mode, set, MODE_CRLF);
  1404. break;
  1405. default:
  1406. fprintf(stderr,
  1407. "erresc: unknown set/reset mode %d\n",
  1408. *args);
  1409. break;
  1410. }
  1411. }
  1412. }
  1413. }
  1414. void
  1415. csihandle(void)
  1416. {
  1417. char buf[40];
  1418. int len;
  1419. switch (csiescseq.mode[0]) {
  1420. default:
  1421. unknown:
  1422. fprintf(stderr, "erresc: unknown csi ");
  1423. csidump();
  1424. /* die(""); */
  1425. break;
  1426. case '@': /* ICH -- Insert <n> blank char */
  1427. DEFAULT(csiescseq.arg[0], 1);
  1428. tinsertblank(csiescseq.arg[0]);
  1429. break;
  1430. case 'A': /* CUU -- Cursor <n> Up */
  1431. DEFAULT(csiescseq.arg[0], 1);
  1432. tmoveto(term.c.x, term.c.y-csiescseq.arg[0]);
  1433. break;
  1434. case 'B': /* CUD -- Cursor <n> Down */
  1435. case 'e': /* VPR --Cursor <n> Down */
  1436. DEFAULT(csiescseq.arg[0], 1);
  1437. tmoveto(term.c.x, term.c.y+csiescseq.arg[0]);
  1438. break;
  1439. case 'i': /* MC -- Media Copy */
  1440. switch (csiescseq.arg[0]) {
  1441. case 0:
  1442. tdump();
  1443. break;
  1444. case 1:
  1445. tdumpline(term.c.y);
  1446. break;
  1447. case 2:
  1448. tdumpsel();
  1449. break;
  1450. case 4:
  1451. term.mode &= ~MODE_PRINT;
  1452. break;
  1453. case 5:
  1454. term.mode |= MODE_PRINT;
  1455. break;
  1456. }
  1457. break;
  1458. case 'c': /* DA -- Device Attributes */
  1459. if (csiescseq.arg[0] == 0)
  1460. ttywrite(vtiden, strlen(vtiden), 0);
  1461. break;
  1462. case 'b': /* REP -- if last char is printable print it <n> more times */
  1463. DEFAULT(csiescseq.arg[0], 1);
  1464. if (term.lastc)
  1465. while (csiescseq.arg[0]-- > 0)
  1466. tputc(term.lastc);
  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. /* FALLTHROUGH */
  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 (u < 127 || !IS_SET(MODE_UTF8 | 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. width = 1;
  2088. }
  2089. if (IS_SET(MODE_PRINT))
  2090. tprinter(c, len);
  2091. /*
  2092. * STR sequence must be checked before anything else
  2093. * because it uses all following characters until it
  2094. * receives a ESC, a SUB, a ST or any other C1 control
  2095. * character.
  2096. */
  2097. if (term.esc & ESC_STR) {
  2098. if (u == '\a' || u == 030 || u == 032 || u == 033 ||
  2099. ISCONTROLC1(u)) {
  2100. term.esc &= ~(ESC_START|ESC_STR|ESC_DCS);
  2101. if (IS_SET(MODE_SIXEL)) {
  2102. /* TODO: render sixel */;
  2103. term.mode &= ~MODE_SIXEL;
  2104. return;
  2105. }
  2106. term.esc |= ESC_STR_END;
  2107. goto check_control_code;
  2108. }
  2109. if (IS_SET(MODE_SIXEL)) {
  2110. /* TODO: implement sixel mode */
  2111. return;
  2112. }
  2113. if (term.esc&ESC_DCS && strescseq.len == 0 && u == 'q')
  2114. term.mode |= MODE_SIXEL;
  2115. if (strescseq.len+len >= strescseq.siz) {
  2116. /*
  2117. * Here is a bug in terminals. If the user never sends
  2118. * some code to stop the str or esc command, then st
  2119. * will stop responding. But this is better than
  2120. * silently failing with unknown characters. At least
  2121. * then users will report back.
  2122. *
  2123. * In the case users ever get fixed, here is the code:
  2124. */
  2125. /*
  2126. * term.esc = 0;
  2127. * strhandle();
  2128. */
  2129. if (strescseq.siz > (SIZE_MAX - UTF_SIZ) / 2)
  2130. return;
  2131. strescseq.siz *= 2;
  2132. strescseq.buf = xrealloc(strescseq.buf, strescseq.siz);
  2133. }
  2134. memmove(&strescseq.buf[strescseq.len], c, len);
  2135. strescseq.len += len;
  2136. return;
  2137. }
  2138. check_control_code:
  2139. /*
  2140. * Actions of control codes must be performed as soon they arrive
  2141. * because they can be embedded inside a control sequence, and
  2142. * they must not cause conflicts with sequences.
  2143. */
  2144. if (control) {
  2145. tcontrolcode(u);
  2146. /*
  2147. * control codes are not shown ever
  2148. */
  2149. if (!term.esc)
  2150. term.lastc = 0;
  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 (selected(term.c.x, term.c.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. term.lastc = u;
  2197. if (width == 2) {
  2198. gp->mode |= ATTR_WIDE;
  2199. if (term.c.x+1 < term.col) {
  2200. gp[1].u = '\0';
  2201. gp[1].mode = ATTR_WDUMMY;
  2202. }
  2203. }
  2204. if (term.c.x+width < term.col) {
  2205. tmoveto(term.c.x+width, term.c.y);
  2206. } else {
  2207. term.c.state |= CURSOR_WRAPNEXT;
  2208. }
  2209. }
  2210. int
  2211. twrite(const char *buf, int buflen, int show_ctrl)
  2212. {
  2213. int charsize;
  2214. Rune u;
  2215. int n;
  2216. for (n = 0; n < buflen; n += charsize) {
  2217. if (IS_SET(MODE_UTF8) && !IS_SET(MODE_SIXEL)) {
  2218. /* process a complete utf8 char */
  2219. charsize = utf8decode(buf + n, &u, buflen - n);
  2220. if (charsize == 0)
  2221. break;
  2222. } else {
  2223. u = buf[n] & 0xFF;
  2224. charsize = 1;
  2225. }
  2226. if (show_ctrl && ISCONTROL(u)) {
  2227. if (u & 0x80) {
  2228. u &= 0x7f;
  2229. tputc('^');
  2230. tputc('[');
  2231. } else if (u != '\n' && u != '\r' && u != '\t') {
  2232. u ^= 0x40;
  2233. tputc('^');
  2234. }
  2235. }
  2236. tputc(u);
  2237. }
  2238. return n;
  2239. }
  2240. void
  2241. tresize(int col, int row)
  2242. {
  2243. int i;
  2244. int minrow = MIN(row, term.row);
  2245. int mincol = MIN(col, term.col);
  2246. int *bp;
  2247. TCursor c;
  2248. if (col < 1 || row < 1) {
  2249. fprintf(stderr,
  2250. "tresize: error resizing to %dx%d\n", col, row);
  2251. return;
  2252. }
  2253. /*
  2254. * slide screen to keep cursor where we expect it -
  2255. * tscrollup would work here, but we can optimize to
  2256. * memmove because we're freeing the earlier lines
  2257. */
  2258. for (i = 0; i <= term.c.y - row; i++) {
  2259. free(term.line[i]);
  2260. free(term.alt[i]);
  2261. }
  2262. /* ensure that both src and dst are not NULL */
  2263. if (i > 0) {
  2264. memmove(term.line, term.line + i, row * sizeof(Line));
  2265. memmove(term.alt, term.alt + i, row * sizeof(Line));
  2266. }
  2267. for (i += row; i < term.row; i++) {
  2268. free(term.line[i]);
  2269. free(term.alt[i]);
  2270. }
  2271. /* resize to new height */
  2272. term.line = xrealloc(term.line, row * sizeof(Line));
  2273. term.alt = xrealloc(term.alt, row * sizeof(Line));
  2274. term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
  2275. term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
  2276. /* resize each row to new width, zero-pad if needed */
  2277. for (i = 0; i < minrow; i++) {
  2278. term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph));
  2279. term.alt[i] = xrealloc(term.alt[i], col * sizeof(Glyph));
  2280. }
  2281. /* allocate any new rows */
  2282. for (/* i = minrow */; i < row; i++) {
  2283. term.line[i] = xmalloc(col * sizeof(Glyph));
  2284. term.alt[i] = xmalloc(col * sizeof(Glyph));
  2285. }
  2286. if (col > term.col) {
  2287. bp = term.tabs + term.col;
  2288. memset(bp, 0, sizeof(*term.tabs) * (col - term.col));
  2289. while (--bp > term.tabs && !*bp)
  2290. /* nothing */ ;
  2291. for (bp += tabspaces; bp < term.tabs + col; bp += tabspaces)
  2292. *bp = 1;
  2293. }
  2294. /* update terminal size */
  2295. term.col = col;
  2296. term.row = row;
  2297. /* reset scrolling region */
  2298. tsetscroll(0, row-1);
  2299. /* make use of the LIMIT in tmoveto */
  2300. tmoveto(term.c.x, term.c.y);
  2301. /* Clearing both screens (it makes dirty all lines) */
  2302. c = term.c;
  2303. for (i = 0; i < 2; i++) {
  2304. if (mincol < col && 0 < minrow) {
  2305. tclearregion(mincol, 0, col - 1, minrow - 1);
  2306. }
  2307. if (0 < col && minrow < row) {
  2308. tclearregion(0, minrow, col - 1, row - 1);
  2309. }
  2310. tswapscreen();
  2311. tcursor(CURSOR_LOAD);
  2312. }
  2313. term.c = c;
  2314. }
  2315. void
  2316. resettitle(void)
  2317. {
  2318. xsettitle(NULL);
  2319. }
  2320. void
  2321. drawregion(int x1, int y1, int x2, int y2)
  2322. {
  2323. int y;
  2324. for (y = y1; y < y2; y++) {
  2325. if (!term.dirty[y])
  2326. continue;
  2327. term.dirty[y] = 0;
  2328. xdrawline(term.line[y], x1, y, x2);
  2329. }
  2330. }
  2331. void
  2332. draw(void)
  2333. {
  2334. int cx = term.c.x, ocx = term.ocx, ocy = term.ocy;
  2335. if (!xstartdraw())
  2336. return;
  2337. /* adjust cursor position */
  2338. LIMIT(term.ocx, 0, term.col-1);
  2339. LIMIT(term.ocy, 0, term.row-1);
  2340. if (term.line[term.ocy][term.ocx].mode & ATTR_WDUMMY)
  2341. term.ocx--;
  2342. if (term.line[term.c.y][cx].mode & ATTR_WDUMMY)
  2343. cx--;
  2344. drawregion(0, 0, term.col, term.row);
  2345. xdrawcursor(cx, term.c.y, term.line[term.c.y][cx],
  2346. term.ocx, term.ocy, term.line[term.ocy][term.ocx]);
  2347. term.ocx = cx;
  2348. term.ocy = term.c.y;
  2349. xfinishdraw();
  2350. if (ocx != term.ocx || ocy != term.ocy)
  2351. xximspot(term.ocx, term.ocy);
  2352. }
  2353. void
  2354. redraw(void)
  2355. {
  2356. tfulldirt();
  2357. draw();
  2358. }