1 /* See LICENSE for licence details. */
2 /* See LICENSE for licence details. */
13 #include <sys/ioctl.h>
14 #include <sys/select.h>
16 #include <sys/types.h>
20 #include <X11/keysym.h>
21 #include <X11/Xutil.h>
29 #define SERRNO strerror(errno)
30 #define MIN(a, b) ((a) < (b) ? (a) : (b))
31 #define MAX(a, b) ((a) < (b) ? (b) : (a))
32 #define LEN(a) (sizeof(a) / sizeof(a[0]))
33 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
34 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
35 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
37 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
38 enum { ATnone=0 , ATreverse=1 , ATunderline=2, ATbold=4 };
39 enum { CSup, CSdown, CSright, CSleft, CShide, CSdraw, CSwrap, CSsave, CSload };
40 enum { CRset=1 , CRupdate=2 };
41 enum { TMwrap=1 , TMinsert=2, TMaltcharset };
42 enum { SCupdate, SCredraw };
52 char c; /* character code */
53 char mode; /* attribute flags */
54 Color fg; /* foreground */
55 Color bg; /* background */
56 char state; /* state flag */
62 Glyph attr; /* current char attributes */
68 /* Escape sequence structs */
69 /* ESC <pre> [[ [<priv>] <arg> [;]] <mode>] */
71 char buf[ESCSIZ+1]; /* raw string */
72 int len; /* raw string length */
76 int narg; /* nb of args */
80 /* Internal representation of the screen */
84 Line* line; /* screen */
85 TCursor c; /* cursor */
86 int top; /* top scroll limit */
87 int bot; /* bottom scroll limit */
88 int mode; /* terminal mode */
91 /* Purely graphic info */
96 int w; /* window width */
97 int h; /* window height */
98 int ch; /* char height */
99 int cw; /* char width */
104 /* Drawing Context */
106 unsigned long col[LEN(colorname)];
111 void die(const char *errstr, ...);
116 void kpress(XKeyEvent *);
117 void resize(XEvent *);
123 void eschandle(void);
127 void tclearregion(int, int, int, int);
130 void tdeletechar(int);
131 void tdeleteline(int);
133 void tinsertblank(int);
134 void tinsertblankline(int);
135 void tmoveto(int, int);
139 void tputs(char*, int);
140 void tresize(int, int);
142 void tsetattr(int*, int);
144 void tsetscroll(int, int);
148 void ttyresize(int, int);
149 void ttywrite(char *, size_t);
151 unsigned long xgetcol(const char *);
152 void xclear(int, int, int, int);
154 void xdrawc(int, int, Glyph);
169 die(const char *errstr, ...) {
172 va_start(ap, errstr);
173 vfprintf(stderr, errstr, ap);
180 char *args[3] = {SHELL, "-i", NULL};
181 putenv("TERM=" TNAME);
186 xbell(void) { /* visual bell */
187 XRectangle r = { 0, 0, xw.w, xw.h };
188 XSetForeground(xw.dis, dc.gc, dc.col[BellCol]);
189 XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
197 if(waitpid(pid, &stat, 0) < 0)
198 die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
200 exit(WEXITSTATUS(stat));
211 if((m = posix_openpt(O_RDWR | O_NOCTTY)) < 0)
212 die("openpt failed: %s\n", SERRNO);
214 die("grandpt failed: %s\n", SERRNO);
216 die("unlockpt failed: %s\n", SERRNO);
217 if(!(pts = ptsname(m)))
218 die("ptsname failed: %s\n", SERRNO);
219 if((s = open(pts, O_RDWR | O_NOCTTY)) < 0)
220 die("Couldn't open slave: %s\n", SERRNO);
221 fcntl(s, F_SETFL, O_NDELAY);
222 switch(pid = fork()) {
224 die("fork failed\n");
227 setsid(); /* create a new process group */
228 dup2(s, STDIN_FILENO);
229 dup2(s, STDOUT_FILENO);
230 dup2(s, STDERR_FILENO);
231 if(ioctl(s, TIOCSCTTY, NULL) < 0)
232 die("ioctl TTIOCSTTY failed: %s\n", SERRNO);
238 signal(SIGCHLD, sigchld);
245 fprintf(stderr, " %02x %c ", c, isprint(c)?c:'.');
247 fprintf(stderr, "\n");
252 char buf[BUFSIZ] = {0};
255 switch(ret = read(cmdfd, buf, BUFSIZ)) {
257 die("Couldn't read from shell: %s\n", SERRNO);
265 ttywrite(char *s, size_t n) {
266 if(write(cmdfd, s, n) == -1)
267 die("write error on tty: %s\n", SERRNO);
271 ttyresize(int x, int y) {
276 w.ws_xpixel = w.ws_ypixel = 0;
277 if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
278 fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
294 else if(BETWEEN(c, 0x40, 0x7E))
305 x = term.c.x, y = term.c.y;
306 else if(mode == CSload)
311 tnew(int col, int row) { /* screen size */
312 term.row = row, term.col = col;
313 term.top = 0, term.bot = term.row - 1;
317 term.c.attr.mode = ATnone;
318 term.c.attr.fg = DefaultFG;
319 term.c.attr.bg = DefaultBG;
320 term.c.x = term.c.y = 0;
322 /* allocate screen */
323 term.line = calloc(term.row, sizeof(Line));
324 for(row = 0 ; row < term.row; row++)
325 term.line[row] = calloc(term.col, sizeof(Glyph));
330 Line temp = term.line[term.top];
333 for(i = term.top; i < term.bot; i++)
334 term.line[i] = term.line[i+1];
335 memset(temp, 0, sizeof(Glyph) * term.col);
336 term.line[term.bot] = temp;
342 int y = term.c.y + 1;
345 tscroll(), y = term.bot;
352 escseq.buf[escseq.len++] = c;
353 if(escfinal(c) || escseq.len >= ESCSIZ) {
354 escparse(), eschandle();
363 char *p = escseq.buf;
366 switch(escseq.pre = *p++) {
369 escseq.priv = 1, p++;
371 while(p < escseq.buf+escseq.len) {
373 escseq.arg[escseq.narg] *= 10;
374 escseq.arg[escseq.narg] += *(p++) - '0'/*, noarg = 0 */;
386 /* XXX: graphic character set */
392 tmoveto(int x, int y) {
393 term.c.x = x < 0 ? 0 : x >= term.col ? term.col-1 : x;
394 term.c.y = y < 0 ? 0 : y >= term.row ? term.row-1 : y;
399 int xf = term.c.x, yf = term.c.y;
411 xf = term.col-1, yf--;
413 yf = term.top, xf = 0;
421 yf = term.bot, tscroll();
430 term.line[term.c.y][term.c.x] = term.c.attr;
431 term.line[term.c.y][term.c.x].c = c;
432 term.line[term.c.y][term.c.x].state |= CRset | CRupdate;
436 tclearregion(int x1, int y1, int x2, int y2) {
439 LIMIT(x1, 0, term.col-1);
440 LIMIT(x2, 0, term.col-1);
441 LIMIT(y1, 0, term.row-1);
442 LIMIT(y2, 0, term.row-1);
444 /* XXX: could be optimized */
445 for(x = x1; x <= x2; x++)
446 for(y = y1; y <= y2; y++)
447 memset(&term.line[y][x], 0, sizeof(Glyph));
449 xclear(x1, y1, x2, y2);
454 int src = term.c.x + n;
456 int size = term.col - src;
458 if(src >= term.col) {
459 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
462 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
463 tclearregion(term.col-size, term.c.y, term.col-1, term.c.y);
467 tinsertblank(int n) {
470 int size = term.col - n - src;
472 if(dst >= term.col) {
473 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
476 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
477 tclearregion(src, term.c.y, dst, term.c.y);
481 tinsertblankline (int n) {
486 if(term.c.y > term.bot)
488 else if(term.c.y < term.top)
490 if(term.c.y + n >= bot) {
491 tclearregion(0, term.c.y, term.col-1, bot);
494 for(i = bot; i >= term.c.y+n; i--) {
495 /* swap deleted line <-> blanked line */
496 blank = term.line[i];
497 term.line[i] = term.line[i-n];
498 term.line[i-n] = blank;
500 memset(blank, 0, term.col * sizeof(Glyph));
511 if(term.c.y > term.bot)
513 else if(term.c.y < term.top)
515 if(term.c.y + n >= bot) {
516 tclearregion(0, term.c.y, term.col-1, bot);
519 for(i = term.c.y; i <= bot-n; i++) {
520 /* swap deleted line <-> blanked line */
521 blank = term.line[i];
522 term.line[i] = term.line[i+n];
523 term.line[i+n] = blank;
525 memset(blank, 0, term.col * sizeof(Glyph));
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) {
599 case '@': /* Insert <n> blank char */
600 DEFAULT(escseq.arg[0], 1);
601 tinsertblank(escseq.arg[0]);
603 case 'A': /* Cursor <n> Up */
605 DEFAULT(escseq.arg[0], 1);
606 tmoveto(term.c.x, term.c.y-escseq.arg[0]);
608 case 'B': /* Cursor <n> Down */
609 DEFAULT(escseq.arg[0], 1);
610 tmoveto(term.c.x, term.c.y+escseq.arg[0]);
612 case 'C': /* Cursor <n> Forward */
614 DEFAULT(escseq.arg[0], 1);
615 tmoveto(term.c.x+escseq.arg[0], term.c.y);
617 case 'D': /* Cursor <n> Backward */
618 DEFAULT(escseq.arg[0], 1);
619 tmoveto(term.c.x-escseq.arg[0], term.c.y);
621 case 'E': /* Cursor <n> Down and first col */
622 DEFAULT(escseq.arg[0], 1);
623 tmoveto(0, term.c.y+escseq.arg[0]);
625 case 'F': /* Cursor <n> Up and first col */
626 DEFAULT(escseq.arg[0], 1);
627 tmoveto(0, term.c.y-escseq.arg[0]);
629 case 'G': /* Move to <col> */
631 DEFAULT(escseq.arg[0], 1);
632 tmoveto(escseq.arg[0]-1, term.c.y);
634 case 'H': /* Move to <row> <col> */
636 DEFAULT(escseq.arg[0], 1);
637 DEFAULT(escseq.arg[1], 1);
638 tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
640 case 'J': /* Clear screen */
641 switch(escseq.arg[0]) {
643 tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
646 tclearregion(0, 0, term.c.x, term.c.y);
649 tclearregion(0, 0, term.col-1, term.row-1);
653 case 'K': /* Clear line */
654 switch(escseq.arg[0]) {
656 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
659 tclearregion(0, term.c.y, term.c.x, term.c.y);
662 tclearregion(0, term.c.y, term.col-1, term.c.y);
666 case 'L': /* Insert <n> blank lines */
667 DEFAULT(escseq.arg[0], 1);
668 tinsertblankline(escseq.arg[0]);
671 if(escseq.priv && escseq.arg[0] == 25)
674 case 'M': /* Delete <n> lines */
675 DEFAULT(escseq.arg[0], 1);
676 tdeleteline(escseq.arg[0]);
678 case 'P': /* Delete <n> char */
679 DEFAULT(escseq.arg[0], 1);
680 tdeletechar(escseq.arg[0]);
682 case 'd': /* Move to <row> */
683 DEFAULT(escseq.arg[0], 1);
684 tmoveto(term.c.x, escseq.arg[0]-1);
686 case 'h': /* Set terminal mode */
687 if(escseq.priv && escseq.arg[0] == 25)
690 case 'm': /* Terminal attribute (color) */
691 tsetattr(escseq.arg, escseq.narg);
697 DEFAULT(escseq.arg[0], 1);
698 DEFAULT(escseq.arg[1], term.row);
699 tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
702 case 's': /* Save cursor position */
705 case 'u': /* Load cursor position */
717 printf("rawbuf : %s\n", escseq.buf);
718 printf("prechar : %c\n", escseq.pre);
719 printf("private : %c\n", escseq.priv ? '?' : ' ');
720 printf("narg : %d\n", escseq.narg);
722 for(i = 0; i < escseq.narg; i++)
723 printf("\targ %d = %d\n", i, escseq.arg[i]);
725 printf("mode : %c\n", escseq.mode);
730 memset(&escseq, 0, sizeof(escseq));
735 int space = TAB - term.c.x % TAB;
737 if(term.c.x + space >= term.col)
740 for(; space > 0; space--)
746 static int inesc = 0;
750 /* start of escseq */
752 escreset(), inesc = 1;
768 tmoveto(0, term.c.y);
780 tputs(char *s, int len) {
781 for(; len > 0; len--)
790 for(row = 0; row < term.row; row++) {
791 for(col = 0; col < term.col; col++) {
792 if(col == term.c.x && row == term.c.y)
795 c = term.line[row][col];
796 putchar(c.state & CRset ? c.c : '.');
804 tresize(int col, int row) {
807 int minrow = MIN(row, term.row);
808 int mincol = MIN(col, term.col);
810 if(col < 1 || row < 1)
813 line = calloc(row, sizeof(Line));
814 for(i = 0 ; i < row; i++)
815 line[i] = calloc(col, sizeof(Glyph));
817 for(i = 0 ; i < minrow; i++)
818 memcpy(line[i], term.line[i], mincol * sizeof(Glyph));
820 for(i = 0; i < term.row; i++)
824 LIMIT(term.c.x, 0, col-1);
825 LIMIT(term.c.y, 0, row-1);
826 LIMIT(term.top, 0, row-1);
827 LIMIT(term.bot, 0, row-1);
831 term.col = col, term.row = row;
835 xgetcol(const char *s) {
837 Colormap cmap = DefaultColormap(xw.dis, xw.scr);
839 if(!XAllocNamedColor(xw.dis, cmap, s, &color, &color)) {
840 color.pixel = WhitePixel(xw.dis, xw.scr);
841 fprintf(stderr, "Could not allocate color '%s'\n", s);
848 xclear(int x1, int y1, int x2, int y2) {
849 XClearArea(xw.dis, xw.win,
850 x1 * xw.cw, y1 * xw.ch,
851 (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch,
858 int srcy = (term.top+1) * xw.ch;
859 int dsty = term.top * xw.ch;
860 int height = (term.bot-term.top) * xw.ch;
863 XCopyArea(xw.dis, xw.win, xw.win, dc.gc, 0, srcy, xw.w, height, 0, dsty);
864 xclear(0, term.bot, term.col-1, term.bot);
873 unsigned long valuemask;
877 char *args[] = {NULL};
880 xw.dis = XOpenDisplay(NULL);
881 xw.scr = XDefaultScreen(xw.dis);
883 die("Can't open display\n");
886 if(!(dc.font = XLoadQueryFont(xw.dis, FONT)))
887 die("Can't load font %s\n", FONT);
889 xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
890 xw.ch = dc.font->ascent + dc.font->descent + LINESPACE;
893 for(i = 0; i < LEN(colorname); i++)
894 dc.col[i] = xgetcol(colorname[i]);
896 term.c.attr.fg = DefaultFG;
897 term.c.attr.bg = DefaultBG;
898 term.c.attr.mode = ATnone;
900 xw.h = term.row * xw.ch;
901 xw.w = term.col * xw.cw;
902 /* XXX: this BORDER is useless after the first resize, handle it in xdraws() */
903 xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
908 values.foreground = XWhitePixel(xw.dis, xw.scr);
909 values.font = dc.font->fid;
910 valuemask = GCForeground | GCFont;
911 dc.gc = XCreateGC(xw.dis, xw.win, valuemask, &values);
912 XMapWindow(xw.dis, xw.win);
914 chint.res_name = TNAME, chint.res_class = TNAME;
915 wmhint.input = 1, wmhint.flags = InputHint;
916 shint.height_inc = xw.ch, shint.width_inc = xw.cw;
917 shint.height = xw.h, shint.width = xw.w;
918 shint.flags = PSize | PResizeInc;
919 XSetWMProperties(xw.dis, xw.win, NULL, NULL, &args[0], 0, &shint, &wmhint, &chint);
920 XStoreName(xw.dis, xw.win, TNAME);
925 xdrawc(int x, int y, Glyph g) {
926 XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
927 unsigned long xfg, xbg;
930 if(g.mode & ATreverse)
931 xfg = dc.col[g.bg], xbg = dc.col[g.fg];
933 xfg = dc.col[g.fg], xbg = dc.col[g.bg];
935 XSetForeground(xw.dis, dc.gc, xbg);
936 XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
938 XSetForeground(xw.dis, dc.gc, xfg);
939 XDrawString(xw.dis, xw.win, dc.gc, r.x, r.y+dc.font->ascent, &(g.c), 1);
940 if(g.mode & ATbold) /* XXX: bold hack (draw again at x+1) */
941 XDrawString(xw.dis, xw.win, dc.gc, r.x+1, r.y+dc.font->ascent, &(g.c), 1);
943 if(g.mode & ATunderline) {
944 r.y += dc.font->ascent + 1;
945 XDrawLine(xw.dis, xw.win, dc.gc, r.x, r.y, r.x+r.width-1, r.y);
953 Glyph g = {' ', ATnone, DefaultBG, DefaultCS, 0};
955 LIMIT(oldx, 0, term.col-1);
956 LIMIT(oldy, 0, term.row-1);
958 if(term.line[term.c.y][term.c.x].state & CRset)
959 g.c = term.line[term.c.y][term.c.x].c;
960 /* remove the old cursor */
961 if(term.line[oldy][oldx].state & CRset)
962 xdrawc(oldx, oldy, term.line[oldy][oldx]);
963 else xclear(oldx, oldy, oldx, oldy); /* XXX: maybe a bug */
964 if(mode == CSdraw && !term.c.hidden) {
965 xdrawc(term.c.x, term.c.y, g);
966 oldx = term.c.x, oldy = term.c.y;
972 draw(int redraw_all) {
977 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((changed && set) || (redraw_all && set)) {
984 term.line[y][x].state &= ~CRupdate;
985 xdrawc(x, y, term.line[y][x]);
995 for(i = 0; i < LEN(key); i++)
997 return (char*)key[i].s;
1002 kpress(XKeyEvent *e) {
1010 meta = e->state & Mod1Mask;
1011 shift = e->state & ShiftMask;
1012 len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
1013 if(skmap = kmap(ksym))
1014 ttywrite(skmap, strlen(skmap));
1016 buf[sizeof(buf)-1] = '\0';
1017 if(meta && len == 1)
1018 ttywrite("\033", 1);
1024 /* XXX: paste X clipboard */;
1027 fprintf(stderr, "errkey: %d\n", (int)ksym);
1035 col = e->xconfigure.width / xw.cw;
1036 row = e->xconfigure.height / xw.ch;
1038 if(term.col != col || term.row != row) {
1040 ttyresize(col, row);
1041 xw.w = e->xconfigure.width;
1042 xw.h = e->xconfigure.height;
1052 int xfd = XConnectionNumber(xw.dis);
1055 XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
1056 XResizeWindow(xw.dis, xw.win, xw.w , xw.h); /* fix resize bug in wmii (?) */
1060 FD_SET(cmdfd, &rfd);
1063 ret = select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL);
1066 die("select failed: %s\n", SERRNO);
1068 if(FD_ISSET(xfd, &rfd)) {
1069 while(XPending(xw.dis)) {
1070 XNextEvent(xw.dis, &ev);
1080 case ConfigureNotify:
1086 if(FD_ISSET(cmdfd, &rfd)) {
1094 main(int argc, char *argv[]) {
1095 if(argc == 2 && !strncmp("-v", argv[1], 3))
1096 die("st-" VERSION ", © 2009 st engineers\n");
1098 die("usage: st [-v]\n");
1099 setlocale(LC_CTYPE, "");