My build of suckless st terminal
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1940 lines
44 KiB

14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
15 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
  1. /* See LICENSE for licence details. */
  2. #define _XOPEN_SOURCE 600
  3. #include <ctype.h>
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <limits.h>
  7. #include <locale.h>
  8. #include <stdarg.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <signal.h>
  13. #include <sys/ioctl.h>
  14. #include <sys/select.h>
  15. #include <sys/stat.h>
  16. #include <sys/types.h>
  17. #include <sys/wait.h>
  18. #include <unistd.h>
  19. #include <X11/Xatom.h>
  20. #include <X11/Xlib.h>
  21. #include <X11/Xutil.h>
  22. #include <X11/cursorfont.h>
  23. #include <X11/keysym.h>
  24. #include <sys/time.h>
  25. #include <time.h>
  26. #if defined(__linux)
  27. #include <pty.h>
  28. #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  29. #include <util.h>
  30. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  31. #include <libutil.h>
  32. #endif
  33. #define USAGE \
  34. "st-" VERSION ", (c) 2010 st engineers\n" \
  35. "usage: st [-t title] [-c class] [-v] [-e cmd]\n"
  36. /* Arbitrary sizes */
  37. #define ESC_TITLE_SIZ 256
  38. #define ESC_BUF_SIZ 256
  39. #define ESC_ARG_SIZ 16
  40. #define DRAW_BUF_SIZ 1024
  41. #define UTF_SIZ 4
  42. #define SERRNO strerror(errno)
  43. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  44. #define MAX(a, b) ((a) < (b) ? (b) : (a))
  45. #define LEN(a) (sizeof(a) / sizeof(a[0]))
  46. #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
  47. #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
  48. #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
  49. #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
  50. #define IS_SET(flag) (term.mode & (flag))
  51. #define TIMEDIFFERENCE(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
  52. /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
  53. enum { ATTR_NULL=0 , ATTR_REVERSE=1 , ATTR_UNDERLINE=2, ATTR_BOLD=4, ATTR_GFX=8 };
  54. enum { CURSOR_UP, CURSOR_DOWN, CURSOR_LEFT, CURSOR_RIGHT,
  55. CURSOR_SAVE, CURSOR_LOAD };
  56. enum { CURSOR_DEFAULT = 0, CURSOR_HIDE = 1, CURSOR_WRAPNEXT = 2 };
  57. enum { GLYPH_SET=1, GLYPH_DIRTY=2 };
  58. enum { MODE_WRAP=1, MODE_INSERT=2, MODE_APPKEYPAD=4, MODE_ALTSCREEN=8,
  59. MODE_CRLF=16 };
  60. enum { ESC_START=1, ESC_CSI=2, ESC_OSC=4, ESC_TITLE=8, ESC_ALTCHARSET=16 };
  61. enum { WIN_VISIBLE=1, WIN_REDRAW=2, WIN_FOCUSED=4 };
  62. #undef B0
  63. enum { B0=1, B1=2, B2=4, B3=8, B4=16, B5=32, B6=64, B7=128 };
  64. typedef struct {
  65. char c[UTF_SIZ]; /* character code */
  66. char mode; /* attribute flags */
  67. int fg; /* foreground */
  68. int bg; /* background */
  69. char state; /* state flags */
  70. } Glyph;
  71. typedef Glyph* Line;
  72. typedef struct {
  73. Glyph attr; /* current char attributes */
  74. int x;
  75. int y;
  76. char state;
  77. } TCursor;
  78. /* CSI Escape sequence structs */
  79. /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
  80. typedef struct {
  81. char buf[ESC_BUF_SIZ]; /* raw string */
  82. int len; /* raw string length */
  83. char priv;
  84. int arg[ESC_ARG_SIZ];
  85. int narg; /* nb of args */
  86. char mode;
  87. } CSIEscape;
  88. /* Internal representation of the screen */
  89. typedef struct {
  90. int row; /* nb row */
  91. int col; /* nb col */
  92. Line* line; /* screen */
  93. Line* alt; /* alternate screen */
  94. TCursor c; /* cursor */
  95. int top; /* top scroll limit */
  96. int bot; /* bottom scroll limit */
  97. int mode; /* terminal mode flags */
  98. int esc; /* escape state flags */
  99. char title[ESC_TITLE_SIZ];
  100. int titlelen;
  101. } Term;
  102. /* Purely graphic info */
  103. typedef struct {
  104. Display* dpy;
  105. Colormap cmap;
  106. Window win;
  107. Pixmap buf;
  108. XIM xim;
  109. XIC xic;
  110. int scr;
  111. int w; /* window width */
  112. int h; /* window height */
  113. int bufw; /* pixmap width */
  114. int bufh; /* pixmap height */
  115. int ch; /* char height */
  116. int cw; /* char width */
  117. char state; /* focus, redraw, visible */
  118. } XWindow;
  119. typedef struct {
  120. KeySym k;
  121. unsigned int mask;
  122. char s[ESC_BUF_SIZ];
  123. } Key;
  124. /* Drawing Context */
  125. typedef struct {
  126. unsigned long col[256];
  127. GC gc;
  128. struct {
  129. int ascent;
  130. int descent;
  131. short lbearing;
  132. short rbearing;
  133. XFontSet set;
  134. } font, bfont;
  135. } DC;
  136. /* TODO: use better name for vars... */
  137. typedef struct {
  138. int mode;
  139. int bx, by;
  140. int ex, ey;
  141. struct {int x, y;} b, e;
  142. char *clip;
  143. Atom xtarget;
  144. struct timeval tclick1;
  145. struct timeval tclick2;
  146. } Selection;
  147. #include "config.h"
  148. static void die(const char *errstr, ...);
  149. static void draw();
  150. static void drawregion(int, int, int, int);
  151. static void execsh(void);
  152. static void sigchld(int);
  153. static void run(void);
  154. static void csidump(void);
  155. static void csihandle(void);
  156. static void csiparse(void);
  157. static void csireset(void);
  158. static void tclearregion(int, int, int, int);
  159. static void tcursor(int);
  160. static void tdeletechar(int);
  161. static void tdeleteline(int);
  162. static void tinsertblank(int);
  163. static void tinsertblankline(int);
  164. static void tmoveto(int, int);
  165. static void tnew(int, int);
  166. static void tnewline(int);
  167. static void tputtab(void);
  168. static void tputc(char*);
  169. static void treset(void);
  170. static int tresize(int, int);
  171. static void tscrollup(int, int);
  172. static void tscrolldown(int, int);
  173. static void tsetattr(int*, int);
  174. static void tsetchar(char*);
  175. static void tsetscroll(int, int);
  176. static void tswapscreen(void);
  177. static void ttynew(void);
  178. static void ttyread(void);
  179. static void ttyresize(int, int);
  180. static void ttywrite(const char *, size_t);
  181. static void xdraws(char *, Glyph, int, int, int, int);
  182. static void xhints(void);
  183. static void xclear(int, int, int, int);
  184. static void xdrawcursor(void);
  185. static void xinit(void);
  186. static void xloadcols(void);
  187. static void xseturgency(int);
  188. static void xsetsel(char*);
  189. static void xresize(int, int);
  190. static void expose(XEvent *);
  191. static void visibility(XEvent *);
  192. static void unmap(XEvent *);
  193. static char* kmap(KeySym, unsigned int state);
  194. static void kpress(XEvent *);
  195. static void resize(XEvent *);
  196. static void focus(XEvent *);
  197. static void brelease(XEvent *);
  198. static void bpress(XEvent *);
  199. static void bmotion(XEvent *);
  200. static void selnotify(XEvent *);
  201. static void selrequest(XEvent *);
  202. static void selinit(void);
  203. static inline int selected(int, int);
  204. static void selcopy(void);
  205. static void selpaste();
  206. static int utf8decode(char *, long *);
  207. static int utf8encode(long *, char *);
  208. static int utf8size(char *);
  209. static int isfullutf8(char *, int);
  210. static void (*handler[LASTEvent])(XEvent *) = {
  211. [KeyPress] = kpress,
  212. [ConfigureNotify] = resize,
  213. [VisibilityNotify] = visibility,
  214. [UnmapNotify] = unmap,
  215. [Expose] = expose,
  216. [FocusIn] = focus,
  217. [FocusOut] = focus,
  218. [MotionNotify] = bmotion,
  219. [ButtonPress] = bpress,
  220. [ButtonRelease] = brelease,
  221. [SelectionNotify] = selnotify,
  222. [SelectionRequest] = selrequest,
  223. };
  224. /* Globals */
  225. static DC dc;
  226. static XWindow xw;
  227. static Term term;
  228. static CSIEscape escseq;
  229. static int cmdfd;
  230. static pid_t pid;
  231. static Selection sel;
  232. static char **opt_cmd = NULL;
  233. static char *opt_title = NULL;
  234. static char *opt_class = NULL;
  235. int
  236. utf8decode(char *s, long *u) {
  237. unsigned char c;
  238. int i, n, rtn;
  239. rtn = 1;
  240. c = *s;
  241. if(~c&B7) { /* 0xxxxxxx */
  242. *u = c;
  243. return rtn;
  244. } else if((c&(B7|B6|B5)) == (B7|B6)) { /* 110xxxxx */
  245. *u = c&(B4|B3|B2|B1|B0);
  246. n = 1;
  247. } else if((c&(B7|B6|B5|B4)) == (B7|B6|B5)) { /* 1110xxxx */
  248. *u = c&(B3|B2|B1|B0);
  249. n = 2;
  250. } else if((c&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4)) { /* 11110xxx */
  251. *u = c&(B2|B1|B0);
  252. n = 3;
  253. } else
  254. goto invalid;
  255. for(i=n,++s; i>0; --i,++rtn,++s) {
  256. c = *s;
  257. if((c&(B7|B6)) != B7) /* 10xxxxxx */
  258. goto invalid;
  259. *u <<= 6;
  260. *u |= c&(B5|B4|B3|B2|B1|B0);
  261. }
  262. if((n == 1 && *u < 0x80) ||
  263. (n == 2 && *u < 0x800) ||
  264. (n == 3 && *u < 0x10000) ||
  265. (*u >= 0xD800 && *u <= 0xDFFF))
  266. goto invalid;
  267. return rtn;
  268. invalid:
  269. *u = 0xFFFD;
  270. return rtn;
  271. }
  272. int
  273. utf8encode(long *u, char *s) {
  274. unsigned char *sp;
  275. unsigned long uc;
  276. int i, n;
  277. sp = (unsigned char*) s;
  278. uc = *u;
  279. if(uc < 0x80) {
  280. *sp = uc; /* 0xxxxxxx */
  281. return 1;
  282. } else if(*u < 0x800) {
  283. *sp = (uc >> 6) | (B7|B6); /* 110xxxxx */
  284. n = 1;
  285. } else if(uc < 0x10000) {
  286. *sp = (uc >> 12) | (B7|B6|B5); /* 1110xxxx */
  287. n = 2;
  288. } else if(uc <= 0x10FFFF) {
  289. *sp = (uc >> 18) | (B7|B6|B5|B4); /* 11110xxx */
  290. n = 3;
  291. } else {
  292. goto invalid;
  293. }
  294. for(i=n,++sp; i>0; --i,++sp)
  295. *sp = ((uc >> 6*(i-1)) & (B5|B4|B3|B2|B1|B0)) | B7; /* 10xxxxxx */
  296. return n+1;
  297. invalid:
  298. /* U+FFFD */
  299. *s++ = '\xEF';
  300. *s++ = '\xBF';
  301. *s = '\xBD';
  302. return 3;
  303. }
  304. /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
  305. UTF-8 otherwise return 0 */
  306. int
  307. isfullutf8(char *s, int b) {
  308. unsigned char *c1, *c2, *c3;
  309. c1 = (unsigned char *) s;
  310. c2 = (unsigned char *) ++s;
  311. c3 = (unsigned char *) ++s;
  312. if(b < 1)
  313. return 0;
  314. else if((*c1&(B7|B6|B5)) == (B7|B6) && b == 1)
  315. return 0;
  316. else if((*c1&(B7|B6|B5|B4)) == (B7|B6|B5) &&
  317. ((b == 1) ||
  318. ((b == 2) && (*c2&(B7|B6)) == B7)))
  319. return 0;
  320. else if((*c1&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4) &&
  321. ((b == 1) ||
  322. ((b == 2) && (*c2&(B7|B6)) == B7) ||
  323. ((b == 3) && (*c2&(B7|B6)) == B7 && (*c3&(B7|B6)) == B7)))
  324. return 0;
  325. else
  326. return 1;
  327. }
  328. int
  329. utf8size(char *s) {
  330. unsigned char c = *s;
  331. if (~c&B7)
  332. return 1;
  333. else if ((c&(B7|B6|B5)) == (B7|B6))
  334. return 2;
  335. else if ((c&(B7|B6|B5|B4)) == (B7|B6|B5))
  336. return 3;
  337. else
  338. return 4;
  339. }
  340. void
  341. selinit(void) {
  342. sel.tclick1.tv_sec = 0;
  343. sel.tclick1.tv_usec = 0;
  344. sel.mode = 0;
  345. sel.bx = -1;
  346. sel.clip = NULL;
  347. sel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
  348. if(sel.xtarget == None)
  349. sel.xtarget = XA_STRING;
  350. }
  351. static inline int
  352. selected(int x, int y) {
  353. if(sel.ey == y && sel.by == y) {
  354. int bx = MIN(sel.bx, sel.ex);
  355. int ex = MAX(sel.bx, sel.ex);
  356. return BETWEEN(x, bx, ex);
  357. }
  358. return ((sel.b.y < y&&y < sel.e.y) || (y==sel.e.y && x<=sel.e.x))
  359. || (y==sel.b.y && x>=sel.b.x && (x<=sel.e.x || sel.b.y!=sel.e.y));
  360. }
  361. void
  362. getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
  363. if(b)
  364. *b = e->xbutton.button;
  365. *x = (e->xbutton.x - BORDER)/xw.cw;
  366. *y = (e->xbutton.y - BORDER)/xw.ch;
  367. sel.b.x = sel.by < sel.ey ? sel.bx : sel.ex;
  368. sel.b.y = MIN(sel.by, sel.ey);
  369. sel.e.x = sel.by < sel.ey ? sel.ex : sel.bx;
  370. sel.e.y = MAX(sel.by, sel.ey);
  371. }
  372. void
  373. bpress(XEvent *e) {
  374. sel.mode = 1;
  375. sel.ex = sel.bx = (e->xbutton.x - BORDER)/xw.cw;
  376. sel.ey = sel.by = (e->xbutton.y - BORDER)/xw.ch;
  377. }
  378. void
  379. selcopy(void) {
  380. char *str, *ptr;
  381. int x, y, sz, sl, ls = 0;
  382. if(sel.bx == -1)
  383. str = NULL;
  384. else {
  385. sz = (term.col+1) * (sel.e.y-sel.b.y+1) * UTF_SIZ;
  386. ptr = str = malloc(sz);
  387. for(y = 0; y < term.row; y++) {
  388. for(x = 0; x < term.col; x++)
  389. if(term.line[y][x].state & GLYPH_SET && (ls = selected(x, y))) {
  390. sl = utf8size(term.line[y][x].c);
  391. memcpy(ptr, term.line[y][x].c, sl);
  392. ptr += sl;
  393. }
  394. if(ls && y < sel.e.y)
  395. *ptr++ = '\n';
  396. }
  397. *ptr = 0;
  398. }
  399. xsetsel(str);
  400. }
  401. void
  402. selnotify(XEvent *e) {
  403. unsigned long nitems;
  404. unsigned long ofs, rem;
  405. int format;
  406. unsigned char *data;
  407. Atom type;
  408. ofs = 0;
  409. do {
  410. if(XGetWindowProperty(xw.dpy, xw.win, XA_PRIMARY, ofs, BUFSIZ/4,
  411. False, AnyPropertyType, &type, &format,
  412. &nitems, &rem, &data)) {
  413. fprintf(stderr, "Clipboard allocation failed\n");
  414. return;
  415. }
  416. ttywrite((const char *) data, nitems * format / 8);
  417. XFree(data);
  418. /* number of 32-bit chunks returned */
  419. ofs += nitems * format / 32;
  420. } while(rem > 0);
  421. }
  422. void
  423. selpaste() {
  424. XConvertSelection(xw.dpy, XA_PRIMARY, sel.xtarget, XA_PRIMARY, xw.win, CurrentTime);
  425. }
  426. void
  427. selrequest(XEvent *e) {
  428. XSelectionRequestEvent *xsre;
  429. XSelectionEvent xev;
  430. Atom xa_targets;
  431. xsre = (XSelectionRequestEvent *) e;
  432. xev.type = SelectionNotify;
  433. xev.requestor = xsre->requestor;
  434. xev.selection = xsre->selection;
  435. xev.target = xsre->target;
  436. xev.time = xsre->time;
  437. /* reject */
  438. xev.property = None;
  439. xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
  440. if(xsre->target == xa_targets) {
  441. /* respond with the supported type */
  442. Atom string = sel.xtarget;
  443. XChangeProperty(xsre->display, xsre->requestor, xsre->property,
  444. XA_ATOM, 32, PropModeReplace,
  445. (unsigned char *) &string, 1);
  446. xev.property = xsre->property;
  447. } else if(xsre->target == sel.xtarget) {
  448. XChangeProperty(xsre->display, xsre->requestor, xsre->property,
  449. xsre->target, 8, PropModeReplace,
  450. (unsigned char *) sel.clip, strlen(sel.clip));
  451. xev.property = xsre->property;
  452. }
  453. /* all done, send a notification to the listener */
  454. if(!XSendEvent(xsre->display, xsre->requestor, True, 0, (XEvent *) &xev))
  455. fprintf(stderr, "Error sending SelectionNotify event\n");
  456. }
  457. void
  458. xsetsel(char *str) {
  459. /* register the selection for both the clipboard and the primary */
  460. Atom clipboard;
  461. free(sel.clip);
  462. sel.clip = str;
  463. XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, CurrentTime);
  464. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  465. XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
  466. XFlush(xw.dpy);
  467. }
  468. void
  469. brelease(XEvent *e) {
  470. int b;
  471. sel.mode = 0;
  472. getbuttoninfo(e, &b, &sel.ex, &sel.ey);
  473. if(sel.bx == sel.ex && sel.by == sel.ey) {
  474. sel.bx = -1;
  475. if(b == 2)
  476. selpaste();
  477. else if(b == 1) {
  478. /* double click to select word */
  479. struct timeval now;
  480. gettimeofday(&now, NULL);
  481. if(TIMEDIFFERENCE(now, sel.tclick1) <= DOUBLECLICK_TIMEOUT) {
  482. sel.bx = sel.ex;
  483. while(term.line[sel.ey][sel.bx-1].state & GLYPH_SET &&
  484. term.line[sel.ey][sel.bx-1].c[0] != ' ') sel.bx--;
  485. sel.b.x = sel.bx;
  486. while(term.line[sel.ey][sel.ex+1].state & GLYPH_SET &&
  487. term.line[sel.ey][sel.ex+1].c[0] != ' ') sel.ex++;
  488. sel.e.x = sel.ex;
  489. sel.b.y = sel.e.y = sel.ey;
  490. selcopy();
  491. }
  492. /* triple click on the line */
  493. if(TIMEDIFFERENCE(now, sel.tclick2) <= TRIPLECLICK_TIMEOUT) {
  494. sel.b.x = sel.bx = 0;
  495. sel.e.x = sel.ex = term.col;
  496. sel.b.y = sel.e.y = sel.ey;
  497. selcopy();
  498. }
  499. }
  500. } else {
  501. if(b == 1)
  502. selcopy();
  503. }
  504. memcpy(&sel.tclick2, &sel.tclick1, sizeof(struct timeval));
  505. gettimeofday(&sel.tclick1, NULL);
  506. draw();
  507. }
  508. void
  509. bmotion(XEvent *e) {
  510. if(sel.mode) {
  511. int oldey = sel.ey,
  512. oldex = sel.ex;
  513. getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
  514. if(oldey != sel.ey || oldex != sel.ex) {
  515. int starty = MIN(oldey, sel.ey);
  516. int endy = MAX(oldey, sel.ey);
  517. drawregion(0, (starty > 0 ? starty : 0), term.col, (sel.ey < term.row ? endy+1 : term.row));
  518. }
  519. }
  520. }
  521. void
  522. die(const char *errstr, ...) {
  523. va_list ap;
  524. va_start(ap, errstr);
  525. vfprintf(stderr, errstr, ap);
  526. va_end(ap);
  527. exit(EXIT_FAILURE);
  528. }
  529. void
  530. execsh(void) {
  531. char **args;
  532. char *envshell = getenv("SHELL");
  533. DEFAULT(envshell, "sh");
  534. putenv("TERM="TNAME);
  535. args = opt_cmd ? opt_cmd : (char*[]){envshell, "-i", NULL};
  536. execvp(args[0], args);
  537. exit(EXIT_FAILURE);
  538. }
  539. void
  540. sigchld(int a) {
  541. int stat = 0;
  542. if(waitpid(pid, &stat, 0) < 0)
  543. die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
  544. if(WIFEXITED(stat))
  545. exit(WEXITSTATUS(stat));
  546. else
  547. exit(EXIT_FAILURE);
  548. }
  549. void
  550. ttynew(void) {
  551. int m, s;
  552. /* seems to work fine on linux, openbsd and freebsd */
  553. struct winsize w = {term.row, term.col, 0, 0};
  554. if(openpty(&m, &s, NULL, NULL, &w) < 0)
  555. die("openpty failed: %s\n", SERRNO);
  556. switch(pid = fork()) {
  557. case -1:
  558. die("fork failed\n");
  559. break;
  560. case 0:
  561. setsid(); /* create a new process group */
  562. dup2(s, STDIN_FILENO);
  563. dup2(s, STDOUT_FILENO);
  564. dup2(s, STDERR_FILENO);
  565. if(ioctl(s, TIOCSCTTY, NULL) < 0)
  566. die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
  567. close(s);
  568. close(m);
  569. execsh();
  570. break;
  571. default:
  572. close(s);
  573. cmdfd = m;
  574. signal(SIGCHLD, sigchld);
  575. }
  576. }
  577. void
  578. dump(char c) {
  579. static int col;
  580. fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
  581. if(++col % 10 == 0)
  582. fprintf(stderr, "\n");
  583. }
  584. void
  585. ttyread(void) {
  586. static char buf[BUFSIZ];
  587. static int buflen = 0;
  588. char *ptr;
  589. char s[UTF_SIZ];
  590. int charsize; /* size of utf8 char in bytes */
  591. long utf8c;
  592. int ret;
  593. /* append read bytes to unprocessed bytes */
  594. if((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
  595. die("Couldn't read from shell: %s\n", SERRNO);
  596. /* process every complete utf8 char */
  597. buflen += ret;
  598. ptr = buf;
  599. while(buflen >= UTF_SIZ || isfullutf8(ptr,buflen)) {
  600. charsize = utf8decode(ptr, &utf8c);
  601. utf8encode(&utf8c, s);
  602. tputc(s);
  603. ptr += charsize;
  604. buflen -= charsize;
  605. }
  606. /* keep any uncomplete utf8 char for the next call */
  607. memmove(buf, ptr, buflen);
  608. }
  609. void
  610. ttywrite(const char *s, size_t n) {
  611. {size_t nn;
  612. for(nn = 0; nn < n; nn++)
  613. dump(s[nn]);
  614. }
  615. if(write(cmdfd, s, n) == -1)
  616. die("write error on tty: %s\n", SERRNO);
  617. }
  618. void
  619. ttyresize(int x, int y) {
  620. struct winsize w;
  621. w.ws_row = term.row;
  622. w.ws_col = term.col;
  623. w.ws_xpixel = w.ws_ypixel = 0;
  624. if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
  625. fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
  626. }
  627. void
  628. tcursor(int mode) {
  629. static TCursor c;
  630. if(mode == CURSOR_SAVE)
  631. c = term.c;
  632. else if(mode == CURSOR_LOAD)
  633. term.c = c, tmoveto(c.x, c.y);
  634. }
  635. void
  636. treset(void) {
  637. term.c = (TCursor){{
  638. .mode = ATTR_NULL,
  639. .fg = DefaultFG,
  640. .bg = DefaultBG
  641. }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
  642. term.top = 0, term.bot = term.row - 1;
  643. term.mode = MODE_WRAP;
  644. tclearregion(0, 0, term.col-1, term.row-1);
  645. }
  646. void
  647. tnew(int col, int row) {
  648. /* set screen size */
  649. term.row = row, term.col = col;
  650. term.line = malloc(term.row * sizeof(Line));
  651. term.alt = malloc(term.row * sizeof(Line));
  652. for(row = 0 ; row < term.row; row++) {
  653. term.line[row] = malloc(term.col * sizeof(Glyph));
  654. term.alt [row] = malloc(term.col * sizeof(Glyph));
  655. }
  656. /* setup screen */
  657. treset();
  658. }
  659. void
  660. tswapscreen(void) {
  661. Line* tmp = term.line;
  662. term.line = term.alt;
  663. term.alt = tmp;
  664. term.mode ^= MODE_ALTSCREEN;
  665. }
  666. void
  667. tscrolldown(int orig, int n) {
  668. int i;
  669. Line temp;
  670. LIMIT(n, 0, term.bot-orig+1);
  671. tclearregion(0, term.bot-n+1, term.col-1, term.bot);
  672. for(i = term.bot; i >= orig+n; i--) {
  673. temp = term.line[i];
  674. term.line[i] = term.line[i-n];
  675. term.line[i-n] = temp;
  676. }
  677. }
  678. void
  679. tscrollup(int orig, int n) {
  680. int i;
  681. Line temp;
  682. LIMIT(n, 0, term.bot-orig+1);
  683. tclearregion(0, orig, term.col-1, orig+n-1);
  684. for(i = orig; i <= term.bot-n; i++) {
  685. temp = term.line[i];
  686. term.line[i] = term.line[i+n];
  687. term.line[i+n] = temp;
  688. }
  689. }
  690. void
  691. tnewline(int first_col) {
  692. int y = term.c.y;
  693. if(y == term.bot)
  694. tscrollup(term.top, 1);
  695. else
  696. y++;
  697. tmoveto(first_col ? 0 : term.c.x, y);
  698. }
  699. void
  700. csiparse(void) {
  701. /* int noarg = 1; */
  702. char *p = escseq.buf;
  703. escseq.narg = 0;
  704. if(*p == '?')
  705. escseq.priv = 1, p++;
  706. while(p < escseq.buf+escseq.len) {
  707. while(isdigit(*p)) {
  708. escseq.arg[escseq.narg] *= 10;
  709. escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
  710. }
  711. if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
  712. escseq.narg++, p++;
  713. else {
  714. escseq.mode = *p;
  715. escseq.narg++;
  716. return;
  717. }
  718. }
  719. }
  720. void
  721. tmoveto(int x, int y) {
  722. LIMIT(x, 0, term.col-1);
  723. LIMIT(y, 0, term.row-1);
  724. term.c.state &= ~CURSOR_WRAPNEXT;
  725. term.c.x = x;
  726. term.c.y = y;
  727. }
  728. void
  729. tsetchar(char *c) {
  730. term.line[term.c.y][term.c.x] = term.c.attr;
  731. memcpy(term.line[term.c.y][term.c.x].c, c, UTF_SIZ);
  732. term.line[term.c.y][term.c.x].state |= GLYPH_SET;
  733. }
  734. void
  735. tclearregion(int x1, int y1, int x2, int y2) {
  736. int x, y, temp;
  737. if(x1 > x2)
  738. temp = x1, x1 = x2, x2 = temp;
  739. if(y1 > y2)
  740. temp = y1, y1 = y2, y2 = temp;
  741. LIMIT(x1, 0, term.col-1);
  742. LIMIT(x2, 0, term.col-1);
  743. LIMIT(y1, 0, term.row-1);
  744. LIMIT(y2, 0, term.row-1);
  745. for(y = y1; y <= y2; y++)
  746. for(x = x1; x <= x2; x++)
  747. term.line[y][x].state = 0;
  748. }
  749. void
  750. tdeletechar(int n) {
  751. int src = term.c.x + n;
  752. int dst = term.c.x;
  753. int size = term.col - src;
  754. if(src >= term.col) {
  755. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  756. return;
  757. }
  758. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  759. tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
  760. }
  761. void
  762. tinsertblank(int n) {
  763. int src = term.c.x;
  764. int dst = src + n;
  765. int size = term.col - dst;
  766. if(dst >= term.col) {
  767. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  768. return;
  769. }
  770. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  771. tclearregion(src, term.c.y, dst - 1, term.c.y);
  772. }
  773. void
  774. tinsertblankline(int n) {
  775. if(term.c.y < term.top || term.c.y > term.bot)
  776. return;
  777. tscrolldown(term.c.y, n);
  778. }
  779. void
  780. tdeleteline(int n) {
  781. if(term.c.y < term.top || term.c.y > term.bot)
  782. return;
  783. tscrollup(term.c.y, n);
  784. }
  785. void
  786. tsetattr(int *attr, int l) {
  787. int i;
  788. for(i = 0; i < l; i++) {
  789. switch(attr[i]) {
  790. case 0:
  791. term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
  792. term.c.attr.fg = DefaultFG;
  793. term.c.attr.bg = DefaultBG;
  794. break;
  795. case 1:
  796. term.c.attr.mode |= ATTR_BOLD;
  797. break;
  798. case 4:
  799. term.c.attr.mode |= ATTR_UNDERLINE;
  800. break;
  801. case 7:
  802. term.c.attr.mode |= ATTR_REVERSE;
  803. break;
  804. case 22:
  805. term.c.attr.mode &= ~ATTR_BOLD;
  806. break;
  807. case 24:
  808. term.c.attr.mode &= ~ATTR_UNDERLINE;
  809. break;
  810. case 27:
  811. term.c.attr.mode &= ~ATTR_REVERSE;
  812. break;
  813. case 38:
  814. if (i + 2 < l && attr[i + 1] == 5) {
  815. i += 2;
  816. if (BETWEEN(attr[i], 0, 255))
  817. term.c.attr.fg = attr[i];
  818. else
  819. fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
  820. }
  821. else
  822. fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
  823. break;
  824. case 39:
  825. term.c.attr.fg = DefaultFG;
  826. break;
  827. case 48:
  828. if (i + 2 < l && attr[i + 1] == 5) {
  829. i += 2;
  830. if (BETWEEN(attr[i], 0, 255))
  831. term.c.attr.bg = attr[i];
  832. else
  833. fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
  834. }
  835. else
  836. fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
  837. break;
  838. case 49:
  839. term.c.attr.bg = DefaultBG;
  840. break;
  841. default:
  842. if(BETWEEN(attr[i], 30, 37))
  843. term.c.attr.fg = attr[i] - 30;
  844. else if(BETWEEN(attr[i], 40, 47))
  845. term.c.attr.bg = attr[i] - 40;
  846. else if(BETWEEN(attr[i], 90, 97))
  847. term.c.attr.fg = attr[i] - 90 + 8;
  848. else if(BETWEEN(attr[i], 100, 107))
  849. term.c.attr.fg = attr[i] - 100 + 8;
  850. else
  851. fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]), csidump();
  852. break;
  853. }
  854. }
  855. }
  856. void
  857. tsetscroll(int t, int b) {
  858. int temp;
  859. LIMIT(t, 0, term.row-1);
  860. LIMIT(b, 0, term.row-1);
  861. if(t > b) {
  862. temp = t;
  863. t = b;
  864. b = temp;
  865. }
  866. term.top = t;
  867. term.bot = b;
  868. }
  869. void
  870. csihandle(void) {
  871. switch(escseq.mode) {
  872. default:
  873. unknown:
  874. fprintf(stderr, "erresc: unknown csi ");
  875. csidump();
  876. /* die(""); */
  877. break;
  878. case '@': /* ICH -- Insert <n> blank char */
  879. DEFAULT(escseq.arg[0], 1);
  880. tinsertblank(escseq.arg[0]);
  881. break;
  882. case 'A': /* CUU -- Cursor <n> Up */
  883. case 'e':
  884. DEFAULT(escseq.arg[0], 1);
  885. tmoveto(term.c.x, term.c.y-escseq.arg[0]);
  886. break;
  887. case 'B': /* CUD -- Cursor <n> Down */
  888. DEFAULT(escseq.arg[0], 1);
  889. tmoveto(term.c.x, term.c.y+escseq.arg[0]);
  890. break;
  891. case 'C': /* CUF -- Cursor <n> Forward */
  892. case 'a':
  893. DEFAULT(escseq.arg[0], 1);
  894. tmoveto(term.c.x+escseq.arg[0], term.c.y);
  895. break;
  896. case 'D': /* CUB -- Cursor <n> Backward */
  897. DEFAULT(escseq.arg[0], 1);
  898. tmoveto(term.c.x-escseq.arg[0], term.c.y);
  899. break;
  900. case 'E': /* CNL -- Cursor <n> Down and first col */
  901. DEFAULT(escseq.arg[0], 1);
  902. tmoveto(0, term.c.y+escseq.arg[0]);
  903. break;
  904. case 'F': /* CPL -- Cursor <n> Up and first col */
  905. DEFAULT(escseq.arg[0], 1);
  906. tmoveto(0, term.c.y-escseq.arg[0]);
  907. break;
  908. case 'G': /* CHA -- Move to <col> */
  909. case '`': /* XXX: HPA -- same? */
  910. DEFAULT(escseq.arg[0], 1);
  911. tmoveto(escseq.arg[0]-1, term.c.y);
  912. break;
  913. case 'H': /* CUP -- Move to <row> <col> */
  914. case 'f': /* XXX: HVP -- same? */
  915. DEFAULT(escseq.arg[0], 1);
  916. DEFAULT(escseq.arg[1], 1);
  917. tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
  918. break;
  919. /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
  920. case 'J': /* ED -- Clear screen */
  921. switch(escseq.arg[0]) {
  922. case 0: /* below */
  923. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  924. if(term.c.y < term.row-1)
  925. tclearregion(0, term.c.y+1, term.col-1, term.row-1);
  926. break;
  927. case 1: /* above */
  928. if(term.c.y > 1)
  929. tclearregion(0, 0, term.col-1, term.c.y-1);
  930. tclearregion(0, term.c.y, term.c.x, term.c.y);
  931. break;
  932. case 2: /* all */
  933. tclearregion(0, 0, term.col-1, term.row-1);
  934. break;
  935. default:
  936. goto unknown;
  937. }
  938. break;
  939. case 'K': /* EL -- Clear line */
  940. switch(escseq.arg[0]) {
  941. case 0: /* right */
  942. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  943. break;
  944. case 1: /* left */
  945. tclearregion(0, term.c.y, term.c.x, term.c.y);
  946. break;
  947. case 2: /* all */
  948. tclearregion(0, term.c.y, term.col-1, term.c.y);
  949. break;
  950. }
  951. break;
  952. case 'S': /* SU -- Scroll <n> line up */
  953. DEFAULT(escseq.arg[0], 1);
  954. tscrollup(term.top, escseq.arg[0]);
  955. break;
  956. case 'T': /* SD -- Scroll <n> line down */
  957. DEFAULT(escseq.arg[0], 1);
  958. tscrolldown(term.top, escseq.arg[0]);
  959. break;
  960. case 'L': /* IL -- Insert <n> blank lines */
  961. DEFAULT(escseq.arg[0], 1);
  962. tinsertblankline(escseq.arg[0]);
  963. break;
  964. case 'l': /* RM -- Reset Mode */
  965. if(escseq.priv) {
  966. switch(escseq.arg[0]) {
  967. case 1:
  968. term.mode &= ~MODE_APPKEYPAD;
  969. break;
  970. case 5: /* TODO: DECSCNM -- Remove reverse video */
  971. break;
  972. case 7:
  973. term.mode &= ~MODE_WRAP;
  974. break;
  975. case 12: /* att610 -- Stop blinking cursor (IGNORED) */
  976. break;
  977. case 20:
  978. term.mode &= ~MODE_CRLF;
  979. break;
  980. case 25:
  981. term.c.state |= CURSOR_HIDE;
  982. break;
  983. case 1049: /* = 1047 and 1048 */
  984. case 1047:
  985. if(IS_SET(MODE_ALTSCREEN)) {
  986. tclearregion(0, 0, term.col-1, term.row-1);
  987. tswapscreen();
  988. }
  989. if(escseq.arg[0] == 1047)
  990. break;
  991. case 1048:
  992. tcursor(CURSOR_LOAD);
  993. break;
  994. default:
  995. goto unknown;
  996. }
  997. } else {
  998. switch(escseq.arg[0]) {
  999. case 4:
  1000. term.mode &= ~MODE_INSERT;
  1001. break;
  1002. default:
  1003. goto unknown;
  1004. }
  1005. }
  1006. break;
  1007. case 'M': /* DL -- Delete <n> lines */
  1008. DEFAULT(escseq.arg[0], 1);
  1009. tdeleteline(escseq.arg[0]);
  1010. break;
  1011. case 'X': /* ECH -- Erase <n> char */
  1012. DEFAULT(escseq.arg[0], 1);
  1013. tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
  1014. break;
  1015. case 'P': /* DCH -- Delete <n> char */
  1016. DEFAULT(escseq.arg[0], 1);
  1017. tdeletechar(escseq.arg[0]);
  1018. break;
  1019. /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
  1020. case 'd': /* VPA -- Move to <row> */
  1021. DEFAULT(escseq.arg[0], 1);
  1022. tmoveto(term.c.x, escseq.arg[0]-1);
  1023. break;
  1024. case 'h': /* SM -- Set terminal mode */
  1025. if(escseq.priv) {
  1026. switch(escseq.arg[0]) {
  1027. case 1:
  1028. term.mode |= MODE_APPKEYPAD;
  1029. break;
  1030. case 5: /* DECSCNM -- Reverve video */
  1031. /* TODO: set REVERSE on the whole screen (f) */
  1032. break;
  1033. case 7:
  1034. term.mode |= MODE_WRAP;
  1035. break;
  1036. case 20:
  1037. term.mode |= MODE_CRLF;
  1038. break;
  1039. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  1040. /* fallthrough for xterm cvvis = CSI [ ? 12 ; 25 h */
  1041. if(escseq.narg > 1 && escseq.arg[1] != 25)
  1042. break;
  1043. case 25:
  1044. term.c.state &= ~CURSOR_HIDE;
  1045. break;
  1046. case 1049: /* = 1047 and 1048 */
  1047. case 1047:
  1048. if(IS_SET(MODE_ALTSCREEN))
  1049. tclearregion(0, 0, term.col-1, term.row-1);
  1050. else
  1051. tswapscreen();
  1052. if(escseq.arg[0] == 1047)
  1053. break;
  1054. case 1048:
  1055. tcursor(CURSOR_SAVE);
  1056. break;
  1057. default: goto unknown;
  1058. }
  1059. } else {
  1060. switch(escseq.arg[0]) {
  1061. case 4:
  1062. term.mode |= MODE_INSERT;
  1063. break;
  1064. default: goto unknown;
  1065. }
  1066. };
  1067. break;
  1068. case 'm': /* SGR -- Terminal attribute (color) */
  1069. tsetattr(escseq.arg, escseq.narg);
  1070. break;
  1071. case 'r': /* DECSTBM -- Set Scrolling Region */
  1072. if(escseq.priv)
  1073. goto unknown;
  1074. else {
  1075. DEFAULT(escseq.arg[0], 1);
  1076. DEFAULT(escseq.arg[1], term.row);
  1077. tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
  1078. tmoveto(0, 0);
  1079. }
  1080. break;
  1081. case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
  1082. tcursor(CURSOR_SAVE);
  1083. break;
  1084. case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
  1085. tcursor(CURSOR_LOAD);
  1086. break;
  1087. }
  1088. }
  1089. void
  1090. csidump(void) {
  1091. int i;
  1092. printf("ESC [ %s", escseq.priv ? "? " : "");
  1093. if(escseq.narg)
  1094. for(i = 0; i < escseq.narg; i++)
  1095. printf("%d ", escseq.arg[i]);
  1096. if(escseq.mode)
  1097. putchar(escseq.mode);
  1098. putchar('\n');
  1099. }
  1100. void
  1101. csireset(void) {
  1102. memset(&escseq, 0, sizeof(escseq));
  1103. }
  1104. void
  1105. tputtab(void) {
  1106. int space = TAB - term.c.x % TAB;
  1107. tmoveto(term.c.x + space, term.c.y);
  1108. }
  1109. void
  1110. tputc(char *c) {
  1111. char ascii = *c;
  1112. if(term.esc & ESC_START) {
  1113. if(term.esc & ESC_CSI) {
  1114. escseq.buf[escseq.len++] = ascii;
  1115. if(BETWEEN(ascii, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
  1116. term.esc = 0;
  1117. csiparse(), csihandle();
  1118. }
  1119. /* TODO: handle other OSC */
  1120. } else if(term.esc & ESC_OSC) {
  1121. if(ascii == ';') {
  1122. term.titlelen = 0;
  1123. term.esc = ESC_START | ESC_TITLE;
  1124. }
  1125. } else if(term.esc & ESC_TITLE) {
  1126. if(ascii == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
  1127. term.esc = 0;
  1128. term.title[term.titlelen] = '\0';
  1129. XStoreName(xw.dpy, xw.win, term.title);
  1130. } else {
  1131. term.title[term.titlelen++] = ascii;
  1132. }
  1133. } else if(term.esc & ESC_ALTCHARSET) {
  1134. switch(ascii) {
  1135. case '0': /* Line drawing crap */
  1136. term.c.attr.mode |= ATTR_GFX;
  1137. break;
  1138. case 'B': /* Back to regular text */
  1139. term.c.attr.mode &= ~ATTR_GFX;
  1140. break;
  1141. default:
  1142. fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
  1143. }
  1144. term.esc = 0;
  1145. } else {
  1146. switch(ascii) {
  1147. case '[':
  1148. term.esc |= ESC_CSI;
  1149. break;
  1150. case ']':
  1151. term.esc |= ESC_OSC;
  1152. break;
  1153. case '(':
  1154. term.esc |= ESC_ALTCHARSET;
  1155. break;
  1156. case 'D': /* IND -- Linefeed */
  1157. if(term.c.y == term.bot)
  1158. tscrollup(term.top, 1);
  1159. else
  1160. tmoveto(term.c.x, term.c.y+1);
  1161. term.esc = 0;
  1162. break;
  1163. case 'E': /* NEL -- Next line */
  1164. tnewline(1); /* always go to first col */
  1165. term.esc = 0;
  1166. break;
  1167. case 'M': /* RI -- Reverse index */
  1168. if(term.c.y == term.top)
  1169. tscrolldown(term.top, 1);
  1170. else
  1171. tmoveto(term.c.x, term.c.y-1);
  1172. term.esc = 0;
  1173. break;
  1174. case 'c': /* RIS -- Reset to inital state */
  1175. treset();
  1176. term.esc = 0;
  1177. break;
  1178. case '=': /* DECPAM -- Application keypad */
  1179. term.mode |= MODE_APPKEYPAD;
  1180. term.esc = 0;
  1181. break;
  1182. case '>': /* DECPNM -- Normal keypad */
  1183. term.mode &= ~MODE_APPKEYPAD;
  1184. term.esc = 0;
  1185. break;
  1186. case '7': /* DECSC -- Save Cursor */
  1187. tcursor(CURSOR_SAVE);
  1188. term.esc = 0;
  1189. break;
  1190. case '8': /* DECRC -- Restore Cursor */
  1191. tcursor(CURSOR_LOAD);
  1192. term.esc = 0;
  1193. break;
  1194. default:
  1195. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
  1196. (unsigned char) ascii, isprint(ascii)?ascii:'.');
  1197. term.esc = 0;
  1198. }
  1199. }
  1200. } else {
  1201. switch(ascii) {
  1202. case '\t':
  1203. tputtab();
  1204. break;
  1205. case '\b':
  1206. tmoveto(term.c.x-1, term.c.y);
  1207. break;
  1208. case '\r':
  1209. tmoveto(0, term.c.y);
  1210. break;
  1211. case '\f':
  1212. case '\v':
  1213. case '\n':
  1214. /* go to first col if the mode is set */
  1215. tnewline(IS_SET(MODE_CRLF));
  1216. break;
  1217. case '\a':
  1218. if(!(xw.state & WIN_FOCUSED))
  1219. xseturgency(1);
  1220. break;
  1221. case '\033':
  1222. csireset();
  1223. term.esc = ESC_START;
  1224. break;
  1225. default:
  1226. if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
  1227. tnewline(1); /* always go to first col */
  1228. tsetchar(c);
  1229. if(term.c.x+1 < term.col)
  1230. tmoveto(term.c.x+1, term.c.y);
  1231. else
  1232. term.c.state |= CURSOR_WRAPNEXT;
  1233. break;
  1234. }
  1235. }
  1236. }
  1237. int
  1238. tresize(int col, int row) {
  1239. int i, x;
  1240. int minrow = MIN(row, term.row);
  1241. int mincol = MIN(col, term.col);
  1242. int slide = term.c.y - row + 1;
  1243. if(col < 1 || row < 1)
  1244. return 0;
  1245. /* free unneeded rows */
  1246. i = 0;
  1247. if(slide > 0) {
  1248. /* slide screen to keep cursor where we expect it -
  1249. * tscrollup would work here, but we can optimize to
  1250. * memmove because we're freeing the earlier lines */
  1251. for(/* i = 0 */; i < slide; i++) {
  1252. free(term.line[i]);
  1253. free(term.alt[i]);
  1254. }
  1255. memmove(term.line, term.line + slide, row * sizeof(Line));
  1256. memmove(term.alt, term.alt + slide, row * sizeof(Line));
  1257. }
  1258. for(i += row; i < term.row; i++) {
  1259. free(term.line[i]);
  1260. free(term.alt[i]);
  1261. }
  1262. /* resize to new height */
  1263. term.line = realloc(term.line, row * sizeof(Line));
  1264. term.alt = realloc(term.alt, row * sizeof(Line));
  1265. /* resize each row to new width, zero-pad if needed */
  1266. for(i = 0; i < minrow; i++) {
  1267. term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
  1268. term.alt[i] = realloc(term.alt[i], col * sizeof(Glyph));
  1269. for(x = mincol; x < col; x++) {
  1270. term.line[i][x].state = 0;
  1271. term.alt[i][x].state = 0;
  1272. }
  1273. }
  1274. /* allocate any new rows */
  1275. for(/* i == minrow */; i < row; i++) {
  1276. term.line[i] = calloc(col, sizeof(Glyph));
  1277. term.alt [i] = calloc(col, sizeof(Glyph));
  1278. }
  1279. /* update terminal size */
  1280. term.col = col, term.row = row;
  1281. /* make use of the LIMIT in tmoveto */
  1282. tmoveto(term.c.x, term.c.y);
  1283. /* reset scrolling region */
  1284. tsetscroll(0, row-1);
  1285. return (slide > 0);
  1286. }
  1287. void
  1288. xresize(int col, int row) {
  1289. Pixmap newbuf;
  1290. int oldw, oldh;
  1291. oldw = xw.bufw;
  1292. oldh = xw.bufh;
  1293. xw.bufw = MAX(1, col * xw.cw);
  1294. xw.bufh = MAX(1, row * xw.ch);
  1295. newbuf = XCreatePixmap(xw.dpy, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dpy, xw.scr));
  1296. XCopyArea(xw.dpy, xw.buf, newbuf, dc.gc, 0, 0, xw.bufw, xw.bufh, 0, 0);
  1297. XFreePixmap(xw.dpy, xw.buf);
  1298. XSetForeground(xw.dpy, dc.gc, dc.col[DefaultBG]);
  1299. if(xw.bufw > oldw)
  1300. XFillRectangle(xw.dpy, newbuf, dc.gc, oldw, 0,
  1301. xw.bufw-oldw, MIN(xw.bufh, oldh));
  1302. else if(xw.bufw < oldw && (BORDER > 0 || xw.w > xw.bufw))
  1303. XClearArea(xw.dpy, xw.win, BORDER+xw.bufw, BORDER,
  1304. xw.w-xw.bufh-BORDER, BORDER+MIN(xw.bufh, oldh),
  1305. False);
  1306. if(xw.bufh > oldh)
  1307. XFillRectangle(xw.dpy, newbuf, dc.gc, 0, oldh,
  1308. xw.bufw, xw.bufh-oldh);
  1309. else if(xw.bufh < oldh && (BORDER > 0 || xw.h > xw.bufh))
  1310. XClearArea(xw.dpy, xw.win, BORDER, BORDER+xw.bufh,
  1311. xw.w-2*BORDER, xw.h-xw.bufh-BORDER,
  1312. False);
  1313. xw.buf = newbuf;
  1314. }
  1315. void
  1316. xloadcols(void) {
  1317. int i, r, g, b;
  1318. XColor color;
  1319. unsigned long white = WhitePixel(xw.dpy, xw.scr);
  1320. for(i = 0; i < 16; i++) {
  1321. if (!XAllocNamedColor(xw.dpy, xw.cmap, colorname[i], &color, &color)) {
  1322. dc.col[i] = white;
  1323. fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
  1324. } else
  1325. dc.col[i] = color.pixel;
  1326. }
  1327. /* same colors as xterm */
  1328. for(r = 0; r < 6; r++)
  1329. for(g = 0; g < 6; g++)
  1330. for(b = 0; b < 6; b++) {
  1331. color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
  1332. color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
  1333. color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
  1334. if (!XAllocColor(xw.dpy, xw.cmap, &color)) {
  1335. dc.col[i] = white;
  1336. fprintf(stderr, "Could not allocate color %d\n", i);
  1337. } else
  1338. dc.col[i] = color.pixel;
  1339. i++;
  1340. }
  1341. for(r = 0; r < 24; r++, i++) {
  1342. color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
  1343. if (!XAllocColor(xw.dpy, xw.cmap, &color)) {
  1344. dc.col[i] = white;
  1345. fprintf(stderr, "Could not allocate color %d\n", i);
  1346. } else
  1347. dc.col[i] = color.pixel;
  1348. }
  1349. }
  1350. void
  1351. xclear(int x1, int y1, int x2, int y2) {
  1352. XSetForeground(xw.dpy, dc.gc, dc.col[DefaultBG]);
  1353. XFillRectangle(xw.dpy, xw.buf, dc.gc,
  1354. x1 * xw.cw, y1 * xw.ch,
  1355. (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
  1356. }
  1357. void
  1358. xhints(void)
  1359. {
  1360. XClassHint class = {opt_class ? opt_class : TNAME, TNAME};
  1361. XWMHints wm = {.flags = InputHint, .input = 1};
  1362. XSizeHints size = {
  1363. .flags = PSize | PResizeInc | PBaseSize,
  1364. .height = xw.h,
  1365. .width = xw.w,
  1366. .height_inc = xw.ch,
  1367. .width_inc = xw.cw,
  1368. .base_height = 2*BORDER,
  1369. .base_width = 2*BORDER,
  1370. };
  1371. XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
  1372. }
  1373. XFontSet
  1374. xinitfont(char *fontstr)
  1375. {
  1376. XFontSet set;
  1377. char *def, **missing;
  1378. int n;
  1379. missing = NULL;
  1380. set = XCreateFontSet(xw.dpy, fontstr, &missing, &n, &def);
  1381. if(missing) {
  1382. while(n--)
  1383. fprintf(stderr, "st: missing fontset: %s\n", missing[n]);
  1384. XFreeStringList(missing);
  1385. }
  1386. return set;
  1387. }
  1388. void
  1389. xgetfontinfo(XFontSet set, int *ascent, int *descent, short *lbearing, short *rbearing)
  1390. {
  1391. XFontStruct **xfonts;
  1392. char **font_names;
  1393. int i, n;
  1394. *ascent = *descent = *lbearing = *rbearing = 0;
  1395. n = XFontsOfFontSet(set, &xfonts, &font_names);
  1396. for(i = 0; i < n; i++) {
  1397. *ascent = MAX(*ascent, (*xfonts)->ascent);
  1398. *descent = MAX(*descent, (*xfonts)->descent);
  1399. *lbearing = MAX(*lbearing, (*xfonts)->min_bounds.lbearing);
  1400. *rbearing = MAX(*rbearing, (*xfonts)->max_bounds.rbearing);
  1401. xfonts++;
  1402. }
  1403. }
  1404. void
  1405. initfonts(char *fontstr, char *bfontstr)
  1406. {
  1407. if((dc.font.set = xinitfont(fontstr)) == NULL ||
  1408. (dc.bfont.set = xinitfont(bfontstr)) == NULL)
  1409. die("Can't load font %s\n", dc.font.set ? BOLDFONT : FONT);
  1410. xgetfontinfo(dc.font.set, &dc.font.ascent, &dc.font.descent,
  1411. &dc.font.lbearing, &dc.font.rbearing);
  1412. xgetfontinfo(dc.bfont.set, &dc.bfont.ascent, &dc.bfont.descent,
  1413. &dc.bfont.lbearing, &dc.bfont.rbearing);
  1414. }
  1415. void
  1416. xinit(void) {
  1417. XSetWindowAttributes attrs;
  1418. Cursor cursor;
  1419. if(!(xw.dpy = XOpenDisplay(NULL)))
  1420. die("Can't open display\n");
  1421. xw.scr = XDefaultScreen(xw.dpy);
  1422. /* font */
  1423. initfonts(FONT, BOLDFONT);
  1424. /* XXX: Assuming same size for bold font */
  1425. xw.cw = dc.font.rbearing - dc.font.lbearing;
  1426. xw.ch = dc.font.ascent + dc.font.descent;
  1427. /* colors */
  1428. xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
  1429. xloadcols();
  1430. /* window - default size */
  1431. xw.bufh = 24 * xw.ch;
  1432. xw.bufw = 80 * xw.cw;
  1433. xw.h = xw.bufh + 2*BORDER;
  1434. xw.w = xw.bufw + 2*BORDER;
  1435. attrs.background_pixel = dc.col[DefaultBG];
  1436. attrs.border_pixel = dc.col[DefaultBG];
  1437. attrs.bit_gravity = NorthWestGravity;
  1438. attrs.event_mask = FocusChangeMask | KeyPressMask
  1439. | ExposureMask | VisibilityChangeMask | StructureNotifyMask
  1440. | PointerMotionMask | ButtonPressMask | ButtonReleaseMask;
  1441. attrs.colormap = xw.cmap;
  1442. xw.win = XCreateWindow(xw.dpy, XRootWindow(xw.dpy, xw.scr), 0, 0,
  1443. xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
  1444. XDefaultVisual(xw.dpy, xw.scr),
  1445. CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
  1446. | CWColormap,
  1447. &attrs);
  1448. xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dpy, xw.scr));
  1449. /* input methods */
  1450. xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL);
  1451. xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
  1452. | XIMStatusNothing, XNClientWindow, xw.win,
  1453. XNFocusWindow, xw.win, NULL);
  1454. /* gc */
  1455. dc.gc = XCreateGC(xw.dpy, xw.win, 0, NULL);
  1456. /* white cursor, black outline */
  1457. cursor = XCreateFontCursor(xw.dpy, XC_xterm);
  1458. XDefineCursor(xw.dpy, xw.win, cursor);
  1459. XRecolorCursor(xw.dpy, cursor,
  1460. &(XColor){.red = 0xffff, .green = 0xffff, .blue = 0xffff},
  1461. &(XColor){.red = 0x0000, .green = 0x0000, .blue = 0x0000});
  1462. XMapWindow(xw.dpy, xw.win);
  1463. xhints();
  1464. XStoreName(xw.dpy, xw.win, opt_title ? opt_title : "st");
  1465. XSync(xw.dpy, 0);
  1466. }
  1467. void
  1468. xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
  1469. unsigned long xfg, xbg;
  1470. int winx = x*xw.cw, winy = y*xw.ch + dc.font.ascent, width = charlen*xw.cw;
  1471. int i;
  1472. if(base.mode & ATTR_REVERSE)
  1473. xfg = dc.col[base.bg], xbg = dc.col[base.fg];
  1474. else
  1475. xfg = dc.col[base.fg], xbg = dc.col[base.bg];
  1476. XSetBackground(xw.dpy, dc.gc, xbg);
  1477. XSetForeground(xw.dpy, dc.gc, xfg);
  1478. if(base.mode & ATTR_GFX) {
  1479. for(i = 0; i < bytelen; i++) {
  1480. char c = gfx[(unsigned int)s[i] % 256];
  1481. if(c)
  1482. s[i] = c;
  1483. else if(s[i] > 0x5f)
  1484. s[i] -= 0x5f;
  1485. }
  1486. }
  1487. XmbDrawImageString(xw.dpy, xw.buf, base.mode & ATTR_BOLD ? dc.bfont.set : dc.font.set,
  1488. dc.gc, winx, winy, s, bytelen);
  1489. if(base.mode & ATTR_UNDERLINE)
  1490. XDrawLine(xw.dpy, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
  1491. }
  1492. void
  1493. xdrawcursor(void) {
  1494. static int oldx = 0;
  1495. static int oldy = 0;
  1496. int sl;
  1497. Glyph g = {{' '}, ATTR_NULL, DefaultBG, DefaultCS, 0};
  1498. LIMIT(oldx, 0, term.col-1);
  1499. LIMIT(oldy, 0, term.row-1);
  1500. if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
  1501. memcpy(g.c, term.line[term.c.y][term.c.x].c, UTF_SIZ);
  1502. /* remove the old cursor */
  1503. if(term.line[oldy][oldx].state & GLYPH_SET) {
  1504. sl = utf8size(term.line[oldy][oldx].c);
  1505. xdraws(term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1, sl);
  1506. } else
  1507. xclear(oldx, oldy, oldx, oldy);
  1508. /* draw the new one */
  1509. if(!(term.c.state & CURSOR_HIDE) && (xw.state & WIN_FOCUSED)) {
  1510. sl = utf8size(g.c);
  1511. xdraws(g.c, g, term.c.x, term.c.y, 1, sl);
  1512. oldx = term.c.x, oldy = term.c.y;
  1513. }
  1514. }
  1515. #ifdef DEBUG
  1516. /* basic drawing routines */
  1517. void
  1518. xdrawc(int x, int y, Glyph g) {
  1519. int sl = utf8size(g.c);
  1520. XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
  1521. XSetBackground(xw.dpy, dc.gc, dc.col[g.bg]);
  1522. XSetForeground(xw.dpy, dc.gc, dc.col[g.fg]);
  1523. XmbDrawImageString(xw.dpy, xw.buf, g.mode&ATTR_BOLD?dc.bfont.fs:dc.font.fs,
  1524. dc.gc, r.x, r.y+dc.font.ascent, g.c, sl);
  1525. }
  1526. void
  1527. drawregion(int x0, int x1, int y0, int y1) {
  1528. draw();
  1529. }
  1530. void
  1531. draw() {
  1532. int x, y;
  1533. xclear(0, 0, term.col-1, term.row-1);
  1534. for(y = 0; y < term.row; y++)
  1535. for(x = 0; x < term.col; x++)
  1536. if(term.line[y][x].state & GLYPH_SET)
  1537. xdrawc(x, y, term.line[y][x]);
  1538. xdrawcursor();
  1539. XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
  1540. XFlush(xw.dpy);
  1541. }
  1542. #else
  1543. /* optimized drawing routine */
  1544. void
  1545. draw() {
  1546. drawregion(0, 0, term.col, term.row);
  1547. }
  1548. void
  1549. drawregion(int x1, int y1, int x2, int y2) {
  1550. int ic, ib, x, y, ox, sl;
  1551. Glyph base, new;
  1552. char buf[DRAW_BUF_SIZ];
  1553. if(!(xw.state & WIN_VISIBLE))
  1554. return;
  1555. xclear(x1, y1, x2-1, y2-1);
  1556. for(y = y1; y < y2; y++) {
  1557. base = term.line[y][0];
  1558. ic = ib = ox = 0;
  1559. for(x = x1; x < x2; x++) {
  1560. new = term.line[y][x];
  1561. if(sel.bx != -1 && *(new.c) && selected(x, y))
  1562. new.mode ^= ATTR_REVERSE;
  1563. if(ib > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
  1564. ib >= DRAW_BUF_SIZ-UTF_SIZ)) {
  1565. xdraws(buf, base, ox, y, ic, ib);
  1566. ic = ib = 0;
  1567. }
  1568. if(new.state & GLYPH_SET) {
  1569. if(ib == 0) {
  1570. ox = x;
  1571. base = new;
  1572. }
  1573. sl = utf8size(new.c);
  1574. memcpy(buf+ib, new.c, sl);
  1575. ib += sl;
  1576. ++ic;
  1577. }
  1578. }
  1579. if(ib > 0)
  1580. xdraws(buf, base, ox, y, ic, ib);
  1581. }
  1582. xdrawcursor();
  1583. XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
  1584. }
  1585. #endif
  1586. void
  1587. expose(XEvent *ev) {
  1588. XExposeEvent *e = &ev->xexpose;
  1589. if(xw.state & WIN_REDRAW) {
  1590. if(!e->count) {
  1591. xw.state &= ~WIN_REDRAW;
  1592. draw();
  1593. }
  1594. } else
  1595. XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, e->x-BORDER, e->y-BORDER,
  1596. e->width, e->height, e->x, e->y);
  1597. }
  1598. void
  1599. visibility(XEvent *ev) {
  1600. XVisibilityEvent *e = &ev->xvisibility;
  1601. if(e->state == VisibilityFullyObscured)
  1602. xw.state &= ~WIN_VISIBLE;
  1603. else if(!(xw.state & WIN_VISIBLE))
  1604. /* need a full redraw for next Expose, not just a buf copy */
  1605. xw.state |= WIN_VISIBLE | WIN_REDRAW;
  1606. }
  1607. void
  1608. unmap(XEvent *ev) {
  1609. xw.state &= ~WIN_VISIBLE;
  1610. }
  1611. void
  1612. xseturgency(int add) {
  1613. XWMHints *h = XGetWMHints(xw.dpy, xw.win);
  1614. h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
  1615. XSetWMHints(xw.dpy, xw.win, h);
  1616. XFree(h);
  1617. }
  1618. void
  1619. focus(XEvent *ev) {
  1620. if(ev->type == FocusIn) {
  1621. xw.state |= WIN_FOCUSED;
  1622. xseturgency(0);
  1623. } else
  1624. xw.state &= ~WIN_FOCUSED;
  1625. draw();
  1626. }
  1627. char*
  1628. kmap(KeySym k, unsigned int state) {
  1629. int i;
  1630. for(i = 0; i < LEN(key); i++)
  1631. if(key[i].k == k && (key[i].mask == 0 || key[i].mask & state))
  1632. return (char*)key[i].s;
  1633. return NULL;
  1634. }
  1635. void
  1636. kpress(XEvent *ev) {
  1637. XKeyEvent *e = &ev->xkey;
  1638. KeySym ksym;
  1639. char buf[32];
  1640. char *customkey;
  1641. int len;
  1642. int meta;
  1643. int shift;
  1644. Status status;
  1645. meta = e->state & Mod1Mask;
  1646. shift = e->state & ShiftMask;
  1647. len = XmbLookupString(xw.xic, e, buf, sizeof(buf), &ksym, &status);
  1648. /* 1. custom keys from config.h */
  1649. if((customkey = kmap(ksym, e->state)))
  1650. ttywrite(customkey, strlen(customkey));
  1651. /* 2. hardcoded (overrides X lookup) */
  1652. else
  1653. switch(ksym) {
  1654. case XK_Up:
  1655. case XK_Down:
  1656. case XK_Left:
  1657. case XK_Right:
  1658. sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', (shift ? "dacb":"DACB")[ksym - XK_Left]);
  1659. ttywrite(buf, 3);
  1660. break;
  1661. case XK_Insert:
  1662. if(shift)
  1663. selpaste();
  1664. break;
  1665. case XK_Return:
  1666. if(IS_SET(MODE_CRLF))
  1667. ttywrite("\r\n", 2);
  1668. else
  1669. ttywrite("\r", 1);
  1670. break;
  1671. /* 3. X lookup */
  1672. default:
  1673. if(len > 0) {
  1674. if(meta && len == 1)
  1675. ttywrite("\033", 1);
  1676. ttywrite(buf, len);
  1677. }
  1678. break;
  1679. }
  1680. }
  1681. void
  1682. resize(XEvent *e) {
  1683. int col, row;
  1684. if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
  1685. return;
  1686. xw.w = e->xconfigure.width;
  1687. xw.h = e->xconfigure.height;
  1688. col = (xw.w - 2*BORDER) / xw.cw;
  1689. row = (xw.h - 2*BORDER) / xw.ch;
  1690. if(col == term.col && row == term.row)
  1691. return;
  1692. if(tresize(col, row))
  1693. draw();
  1694. ttyresize(col, row);
  1695. xresize(col, row);
  1696. }
  1697. void
  1698. run(void) {
  1699. XEvent ev;
  1700. fd_set rfd;
  1701. int xfd = XConnectionNumber(xw.dpy);
  1702. for(;;) {
  1703. FD_ZERO(&rfd);
  1704. FD_SET(cmdfd, &rfd);
  1705. FD_SET(xfd, &rfd);
  1706. if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) < 0) {
  1707. if(errno == EINTR)
  1708. continue;
  1709. die("select failed: %s\n", SERRNO);
  1710. }
  1711. if(FD_ISSET(cmdfd, &rfd)) {
  1712. ttyread();
  1713. draw();
  1714. }
  1715. while(XPending(xw.dpy)) {
  1716. XNextEvent(xw.dpy, &ev);
  1717. if (XFilterEvent(&ev, xw.win))
  1718. continue;
  1719. if(handler[ev.type])
  1720. (handler[ev.type])(&ev);
  1721. }
  1722. }
  1723. }
  1724. int
  1725. main(int argc, char *argv[]) {
  1726. int i;
  1727. for(i = 1; i < argc; i++) {
  1728. switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) {
  1729. case 't':
  1730. if(++i < argc) opt_title = argv[i];
  1731. break;
  1732. case 'c':
  1733. if(++i < argc) opt_class = argv[i];
  1734. break;
  1735. case 'e':
  1736. if(++i < argc) opt_cmd = &argv[i];
  1737. break;
  1738. case 'v':
  1739. default:
  1740. die(USAGE);
  1741. }
  1742. /* -e eats every remaining arguments */
  1743. if(opt_cmd)
  1744. break;
  1745. }
  1746. setlocale(LC_CTYPE, "");
  1747. tnew(80, 24);
  1748. ttynew();
  1749. xinit();
  1750. selinit();
  1751. run();
  1752. return 0;
  1753. }