JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
add break;s for last cases in switch statements
[st.git] / st.c
diff --git a/st.c b/st.c
index 2741ef8..49df792 100644 (file)
--- a/st.c
+++ b/st.c
@@ -149,7 +149,7 @@ enum charset {
 enum escape_state {
        ESC_START      = 1,
        ESC_CSI        = 2,
-       ESC_STR        = 4,  /* DSC, OSC, PM, APC */
+       ESC_STR        = 4,  /* DCS, OSC, PM, APC */
        ESC_ALTCHARSET = 8,
        ESC_STR_END    = 16, /* a final string was encountered */
        ESC_TEST       = 32, /* Enter in test mode */
@@ -375,7 +375,7 @@ static void tmoveto(int, int);
 static void tmoveato(int, int);
 static void tnew(int, int);
 static void tnewline(int);
-static void tputtab(bool);
+static void tputtab(int);
 static void tputc(char *, int);
 static void treset(void);
 static int tresize(int, int);
@@ -452,7 +452,7 @@ static char utf8encodebyte(long, size_t);
 static size_t utf8len(char *);
 static size_t utf8validate(long *, size_t);
 
-static ssize_t xwrite(int, char *, size_t);
+static ssize_t xwrite(int, const char *, size_t);
 static void *xmalloc(size_t);
 static void *xrealloc(void *, size_t);
 static char *xstrdup(char *);
@@ -518,7 +518,7 @@ static Fontcache frc[16];
 static int frclen = 0;
 
 ssize_t
-xwrite(int fd, char *s, size_t len) {
+xwrite(int fd, const char *s, size_t len) {
        size_t aux = len;
 
        while(len > 0) {
@@ -992,7 +992,7 @@ selnotify(XEvent *e) {
                }
 
                /*
-                * As seen in selcopy:
+                * As seen in getsel:
                 * Line endings are inconsistent in the terminal and GUI world
                 * copy and pasting. When receiving some selection data,
                 * replace all '\n' with '\r'.
@@ -1228,6 +1228,7 @@ ttynew(void) {
                                        opt_io, strerror(errno));
                        }
                }
+               break;
        }
 }
 
@@ -1270,7 +1271,7 @@ ttyread(void) {
 
 void
 ttywrite(const char *s, size_t n) {
-       if(write(cmdfd, s, n) == -1)
+       if(xwrite(cmdfd, s, n) == -1)
                die("write error on tty: %s\n", strerror(errno));
 }
 
@@ -1673,6 +1674,7 @@ tdefcolor(int *attr, int *npar, int l) {
        default:
                fprintf(stderr,
                        "erresc(38): gfx attr %d unknown\n", attr[*npar]);
+               break;
        }
 
        return idx;
@@ -1781,7 +1783,6 @@ tsetmode(bool priv, bool set, int *args, int narg) {
        for(lim = args + narg; args < lim; ++args) {
                if(priv) {
                        switch(*args) {
-                               break;
                        case 1: /* DECCKM -- Cursor key */
                                MODBIT(term.mode, set, MODE_APPCURSOR);
                                break;
@@ -1996,8 +1997,7 @@ csihandle(void) {
                break;
        case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
                DEFAULT(csiescseq.arg[0], 1);
-               while(csiescseq.arg[0]--)
-                       tputtab(1);
+               tputtab(csiescseq.arg[0]);
                break;
        case 'J': /* ED -- Clear screen */
                selclear(NULL);
@@ -2065,8 +2065,7 @@ csihandle(void) {
                break;
        case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
                DEFAULT(csiescseq.arg[0], 1);
-               while(csiescseq.arg[0]--)
-                       tputtab(0);
+               tputtab(-csiescseq.arg[0]);
                break;
        case 'd': /* VPA -- Move to <row> */
                DEFAULT(csiescseq.arg[0], 1);
@@ -2172,7 +2171,7 @@ strhandle(void) {
        case 'k': /* old title set compatibility */
                xsettitle(strescseq.args[0]);
                return;
-       case 'P': /* DSC -- Device Control String */
+       case 'P': /* DCS -- Device Control String */
        case '_': /* APC -- Application Program Command */
        case '^': /* PM -- Privacy Message */
                return;
@@ -2281,19 +2280,17 @@ tdump(void) {
 }
 
 void
-tputtab(bool forward) {
+tputtab(int n) {
        uint x = term.c.x;
 
-       if(forward) {
-               if(x == term.col)
-                       return;
-               for(++x; x < term.col && !term.tabs[x]; ++x)
-                       /* nothing */ ;
-       } else {
-               if(x == 0)
-                       return;
-               for(--x; x > 0 && !term.tabs[x]; --x)
-                       /* nothing */ ;
+       if(n > 0) {
+               while(x < term.col && n--)
+                       for(++x; x < term.col && !term.tabs[x]; ++x)
+                               /* nothing */ ;
+       } else if(n < 0) {
+               while(x > 0 && n++)
+                       for(--x; x > 0 && !term.tabs[x]; --x)
+                               /* nothing */ ;
        }
        tmoveto(x, term.c.y);
 }
@@ -2303,7 +2300,7 @@ techo(char *buf, int len) {
        for(; len > 0; buf++, len--) {
                char c = *buf;
 
-               if(c < 0x20 || c == 0177) { /* control code */
+               if(BETWEEN(c, 0x00, 0x1f) || c == 0x7f) { /* control code */
                        if(c != '\n' && c != '\r' && c != '\t') {
                                c ^= '\x40';
                                tputc("^", 1);
@@ -2392,6 +2389,7 @@ tputc(char *c, int len) {
                         * strhandle();
                         */
                        }
+                       break;
                }
                return;
        }
@@ -2457,10 +2455,6 @@ tputc(char *c, int len) {
                                csiparse();
                                csihandle();
                        }
-               } else if(term.esc & ESC_STR_END) {
-                       term.esc = 0;
-                       if(ascii == '\\')
-                               strhandle();
                } else if(term.esc & ESC_ALTCHARSET) {
                        tdeftran(ascii);
                        tselcs();
@@ -2550,13 +2544,16 @@ tputc(char *c, int len) {
                                tcursor(CURSOR_LOAD);
                                term.esc = 0;
                                break;
-                       case '\\': /* ST -- Stop */
+                       case '\\': /* ST -- String Terminator */
+                               if(term.esc & ESC_STR_END)
+                                       strhandle();
                                term.esc = 0;
                                break;
                        default:
                                fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
                                        (uchar) ascii, isprint(ascii)? ascii:'.');
                                term.esc = 0;
+                               break;
                        }
                }
                /*
@@ -2678,7 +2675,9 @@ tresize(int col, int row) {
                if(0 < col && minrow < row) {
                        tclearregion(0, minrow, col - 1, row - 1);
                }
+               tcursor(CURSOR_SAVE);
                tswapscreen();
+               tcursor(CURSOR_LOAD);
        } while(orig != term.line);
 
        return (slide > 0);
@@ -2751,10 +2750,10 @@ int
 xsetcolorname(int x, const char *name) {
        XRenderColor color = { .alpha = 0xffff };
        Colour colour;
-       if (x < 0 || x > LEN(colorname))
+       if(!BETWEEN(x, 0, LEN(colorname)))
                return -1;
        if(!name) {
-               if(16 <= x && x < 16 + 216) {
+               if(BETWEEN(x, 16, 16 + 215)) {
                        int r = (x - 16) / 36, g = ((x - 16) % 36) / 6, b = (x - 16) % 6;
                        color.red = sixd_to_16bit(r);
                        color.green = sixd_to_16bit(g);
@@ -2763,7 +2762,7 @@ xsetcolorname(int x, const char *name) {
                                return 0; /* something went wrong */
                        dc.col[x] = colour;
                        return 1;
-               } else if (16 + 216 <= x && x < 256) {
+               } else if(BETWEEN(x, 16 + 216, 255)) {
                        color.red = color.green = color.blue = 0x0808 + 0x0a0a * (x - (16 + 216));
                        if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color, &colour))
                                return 0; /* something went wrong */
@@ -3457,7 +3456,7 @@ drawregion(int x1, int y1, int x2, int y2) {
        bool ena_sel = sel.ob.x != -1;
        long unicodep;
 
-       if(sel.alt ^ IS_SET(MODE_ALTSCREEN))
+       if(sel.alt != IS_SET(MODE_ALTSCREEN))
                ena_sel = 0;
 
        if(!(xw.state & WIN_VISIBLE))