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);
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));
210 if((m = posix_openpt(O_RDWR | O_NOCTTY)) < 0)
211 die("openpt failed: %s\n", SERRNO);
213 die("grandpt failed: %s\n", SERRNO);
215 die("unlockpt failed: %s\n", SERRNO);
216 if(!(pts = ptsname(m)))
217 die("ptsname failed: %s\n", SERRNO);
218 if((s = open(pts, O_RDWR | O_NOCTTY)) < 0)
219 die("Couldn't open slave: %s\n", SERRNO);
220 fcntl(s, F_SETFL, O_NDELAY);
221 switch(pid = fork()) {
223 die("fork failed\n");
226 setsid(); /* create a new process group */
227 dup2(s, STDIN_FILENO);
228 dup2(s, STDOUT_FILENO);
229 dup2(s, STDERR_FILENO);
230 if(ioctl(s, TIOCSCTTY, NULL) < 0)
231 die("ioctl TTIOCSTTY failed: %s\n", SERRNO);
237 signal(SIGCHLD, sigchld);
244 fprintf(stderr, " %02x %c ", c, isprint(c)?c:'.');
246 fprintf(stderr, "\n");
251 char buf[BUFSIZ] = {0};
254 switch(ret = read(cmdfd, buf, BUFSIZ)) {
256 die("Couldn't read from shell: %s\n", SERRNO);
264 ttywrite(char *s, size_t n) {
265 if(write(cmdfd, s, n) == -1)
266 die("write error on tty: %s\n", SERRNO);
270 ttyresize(int x, int y) {
275 w.ws_xpixel = w.ws_ypixel = 0;
276 if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
277 fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
293 else if(BETWEEN(c, 0x40, 0x7E))
304 x = term.c.x, y = term.c.y;
305 else if(mode == CSload)
310 tnew(int col, int row) { /* screen size */
311 term.row = row, term.col = col;
312 term.top = 0, term.bot = term.row - 1;
316 term.c.attr.mode = ATnone;
317 term.c.attr.fg = DefaultFG;
318 term.c.attr.bg = DefaultBG;
319 term.c.x = term.c.y = 0;
321 /* allocate screen */
322 term.line = calloc(term.row, sizeof(Line));
323 for(row = 0 ; row < term.row; row++)
324 term.line[row] = calloc(term.col, sizeof(Glyph));
329 Line temp = term.line[term.top];
331 /* X stuff _before_ the line swapping (results in wrong line index) */
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;
341 int y = term.c.y + 1;
343 tscroll(), y = term.bot;
349 escseq.buf[escseq.len++] = c;
350 if(escfinal(c) || escseq.len >= ESCSIZ) {
351 escparse(), eschandle();
360 char *p = escseq.buf;
363 switch(escseq.pre = *p++) {
366 escseq.priv = 1, p++;
368 while(p < escseq.buf+escseq.len) {
370 escseq.arg[escseq.narg] *= 10;
371 escseq.arg[escseq.narg] += *(p++) - '0'/*, noarg = 0 */;
383 /* XXX: graphic character set */
389 tmoveto(int x, int y) {
390 term.c.x = x < 0 ? 0 : x >= term.col ? term.col-1 : x;
391 term.c.y = y < 0 ? 0 : y >= term.row ? term.row-1 : y;
396 int xf = term.c.x, yf = term.c.y;
408 xf = term.col-1, yf--;
410 yf = term.top, xf = 0;
418 yf = term.bot, tscroll();
427 term.line[term.c.y][term.c.x] = term.c.attr;
428 term.line[term.c.y][term.c.x].c = c;
429 term.line[term.c.y][term.c.x].state |= CRset | CRupdate;
433 tclearregion(int x1, int y1, int x2, int y2) {
436 LIMIT(x1, 0, term.col-1);
437 LIMIT(x2, 0, term.col-1);
438 LIMIT(y1, 0, term.row-1);
439 LIMIT(y2, 0, term.row-1);
441 /* XXX: could be optimized */
442 for(x = x1; x <= x2; x++)
443 for(y = y1; y <= y2; y++)
444 memset(&term.line[y][x], 0, sizeof(Glyph));
446 xclear(x1, y1, x2, y2);
451 int src = term.c.x + n;
453 int size = term.col - src;
455 if(src >= term.col) {
456 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
459 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
460 tclearregion(term.col-size, term.c.y, term.col-1, term.c.y);
464 tinsertblank(int n) {
467 int size = term.col - n - src;
469 if(dst >= term.col) {
470 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
473 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
474 tclearregion(src, term.c.y, dst, term.c.y);
478 tsetlinestate(int n, int state) {
480 for(i = 0; i < term.col; i++)
481 term.line[n][i].state |= state;
485 tinsertblankline (int n) {
490 if(term.c.y > term.bot)
492 else if(term.c.y < term.top)
494 if(term.c.y + n >= bot) {
495 tclearregion(0, term.c.y, term.col-1, bot);
498 for(i = bot; i >= term.c.y+n; i--) {
499 /* swap deleted line <-> blanked line */
500 blank = term.line[i];
501 term.line[i] = term.line[i-n];
502 term.line[i-n] = blank;
504 memset(blank, 0, term.col * sizeof(Glyph));
505 tsetlinestate(i, CRupdate);
506 tsetlinestate(i-n, CRupdate);
516 if(term.c.y > term.bot)
518 else if(term.c.y < term.top)
520 if(term.c.y + n >= bot) {
521 tclearregion(0, term.c.y, term.col-1, bot);
524 for(i = term.c.y; i <= bot-n; i++) {
525 /* swap deleted line <-> blanked line */
526 blank = term.line[i];
527 term.line[i] = term.line[i+n];
528 term.line[i+n] = blank;
530 memset(blank, 0, term.col * sizeof(Glyph));
531 tsetlinestate(i, CRupdate);
532 tsetlinestate(i-n, CRupdate);
537 tsetattr(int *attr, int l) {
540 for(i = 0; i < l; i++) {
543 memset(&term.c.attr, 0, sizeof(term.c.attr));
544 term.c.attr.fg = DefaultFG;
545 term.c.attr.bg = DefaultBG;
548 term.c.attr.mode |= ATbold;
551 term.c.attr.mode |= ATunderline;
554 term.c.attr.mode |= ATreverse;
557 term.c.hidden = CShide;
560 term.c.attr.mode &= ~ATbold;
563 term.c.attr.mode &= ~ATunderline;
566 term.c.attr.mode &= ~ATreverse;
569 term.c.attr.fg = DefaultFG;
572 term.c.attr.fg = DefaultBG;
575 if(BETWEEN(attr[i], 30, 37))
576 term.c.attr.fg = attr[i] - 30;
577 else if(BETWEEN(attr[i], 40, 47))
578 term.c.attr.bg = attr[i] - 40;
585 tsetscroll(int t, int b) {
588 LIMIT(t, 0, term.row-1);
589 LIMIT(b, 0, term.row-1);
605 switch(escseq.mode) {
608 fprintf(stderr, "erresc: unknown sequence\n");
611 case '@': /* Insert <n> blank char */
612 DEFAULT(escseq.arg[0], 1);
613 tinsertblank(escseq.arg[0]);
615 case 'A': /* Cursor <n> Up */
617 DEFAULT(escseq.arg[0], 1);
618 tmoveto(term.c.x, term.c.y-escseq.arg[0]);
620 case 'B': /* Cursor <n> Down */
621 DEFAULT(escseq.arg[0], 1);
622 tmoveto(term.c.x, term.c.y+escseq.arg[0]);
624 case 'C': /* Cursor <n> Forward */
626 DEFAULT(escseq.arg[0], 1);
627 tmoveto(term.c.x+escseq.arg[0], term.c.y);
629 case 'D': /* Cursor <n> Backward */
630 DEFAULT(escseq.arg[0], 1);
631 tmoveto(term.c.x-escseq.arg[0], term.c.y);
633 case 'E': /* Cursor <n> Down and first col */
634 DEFAULT(escseq.arg[0], 1);
635 tmoveto(0, term.c.y+escseq.arg[0]);
637 case 'F': /* Cursor <n> Up and first col */
638 DEFAULT(escseq.arg[0], 1);
639 tmoveto(0, term.c.y-escseq.arg[0]);
641 case 'G': /* Move to <col> */
643 DEFAULT(escseq.arg[0], 1);
644 tmoveto(escseq.arg[0]-1, term.c.y);
646 case 'H': /* Move to <row> <col> */
648 DEFAULT(escseq.arg[0], 1);
649 DEFAULT(escseq.arg[1], 1);
650 tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
652 case 'J': /* Clear screen */
653 switch(escseq.arg[0]) {
655 tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
658 tclearregion(0, 0, term.c.x, term.c.y);
661 tclearregion(0, 0, term.col-1, term.row-1);
665 case 'K': /* Clear line */
666 switch(escseq.arg[0]) {
668 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
671 tclearregion(0, term.c.y, term.c.x, term.c.y);
674 tclearregion(0, term.c.y, term.col-1, term.c.y);
678 case 'L': /* Insert <n> blank lines */
679 DEFAULT(escseq.arg[0], 1);
680 tinsertblankline(escseq.arg[0]);
683 if(escseq.priv && escseq.arg[0] == 25)
686 case 'M': /* Delete <n> lines */
687 DEFAULT(escseq.arg[0], 1);
688 tdeleteline(escseq.arg[0]);
690 case 'P': /* Delete <n> char */
691 DEFAULT(escseq.arg[0], 1);
692 tdeletechar(escseq.arg[0]);
694 case 'd': /* Move to <row> */
695 DEFAULT(escseq.arg[0], 1);
696 tmoveto(term.c.x, escseq.arg[0]-1);
698 case 'h': /* Set terminal mode */
699 if(escseq.priv && escseq.arg[0] == 25)
702 case 'm': /* Terminal attribute (color) */
703 tsetattr(escseq.arg, escseq.narg);
709 DEFAULT(escseq.arg[0], 1);
710 DEFAULT(escseq.arg[1], term.row);
711 tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
714 case 's': /* Save cursor position */
717 case 'u': /* Load cursor position */
728 printf("rawbuf : %s\n", escseq.buf);
729 printf("prechar : %c\n", escseq.pre);
730 printf("private : %c\n", escseq.priv ? '?' : ' ');
731 printf("narg : %d\n", escseq.narg);
733 for(i = 0; i < escseq.narg; i++)
734 printf("\targ %d = %d\n", i, escseq.arg[i]);
735 printf("mode : %c\n", escseq.mode);
740 memset(&escseq, 0, sizeof(escseq));
745 int space = TAB - term.c.x % TAB;
747 if(term.c.x + space >= term.col)
750 for(; space > 0; space--)
756 static int inesc = 0;
760 /* start of escseq */
762 escreset(), inesc = 1;
778 tmoveto(0, term.c.y);
790 tputs(char *s, int len) {
791 for(; len > 0; len--)
800 for(row = 0; row < term.row; row++) {
801 for(col = 0; col < term.col; col++) {
802 if(col == term.c.x && row == term.c.y)
805 c = term.line[row][col];
806 putchar(c.state & CRset ? c.c : '.');
814 tresize(int col, int row) {
817 int minrow = MIN(row, term.row);
818 int mincol = MIN(col, term.col);
820 if(col < 1 || row < 1)
823 line = calloc(row, sizeof(Line));
824 for(i = 0 ; i < row; i++)
825 line[i] = calloc(col, sizeof(Glyph));
827 for(i = 0 ; i < minrow; i++)
828 memcpy(line[i], term.line[i], mincol * sizeof(Glyph));
830 for(i = 0; i < term.row; i++)
834 LIMIT(term.c.x, 0, col-1);
835 LIMIT(term.c.y, 0, row-1);
836 LIMIT(term.top, 0, row-1);
837 LIMIT(term.bot, 0, row-1);
841 term.col = col, term.row = row;
845 xgetcol(const char *s) {
847 Colormap cmap = DefaultColormap(xw.dis, xw.scr);
849 if(!XAllocNamedColor(xw.dis, cmap, s, &color, &color)) {
850 color.pixel = WhitePixel(xw.dis, xw.scr);
851 fprintf(stderr, "Could not allocate color '%s'\n", s);
857 xclear(int x1, int y1, int x2, int y2) {
858 XClearArea(xw.dis, xw.win,
859 x1 * xw.cw, y1 * xw.ch,
860 (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch,
866 int srcy = (term.top+1) * xw.ch;
867 int dsty = term.top * xw.ch;
868 int height = (term.bot-term.top) * xw.ch;
871 XCopyArea(xw.dis, xw.win, xw.win, dc.gc, 0, srcy, xw.w, height, 0, dsty);
872 xclear(0, term.bot, term.col-1, term.bot);
878 unsigned long valuemask;
882 char *args[] = {NULL};
885 xw.dis = XOpenDisplay(NULL);
886 xw.scr = XDefaultScreen(xw.dis);
888 die("Can't open display\n");
891 if(!(dc.font = XLoadQueryFont(xw.dis, FONT)))
892 die("Can't load font %s\n", FONT);
894 xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
895 xw.ch = dc.font->ascent + dc.font->descent + LINESPACE;
898 for(i = 0; i < LEN(colorname); i++)
899 dc.col[i] = xgetcol(colorname[i]);
901 term.c.attr.fg = DefaultFG;
902 term.c.attr.bg = DefaultBG;
903 term.c.attr.mode = ATnone;
905 xw.h = term.row * xw.ch;
906 xw.w = term.col * xw.cw;
907 /* XXX: this BORDER is useless after the first resize, handle it in xdraws() */
908 xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
913 values.foreground = XWhitePixel(xw.dis, xw.scr);
914 values.font = dc.font->fid;
915 valuemask = GCForeground | GCFont;
916 dc.gc = XCreateGC(xw.dis, xw.win, valuemask, &values);
917 XMapWindow(xw.dis, xw.win);
919 chint.res_name = TNAME, chint.res_class = TNAME;
920 wmhint.input = 1, wmhint.flags = InputHint;
921 shint.height_inc = xw.ch, shint.width_inc = xw.cw;
922 shint.height = xw.h, shint.width = xw.w;
923 shint.flags = PSize | PResizeInc;
924 XSetWMProperties(xw.dis, xw.win, NULL, NULL, &args[0], 0, &shint, &wmhint, &chint);
925 XStoreName(xw.dis, xw.win, TNAME);
930 xdrawc(int x, int y, Glyph g) {
931 XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
932 unsigned long xfg, xbg;
935 if(g.mode & ATreverse)
936 xfg = dc.col[g.bg], xbg = dc.col[g.fg];
938 xfg = dc.col[g.fg], xbg = dc.col[g.bg];
940 XSetForeground(xw.dis, dc.gc, xbg);
941 XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
943 XSetForeground(xw.dis, dc.gc, xfg);
944 XDrawString(xw.dis, xw.win, dc.gc, r.x, r.y+dc.font->ascent, &(g.c), 1);
945 if(g.mode & ATbold) /* XXX: bold hack (draw again at x+1) */
946 XDrawString(xw.dis, xw.win, dc.gc, r.x+1, r.y+dc.font->ascent, &(g.c), 1);
948 if(g.mode & ATunderline) {
949 r.y += dc.font->ascent + 1;
950 XDrawLine(xw.dis, xw.win, dc.gc, r.x, r.y, r.x+r.width-1, r.y);
958 Glyph g = {' ', ATnone, DefaultBG, DefaultCS, 0};
960 LIMIT(oldx, 0, term.col-1);
961 LIMIT(oldy, 0, term.row-1);
963 if(term.line[term.c.y][term.c.x].state & CRset)
964 g.c = term.line[term.c.y][term.c.x].c;
965 /* remove the old cursor */
966 if(term.line[oldy][oldx].state & CRset)
967 xdrawc(oldx, oldy, term.line[oldy][oldx]);
969 xclear(oldx, oldy, oldx, oldy);
970 /* draw the new one */
972 xdrawc(term.c.x, term.c.y, g);
973 oldx = term.c.x, oldy = term.c.y;
978 draw(int redraw_all) {
983 XClearWindow(xw.dis, xw.win);
985 /* XXX: drawing could be optimised */
986 for(y = 0; y < term.row; y++) {
987 for(x = 0; x < term.col; x++) {
988 changed = term.line[y][x].state & CRupdate;
989 set = term.line[y][x].state & CRset;
990 if(redraw_all || changed) {
991 term.line[y][x].state &= ~CRupdate;
993 xdrawc(x, y, term.line[y][x]);
1005 for(i = 0; i < LEN(key); i++)
1007 return (char*)key[i].s;
1012 kpress(XKeyEvent *e) {
1020 meta = e->state & Mod1Mask;
1021 shift = e->state & ShiftMask;
1022 len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
1023 if(skmap = kmap(ksym))
1024 ttywrite(skmap, strlen(skmap));
1026 buf[sizeof(buf)-1] = '\0';
1027 if(meta && len == 1)
1028 ttywrite("\033", 1);
1034 /* XXX: paste X clipboard */;
1037 fprintf(stderr, "errkey: %d\n", (int)ksym);
1045 col = e->xconfigure.width / xw.cw;
1046 row = e->xconfigure.height / xw.ch;
1048 if(term.col != col || term.row != row) {
1050 ttyresize(col, row);
1051 xw.w = e->xconfigure.width;
1052 xw.h = e->xconfigure.height;
1062 int xfd = XConnectionNumber(xw.dis);
1065 XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
1066 XResizeWindow(xw.dis, xw.win, xw.w , xw.h); /* fix resize bug in wmii (?) */
1070 FD_SET(cmdfd, &rfd);
1073 ret = select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL);
1076 die("select failed: %s\n", SERRNO);
1078 if(FD_ISSET(xfd, &rfd)) {
1079 while(XPending(xw.dis)) {
1080 XNextEvent(xw.dis, &ev);
1090 case ConfigureNotify:
1096 if(FD_ISSET(cmdfd, &rfd)) {
1104 main(int argc, char *argv[]) {
1105 if(argc == 2 && !strncmp("-v", argv[1], 3))
1106 die("st-" VERSION ", © 2009 st engineers\n");
1108 die("usage: st [-v]\n");
1109 setlocale(LC_CTYPE, "");