From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 21786 invoked by alias); 23 May 2010 20:34:29 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 27965 Received: (qmail 25864 invoked from network); 23 May 2010 20:34:04 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham version=3.3.1 Received-SPF: pass (ns1.primenet.com.au: SPF record at ntlworld.com designates 81.103.221.56 as permitted sender) From: Peter Stephenson To: zsh-workers@zsh.org (Zsh hackers list) Subject: ${(q)...} for newline X-Mailer: MH-E 8.2; nmh 1.3; GNU Emacs 23.1.1 Date: Sun, 23 May 2010 20:58:40 +0100 Message-ID: <10739.1274644720@pws-pc> X-Cloudmark-Analysis: v=1.1 cv=1ggfb5FlKZQUfF3vzm9UBYZ2uTfLsbs/8dSljwg5+mE= c=1 sm=0 a=zNANCjgXmV0A:10 a=DogomfpGjd0A:10 a=NLZqzBF-AAAA:8 a=5BWZU31F6MHNxvLCMyUA:9 a=hkLpW_Hmcz_IGmQIu4IA:7 a=XlofVo_e86mMzSYDXXXBvlOBqZEA:4 a=_dQi-Dcv4p4A:10 a=HpAAvcLHHh0Zw7uRqdWCyQ==:117 Param-style simple (i.e. backslash) quoting of a newline appends a real newline in double quotes. It seems to me it would occasion far fewer surprises if this never output a literal newline, so this uses $'\n'. We already bargain for up to 7 output characters per input character. The first two hunks are just a tidy up (written before I realised I didn't need to do any more counting). Index: Src/utils.c =================================================================== RCS file: /cvsroot/zsh/zsh/Src/utils.c,v retrieving revision 1.242 diff -p -u -r1.242 utils.c --- Src/utils.c 27 Mar 2010 19:04:36 -0000 1.242 +++ Src/utils.c 23 May 2010 19:54:21 -0000 @@ -4585,11 +4585,12 @@ quotestring(const char *s, char **e, int * quotesub = 2: mechanism active, added opening "'"; need * closing "'". */ - int quotesub = 0; + int quotesub = 0, slen; char *quotestart; convchar_t cc; const char *uend; + slen = strlen(s); switch (instring) { case QT_BACKSLASH: @@ -4598,21 +4599,22 @@ quotestring(const char *s, char **e, int * Keep memory usage within limits by allocating temporary * storage and using heap for correct size at end. */ - alloclen = strlen(s) * 7 + 1; + alloclen = slen * 7 + 1; break; case QT_SINGLE_OPTIONAL: /* * Here, we may need to add single quotes. */ - alloclen = strlen(s) * 4 + 3; + alloclen = slen * 4 + 3; quotesub = 1; break; default: - alloclen = strlen(s) * 4 + 1; + alloclen = slen * 4 + 1; break; } + tt = quotestart = v = buf = zshcalloc(alloclen); DPUTS(instring < QT_BACKSLASH || instring == QT_BACKTICK || @@ -4771,15 +4773,19 @@ quotestring(const char *s, char **e, int continue; } else if (*u == '\n' || (instring == QT_SINGLE && *u == '\'')) { - if (unset(RCQUOTES)) { + if (*u == '\n') { + *v++ = '$'; + *v++ = '\''; + *v++ = '\\'; + *v++ = 'n'; + *v++ = '\''; + } else if (unset(RCQUOTES)) { *v++ = '\''; if (*u == '\'') *v++ = '\\'; *v++ = *u; *v++ = '\''; - } else if (*u == '\n') - *v++ = '"', *v++ = '\n', *v++ = '"'; - else + } else *v++ = '\'', *v++ = '\''; u++; continue; -- Peter Stephenson Web page now at http://homepage.ntlworld.com/p.w.stephenson/