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

Re: Bugs with (q-) parameter flag



On Sat, 14 Apr 2012 10:27:12 -0700
Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> torch% x=( a '' '\b' 'c d' '$e' )
>
> I'm not certain this first one qualifies as a bug.  Is the empty string a
> "special character"?
> 
> torch% print -rl ${(q-)x}
> a
> '\b'
> 'c d'
> '$e'

I don't think that can be right, it doesn't happen with any other form
of quoting.  It's got lost in the gaps between backslash and
double-quote handling.

Index: Src/subst.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/subst.c,v
retrieving revision 1.133
diff -p -u -r1.133 subst.c
--- Src/subst.c	29 Feb 2012 09:57:41 -0000	1.133
+++ Src/subst.c	16 Apr 2012 09:56:19 -0000
@@ -1828,6 +1828,10 @@ paramsubst(LinkList l, LinkNode n, char 
 			quotemod = 1;
 			quotetype = QT_SINGLE_OPTIONAL;
 		    } else {
+			if (quotetype == QT_SINGLE_OPTIONAL) {
+			    /* extra q's after '-' not allowed */
+			    goto flagerr;
+			}
 			quotemod++, quotetype++;
 		    }
 		    break;
Index: Src/utils.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/utils.c,v
retrieving revision 1.267
diff -p -u -r1.267 utils.c
--- Src/utils.c	13 Mar 2012 09:47:02 -0000	1.267
+++ Src/utils.c	16 Apr 2012 09:56:20 -0000
@@ -4735,7 +4735,7 @@ quotestring(const char *s, char **e, int
     char *v;
     int alloclen;
     char *buf;
-    int sf = 0, shownull;
+    int sf = 0, shownull = 0;
     /*
      * quotesub is used with QT_SINGLE_OPTIONAL.
      * quotesub = 0:  mechanism not active
@@ -4750,14 +4750,12 @@ quotestring(const char *s, char **e, int
     const char *uend;
 
     slen = strlen(s);
-    if (instring == QT_BACKSLASH_SHOWNULL) {
-	shownull = 1;
-	instring = QT_BACKSLASH;
-    } else {
-	shownull = 0;
-    }
     switch (instring)
     {
+    case QT_BACKSLASH_SHOWNULL:
+	shownull = 1;
+	instring = QT_BACKSLASH;
+	/*FALLTHROUGH*/
     case QT_BACKSLASH:
 	/*
 	 * With QT_BACKSLASH we may need to use $'\300' stuff.
@@ -4765,22 +4763,23 @@ quotestring(const char *s, char **e, int
 	 * storage and using heap for correct size at end.
 	 */
 	alloclen = slen * 7 + 1;
-	if (!*s && shownull)
-	    alloclen += 2;	/* for '' */
 	break;
 
     case QT_SINGLE_OPTIONAL:
 	/*
 	 * Here, we may need to add single quotes.
+	 * Always show empty strings.
 	 */
 	alloclen = slen * 4 + 3;
-	quotesub = 1;
+	quotesub = shownull = 1;
 	break;
 
     default:
 	alloclen = slen * 4 + 1;
 	break;
     }
+    if (!*s && shownull)
+	alloclen += 2;	/* for '' */
 
     quotestart = v = buf = zshcalloc(alloclen);
 
Index: Test/D04parameter.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/D04parameter.ztst,v
retrieving revision 1.64
diff -p -u -r1.64 D04parameter.ztst
--- Test/D04parameter.ztst	10 Apr 2012 01:17:03 -0000	1.64
+++ Test/D04parameter.ztst	16 Apr 2012 09:56:20 -0000
@@ -385,6 +385,13 @@
 >$'playing \'stupid\' "games" \\w\\i\\t\\h $quoting.'
 >'playing '\'stupid\'' "games" \w\i\t\h $quoting.'
 
+  x=( a '' '\b' 'c d' '$e' )
+  print -r ${(q)x}
+  print -r ${(q-)x}
+0:Another ${(q...)...} test
+>a '' \\b c\ d \$e
+>a '' '\b' 'c d' '$e'
+
   print -r -- ${(q-):-foo}
   print -r -- ${(q-):-foo bar}
   print -r -- ${(q-):-"*(.)"}

-- 
Peter Stephenson <pws@xxxxxxx>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom
More information can be found at www.csr.com. Follow CSR on Twitter at http://twitter.com/CSR_PLC and read our blog at www.csr.com/blog



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