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

2466 lines
51 KiB

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