1 /* See LICENSE for licence details. */
14 die(const char *errstr, ...) {
18 vfprintf(stderr, errstr, ap);
25 char *args[3] = {SHELL, "-i", NULL};
26 putenv("TERM=" TNAME);
31 xbell(void) { /* visual bell */
32 XRectangle r = { 0, 0, xw.w, xw.h };
33 XSetForeground(xw.dis, dc.gc, dc.col[BellCol]);
34 XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
42 if(waitpid(pid, &stat, 0) < 0)
43 die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
45 exit(WEXITSTATUS(stat));
56 if((m = posix_openpt(O_RDWR | O_NOCTTY)) < 0)
57 die("openpt failed: %s\n", SERRNO);
59 die("grandpt failed: %s\n", SERRNO);
61 die("unlockpt failed: %s\n", SERRNO);
62 if(!(pts = ptsname(m)))
63 die("ptsname failed: %s\n", SERRNO);
64 if((s = open(pts, O_RDWR | O_NOCTTY)) < 0)
65 die("Couldn't open slave: %s\n", SERRNO);
66 fcntl(s, F_SETFL, O_NDELAY);
67 switch(pid = fork()) {
72 setsid(); /* create a new process group */
73 dup2(s, STDIN_FILENO);
74 dup2(s, STDOUT_FILENO);
75 dup2(s, STDERR_FILENO);
76 if(ioctl(s, TIOCSCTTY, NULL) < 0)
77 die("ioctl TTIOCSTTY failed: %s\n", SERRNO);
83 signal(SIGCHLD, sigchld);
90 fprintf(stderr, " %02x %c ", c, isprint(c)?c:'.');
92 fprintf(stderr, "\n");
97 char buf[BUFSIZ] = {0};
100 switch(ret = read(cmdfd, buf, BUFSIZ)) {
102 die("Couldn't read from shell: %s\n", SERRNO);
110 ttywrite(char *s, size_t n) {
111 if(write(cmdfd, s, n) == -1)
112 die("write error on tty: %s\n", SERRNO);
116 ttyresize(int x, int y) {
121 w.ws_xpixel = w.ws_ypixel = 0;
122 if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
123 fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
139 else if(BETWEEN(c, 0x40, 0x7E))
150 x = term.c.x, y = term.c.y;
151 else if(mode == CSload)
156 tnew(int col, int row) { /* screen size */
157 term.row = row, term.col = col;
158 term.top = 0, term.bot = term.row - 1;
162 term.c.attr.mode = ATnone;
163 term.c.attr.fg = DefaultFG;
164 term.c.attr.bg = DefaultBG;
165 term.c.x = term.c.y = 0;
167 /* allocate screen */
168 term.line = calloc(term.row, sizeof(Line));
169 for(row = 0 ; row < term.row; row++)
170 term.line[row] = calloc(term.col, sizeof(Glyph));
175 Line temp = term.line[term.top];
178 for(i = term.top; i < term.bot; i++)
179 term.line[i] = term.line[i+1];
180 memset(temp, 0, sizeof(Glyph) * term.col);
181 term.line[term.bot] = temp;
187 int y = term.c.y + 1;
190 tscroll(), y = term.bot;
197 escseq.buf[escseq.len++] = c;
198 if(escfinal(c) || escseq.len >= ESCSIZ) {
199 escparse(), eschandle();
208 char *p = escseq.buf;
211 switch(escseq.pre = *p++) {
214 escseq.priv = 1, p++;
216 while(p < escseq.buf+escseq.len) {
218 escseq.arg[escseq.narg] *= 10;
219 escseq.arg[escseq.narg] += *(p++) - '0'/*, noarg = 0 */;
231 /* XXX: graphic character set */
237 tmoveto(int x, int y) {
238 term.c.x = x < 0 ? 0 : x >= term.col ? term.col-1 : x;
239 term.c.y = y < 0 ? 0 : y >= term.row ? term.row-1 : y;
244 int xf = term.c.x, yf = term.c.y;
256 xf = term.col-1, yf--;
258 yf = term.top, xf = 0;
266 yf = term.bot, tscroll();
275 term.line[term.c.y][term.c.x] = term.c.attr;
276 term.line[term.c.y][term.c.x].c = c;
277 term.line[term.c.y][term.c.x].state |= CRset | CRupdate;
281 tclearregion(int x1, int y1, int x2, int y2) {
284 LIMIT(x1, 0, term.col-1);
285 LIMIT(x2, 0, term.col-1);
286 LIMIT(y1, 0, term.row-1);
287 LIMIT(y2, 0, term.row-1);
289 /* XXX: could be optimized */
290 for(x = x1; x <= x2; x++)
291 for(y = y1; y <= y2; y++)
292 memset(&term.line[y][x], 0, sizeof(Glyph));
294 xclear(x1, y1, x2, y2);
299 int src = term.c.x + n;
301 int size = term.col - src;
303 if(src >= term.col) {
304 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
307 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
308 tclearregion(term.col-size, term.c.y, term.col-1, term.c.y);
312 tinsertblank(int n) {
315 int size = term.col - n - src;
317 if(dst >= term.col) {
318 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
321 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
322 tclearregion(src, term.c.y, dst, term.c.y);
326 tinsertblankline (int n) {
331 if(term.c.y > term.bot)
333 else if(term.c.y < term.top)
335 if(term.c.y + n >= bot) {
336 tclearregion(0, term.c.y, term.col-1, bot);
339 for(i = bot; i >= term.c.y+n; i--) {
340 /* swap deleted line <-> blanked line */
341 blank = term.line[i];
342 term.line[i] = term.line[i-n];
343 term.line[i-n] = blank;
345 memset(blank, 0, term.col * sizeof(Glyph));
356 if(term.c.y > term.bot)
358 else if(term.c.y < term.top)
360 if(term.c.y + n >= bot) {
361 tclearregion(0, term.c.y, term.col-1, bot);
364 for(i = term.c.y; i <= bot-n; i++) {
365 /* swap deleted line <-> blanked line */
366 blank = term.line[i];
367 term.line[i] = term.line[i+n];
368 term.line[i+n] = blank;
370 memset(blank, 0, term.col * sizeof(Glyph));
375 tsetattr(int *attr, int l) {
378 for(i = 0; i < l; i++) {
381 memset(&term.c.attr, 0, sizeof(term.c.attr));
382 term.c.attr.fg = DefaultFG;
383 term.c.attr.bg = DefaultBG;
386 term.c.attr.mode |= ATbold;
389 term.c.attr.mode |= ATunderline;
392 term.c.attr.mode |= ATreverse;
395 term.c.hidden = CShide;
398 term.c.attr.mode &= ~ATbold;
401 term.c.attr.mode &= ~ATunderline;
404 term.c.attr.mode &= ~ATreverse;
407 term.c.attr.fg = DefaultFG;
410 term.c.attr.fg = DefaultBG;
413 if(BETWEEN(attr[i], 30, 37))
414 term.c.attr.fg = attr[i] - 30;
415 else if(BETWEEN(attr[i], 40, 47))
416 term.c.attr.bg = attr[i] - 40;
423 tsetscroll(int t, int b) {
426 LIMIT(t, 0, term.row-1);
427 LIMIT(b, 0, term.row-1);
443 switch(escseq.mode) {
444 case '@': /* Insert <n> blank char */
445 DEFAULT(escseq.arg[0], 1);
446 tinsertblank(escseq.arg[0]);
448 case 'A': /* Cursor <n> Up */
450 DEFAULT(escseq.arg[0], 1);
451 tmoveto(term.c.x, term.c.y-escseq.arg[0]);
453 case 'B': /* Cursor <n> Down */
454 DEFAULT(escseq.arg[0], 1);
455 tmoveto(term.c.x, term.c.y+escseq.arg[0]);
457 case 'C': /* Cursor <n> Forward */
459 DEFAULT(escseq.arg[0], 1);
460 tmoveto(term.c.x+escseq.arg[0], term.c.y);
462 case 'D': /* Cursor <n> Backward */
463 DEFAULT(escseq.arg[0], 1);
464 tmoveto(term.c.x-escseq.arg[0], term.c.y);
466 case 'E': /* Cursor <n> Down and first col */
467 DEFAULT(escseq.arg[0], 1);
468 tmoveto(0, term.c.y+escseq.arg[0]);
470 case 'F': /* Cursor <n> Up and first col */
471 DEFAULT(escseq.arg[0], 1);
472 tmoveto(0, term.c.y-escseq.arg[0]);
474 case 'G': /* Move to <col> */
476 DEFAULT(escseq.arg[0], 1);
477 tmoveto(escseq.arg[0]-1, term.c.y);
479 case 'H': /* Move to <row> <col> */
481 DEFAULT(escseq.arg[0], 1);
482 DEFAULT(escseq.arg[1], 1);
483 tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
485 case 'J': /* Clear screen */
486 switch(escseq.arg[0]) {
488 tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
491 tclearregion(0, 0, term.c.x, term.c.y);
494 tclearregion(0, 0, term.col-1, term.row-1);
498 case 'K': /* Clear line */
499 switch(escseq.arg[0]) {
501 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
504 tclearregion(0, term.c.y, term.c.x, term.c.y);
507 tclearregion(0, term.c.y, term.col-1, term.c.y);
511 case 'L': /* Insert <n> blank lines */
512 DEFAULT(escseq.arg[0], 1);
513 tinsertblankline(escseq.arg[0]);
516 if(escseq.priv && escseq.arg[0] == 25)
519 case 'M': /* Delete <n> lines */
520 DEFAULT(escseq.arg[0], 1);
521 tdeleteline(escseq.arg[0]);
523 case 'P': /* Delete <n> char */
524 DEFAULT(escseq.arg[0], 1);
525 tdeletechar(escseq.arg[0]);
527 case 'd': /* Move to <row> */
528 DEFAULT(escseq.arg[0], 1);
529 tmoveto(term.c.x, escseq.arg[0]-1);
531 case 'h': /* Set terminal mode */
532 if(escseq.priv && escseq.arg[0] == 25)
535 case 'm': /* Terminal attribute (color) */
536 tsetattr(escseq.arg, escseq.narg);
542 DEFAULT(escseq.arg[0], 1);
543 DEFAULT(escseq.arg[1], term.row);
544 tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
547 case 's': /* Save cursor position */
550 case 'u': /* Load cursor position */
562 printf("rawbuf : %s\n", escseq.buf);
563 printf("prechar : %c\n", escseq.pre);
564 printf("private : %c\n", escseq.priv ? '?' : ' ');
565 printf("narg : %d\n", escseq.narg);
567 for(i = 0; i < escseq.narg; i++)
568 printf("\targ %d = %d\n", i, escseq.arg[i]);
570 printf("mode : %c\n", escseq.mode);
575 memset(&escseq, 0, sizeof(escseq));
580 int space = TAB - term.c.x % TAB;
582 if(term.c.x + space >= term.col)
585 for(; space > 0; space--)
591 static int inesc = 0;
595 /* start of escseq */
597 escreset(), inesc = 1;
613 tmoveto(0, term.c.y);
625 tputs(char *s, int len) {
626 for(; len > 0; len--)
635 for(row = 0; row < term.row; row++) {
636 for(col = 0; col < term.col; col++) {
637 if(col == term.c.x && row == term.c.y)
640 c = term.line[row][col];
641 putchar(c.state & CRset ? c.c : '.');
649 tresize(int col, int row) {
652 int minrow = MIN(row, term.row);
653 int mincol = MIN(col, term.col);
655 if(col < 1 || row < 1)
658 line = calloc(row, sizeof(Line));
659 for(i = 0 ; i < row; i++)
660 line[i] = calloc(col, sizeof(Glyph));
662 for(i = 0 ; i < minrow; i++)
663 memcpy(line[i], term.line[i], mincol * sizeof(Glyph));
665 for(i = 0; i < term.row; i++)
669 LIMIT(term.c.x, 0, col-1);
670 LIMIT(term.c.y, 0, row-1);
671 LIMIT(term.top, 0, row-1);
672 LIMIT(term.bot, 0, row-1);
676 term.col = col, term.row = row;
680 xgetcol(const char *s) {
682 Colormap cmap = DefaultColormap(xw.dis, xw.scr);
684 if(!XAllocNamedColor(xw.dis, cmap, s, &color, &color)) {
685 color.pixel = WhitePixel(xw.dis, xw.scr);
686 fprintf(stderr, "Could not allocate color '%s'\n", s);
693 xclear(int x1, int y1, int x2, int y2) {
694 XClearArea(xw.dis, xw.win,
695 x1 * xw.cw, y1 * xw.ch,
696 (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch,
703 int srcy = (term.top+1) * xw.ch;
704 int dsty = term.top * xw.ch;
705 int height = (term.bot-term.top) * xw.ch;
708 XCopyArea(xw.dis, xw.win, xw.win, dc.gc, 0, srcy, xw.w, height, 0, dsty);
709 xclear(0, term.bot, term.col-1, term.bot);
718 unsigned long valuemask;
722 char *args[] = {NULL};
725 xw.dis = XOpenDisplay(NULL);
726 xw.scr = XDefaultScreen(xw.dis);
728 die("can not open display");
731 if(!(dc.font = XLoadQueryFont(xw.dis, FONT)))
732 die("can not find font " FONT);
734 xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
735 xw.ch = dc.font->ascent + dc.font->descent + LINESPACE;
738 for(i = 0; i < LEN(colorname); i++)
739 dc.col[i] = xgetcol(colorname[i]);
741 term.c.attr.fg = DefaultFG;
742 term.c.attr.bg = DefaultBG;
743 term.c.attr.mode = ATnone;
745 xw.h = term.row * xw.ch;
746 xw.w = term.col * xw.cw;
747 /* XXX: this BORDER is useless after the first resize, handle it in xdraws() */
748 xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
753 values.foreground = XWhitePixel(xw.dis, xw.scr);
754 values.font = dc.font->fid;
755 valuemask = GCForeground | GCFont;
756 dc.gc = XCreateGC(xw.dis, xw.win, valuemask, &values);
757 XMapWindow(xw.dis, xw.win);
759 chint.res_name = TNAME, chint.res_class = TNAME;
760 wmhint.input = 1, wmhint.flags = InputHint;
761 shint.height_inc = xw.ch, shint.width_inc = xw.cw;
762 shint.height = xw.h, shint.width = xw.w;
763 shint.flags = PSize | PResizeInc;
764 XSetWMProperties(xw.dis, xw.win, NULL, NULL, &args[0], 0, &shint, &wmhint, &chint);
765 XStoreName(xw.dis, xw.win, TNAME);
770 xdrawc(int x, int y, Glyph g) {
771 XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
772 unsigned long xfg, xbg;
775 if(g.mode & ATreverse)
776 xfg = dc.col[g.bg], xbg = dc.col[g.fg];
778 xfg = dc.col[g.fg], xbg = dc.col[g.bg];
780 XSetForeground(xw.dis, dc.gc, xbg);
781 XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
783 XSetForeground(xw.dis, dc.gc, xfg);
784 XDrawString(xw.dis, xw.win, dc.gc, r.x, r.y+dc.font->ascent, &(g.c), 1);
785 if(g.mode & ATbold) /* XXX: bold hack (draw again at x+1) */
786 XDrawString(xw.dis, xw.win, dc.gc, r.x+1, r.y+dc.font->ascent, &(g.c), 1);
788 if(g.mode & ATunderline) {
789 r.y += dc.font->ascent + 1;
790 XDrawLine(xw.dis, xw.win, dc.gc, r.x, r.y, r.x+r.width-1, r.y);
798 Glyph g = {' ', ATnone, DefaultBG, DefaultCS, 0};
800 LIMIT(oldx, 0, term.col-1);
801 LIMIT(oldy, 0, term.row-1);
803 if(term.line[term.c.y][term.c.x].state & CRset)
804 g.c = term.line[term.c.y][term.c.x].c;
805 /* remove the old cursor */
806 if(term.line[oldy][oldx].state & CRset)
807 xdrawc(oldx, oldy, term.line[oldy][oldx]);
808 else xclear(oldx, oldy, oldx, oldy); /* XXX: maybe a bug */
809 if(mode == CSdraw && !term.c.hidden) {
810 xdrawc(term.c.x, term.c.y, g);
811 oldx = term.c.x, oldy = term.c.y;
817 draw(int redraw_all) {
822 XClearWindow(xw.dis, xw.win);
823 /* XXX: drawing could be optimised */
824 for(y = 0; y < term.row; y++) {
825 for(x = 0; x < term.col; x++) {
826 changed = term.line[y][x].state & CRupdate;
827 set = term.line[y][x].state & CRset;
828 if((changed && set) || (redraw_all && set)) {
829 term.line[y][x].state &= ~CRupdate;
830 xdrawc(x, y, term.line[y][x]);
838 kpress(XKeyEvent *e) {
845 meta = e->state & Mod1Mask;
846 shift = e->state & ShiftMask;
847 len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
849 buf[sizeof(buf)-1] = '\0';
857 fprintf(stderr, "errkey: %d\n", (int)ksym);
863 sprintf(buf, "\033[%c", "DACB"[ksym - XK_Left]);
866 case XK_Delete: ttywrite(KEYDELETE, sizeof(KEYDELETE)-1); break;
867 case XK_Home: ttywrite(KEYHOME, sizeof(KEYHOME)-1); break;
868 case XK_End: ttywrite(KEYEND, sizeof(KEYEND) -1); break;
869 case XK_Prior: ttywrite(KEYPREV, sizeof(KEYPREV)-1); break;
870 case XK_Next: ttywrite(KEYNEXT, sizeof(KEYNEXT)-1); break;
872 /* XXX: paste X clipboard */
882 col = e->xconfigure.width / xw.cw;
883 row = e->xconfigure.height / xw.ch;
885 if(term.col != col || term.row != row) {
888 xw.w = e->xconfigure.width;
889 xw.h = e->xconfigure.height;
900 int xfd = XConnectionNumber(xw.dis);
903 XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
904 XResizeWindow(xw.dis, xw.win, xw.w , xw.h); /* fix resize bug in wmii (?) */
911 ret = select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL);
914 die("select failed: %s\n", SERRNO);
916 if(FD_ISSET(xfd, &rfd)) {
917 while(XPending(xw.dis)) {
918 XNextEvent(xw.dis, &ev);
928 case ConfigureNotify:
934 if(FD_ISSET(cmdfd, &rfd)) {
942 main(int argc, char *argv[]) {
943 if(argc == 2 && !strncmp("-v", argv[1], 3))
944 die("st-" VERSION ", © 2009 st engineers\n");
946 die("usage: st [-v]\n");
947 setlocale(LC_CTYPE, "");