My build of suckless st terminal
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1370 lines
30 KiB

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