1 /* See LICENSE for licence details. */
13 die(const char *errstr, ...) {
17 vfprintf(stderr, errstr, ap);
24 char *args[3] = {SHELL, "-i", NULL};
25 putenv("TERM=" TNAME);
30 xbell(void) { /* visual bell */
31 XRectangle r = { 0, 0, xw.w, xw.h };
32 XSetForeground(xw.dis, dc.gc, dc.col[BellCol]);
33 XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
45 if((m = posix_openpt(O_RDWR | O_NOCTTY)) < 0)
51 if((pts = ptsname(m)) == NULL)
53 if((s = open(pts, O_RDWR | O_NOCTTY)) < 0)
55 fcntl(s, F_SETFL, O_NDELAY);
56 switch(pid = fork()) {
61 setsid(); /* create a new process group */
62 dup2(s, STDIN_FILENO);
63 dup2(s, STDOUT_FILENO);
64 dup2(s, STDERR_FILENO);
65 if(ioctl(s, TIOCSCTTY, NULL) < 0)
66 die("slave TTIOCSTTY");
78 fprintf(stderr, " %02x %c ", c, isprint(c)?c:'.');
80 fprintf(stderr, "\n");
85 char buf[BUFSIZ] = {0};
88 switch(ret = read(cmdfd, buf, BUFSIZ)) {
89 case -1: /* error or exit */
90 /* XXX: be more precise */
99 ttywrite(char *s, size_t n) {
100 if(write(cmdfd, s, n) == -1)
101 die("write error on tty.");
105 ttyresize(int x, int y) {
110 w.ws_xpixel = w.ws_ypixel = 0;
111 if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
112 fprintf(stderr, "Couldn't set window size: %m\n");
128 else if(BETWEEN(c, 0x40, 0x7E))
139 x = term.c.x, y = term.c.y;
140 else if(mode == CSload)
145 tnew(int col, int row) { /* screen size */
146 term.row = row, term.col = col;
147 term.top = 0, term.bot = term.row - 1;
151 term.c.attr.mode = ATnone;
152 term.c.attr.fg = DefaultFG;
153 term.c.attr.bg = DefaultBG;
154 term.c.x = term.c.y = 0;
156 /* allocate screen */
157 term.line = calloc(term.row, sizeof(Line));
158 for(row = 0 ; row < term.row; row++)
159 term.line[row] = calloc(term.col, sizeof(Glyph));
164 Line temp = term.line[term.top];
167 for(i = term.top; i < term.bot; i++)
168 term.line[i] = term.line[i+1];
169 memset(temp, 0, sizeof(Glyph) * term.col);
170 term.line[term.bot] = temp;
176 int y = term.c.y + 1;
179 tscroll(), y = term.bot;
186 escseq.buf[escseq.len++] = c;
187 if(escfinal(c) || escseq.len >= ESCSIZ) {
188 escparse(), eschandle();
197 char *p = escseq.buf;
200 switch(escseq.pre = *p++) {
203 escseq.priv = 1, p++;
205 while(p < escseq.buf+escseq.len) {
207 escseq.arg[escseq.narg] *= 10;
208 escseq.arg[escseq.narg] += *(p++) - '0'/*, noarg = 0 */;
220 /* XXX: graphic character set */
226 tmoveto(int x, int y) {
227 term.c.x = x < 0 ? 0 : x >= term.col ? term.col-1 : x;
228 term.c.y = y < 0 ? 0 : y >= term.row ? term.row-1 : y;
233 int xf = term.c.x, yf = term.c.y;
245 xf = term.col-1, yf--;
247 yf = term.top, xf = 0;
255 yf = term.bot, tscroll();
264 term.line[term.c.y][term.c.x] = term.c.attr;
265 term.line[term.c.y][term.c.x].c = c;
266 term.line[term.c.y][term.c.x].state |= CRset | CRupdate;
270 tclearregion(int x1, int y1, int x2, int y2) {
273 LIMIT(x1, 0, term.col-1);
274 LIMIT(x2, 0, term.col-1);
275 LIMIT(y1, 0, term.row-1);
276 LIMIT(y2, 0, term.row-1);
278 /* XXX: could be optimized */
279 for(x = x1; x <= x2; x++)
280 for(y = y1; y <= y2; y++)
281 memset(&term.line[y][x], 0, sizeof(Glyph));
283 xclear(x1, y1, x2, y2);
288 int src = term.c.x + n;
290 int size = term.col - src;
292 if(src >= term.col) {
293 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
296 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
297 tclearregion(term.col-size, term.c.y, term.col-1, term.c.y);
301 tinsertblank(int n) {
304 int size = term.col - n - src;
306 if(dst >= term.col) {
307 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
310 memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
311 tclearregion(src, term.c.y, dst, term.c.y);
315 tinsertblankline (int n) {
320 if(term.c.y > term.bot)
322 else if(term.c.y < term.top)
324 if(term.c.y + n >= bot) {
325 tclearregion(0, term.c.y, term.col-1, bot);
328 for(i = bot; i >= term.c.y+n; i--) {
329 /* swap deleted line <-> blanked line */
330 blank = term.line[i];
331 term.line[i] = term.line[i-n];
332 term.line[i-n] = blank;
334 memset(blank, 0, term.col * sizeof(Glyph));
345 if(term.c.y > term.bot)
347 else if(term.c.y < term.top)
349 if(term.c.y + n >= bot) {
350 tclearregion(0, term.c.y, term.col-1, bot);
353 for(i = term.c.y; i <= bot-n; i++) {
354 /* swap deleted line <-> blanked line */
355 blank = term.line[i];
356 term.line[i] = term.line[i+n];
357 term.line[i+n] = blank;
359 memset(blank, 0, term.col * sizeof(Glyph));
364 tsetattr(int *attr, int l) {
367 #ifdef TRUECOLOR /* ESC [ ? <fg/bg> ; <r> ; <g> ; <b> m */
369 if(escseq.priv && escseq.len == 4) { /* True color extension :) */
370 col = (escseq.arg[1]<<16) + (escseq.arg[2]<<8) + escseq.arg[3];
371 switch(escseq.arg[0]) {
372 case 3: /* foreground */
373 term.c.attr.fg = col;
375 case 4: /* background */
376 term.c.attr.bg = col;
382 for(i = 0; i < l; i++) {
385 memset(&term.c.attr, 0, sizeof(term.c.attr));
386 term.c.attr.fg = DefaultFG;
387 term.c.attr.bg = DefaultBG;
390 term.c.attr.mode |= ATbold;
393 term.c.attr.mode |= ATunderline;
396 term.c.attr.mode |= ATreverse;
399 term.c.hidden = CShide;
402 term.c.attr.mode &= ~ATbold;
405 term.c.attr.mode &= ~ATunderline;
408 term.c.attr.mode &= ~ATreverse;
411 term.c.attr.fg = DefaultFG;
414 term.c.attr.fg = DefaultBG;
417 if(BETWEEN(attr[i], 30, 37))
418 term.c.attr.fg = attr[i] - 30;
419 else if(BETWEEN(attr[i], 40, 47))
420 term.c.attr.bg = attr[i] - 40;
427 tsetscroll(int t, int b) {
430 LIMIT(t, 0, term.row-1);
431 LIMIT(b, 0, term.row-1);
447 switch(escseq.mode) {
448 case '@': /* Insert <n> blank char */
449 DEFAULT(escseq.arg[0], 1);
450 tinsertblank(escseq.arg[0]);
452 case 'A': /* Cursor <n> Up */
454 DEFAULT(escseq.arg[0], 1);
455 tmoveto(term.c.x, term.c.y-escseq.arg[0]);
457 case 'B': /* Cursor <n> Down */
458 DEFAULT(escseq.arg[0], 1);
459 tmoveto(term.c.x, term.c.y+escseq.arg[0]);
461 case 'C': /* Cursor <n> Forward */
463 DEFAULT(escseq.arg[0], 1);
464 tmoveto(term.c.x+escseq.arg[0], term.c.y);
466 case 'D': /* Cursor <n> Backward */
467 DEFAULT(escseq.arg[0], 1);
468 tmoveto(term.c.x-escseq.arg[0], term.c.y);
470 case 'E': /* Cursor <n> Down and first col */
471 DEFAULT(escseq.arg[0], 1);
472 tmoveto(0, term.c.y+escseq.arg[0]);
474 case 'F': /* Cursor <n> Up and first col */
475 DEFAULT(escseq.arg[0], 1);
476 tmoveto(0, term.c.y-escseq.arg[0]);
478 case 'G': /* Move to <col> */
480 DEFAULT(escseq.arg[0], 1);
481 tmoveto(escseq.arg[0]-1, term.c.y);
483 case 'H': /* Move to <row> <col> */
485 DEFAULT(escseq.arg[0], 1);
486 DEFAULT(escseq.arg[1], 1);
487 tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
489 case 'J': /* Clear screen */
490 switch(escseq.arg[0]) {
492 tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
495 tclearregion(0, 0, term.c.x, term.c.y);
498 tclearregion(0, 0, term.col-1, term.row-1);
502 case 'K': /* Clear line */
503 switch(escseq.arg[0]) {
505 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
508 tclearregion(0, term.c.y, term.c.x, term.c.y);
511 tclearregion(0, term.c.y, term.col-1, term.c.y);
515 case 'L': /* Insert <n> blank lines */
516 DEFAULT(escseq.arg[0], 1);
517 tinsertblankline(escseq.arg[0]);
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 */
533 case 'm': /* Terminal attribute (color) */
534 tsetattr(escseq.arg, escseq.narg);
540 DEFAULT(escseq.arg[0], 1);
541 DEFAULT(escseq.arg[1], term.row);
542 tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
545 case 's': /* Save cursor position */
548 case 'u': /* Load cursor position */
560 printf("rawbuf : %s\n", escseq.buf);
561 printf("prechar : %c\n", escseq.pre);
562 printf("private : %c\n", escseq.priv ? '?' : ' ');
563 printf("narg : %d\n", escseq.narg);
565 for(i = 0; i < escseq.narg; i++)
566 printf("\targ %d = %d\n", i, escseq.arg[i]);
568 printf("mode : %c\n", escseq.mode);
573 memset(&escseq, 0, sizeof(escseq));
578 static int inesc = 0;
581 /* start of escseq */
583 escreset(), inesc = 1;
596 tmoveto(0, term.c.y);
608 tputs(char *s, int len) {
609 for(; len > 0; len--)
618 for(row = 0; row < term.row; row++) {
619 for(col = 0; col < term.col; col++) {
620 if(col == term.c.x && row == term.c.y)
623 c = term.line[row][col];
624 putchar(c.state & CRset ? c.c : '.');
632 tresize(int col, int row) {
635 int minrow = MIN(row, term.row);
636 int mincol = MIN(col, term.col);
638 if(col < 1 || row < 1)
640 line = calloc(row, sizeof(Line));
641 for(i = 0 ; i < row; i++)
642 line[i] = calloc(col, sizeof(Glyph));
643 for(i = 0 ; i < minrow; i++) {
644 memcpy(line[i], term.line[i], mincol * sizeof(Glyph));
648 LIMIT(term.c.x, 0, col-1);
649 LIMIT(term.c.y, 0, row-1);
650 LIMIT(term.top, 0, row-1);
651 LIMIT(term.bot, 0, row-1);
652 // if(term.bot == term.row-1)
655 term.col = col, term.row = row;
659 xgetcol(const char *s) {
661 Colormap cmap = DefaultColormap(xw.dis, xw.scr);
663 if(!XAllocNamedColor(xw.dis, cmap, s, &color, &color)) {
664 color.pixel = WhitePixel(xw.dis, xw.scr);
665 fprintf(stderr, "Could not allocate color '%s'\n", s);
672 xclear(int x1, int y1, int x2, int y2) {
673 XClearArea(xw.dis, xw.win,
674 x1 * xw.cw, y1 * xw.ch,
675 (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch,
682 int srcy = (term.top+1) * xw.ch;
683 int dsty = term.top * xw.ch;
684 int height = (term.bot-term.top) * xw.ch;
687 XCopyArea(xw.dis, xw.win, xw.win, dc.gc, 0, srcy, xw.w, height, 0, dsty);
688 xclear(0, term.bot, term.col-1, term.bot);
697 unsigned long valuemask;
701 char *args[] = {NULL};
704 xw.dis = XOpenDisplay(NULL);
705 xw.scr = XDefaultScreen(xw.dis);
707 die("can not open display");
710 if(!(dc.font = XLoadQueryFont(xw.dis, FONT)))
711 die("can not find font " FONT);
713 xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
714 xw.ch = dc.font->ascent + dc.font->descent + LINESPACE;
717 for(i = 0; i < LEN(colorname); i++)
718 dc.col[i] = xgetcol(colorname[i]);
720 term.c.attr.fg = DefaultFG;
721 term.c.attr.bg = DefaultBG;
722 term.c.attr.mode = ATnone;
724 xw.h = term.row * xw.ch;
725 xw.w = term.col * xw.cw;
726 /* XXX: this BORDER is useless after the first resize, handle it in xdraws() */
727 xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
732 values.foreground = XWhitePixel(xw.dis, xw.scr);
733 values.font = dc.font->fid;
734 valuemask = GCForeground | GCFont;
735 dc.gc = XCreateGC(xw.dis, xw.win, valuemask, &values);
736 XMapWindow(xw.dis, xw.win);
738 chint.res_name = TNAME, chint.res_class = TNAME;
739 wmhint.input = 1, wmhint.flags = InputHint;
740 shint.height_inc = xw.ch, shint.width_inc = xw.cw;
741 shint.height = xw.h, shint.width = xw.w;
742 shint.flags = PSize | PResizeInc;
743 XSetWMProperties(xw.dis, xw.win, NULL, NULL, &args[0], 0, &shint, &wmhint, &chint);
744 XStoreName(xw.dis, xw.win, TNAME);
750 xdrawc(int x, int y, Glyph g) {
751 XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
752 unsigned long xfg, xbg;
755 if(g.mode & ATreverse)
756 xfg = dc.col[g.bg], xbg = dc.col[g.fg];
758 xfg = dc.col[g.fg], xbg = dc.col[g.bg];
760 XSetForeground(xw.dis, dc.gc, xbg);
761 XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
763 XSetForeground(xw.dis, dc.gc, xfg);
764 XDrawString(xw.dis, xw.win, dc.gc, r.x, r.y+dc.font->ascent, &(g.c), 1);
765 if(g.mode & ATbold) /* XXX: bold hack (draw again at x+1) */
766 XDrawString(xw.dis, xw.win, dc.gc, r.x+1, r.y+dc.font->ascent, &(g.c), 1);
768 if(g.mode & ATunderline) {
769 r.y += dc.font->ascent + 1;
770 XDrawLine(xw.dis, xw.win, dc.gc, r.x, r.y, r.x+r.width-1, r.y);
778 Glyph g = {' ', ATnone, DefaultBG, DefaultCS, 0};
780 if(term.line[term.c.y][term.c.x].state & CRset)
781 g.c = term.line[term.c.y][term.c.x].c;
782 /* remove the old cursor */
783 if(term.line[oldy][oldx].state & CRset)
784 xdrawc(oldx, oldy, term.line[oldy][oldx]);
785 else xclear(oldx, oldy, oldx, oldy); /* XXX: maybe a bug */
786 if(mode == CSdraw && !term.c.hidden) {
787 xdrawc(term.c.x, term.c.y, g);
788 oldx = term.c.x, oldy = term.c.y;
794 draw(int redraw_all) {
799 XClearWindow(xw.dis, xw.win);
800 /* XXX: drawing could be optimised */
801 for(y = 0; y < term.row; y++) {
802 for(x = 0; x < term.col; x++) {
803 changed = term.line[y][x].state & CRupdate;
804 set = term.line[y][x].state & CRset;
805 if((changed && set) || (redraw_all && set)) {
806 term.line[y][x].state &= ~CRupdate;
807 xdrawc(x, y, term.line[y][x]);
815 kpress(XKeyEvent *e) {
822 meta = e->state & Mod4Mask;
823 shift = e->state & ShiftMask;
824 len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
826 buf[sizeof(buf)-1] = '\0';
835 printf("errkey: %d\n", (int)ksym);
842 sprintf(buf, "\033[%c", "DACB"[ksym - XK_Left]);
845 case XK_Delete: ttywrite(KEYDELETE, sizeof(KEYDELETE)-1); break;
846 case XK_Home: ttywrite( KEYHOME, sizeof( KEYHOME)-1); break;
847 case XK_End: ttywrite( KEYEND, sizeof( KEYEND)-1); break;
848 case XK_Prior: ttywrite( KEYPREV, sizeof( KEYPREV)-1); break;
849 case XK_Next: ttywrite( KEYNEXT, sizeof( KEYNEXT)-1); break;
851 /* XXX: paste X clipboard */
860 col = e->xconfigure.width / xw.cw;
861 row = e->xconfigure.height / xw.ch;
863 if(term.col != col && term.row != row) {
866 xw.w = e->xconfigure.width;
867 xw.h = e->xconfigure.height;
878 struct timeval tv = {0, 10000};
881 XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
882 XResizeWindow(xw.dis, xw.win, xw.w , xw.h); /* seems to fix the resize bug in wmii */
884 while(XPending(xw.dis)) {
885 XNextEvent(xw.dis, &ev);
895 case ConfigureNotify:
902 ret = select(cmdfd+1, &rfd, NULL, NULL, &tv);
904 fprintf(stderr, "select: %m\n");
909 if(FD_ISSET(cmdfd, &rfd)) {
917 main(int argc, char *argv[]) {
918 if(argc == 2 && !strncmp("-v", argv[1], 3))
919 die("st-" VERSION ", © 2009 st engineers\n");
921 die("usage: st [-v]\n");
922 setlocale(LC_CTYPE, "");