Another copy of my dotfiles. Because I don't completely trust GitHub.
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.

2758 lines
58 KiB

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