X-Git-Url: https://git.xinqibao.xyz/st.git/blobdiff_plain/ad39f000be87ec16e74c05bdc3fa81ef307b5477..dab8326e6428a5db6b7a6e08f40fa1e5ac77bc6f:/st.c diff --git a/st.c b/st.c index 64a37e1..5f40ddd 100644 --- a/st.c +++ b/st.c @@ -1,14 +1,188 @@ /* See LICENSE for licence details. */ -#include "st.h" +#define _XOPEN_SOURCE 600 +#include <ctype.h> +#include <errno.h> +#include <fcntl.h> +#include <locale.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <signal.h> +#include <sys/ioctl.h> +#include <sys/select.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> +#include <X11/Xlib.h> +#include <X11/keysym.h> +#include <X11/Xutil.h> + +#define TNAME "st" + +/* Arbitrary sizes */ +#define ESCSIZ 256 +#define ESCARG 16 + +#define SERRNO strerror(errno) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) < (b) ? (b) : (a)) +#define LEN(a) (sizeof(a) / sizeof(a[0])) +#define DEFAULT(a, b) (a) = (a) ? (a) : (b) +#define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b)) +#define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x) + +/* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */ +enum { ATnone=0 , ATreverse=1 , ATunderline=2, ATbold=4 }; +enum { CSup, CSdown, CSright, CSleft, CShide, CSdraw, CSwrap, CSsave, CSload }; +enum { CRset=1, CRupdate=2 }; +enum { TMwrap=1, TMinsert=2 }; +enum { SCupdate, SCredraw }; + +typedef int Color; + +typedef struct { + char c; /* character code */ + char mode; /* attribute flags */ + Color fg; /* foreground */ + Color bg; /* background */ + char state; /* state flag */ +} Glyph; + +typedef Glyph* Line; + +typedef struct { + Glyph attr; /* current char attributes */ + char hidden; + int x; + int y; +} TCursor; + +/* Escape sequence structs */ +/* ESC <pre> [[ [<priv>] <arg> [;]] <mode>] */ +typedef struct { + char buf[ESCSIZ+1]; /* raw string */ + int len; /* raw string length */ + char pre; + char priv; + int arg[ESCARG+1]; + int narg; /* nb of args */ + char mode; +} Escseq; + +/* Internal representation of the screen */ +typedef struct { + int row; /* nb row */ + int col; /* nb col */ + Line* line; /* screen */ + TCursor c; /* cursor */ + int top; /* top scroll limit */ + int bot; /* bottom scroll limit */ + int mode; /* terminal mode */ +} Term; + +/* Purely graphic info */ +typedef struct { + Display* dis; + Window win; + int scr; + int w; /* window width */ + int h; /* window height */ + int ch; /* char height */ + int cw; /* char width */ +} XWindow; + +#include "config.h" + +/* Drawing Context */ +typedef struct { + unsigned long col[LEN(colorname)]; + XFontStruct* font; + GC gc; +} DC; + +static void die(const char *errstr, ...); +static void draw(int); +static void execsh(void); +static void sigchld(int); +static void run(void); + +static int escaddc(char); +static int escfinal(char); +static void escdump(void); +static void eschandle(void); +static void escparse(void); +static void escreset(void); + +static void tclearregion(int, int, int, int); +static void tcpos(int); +static void tcursor(int); +static void tdeletechar(int); +static void tdeleteline(int); +static void tinsertblank(int); +static void tinsertblankline(int); +static void tmoveto(int, int); +static void tnew(int, int); +static void tnewline(void); +static void tputc(char); +static void tputs(char*, int); +static void tresize(int, int); +static void tscroll(void); +static void tsetattr(int*, int); +static void tsetchar(char); +static void tsetscroll(int, int); + +static void ttynew(void); +static void ttyread(void); +static void ttyresize(int, int); +static void ttywrite(const char *, size_t); + +static unsigned long xgetcol(const char *); +static void xclear(int, int, int, int); +static void xcursor(int); +static void xdrawc(int, int, Glyph); +static void xinit(void); +static void xscroll(void); + +static void expose(XEvent *); +static void kpress(XEvent *); +static void resize(XEvent *); + +static void (*handler[LASTEvent])(XEvent *) = { + [KeyPress] = kpress, + [Expose] = expose, + [ConfigureNotify] = resize +}; /* Globals */ -DC dc; -XWindow xw; -Term term; -Escseq escseq; -int cmdfd; -pid_t pid; -int running; +static DC dc; +static XWindow xw; +static Term term; +static Escseq escseq; +static int cmdfd; +static pid_t pid; +static int running; + +#ifdef DEBUG +void +tdump(void) { + int row, col; + Glyph c; + + for(row = 0; row < term.row; row++) { + for(col = 0; col < term.col; col++) { + if(col == term.c.x && row == term.c.y) + putchar('#'); + else { + c = term.line[row][col]; + putchar(c.state & CRset ? c.c : '.'); + } + } + putchar('\n'); + } +} +#endif void die(const char *errstr, ...) { @@ -47,7 +221,6 @@ sigchld(int a) { exit(EXIT_FAILURE); } - void ttynew(void) { int m, s; @@ -107,7 +280,7 @@ ttyread(void) { } void -ttywrite(char *s, size_t n) { +ttywrite(const char *s, size_t n) { if(write(cmdfd, s, n) == -1) die("write error on tty: %s\n", SERRNO); } @@ -174,21 +347,19 @@ void tscroll(void) { Line temp = term.line[term.top]; int i; - + /* X stuff _before_ the line swapping (results in wrong line index) */ + xscroll(); for(i = term.top; i < term.bot; i++) term.line[i] = term.line[i+1]; memset(temp, 0, sizeof(Glyph) * term.col); term.line[term.bot] = temp; - xscroll(); } void tnewline(void) { int y = term.c.y + 1; - - if(y > term.bot) { + if(y > term.bot) tscroll(), y = term.bot; - } tmoveto(0, y); } @@ -322,6 +493,13 @@ tinsertblank(int n) { tclearregion(src, term.c.y, dst, term.c.y); } +void +tsetlinestate(int n, int state) { + int i; + for(i = 0; i < term.col; i++) + term.line[n][i].state |= state; +} + void tinsertblankline (int n) { int i; @@ -343,10 +521,11 @@ tinsertblankline (int n) { term.line[i-n] = blank; /* blank it */ memset(blank, 0, term.col * sizeof(Glyph)); + tsetlinestate(i, CRupdate); + tsetlinestate(i-n, CRupdate); } } - void tdeleteline(int n) { int i; @@ -368,6 +547,8 @@ tdeleteline(int n) { term.line[i+n] = blank; /* blank it */ memset(blank, 0, term.col * sizeof(Glyph)); + tsetlinestate(i, CRupdate); + tsetlinestate(i-n, CRupdate); } } @@ -375,48 +556,48 @@ void tsetattr(int *attr, int l) { int i; - for(i = 0; i < l; i++) { - switch(attr[i]) { - case 0: - memset(&term.c.attr, 0, sizeof(term.c.attr)); - term.c.attr.fg = DefaultFG; - term.c.attr.bg = DefaultBG; - break; - case 1: - term.c.attr.mode |= ATbold; - break; - case 4: - term.c.attr.mode |= ATunderline; - break; - case 7: - term.c.attr.mode |= ATreverse; - break; - case 8: - term.c.hidden = CShide; - break; - case 22: - term.c.attr.mode &= ~ATbold; - break; - case 24: - term.c.attr.mode &= ~ATunderline; - break; - case 27: - term.c.attr.mode &= ~ATreverse; - break; - case 39: - term.c.attr.fg = DefaultFG; - break; - case 49: - term.c.attr.fg = DefaultBG; - break; - default: - if(BETWEEN(attr[i], 30, 37)) - term.c.attr.fg = attr[i] - 30; - else if(BETWEEN(attr[i], 40, 47)) - term.c.attr.bg = attr[i] - 40; - break; - } + for(i = 0; i < l; i++) { + switch(attr[i]) { + case 0: + memset(&term.c.attr, 0, sizeof(term.c.attr)); + term.c.attr.fg = DefaultFG; + term.c.attr.bg = DefaultBG; + break; + case 1: + term.c.attr.mode |= ATbold; + break; + case 4: + term.c.attr.mode |= ATunderline; + break; + case 7: + term.c.attr.mode |= ATreverse; + break; + case 8: + term.c.hidden = CShide; + break; + case 22: + term.c.attr.mode &= ~ATbold; + break; + case 24: + term.c.attr.mode &= ~ATunderline; + break; + case 27: + term.c.attr.mode &= ~ATreverse; + break; + case 39: + term.c.attr.fg = DefaultFG; + break; + case 49: + term.c.attr.fg = DefaultBG; + break; + default: + if(BETWEEN(attr[i], 30, 37)) + term.c.attr.fg = attr[i] - 30; + else if(BETWEEN(attr[i], 40, 47)) + term.c.attr.bg = attr[i] - 40; + break; } + } } void @@ -434,13 +615,18 @@ tsetscroll(int t, int b) { term.bot = b; } - void -eschandle(void) { - /* escdump(); */ +eschandle(void) { switch(escseq.pre) { + default: + goto unknown_seq; case '[': switch(escseq.mode) { + default: + unknown_seq: + fprintf(stderr, "erresc: unknown sequence\n"); + escdump(); + break; case '@': /* Insert <n> blank char */ DEFAULT(escseq.arg[0], 1); tinsertblank(escseq.arg[0]); @@ -558,15 +744,13 @@ eschandle(void) { void escdump(void) { int i; - puts("------"); printf("rawbuf : %s\n", escseq.buf); printf("prechar : %c\n", escseq.pre); printf("private : %c\n", escseq.priv ? '?' : ' '); printf("narg : %d\n", escseq.narg); - if(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); } @@ -627,24 +811,6 @@ tputs(char *s, int len) { tputc(*s++); } -void -tdump(void) { - int row, col; - Glyph c; - - for(row = 0; row < term.row; row++) { - for(col = 0; col < term.col; col++) { - if(col == term.c.x && row == term.c.y) - putchar('#'); - else { - c = term.line[row][col]; - putchar(c.state & CRset ? c.c : '.'); - } - } - putchar('\n'); - } -} - void tresize(int col, int row) { int i; @@ -688,7 +854,6 @@ xgetcol(const char *s) { return color.pixel; } - void xclear(int x1, int y1, int x2, int y2) { XClearArea(xw.dis, xw.win, @@ -697,7 +862,6 @@ xclear(int x1, int y1, int x2, int y2) { False); } - void xscroll(void) { int srcy = (term.top+1) * xw.ch; @@ -709,9 +873,6 @@ xscroll(void) { xclear(0, term.bot, term.col-1, term.bot); } - - - void xinit(void) { XGCValues values; @@ -725,11 +886,11 @@ xinit(void) { xw.dis = XOpenDisplay(NULL); xw.scr = XDefaultScreen(xw.dis); if(!xw.dis) - die("can not open display"); + die("Can't open display\n"); /* font */ if(!(dc.font = XLoadQueryFont(xw.dis, FONT))) - die("can not find font " FONT); + die("Can't load font %s\n", FONT); xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing; xw.ch = dc.font->ascent + dc.font->descent + LINESPACE; @@ -805,14 +966,15 @@ xcursor(int mode) { /* remove the old cursor */ if(term.line[oldy][oldx].state & CRset) xdrawc(oldx, oldy, term.line[oldy][oldx]); - else xclear(oldx, oldy, oldx, oldy); /* XXX: maybe a bug */ - if(mode == CSdraw && !term.c.hidden) { + else + xclear(oldx, oldy, oldx, oldy); + /* draw the new one */ + if(mode == CSdraw) { xdrawc(term.c.x, term.c.y, g); oldx = term.c.x, oldy = term.c.y; } } - void draw(int redraw_all) { int x, y; @@ -820,14 +982,18 @@ draw(int redraw_all) { if(redraw_all) XClearWindow(xw.dis, xw.win); + /* XXX: drawing could be optimised */ for(y = 0; y < term.row; y++) { for(x = 0; x < term.col; x++) { changed = term.line[y][x].state & CRupdate; set = term.line[y][x].state & CRset; - if((changed && set) || (redraw_all && set)) { + if(redraw_all || changed) { term.line[y][x].state &= ~CRupdate; - xdrawc(x, y, term.line[y][x]); + if(set) + xdrawc(x, y, term.line[y][x]); + else + xclear(x, y, x, y); } } } @@ -835,7 +1001,13 @@ draw(int redraw_all) { } void -kpress(XKeyEvent *e) { +expose(XEvent *ev) { + draw(SCredraw); +} + +void +kpress(XEvent *ev) { + XKeyEvent *e = &ev->xkey; KeySym ksym; char buf[32]; int len; @@ -845,35 +1017,23 @@ kpress(XKeyEvent *e) { meta = e->state & Mod1Mask; shift = e->state & ShiftMask; len = XLookupString(e, buf, sizeof(buf), &ksym, NULL); - if(len > 0) { + if(key[ksym]) + ttywrite(key[ksym], strlen(key[ksym])); + else if(len > 0) { buf[sizeof(buf)-1] = '\0'; if(meta && len == 1) ttywrite("\033", 1); ttywrite(buf, len); - return; - } - switch(ksym) { - default: - fprintf(stderr, "errkey: %d\n", (int)ksym); - break; - case XK_Up: - case XK_Down: - case XK_Left: - case XK_Right: - sprintf(buf, "\033[%c", "DACB"[ksym - XK_Left]); - 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_Insert: - /* XXX: paste X clipboard */ - if(shift) - ; - break; - } + } else + switch(ksym) { + case XK_Insert: + if(shift) + /* XXX: paste X clipboard */; + break; + default: + fprintf(stderr, "errkey: %d\n", (int)ksym); + break; + } } void @@ -891,10 +1051,8 @@ resize(XEvent *e) { } } - void run(void) { - int ret; XEvent ev; fd_set rfd; int xfd = XConnectionNumber(xw.dis); @@ -902,39 +1060,25 @@ run(void) { running = 1; XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask); XResizeWindow(xw.dis, xw.win, xw.w , xw.h); /* fix resize bug in wmii (?) */ - + while(running) { FD_ZERO(&rfd); FD_SET(cmdfd, &rfd); FD_SET(xfd, &rfd); - XFlush(xw.dis); - ret = select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL); - - if(ret < 0) + if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) == -1) { + if(errno == EINTR) + continue; 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(FD_ISSET(cmdfd, &rfd)) { ttyread(); draw(SCupdate); } + while(XPending(xw.dis)) { + XNextEvent(xw.dis, &ev); + if(handler[ev.type]) + (handler[ev.type])(&ev); + } } }