Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: zsh regards reserved word as candidate for alias substitution



On Wed, 25 Feb 2009 01:17:17 +0100
Vincent Lefevre <vincent@xxxxxxxxxx> wrote:
> I've reported the following bug on
> 
>   http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=516998
> 
> POSIX.1-2008 says[*]:
> 
>   2.3.1 Alias Substitution
>   [...] However, reserved words in correct grammatical context
>   shall not be candidates for alias substitution.

It's hardly worth an option, but it's best to keep options doing what they
say...

Index: Doc/Zsh/options.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/options.yo,v
retrieving revision 1.76
diff -u -r1.76 options.yo
--- Doc/Zsh/options.yo	14 Feb 2009 13:35:37 -0000	1.76
+++ Doc/Zsh/options.yo	3 Mar 2009 17:22:02 -0000
@@ -1708,6 +1708,43 @@
 This option is for compatibility with older versions of the shell and
 is not recommended in new code.
 )
+pindex(POSIX_ALIASES)
+pindex(NO_POSIX_ALIASES)
+pindex(POSIXALIASES)
+pindex(NOPOSIXALIASES)
+item(tt(POSIX_ALIASES) <K> <S>)(
+When this option is set, reserved words are not candidates for
+alias expansion:  it is still possible to declare any of them as an alias,
+but the alias will never be expanded.  Reserved words are
+tt(!),
+tt([[),
+tt({),
+tt(}),
+tt(case),
+tt(coproc),
+tt(do),
+tt(done),
+tt(elif),
+tt(else),
+tt(end),
+tt(esac),
+tt(fi),
+tt(for),
+tt(foreach),
+tt(function),
+tt(if),
+tt(nocorrect),
+tt(repeat),
+tt(select),
+tt(then),
+tt(time),
+tt(until),
+tt(while).
+
+Alias expansion takes place while text is being read; hence when this
+option is set it does not take effect until the end of any function or
+other piece of shell code evaluated as one unit.
+)
 pindex(POSIX_BUILTINS)
 pindex(NO_POSIX_BUILTINS)
 pindex(POSIXBUILTINS)
Index: Src/lex.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/lex.c,v
retrieving revision 1.51
diff -u -r1.51 lex.c
--- Src/lex.c	27 Feb 2009 10:24:16 -0000	1.51
+++ Src/lex.c	3 Mar 2009 17:22:02 -0000
@@ -1748,9 +1748,11 @@
 
 	if (tok == STRING) {
 	    /* Check for an alias */
-	    if (!noaliases && isset(ALIASESOPT)) {
+	    if (!noaliases && isset(ALIASESOPT) &&
+		(!isset(POSIXALIASES) ||
+		 !reswdtab->getnode(reswdtab, zshlextext))) {
 		char *suf;
-		
+
 		an = (Alias) aliastab->getnode(aliastab, zshlextext);
 		if (an && !an->inuse &&
 		    ((an->node.flags & ALIAS_GLOBAL) || incmdpos || inalmore)) {
Index: Src/options.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/options.c,v
retrieving revision 1.47
diff -u -r1.47 options.c
--- Src/options.c	11 Feb 2009 20:42:16 -0000	1.47
+++ Src/options.c	3 Mar 2009 17:22:02 -0000
@@ -198,6 +198,7 @@
 {{NULL, "octalzeroes",        OPT_EMULATE|OPT_SH},	 OCTALZEROES},
 {{NULL, "overstrike",	      0},			 OVERSTRIKE},
 {{NULL, "pathdirs",	      OPT_EMULATE},		 PATHDIRS},
+{{NULL, "posixaliases",       OPT_EMULATE|OPT_BOURNE},	 POSIXALIASES},
 {{NULL, "posixbuiltins",      OPT_EMULATE|OPT_BOURNE},	 POSIXBUILTINS},
 {{NULL, "posixidentifiers",   OPT_EMULATE|OPT_BOURNE},	 POSIXIDENTIFIERS},
 {{NULL, "printeightbit",      0},                        PRINTEIGHTBIT},
Index: Src/zsh.h
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/zsh.h,v
retrieving revision 1.153
diff -u -r1.153 zsh.h
--- Src/zsh.h	19 Feb 2009 10:12:39 -0000	1.153
+++ Src/zsh.h	3 Mar 2009 17:22:02 -0000
@@ -1933,6 +1933,7 @@
     OCTALZEROES,
     OVERSTRIKE,
     PATHDIRS,
+    POSIXALIASES,
     POSIXBUILTINS,
     POSIXIDENTIFIERS,
     PRINTEIGHTBIT,
Index: Test/A02alias.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/A02alias.ztst,v
retrieving revision 1.6
diff -u -r1.6 A02alias.ztst
--- Test/A02alias.ztst	27 Mar 2008 09:41:13 -0000	1.6
+++ Test/A02alias.ztst	3 Mar 2009 17:22:02 -0000
@@ -25,3 +25,14 @@
   \bar \bar
 0:Aliasing with a backslash
 >bar
+
+  (alias '!=echo This command has the argument'
+  eval 'print Without
+  ! true'
+  setopt posixaliases
+  eval 'print With
+  ! true')
+1:POSIX_ALIASES option
+>Without
+>This command has the argument true
+>With

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070



Messages sorted by: Reverse Date, Date, Thread, Author