My build of the simple terminal from suckless.org.
 
 
 
 
 

2597 rindas
54 KiB

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