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.

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