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.

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