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.

2699 lines
55 KiB

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