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.

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