Simple Terminal from SuckLess
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2534 lines
52 KiB

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