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 };
51 char c; /* character code */
52 char mode; /* attribute flags */
53 Color fg; /* foreground */
54 Color bg; /* background */
55 char state; /* state flag */
61 Glyph attr; /* current char attributes */
67 /* Escape sequence structs */
68 /* ESC <pre> [[ [<priv>] <arg> [;]] <mode>] */
70 char buf[ESCSIZ+1]; /* raw string */
71 int len; /* raw string length */
75 int narg; /* nb of args */
79 /* Internal representation of the screen */
83 Line* line; /* screen */
84 TCursor c; /* cursor */
85 int top; /* top scroll limit */
86 int bot; /* bottom scroll limit */
87 int mode; /* terminal mode */
90 /* Purely graphic info */
95 int w; /* window width */
96 int h; /* window height */
97 int ch; /* char height */
98 int cw; /* char width */
103 /* Drawing Context */
105 unsigned long col[LEN(colorname)];
110 void die(const char *errstr, ...);
115 void kpress(XKeyEvent *);
116 void resize(XEvent *);
122 void eschandle(void);
126 void tclearregion(int, int, int, int);
129 void tdeletechar(int);
130 void tdeleteline(int);
132 void tinsertblank(int);
133 void tinsertblankline(int);
134 void tmoveto(int, int);
138 void tputs(char*, int);
139 void tresize(int, int);
141 void tsetattr(int*, int);
143 void tsetscroll(int, int);
147 void ttyresize(int, int);
148 void ttywrite(char *, size_t);
150 unsigned long xgetcol(const char *);
151 void xclear(int, int, int, int);
153 void xdrawc(int, int, Glyph);
167 die(const char *errstr, ...) {
170 va_start(ap, errstr);
171 vfprintf(stderr, errstr, ap);
178 char *args[3] = {SHELL, "-i", NULL};
179 putenv("TERM=" TNAME);
184 xbell(void) { /* visual bell */
185 XRectangle r = { 0, 0, xw.w, xw.h };
186 XSetForeground(xw.dis, dc.gc, dc.col[BellCol]);
187 XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
195 if(waitpid(pid, &stat, 0) < 0)
196 die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
198 exit(WEXITSTATUS(stat));
208 if((m = posix_openpt(O_RDWR | O_NOCTTY)) < 0)
209 die("openpt failed: %s\n", SERRNO);
211 die("grandpt failed: %s\n", SERRNO);
213 die("unlockpt failed: %s\n", SERRNO);
214 if(!(pts = ptsname(m)))
215 die("ptsname failed: %s\n", SERRNO);
216 if((s = open(pts, O_RDWR | O_NOCTTY)) < 0)
217 die("Couldn't open slave: %s\n", SERRNO);
218 fcntl(s, F_SETFL, O_NDELAY);
219 switch(pid = fork()) {
221 die("fork failed\n");
224 setsid(); /* create a new process group */
225 dup2(s, STDIN_FILENO);
226 dup2(s, STDOUT_FILENO);
227 dup2(s, STDERR_FILENO);
228 if(ioctl(s, TIOCSCTTY, NULL) < 0)
229 die("ioctl TTIOCSTTY failed: %s\n", SERRNO);
235 signal(SIGCHLD, sigchld);
242 fprintf(stderr, " %02x %c ", c, isprint(c)?c:'.');
244 fprintf(stderr, "\n");
249 char buf[BUFSIZ] = {0};
252 switch(ret = read(cmdfd, buf, BUFSIZ)) {
254 die("Couldn't read from shell: %s\n", SERRNO);
262 ttywrite(char *s, size_t n) {
263 if(write(cmdfd, s, n) == -1)
264 die("write error on tty: %s\n", SERRNO);
268 ttyresize(int x, int y) {
273 w.ws_xpixel = w.ws_ypixel = 0;
274 if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
275 fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
291 else if(BETWEEN(c, 0x40, 0x7E))
302 x = term.c.x, y = term.c.y;
303 else if(mode == CSload)
308 tnew(int col, int row) { /* screen size */
309 term.row = row, term.col = col;
310 term.top = 0, term.bot = term.row - 1;
314 term.c.attr.mode = ATnone;
315 term.c.attr.fg = DefaultFG;
316 term.c.attr.bg = DefaultBG;
317 term.c.x = term.c.y = 0;
319 /* allocate screen */
320 term.line = calloc(term.row, sizeof(Line));
321 for(row = 0 ; row < term.row; row++)
322 term.line[row] = calloc(term.col, sizeof(Glyph));
327 Line temp = term.line[term.top];
329 /* X stuff _before_ the line swapping (results in wrong line index) */
331 for(i = term.top; i < term.bot; i++)
332 term.line[i] = term.line[i+1];
333 memset(temp, 0, sizeof(Glyph) * term.col);
334 term.line[term.bot] = temp;
339 int y = term.c.y + 1;
341 tscroll(), y = term.bot;
347 escseq.buf[escseq.len++] = c;
348 if(escfinal(c) || escseq.len >= ESCSIZ) {
349 escparse(), eschandle();
358 char *p = escseq.buf;
361 switch(escseq.pre = *p++) {
364 escseq.priv = 1, p++;
366 while(p < escseq.buf+escseq.len) {
368 escseq.arg[escseq.narg] *= 10;
369 escseq.arg[escseq.narg] += *(p++) - '0'/*, noarg = 0 */;
381 /* XXX: graphic character set */
387 tmoveto(int x, int y) {
388 term.c.x = x < 0 ? 0 : x >= term.col ? term.col-1 : x;
389 term.c.y = y < 0 ? 0 : y >= term.row ? term.row-1 : y;
394 int xf = term.c.x, yf = term.c.y;
406 xf = term.col-1, yf--;
408 yf = term.top, xf = 0;
416 yf = term.bot, tscroll();
425 term.line[term.c.y][term.c.x] = term.c.attr;
426 term.line[term.c.y][term.c.x].c = c;
427 term.line[term.c.y][term.c.x].state |= CRset | CRupdate;
431 tclearregion(int x1, int y1, int x2, int y2) {
434 LIMIT(x1, 0, term.col-1);
435 LIMIT(x2, 0, term.col-1);
436 LIMIT(y1, 0, term.row-1);
437 LIMIT(y2, 0, term.row-1);
439 /* XXX: could be optimized */
440 for(x = x1; x <= x2; x++)
441 for(y = y1; y <= y2; y++)
442 memset(&term.line[y][x], 0, sizeof(Glyph));
444 xclear(x1, y1, x2, y2);
449 int src = term.c.x + n;
451 int size = term.col - src;
453 if(src >= term.col) {
454 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
457 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
458 tclearregion(term.col-size, term.c.y, term.col-1, term.c.y);
462 tinsertblank(int n) {
465 int size = term.col - n - src;
467 if(dst >= term.col) {
468 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
471 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
472 tclearregion(src, term.c.y, dst, term.c.y);
476 tsetlinestate(int n, int state) {
478 for(i = 0; i < term.col; i++)
479 term.line[n][i].state |= state;
483 tinsertblankline (int n) {
488 if(term.c.y > term.bot)
490 else if(term.c.y < term.top)
492 if(term.c.y + n >= bot) {
493 tclearregion(0, term.c.y, term.col-1, bot);
496 for(i = bot; i >= term.c.y+n; i--) {
497 /* swap deleted line <-> blanked line */
498 blank = term.line[i];
499 term.line[i] = term.line[i-n];
500 term.line[i-n] = blank;
502 memset(blank, 0, term.col * sizeof(Glyph));
503 tsetlinestate(i, CRupdate);
504 tsetlinestate(i-n, CRupdate);
514 if(term.c.y > term.bot)
516 else if(term.c.y < term.top)
518 if(term.c.y + n >= bot) {
519 tclearregion(0, term.c.y, term.col-1, bot);
522 for(i = term.c.y; i <= bot-n; i++) {
523 /* swap deleted line <-> blanked line */
524 blank = term.line[i];
525 term.line[i] = term.line[i+n];
526 term.line[i+n] = blank;
528 memset(blank, 0, term.col * sizeof(Glyph));
529 tsetlinestate(i, CRupdate);
530 tsetlinestate(i-n, CRupdate);
535 tsetattr(int *attr, int l) {
538 for(i = 0; i < l; i++) {
541 memset(&term.c.attr, 0, sizeof(term.c.attr));
542 term.c.attr.fg = DefaultFG;
543 term.c.attr.bg = DefaultBG;
546 term.c.attr.mode |= ATbold;
549 term.c.attr.mode |= ATunderline;
552 term.c.attr.mode |= ATreverse;
555 term.c.hidden = CShide;
558 term.c.attr.mode &= ~ATbold;
561 term.c.attr.mode &= ~ATunderline;
564 term.c.attr.mode &= ~ATreverse;
567 term.c.attr.fg = DefaultFG;
570 term.c.attr.fg = DefaultBG;
573 if(BETWEEN(attr[i], 30, 37))
574 term.c.attr.fg = attr[i] - 30;
575 else if(BETWEEN(attr[i], 40, 47))
576 term.c.attr.bg = attr[i] - 40;
583 tsetscroll(int t, int b) {
586 LIMIT(t, 0, term.row-1);
587 LIMIT(b, 0, term.row-1);
603 switch(escseq.mode) {
606 fprintf(stderr, "erresc: unknown sequence\n");
609 case '@': /* Insert <n> blank char */
610 DEFAULT(escseq.arg[0], 1);
611 tinsertblank(escseq.arg[0]);
613 case 'A': /* Cursor <n> Up */
615 DEFAULT(escseq.arg[0], 1);
616 tmoveto(term.c.x, term.c.y-escseq.arg[0]);
618 case 'B': /* Cursor <n> Down */
619 DEFAULT(escseq.arg[0], 1);
620 tmoveto(term.c.x, term.c.y+escseq.arg[0]);
622 case 'C': /* Cursor <n> Forward */
624 DEFAULT(escseq.arg[0], 1);
625 tmoveto(term.c.x+escseq.arg[0], term.c.y);
627 case 'D': /* Cursor <n> Backward */
628 DEFAULT(escseq.arg[0], 1);
629 tmoveto(term.c.x-escseq.arg[0], term.c.y);
631 case 'E': /* Cursor <n> Down and first col */
632 DEFAULT(escseq.arg[0], 1);
633 tmoveto(0, term.c.y+escseq.arg[0]);
635 case 'F': /* Cursor <n> Up and first col */
636 DEFAULT(escseq.arg[0], 1);
637 tmoveto(0, term.c.y-escseq.arg[0]);
639 case 'G': /* Move to <col> */
641 DEFAULT(escseq.arg[0], 1);
642 tmoveto(escseq.arg[0]-1, term.c.y);
644 case 'H': /* Move to <row> <col> */
646 DEFAULT(escseq.arg[0], 1);
647 DEFAULT(escseq.arg[1], 1);
648 tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
650 case 'J': /* Clear screen */
651 switch(escseq.arg[0]) {
653 tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
656 tclearregion(0, 0, term.c.x, term.c.y);
659 tclearregion(0, 0, term.col-1, term.row-1);
663 case 'K': /* Clear line */
664 switch(escseq.arg[0]) {
666 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
669 tclearregion(0, term.c.y, term.c.x, term.c.y);
672 tclearregion(0, term.c.y, term.col-1, term.c.y);
676 case 'L': /* Insert <n> blank lines */
677 DEFAULT(escseq.arg[0], 1);
678 tinsertblankline(escseq.arg[0]);
681 if(escseq.priv && escseq.arg[0] == 25)
684 case 'M': /* Delete <n> lines */
685 DEFAULT(escseq.arg[0], 1);
686 tdeleteline(escseq.arg[0]);
688 case 'P': /* Delete <n> char */
689 DEFAULT(escseq.arg[0], 1);
690 tdeletechar(escseq.arg[0]);
692 case 'd': /* Move to <row> */
693 DEFAULT(escseq.arg[0], 1);
694 tmoveto(term.c.x, escseq.arg[0]-1);
696 case 'h': /* Set terminal mode */
697 if(escseq.priv && escseq.arg[0] == 25)
700 case 'm': /* Terminal attribute (color) */
701 tsetattr(escseq.arg, escseq.narg);
707 DEFAULT(escseq.arg[0], 1);
708 DEFAULT(escseq.arg[1], term.row);
709 tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
712 case 's': /* Save cursor position */
715 case 'u': /* Load cursor position */
726 printf("rawbuf : %s\n", escseq.buf);
727 printf("prechar : %c\n", escseq.pre);
728 printf("private : %c\n", escseq.priv ? '?' : ' ');
729 printf("narg : %d\n", escseq.narg);
731 for(i = 0; i < escseq.narg; i++)
732 printf("\targ %d = %d\n", i, escseq.arg[i]);
733 printf("mode : %c\n", escseq.mode);
738 memset(&escseq, 0, sizeof(escseq));
743 int space = TAB - term.c.x % TAB;
745 if(term.c.x + space >= term.col)
748 for(; space > 0; space--)
754 static int inesc = 0;
758 /* start of escseq */
760 escreset(), inesc = 1;
776 tmoveto(0, term.c.y);
788 tputs(char *s, int len) {
789 for(; len > 0; len--)
798 for(row = 0; row < term.row; row++) {
799 for(col = 0; col < term.col; col++) {
800 if(col == term.c.x && row == term.c.y)
803 c = term.line[row][col];
804 putchar(c.state & CRset ? c.c : '.');
812 tresize(int col, int row) {
815 int minrow = MIN(row, term.row);
816 int mincol = MIN(col, term.col);
818 if(col < 1 || row < 1)
821 line = calloc(row, sizeof(Line));
822 for(i = 0 ; i < row; i++)
823 line[i] = calloc(col, sizeof(Glyph));
825 for(i = 0 ; i < minrow; i++)
826 memcpy(line[i], term.line[i], mincol * sizeof(Glyph));
828 for(i = 0; i < term.row; i++)
832 LIMIT(term.c.x, 0, col-1);
833 LIMIT(term.c.y, 0, row-1);
834 LIMIT(term.top, 0, row-1);
835 LIMIT(term.bot, 0, row-1);
839 term.col = col, term.row = row;
843 xgetcol(const char *s) {
845 Colormap cmap = DefaultColormap(xw.dis, xw.scr);
847 if(!XAllocNamedColor(xw.dis, cmap, s, &color, &color)) {
848 color.pixel = WhitePixel(xw.dis, xw.scr);
849 fprintf(stderr, "Could not allocate color '%s'\n", s);
855 xclear(int x1, int y1, int x2, int y2) {
856 XClearArea(xw.dis, xw.win,
857 x1 * xw.cw, y1 * xw.ch,
858 (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch,
864 int srcy = (term.top+1) * xw.ch;
865 int dsty = term.top * xw.ch;
866 int height = (term.bot-term.top) * xw.ch;
869 XCopyArea(xw.dis, xw.win, xw.win, dc.gc, 0, srcy, xw.w, height, 0, dsty);
870 xclear(0, term.bot, term.col-1, term.bot);
876 unsigned long valuemask;
880 char *args[] = {NULL};
883 xw.dis = XOpenDisplay(NULL);
884 xw.scr = XDefaultScreen(xw.dis);
886 die("Can't open display\n");
889 if(!(dc.font = XLoadQueryFont(xw.dis, FONT)))
890 die("Can't load font %s\n", FONT);
892 xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
893 xw.ch = dc.font->ascent + dc.font->descent + LINESPACE;
896 for(i = 0; i < LEN(colorname); i++)
897 dc.col[i] = xgetcol(colorname[i]);
899 term.c.attr.fg = DefaultFG;
900 term.c.attr.bg = DefaultBG;
901 term.c.attr.mode = ATnone;
903 xw.h = term.row * xw.ch;
904 xw.w = term.col * xw.cw;
905 /* XXX: this BORDER is useless after the first resize, handle it in xdraws() */
906 xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
911 values.foreground = XWhitePixel(xw.dis, xw.scr);
912 values.font = dc.font->fid;
913 valuemask = GCForeground | GCFont;
914 dc.gc = XCreateGC(xw.dis, xw.win, valuemask, &values);
915 XMapWindow(xw.dis, xw.win);
917 chint.res_name = TNAME, chint.res_class = TNAME;
918 wmhint.input = 1, wmhint.flags = InputHint;
919 shint.height_inc = xw.ch, shint.width_inc = xw.cw;
920 shint.height = xw.h, shint.width = xw.w;
921 shint.flags = PSize | PResizeInc;
922 XSetWMProperties(xw.dis, xw.win, NULL, NULL, &args[0], 0, &shint, &wmhint, &chint);
923 XStoreName(xw.dis, xw.win, TNAME);
928 xdrawc(int x, int y, Glyph g) {
929 XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
930 unsigned long xfg, xbg;
933 if(g.mode & ATreverse)
934 xfg = dc.col[g.bg], xbg = dc.col[g.fg];
936 xfg = dc.col[g.fg], xbg = dc.col[g.bg];
938 XSetForeground(xw.dis, dc.gc, xbg);
939 XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
941 XSetForeground(xw.dis, dc.gc, xfg);
942 XDrawString(xw.dis, xw.win, dc.gc, r.x, r.y+dc.font->ascent, &(g.c), 1);
943 if(g.mode & ATbold) /* XXX: bold hack (draw again at x+1) */
944 XDrawString(xw.dis, xw.win, dc.gc, r.x+1, r.y+dc.font->ascent, &(g.c), 1);
946 if(g.mode & ATunderline) {
947 r.y += dc.font->ascent + 1;
948 XDrawLine(xw.dis, xw.win, dc.gc, r.x, r.y, r.x+r.width-1, r.y);
956 Glyph g = {' ', ATnone, DefaultBG, DefaultCS, 0};
958 LIMIT(oldx, 0, term.col-1);
959 LIMIT(oldy, 0, term.row-1);
961 if(term.line[term.c.y][term.c.x].state & CRset)
962 g.c = term.line[term.c.y][term.c.x].c;
963 /* remove the old cursor */
964 if(term.line[oldy][oldx].state & CRset)
965 xdrawc(oldx, oldy, term.line[oldy][oldx]);
967 xclear(oldx, oldy, oldx, oldy);
968 /* draw the new one */
970 xdrawc(term.c.x, term.c.y, g);
971 oldx = term.c.x, oldy = term.c.y;
976 draw(int redraw_all) {
981 XClearWindow(xw.dis, xw.win);
983 /* XXX: drawing could be optimised */
984 for(y = 0; y < term.row; y++) {
985 for(x = 0; x < term.col; x++) {
986 changed = term.line[y][x].state & CRupdate;
987 set = term.line[y][x].state & CRset;
988 if(redraw_all || changed) {
989 term.line[y][x].state &= ~CRupdate;
991 xdrawc(x, y, term.line[y][x]);
1003 for(i = 0; i < LEN(key); i++)
1005 return (char*)key[i].s;
1010 kpress(XKeyEvent *e) {
1018 meta = e->state & Mod1Mask;
1019 shift = e->state & ShiftMask;
1020 len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
1021 if(skmap = kmap(ksym))
1022 ttywrite(skmap, strlen(skmap));
1024 buf[sizeof(buf)-1] = '\0';
1025 if(meta && len == 1)
1026 ttywrite("\033", 1);
1032 /* XXX: paste X clipboard */;
1035 fprintf(stderr, "errkey: %d\n", (int)ksym);
1043 col = e->xconfigure.width / xw.cw;
1044 row = e->xconfigure.height / xw.ch;
1046 if(term.col != col || term.row != row) {
1048 ttyresize(col, row);
1049 xw.w = e->xconfigure.width;
1050 xw.h = e->xconfigure.height;
1060 int xfd = XConnectionNumber(xw.dis);
1063 XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
1064 XResizeWindow(xw.dis, xw.win, xw.w , xw.h); /* fix resize bug in wmii (?) */
1068 FD_SET(cmdfd, &rfd);
1071 ret = select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL);
1074 die("select failed: %s\n", SERRNO);
1076 if(FD_ISSET(xfd, &rfd)) {
1077 while(XPending(xw.dis)) {
1078 XNextEvent(xw.dis, &ev);
1088 case ConfigureNotify:
1094 if(FD_ISSET(cmdfd, &rfd)) {
1102 main(int argc, char *argv[]) {
1103 if(argc == 2 && !strncmp("-v", argv[1], 3))
1104 die("st-" VERSION ", © 2009 st engineers\n");
1106 die("usage: st [-v]\n");
1107 setlocale(LC_CTYPE, "");