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.

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