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.

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