My build of suckless st terminal
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2681 lines
56 KiB

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