My patched version of suckless' terminal - st.
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.

2666 lines
56 KiB

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