st.c 41 KB

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