zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: disabling pattern groups
@ 2013-10-23 21:05 Peter Stephenson
  2013-10-24 18:21 ` Peter Stephenson
  0 siblings, 1 reply; 2+ messages in thread
From: Peter Stephenson @ 2013-10-23 21:05 UTC (permalink / raw)
  To: Zsh Hackers' List

I'm well aware it's high time for another release, and the main thing
blocking it is getting pattern disables properly tested.

Goodness knows why I let parentheses and related ksh-style groups be
disabled, it just seemed consistent.  The main reason to have this
feature at all is so people complaining about "^" being a pattern
character, or "~" if they're Emacs-backup-with-version-number fanatics,
can disable those and still use extendedglob, so I could probably have
got away without.  However, now I've done it, disabling groups should
work with some degree of consistency without breaking anything else.

Anyway, this seems to get the basics and some of the not-so-basics
working:

  disable -p '('
  [[ "(foo)" = (foo) ]]

succeeds

  setopt kshglob
  disable -p '@('
  [[ "@(foo)" = @(foo) ]]

fails unless either "(" is disabled, or SH_GLOB is set, in which case it
succeeds --- subtle but correct: we've always allowed the mix of zsh and
ksh pattern styles and while it may be morally questionable I'm not
going to change that here.

  setopt kshglob
  disable -p '(' '@(' '|'
  [[ "@(foo|bar)" = @(foo|bar) ]]

succeeds.

Next, I'll start constructing some tests.

diff --git a/Src/pattern.c b/Src/pattern.c
index 609a9e3..c86f86d 100644
--- a/Src/pattern.c
+++ b/Src/pattern.c
@@ -748,7 +748,7 @@ patcompswitch(int paren, int *flagp)
 	starter = 0;
 
     br = patnode(P_BRANCH);
-    if (!patcompbranch(&flags))
+    if (!patcompbranch(&flags, paren))
 	return 0;
     if (patglobflags != (int)savglobflags)
 	gfchanged++;
@@ -839,7 +839,7 @@ patcompswitch(int paren, int *flagp)
 		patglobflags = (int)savglobflags;
 	    }
 	}
-	newbr = patcompbranch(&flags);
+	newbr = patcompbranch(&flags, paren);
 	if (tilde == 2) {
 	    /* restore special treatment of / */
 	    zpc_special[ZPC_SLASH] = '/';
@@ -902,7 +902,7 @@ patcompswitch(int paren, int *flagp)
 
 /**/
 static long
-patcompbranch(int *flagp)
+patcompbranch(int *flagp, int paren)
 {
     long chain, latest = 0, starter;
     int flags = 0;
@@ -973,7 +973,7 @@ patcompbranch(int *flagp)
 	    patparse++;
 	    latest = patcompnot(0, &flags);
 	} else
-	    latest = patcomppiece(&flags);
+	    latest = patcomppiece(&flags, paren);
 	if (!latest)
 	    return 0;
 	if (!starter)
@@ -1221,7 +1221,7 @@ pattern_range_to_string(char *rangestr, char *outstr)
 
 /**/
 static long
-patcomppiece(int *flagp)
+patcomppiece(int *flagp, int paren)
 {
     long starter = 0, next, op, opnd;
     int flags, flags2, kshchar, len, ch, patch, nmeta;
@@ -1253,17 +1253,25 @@ patcomppiece(int *flagp)
 	}
 
 	/*
-	 * End of string (or no string at all) if ksh-type parentheses,
-	 * or special character, unless that character is a tilde and
-	 * the character following is an end-of-segment character.  Thus
-	 * tildes are not special if there is nothing following to
-	 * be excluded.
+	 * If '(' is disabled as a pattern char, allow ')' as
+	 * an ordinary string character if there are no parentheses to
+	 * close.  Don't allow it otherwise, it changes the syntax.
 	 */
-	if (kshchar || (memchr(zpc_special, *patparse, ZPC_COUNT) &&
-			(*patparse != zpc_special[ZPC_TILDE] ||
-			 patparse[1] == '/' ||
-			 !memchr(zpc_special, patparse[1], ZPC_SEG_COUNT))))
-	    break;
+	if (zpc_special[ZPC_INPAR] != Marker || *patparse != Outpar ||
+	    paren) {
+	    /*
+	     * End of string (or no string at all) if ksh-type parentheses,
+	     * or special character, unless that character is a tilde and
+	     * the character following is an end-of-segment character.  Thus
+	     * tildes are not special if there is nothing following to
+	     * be excluded.
+	     */
+	    if (kshchar || (memchr(zpc_special, *patparse, ZPC_COUNT) &&
+			    (*patparse != zpc_special[ZPC_TILDE] ||
+			     patparse[1] == '/' ||
+			     !memchr(zpc_special, patparse[1], ZPC_SEG_COUNT))))
+		break;
+    	}
 
 	/* Remember the previous character for backtracking */
 	patprev = patparse;
@@ -1438,7 +1446,7 @@ patcomppiece(int *flagp)
 	    patadd(NULL, 0, 1, 0);
 	    break;
 	case Inpar:
-	    DPUTS(zpc_special[ZPC_INPAR] == Marker,
+	    DPUTS(!kshchar && zpc_special[ZPC_INPAR] == Marker,
 		  "Treating '(' as pattern character although disabled");
 	    DPUTS(isset(SHGLOB) && !kshchar,
 		  "Treating bare '(' as pattern character with SHGLOB");
@@ -1523,7 +1531,7 @@ patcomppiece(int *flagp)
 	     * Marker for restoring a backslash in output:
 	     * does not match a character.
 	     */
-	    next = patcomppiece(flagp);
+	    next = patcomppiece(flagp, paren);
 	    /*
 	     * Can't match a pure string since we need to do this
 	     * as multiple chunks.
@@ -1710,7 +1718,7 @@ patcompnot(int paren, int *flagsp)
     pattail(starter, excl = patnode(P_EXCLUDE));
     up.p = NULL;
     patadd((char *)&up, 0, sizeof(up), 0);
-    if (!(br = (paren ? patcompswitch(1, &dummy) : patcompbranch(&dummy))))
+    if (!(br = (paren ? patcompswitch(1, &dummy) : patcompbranch(&dummy, 0))))
 	return 0;
     pattail(br, patnode(P_EXCEND));
     n = patnode(P_NOTHING); /* just so much easier */

-- 
Peter Stephenson <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: PATCH: disabling pattern groups
  2013-10-23 21:05 PATCH: disabling pattern groups Peter Stephenson
@ 2013-10-24 18:21 ` Peter Stephenson
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 2013-10-24 18:21 UTC (permalink / raw)
  To: Zsh Hackers' List

On Wed, 23 Oct 2013 22:05:26 +0100
Peter Stephenson <p.w.stephenson@ntlworld.com> wrote:
> Next, I'll start constructing some tests.

This covers the basics.

diff --git a/Test/D02glob.ztst b/Test/D02glob.ztst
index 0aea261..d6ab733 100644
--- a/Test/D02glob.ztst
+++ b/Test/D02glob.ztst
@@ -433,3 +433,61 @@
  print glob.tmp/dir5/N<->(N)
 0:Numeric glob is not usurped by process substitution.
 >glob.tmp/dir5/N123
+
+ tpd() {
+   [[ $1 = $~2 ]]
+  print -r "$1, $2: $?"
+ }
+ test_pattern_disables() {
+   emulate -L zsh
+   tpd 'forthcoming' 'f*g'
+   disable -p '*'
+   tpd 'forthcoming' 'f*g'
+   tpd 'f*g' 'f*g'
+   tpd '[frog]' '[frog]'
+   tpd '[frog]' '\[[f]rog\]'
+   disable -p '['
+   tpd '[frog]' '[frog]'
+   tpd '[frog]' '\[[f]rog\]'
+   setopt extendedglob
+   tpd 'foo' '^bar'
+   disable -p '^'
+   tpd 'foo' '^bar'
+   tpd '^bar' '^bar'
+   tpd 'rumble' '(rumble|bluster)'
+   tpd '(thunder)' '(thunder)'
+   disable -p '('
+   tpd 'rumble' '(rumble|bluster)'
+   tpd '(thunder)' '(thunder)'
+   setopt kshglob
+   tpd 'scramble' '@(panic|frenzy|scramble)'
+   tpd '@(scrimf)' '@(scrimf)'
+   disable -p '@('
+   tpd 'scramble' '@(panic|frenzy|scramble)'
+   tpd '@(scrimf)' '@(scrimf)'
+   disable -p
+ }
+ test_pattern_disables
+ print Nothing should be disabled.
+ disable -p
+0:disable -p
+>forthcoming, f*g: 0
+>forthcoming, f*g: 1
+>f*g, f*g: 0
+>[frog], [frog]: 1
+>[frog], \[[f]rog\]: 0
+>[frog], [frog]: 0
+>[frog], \[[f]rog\]: 1
+>foo, ^bar: 0
+>foo, ^bar: 1
+>^bar, ^bar: 0
+>rumble, (rumble|bluster): 0
+>(thunder), (thunder): 1
+>rumble, (rumble|bluster): 1
+>(thunder), (thunder): 0
+>scramble, @(panic|frenzy|scramble): 0
+>@(scrimf), @(scrimf): 1
+>scramble, @(panic|frenzy|scramble): 1
+>@(scrimf), @(scrimf): 0
+>'(' '*' '[' '^' '@('
+>Nothing should be disabled.

-- 
Peter Stephenson <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2013-10-24 18:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-10-23 21:05 PATCH: disabling pattern groups Peter Stephenson
2013-10-24 18:21 ` Peter Stephenson

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).