From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 10917 invoked from network); 15 Oct 2001 11:55:56 -0000 Received: from unknown (HELO sunsite.dk) (130.225.247.90) by ns1.primenet.com.au with SMTP; 15 Oct 2001 11:55:56 -0000 Received: (qmail 28717 invoked by alias); 15 Oct 2001 11:55:47 -0000 Mailing-List: contact zsh-workers-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 16044 Received: (qmail 28703 invoked from network); 15 Oct 2001 11:55:45 -0000 X-VirusChecked: Checked Sender: kiddleo@cav.logica.co.uk Message-ID: <3BCACE03.FFDB95AA@yahoo.co.uk> Date: Mon, 15 Oct 2001 12:52:35 +0100 From: Oliver Kiddle X-Mailer: Mozilla 4.77 [en] (X11; U; Linux 2.2.15 i686) X-Accept-Language: en MIME-Version: 1.0 To: zsh-workers@sunsite.dk Subject: behaviour of %q format specifier Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit 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