zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: Zsh hackers list <zsh-workers@sunsite.dk>
Subject: Re: set -x and [[ xxxx = pattern ]]
Date: Sun, 11 May 2008 20:49:16 +0100	[thread overview]
Message-ID: <20080511204916.17e11de0@pws-pc> (raw)
In-Reply-To: <20080510161823.GB5560@sc.homeunix.net>

On Sat, 10 May 2008 17:18:23 +0100
Stephane Chazelas <Stephane_Chazelas@yahoo.fr> 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 */


      reply	other threads:[~2008-05-11 19:50 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-10 16:18 Stephane Chazelas
2008-05-11 19:49 ` Peter Stephenson [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20080511204916.17e11de0@pws-pc \
    --to=p.w.stephenson@ntlworld.com \
    --cc=zsh-workers@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).