X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=st.c;h=eabc0c585c334b17050cc40a0f505f03ec514880;hb=c186c8ef9a995616384c425c5568b6913676b252;hp=9667dbdb509c12b96724c2c15f7cedd42d491965;hpb=42b2912e2151f02e181bd014ce1610f7e03a7d07;p=st.git diff --git a/st.c b/st.c index 9667dbd..eabc0c5 100644 --- a/st.c +++ b/st.c @@ -20,6 +20,14 @@ #include #include +#if defined(LINUX) + #include +#elif defined(OPENBSD) + #include +#elif defined(FREEBSD) + #include +#endif + /* Arbitrary sizes */ #define ESC_TITLE_SIZ 256 #define ESC_BUF_SIZ 256 @@ -34,7 +42,7 @@ #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b)) #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x) #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg) -#define IS_SET(flag) (term.mode & flag) +#define IS_SET(flag) (term.mode & (flag)) /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */ enum { ATTR_NULL=0 , ATTR_REVERSE=1 , ATTR_UNDERLINE=2, ATTR_BOLD=4, ATTR_GFX=8 }; @@ -129,7 +137,6 @@ static void csireset(void); static void tclearregion(int, int, int, int); static void tcursor(int); -static void twrapcursor(void); static void tdeletechar(int); static void tdeleteline(int); static void tinsertblank(int); @@ -142,7 +149,6 @@ static void tputc(char); static void tputs(char*, int); static void treset(void); static void tresize(int, int); -static void tscroll(void); static void tscrollup(int); static void tscrolldown(int); static void tsetattr(int*, int); @@ -243,19 +249,12 @@ sigchld(int a) { void ttynew(void) { int m, s; - char *pts; - - if((m = posix_openpt(O_RDWR | O_NOCTTY)) < 0) - die("openpt failed: %s\n", SERRNO); - if(grantpt(m) < 0) - die("grantpt 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("Couldn't open slave: %s\n", SERRNO); - fcntl(s, F_SETFL, O_NDELAY); + + /* seems to work fine on linux, openbsd and freebsd */ + struct winsize w = {term.row, term.col, 0, 0}; + if(openpty(&m, &s, NULL, NULL, &w) < 0) + die("openpty failed: %s\n", SERRNO); + switch(pid = fork()) { case -1: die("fork failed\n"); @@ -337,7 +336,8 @@ treset(void) { } void -tnew(int col, int row) { /* screen size */ +tnew(int col, int row) { + /* screen size */ term.row = row, term.col = col; term.top = 0, term.bot = term.row - 1; /* mode */ @@ -354,18 +354,6 @@ tnew(int col, int row) { /* screen size */ term.line[row] = calloc(term.col, sizeof(Glyph)); } -/* TODO: Replace with scrollup/scolldown */ -void -tscroll(void) { - Line temp = term.line[term.top]; - int i; - - 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; -} - void tscrolldown (int n) { int i; @@ -403,7 +391,7 @@ void tnewline(void) { int y = term.c.y + 1; if(y > term.bot) - tscroll(), y = term.bot; + tscrollup(1), y = term.bot; tmoveto(0, y); } @@ -437,16 +425,6 @@ tmoveto(int x, int y) { term.c.y = y < 0 ? 0 : y >= term.row ? term.row-1 : y; } -void -twrapcursor(void) { - int y = term.c.y+1; - if(y > term.bot) { - tmoveto(0, term.bot); - tscroll(); - } else - tmoveto(0, y); -} - void tsetchar(char c) { term.line[term.c.y][term.c.x] = term.c.attr; @@ -974,7 +952,7 @@ tputc(char c) { if(term.c.x+1 < term.col) { tmoveto(term.c.x+1, term.c.y); } else if(IS_SET(MODE_WRAP)) - twrapcursor(); + tnewline(); break; } } @@ -995,13 +973,20 @@ tresize(int col, int row) { if(col < 1 || row < 1) return; + /* free uneeded rows */ for(i = row; i < term.row; i++) free(term.line[i]); + + /* resize to new height */ term.line = realloc(term.line, row * sizeof(Line)); + + /* resize each row to new width, zero-pad if needed */ for(i = 0; i < minrow; i++) { term.line[i] = realloc(term.line[i], col * sizeof(Glyph)); memset(term.line[i] + mincol, 0, (col - mincol) * sizeof(Glyph)); } + + /* allocate any new rows */ for(/* i == minrow */; i < row; i++) term.line[i] = calloc(col, sizeof(Glyph)); @@ -1081,10 +1066,9 @@ xhints(void) void xinit(void) { - xw.dis = XOpenDisplay(NULL); - xw.scr = XDefaultScreen(xw.dis); - if(!xw.dis) + if(!(xw.dis = XOpenDisplay(NULL))) die("Can't open display\n"); + xw.scr = XDefaultScreen(xw.dis); /* font */ if(!(dc.font = XLoadQueryFont(xw.dis, FONT)) || !(dc.bfont = XLoadQueryFont(xw.dis, BOLDFONT))) @@ -1337,7 +1321,7 @@ run(void) { int main(int argc, char *argv[]) { if(argc == 2 && !strncmp("-v", argv[1], 3)) - die("st-" VERSION ", © 2009 st engineers\n"); + die("st-" VERSION ", (c) 2010 st engineers\n"); else if(argc != 1) die("usage: st [-v]\n"); setlocale(LC_CTYPE, "");