JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
change -e behaviour and update man page.
[st.git] / st.c
diff --git a/st.c b/st.c
index e988696..2be6772 100644 (file)
--- a/st.c
+++ b/st.c
@@ -247,7 +247,7 @@ static CSIEscape escseq;
 static int cmdfd;
 static pid_t pid;
 static Selection sel;
-static char *opt_cmd   = NULL;
+static char **opt_cmd  = NULL;
 static char *opt_title = NULL;
 static char *opt_class = NULL;
 
@@ -545,13 +545,14 @@ die(const char *errstr, ...) {
 
 void
 execsh(void) {
-       char *args[] = {getenv("SHELL"), "-i", NULL};
-       if(opt_cmd)
-               args[0] = opt_cmd, args[1] = NULL;
-       else
-               DEFAULT(args[0], SHELL);
+       char **args;
+       char *envshell = getenv("SHELL");
+
+       DEFAULT(envshell, "sh");
        putenv("TERM="TNAME);
+       args = opt_cmd ? opt_cmd : (char*[]){envshell, "-i", NULL};
        execvp(args[0], args);
+       exit(EXIT_FAILURE);
 }
 
 void 
@@ -606,24 +607,31 @@ dump(char c) {
 
 void
 ttyread(void) {
-       char buf[BUFSIZ], *ptr;
+       static char buf[BUFSIZ];
+       static int buflen = 0; 
+       char *ptr;
        char s[UTF_SIZ];
-       int ret, br;
-       static int buflen = 0;
-       long u;
+       int charsize; /* size of utf8 char in bytes */
+       long utf8c;
+       int ret;
 
+       /* append read bytes to unprocessed bytes */
        if((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
                die("Couldn't read from shell: %s\n", SERRNO);
-       else {
-               buflen += ret;
-               for(ptr=buf; buflen>=UTF_SIZ||isfullutf8(ptr,buflen); buflen-=br) {
-                       br = utf8decode(ptr, &u);
-                       utf8encode(&u, s);
-                       tputc(s);
-                       ptr += br;
-               }
-               memcpy(buf, ptr, buflen);
+
+       /* process every complete utf8 char */
+       buflen += ret;
+       ptr = buf;
+       while(buflen >= UTF_SIZ || isfullutf8(ptr,buflen)) {
+               charsize = utf8decode(ptr, &utf8c);
+               utf8encode(&utf8c, s);
+               tputc(s);
+               ptr    += charsize;
+               buflen -= charsize;
        }
+
+       /* keep any uncomplete utf8 char for the next call */
+       memmove(buf, ptr, buflen);
 }
 
 void
@@ -1774,7 +1782,6 @@ kpress(XEvent *ev) {
                        /* 3. X lookup  */
                default:
                        if(len > 0) {
-                               buf[sizeof(buf)-1] = '\0';
                                if(meta && len == 1)
                                        ttywrite("\033", 1);
                                ttywrite(buf, len);
@@ -1845,12 +1852,15 @@ main(int argc, char *argv[]) {
                        if(++i < argc) opt_class = argv[i];
                        break;
                case 'e':
-                       if(++i < argc) opt_cmd = argv[i];
+                       if(++i < argc) opt_cmd = &argv[i];
                        break;
                case 'v':
                default:
                        die(USAGE);
                }
+               /* -e eats every remaining arguments */
+               if(opt_cmd)
+                       break;
        }
        setlocale(LC_CTYPE, "");
        tnew(80, 24);