1 /* See LICENSE for licence details. */
12 #include <sys/ioctl.h>
13 #include <sys/select.h>
15 #include <sys/types.h>
19 #include <X11/keysym.h>
20 #include <X11/Xutil.h>
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)
36 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
37 enum { ATnone=0 , ATreverse=1 , ATunderline=2, ATbold=4 };
38 enum { CSup, CSdown, CSright, CSleft, CShide, CSdraw, CSwrap, CSsave, CSload };
39 enum { CRset=1, CRupdate=2 };
40 enum { TMwrap=1, TMinsert=2 };
41 enum { SCupdate, SCredraw };
48 char c; /* character code */
49 char mode; /* attribute flags */
50 Color fg; /* foreground */
51 Color bg; /* background */
52 char state; /* state flag */
58 Glyph attr; /* current char attributes */
64 /* Escape sequence structs */
65 /* ESC <pre> [[ [<priv>] <arg> [;]] <mode>] */
67 char buf[ESCSIZ+1]; /* raw string */
68 int len; /* raw string length */
72 int narg; /* nb of args */
76 /* Internal representation of the screen */
80 Line* line; /* screen */
81 TCursor c; /* cursor */
82 int top; /* top scroll limit */
83 int bot; /* bottom scroll limit */
84 int mode; /* terminal mode */
87 /* Purely graphic info */
92 int w; /* window width */
93 int h; /* window height */
94 int ch; /* char height */
95 int cw; /* char width */
100 unsigned long col[LEN(colorname)];
105 void die(const char *errstr, ...);
110 void kpress(XKeyEvent *);
111 void resize(XEvent *);
117 void eschandle(void);
121 void tclearregion(int, int, int, int);
124 void tdeletechar(int);
125 void tdeleteline(int);
127 void tinsertblank(int);
128 void tinsertblankline(int);
129 void tmoveto(int, int);
133 void tputs(char*, int);
134 void tresize(int, int);
136 void tsetattr(int*, int);
138 void tsetscroll(int, int);
142 void ttyresize(int, int);
143 void ttywrite(char *, size_t);
145 unsigned long xgetcol(const char *);
146 void xclear(int, int, int, int);
148 void xdrawc(int, int, Glyph);
162 die(const char *errstr, ...) {
165 va_start(ap, errstr);
166 vfprintf(stderr, errstr, ap);
173 char *args[3] = {SHELL, "-i", NULL};
174 putenv("TERM=" TNAME);
179 xbell(void) { /* visual bell */
180 XRectangle r = { 0, 0, xw.w, xw.h };
181 XSetForeground(xw.dis, dc.gc, dc.col[BellCol]);
182 XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
190 if(waitpid(pid, &stat, 0) < 0)
191 die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
193 exit(WEXITSTATUS(stat));
203 if((m = posix_openpt(O_RDWR | O_NOCTTY)) < 0)
204 die("openpt failed: %s\n", SERRNO);
206 die("grandpt failed: %s\n", SERRNO);
208 die("unlockpt failed: %s\n", SERRNO);
209 if(!(pts = ptsname(m)))
210 die("ptsname failed: %s\n", SERRNO);
211 if((s = open(pts, O_RDWR | O_NOCTTY)) < 0)
212 die("Couldn't open slave: %s\n", SERRNO);
213 fcntl(s, F_SETFL, O_NDELAY);
214 switch(pid = fork()) {
216 die("fork failed\n");
219 setsid(); /* create a new process group */
220 dup2(s, STDIN_FILENO);
221 dup2(s, STDOUT_FILENO);
222 dup2(s, STDERR_FILENO);
223 if(ioctl(s, TIOCSCTTY, NULL) < 0)
224 die("ioctl TTIOCSTTY failed: %s\n", SERRNO);
230 signal(SIGCHLD, sigchld);
237 fprintf(stderr, " %02x %c ", c, isprint(c)?c:'.');
239 fprintf(stderr, "\n");
244 char buf[BUFSIZ] = {0};
247 switch(ret = read(cmdfd, buf, BUFSIZ)) {
249 die("Couldn't read from shell: %s\n", SERRNO);
257 ttywrite(char *s, size_t n) {
258 if(write(cmdfd, s, n) == -1)
259 die("write error on tty: %s\n", SERRNO);
263 ttyresize(int x, int y) {
268 w.ws_xpixel = w.ws_ypixel = 0;
269 if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
270 fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
286 else if(BETWEEN(c, 0x40, 0x7E))
297 x = term.c.x, y = term.c.y;
298 else if(mode == CSload)
303 tnew(int col, int row) { /* screen size */
304 term.row = row, term.col = col;
305 term.top = 0, term.bot = term.row - 1;
309 term.c.attr.mode = ATnone;
310 term.c.attr.fg = DefaultFG;
311 term.c.attr.bg = DefaultBG;
312 term.c.x = term.c.y = 0;
314 /* allocate screen */
315 term.line = calloc(term.row, sizeof(Line));
316 for(row = 0 ; row < term.row; row++)
317 term.line[row] = calloc(term.col, sizeof(Glyph));
322 Line temp = term.line[term.top];
324 /* X stuff _before_ the line swapping (results in wrong line index) */
326 for(i = term.top; i < term.bot; i++)
327 term.line[i] = term.line[i+1];
328 memset(temp, 0, sizeof(Glyph) * term.col);
329 term.line[term.bot] = temp;
334 int y = term.c.y + 1;
336 tscroll(), y = term.bot;
342 escseq.buf[escseq.len++] = c;
343 if(escfinal(c) || escseq.len >= ESCSIZ) {
344 escparse(), eschandle();
353 char *p = escseq.buf;
356 switch(escseq.pre = *p++) {
359 escseq.priv = 1, p++;
361 while(p < escseq.buf+escseq.len) {
363 escseq.arg[escseq.narg] *= 10;
364 escseq.arg[escseq.narg] += *(p++) - '0'/*, noarg = 0 */;
376 /* XXX: graphic character set */
382 tmoveto(int x, int y) {
383 term.c.x = x < 0 ? 0 : x >= term.col ? term.col-1 : x;
384 term.c.y = y < 0 ? 0 : y >= term.row ? term.row-1 : y;
389 int xf = term.c.x, yf = term.c.y;
401 xf = term.col-1, yf--;
403 yf = term.top, xf = 0;
411 yf = term.bot, tscroll();
420 term.line[term.c.y][term.c.x] = term.c.attr;
421 term.line[term.c.y][term.c.x].c = c;
422 term.line[term.c.y][term.c.x].state |= CRset | CRupdate;
426 tclearregion(int x1, int y1, int x2, int y2) {
429 LIMIT(x1, 0, term.col-1);
430 LIMIT(x2, 0, term.col-1);
431 LIMIT(y1, 0, term.row-1);
432 LIMIT(y2, 0, term.row-1);
434 /* XXX: could be optimized */
435 for(x = x1; x <= x2; x++)
436 for(y = y1; y <= y2; y++)
437 memset(&term.line[y][x], 0, sizeof(Glyph));
439 xclear(x1, y1, x2, y2);
444 int src = term.c.x + n;
446 int size = term.col - src;
448 if(src >= term.col) {
449 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
452 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
453 tclearregion(term.col-size, term.c.y, term.col-1, term.c.y);
457 tinsertblank(int n) {
460 int size = term.col - n - src;
462 if(dst >= term.col) {
463 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
466 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
467 tclearregion(src, term.c.y, dst, term.c.y);
471 tsetlinestate(int n, int state) {
473 for(i = 0; i < term.col; i++)
474 term.line[n][i].state |= state;
478 tinsertblankline (int n) {
483 if(term.c.y > term.bot)
485 else if(term.c.y < term.top)
487 if(term.c.y + n >= bot) {
488 tclearregion(0, term.c.y, term.col-1, bot);
491 for(i = bot; i >= term.c.y+n; i--) {
492 /* swap deleted line <-> blanked line */
493 blank = term.line[i];
494 term.line[i] = term.line[i-n];
495 term.line[i-n] = blank;
497 memset(blank, 0, term.col * sizeof(Glyph));
498 tsetlinestate(i, CRupdate);
499 tsetlinestate(i-n, CRupdate);
509 if(term.c.y > term.bot)
511 else if(term.c.y < term.top)
513 if(term.c.y + n >= bot) {
514 tclearregion(0, term.c.y, term.col-1, bot);
517 for(i = term.c.y; i <= bot-n; i++) {
518 /* swap deleted line <-> blanked line */
519 blank = term.line[i];
520 term.line[i] = term.line[i+n];
521 term.line[i+n] = blank;
523 memset(blank, 0, term.col * sizeof(Glyph));
524 tsetlinestate(i, CRupdate);
525 tsetlinestate(i-n, CRupdate);
530 tsetattr(int *attr, int l) {
533 for(i = 0; i < l; i++) {
536 memset(&term.c.attr, 0, sizeof(term.c.attr));
537 term.c.attr.fg = DefaultFG;
538 term.c.attr.bg = DefaultBG;
541 term.c.attr.mode |= ATbold;
544 term.c.attr.mode |= ATunderline;
547 term.c.attr.mode |= ATreverse;
550 term.c.hidden = CShide;
553 term.c.attr.mode &= ~ATbold;
556 term.c.attr.mode &= ~ATunderline;
559 term.c.attr.mode &= ~ATreverse;
562 term.c.attr.fg = DefaultFG;
565 term.c.attr.fg = DefaultBG;
568 if(BETWEEN(attr[i], 30, 37))
569 term.c.attr.fg = attr[i] - 30;
570 else if(BETWEEN(attr[i], 40, 47))
571 term.c.attr.bg = attr[i] - 40;
578 tsetscroll(int t, int b) {
581 LIMIT(t, 0, term.row-1);
582 LIMIT(b, 0, term.row-1);
598 switch(escseq.mode) {
601 fprintf(stderr, "erresc: unknown sequence\n");
604 case '@': /* Insert <n> blank char */
605 DEFAULT(escseq.arg[0], 1);
606 tinsertblank(escseq.arg[0]);
608 case 'A': /* Cursor <n> Up */
610 DEFAULT(escseq.arg[0], 1);
611 tmoveto(term.c.x, term.c.y-escseq.arg[0]);
613 case 'B': /* Cursor <n> Down */
614 DEFAULT(escseq.arg[0], 1);
615 tmoveto(term.c.x, term.c.y+escseq.arg[0]);
617 case 'C': /* Cursor <n> Forward */
619 DEFAULT(escseq.arg[0], 1);
620 tmoveto(term.c.x+escseq.arg[0], term.c.y);
622 case 'D': /* Cursor <n> Backward */
623 DEFAULT(escseq.arg[0], 1);
624 tmoveto(term.c.x-escseq.arg[0], term.c.y);
626 case 'E': /* Cursor <n> Down and first col */
627 DEFAULT(escseq.arg[0], 1);
628 tmoveto(0, term.c.y+escseq.arg[0]);
630 case 'F': /* Cursor <n> Up and first col */
631 DEFAULT(escseq.arg[0], 1);
632 tmoveto(0, term.c.y-escseq.arg[0]);
634 case 'G': /* Move to <col> */
636 DEFAULT(escseq.arg[0], 1);
637 tmoveto(escseq.arg[0]-1, term.c.y);
639 case 'H': /* Move to <row> <col> */
641 DEFAULT(escseq.arg[0], 1);
642 DEFAULT(escseq.arg[1], 1);
643 tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
645 case 'J': /* Clear screen */
646 switch(escseq.arg[0]) {
648 tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
651 tclearregion(0, 0, term.c.x, term.c.y);
654 tclearregion(0, 0, term.col-1, term.row-1);
658 case 'K': /* Clear line */
659 switch(escseq.arg[0]) {
661 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
664 tclearregion(0, term.c.y, term.c.x, term.c.y);
667 tclearregion(0, term.c.y, term.col-1, term.c.y);
671 case 'L': /* Insert <n> blank lines */
672 DEFAULT(escseq.arg[0], 1);
673 tinsertblankline(escseq.arg[0]);
676 if(escseq.priv && escseq.arg[0] == 25)
679 case 'M': /* Delete <n> lines */
680 DEFAULT(escseq.arg[0], 1);
681 tdeleteline(escseq.arg[0]);
683 case 'P': /* Delete <n> char */
684 DEFAULT(escseq.arg[0], 1);
685 tdeletechar(escseq.arg[0]);
687 case 'd': /* Move to <row> */
688 DEFAULT(escseq.arg[0], 1);
689 tmoveto(term.c.x, escseq.arg[0]-1);
691 case 'h': /* Set terminal mode */
692 if(escseq.priv && escseq.arg[0] == 25)
695 case 'm': /* Terminal attribute (color) */
696 tsetattr(escseq.arg, escseq.narg);
702 DEFAULT(escseq.arg[0], 1);
703 DEFAULT(escseq.arg[1], term.row);
704 tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
707 case 's': /* Save cursor position */
710 case 'u': /* Load cursor position */
721 printf("rawbuf : %s\n", escseq.buf);
722 printf("prechar : %c\n", escseq.pre);
723 printf("private : %c\n", escseq.priv ? '?' : ' ');
724 printf("narg : %d\n", escseq.narg);
726 for(i = 0; i < escseq.narg; i++)
727 printf("\targ %d = %d\n", i, escseq.arg[i]);
728 printf("mode : %c\n", escseq.mode);
733 memset(&escseq, 0, sizeof(escseq));
738 int space = TAB - term.c.x % TAB;
740 if(term.c.x + space >= term.col)
743 for(; space > 0; space--)
749 static int inesc = 0;
753 /* start of escseq */
755 escreset(), inesc = 1;
771 tmoveto(0, term.c.y);
783 tputs(char *s, int len) {
784 for(; len > 0; len--)
793 for(row = 0; row < term.row; row++) {
794 for(col = 0; col < term.col; col++) {
795 if(col == term.c.x && row == term.c.y)
798 c = term.line[row][col];
799 putchar(c.state & CRset ? c.c : '.');
807 tresize(int col, int row) {
810 int minrow = MIN(row, term.row);
811 int mincol = MIN(col, term.col);
813 if(col < 1 || row < 1)
816 line = calloc(row, sizeof(Line));
817 for(i = 0 ; i < row; i++)
818 line[i] = calloc(col, sizeof(Glyph));
820 for(i = 0 ; i < minrow; i++)
821 memcpy(line[i], term.line[i], mincol * sizeof(Glyph));
823 for(i = 0; i < term.row; i++)
827 LIMIT(term.c.x, 0, col-1);
828 LIMIT(term.c.y, 0, row-1);
829 LIMIT(term.top, 0, row-1);
830 LIMIT(term.bot, 0, row-1);
834 term.col = col, term.row = row;
838 xgetcol(const char *s) {
840 Colormap cmap = DefaultColormap(xw.dis, xw.scr);
842 if(!XAllocNamedColor(xw.dis, cmap, s, &color, &color)) {
843 color.pixel = WhitePixel(xw.dis, xw.scr);
844 fprintf(stderr, "Could not allocate color '%s'\n", s);
850 xclear(int x1, int y1, int x2, int y2) {
851 XClearArea(xw.dis, xw.win,
852 x1 * xw.cw, y1 * xw.ch,
853 (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch,
859 int srcy = (term.top+1) * xw.ch;
860 int dsty = term.top * xw.ch;
861 int height = (term.bot-term.top) * xw.ch;
864 XCopyArea(xw.dis, xw.win, xw.win, dc.gc, 0, srcy, xw.w, height, 0, dsty);
865 xclear(0, term.bot, term.col-1, term.bot);
871 unsigned long valuemask;
875 char *args[] = {NULL};
878 xw.dis = XOpenDisplay(NULL);
879 xw.scr = XDefaultScreen(xw.dis);
881 die("Can't open display\n");
884 if(!(dc.font = XLoadQueryFont(xw.dis, FONT)))
885 die("Can't load font %s\n", FONT);
887 xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
888 xw.ch = dc.font->ascent + dc.font->descent + LINESPACE;
891 for(i = 0; i < LEN(colorname); i++)
892 dc.col[i] = xgetcol(colorname[i]);
894 term.c.attr.fg = DefaultFG;
895 term.c.attr.bg = DefaultBG;
896 term.c.attr.mode = ATnone;
898 xw.h = term.row * xw.ch;
899 xw.w = term.col * xw.cw;
900 /* XXX: this BORDER is useless after the first resize, handle it in xdraws() */
901 xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
906 values.foreground = XWhitePixel(xw.dis, xw.scr);
907 values.font = dc.font->fid;
908 valuemask = GCForeground | GCFont;
909 dc.gc = XCreateGC(xw.dis, xw.win, valuemask, &values);
910 XMapWindow(xw.dis, xw.win);
912 chint.res_name = TNAME, chint.res_class = TNAME;
913 wmhint.input = 1, wmhint.flags = InputHint;
914 shint.height_inc = xw.ch, shint.width_inc = xw.cw;
915 shint.height = xw.h, shint.width = xw.w;
916 shint.flags = PSize | PResizeInc;
917 XSetWMProperties(xw.dis, xw.win, NULL, NULL, &args[0], 0, &shint, &wmhint, &chint);
918 XStoreName(xw.dis, xw.win, TNAME);
923 xdrawc(int x, int y, Glyph g) {
924 XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
925 unsigned long xfg, xbg;
928 if(g.mode & ATreverse)
929 xfg = dc.col[g.bg], xbg = dc.col[g.fg];
931 xfg = dc.col[g.fg], xbg = dc.col[g.bg];
933 XSetForeground(xw.dis, dc.gc, xbg);
934 XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
936 XSetForeground(xw.dis, dc.gc, xfg);
937 XDrawString(xw.dis, xw.win, dc.gc, r.x, r.y+dc.font->ascent, &(g.c), 1);
938 if(g.mode & ATbold) /* XXX: bold hack (draw again at x+1) */
939 XDrawString(xw.dis, xw.win, dc.gc, r.x+1, r.y+dc.font->ascent, &(g.c), 1);
941 if(g.mode & ATunderline) {
942 r.y += dc.font->ascent + 1;
943 XDrawLine(xw.dis, xw.win, dc.gc, r.x, r.y, r.x+r.width-1, r.y);
951 Glyph g = {' ', ATnone, DefaultBG, DefaultCS, 0};
953 LIMIT(oldx, 0, term.col-1);
954 LIMIT(oldy, 0, term.row-1);
956 if(term.line[term.c.y][term.c.x].state & CRset)
957 g.c = term.line[term.c.y][term.c.x].c;
958 /* remove the old cursor */
959 if(term.line[oldy][oldx].state & CRset)
960 xdrawc(oldx, oldy, term.line[oldy][oldx]);
962 xclear(oldx, oldy, oldx, oldy);
963 /* draw the new one */
965 xdrawc(term.c.x, term.c.y, g);
966 oldx = term.c.x, oldy = term.c.y;
971 draw(int redraw_all) {
976 XClearWindow(xw.dis, xw.win);
978 /* XXX: drawing could be optimised */
979 for(y = 0; y < term.row; y++) {
980 for(x = 0; x < term.col; x++) {
981 changed = term.line[y][x].state & CRupdate;
982 set = term.line[y][x].state & CRset;
983 if(redraw_all || changed) {
984 term.line[y][x].state &= ~CRupdate;
986 xdrawc(x, y, term.line[y][x]);
996 kpress(XKeyEvent *e) {
1004 meta = e->state & Mod1Mask;
1005 shift = e->state & ShiftMask;
1006 len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
1007 if(skmap = key[ksym])
1008 ttywrite(skmap, strlen(skmap));
1010 buf[sizeof(buf)-1] = '\0';
1011 if(meta && len == 1)
1012 ttywrite("\033", 1);
1018 /* XXX: paste X clipboard */;
1021 fprintf(stderr, "errkey: %d\n", (int)ksym);
1029 col = e->xconfigure.width / xw.cw;
1030 row = e->xconfigure.height / xw.ch;
1032 if(term.col != col || term.row != row) {
1034 ttyresize(col, row);
1035 xw.w = e->xconfigure.width;
1036 xw.h = e->xconfigure.height;
1046 int xfd = XConnectionNumber(xw.dis);
1049 XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
1050 XResizeWindow(xw.dis, xw.win, xw.w , xw.h); /* fix resize bug in wmii (?) */
1054 FD_SET(cmdfd, &rfd);
1057 ret = select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL);
1060 die("select failed: %s\n", SERRNO);
1062 if(FD_ISSET(xfd, &rfd)) {
1063 while(XPending(xw.dis)) {
1064 XNextEvent(xw.dis, &ev);
1074 case ConfigureNotify:
1080 if(FD_ISSET(cmdfd, &rfd)) {
1088 main(int argc, char *argv[]) {
1089 if(argc == 2 && !strncmp("-v", argv[1], 3))
1090 die("st-" VERSION ", © 2009 st engineers\n");
1092 die("usage: st [-v]\n");
1093 setlocale(LC_CTYPE, "");