tiny code-style and typo-fix in comment
[st.git] / st.c
diff --git a/st.c b/st.c
index a8f8232..c161497 100644 (file)
--- a/st.c
+++ b/st.c
@@ -38,7 +38,7 @@
 
 /* macros */
 #define IS_SET(flag)           ((term.mode & (flag)) != 0)
-#define ISCONTROLC0(c)         (BETWEEN(c, 0, 0x1f) || (c) == '\177')
+#define ISCONTROLC0(c)         (BETWEEN(c, 0, 0x1f) || (c) == 0x7f)
 #define ISCONTROLC1(c)         (BETWEEN(c, 0x80, 0x9f))
 #define ISCONTROL(c)           (ISCONTROLC0(c) || ISCONTROLC1(c))
 #define ISDELIM(u)             (u && wcschr(worddelimiters, u))
@@ -135,7 +135,7 @@ typedef struct {
 /* ESC '[' [[ [<priv>] <arg> [;]] <mode> [<mode>]] */
 typedef struct {
        char buf[ESC_BUF_SIZ]; /* raw string */
-       int len;               /* raw string length */
+       size_t len;            /* raw string length */
        char priv;
        int arg[ESC_ARG_SIZ];
        int narg;              /* nb of args */
@@ -146,8 +146,9 @@ typedef struct {
 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
 typedef struct {
        char type;             /* ESC type ... */
-       char buf[STR_BUF_SIZ]; /* raw string */
-       int len;               /* raw string length */
+       char *buf;             /* allocated raw string */
+       size_t siz;            /* allocation size */
+       size_t len;            /* raw string length */
        char *args[STR_ARG_SIZ];
        int narg;              /* nb of args */
 } STREscape;
@@ -365,7 +366,8 @@ static const char base64_digits[] = {
 char
 base64dec_getc(const char **src)
 {
-       while (**src && !isprint(**src)) (*src)++;
+       while (**src && !isprint(**src))
+               (*src)++;
        return **src ? *((*src)++) : '=';  /* emulate padding if string ends */
 }
 
@@ -663,7 +665,7 @@ die(const char *errstr, ...)
 void
 execsh(char *cmd, char **args)
 {
-       char *sh, *prog;
+       char *sh, *prog, *arg;
        const struct passwd *pw;
 
        errno = 0;
@@ -677,13 +679,20 @@ execsh(char *cmd, char **args)
        if ((sh = getenv("SHELL")) == NULL)
                sh = (pw->pw_shell[0]) ? pw->pw_shell : cmd;
 
-       if (args)
+       if (args) {
                prog = args[0];
-       else if (utmp)
+               arg = NULL;
+       } else if (scroll) {
+               prog = scroll;
+               arg = utmp ? utmp : sh;
+       } else if (utmp) {
                prog = utmp;
-       else
+               arg = NULL;
+       } else {
                prog = sh;
-       DEFAULT(args, ((char *[]) {prog, NULL}));
+               arg = NULL;
+       }
+       DEFAULT(args, ((char *[]) {prog, arg, NULL}));
 
        unsetenv("COLUMNS");
        unsetenv("LINES");
@@ -721,7 +730,7 @@ sigchld(int a)
                die("child exited with status %d\n", WEXITSTATUS(stat));
        else if (WIFSIGNALED(stat))
                die("child terminated due to signal %d\n", WTERMSIG(stat));
-       exit(0);
+       _exit(0);
 }
 
 void
@@ -814,21 +823,26 @@ ttyread(void)
 {
        static char buf[BUFSIZ];
        static int buflen = 0;
-       int written;
-       int ret;
+       int ret, written;
 
        /* append read bytes to unprocessed bytes */
-       if ((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
-               die("couldn't read from shell: %s\n", strerror(errno));
-       buflen += ret;
+       ret = read(cmdfd, buf+buflen, LEN(buf)-buflen);
 
-       written = twrite(buf, buflen, 0);
-       buflen -= written;
-       /* keep any uncomplete utf8 char for the next call */
-       if (buflen > 0)
-               memmove(buf, buf + written, buflen);
+       switch (ret) {
+       case 0:
+               exit(0);
+       case -1:
+               die("couldn't read from shell: %s\n", strerror(errno));
+       default:
+               buflen += ret;
+               written = twrite(buf, buflen, 0);
+               buflen -= written;
+               /* keep any incomplete UTF-8 byte sequence for the next call */
+               if (buflen > 0)
+                       memmove(buf, buf + written, buflen);
+               return ret;
 
-       return ret;
+       }
 }
 
 void
@@ -1803,7 +1817,7 @@ csihandle(void)
 void
 csidump(void)
 {
-       int i;
+       size_t i;
        uint c;
 
        fprintf(stderr, "ESC[");
@@ -1921,7 +1935,7 @@ strparse(void)
 void
 strdump(void)
 {
-       int i;
+       size_t i;
        uint c;
 
        fprintf(stderr, "ESC%c", strescseq.type);
@@ -1948,7 +1962,10 @@ strdump(void)
 void
 strreset(void)
 {
-       memset(&strescseq, 0, sizeof(strescseq));
+       strescseq = (STREscape){
+               .buf = xrealloc(strescseq.buf, STR_BUF_SIZ),
+               .siz = STR_BUF_SIZ,
+       };
 }
 
 void
@@ -2006,7 +2023,7 @@ tdumpline(int n)
        bp = &term.line[n][0];
        end = &bp[MIN(tlinelen(n), term.col) - 1];
        if (bp != end || bp->u != ' ') {
-               for ( ;bp <= end; ++bp)
+               for ( ; bp <= end; ++bp)
                        tprinter(buf, utf8encode(bp->u, buf));
        }
        tprinter("\n", 1);
@@ -2290,7 +2307,7 @@ tputc(Rune u)
        Glyph *gp;
 
        control = ISCONTROL(u);
-       if (!IS_SET(MODE_UTF8) && !IS_SET(MODE_SIXEL)) {
+       if (!IS_SET(MODE_UTF8 | MODE_SIXEL)) {
                c[0] = u;
                width = len = 1;
        } else {
@@ -2330,7 +2347,7 @@ tputc(Rune u)
                if (term.esc&ESC_DCS && strescseq.len == 0 && u == 'q')
                        term.mode |= MODE_SIXEL;
 
-               if (strescseq.len+len >= sizeof(strescseq.buf)-1) {
+               if (strescseq.len+len >= strescseq.siz) {
                        /*
                         * Here is a bug in terminals. If the user never sends
                         * some code to stop the str or esc command, then st
@@ -2344,7 +2361,10 @@ tputc(Rune u)
                         * term.esc = 0;
                         * strhandle();
                         */
-                       return;
+                       if (strescseq.siz > (SIZE_MAX - UTF_SIZ) / 2)
+                               return;
+                       strescseq.siz *= 2;
+                       strescseq.buf = xrealloc(strescseq.buf, strescseq.siz);
                }
 
                memmove(&strescseq.buf[strescseq.len], c, len);
@@ -2551,6 +2571,7 @@ void
 drawregion(int x1, int y1, int x2, int y2)
 {
        int y;
+
        for (y = y1; y < y2; y++) {
                if (!term.dirty[y])
                        continue;
@@ -2563,7 +2584,7 @@ drawregion(int x1, int y1, int x2, int y2)
 void
 draw(void)
 {
-       int cx = term.c.x;
+       int cx = term.c.x, ocx = term.ocx, ocy = term.ocy;
 
        if (!xstartdraw())
                return;
@@ -2579,9 +2600,11 @@ draw(void)
        drawregion(0, 0, term.col, term.row);
        xdrawcursor(cx, term.c.y, term.line[term.c.y][cx],
                        term.ocx, term.ocy, term.line[term.ocy][term.ocx]);
-       term.ocx = cx, term.ocy = term.c.y;
+       term.ocx = cx;
+       term.ocy = term.c.y;
        xfinishdraw();
-       xximspot(term.ocx, term.ocy);
+       if (ocx != term.ocx || ocy != term.ocy)
+               xximspot(term.ocx, term.ocy);
 }
 
 void