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.

2602 lines
54 KiB

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