Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Lots of test failures when --disable-multibyte



On 04 April 2022 at 03:03 Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> Many seem to be because typeset fails to apply $'...' quoting to
> strings with embedded metacharacters or NUL bytes, but there are
> several other odd ones too.

Adding some extra single-byte "nice" quoting isn't so hard; this won't fix
everything but seems OK as far as it goes.

pws

commit 61802c2b2b06cab976d392e145d8db5d372a879d
Author: Peter Stephenson <p.stephenson@xxxxxxxxxxx>
Date:   Mon Apr 4 15:34:40 2022 +0100

    Single byte versions of nice quoting

diff --git a/Src/utils.c b/Src/utils.c
index f9127c70c..c5ee78db6 100644
--- a/Src/utils.c
+++ b/Src/utils.c
@@ -438,7 +438,6 @@ putshout(int c)
     return 0;
 }
 
-#ifdef MULTIBYTE_SUPPORT
 /*
  * Turn a character into a visible representation thereof.  The visible
  * string is put together in a static buffer, and this function returns
@@ -517,6 +516,8 @@ nicechar_sel(int c, int quotable)
     return buf;
 }
 
+#ifdef MULTIBYTE_SUPPORT
+
 /**/
 mod_export char *
 nicechar(int c)
@@ -5793,7 +5794,7 @@ mb_charlenconv(const char *s, int slen, wint_t *wcp)
 }
 
 /**/
-#else
+#else /* MULTIBYTE_SUPPORT */
 
 /* Simple replacement for mb_metacharlenconv */
 
@@ -5833,6 +5834,121 @@ charlenconv(const char *x, int len, int *c)
     return 1;
 }
 
+/*
+ * Non-multibyte version of mb_niceformat() above.  Same basic interface.
+ */
+
+/**/
+mod_export size_t
+sb_niceformat(const char *s, FILE *stream, char **outstrp, int flags)
+{
+    size_t l = 0, newl;
+    int umlen, outalloc, outleft;
+    char *ums, *ptr, *eptr, *fmt, *outstr, *outptr;
+
+    if (outstrp) {
+	outleft = outalloc = 2 * strlen(s);
+	outptr = outstr = zalloc(outalloc);
+    } else {
+	outleft = outalloc = 0;
+	outptr = outstr = NULL;
+    }
+
+    ums = ztrdup(s);
+    /*
+     * is this necessary at this point? niceztrlen does this
+     * but it's used in lots of places.  however, one day this may
+     * be, too.
+     */
+    untokenize(ums);
+    ptr = unmetafy(ums, &umlen);
+    eptr = ptr + umlen;
+
+    while (ptr < eptr) {
+	int c = STOUC(*ptr);
+	if (c == '\'' && (flags & NICEFLAG_QUOTE)) {
+	    fmt = "\\'";
+	    newl = 2;
+	}
+	else if (c == '\\' && (flags & NICEFLAG_QUOTE)) {
+	    fmt = "\\\\";
+	    newl = 2;
+	}
+	else {
+	    fmt = nicechar_sel(c, flags & NICEFLAG_QUOTE);
+	    newl = 1;
+	}
+
+	++ptr;
+	l += newl;
+
+	if (stream)
+	    zputs(fmt, stream);
+	if (outstr) {
+	    /* Append to output string */
+	    int outlen = strlen(fmt);
+	    if (outlen >= outleft) {
+		/* Reallocate to twice the length */
+		int outoffset = outptr - outstr;
+
+		outleft += outalloc;
+		outalloc *= 2;
+		outstr = zrealloc(outstr, outalloc);
+		outptr = outstr + outoffset;
+	    }
+	    memcpy(outptr, fmt, outlen);
+	    /* Update start position */
+	    outptr += outlen;
+	    /* Update available bytes */
+	    outleft -= outlen;
+	}
+    }
+
+    free(ums);
+    if (outstrp) {
+	*outptr = '\0';
+	/* Use more efficient storage for returned string */
+	if (flags & NICEFLAG_NODUP)
+	    *outstrp = outstr;
+	else {
+	    *outstrp = (flags & NICEFLAG_HEAP) ? dupstring(outstr) :
+		ztrdup(outstr);
+	    free(outstr);
+	}
+    }
+
+    return l;
+}
+
+/*
+ * Return 1 if sb_niceformat() would reformat this string, else 0.
+ */
+
+/**/
+mod_export int
+is_sb_niceformat(const char *s)
+{
+    int umlen, ret = 0;
+    char *ums, *ptr, *eptr;
+
+    ums = ztrdup(s);
+    untokenize(ums);
+    ptr = unmetafy(ums, &umlen);
+    eptr = ptr + umlen;
+
+    while (ptr < eptr) {
+	if (is_nicechar(*ptr))  {
+	    ret = 1;
+	    break;
+	}
+	++ptr;
+    }
+
+    free(ums);
+
+    return ret;
+}
+
 /**/
 #endif /* MULTIBYTE_SUPPORT */
 
@@ -6366,6 +6482,22 @@ quotedzputs(char const *s, FILE *stream)
 	    return outstr;
 	}
     }
+#else
+    if (is_sb_niceformat(s)){
+	if (stream) {
+	    fputs("$'", stream);
+	    sb_niceformat(s, stream, NULL, NICEFLAG_QUOTE);
+	    fputc('\'', stream);
+	    return NULL;
+	} else {
+	    char *substr;
+	    sb_niceformat(s, NULL, &substr, NICEFLAG_QUOTE|NICEFLAG_NODUP);
+	    outstr = (char *)zhalloc(4 + strlen(substr));
+	    sprintf(outstr, "$'%s'", substr);
+	    free(substr);
+	    return outstr;
+	}
+    }
 #endif /* MULTIBYTE_SUPPORT */
 
     if (!hasspecial(s)) {
diff --git a/Src/zsh.h b/Src/zsh.h
index 641d9c95c..40f9ea537 100644
--- a/Src/zsh.h
+++ b/Src/zsh.h
@@ -3277,14 +3277,15 @@ enum zexit_t {
 #define AFTERTRAPHOOK  (zshhooks + 2)
 #define GETCOLORATTR   (zshhooks + 3)
 
-#ifdef MULTIBYTE_SUPPORT
-/* Final argument to mb_niceformat() */
+/* Final argument to [ms]b_niceformat() */
 enum {
     NICEFLAG_HEAP = 1,		/* Heap allocation where needed */
     NICEFLAG_QUOTE = 2,		/* Result will appear in $'...' */
     NICEFLAG_NODUP = 4,         /* Leave allocated */
 };
 
+#ifdef MULTIBYTE_SUPPORT
+
 /* Metafied input */
 #define nicezputs(str, outs)	(void)mb_niceformat((str), (outs), NULL, 0)
 #define MB_METACHARINIT()	mb_charinit()




Messages sorted by: Reverse Date, Date, Thread, Author