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.

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