JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Fix crash due to wide characters
[st.git] / st.c
diff --git a/st.c b/st.c
index 23dd7f1..6a68c3c 100644 (file)
--- a/st.c
+++ b/st.c
@@ -947,6 +947,8 @@ getsel(void) {
                        lastx = (sel.ne.y == y) ? sel.ne.x : term.col-1;
                }
                last = &term.line[y][MIN(lastx, linelen-1)];
+               while(last >= gp && last->c[0] == ' ')
+                       --last;
 
                for( ; gp <= last; ++gp) {
                        if(gp->mode & ATTR_WDUMMY)
@@ -1158,7 +1160,10 @@ execsh(void) {
                        die("who are you?\n");
        }
 
-       sh = (pw->pw_shell[0]) ? pw->pw_shell : shell;
+       if (!(sh = getenv("SHELL"))) {
+               sh = (pw->pw_shell[0]) ? pw->pw_shell : shell;
+       }
+
        if(opt_cmd)
                prog = opt_cmd[0];
        else if(utmp)
@@ -1187,7 +1192,7 @@ execsh(void) {
        signal(SIGALRM, SIG_DFL);
 
        execvp(prog, args);
-       exit(EXIT_FAILURE);
+       _exit(EXIT_FAILURE);
 }
 
 void
@@ -2576,7 +2581,10 @@ tputc(char *c, int len) {
                unicodep = ascii = *c;
        } else {
                utf8decode(c, &unicodep, UTF_SIZ);
-               width = wcwidth(unicodep);
+               if ((width = wcwidth(unicodep)) == -1) {
+                       c = "\357\277\275";     /* UTF_INVALID */
+                       width = 1;
+               }
                control = ISCONTROLC1(unicodep);
                ascii = unicodep;
        }
@@ -2665,13 +2673,16 @@ tputc(char *c, int len) {
        if(IS_SET(MODE_WRAP) && (term.c.state & CURSOR_WRAPNEXT)) {
                gp->mode |= ATTR_WRAP;
                tnewline(1);
+               gp = &term.line[term.c.y][term.c.x];
        }
 
        if(IS_SET(MODE_INSERT) && term.c.x+1 < term.col)
                memmove(gp+1, gp, (term.col - term.c.x - 1) * sizeof(Glyph));
 
-       if(term.c.x+width > term.col)
+       if(term.c.x+width > term.col) {
                tnewline(1);
+               gp = &term.line[term.c.y][term.c.x];
+       }
 
        tsetchar(c, &term.c.attr, term.c.x, term.c.y);
 
@@ -3914,6 +3925,9 @@ run(void) {
                                                        TIMEDIFF(now,
                                                                lastblink)));
                                        }
+                                       drawtimeout.tv_sec = \
+                                           drawtimeout.tv_nsec / 1E9;
+                                       drawtimeout.tv_nsec %= (long)1E9;
                                } else {
                                        tv = NULL;
                                }