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 afa6813..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,27 +1294,30 @@ tnewline(int first_col) {
 
 void
 csiparse(void) {
-       /* int noarg = 1; */
-       char *p = csiescseq.buf;
+       char *p = csiescseq.buf, *np;
+       long int v;
 
        csiescseq.narg = 0;
-       if(*p == '?')
-               csiescseq.priv = 1, p++;
+       if(*p == '?') {
+               csiescseq.priv = 1;
+               p++;
+       }
 
+       csiescseq.buf[csiescseq.len] = '\0';
        while(p < csiescseq.buf+csiescseq.len) {
-               while(isdigit(*p)) {
-                       csiescseq.arg[csiescseq.narg] *= 10;
-                       csiescseq.arg[csiescseq.narg] += *p++ - '0'/*, noarg = 0 */;
-               }
-               if(*p == ';' && csiescseq.narg+1 < ESC_ARG_SIZ) {
-                       csiescseq.narg++, p++;
-               } else {
-                       csiescseq.mode = *p;
-                       csiescseq.narg++;
-
-                       return;
-               }
+               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;
+               p = np;
+               if(*p != ';' || csiescseq.narg == ESC_ARG_SIZ)
+                       break;
+               p++;
        }
+       csiescseq.mode = *p;
 }
 
 /* for absolute user moves, when decom is set */
@@ -1923,23 +1925,12 @@ strhandle(void) {
 
 void
 strparse(void) {
-       /*
-        * TODO: Implement parsing like for CSI when required.
-        * Format: ESC type cmd ';' arg0 [';' argn] ESC \
-        */
-       int narg = 0;
-       char *start = strescseq.buf, *end = start + strescseq.len;
-       strescseq.args[0] = start;
-       while(start < end && narg < LEN(strescseq.args)) {
-               start = memchr(start, ';', end - start);
-               if(!start)
-                       break;
-               *start++ = '\0';
-               if(start < end) {
-                       strescseq.args[++narg] = start;
-               }
-       }
-       strescseq.narg = narg + 1;
+       char *p = strescseq.buf;
+
+       strescseq.narg = 0;
+       strescseq.buf[strescseq.len] = '\0';
+       while(p && strescseq.narg < STR_ARG_SIZ)
+               strescseq.args[strescseq.narg++] = strsep(&p, ";");
 }
 
 void
@@ -1950,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)");
@@ -2038,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 {
@@ -2114,9 +2107,11 @@ 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();
+                               csiparse();
+                               csihandle();
                        }
                } else if(term.esc & ESC_STR_END) {
                        term.esc = 0;