Simple Terminal from SuckLess
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.

1369 lines
30 KiB

14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
  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 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 38:
  525. if (i + 2 < l && attr[i + 1] == 5) {
  526. i += 2;
  527. if (BETWEEN(attr[i], 0, 255))
  528. term.c.attr.fg = attr[i];
  529. else
  530. fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
  531. }
  532. else
  533. fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
  534. break;
  535. case 39:
  536. term.c.attr.fg = DefaultFG;
  537. break;
  538. case 48:
  539. if (i + 2 < l && attr[i + 1] == 5) {
  540. i += 2;
  541. if (BETWEEN(attr[i], 0, 255))
  542. term.c.attr.bg = attr[i];
  543. else
  544. fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
  545. }
  546. else
  547. fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
  548. break;
  549. case 49:
  550. term.c.attr.bg = DefaultBG;
  551. break;
  552. default:
  553. if(BETWEEN(attr[i], 30, 37))
  554. term.c.attr.fg = attr[i] - 30;
  555. else if(BETWEEN(attr[i], 40, 47))
  556. term.c.attr.bg = attr[i] - 40;
  557. else if(BETWEEN(attr[i], 90, 97))
  558. term.c.attr.fg = attr[i] - 90 + 8;
  559. else if(BETWEEN(attr[i], 100, 107))
  560. term.c.attr.fg = attr[i] - 100 + 8;
  561. else
  562. fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
  563. break;
  564. }
  565. }
  566. }
  567. void
  568. tsetscroll(int t, int b) {
  569. int temp;
  570. LIMIT(t, 0, term.row-1);
  571. LIMIT(b, 0, term.row-1);
  572. if(t > b) {
  573. temp = t;
  574. t = b;
  575. b = temp;
  576. }
  577. term.top = t;
  578. term.bot = b;
  579. }
  580. void
  581. csihandle(void) {
  582. switch(escseq.mode) {
  583. default:
  584. unknown:
  585. printf("erresc: unknown csi ");
  586. csidump();
  587. /* die(""); */
  588. break;
  589. case '@': /* ICH -- Insert <n> blank char */
  590. DEFAULT(escseq.arg[0], 1);
  591. tinsertblank(escseq.arg[0]);
  592. break;
  593. case 'A': /* CUU -- Cursor <n> Up */
  594. case 'e':
  595. DEFAULT(escseq.arg[0], 1);
  596. tmoveto(term.c.x, term.c.y-escseq.arg[0]);
  597. break;
  598. case 'B': /* CUD -- Cursor <n> Down */
  599. DEFAULT(escseq.arg[0], 1);
  600. tmoveto(term.c.x, term.c.y+escseq.arg[0]);
  601. break;
  602. case 'C': /* CUF -- Cursor <n> Forward */
  603. case 'a':
  604. DEFAULT(escseq.arg[0], 1);
  605. tmoveto(term.c.x+escseq.arg[0], term.c.y);
  606. break;
  607. case 'D': /* CUB -- Cursor <n> Backward */
  608. DEFAULT(escseq.arg[0], 1);
  609. tmoveto(term.c.x-escseq.arg[0], term.c.y);
  610. break;
  611. case 'E': /* CNL -- Cursor <n> Down and first col */
  612. DEFAULT(escseq.arg[0], 1);
  613. tmoveto(0, term.c.y+escseq.arg[0]);
  614. break;
  615. case 'F': /* CPL -- Cursor <n> Up and first col */
  616. DEFAULT(escseq.arg[0], 1);
  617. tmoveto(0, term.c.y-escseq.arg[0]);
  618. break;
  619. case 'G': /* CHA -- Move to <col> */
  620. case '`': /* XXX: HPA -- same? */
  621. DEFAULT(escseq.arg[0], 1);
  622. tmoveto(escseq.arg[0]-1, term.c.y);
  623. break;
  624. case 'H': /* CUP -- Move to <row> <col> */
  625. case 'f': /* XXX: HVP -- same? */
  626. DEFAULT(escseq.arg[0], 1);
  627. DEFAULT(escseq.arg[1], 1);
  628. tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
  629. break;
  630. /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
  631. case 'J': /* ED -- Clear screen */
  632. switch(escseq.arg[0]) {
  633. case 0: /* below */
  634. tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
  635. break;
  636. case 1: /* above */
  637. tclearregion(0, 0, term.c.x, term.c.y);
  638. break;
  639. case 2: /* all */
  640. tclearregion(0, 0, term.col-1, term.row-1);
  641. break;
  642. case 3: /* XXX: erase saved lines (xterm) */
  643. default:
  644. goto unknown;
  645. }
  646. break;
  647. case 'K': /* EL -- Clear line */
  648. switch(escseq.arg[0]) {
  649. case 0: /* right */
  650. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  651. break;
  652. case 1: /* left */
  653. tclearregion(0, term.c.y, term.c.x, term.c.y);
  654. break;
  655. case 2: /* all */
  656. tclearregion(0, term.c.y, term.col-1, term.c.y);
  657. break;
  658. }
  659. break;
  660. case 'S': /* SU -- Scroll <n> line up */
  661. DEFAULT(escseq.arg[0], 1);
  662. tscrollup(escseq.arg[0]);
  663. break;
  664. case 'T': /* SD -- Scroll <n> line down */
  665. DEFAULT(escseq.arg[0], 1);
  666. tscrolldown(escseq.arg[0]);
  667. break;
  668. case 'L': /* IL -- Insert <n> blank lines */
  669. DEFAULT(escseq.arg[0], 1);
  670. tinsertblankline(escseq.arg[0]);
  671. break;
  672. case 'l': /* RM -- Reset Mode */
  673. if(escseq.priv) {
  674. switch(escseq.arg[0]) {
  675. case 1:
  676. term.mode &= ~MODE_APPKEYPAD;
  677. break;
  678. case 7:
  679. term.mode &= ~MODE_WRAP;
  680. break;
  681. case 12: /* att610 -- Stop blinking cursor (IGNORED) */
  682. break;
  683. case 25:
  684. term.hidec = 1;
  685. break;
  686. case 1048: /* XXX: no alt. screen to erase/save */
  687. case 1049:
  688. tcursor(CURSOR_LOAD);
  689. tclearregion(0, 0, term.col-1, term.row-1);
  690. break;
  691. default:
  692. goto unknown;
  693. }
  694. } else {
  695. switch(escseq.arg[0]) {
  696. case 4:
  697. term.mode &= ~MODE_INSERT;
  698. break;
  699. default:
  700. goto unknown;
  701. }
  702. }
  703. break;
  704. case 'M': /* DL -- Delete <n> lines */
  705. DEFAULT(escseq.arg[0], 1);
  706. tdeleteline(escseq.arg[0]);
  707. break;
  708. case 'X': /* ECH -- Erase <n> char */
  709. DEFAULT(escseq.arg[0], 1);
  710. tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
  711. break;
  712. case 'P': /* DCH -- Delete <n> char */
  713. DEFAULT(escseq.arg[0], 1);
  714. tdeletechar(escseq.arg[0]);
  715. break;
  716. /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
  717. case 'd': /* VPA -- Move to <row> */
  718. DEFAULT(escseq.arg[0], 1);
  719. tmoveto(term.c.x, escseq.arg[0]-1);
  720. break;
  721. case 'h': /* SM -- Set terminal mode */
  722. if(escseq.priv) {
  723. switch(escseq.arg[0]) {
  724. case 1:
  725. term.mode |= MODE_APPKEYPAD;
  726. break;
  727. case 7:
  728. term.mode |= MODE_WRAP;
  729. break;
  730. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  731. break;
  732. case 25:
  733. term.hidec = 0;
  734. break;
  735. case 1048:
  736. case 1049: /* XXX: no alt. screen to erase/save */
  737. tcursor(CURSOR_SAVE);
  738. tclearregion(0, 0, term.col-1, term.row-1);
  739. break;
  740. default: goto unknown;
  741. }
  742. } else {
  743. switch(escseq.arg[0]) {
  744. case 4:
  745. term.mode |= MODE_INSERT;
  746. break;
  747. default: goto unknown;
  748. }
  749. };
  750. break;
  751. case 'm': /* SGR -- Terminal attribute (color) */
  752. tsetattr(escseq.arg, escseq.narg);
  753. break;
  754. case 'r': /* DECSTBM -- Set Scrolling Region */
  755. if(escseq.priv)
  756. goto unknown;
  757. else {
  758. DEFAULT(escseq.arg[0], 1);
  759. DEFAULT(escseq.arg[1], term.row);
  760. tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
  761. }
  762. break;
  763. case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
  764. tcursor(CURSOR_SAVE);
  765. break;
  766. case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
  767. tcursor(CURSOR_LOAD);
  768. break;
  769. }
  770. }
  771. void
  772. csidump(void) {
  773. int i;
  774. printf("ESC [ %s", escseq.priv ? "? " : "");
  775. if(escseq.narg)
  776. for(i = 0; i < escseq.narg; i++)
  777. printf("%d ", escseq.arg[i]);
  778. if(escseq.mode)
  779. putchar(escseq.mode);
  780. putchar('\n');
  781. }
  782. void
  783. csireset(void) {
  784. memset(&escseq, 0, sizeof(escseq));
  785. }
  786. void
  787. tputtab(void) {
  788. int space = TAB - term.c.x % TAB;
  789. if(term.c.x + space >= term.col)
  790. space--;
  791. for(; space > 0; space--)
  792. tmovecursor(CURSOR_RIGHT);
  793. }
  794. void
  795. tputc(char c) {
  796. if(term.esc & ESC_START) {
  797. if(term.esc & ESC_CSI) {
  798. escseq.buf[escseq.len++] = c;
  799. if(BETWEEN(c, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
  800. term.esc = 0;
  801. csiparse(), csihandle();
  802. }
  803. } else if(term.esc & ESC_OSC) {
  804. if(c == ';') {
  805. term.titlelen = 0;
  806. term.esc = ESC_START | ESC_TITLE;
  807. }
  808. } else if(term.esc & ESC_TITLE) {
  809. if(c == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
  810. term.esc = 0;
  811. term.title[term.titlelen] = '\0';
  812. XStoreName(xw.dis, xw.win, term.title);
  813. } else {
  814. term.title[term.titlelen++] = c;
  815. }
  816. } else if(term.esc & ESC_ALTCHARSET) {
  817. switch(c) {
  818. case '0': /* Line drawing crap */
  819. term.c.attr.mode |= ATTR_GFX;
  820. break;
  821. case 'B': /* Back to regular text */
  822. term.c.attr.mode &= ~ATTR_GFX;
  823. break;
  824. default:
  825. printf("esc unhandled charset: ESC ( %c\n", c);
  826. }
  827. term.esc = 0;
  828. } else {
  829. switch(c) {
  830. case '[':
  831. term.esc |= ESC_CSI;
  832. break;
  833. case ']':
  834. term.esc |= ESC_OSC;
  835. break;
  836. case '(':
  837. term.esc |= ESC_ALTCHARSET;
  838. break;
  839. case 'A':
  840. tmoveto(term.c.x, term.c.y-1);
  841. term.esc = 0;
  842. break;
  843. case 'B':
  844. tmoveto(term.c.x, term.c.y+1);
  845. term.esc = 0;
  846. break;
  847. case 'C':
  848. tmoveto(term.c.x+1, term.c.y);
  849. term.esc = 0;
  850. break;
  851. case 'D': /* XXX: CUP (VT100) or IND (VT52) ... */
  852. tmoveto(term.c.x-1, term.c.y);
  853. term.esc = 0;
  854. break;
  855. case 'E': /* NEL -- Next line */
  856. tnewline();
  857. term.esc = 0;
  858. break;
  859. case 'M': /* RI -- Reverse index */
  860. if(term.c.y == term.top)
  861. tscrolldown(1);
  862. else
  863. tmoveto(term.c.x, term.c.y-1);
  864. term.esc = 0;
  865. break;
  866. case 'c': /* RIS -- Reset to inital state */
  867. treset();
  868. term.esc = 0;
  869. break;
  870. case '=': /* DECPAM */
  871. term.mode |= MODE_APPKEYPAD;
  872. term.esc = 0;
  873. break;
  874. case '>': /* DECPNM */
  875. term.mode &= ~MODE_APPKEYPAD;
  876. term.esc = 0;
  877. break;
  878. case '7':
  879. tcursor(CURSOR_SAVE);
  880. term.esc = 0;
  881. break;
  882. case '8':
  883. tcursor(CURSOR_LOAD);
  884. term.esc = 0;
  885. break;
  886. default:
  887. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n", c, isprint(c)?c:'.');
  888. term.esc = 0;
  889. }
  890. }
  891. } else {
  892. switch(c) {
  893. case '\t':
  894. tputtab();
  895. break;
  896. case '\b':
  897. tmovecursor(CURSOR_LEFT);
  898. break;
  899. case '\r':
  900. tmoveto(0, term.c.y);
  901. break;
  902. case '\n':
  903. tnewline();
  904. break;
  905. case '\a':
  906. xbell();
  907. break;
  908. case '\033':
  909. csireset();
  910. term.esc = ESC_START;
  911. break;
  912. default:
  913. tsetchar(c);
  914. tmovecursor(CURSOR_RIGHT);
  915. break;
  916. }
  917. }
  918. }
  919. void
  920. tputs(char *s, int len) {
  921. for(; len > 0; len--)
  922. tputc(*s++);
  923. }
  924. void
  925. tresize(int col, int row) {
  926. int i;
  927. Line *line;
  928. int minrow = MIN(row, term.row);
  929. int mincol = MIN(col, term.col);
  930. if(col < 1 || row < 1)
  931. return;
  932. /* alloc */
  933. line = calloc(row, sizeof(Line));
  934. for(i = 0 ; i < row; i++)
  935. line[i] = calloc(col, sizeof(Glyph));
  936. /* copy */
  937. for(i = 0 ; i < minrow; i++)
  938. memcpy(line[i], term.line[i], mincol * sizeof(Glyph));
  939. /* free */
  940. for(i = 0; i < term.row; i++)
  941. free(term.line[i]);
  942. free(term.line);
  943. LIMIT(term.c.x, 0, col-1);
  944. LIMIT(term.c.y, 0, row-1);
  945. LIMIT(term.top, 0, row-1);
  946. LIMIT(term.bot, 0, row-1);
  947. term.bot = row-1;
  948. term.line = line;
  949. term.col = col, term.row = row;
  950. }
  951. void
  952. tloadcols(void) {
  953. int i, r, g, b;
  954. XColor color;
  955. Colormap cmap = DefaultColormap(xw.dis, xw.scr);
  956. unsigned long white = WhitePixel(xw.dis, xw.scr);
  957. for(i = 0; i < 16; i++) {
  958. if (!XAllocNamedColor(xw.dis, cmap, colorname[i], &color, &color)) {
  959. dc.col[i] = white;
  960. fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
  961. } else
  962. dc.col[i] = color.pixel;
  963. }
  964. /* same colors as xterm */
  965. for(r = 0; r < 6; r++)
  966. for(g = 0; g < 6; g++)
  967. for(b = 0; b < 6; b++) {
  968. color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
  969. color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
  970. color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
  971. if (!XAllocColor(xw.dis, cmap, &color)) {
  972. dc.col[i] = white;
  973. fprintf(stderr, "Could not allocate color %d\n", i);
  974. } else
  975. dc.col[i] = color.pixel;
  976. i++;
  977. }
  978. for(r = 0; r < 24; r++, i++) {
  979. color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
  980. if (!XAllocColor(xw.dis, cmap, &color)) {
  981. dc.col[i] = white;
  982. fprintf(stderr, "Could not allocate color %d\n", i);
  983. } else
  984. dc.col[i] = color.pixel;
  985. }
  986. }
  987. void
  988. xclear(int x1, int y1, int x2, int y2) {
  989. XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
  990. XFillRectangle(xw.dis, xw.buf, dc.gc,
  991. x1 * xw.cw, y1 * xw.ch,
  992. (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
  993. }
  994. void
  995. xhints(void)
  996. {
  997. XClassHint class = {TNAME, TNAME};
  998. XWMHints wm = {.flags = InputHint, .input = 1};
  999. XSizeHints size = {
  1000. .flags = PSize | PResizeInc | PBaseSize,
  1001. .height = xw.h,
  1002. .width = xw.w,
  1003. .height_inc = xw.ch,
  1004. .width_inc = xw.cw,
  1005. .base_height = 2*BORDER,
  1006. .base_width = 2*BORDER,
  1007. };
  1008. XSetWMProperties(xw.dis, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
  1009. }
  1010. void
  1011. xinit(void) {
  1012. xw.dis = XOpenDisplay(NULL);
  1013. xw.scr = XDefaultScreen(xw.dis);
  1014. if(!xw.dis)
  1015. die("Can't open display\n");
  1016. /* font */
  1017. if(!(dc.font = XLoadQueryFont(xw.dis, FONT)) || !(dc.bfont = XLoadQueryFont(xw.dis, BOLDFONT)))
  1018. die("Can't load font %s\n", dc.font ? BOLDFONT : FONT);
  1019. /* XXX: Assuming same size for bold font */
  1020. xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
  1021. xw.ch = dc.font->ascent + dc.font->descent;
  1022. /* colors */
  1023. tloadcols();
  1024. term.c.attr.fg = DefaultFG;
  1025. term.c.attr.bg = DefaultBG;
  1026. term.c.attr.mode = ATTR_NULL;
  1027. /* windows */
  1028. xw.h = term.row * xw.ch + 2*BORDER;
  1029. xw.w = term.col * xw.cw + 2*BORDER;
  1030. xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
  1031. xw.w, xw.h, 0,
  1032. dc.col[DefaultBG],
  1033. dc.col[DefaultBG]);
  1034. xw.bufw = xw.w - 2*BORDER;
  1035. xw.bufh = xw.h - 2*BORDER;
  1036. xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
  1037. /* gc */
  1038. dc.gc = XCreateGC(xw.dis, xw.win, 0, NULL);
  1039. XMapWindow(xw.dis, xw.win);
  1040. xhints();
  1041. XStoreName(xw.dis, xw.win, "st");
  1042. XSync(xw.dis, 0);
  1043. }
  1044. void
  1045. xdraws(char *s, Glyph base, int x, int y, int len) {
  1046. unsigned long xfg, xbg;
  1047. int winx = x*xw.cw, winy = y*xw.ch + dc.font->ascent, width = len*xw.cw;
  1048. int i;
  1049. if(base.mode & ATTR_REVERSE)
  1050. xfg = dc.col[base.bg], xbg = dc.col[base.fg];
  1051. else
  1052. xfg = dc.col[base.fg], xbg = dc.col[base.bg];
  1053. XSetBackground(xw.dis, dc.gc, xbg);
  1054. XSetForeground(xw.dis, dc.gc, xfg);
  1055. if(base.mode & ATTR_GFX)
  1056. for(i = 0; i < len; i++)
  1057. s[i] = gfx[(int)s[i]];
  1058. XSetFont(xw.dis, dc.gc, base.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
  1059. XDrawImageString(xw.dis, xw.buf, dc.gc, winx, winy, s, len);
  1060. if(base.mode & ATTR_UNDERLINE)
  1061. XDrawLine(xw.dis, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
  1062. }
  1063. void
  1064. xcursor(int mode) {
  1065. static int oldx = 0;
  1066. static int oldy = 0;
  1067. Glyph g = {' ', ATTR_NULL, DefaultBG, DefaultCS, 0};
  1068. LIMIT(oldx, 0, term.col-1);
  1069. LIMIT(oldy, 0, term.row-1);
  1070. if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
  1071. g.c = term.line[term.c.y][term.c.x].c;
  1072. /* remove the old cursor */
  1073. if(term.line[oldy][oldx].state & GLYPH_SET)
  1074. xdraws(&term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1);
  1075. else
  1076. xclear(oldx, oldy, oldx, oldy);
  1077. /* draw the new one */
  1078. if(mode == CURSOR_DRAW) {
  1079. xdraws(&g.c, g, term.c.x, term.c.y, 1);
  1080. oldx = term.c.x, oldy = term.c.y;
  1081. }
  1082. }
  1083. #ifdef DEBUG
  1084. /* basic drawing routines */
  1085. void
  1086. xdrawc(int x, int y, Glyph g) {
  1087. XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
  1088. XSetBackground(xw.dis, dc.gc, dc.col[g.bg]);
  1089. XSetForeground(xw.dis, dc.gc, dc.col[g.fg]);
  1090. XSetFont(xw.dis, dc.gc, g.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
  1091. XDrawImageString(xw.dis, xw.buf, dc.gc, r.x, r.y+dc.font->ascent, &g.c, 1);
  1092. }
  1093. void
  1094. draw(int dummy) {
  1095. int x, y;
  1096. xclear(0, 0, term.col-1, term.row-1);
  1097. for(y = 0; y < term.row; y++)
  1098. for(x = 0; x < term.col; x++)
  1099. if(term.line[y][x].state & GLYPH_SET)
  1100. xdrawc(x, y, term.line[y][x]);
  1101. if(!term.hidec)
  1102. xcursor(CURSOR_DRAW);
  1103. XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
  1104. XFlush(xw.dis);
  1105. }
  1106. #else
  1107. /* optimized drawing routine */
  1108. void
  1109. draw(int redraw_all) {
  1110. int i, x, y, ox;
  1111. Glyph base, new;
  1112. char buf[DRAW_BUF_SIZ];
  1113. XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
  1114. XFillRectangle(xw.dis, xw.buf, dc.gc, 0, 0, xw.w, xw.h);
  1115. for(y = 0; y < term.row; y++) {
  1116. base = term.line[y][0];
  1117. i = ox = 0;
  1118. for(x = 0; x < term.col; x++) {
  1119. new = term.line[y][x];
  1120. if(!ATTRCMP(base, new) && i < DRAW_BUF_SIZ)
  1121. buf[i++] = new.c;
  1122. else {
  1123. xdraws(buf, base, ox, y, i);
  1124. buf[0] = new.c;
  1125. i = 1;
  1126. ox = x;
  1127. base = new;
  1128. }
  1129. }
  1130. xdraws(buf, base, ox, y, i);
  1131. }
  1132. xcursor(term.hidec ? CURSOR_HIDE : CURSOR_DRAW);
  1133. XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
  1134. XFlush(xw.dis);
  1135. }
  1136. #endif
  1137. void
  1138. expose(XEvent *ev) {
  1139. draw(SCREEN_REDRAW);
  1140. }
  1141. char*
  1142. kmap(KeySym k) {
  1143. int i;
  1144. for(i = 0; i < LEN(key); i++)
  1145. if(key[i].k == k)
  1146. return (char*)key[i].s;
  1147. return NULL;
  1148. }
  1149. void
  1150. kpress(XEvent *ev) {
  1151. XKeyEvent *e = &ev->xkey;
  1152. KeySym ksym;
  1153. char buf[32];
  1154. char *customkey;
  1155. int len;
  1156. int meta;
  1157. int shift;
  1158. meta = e->state & Mod1Mask;
  1159. shift = e->state & ShiftMask;
  1160. len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
  1161. if((customkey = kmap(ksym)))
  1162. ttywrite(customkey, strlen(customkey));
  1163. else if(len > 0) {
  1164. buf[sizeof(buf)-1] = '\0';
  1165. if(meta && len == 1)
  1166. ttywrite("\033", 1);
  1167. ttywrite(buf, len);
  1168. } else
  1169. switch(ksym) {
  1170. case XK_Up:
  1171. case XK_Down:
  1172. case XK_Left:
  1173. case XK_Right:
  1174. sprintf(buf, "\033%c%c", term.mode & MODE_APPKEYPAD ? 'O' : '[', "DACB"[ksym - XK_Left]);
  1175. ttywrite(buf, 3);
  1176. break;
  1177. case XK_Insert:
  1178. if(shift)
  1179. draw(1), puts("draw!")/* XXX: paste X clipboard */;
  1180. break;
  1181. default:
  1182. fprintf(stderr, "errkey: %d\n", (int)ksym);
  1183. break;
  1184. }
  1185. }
  1186. void
  1187. resize(XEvent *e) {
  1188. int col, row;
  1189. if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
  1190. return;
  1191. xw.w = e->xconfigure.width;
  1192. xw.h = e->xconfigure.height;
  1193. xw.bufw = xw.w - 2*BORDER;
  1194. xw.bufh = xw.h - 2*BORDER;
  1195. col = xw.bufw / xw.cw;
  1196. row = xw.bufh / xw.ch;
  1197. tresize(col, row);
  1198. ttyresize(col, row);
  1199. XFreePixmap(xw.dis, xw.buf);
  1200. xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
  1201. draw(SCREEN_REDRAW);
  1202. }
  1203. void
  1204. run(void) {
  1205. XEvent ev;
  1206. fd_set rfd;
  1207. int xfd = XConnectionNumber(xw.dis);
  1208. running = 1;
  1209. XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
  1210. XResizeWindow(xw.dis, xw.win, xw.w, xw.h); /* XXX: fix resize bug in wmii (?) */
  1211. while(running) {
  1212. FD_ZERO(&rfd);
  1213. FD_SET(cmdfd, &rfd);
  1214. FD_SET(xfd, &rfd);
  1215. if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) == -1) {
  1216. if(errno == EINTR)
  1217. continue;
  1218. die("select failed: %s\n", SERRNO);
  1219. }
  1220. if(FD_ISSET(cmdfd, &rfd)) {
  1221. ttyread();
  1222. draw(SCREEN_UPDATE);
  1223. }
  1224. while(XPending(xw.dis)) {
  1225. XNextEvent(xw.dis, &ev);
  1226. if(handler[ev.type])
  1227. (handler[ev.type])(&ev);
  1228. }
  1229. }
  1230. }
  1231. int
  1232. main(int argc, char *argv[]) {
  1233. if(argc == 2 && !strncmp("-v", argv[1], 3))
  1234. die("st-" VERSION ", © 2009 st engineers\n");
  1235. else if(argc != 1)
  1236. die("usage: st [-v]\n");
  1237. setlocale(LC_CTYPE, "");
  1238. tnew(80, 24);
  1239. ttynew();
  1240. xinit();
  1241. run();
  1242. return 0;
  1243. }