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.

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