X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=st.c;h=64a37e151f59486594688c3e828a2f06b60ba35b;hb=ad39f000be87ec16e74c05bdc3fa81ef307b5477;hp=2318435a4dd7302482bc0e9ffcb5d585898b7588;hpb=86fa756033e9072a4b114a50896baff36b804ce4;p=st.git diff --git a/st.c b/st.c index 2318435..64a37e1 100644 --- a/st.c +++ b/st.c @@ -1,4 +1,4 @@ -/* See LICENSE for licence details. */ +/* See LICENSE for licence details. */ #include "st.h" /* Globals */ @@ -7,6 +7,7 @@ XWindow xw; Term term; Escseq escseq; int cmdfd; +pid_t pid; int running; void @@ -27,35 +28,45 @@ execsh(void) { } void -xbell(void) { /* visual bell */ +xbell(void) { /* visual bell */ XRectangle r = { 0, 0, xw.w, xw.h }; XSetForeground(xw.dis, dc.gc, dc.col[BellCol]); XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1); - XFlush(xw.dis); - usleep(30000); + /* usleep(30000); */ draw(SCredraw); } +void +sigchld(int a) { + int stat = 0; + if(waitpid(pid, &stat, 0) < 0) + die("Waiting for pid %hd failed: %s\n", pid, SERRNO); + if(WIFEXITED(stat)) + exit(WEXITSTATUS(stat)); + else + exit(EXIT_FAILURE); +} + + void ttynew(void) { int m, s; - pid_t pid; char *pts; if((m = posix_openpt(O_RDWR | O_NOCTTY)) < 0) - die("openpt"); - if(grantpt(m) == -1) - die("grandpt"); - if(unlockpt(m) == -1) - die("unlockpt"); - if((pts = ptsname(m)) == NULL) - die("ptsname"); + die("openpt failed: %s\n", SERRNO); + if(grantpt(m) < 0) + die("grandpt failed: %s\n", SERRNO); + if(unlockpt(m) < 0) + die("unlockpt failed: %s\n", SERRNO); + if(!(pts = ptsname(m))) + die("ptsname failed: %s\n", SERRNO); if((s = open(pts, O_RDWR | O_NOCTTY)) < 0) - die("slave open"); + die("Couldn't open slave: %s\n", SERRNO); fcntl(s, F_SETFL, O_NDELAY); switch(pid = fork()) { case -1: - die("fork"); + die("fork failed\n"); break; case 0: setsid(); /* create a new process group */ @@ -63,12 +74,13 @@ ttynew(void) { dup2(s, STDOUT_FILENO); dup2(s, STDERR_FILENO); if(ioctl(s, TIOCSCTTY, NULL) < 0) - die("slave TTIOCSTTY"); + die("ioctl TTIOCSTTY failed: %s\n", SERRNO); execsh(); break; default: close(s); cmdfd = m; + signal(SIGCHLD, sigchld); } } @@ -86,9 +98,8 @@ ttyread(void) { int ret; switch(ret = read(cmdfd, buf, BUFSIZ)) { - case -1: /* error or exit */ - /* XXX: be more precise */ - running = 0; + case -1: + die("Couldn't read from shell: %s\n", SERRNO); break; default: tputs(buf, ret); @@ -98,7 +109,7 @@ ttyread(void) { void ttywrite(char *s, size_t n) { if(write(cmdfd, s, n) == -1) - die("write error on tty."); + die("write error on tty: %s\n", SERRNO); } void @@ -109,7 +120,7 @@ ttyresize(int x, int y) { w.ws_col = term.col; w.ws_xpixel = w.ws_ypixel = 0; if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0) - fprintf(stderr, "Couldn't set window size: %m\n"); + fprintf(stderr, "Couldn't set window size: %s\n", SERRNO); } int @@ -127,7 +138,7 @@ escfinal(char c) { } else if(BETWEEN(c, 0x40, 0x7E)) return 1; - return 0; + return 0; } void @@ -168,7 +179,7 @@ tscroll(void) { term.line[i] = term.line[i+1]; memset(temp, 0, sizeof(Glyph) * term.col); term.line[term.bot] = temp; - xscroll(); + xscroll(); } void @@ -364,21 +375,6 @@ void tsetattr(int *attr, int l) { int i; -#ifdef TRUECOLOR /* ESC [ ? ; ; ; m */ - Color col; - if(escseq.priv && escseq.len == 4) { /* True color extension :) */ - col = (escseq.arg[1]<<16) + (escseq.arg[2]<<8) + escseq.arg[3]; - switch(escseq.arg[0]) { - case 3: /* foreground */ - term.c.attr.fg = col; - break; - case 4: /* background */ - term.c.attr.bg = col; - break; - } - } - else -#endif for(i = 0; i < l; i++) { switch(attr[i]) { case 0: @@ -387,13 +383,13 @@ tsetattr(int *attr, int l) { term.c.attr.bg = DefaultBG; break; case 1: - term.c.attr.mode |= ATbold; + term.c.attr.mode |= ATbold; break; case 4: term.c.attr.mode |= ATunderline; break; case 7: - term.c.attr.mode |= ATreverse; + term.c.attr.mode |= ATreverse; break; case 8: term.c.hidden = CShide; @@ -405,7 +401,7 @@ tsetattr(int *attr, int l) { term.c.attr.mode &= ~ATunderline; break; case 27: - term.c.attr.mode &= ~ATreverse; + term.c.attr.mode &= ~ATreverse; break; case 39: term.c.attr.fg = DefaultFG; @@ -435,7 +431,7 @@ tsetscroll(int t, int b) { b = temp; } term.top = t; - term.bot = b; + term.bot = b; } @@ -496,7 +492,7 @@ eschandle(void) { break; case 2: /* all */ tclearregion(0, 0, term.col-1, term.row-1); - break; + break; } break; case 'K': /* Clear line */ @@ -516,6 +512,10 @@ eschandle(void) { DEFAULT(escseq.arg[0], 1); tinsertblankline(escseq.arg[0]); break; + case 'l': + if(escseq.priv && escseq.arg[0] == 25) + term.c.hidden = 1; + break; case 'M': /* Delete lines */ DEFAULT(escseq.arg[0], 1); tdeleteline(escseq.arg[0]); @@ -529,6 +529,8 @@ eschandle(void) { tmoveto(term.c.x, escseq.arg[0]-1); break; case 'h': /* Set terminal mode */ + if(escseq.priv && escseq.arg[0] == 25) + term.c.hidden = 0; break; case 'm': /* Terminal attribute (color) */ tsetattr(escseq.arg, escseq.narg); @@ -557,15 +559,15 @@ void escdump(void) { int i; puts("------"); - printf("rawbuf : %s\n", escseq.buf); + printf("rawbuf : %s\n", escseq.buf); printf("prechar : %c\n", escseq.pre); printf("private : %c\n", escseq.priv ? '?' : ' '); - printf("narg : %d\n", escseq.narg); + printf("narg : %d\n", escseq.narg); if(escseq.narg) { for(i = 0; i < escseq.narg; i++) printf("\targ %d = %d\n", i, escseq.arg[i]); } - printf("mode : %c\n", escseq.mode); + printf("mode : %c\n", escseq.mode); } void @@ -573,11 +575,23 @@ escreset(void) { memset(&escseq, 0, sizeof(escseq)); } +void +tputtab(void) { + int space = TAB - term.c.x % TAB; + + if(term.c.x + space >= term.col) + space--; + + for(; space > 0; space--) + tcursor(CSright); +} + void tputc(char c) { static int inesc = 0; - +#if 0 dump(c); +#endif /* start of escseq */ if(c == '\033') escreset(), inesc = 1; @@ -589,6 +603,9 @@ tputc(char c) { tsetchar(c); tcursor(CSright); break; + case '\t': + tputtab(); + break; case '\b': tcursor(CSleft); break; @@ -637,19 +654,23 @@ tresize(int col, int row) { if(col < 1 || row < 1) return; + /* alloc */ line = calloc(row, sizeof(Line)); for(i = 0 ; i < row; i++) line[i] = calloc(col, sizeof(Glyph)); - for(i = 0 ; i < minrow; i++) { + /* copy */ + for(i = 0 ; i < minrow; i++) memcpy(line[i], term.line[i], mincol * sizeof(Glyph)); + /* free */ + for(i = 0; i < term.row; i++) free(term.line[i]); - } free(term.line); + LIMIT(term.c.x, 0, col-1); LIMIT(term.c.y, 0, row-1); LIMIT(term.top, 0, row-1); LIMIT(term.bot, 0, row-1); - // if(term.bot == term.row-1) + term.bot = row-1; term.line = line; term.col = col, term.row = row; @@ -703,12 +724,12 @@ xinit(void) { xw.dis = XOpenDisplay(NULL); xw.scr = XDefaultScreen(xw.dis); - if(!xw.dis) - die("can not open display"); - + if(!xw.dis) + die("can not open display"); + /* font */ if(!(dc.font = XLoadQueryFont(xw.dis, FONT))) - die("can not find font " FONT); + die("can not find font " FONT); xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing; xw.ch = dc.font->ascent + dc.font->descent + LINESPACE; @@ -721,7 +742,7 @@ xinit(void) { term.c.attr.bg = DefaultBG; term.c.attr.mode = ATnone; /* windows */ - xw.h = term.row * xw.ch; + xw.h = term.row * xw.ch; xw.w = term.col * xw.cw; /* XXX: this BORDER is useless after the first resize, handle it in xdraws() */ xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0, @@ -743,7 +764,6 @@ xinit(void) { XSetWMProperties(xw.dis, xw.win, NULL, NULL, &args[0], 0, &shint, &wmhint, &chint); XStoreName(xw.dis, xw.win, TNAME); XSync(xw.dis, 0); - } void @@ -762,7 +782,7 @@ xdrawc(int x, int y, Glyph g) { /* string */ XSetForeground(xw.dis, dc.gc, xfg); XDrawString(xw.dis, xw.win, dc.gc, r.x, r.y+dc.font->ascent, &(g.c), 1); - if(g.mode & ATbold) /* XXX: bold hack (draw again at x+1) */ + if(g.mode & ATbold) /* XXX: bold hack (draw again at x+1) */ XDrawString(xw.dis, xw.win, dc.gc, r.x+1, r.y+dc.font->ascent, &(g.c), 1); /* underline */ if(g.mode & ATunderline) { @@ -776,7 +796,10 @@ xcursor(int mode) { static int oldx = 0; static int oldy = 0; Glyph g = {' ', ATnone, DefaultBG, DefaultCS, 0}; - + + LIMIT(oldx, 0, term.col-1); + LIMIT(oldy, 0, term.row-1); + if(term.line[term.c.y][term.c.x].state & CRset) g.c = term.line[term.c.y][term.c.x].c; /* remove the old cursor */ @@ -819,7 +842,7 @@ kpress(XKeyEvent *e) { int meta; int shift; - meta = e->state & Mod4Mask; + meta = e->state & Mod1Mask; shift = e->state & ShiftMask; len = XLookupString(e, buf, sizeof(buf), &ksym, NULL); if(len > 0) { @@ -830,11 +853,9 @@ kpress(XKeyEvent *e) { return; } switch(ksym) { -#ifdef DEBUG1 default: - printf("errkey: %d\n", (int)ksym); + fprintf(stderr, "errkey: %d\n", (int)ksym); break; -#endif case XK_Up: case XK_Down: case XK_Left: @@ -843,13 +864,14 @@ kpress(XKeyEvent *e) { ttywrite(buf, 3); break; case XK_Delete: ttywrite(KEYDELETE, sizeof(KEYDELETE)-1); break; - case XK_Home: ttywrite( KEYHOME, sizeof( KEYHOME)-1); break; - case XK_End: ttywrite( KEYEND, sizeof( KEYEND)-1); break; - case XK_Prior: ttywrite( KEYPREV, sizeof( KEYPREV)-1); break; - case XK_Next: ttywrite( KEYNEXT, sizeof( KEYNEXT)-1); break; + case XK_Home: ttywrite(KEYHOME, sizeof(KEYHOME)-1); break; + case XK_End: ttywrite(KEYEND, sizeof(KEYEND) -1); break; + case XK_Prior: ttywrite(KEYPREV, sizeof(KEYPREV)-1); break; + case XK_Next: ttywrite(KEYNEXT, sizeof(KEYNEXT)-1); break; case XK_Insert: /* XXX: paste X clipboard */ - if(shift); + if(shift) + ; break; } } @@ -859,8 +881,8 @@ resize(XEvent *e) { int col, row; col = e->xconfigure.width / xw.cw; row = e->xconfigure.height / xw.ch; - - if(term.col != col && term.row != row) { + + if(term.col != col || term.row != row) { tresize(col, row); ttyresize(col, row); xw.w = e->xconfigure.width; @@ -875,37 +897,40 @@ run(void) { int ret; XEvent ev; fd_set rfd; - struct timeval tv = {0, 10000}; + int xfd = XConnectionNumber(xw.dis); running = 1; XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask); - XResizeWindow(xw.dis, xw.win, xw.w , xw.h); /* seems to fix the resize bug in wmii */ + XResizeWindow(xw.dis, xw.win, xw.w , xw.h); /* fix resize bug in wmii (?) */ + while(running) { - while(XPending(xw.dis)) { - XNextEvent(xw.dis, &ev); - switch (ev.type) { - default: - break; - case KeyPress: - kpress(&ev.xkey); - break; - case Expose: - draw(SCredraw); - break; - case ConfigureNotify: - resize(&ev); - break; - } - } FD_ZERO(&rfd); FD_SET(cmdfd, &rfd); - ret = select(cmdfd+1, &rfd, NULL, NULL, &tv); - if(ret < 0) { - fprintf(stderr, "select: %m\n"); - running = 0; + FD_SET(xfd, &rfd); + XFlush(xw.dis); + ret = select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL); + + if(ret < 0) + die("select failed: %s\n", SERRNO); + + if(FD_ISSET(xfd, &rfd)) { + while(XPending(xw.dis)) { + XNextEvent(xw.dis, &ev); + switch (ev.type) { + default: + break; + case KeyPress: + kpress(&ev.xkey); + break; + case Expose: + draw(SCredraw); + break; + case ConfigureNotify: + resize(&ev); + break; + } + } } - if(!ret) - continue; if(FD_ISSET(cmdfd, &rfd)) { ttyread(); draw(SCupdate); @@ -920,9 +945,9 @@ main(int argc, char *argv[]) { else if(argc != 1) die("usage: st [-v]\n"); setlocale(LC_CTYPE, ""); - tnew(80, 24); - ttynew(); - xinit(); - run(); + tnew(80, 24); + ttynew(); + xinit(); + run(); return 0; }