zsh-workers
 help / color / mirror / code / Atom feed
* Re: set -A
       [not found] <20030207000138.GK5418@msi.ld>
  2003-02-07 11:53 ` set -A Peter Stephenson
@ 2003-02-11  8:45 ` Oliver Kiddle
  2003-02-11 10:38   ` Peter Stephenson
  1 sibling, 1 reply; 4+ messages in thread
From: Oliver Kiddle @ 2003-02-11  8:45 UTC (permalink / raw)
  To: Paul Ackersviller; +Cc: zsh-workers

On 6 Feb, you wrote:
> I think this is a bug in all versions, but if there's some reason
> the behaviour could be considered correct, it'd be nice if it at
> least worked like ksh in emulation mode.  I recently had some trouble
> getting a ksh script to work properly in zsh, as illustrated by:
> 
> unset arr
> set -A arr "${arr[@]}" first
> [ ${#arr[@]} = 2 ] && echo "arr[0]='${arr[0]}' arr[1]='${arr[1]}'"
> 
> Note the null entry on the start of the array.  I believe the above
> should work as in ksh, and consistently with positional parameters,
> like this:

I'm inclined to agree that this is wrong. It would seem more logical to
me for an unset variable to be interpreted as an empty array when
retrieved in array context. A similar thing which I find annoying is
that local defines parameters to be scalar by default instead of
leaving their type undefined causing similar empty element problems.

I've been aware of this for some while and had intended to change it if
I ever have time to finish work on the parameter code. Would changing
it break much? It would improve bash/ksh compatibility.

Oliver

This e-mail and any attachment is for authorised use by the intended recipient(s) only.  It may contain proprietary material, confidential information and/or be subject to legal privilege.  It should not be copied, disclosed to, retained or used by, any other party.  If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender.  Thank you.


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

* Re: set -A
  2003-02-11  8:45 ` Oliver Kiddle
@ 2003-02-11 10:38   ` Peter Stephenson
  0 siblings, 0 replies; 4+ messages in thread
From: Peter Stephenson @ 2003-02-11 10:38 UTC (permalink / raw)
  To: zsh-workers

Oliver Kiddle wrote:
> I'm inclined to agree that this is wrong. It would seem more logical to
> me for an unset variable to be interpreted as an empty array when
> retrieved in array context. 
>...
> I've been aware of this for some while and had intended to change it if
> I ever have time to finish work on the parameter code. Would changing
> it break much? It would improve bash/ksh compatibility.

I suspect it would improve matters in the particular case of `[@]' --- I
see no occasion when someone would deliberately use that in the
expectation it caused "$foo[@]" to turn into a zero-length string for an
empty $foo of any type.  However, note there is in general no such thing
as `array context' in zsh, so we need to be careful with other subscripts.

> A similar thing which I find annoying is
> that local defines parameters to be scalar by default instead of
> leaving their type undefined causing similar empty element problems.

There's no such thing as an undefined type for a declared parameter,
either, nor is it clear what that would mean.  You have to declare it with
a particular type.  You need to declare it as `local -a' if you want it
to behave as an array.  I think the problem is few people have yet got
into the habit of doing this (though increasing use of += makes it more
necessary).

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential 
and/or privileged material. 
Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by 
persons or entities other than the intended recipient is 
prohibited.  
If you received this in error, please contact the sender and 
delete the material from any computer.
**********************************************************************


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

* Re: set -A
  2003-02-07 11:53 ` set -A Peter Stephenson
@ 2003-02-07 16:08   ` Bart Schaefer
  0 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2003-02-07 16:08 UTC (permalink / raw)
  To: Zsh hackers list

On Feb 7, 11:53am, Peter Stephenson wrote:
}
} Here's a patch which gives the ksh behaviour when KSH_ARRAYS is set; it
} could do with more testing of all the possible cases, since the logic
} isn't entirely trivial.

It -looks- right to me.

} As well as describing this, I've also tried to make the documentation
} more explicit about argument and option behaviour; this could do with a
} read through by other people who might spot things I've described badly
} (= Bart).

I think this is fine too.  About the only other thing I'd have done is
stick in a `pindex(KSH_ARRAYS, use of)'.


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

* Re: set -A
       [not found] <20030207000138.GK5418@msi.ld>
@ 2003-02-07 11:53 ` Peter Stephenson
  2003-02-07 16:08   ` Bart Schaefer
  2003-02-11  8:45 ` Oliver Kiddle
  1 sibling, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2003-02-07 11:53 UTC (permalink / raw)
  To: Paul Ackersviller, Zsh hackers list

Paul Ackersviller wrote:
> Secondly, I have more of an observation than a bug report that better
> fits the subject since it has to do with parsing after the -A of set.
> 	set -A arr -x
> puts -x into the beginning of the array, whereas ksh treats the -x as
> an option to set instead.  In ksh
> 	set -A arr -- -x
> would get around that, but the same command in zsh now put the -- into
> the array.  I think either way could be considered correct, but it
> would be nicer if the same command had the same effect in both shells.

[Hope I've got the commas in the right place this time.]

Yes, this is quite a nasty incompatibility --- particularly since this
is the standard way to set arrays in ksh.  I don't think there's any
chance of changing zsh's native behaviour now --- I tried it and it
immediately fell over in one of my own functions as soon as I started
the shell.

Here's a patch which gives the ksh behaviour when KSH_ARRAYS is set; it
could do with more testing of all the possible cases, since the logic
isn't entirely trivial.

As well as describing this, I've also tried to make the documentation
more explicit about argument and option behaviour; this could do with a
read through by other people who might spot things I've described badly
(= Bart).

Index: Doc/Zsh/builtins.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/builtins.yo,v
retrieving revision 1.55
diff -u -r1.55 builtins.yo
--- Doc/Zsh/builtins.yo	5 Feb 2003 11:57:08 -0000	1.55
+++ Doc/Zsh/builtins.yo	7 Feb 2003 11:50:07 -0000
@@ -976,14 +976,34 @@
 ifnzman(noderef(Options))\
 .  Flags may be specified by name using the tt(-o) option.
 
-If the tt(-A) flag is specified, var(name) is
-set to an array containing the given var(arg)s. if tt(PLUS()A) is used and
-var(name) is an array, the given arguments will replace the initial
-elements of that array; if no var(name) is specified, all arrays are
-printed.  Otherwise the positional parameters are set.  If no arguments are
-given, then the names and values of all parameters are printed on the
-standard output.  If the only argument is `tt(PLUS())',
-the names of all parameters are printed.
+If the tt(-A) flag is specified, var(name) is set to an array containing
+the given var(arg)s; if no var(name) is specified, all arrays are printed
+together with their values.
+
+If tt(PLUS()A) is used and var(name) is an array, the
+given arguments will replace the initial elements of that array; if no
+var(name) is specified, all arrays are printed without their values.
+
+The behaviour of arguments after tt(-A) var(name) or tt(PLUS()A) var(name)
+depends on whether the option tt(KSH_ARRAYS) is set.  If it is not set, all
+arguments following var(name) are treated as values for the array,
+regardless of their form.  If the option is set, normal option processing
+continues at that point; only regular arguments are treated as values for
+the array.  This means that
+
+example(set -A array -x -- foo)
+
+sets tt(array) to `tt(-x -- foo)' if tt(KSH_ARRAYS) is not set, but sets
+the array to tt(foo) and turns on the option `tt(-x)' if it is set.
+
+If the tt(-A) flag is not present, but there are arguments beyond the
+options, the positional parameters are set.  If the option list (if any)
+is terminated by `tt(-)tt(-)', and there are no further arguments, the
+positional parameters will be unset.
+
+If no arguments and no `tt(-)tt(-)' are given, then the names and values of
+all parameters are printed on the standard output.  If the only argument is
+`tt(PLUS())', the names of all parameters are printed.
 )
 module(setcap)(zsh/cap)
 findex(setopt)
Index: Src/builtin.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v
retrieving revision 1.94
diff -u -r1.94 builtin.c
--- Src/builtin.c	5 Feb 2003 11:57:09 -0000	1.94
+++ Src/builtin.c	7 Feb 2003 11:50:07 -0000
@@ -531,7 +531,7 @@
 {
     int action, optno, array = 0, hadopt = 0,
 	hadplus = 0, hadend = 0, sort = 0;
-    char **x;
+    char **x, *arrayname = NULL;
 
     /* Obsolescent sh compatibility: set - is the same as set +xv *
      * and set - args is the same as set +xv -- args              */
@@ -575,7 +575,15 @@
 		if(!*++*args)
 		    args++;
 		array = action ? 1 : -1;
-		goto doneoptions;
+		arrayname = *args;
+		if (!arrayname)
+		    goto doneoptions;
+		else if  (!isset(KSHARRAYS))
+		{
+		    args++;
+		    goto doneoptions;
+		}
+		break;
 	    } else if (**args == 's')
 		sort = action ? 1 : -1;
 	    else {
@@ -592,30 +600,31 @@
 
     /* Show the parameters, possibly with values */
     queue_signals();
-    if (!hadopt && !*args)
-	scanhashtable(paramtab, 1, 0, 0, paramtab->printnode,
-		      hadplus ? PRINT_NAMEONLY : 0);
-
-    if (array && !*args) {
-	/* display arrays */
-	scanhashtable(paramtab, 1, PM_ARRAY, 0, paramtab->printnode,
-		      hadplus ? PRINT_NAMEONLY : 0);
-    }
-    if (!*args && !hadend) {
-	unqueue_signals();
-	return 0;
+    if (!arrayname)
+    {
+	if (!hadopt && !*args)
+	    scanhashtable(paramtab, 1, 0, 0, paramtab->printnode,
+			  hadplus ? PRINT_NAMEONLY : 0);
+
+	if (array) {
+	    /* display arrays */
+	    scanhashtable(paramtab, 1, PM_ARRAY, 0, paramtab->printnode,
+			  hadplus ? PRINT_NAMEONLY : 0);
+	}
+	if (!*args && !hadend) {
+	    unqueue_signals();
+	    return 0;
+	}
     }
-    if (array)
-	args++;
     if (sort)
 	qsort(args, arrlen(args), sizeof(char *),
 	      sort > 0 ? strpcmp : invstrpcmp);
     if (array) {
 	/* create an array with the specified elements */
-	char **a = NULL, **y, *name = args[-1];
+	char **a = NULL, **y;
 	int len = arrlen(args);
 
-	if (array < 0 && (a = getaparam(name))) {
+	if (array < 0 && (a = getaparam(arrayname))) {
 	    int al = arrlen(a);
 
 	    if (al > len)
@@ -627,7 +636,7 @@
 	    *y++ = ztrdup(*args++);
 	}
 	*y++ = NULL;
-	setaparam(name, x);
+	setaparam(arrayname, x);
     } else {
 	/* set shell arguments */
 	freearray(pparams);

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential 
and/or privileged material. 
Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by 
persons or entities other than the intended recipient is 
prohibited.  
If you received this in error, please contact the sender and 
delete the material from any computer.
**********************************************************************


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

end of thread, other threads:[~2003-02-11 10:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20030207000138.GK5418@msi.ld>
2003-02-07 11:53 ` set -A Peter Stephenson
2003-02-07 16:08   ` Bart Schaefer
2003-02-11  8:45 ` Oliver Kiddle
2003-02-11 10:38   ` 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).