JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Using strsep and fixing null termination in csiparse.
[st.git] / st.c
diff --git a/st.c b/st.c
index 7ddce0c..fc9ed70 100644 (file)
--- a/st.c
+++ b/st.c
@@ -1,5 +1,4 @@
 /* See LICENSE for licence details. */
-#define _XOPEN_SOURCE 600
 #include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -796,7 +795,7 @@ selcopy(void) {
                        }
                        /* \n at the end of every selected line except for the last one */
                        if(is_selected && y < sel.e.y)
-                               *ptr++ = '\n';
+                               *ptr++ = '\r';
                }
                *ptr = 0;
        }
@@ -1295,7 +1294,6 @@ tnewline(int first_col) {
 
 void
 csiparse(void) {
-       /* int noarg = 1; */
        char *p = csiescseq.buf, *np;
        long int v;
 
@@ -1305,24 +1303,21 @@ csiparse(void) {
                p++;
        }
 
+       csiescseq.buf[csiescseq.len] = '\0';
        while(p < csiescseq.buf+csiescseq.len) {
                np = NULL;
                v = strtol(p, &np, 10);
+               if(np == p)
+                       v = 0;
                if(v == LONG_MAX || v == LONG_MIN)
                        v = -1;
-               csiescseq.arg[csiescseq.narg] = v;
-               if(np != NULL)
-                       p = np;
-
-               if(*p == ';' && csiescseq.narg+1 < ESC_ARG_SIZ) {
-                       csiescseq.narg++, p++;
-               } else {
-                       csiescseq.mode = *p;
-                       csiescseq.narg++;
-
-                       return;
-               }
+               csiescseq.arg[csiescseq.narg++] = v;
+               p = np;
+               if(*p != ';' || csiescseq.narg == ESC_ARG_SIZ)
+                       break;
+               p++;
        }
+       csiescseq.mode = *p;
 }
 
 /* for absolute user moves, when decom is set */
@@ -1930,17 +1925,12 @@ strhandle(void) {
 
 void
 strparse(void) {
-       char *p = strescseq.buf, *np, *sp;
+       char *p = strescseq.buf;
 
        strescseq.narg = 0;
-       np = strtok_r(strescseq.buf, ";", &sp);
-       while(p < strescseq.buf+strescseq.len && np != NULL) {
-               strescseq.args[strescseq.narg++] = p;
-
-               np = strtok_r(NULL, ";", &sp);
-               if(np != NULL)
-                       p = np;
-       }
+       strescseq.buf[strescseq.len] = '\0';
+       while(p && strescseq.narg < STR_ARG_SIZ)
+               strescseq.args[strescseq.narg++] = strsep(&p, ";");
 }
 
 void
@@ -1951,7 +1941,9 @@ strdump(void) {
        printf("ESC%c", strescseq.type);
        for(i = 0; i < strescseq.len; i++) {
                c = strescseq.buf[i] & 0xff;
-               if(isprint(c)) {
+               if(c == '\0') {
+                       return;
+               } else if(isprint(c)) {
                        putchar(c);
                } else if(c == '\n') {
                        printf("(\\n)");
@@ -2039,7 +2031,7 @@ tputc(char *c, int len) {
                        strhandle();
                        break;
                default:
-                       if(strescseq.len + len < sizeof(strescseq.buf)) {
+                       if(strescseq.len + len < sizeof(strescseq.buf) - 1) {
                                memmove(&strescseq.buf[strescseq.len], c, len);
                                strescseq.len += len;
                        } else {
@@ -2115,7 +2107,8 @@ tputc(char *c, int len) {
                if(term.esc & ESC_CSI) {
                        csiescseq.buf[csiescseq.len++] = ascii;
                        if(BETWEEN(ascii, 0x40, 0x7E)
-                                       || csiescseq.len >= ESC_BUF_SIZ) {
+                                       || csiescseq.len >= \
+                                       sizeof(csiescseq.buf)-1) {
                                term.esc = 0;
                                csiparse();
                                csihandle();