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

2513 lines
52 KiB

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