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.

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