From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 16265 invoked from network); 11 May 2008 19:50:34 -0000 X-Spam-Checker-Version: SpamAssassin 3.2.4 (2008-01-01) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.2.4 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 11 May 2008 19:50:34 -0000 Received-SPF: none (ns1.primenet.com.au: domain at sunsite.dk does not designate permitted sender hosts) Received: (qmail 62232 invoked from network); 11 May 2008 19:50:27 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 11 May 2008 19:50:27 -0000 Received: (qmail 23777 invoked by alias); 11 May 2008 19:50:23 -0000 Mailing-List: contact zsh-workers-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 24996 Received: (qmail 23760 invoked from network); 11 May 2008 19:50:23 -0000 Received: from bifrost.dotsrc.org (130.225.254.106) by sunsite.dk with SMTP; 11 May 2008 19:50:23 -0000 Received: from mtaout01-winn.ispmail.ntl.com (mtaout01-winn.ispmail.ntl.com [81.103.221.47]) by bifrost.dotsrc.org (Postfix) with ESMTP id C82CA80ED172 for ; Sun, 11 May 2008 21:50:14 +0200 (CEST) Received: from aamtaout01-winn.ispmail.ntl.com ([81.103.221.35]) by mtaout01-winn.ispmail.ntl.com with ESMTP id <20080511195349.OCFG14647.mtaout01-winn.ispmail.ntl.com@aamtaout01-winn.ispmail.ntl.com> for ; Sun, 11 May 2008 20:53:49 +0100 Received: from pws-pc ([81.107.40.67]) by aamtaout01-winn.ispmail.ntl.com with ESMTP id <20080511195500.CUUJ219.aamtaout01-winn.ispmail.ntl.com@pws-pc> for ; Sun, 11 May 2008 20:55:00 +0100 Date: Sun, 11 May 2008 20:49:16 +0100 From: Peter Stephenson To: Zsh hackers list Subject: Re: set -x and [[ xxxx = pattern ]] Message-ID: <20080511204916.17e11de0@pws-pc> In-Reply-To: <20080510161823.GB5560@sc.homeunix.net> References: <20080510161823.GB5560@sc.homeunix.net> X-Mailer: Claws Mail 3.3.1 (GTK+ 2.12.5; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Virus-Scanned: ClamAV 0.91.2/7091/Sun May 11 21:02:57 2008 on bifrost X-Virus-Status: Clean On Sat, 10 May 2008 17:18:23 +0100 Stephane Chazelas wrote: > $ zsh -xc '[[ a = * ]]' > +zsh:1> [[ a == '*' ]] > $ zsh -xc "[[ a = '*' ]]" > +zsh:1> [[ a == '*' ]] > $ zsh -xc '[[ a = "*" ]]' > +zsh:1> [[ a == '*' ]] > > Shouldn't the output be different? (same goes for "case"). Yes, we could do this better. Here's an attempt: it's possible I've missed some characters that need quoting (though if so zshtokenize() probably has, too, and that's widely used), or more likely I've missed some cases where tokenized output could be better displayed. The zshtokenize() change is yet more unnecessary obsessive neatness. Index: Src/cond.c =================================================================== RCS file: /cvsroot/zsh/zsh/Src/cond.c,v retrieving revision 1.14 diff -u -r1.14 cond.c --- Src/cond.c 1 Nov 2007 17:57:57 -0000 1.14 +++ Src/cond.c 11 May 2008 19:46:48 -0000 @@ -182,16 +182,16 @@ } if (tracingcond) { if (ctype < COND_MOD) { - char *rt = (char *) right; - if (ctype == COND_STREQ || ctype == COND_STRNEQ) { - rt = dupstring(ecrawstr(state->prog, state->pc, NULL)); - singsub(&rt); - untokenize(rt); - } fputc(' ',xtrerr); quotedzputs(left, xtrerr); fprintf(xtrerr, " %s ", condstr[ctype]); - quotedzputs(rt, xtrerr); + if (ctype == COND_STREQ || ctype == COND_STRNEQ) { + char *rt = dupstring(ecrawstr(state->prog, state->pc, NULL)); + singsub(&rt); + quote_tokenized_output(rt, xtrerr); + } + else + quotedzputs((char *)right, xtrerr); } else { fprintf(xtrerr, " -%c ", ctype); quotedzputs(left, xtrerr); Index: Src/exec.c =================================================================== RCS file: /cvsroot/zsh/zsh/Src/exec.c,v retrieving revision 1.130 diff -u -r1.130 exec.c --- Src/exec.c 23 Feb 2008 18:34:02 -0000 1.130 +++ Src/exec.c 11 May 2008 19:46:49 -0000 @@ -1609,6 +1609,66 @@ } } + +/* + * Given a tokenized string, output it to standard output in + * such a way that it's clear which tokens are active. + * Hence Star becomes an unquoted "*", while a "*" becomes "\*". + * + * The code here is a kind of amalgamation of the tests in + * zshtokenize() and untokenize() with some outputting. + */ + +/**/ +void +quote_tokenized_output(char *str, FILE *file) +{ + char *s = str; + + for (; *s; s++) { + switch (*s) { + case Meta: + putc(*++s ^ 32, file); + continue; + + case Nularg: + /* Do nothing. I think. */ + continue; + + case '\\': + case '<': + case '>': + case '(': + case '|': + case ')': + case '^': + case '#': + case '~': + case '[': + case ']': + case '*': + case '?': + case '$': + putc('\\', file); + break; + + case '=': + if (s == str) + putc('\\', file); + break; + + default: + if (itok(*s)) { + putc(ztokens[*s - Pound], file); + continue; + } + break; + } + + putc(*s, file); + } +} + /* Check that we can use a parameter for allocating a file descriptor. */ static int Index: Src/glob.c =================================================================== RCS file: /cvsroot/zsh/zsh/Src/glob.c,v retrieving revision 1.64 diff -u -r1.64 glob.c --- Src/glob.c 29 Apr 2008 19:53:48 -0000 1.64 +++ Src/glob.c 11 May 2008 19:46:50 -0000 @@ -2987,7 +2987,7 @@ mod_export void tokenize(char *s) { - zshtokenize(s, 0, 0); + zshtokenize(s, 0); } /* @@ -3004,12 +3004,15 @@ mod_export void shtokenize(char *s) { - zshtokenize(s, 1, isset(SHGLOB)); + int flags = ZSHTOK_SUBST; + if (isset(SHGLOB)) + flags |= ZSHTOK_SHGLOB; + zshtokenize(s, flags); } /**/ static void -zshtokenize(char *s, int glbsbst, int shglob) +zshtokenize(char *s, int flags) { char *t; int bslash = 0; @@ -3021,16 +3024,16 @@ case Bnullkeep: case '\\': if (bslash) { - s[-1] = glbsbst ? Bnullkeep : Bnull; + s[-1] = (flags & ZSHTOK_SUBST) ? Bnullkeep : Bnull; break; } bslash = 1; continue; case '<': - if (shglob) + if (flags & ZSHTOK_SHGLOB) break; if (bslash) { - s[-1] = glbsbst ? Bnullkeep : Bnull; + s[-1] = (flags & ZSHTOK_SUBST) ? Bnullkeep : Bnull; break; } t = s; @@ -3046,7 +3049,7 @@ case '(': case '|': case ')': - if (shglob) + if (flags & ZSHTOK_SHGLOB) break; case '>': case '^': @@ -3060,7 +3063,7 @@ for (t = ztokens; *t; t++) if (*t == *s) { if (bslash) - s[-1] = glbsbst ? Bnullkeep : Bnull; + s[-1] = (flags & ZSHTOK_SUBST) ? Bnullkeep : Bnull; else *s = (t - ztokens) + Pound; break; Index: Src/loop.c =================================================================== RCS file: /cvsroot/zsh/zsh/Src/loop.c,v retrieving revision 1.22 diff -u -r1.22 loop.c --- Src/loop.c 11 May 2008 19:03:58 -0000 1.22 +++ Src/loop.c 11 May 2008 19:46:50 -0000 @@ -559,10 +559,10 @@ save = (!(state->prog->flags & EF_HEAP) && !strcmp(pat, opat) && *spprog != dummy_patprog2); - pat2 = dupstring(pat); - untokenize(pat2); printprompt4(); - fprintf(xtrerr, "case %s (%s)\n", word, pat2); + fprintf(xtrerr, "case %s (", word); + quote_tokenized_output(pat, xtrerr); + fprintf(xtrerr, ")\n"); fflush(xtrerr); } state->pc += 2; Index: Src/zsh.h =================================================================== RCS file: /cvsroot/zsh/zsh/Src/zsh.h,v retrieving revision 1.132 diff -u -r1.132 zsh.h --- Src/zsh.h 9 May 2008 17:33:51 -0000 1.132 +++ Src/zsh.h 11 May 2008 19:46:53 -0000 @@ -1516,6 +1516,15 @@ }; typedef struct repldata *Repldata; +/* + * Flags to zshtokenize. + */ +enum { + /* Do glob substitution */ + ZSHTOK_SUBST = 0x0001, + /* Use sh-style globbing */ + ZSHTOK_SHGLOB = 0x0002 +}; /* Flags as the second argument to prefork */ #define PF_TYPESET 0x01 /* argument handled like typeset foo=bar */