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.

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