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.

2618 lines
54 KiB

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