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.

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