st.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  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/keysym.h>
  21. #include <X11/Xutil.h>
  22. #define TNAME "xterm"
  23. /* Arbitrary sizes */
  24. #define ESC_TITLE_SIZ 256
  25. #define ESC_BUF_SIZ 256
  26. #define ESC_ARG_SIZ 16
  27. #define DRAW_BUF_SIZ 1024
  28. #define SERRNO strerror(errno)
  29. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  30. #define MAX(a, b) ((a) < (b) ? (b) : (a))
  31. #define LEN(a) (sizeof(a) / sizeof(a[0]))
  32. #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
  33. #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
  34. #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
  35. #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
  36. /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
  37. enum { ATTR_NULL=0 , ATTR_REVERSE=1 , ATTR_UNDERLINE=2, ATTR_BOLD=4, ATTR_GFX=8 };
  38. enum { CURSOR_UP, CURSOR_DOWN, CURSOR_LEFT, CURSOR_RIGHT, CURSOR_HIDE, CURSOR_DRAW, CURSOR_SAVE, CURSOR_LOAD };
  39. enum { GLYPH_SET=1, GLYPH_DIRTY=2 };
  40. enum { MODE_WRAP=1, MODE_INSERT=2, MODE_APPKEYPAD=4 };
  41. enum { ESC_START=1, ESC_CSI=2, ESC_OSC=4, ESC_TITLE=8, ESC_ALTCHARSET=16 };
  42. enum { SCREEN_UPDATE, SCREEN_REDRAW };
  43. typedef struct {
  44. char c; /* character code */
  45. char mode; /* attribute flags */
  46. int fg; /* foreground */
  47. int bg; /* background */
  48. char state; /* state flags */
  49. } Glyph;
  50. typedef Glyph* Line;
  51. typedef struct {
  52. Glyph attr; /* current char attributes */
  53. int x;
  54. int y;
  55. } TCursor;
  56. /* CSI Escape sequence structs */
  57. /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
  58. typedef struct {
  59. char buf[ESC_BUF_SIZ]; /* raw string */
  60. int len; /* raw string length */
  61. char priv;
  62. int arg[ESC_ARG_SIZ];
  63. int narg; /* nb of args */
  64. char mode;
  65. } CSIEscape;
  66. /* Internal representation of the screen */
  67. typedef struct {
  68. int row; /* nb row */
  69. int col; /* nb col */
  70. Line* line; /* screen */
  71. TCursor c; /* cursor */
  72. char hidec;
  73. int top; /* top scroll limit */
  74. int bot; /* bottom scroll limit */
  75. int mode; /* terminal mode flags */
  76. int esc; /* escape state flags */
  77. char title[ESC_TITLE_SIZ];
  78. int titlelen;
  79. } Term;
  80. /* Purely graphic info */
  81. typedef struct {
  82. Display* dis;
  83. Window win;
  84. Pixmap buf;
  85. int scr;
  86. int w; /* window width */
  87. int h; /* window height */
  88. int bufw; /* pixmap width */
  89. int bufh; /* pixmap height */
  90. int ch; /* char height */
  91. int cw; /* char width */
  92. } XWindow;
  93. typedef struct {
  94. KeySym k;
  95. char s[ESC_BUF_SIZ];
  96. } Key;
  97. #include "config.h"
  98. /* Drawing Context */
  99. typedef struct {
  100. unsigned long col[LEN(colorname)];
  101. XFontStruct* font;
  102. XFontStruct* bfont;
  103. GC gc;
  104. } DC;
  105. static void die(const char *errstr, ...);
  106. static void draw(int);
  107. static void execsh(void);
  108. static void sigchld(int);
  109. static void run(void);
  110. static void csidump(void);
  111. static void csihandle(void);
  112. static void csiparse(void);
  113. static void csireset(void);
  114. static void tclearregion(int, int, int, int);
  115. static void tcursor(int);
  116. static void tmovecursor(int);
  117. static void tdeletechar(int);
  118. static void tdeleteline(int);
  119. static void tinsertblank(int);
  120. static void tinsertblankline(int);
  121. static void tmoveto(int, int);
  122. static void tnew(int, int);
  123. static void tnewline(void);
  124. static void tputc(char);
  125. static void tputs(char*, int);
  126. static void treset(void);
  127. static void tresize(int, int);
  128. static void tscroll(void);
  129. static void tscrollup(int);
  130. static void tscrolldown(int);
  131. static void tsetattr(int*, int);
  132. static void tsetchar(char);
  133. static void tsetscroll(int, int);
  134. static void ttynew(void);
  135. static void ttyread(void);
  136. static void ttyresize(int, int);
  137. static void ttywrite(const char *, size_t);
  138. static unsigned long xgetcol(const char *);
  139. static void xclear(int, int, int, int);
  140. static void xcursor(int);
  141. static void xinit(void);
  142. static void expose(XEvent *);
  143. static char* kmap(KeySym);
  144. static void kpress(XEvent *);
  145. static void resize(XEvent *);
  146. static void (*handler[LASTEvent])(XEvent *) = {
  147. [KeyPress] = kpress,
  148. [Expose] = expose,
  149. [ConfigureNotify] = resize
  150. };
  151. /* Globals */
  152. static DC dc;
  153. static XWindow xw;
  154. static Term term;
  155. static CSIEscape escseq;
  156. static int cmdfd;
  157. static pid_t pid;
  158. static int running;
  159. #ifdef DEBUG
  160. void
  161. tdump(void) {
  162. int row, col;
  163. Glyph c;
  164. for(row = 0; row < term.row; row++) {
  165. for(col = 0; col < term.col; col++) {
  166. if(col == term.c.x && row == term.c.y)
  167. putchar('#');
  168. else {
  169. c = term.line[row][col];
  170. putchar(c.state & GLYPH_SET ? c.c : '.');
  171. }
  172. }
  173. putchar('\n');
  174. }
  175. }
  176. #endif
  177. void
  178. die(const char *errstr, ...) {
  179. va_list ap;
  180. va_start(ap, errstr);
  181. vfprintf(stderr, errstr, ap);
  182. va_end(ap);
  183. exit(EXIT_FAILURE);
  184. }
  185. void
  186. execsh(void) {
  187. char *args[3] = {getenv("SHELL"), "-i", NULL};
  188. DEFAULT(args[0], "/bin/sh"); /* default shell if getenv() failed */
  189. putenv("TERM=" TNAME);
  190. execvp(args[0], args);
  191. }
  192. void
  193. xbell(void) { /* visual bell */
  194. XRectangle r = { BORDER, BORDER, xw.bufw, xw.bufh };
  195. XSetForeground(xw.dis, dc.gc, dc.col[BellCol]);
  196. XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
  197. /* usleep(30000); */
  198. draw(SCREEN_REDRAW);
  199. }
  200. void
  201. sigchld(int a) {
  202. int stat = 0;
  203. if(waitpid(pid, &stat, 0) < 0)
  204. die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
  205. if(WIFEXITED(stat))
  206. exit(WEXITSTATUS(stat));
  207. else
  208. exit(EXIT_FAILURE);
  209. }
  210. void
  211. ttynew(void) {
  212. int m, s;
  213. char *pts;
  214. if((m = posix_openpt(O_RDWR | O_NOCTTY)) < 0)
  215. die("openpt failed: %s\n", SERRNO);
  216. if(grantpt(m) < 0)
  217. die("grandpt failed: %s\n", SERRNO);
  218. if(unlockpt(m) < 0)
  219. die("unlockpt failed: %s\n", SERRNO);
  220. if(!(pts = ptsname(m)))
  221. die("ptsname failed: %s\n", SERRNO);
  222. if((s = open(pts, O_RDWR | O_NOCTTY)) < 0)
  223. die("Couldn't open slave: %s\n", SERRNO);
  224. fcntl(s, F_SETFL, O_NDELAY);
  225. switch(pid = fork()) {
  226. case -1:
  227. die("fork failed\n");
  228. break;
  229. case 0:
  230. setsid(); /* create a new process group */
  231. dup2(s, STDIN_FILENO);
  232. dup2(s, STDOUT_FILENO);
  233. dup2(s, STDERR_FILENO);
  234. if(ioctl(s, TIOCSCTTY, NULL) < 0)
  235. die("ioctl TTIOCSTTY failed: %s\n", SERRNO);
  236. execsh();
  237. break;
  238. default:
  239. close(s);
  240. cmdfd = m;
  241. signal(SIGCHLD, sigchld);
  242. }
  243. }
  244. void
  245. dump(char c) {
  246. static int col;
  247. fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
  248. if(++col % 10 == 0)
  249. fprintf(stderr, "\n");
  250. }
  251. void
  252. ttyread(void) {
  253. char buf[BUFSIZ] = {0};
  254. int ret;
  255. if((ret = read(cmdfd, buf, BUFSIZ)) < 0)
  256. die("Couldn't read from shell: %s\n", SERRNO);
  257. else
  258. tputs(buf, ret);
  259. }
  260. void
  261. ttywrite(const char *s, size_t n) {
  262. if(write(cmdfd, s, n) == -1)
  263. die("write error on tty: %s\n", SERRNO);
  264. }
  265. void
  266. ttyresize(int x, int y) {
  267. struct winsize w;
  268. w.ws_row = term.row;
  269. w.ws_col = term.col;
  270. w.ws_xpixel = w.ws_ypixel = 0;
  271. if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
  272. fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
  273. }
  274. void
  275. tcursor(int mode) {
  276. static TCursor c;
  277. if(mode == CURSOR_SAVE)
  278. c = term.c;
  279. else if(mode == CURSOR_LOAD)
  280. term.c = c, tmoveto(c.x, c.y);
  281. }
  282. void
  283. treset(void) {
  284. term.c.attr.mode = ATTR_NULL;
  285. term.c.attr.fg = DefaultFG;
  286. term.c.attr.bg = DefaultBG;
  287. term.c.x = term.c.y = 0;
  288. term.hidec = 0;
  289. term.top = 0, term.bot = term.row - 1;
  290. term.mode = MODE_WRAP;
  291. tclearregion(0, 0, term.col-1, term.row-1);
  292. }
  293. void
  294. tnew(int col, int row) { /* screen size */
  295. term.row = row, term.col = col;
  296. term.top = 0, term.bot = term.row - 1;
  297. /* mode */
  298. term.mode = MODE_WRAP;
  299. /* cursor */
  300. term.c.attr.mode = ATTR_NULL;
  301. term.c.attr.fg = DefaultFG;
  302. term.c.attr.bg = DefaultBG;
  303. term.c.x = term.c.y = 0;
  304. term.hidec = 0;
  305. /* allocate screen */
  306. term.line = calloc(term.row, sizeof(Line));
  307. for(row = 0 ; row < term.row; row++)
  308. term.line[row] = calloc(term.col, sizeof(Glyph));
  309. }
  310. /* TODO: Replace with scrollup/scolldown */
  311. void
  312. tscroll(void) {
  313. Line temp = term.line[term.top];
  314. int i;
  315. for(i = term.top; i < term.bot; i++)
  316. term.line[i] = term.line[i+1];
  317. memset(temp, 0, sizeof(Glyph) * term.col);
  318. term.line[term.bot] = temp;
  319. }
  320. void
  321. tscrolldown (int n) {
  322. int i;
  323. Line temp;
  324. LIMIT(n, 0, term.bot-term.top+1);
  325. for(i = 0; i < n; i++)
  326. memset(term.line[term.bot-i], 0, term.col*sizeof(Glyph));
  327. for(i = term.bot; i >= term.top+n; i--) {
  328. temp = term.line[i];
  329. term.line[i] = term.line[i-n];
  330. term.line[i-n] = temp;
  331. }
  332. }
  333. void
  334. tscrollup (int n) {
  335. int i;
  336. Line temp;
  337. LIMIT(n, 0, term.bot-term.top+1);
  338. for(i = 0; i < n; i++)
  339. memset(term.line[term.top+i], 0, term.col*sizeof(Glyph));
  340. for(i = term.top; i <= term.bot-n; i++) {
  341. temp = term.line[i];
  342. term.line[i] = term.line[i+n];
  343. term.line[i+n] = temp;
  344. }
  345. }
  346. void
  347. tnewline(void) {
  348. int y = term.c.y + 1;
  349. if(y > term.bot)
  350. tscroll(), y = term.bot;
  351. tmoveto(0, y);
  352. }
  353. void
  354. csiparse(void) {
  355. /* int noarg = 1; */
  356. char *p = escseq.buf;
  357. escseq.narg = 0;
  358. if(*p == '?')
  359. escseq.priv = 1, p++;
  360. while(p < escseq.buf+escseq.len) {
  361. while(isdigit(*p)) {
  362. escseq.arg[escseq.narg] *= 10;
  363. escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
  364. }
  365. if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
  366. escseq.narg++, p++;
  367. else {
  368. escseq.mode = *p;
  369. escseq.narg++;
  370. return;
  371. }
  372. }
  373. }
  374. void
  375. tmoveto(int x, int y) {
  376. term.c.x = x < 0 ? 0 : x >= term.col ? term.col-1 : x;
  377. term.c.y = y < 0 ? 0 : y >= term.row ? term.row-1 : y;
  378. }
  379. void
  380. tmovecursor(int dir) {
  381. int xf = term.c.x, yf = term.c.y;
  382. switch(dir) {
  383. case CURSOR_UP:
  384. yf--;
  385. break;
  386. case CURSOR_DOWN:
  387. yf++;
  388. break;
  389. case CURSOR_LEFT:
  390. xf--;
  391. if(term.mode & MODE_WRAP && xf < 0) {
  392. xf = term.col-1, yf--;
  393. if(yf < term.top)
  394. yf = term.top, xf = 0;
  395. }
  396. break;
  397. case CURSOR_RIGHT:
  398. xf++;
  399. if(term.mode & MODE_WRAP && xf >= term.col) {
  400. xf = 0, yf++;
  401. if(yf > term.bot)
  402. yf = term.bot, tscroll();
  403. }
  404. break;
  405. }
  406. tmoveto(xf, yf);
  407. }
  408. void
  409. tsetchar(char c) {
  410. term.line[term.c.y][term.c.x] = term.c.attr;
  411. term.line[term.c.y][term.c.x].c = c;
  412. term.line[term.c.y][term.c.x].state |= GLYPH_SET;
  413. }
  414. void
  415. tclearregion(int x1, int y1, int x2, int y2) {
  416. int y, temp;
  417. if(x1 > x2)
  418. temp = x1, x1 = x2, x2 = temp;
  419. if(y1 > y2)
  420. temp = y1, y1 = y2, y2 = temp;
  421. LIMIT(x1, 0, term.col-1);
  422. LIMIT(x2, 0, term.col-1);
  423. LIMIT(y1, 0, term.row-1);
  424. LIMIT(y2, 0, term.row-1);
  425. for(y = y1; y <= y2; y++)
  426. memset(&term.line[y][x1], 0, sizeof(Glyph)*(x2-x1+1));
  427. }
  428. void
  429. tdeletechar(int n) {
  430. int src = term.c.x + n;
  431. int dst = term.c.x;
  432. int size = term.col - src;
  433. if(src >= term.col) {
  434. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  435. return;
  436. }
  437. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  438. tclearregion(term.col-size, term.c.y, term.col-1, term.c.y);
  439. }
  440. void
  441. tinsertblank(int n) {
  442. int src = term.c.x;
  443. int dst = src + n;
  444. int size = term.col - n - src;
  445. if(dst >= term.col) {
  446. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  447. return;
  448. }
  449. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  450. tclearregion(src, term.c.y, dst, term.c.y);
  451. }
  452. void
  453. tinsertblankline(int n) {
  454. int i;
  455. Line blank;
  456. int bot = term.bot;
  457. if(term.c.y > term.bot)
  458. bot = term.row - 1;
  459. else if(term.c.y < term.top)
  460. bot = term.top - 1;
  461. if(term.c.y + n >= bot) {
  462. tclearregion(0, term.c.y, term.col-1, bot);
  463. return;
  464. }
  465. for(i = bot; i >= term.c.y+n; i--) {
  466. /* swap deleted line <-> blanked line */
  467. blank = term.line[i];
  468. term.line[i] = term.line[i-n];
  469. term.line[i-n] = blank;
  470. /* blank it */
  471. memset(blank, 0, term.col * sizeof(Glyph));
  472. }
  473. }
  474. void
  475. tdeleteline(int n) {
  476. int i;
  477. Line blank;
  478. int bot = term.bot;
  479. if(term.c.y > term.bot)
  480. bot = term.row - 1;
  481. else if(term.c.y < term.top)
  482. bot = term.top - 1;
  483. if(term.c.y + n >= bot) {
  484. tclearregion(0, term.c.y, term.col-1, bot);
  485. return;
  486. }
  487. for(i = term.c.y; i <= bot-n; i++) {
  488. /* swap deleted line <-> blanked line */
  489. blank = term.line[i];
  490. term.line[i] = term.line[i+n];
  491. term.line[i+n] = blank;
  492. /* blank it */
  493. memset(blank, 0, term.col * sizeof(Glyph));
  494. }
  495. }
  496. void
  497. tsetattr(int *attr, int l) {
  498. int i;
  499. for(i = 0; i < l; i++) {
  500. switch(attr[i]) {
  501. case 0:
  502. term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
  503. term.c.attr.fg = DefaultFG;
  504. term.c.attr.bg = DefaultBG;
  505. break;
  506. case 1:
  507. term.c.attr.mode |= ATTR_BOLD;
  508. break;
  509. case 4:
  510. term.c.attr.mode |= ATTR_UNDERLINE;
  511. break;
  512. case 7:
  513. term.c.attr.mode |= ATTR_REVERSE;
  514. break;
  515. case 22:
  516. term.c.attr.mode &= ~ATTR_BOLD;
  517. break;
  518. case 24:
  519. term.c.attr.mode &= ~ATTR_UNDERLINE;
  520. break;
  521. case 27:
  522. term.c.attr.mode &= ~ATTR_REVERSE;
  523. break;
  524. case 39:
  525. term.c.attr.fg = DefaultFG;
  526. break;
  527. case 49:
  528. term.c.attr.fg = DefaultBG;
  529. break;
  530. default:
  531. if(BETWEEN(attr[i], 30, 37))
  532. term.c.attr.fg = attr[i] - 30;
  533. else if(BETWEEN(attr[i], 40, 47))
  534. term.c.attr.bg = attr[i] - 40;
  535. else
  536. fprintf(stderr, "erresc: gfx attr %d unkown\n", attr[i]);
  537. break;
  538. }
  539. }
  540. }
  541. void
  542. tsetscroll(int t, int b) {
  543. int temp;
  544. LIMIT(t, 0, term.row-1);
  545. LIMIT(b, 0, term.row-1);
  546. if(t > b) {
  547. temp = t;
  548. t = b;
  549. b = temp;
  550. }
  551. term.top = t;
  552. term.bot = b;
  553. }
  554. void
  555. csihandle(void) {
  556. switch(escseq.mode) {
  557. default:
  558. unknown:
  559. printf("erresc: unknown csi ");
  560. csidump();
  561. /* die(""); */
  562. break;
  563. case '@': /* ICH -- Insert <n> blank char */
  564. DEFAULT(escseq.arg[0], 1);
  565. tinsertblank(escseq.arg[0]);
  566. break;
  567. case 'A': /* CUU -- Cursor <n> Up */
  568. case 'e':
  569. DEFAULT(escseq.arg[0], 1);
  570. tmoveto(term.c.x, term.c.y-escseq.arg[0]);
  571. break;
  572. case 'B': /* CUD -- Cursor <n> Down */
  573. DEFAULT(escseq.arg[0], 1);
  574. tmoveto(term.c.x, term.c.y+escseq.arg[0]);
  575. break;
  576. case 'C': /* CUF -- Cursor <n> Forward */
  577. case 'a':
  578. DEFAULT(escseq.arg[0], 1);
  579. tmoveto(term.c.x+escseq.arg[0], term.c.y);
  580. break;
  581. case 'D': /* CUB -- Cursor <n> Backward */
  582. DEFAULT(escseq.arg[0], 1);
  583. tmoveto(term.c.x-escseq.arg[0], term.c.y);
  584. break;
  585. case 'E': /* CNL -- Cursor <n> Down and first col */
  586. DEFAULT(escseq.arg[0], 1);
  587. tmoveto(0, term.c.y+escseq.arg[0]);
  588. break;
  589. case 'F': /* CPL -- Cursor <n> Up and first col */
  590. DEFAULT(escseq.arg[0], 1);
  591. tmoveto(0, term.c.y-escseq.arg[0]);
  592. break;
  593. case 'G': /* CHA -- Move to <col> */
  594. case '`': /* XXX: HPA -- same? */
  595. DEFAULT(escseq.arg[0], 1);
  596. tmoveto(escseq.arg[0]-1, term.c.y);
  597. break;
  598. case 'H': /* CUP -- Move to <row> <col> */
  599. case 'f': /* XXX: HVP -- same? */
  600. DEFAULT(escseq.arg[0], 1);
  601. DEFAULT(escseq.arg[1], 1);
  602. tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
  603. break;
  604. /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
  605. case 'J': /* ED -- Clear screen */
  606. switch(escseq.arg[0]) {
  607. case 0: /* below */
  608. tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
  609. break;
  610. case 1: /* above */
  611. tclearregion(0, 0, term.c.x, term.c.y);
  612. break;
  613. case 2: /* all */
  614. tclearregion(0, 0, term.col-1, term.row-1);
  615. break;
  616. case 3: /* XXX: erase saved lines (xterm) */
  617. default:
  618. goto unknown;
  619. }
  620. break;
  621. case 'K': /* EL -- Clear line */
  622. switch(escseq.arg[0]) {
  623. case 0: /* right */
  624. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  625. break;
  626. case 1: /* left */
  627. tclearregion(0, term.c.y, term.c.x, term.c.y);
  628. break;
  629. case 2: /* all */
  630. tclearregion(0, term.c.y, term.col-1, term.c.y);
  631. break;
  632. }
  633. break;
  634. case 'S': /* SU -- Scroll <n> line up */
  635. DEFAULT(escseq.arg[0], 1);
  636. tscrollup(escseq.arg[0]);
  637. break;
  638. case 'T': /* SD -- Scroll <n> line down */
  639. DEFAULT(escseq.arg[0], 1);
  640. tscrolldown(escseq.arg[0]);
  641. break;
  642. case 'L': /* IL -- Insert <n> blank lines */
  643. DEFAULT(escseq.arg[0], 1);
  644. tinsertblankline(escseq.arg[0]);
  645. break;
  646. case 'l': /* RM -- Reset Mode */
  647. if(escseq.priv) {
  648. switch(escseq.arg[0]) {
  649. case 1:
  650. term.mode &= ~MODE_APPKEYPAD;
  651. break;
  652. case 7:
  653. term.mode &= ~MODE_WRAP;
  654. break;
  655. case 12: /* att610 -- Stop blinking cursor (IGNORED) */
  656. break;
  657. case 25:
  658. term.hidec = 1;
  659. break;
  660. case 1048: /* XXX: no alt. screen to erase/save */
  661. case 1049:
  662. tcursor(CURSOR_LOAD);
  663. tclearregion(0, 0, term.col-1, term.row-1);
  664. break;
  665. default:
  666. goto unknown;
  667. }
  668. } else {
  669. switch(escseq.arg[0]) {
  670. case 4:
  671. term.mode &= ~MODE_INSERT;
  672. break;
  673. default:
  674. goto unknown;
  675. }
  676. }
  677. break;
  678. case 'M': /* DL -- Delete <n> lines */
  679. DEFAULT(escseq.arg[0], 1);
  680. tdeleteline(escseq.arg[0]);
  681. break;
  682. case 'X': /* ECH -- Erase <n> char */
  683. DEFAULT(escseq.arg[0], 1);
  684. tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
  685. break;
  686. case 'P': /* DCH -- Delete <n> char */
  687. DEFAULT(escseq.arg[0], 1);
  688. tdeletechar(escseq.arg[0]);
  689. break;
  690. /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
  691. case 'd': /* VPA -- Move to <row> */
  692. DEFAULT(escseq.arg[0], 1);
  693. tmoveto(term.c.x, escseq.arg[0]-1);
  694. break;
  695. case 'h': /* SM -- Set terminal mode */
  696. if(escseq.priv) {
  697. switch(escseq.arg[0]) {
  698. case 1:
  699. term.mode |= MODE_APPKEYPAD;
  700. break;
  701. case 7:
  702. term.mode |= MODE_WRAP;
  703. break;
  704. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  705. break;
  706. case 25:
  707. term.hidec = 0;
  708. break;
  709. case 1048:
  710. case 1049: /* XXX: no alt. screen to erase/save */
  711. tcursor(CURSOR_SAVE);
  712. tclearregion(0, 0, term.col-1, term.row-1);
  713. break;
  714. default: goto unknown;
  715. }
  716. } else {
  717. switch(escseq.arg[0]) {
  718. case 4:
  719. term.mode |= MODE_INSERT;
  720. break;
  721. default: goto unknown;
  722. }
  723. };
  724. break;
  725. case 'm': /* SGR -- Terminal attribute (color) */
  726. tsetattr(escseq.arg, escseq.narg);
  727. break;
  728. case 'r': /* DECSTBM -- Set Scrolling Region */
  729. if(escseq.priv)
  730. goto unknown;
  731. else {
  732. DEFAULT(escseq.arg[0], 1);
  733. DEFAULT(escseq.arg[1], term.row);
  734. tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
  735. }
  736. break;
  737. case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
  738. tcursor(CURSOR_SAVE);
  739. break;
  740. case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
  741. tcursor(CURSOR_LOAD);
  742. break;
  743. }
  744. }
  745. void
  746. csidump(void) {
  747. int i;
  748. printf("ESC [ %s", escseq.priv ? "? " : "");
  749. if(escseq.narg)
  750. for(i = 0; i < escseq.narg; i++)
  751. printf("%d ", escseq.arg[i]);
  752. if(escseq.mode)
  753. putchar(escseq.mode);
  754. putchar('\n');
  755. }
  756. void
  757. csireset(void) {
  758. memset(&escseq, 0, sizeof(escseq));
  759. }
  760. void
  761. tputtab(void) {
  762. int space = TAB - term.c.x % TAB;
  763. if(term.c.x + space >= term.col)
  764. space--;
  765. for(; space > 0; space--)
  766. tmovecursor(CURSOR_RIGHT);
  767. }
  768. void
  769. tputc(char c) {
  770. if(term.esc & ESC_START) {
  771. if(term.esc & ESC_CSI) {
  772. escseq.buf[escseq.len++] = c;
  773. if(BETWEEN(c, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
  774. term.esc = 0;
  775. csiparse(), csihandle();
  776. }
  777. } else if(term.esc & ESC_OSC) {
  778. if(c == ';') {
  779. term.titlelen = 0;
  780. term.esc = ESC_START | ESC_TITLE;
  781. }
  782. } else if(term.esc & ESC_TITLE) {
  783. if(c == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
  784. term.esc = 0;
  785. term.title[term.titlelen] = '\0';
  786. XStoreName(xw.dis, xw.win, term.title);
  787. } else {
  788. term.title[term.titlelen++] = c;
  789. }
  790. } else if(term.esc & ESC_ALTCHARSET) {
  791. switch(c) {
  792. case '0': /* Line drawing crap */
  793. term.c.attr.mode |= ATTR_GFX;
  794. break;
  795. case 'B': /* Back to regular text */
  796. term.c.attr.mode &= ~ATTR_GFX;
  797. break;
  798. default:
  799. printf("esc unhandled charset: ESC ( %c\n", c);
  800. }
  801. term.esc = 0;
  802. } else {
  803. switch(c) {
  804. case '[':
  805. term.esc |= ESC_CSI;
  806. break;
  807. case ']':
  808. term.esc |= ESC_OSC;
  809. break;
  810. case '(':
  811. term.esc |= ESC_ALTCHARSET;
  812. break;
  813. case 'A':
  814. tmoveto(term.c.x, term.c.y-1);
  815. term.esc = 0;
  816. break;
  817. case 'B':
  818. tmoveto(term.c.x, term.c.y+1);
  819. term.esc = 0;
  820. break;
  821. case 'C':
  822. tmoveto(term.c.x+1, term.c.y);
  823. term.esc = 0;
  824. break;
  825. case 'D': /* XXX: CUP (VT100) or IND (VT52) ... */
  826. tmoveto(term.c.x-1, term.c.y);
  827. term.esc = 0;
  828. break;
  829. case 'E': /* NEL -- Next line */
  830. tnewline();
  831. term.esc = 0;
  832. break;
  833. case 'M': /* RI -- Reverse index */
  834. if(term.c.y == term.top)
  835. tscrolldown(1);
  836. else
  837. tmoveto(term.c.x, term.c.y-1);
  838. term.esc = 0;
  839. break;
  840. case 'c': /* RIS -- Reset to inital state */
  841. treset();
  842. term.esc = 0;
  843. break;
  844. case '=': /* DECPAM */
  845. term.mode |= MODE_APPKEYPAD;
  846. term.esc = 0;
  847. break;
  848. case '>': /* DECPNM */
  849. term.mode &= ~MODE_APPKEYPAD;
  850. term.esc = 0;
  851. break;
  852. case '7':
  853. tcursor(CURSOR_SAVE);
  854. term.esc = 0;
  855. break;
  856. case '8':
  857. tcursor(CURSOR_LOAD);
  858. term.esc = 0;
  859. break;
  860. default:
  861. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n", c, isprint(c)?c:'.');
  862. term.esc = 0;
  863. }
  864. }
  865. } else {
  866. switch(c) {
  867. case '\t':
  868. tputtab();
  869. break;
  870. case '\b':
  871. tmovecursor(CURSOR_LEFT);
  872. break;
  873. case '\r':
  874. tmoveto(0, term.c.y);
  875. break;
  876. case '\n':
  877. tnewline();
  878. break;
  879. case '\a':
  880. xbell();
  881. break;
  882. case '\033':
  883. csireset();
  884. term.esc = ESC_START;
  885. break;
  886. default:
  887. tsetchar(c);
  888. tmovecursor(CURSOR_RIGHT);
  889. break;
  890. }
  891. }
  892. }
  893. void
  894. tputs(char *s, int len) {
  895. for(; len > 0; len--)
  896. tputc(*s++);
  897. }
  898. void
  899. tresize(int col, int row) {
  900. int i;
  901. Line *line;
  902. int minrow = MIN(row, term.row);
  903. int mincol = MIN(col, term.col);
  904. if(col < 1 || row < 1)
  905. return;
  906. /* alloc */
  907. line = calloc(row, sizeof(Line));
  908. for(i = 0 ; i < row; i++)
  909. line[i] = calloc(col, sizeof(Glyph));
  910. /* copy */
  911. for(i = 0 ; i < minrow; i++)
  912. memcpy(line[i], term.line[i], mincol * sizeof(Glyph));
  913. /* free */
  914. for(i = 0; i < term.row; i++)
  915. free(term.line[i]);
  916. free(term.line);
  917. LIMIT(term.c.x, 0, col-1);
  918. LIMIT(term.c.y, 0, row-1);
  919. LIMIT(term.top, 0, row-1);
  920. LIMIT(term.bot, 0, row-1);
  921. term.bot = row-1;
  922. term.line = line;
  923. term.col = col, term.row = row;
  924. }
  925. unsigned long
  926. xgetcol(const char *s) {
  927. XColor color;
  928. Colormap cmap = DefaultColormap(xw.dis, xw.scr);
  929. if(!XAllocNamedColor(xw.dis, cmap, s, &color, &color)) {
  930. color.pixel = WhitePixel(xw.dis, xw.scr);
  931. fprintf(stderr, "Could not allocate color '%s'\n", s);
  932. }
  933. return color.pixel;
  934. }
  935. void
  936. xclear(int x1, int y1, int x2, int y2) {
  937. XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
  938. XFillRectangle(xw.dis, xw.buf, dc.gc,
  939. x1 * xw.cw, y1 * xw.ch,
  940. (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
  941. }
  942. void
  943. xhints(void)
  944. {
  945. XClassHint chint = {TNAME, TNAME};
  946. XWMHints wmhint = {.flags = InputHint, .input = 1};
  947. XSizeHints shint = {
  948. .flags = PSize | PResizeInc,
  949. .height = xw.h, /* XXX: doesn't seem to work, see run() */
  950. .width = xw.w,
  951. .height_inc = xw.ch,
  952. .width_inc = xw.cw,
  953. };
  954. XSetWMProperties(xw.dis, xw.win, NULL, NULL, NULL, 0, &shint, &wmhint, &chint);
  955. }
  956. void
  957. xinit(void) {
  958. int i;
  959. xw.dis = XOpenDisplay(NULL);
  960. xw.scr = XDefaultScreen(xw.dis);
  961. if(!xw.dis)
  962. die("Can't open display\n");
  963. /* font */
  964. if(!(dc.font = XLoadQueryFont(xw.dis, FONT)) || !(dc.bfont = XLoadQueryFont(xw.dis, BOLDFONT)))
  965. die("Can't load font %s\n", dc.font ? BOLDFONT : FONT);
  966. /* XXX: Assuming same size for bold font */
  967. xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
  968. xw.ch = dc.font->ascent + dc.font->descent;
  969. /* colors */
  970. for(i = 0; i < LEN(colorname); i++)
  971. dc.col[i] = xgetcol(colorname[i]);
  972. term.c.attr.fg = DefaultFG;
  973. term.c.attr.bg = DefaultBG;
  974. term.c.attr.mode = ATTR_NULL;
  975. /* windows */
  976. xw.h = term.row * xw.ch + 2*BORDER;
  977. xw.w = term.col * xw.cw + 2*BORDER;
  978. xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
  979. xw.w, xw.h, 0,
  980. dc.col[DefaultBG],
  981. dc.col[DefaultBG]);
  982. xw.bufw = xw.w - 2*BORDER;
  983. xw.bufh = xw.h - 2*BORDER;
  984. xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
  985. /* gc */
  986. dc.gc = XCreateGC(xw.dis, xw.win, 0, NULL);
  987. XMapWindow(xw.dis, xw.win);
  988. xhints();
  989. XStoreName(xw.dis, xw.win, TNAME);
  990. XSync(xw.dis, 0);
  991. }
  992. void
  993. xdraws(char *s, Glyph base, int x, int y, int len) {
  994. unsigned long xfg, xbg;
  995. int winx = x*xw.cw, winy = y*xw.ch + dc.font->ascent, width = len*xw.cw;
  996. int i;
  997. if(base.mode & ATTR_REVERSE)
  998. xfg = dc.col[base.bg], xbg = dc.col[base.fg];
  999. else
  1000. xfg = dc.col[base.fg], xbg = dc.col[base.bg];
  1001. XSetBackground(xw.dis, dc.gc, xbg);
  1002. XSetForeground(xw.dis, dc.gc, xfg);
  1003. if(base.mode & ATTR_GFX)
  1004. for(i = 0; i < len; i++)
  1005. s[i] = gfx[s[i]];
  1006. XSetFont(xw.dis, dc.gc, base.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
  1007. XDrawImageString(xw.dis, xw.buf, dc.gc, winx, winy, s, len);
  1008. if(base.mode & ATTR_UNDERLINE)
  1009. XDrawLine(xw.dis, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
  1010. }
  1011. void
  1012. xcursor(int mode) {
  1013. static int oldx = 0;
  1014. static int oldy = 0;
  1015. Glyph g = {' ', ATTR_NULL, DefaultBG, DefaultCS, 0};
  1016. LIMIT(oldx, 0, term.col-1);
  1017. LIMIT(oldy, 0, term.row-1);
  1018. if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
  1019. g.c = term.line[term.c.y][term.c.x].c;
  1020. /* remove the old cursor */
  1021. if(term.line[oldy][oldx].state & GLYPH_SET)
  1022. xdraws(&term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1);
  1023. else
  1024. xclear(oldx, oldy, oldx, oldy);
  1025. /* draw the new one */
  1026. if(mode == CURSOR_DRAW) {
  1027. xdraws(&g.c, g, term.c.x, term.c.y, 1);
  1028. oldx = term.c.x, oldy = term.c.y;
  1029. }
  1030. }
  1031. #ifdef DEBUG
  1032. /* basic drawing routines */
  1033. void
  1034. xdrawc(int x, int y, Glyph g) {
  1035. XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
  1036. XSetBackground(xw.dis, dc.gc, dc.col[g.bg]);
  1037. XSetForeground(xw.dis, dc.gc, dc.col[g.fg]);
  1038. XSetFont(xw.dis, dc.gc, g.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
  1039. XDrawImageString(xw.dis, xw.buf, dc.gc, r.x, r.y+dc.font->ascent, &g.c, 1);
  1040. }
  1041. void
  1042. draw_(int dummy) {
  1043. int x, y;
  1044. xclear(0, 0, term.col-1, term.row-1);
  1045. for(y = 0; y < term.row; y++)
  1046. for(x = 0; x < term.col; x++)
  1047. if(term.line[y][x].state & GLYPH_SET)
  1048. xdrawc(x, y, term.line[y][x]);
  1049. if(!term.hidec)
  1050. xcursor(CURSOR_DRAW);
  1051. XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
  1052. XFlush(xw.dis);
  1053. }
  1054. #endif
  1055. void
  1056. draw(int redraw_all) {
  1057. int i, x, y, ox;
  1058. Glyph base, new;
  1059. char buf[DRAW_BUF_SIZ];
  1060. for(y = 0; y < term.row; y++) {
  1061. base = term.line[y][0];
  1062. i = ox = 0;
  1063. for(x = 0; x < term.col; x++) {
  1064. new = term.line[y][x];
  1065. if(!ATTRCMP(base, new) && i < DRAW_BUF_SIZ)
  1066. buf[i++] = new.c;
  1067. else {
  1068. xdraws(buf, base, ox, y, i);
  1069. buf[0] = new.c;
  1070. i = 1;
  1071. ox = x;
  1072. base = new;
  1073. }
  1074. }
  1075. xdraws(buf, base, ox, y, i);
  1076. }
  1077. xcursor(term.hidec ? CURSOR_HIDE : CURSOR_DRAW);
  1078. XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
  1079. XFlush(xw.dis);
  1080. }
  1081. void
  1082. expose(XEvent *ev) {
  1083. draw(SCREEN_REDRAW);
  1084. }
  1085. char*
  1086. kmap(KeySym k) {
  1087. int i;
  1088. for(i = 0; i < LEN(key); i++)
  1089. if(key[i].k == k)
  1090. return (char*)key[i].s;
  1091. return NULL;
  1092. }
  1093. void
  1094. kpress(XEvent *ev) {
  1095. XKeyEvent *e = &ev->xkey;
  1096. KeySym ksym;
  1097. char buf[32];
  1098. char *customkey;
  1099. int len;
  1100. int meta;
  1101. int shift;
  1102. meta = e->state & Mod1Mask;
  1103. shift = e->state & ShiftMask;
  1104. len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
  1105. if(customkey = kmap(ksym))
  1106. ttywrite(customkey, strlen(customkey));
  1107. else if(len > 0) {
  1108. buf[sizeof(buf)-1] = '\0';
  1109. if(meta && len == 1)
  1110. ttywrite("\033", 1);
  1111. ttywrite(buf, len);
  1112. } else
  1113. switch(ksym) {
  1114. case XK_Up:
  1115. case XK_Down:
  1116. case XK_Left:
  1117. case XK_Right:
  1118. sprintf(buf, "\033%c%c", term.mode & MODE_APPKEYPAD ? 'O' : '[', "DACB"[ksym - XK_Left]);
  1119. ttywrite(buf, 3);
  1120. break;
  1121. case XK_Insert:
  1122. if(shift)
  1123. draw(1), puts("draw!")/* XXX: paste X clipboard */;
  1124. break;
  1125. default:
  1126. fprintf(stderr, "errkey: %d\n", (int)ksym);
  1127. break;
  1128. }
  1129. }
  1130. void
  1131. resize(XEvent *e) {
  1132. int col, row;
  1133. if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
  1134. return;
  1135. xw.w = e->xconfigure.width;
  1136. xw.h = e->xconfigure.height;
  1137. xw.bufw = xw.w - 2*BORDER;
  1138. xw.bufh = xw.h - 2*BORDER;
  1139. col = xw.bufw / xw.cw;
  1140. row = xw.bufh / xw.ch;
  1141. tresize(col, row);
  1142. ttyresize(col, row);
  1143. XFreePixmap(xw.dis, xw.buf);
  1144. xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
  1145. draw(SCREEN_REDRAW);
  1146. }
  1147. void
  1148. run(void) {
  1149. XEvent ev;
  1150. fd_set rfd;
  1151. int xfd = XConnectionNumber(xw.dis);
  1152. running = 1;
  1153. XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
  1154. XResizeWindow(xw.dis, xw.win, xw.w, xw.h); /* XXX: fix resize bug in wmii (?) */
  1155. while(running) {
  1156. FD_ZERO(&rfd);
  1157. FD_SET(cmdfd, &rfd);
  1158. FD_SET(xfd, &rfd);
  1159. if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) == -1) {
  1160. if(errno == EINTR)
  1161. continue;
  1162. die("select failed: %s\n", SERRNO);
  1163. }
  1164. if(FD_ISSET(cmdfd, &rfd)) {
  1165. ttyread();
  1166. draw(SCREEN_UPDATE);
  1167. }
  1168. while(XPending(xw.dis)) {
  1169. XNextEvent(xw.dis, &ev);
  1170. if(handler[ev.type])
  1171. (handler[ev.type])(&ev);
  1172. }
  1173. }
  1174. }
  1175. int
  1176. main(int argc, char *argv[]) {
  1177. if(argc == 2 && !strncmp("-v", argv[1], 3))
  1178. die("st-" VERSION ", © 2009 st engineers\n");
  1179. else if(argc != 1)
  1180. die("usage: st [-v]\n");
  1181. setlocale(LC_CTYPE, "");
  1182. tnew(80, 24);
  1183. ttynew();
  1184. xinit();
  1185. run();
  1186. return 0;
  1187. }