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.

2603 lines
54 KiB

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