st.c 42 KB

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