Simple Terminal from SuckLess
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.

2627 lines
55 KiB

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