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, TMaltcharset };
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);
168 die(const char *errstr, ...) {
171 va_start(ap, errstr);
172 vfprintf(stderr, errstr, ap);
179 char *args[3] = {SHELL, "-i", NULL};
180 putenv("TERM=" TNAME);
185 xbell(void) { /* visual bell */
186 XRectangle r = { 0, 0, xw.w, xw.h };
187 XSetForeground(xw.dis, dc.gc, dc.col[BellCol]);
188 XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
196 if(waitpid(pid, &stat, 0) < 0)
197 die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
199 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];
332 for(i = term.top; i < term.bot; i++)
333 term.line[i] = term.line[i+1];
334 memset(temp, 0, sizeof(Glyph) * term.col);
335 term.line[term.bot] = temp;
341 int y = term.c.y + 1;
344 tscroll(), y = term.bot;
351 escseq.buf[escseq.len++] = c;
352 if(escfinal(c) || escseq.len >= ESCSIZ) {
353 escparse(), eschandle();
362 char *p = escseq.buf;
365 switch(escseq.pre = *p++) {
368 escseq.priv = 1, p++;
370 while(p < escseq.buf+escseq.len) {
372 escseq.arg[escseq.narg] *= 10;
373 escseq.arg[escseq.narg] += *(p++) - '0'/*, noarg = 0 */;
385 /* XXX: graphic character set */
391 tmoveto(int x, int y) {
392 term.c.x = x < 0 ? 0 : x >= term.col ? term.col-1 : x;
393 term.c.y = y < 0 ? 0 : y >= term.row ? term.row-1 : y;
398 int xf = term.c.x, yf = term.c.y;
410 xf = term.col-1, yf--;
412 yf = term.top, xf = 0;
420 yf = term.bot, tscroll();
429 term.line[term.c.y][term.c.x] = term.c.attr;
430 term.line[term.c.y][term.c.x].c = c;
431 term.line[term.c.y][term.c.x].state |= CRset | CRupdate;
435 tclearregion(int x1, int y1, int x2, int y2) {
438 LIMIT(x1, 0, term.col-1);
439 LIMIT(x2, 0, term.col-1);
440 LIMIT(y1, 0, term.row-1);
441 LIMIT(y2, 0, term.row-1);
443 /* XXX: could be optimized */
444 for(x = x1; x <= x2; x++)
445 for(y = y1; y <= y2; y++)
446 memset(&term.line[y][x], 0, sizeof(Glyph));
448 xclear(x1, y1, x2, y2);
453 int src = term.c.x + n;
455 int size = term.col - src;
457 if(src >= term.col) {
458 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
461 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
462 tclearregion(term.col-size, term.c.y, term.col-1, term.c.y);
466 tinsertblank(int n) {
469 int size = term.col - n - src;
471 if(dst >= term.col) {
472 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
475 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
476 tclearregion(src, term.c.y, dst, term.c.y);
480 tinsertblankline (int n) {
485 if(term.c.y > term.bot)
487 else if(term.c.y < term.top)
489 if(term.c.y + n >= bot) {
490 tclearregion(0, term.c.y, term.col-1, bot);
493 for(i = bot; i >= term.c.y+n; i--) {
494 /* swap deleted line <-> blanked line */
495 blank = term.line[i];
496 term.line[i] = term.line[i-n];
497 term.line[i-n] = blank;
499 memset(blank, 0, term.col * sizeof(Glyph));
510 if(term.c.y > term.bot)
512 else if(term.c.y < term.top)
514 if(term.c.y + n >= bot) {
515 tclearregion(0, term.c.y, term.col-1, bot);
518 for(i = term.c.y; i <= bot-n; i++) {
519 /* swap deleted line <-> blanked line */
520 blank = term.line[i];
521 term.line[i] = term.line[i+n];
522 term.line[i+n] = blank;
524 memset(blank, 0, term.col * sizeof(Glyph));
529 tsetattr(int *attr, int l) {
532 for(i = 0; i < l; i++) {
535 memset(&term.c.attr, 0, sizeof(term.c.attr));
536 term.c.attr.fg = DefaultFG;
537 term.c.attr.bg = DefaultBG;
540 term.c.attr.mode |= ATbold;
543 term.c.attr.mode |= ATunderline;
546 term.c.attr.mode |= ATreverse;
549 term.c.hidden = CShide;
552 term.c.attr.mode &= ~ATbold;
555 term.c.attr.mode &= ~ATunderline;
558 term.c.attr.mode &= ~ATreverse;
561 term.c.attr.fg = DefaultFG;
564 term.c.attr.fg = DefaultBG;
567 if(BETWEEN(attr[i], 30, 37))
568 term.c.attr.fg = attr[i] - 30;
569 else if(BETWEEN(attr[i], 40, 47))
570 term.c.attr.bg = attr[i] - 40;
577 tsetscroll(int t, int b) {
580 LIMIT(t, 0, term.row-1);
581 LIMIT(b, 0, term.row-1);
597 switch(escseq.mode) {
598 case '@': /* Insert <n> blank char */
599 DEFAULT(escseq.arg[0], 1);
600 tinsertblank(escseq.arg[0]);
602 case 'A': /* Cursor <n> Up */
604 DEFAULT(escseq.arg[0], 1);
605 tmoveto(term.c.x, term.c.y-escseq.arg[0]);
607 case 'B': /* Cursor <n> Down */
608 DEFAULT(escseq.arg[0], 1);
609 tmoveto(term.c.x, term.c.y+escseq.arg[0]);
611 case 'C': /* Cursor <n> Forward */
613 DEFAULT(escseq.arg[0], 1);
614 tmoveto(term.c.x+escseq.arg[0], term.c.y);
616 case 'D': /* Cursor <n> Backward */
617 DEFAULT(escseq.arg[0], 1);
618 tmoveto(term.c.x-escseq.arg[0], term.c.y);
620 case 'E': /* Cursor <n> Down and first col */
621 DEFAULT(escseq.arg[0], 1);
622 tmoveto(0, term.c.y+escseq.arg[0]);
624 case 'F': /* Cursor <n> Up and first col */
625 DEFAULT(escseq.arg[0], 1);
626 tmoveto(0, term.c.y-escseq.arg[0]);
628 case 'G': /* Move to <col> */
630 DEFAULT(escseq.arg[0], 1);
631 tmoveto(escseq.arg[0]-1, term.c.y);
633 case 'H': /* Move to <row> <col> */
635 DEFAULT(escseq.arg[0], 1);
636 DEFAULT(escseq.arg[1], 1);
637 tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
639 case 'J': /* Clear screen */
640 switch(escseq.arg[0]) {
642 tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
645 tclearregion(0, 0, term.c.x, term.c.y);
648 tclearregion(0, 0, term.col-1, term.row-1);
652 case 'K': /* Clear line */
653 switch(escseq.arg[0]) {
655 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
658 tclearregion(0, term.c.y, term.c.x, term.c.y);
661 tclearregion(0, term.c.y, term.col-1, term.c.y);
665 case 'L': /* Insert <n> blank lines */
666 DEFAULT(escseq.arg[0], 1);
667 tinsertblankline(escseq.arg[0]);
670 if(escseq.priv && escseq.arg[0] == 25)
673 case 'M': /* Delete <n> lines */
674 DEFAULT(escseq.arg[0], 1);
675 tdeleteline(escseq.arg[0]);
677 case 'P': /* Delete <n> char */
678 DEFAULT(escseq.arg[0], 1);
679 tdeletechar(escseq.arg[0]);
681 case 'd': /* Move to <row> */
682 DEFAULT(escseq.arg[0], 1);
683 tmoveto(term.c.x, escseq.arg[0]-1);
685 case 'h': /* Set terminal mode */
686 if(escseq.priv && escseq.arg[0] == 25)
689 case 'm': /* Terminal attribute (color) */
690 tsetattr(escseq.arg, escseq.narg);
696 DEFAULT(escseq.arg[0], 1);
697 DEFAULT(escseq.arg[1], term.row);
698 tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
701 case 's': /* Save cursor position */
704 case 'u': /* Load cursor position */
716 printf("rawbuf : %s\n", escseq.buf);
717 printf("prechar : %c\n", escseq.pre);
718 printf("private : %c\n", escseq.priv ? '?' : ' ');
719 printf("narg : %d\n", escseq.narg);
721 for(i = 0; i < escseq.narg; i++)
722 printf("\targ %d = %d\n", i, escseq.arg[i]);
724 printf("mode : %c\n", escseq.mode);
729 memset(&escseq, 0, sizeof(escseq));
734 int space = TAB - term.c.x % TAB;
736 if(term.c.x + space >= term.col)
739 for(; space > 0; space--)
745 static int inesc = 0;
749 /* start of escseq */
751 escreset(), inesc = 1;
767 tmoveto(0, term.c.y);
779 tputs(char *s, int len) {
780 for(; len > 0; len--)
789 for(row = 0; row < term.row; row++) {
790 for(col = 0; col < term.col; col++) {
791 if(col == term.c.x && row == term.c.y)
794 c = term.line[row][col];
795 putchar(c.state & CRset ? c.c : '.');
803 tresize(int col, int row) {
806 int minrow = MIN(row, term.row);
807 int mincol = MIN(col, term.col);
809 if(col < 1 || row < 1)
812 line = calloc(row, sizeof(Line));
813 for(i = 0 ; i < row; i++)
814 line[i] = calloc(col, sizeof(Glyph));
816 for(i = 0 ; i < minrow; i++)
817 memcpy(line[i], term.line[i], mincol * sizeof(Glyph));
819 for(i = 0; i < term.row; i++)
823 LIMIT(term.c.x, 0, col-1);
824 LIMIT(term.c.y, 0, row-1);
825 LIMIT(term.top, 0, row-1);
826 LIMIT(term.bot, 0, row-1);
830 term.col = col, term.row = row;
834 xgetcol(const char *s) {
836 Colormap cmap = DefaultColormap(xw.dis, xw.scr);
838 if(!XAllocNamedColor(xw.dis, cmap, s, &color, &color)) {
839 color.pixel = WhitePixel(xw.dis, xw.scr);
840 fprintf(stderr, "Could not allocate color '%s'\n", s);
847 xclear(int x1, int y1, int x2, int y2) {
848 XClearArea(xw.dis, xw.win,
849 x1 * xw.cw, y1 * xw.ch,
850 (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch,
857 int srcy = (term.top+1) * xw.ch;
858 int dsty = term.top * xw.ch;
859 int height = (term.bot-term.top) * xw.ch;
862 XCopyArea(xw.dis, xw.win, xw.win, dc.gc, 0, srcy, xw.w, height, 0, dsty);
863 xclear(0, term.bot, term.col-1, term.bot);
872 unsigned long valuemask;
876 char *args[] = {NULL};
879 xw.dis = XOpenDisplay(NULL);
880 xw.scr = XDefaultScreen(xw.dis);
882 die("Can't open display\n");
885 if(!(dc.font = XLoadQueryFont(xw.dis, FONT)))
886 die("Can't load font %s\n", FONT);
888 xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
889 xw.ch = dc.font->ascent + dc.font->descent + LINESPACE;
892 for(i = 0; i < LEN(colorname); i++)
893 dc.col[i] = xgetcol(colorname[i]);
895 term.c.attr.fg = DefaultFG;
896 term.c.attr.bg = DefaultBG;
897 term.c.attr.mode = ATnone;
899 xw.h = term.row * xw.ch;
900 xw.w = term.col * xw.cw;
901 /* XXX: this BORDER is useless after the first resize, handle it in xdraws() */
902 xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
907 values.foreground = XWhitePixel(xw.dis, xw.scr);
908 values.font = dc.font->fid;
909 valuemask = GCForeground | GCFont;
910 dc.gc = XCreateGC(xw.dis, xw.win, valuemask, &values);
911 XMapWindow(xw.dis, xw.win);
913 chint.res_name = TNAME, chint.res_class = TNAME;
914 wmhint.input = 1, wmhint.flags = InputHint;
915 shint.height_inc = xw.ch, shint.width_inc = xw.cw;
916 shint.height = xw.h, shint.width = xw.w;
917 shint.flags = PSize | PResizeInc;
918 XSetWMProperties(xw.dis, xw.win, NULL, NULL, &args[0], 0, &shint, &wmhint, &chint);
919 XStoreName(xw.dis, xw.win, TNAME);
924 xdrawc(int x, int y, Glyph g) {
925 XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
926 unsigned long xfg, xbg;
929 if(g.mode & ATreverse)
930 xfg = dc.col[g.bg], xbg = dc.col[g.fg];
932 xfg = dc.col[g.fg], xbg = dc.col[g.bg];
934 XSetForeground(xw.dis, dc.gc, xbg);
935 XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
937 XSetForeground(xw.dis, dc.gc, xfg);
938 XDrawString(xw.dis, xw.win, dc.gc, r.x, r.y+dc.font->ascent, &(g.c), 1);
939 if(g.mode & ATbold) /* XXX: bold hack (draw again at x+1) */
940 XDrawString(xw.dis, xw.win, dc.gc, r.x+1, r.y+dc.font->ascent, &(g.c), 1);
942 if(g.mode & ATunderline) {
943 r.y += dc.font->ascent + 1;
944 XDrawLine(xw.dis, xw.win, dc.gc, r.x, r.y, r.x+r.width-1, r.y);
952 Glyph g = {' ', ATnone, DefaultBG, DefaultCS, 0};
954 LIMIT(oldx, 0, term.col-1);
955 LIMIT(oldy, 0, term.row-1);
957 if(term.line[term.c.y][term.c.x].state & CRset)
958 g.c = term.line[term.c.y][term.c.x].c;
959 /* remove the old cursor */
960 if(term.line[oldy][oldx].state & CRset)
961 xdrawc(oldx, oldy, term.line[oldy][oldx]);
962 else xclear(oldx, oldy, oldx, oldy); /* XXX: maybe a bug */
963 if(mode == CSdraw && !term.c.hidden) {
964 xdrawc(term.c.x, term.c.y, g);
965 oldx = term.c.x, oldy = term.c.y;
971 draw(int redraw_all) {
976 XClearWindow(xw.dis, xw.win);
977 /* XXX: drawing could be optimised */
978 for(y = 0; y < term.row; y++) {
979 for(x = 0; x < term.col; x++) {
980 changed = term.line[y][x].state & CRupdate;
981 set = term.line[y][x].state & CRset;
982 if((changed && set) || (redraw_all && set)) {
983 term.line[y][x].state &= ~CRupdate;
984 xdrawc(x, y, term.line[y][x]);
994 for(i = 0; i < LEN(key); i++)
996 return (char*)key[i].s;
1001 kpress(XKeyEvent *e) {
1009 meta = e->state & Mod1Mask;
1010 shift = e->state & ShiftMask;
1011 len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
1012 if(skmap = kmap(ksym))
1013 ttywrite(skmap, strlen(skmap));
1015 buf[sizeof(buf)-1] = '\0';
1016 if(meta && len == 1)
1017 ttywrite("\033", 1);
1023 /* XXX: paste X clipboard */;
1026 fprintf(stderr, "errkey: %d\n", (int)ksym);
1034 col = e->xconfigure.width / xw.cw;
1035 row = e->xconfigure.height / xw.ch;
1037 if(term.col != col || term.row != row) {
1039 ttyresize(col, row);
1040 xw.w = e->xconfigure.width;
1041 xw.h = e->xconfigure.height;
1051 int xfd = XConnectionNumber(xw.dis);
1054 XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
1055 XResizeWindow(xw.dis, xw.win, xw.w , xw.h); /* fix resize bug in wmii (?) */
1059 FD_SET(cmdfd, &rfd);
1062 ret = select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL);
1065 die("select failed: %s\n", SERRNO);
1067 if(FD_ISSET(xfd, &rfd)) {
1068 while(XPending(xw.dis)) {
1069 XNextEvent(xw.dis, &ev);
1079 case ConfigureNotify:
1085 if(FD_ISSET(cmdfd, &rfd)) {
1093 main(int argc, char *argv[]) {
1094 if(argc == 2 && !strncmp("-v", argv[1], 3))
1095 die("st-" VERSION ", © 2009 st engineers\n");
1097 die("usage: st [-v]\n");
1098 setlocale(LC_CTYPE, "");