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.

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