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

behaviour of %q format specifier



In ksh, the %q printf format specifier will put posix quotes around
strings containing non-printable characters. Bash doesn't. The
following patch would add this to zsh. I'm slightly unsure about this
though - it wouldn't be necessary if the string is then passed through
eval (probably the only use of %q) and it is inconsistent with the q
history modifier and parameter expansion flag.

Any thoughts? Should I apply this or not, or something different?

Oliver

Index: Src/builtin.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v
retrieving revision 1.58
diff -u -r1.58 builtin.c
--- Src/builtin.c	2001/10/15 11:34:27	1.58
+++ Src/builtin.c	2001/10/15 11:47:48
@@ -3171,7 +3171,7 @@
 		}
 		break;
 	    case 'q':
-		stringval = *args ? bslashquote(*args++, NULL, 0) : &nullstr;
+		stringval = *args ? bslashquote(*args++, NULL, 4) : &nullstr;
 		*d = 's';
 		print_val(stringval);
 		break;
Index: Src/utils.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/utils.c,v
retrieving revision 1.35
diff -u -r1.35 utils.c
--- Src/utils.c	2001/06/15 23:55:08	1.35
+++ Src/utils.c	2001/10/15 11:47:48
@@ -2929,8 +2929,10 @@
  * pointer it points to may point to a position in s and in e the position  *
  * of the corresponding character in the quoted string is returned.         *
  * The last argument should be zero if this is to be used outside a string, *
- * one if it is to be quoted for the inside of a single quoted string, and  *
- * two if it is for the inside of  double quoted string.                    *
+ * one if it is to be quoted for the inside of a single quoted string,      *
+ * two if it is for the inside of double quoted string, and                 *
+ * three if it is for the inside of a posix quoted string, and              *
+ * four if posix quotes should be added if s contains non-printable chars   *
  * The string may be metafied and contain tokens.                           */
 
 /**/
@@ -2939,15 +2941,24 @@
 {
     const char *u, *tt;
     char *v;
-    char *buf = hcalloc(4 * strlen(s) + 1);
+    char *buf = hcalloc(4 * strlen(s) + 4);
     int sf = 0;
 
+    if (instring == 4) {
+    	instring = 0;
+    	for (u=s;*u;u++)
+    	    if (!isprint((*u == Meta) ? *++u ^ 32 : *u)) {
+	    	instring = 4;
+		buf += 2;
+		break;
+	    }
+    }
     tt = v = buf;
     u = s;
     for (; *u; u++) {
 	if (e && *e == u)
 	    *e = v, sf = 1;
-	if (instring == 3) {
+	if (instring >= 3) {
 	  int c = *u;
 	  if (c == Meta) {
 	    c = *++u ^ 32;
@@ -3065,6 +3076,11 @@
 	if(*u == Meta)
 	    *v++ = *u++;
 	*v++ = *u;
+    }
+    if (instring == 4) {
+    	/* enclose buf in $'...' */
+    	*v++ = *--buf = '\'';
+	*--buf = '$';
     }
     *v = '\0';

_____________________________________________________________________
This message has been checked for all known viruses by the 
MessageLabs Virus Scanning Service. For further information visit
http://www.messagelabs.com/stats.asp



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