zsh-workers
 help / color / mirror / code / Atom feed
* "Array" variables that aren't set, and NO_UNSET
@ 1996-07-22 19:35 Bart Schaefer
  1996-07-23 12:17 ` Peter Stephenson
  0 siblings, 1 reply; 2+ messages in thread
From: Bart Schaefer @ 1996-07-22 19:35 UTC (permalink / raw)
  To: zsh-workers

Consider:

zsh% setopt | grep set
nounset             off
zsh% echo ${thisisnotavariable}

zsh% echo ${thisisnotavariable[1]}
zsh: closing brace expected
zsh% 

Why is it a *syntax* error to subscript a variable that's not set, when
it is not a syntax error to refer to the string value of such a variable?

-- 
Bart Schaefer                             Brass Lantern Enterprises
http://www.well.com/user/barts            http://www.nbn.com/people/lantern

New male in /home/schaefer:
>N  2 Justin William Schaefer  Sat May 11 03:43  53/4040  "Happy Birthday"



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

* Re: "Array" variables that aren't set, and NO_UNSET
  1996-07-22 19:35 "Array" variables that aren't set, and NO_UNSET Bart Schaefer
@ 1996-07-23 12:17 ` Peter Stephenson
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 1996-07-23 12:17 UTC (permalink / raw)
  To: Zsh hackers list

schaefer@candle.brasslantern.com wrote:
> Consider:
> 
> zsh% setopt | grep set
> nounset             off
> zsh% echo ${thisisnotavariable}
> 
> zsh% echo ${thisisnotavariable[1]}
> zsh: closing brace expected
> zsh% 
> 
> Why is it a *syntax* error to subscript a variable that's not set, when
> it is not a syntax error to refer to the string value of such a variable?

This does seem inconsistent.  The problem is that getvalue() doesn't
bother parsing any further if it finds the variable is unset.

This patch is one suggestion, though I suspect there may be others.
Parsing continues (that's the first hunk), and one then has to be
careful that the parameter struct v->pm is not dereferenced since it
may now be null (that's all the rest).  The change to check this in
getstrvalue() is necessary; the changes in getintvalue() and
getarrvalue() shouldn't be if everything is going according to plan,
but I have made them anyway.

It's very intricate, so it's rather hard to be completely certain
v->pm won't get dereferenced.  Perhaps there's a better way of doing
it.  Make v->pm instead reference a dummy scalar?  That's hard too,
since if getvalue() returns non-zero then v->pm is liable to be
hijacked and altered.  I'm not sure there's currently any way of making
readonlyness watertight.

*** Src/params.c~	Mon Jul  1 19:10:53 1996
--- Src/params.c	Tue Jul 23 13:49:28 1996
***************
*** 549,560 ****
  	if (sav)
  	    *s = sav;
  	*pptr = s;
- 	if (!pm || (pm->flags & PM_UNSET))
- 	    return NULL;
  	v = (Value) hcalloc(sizeof *v);
! 	if (PM_TYPE(pm->flags) == PM_ARRAY)
  	    v->isarr = isvarat ? -1 : 1;
! 	v->pm = pm;
  	v->inv = 0;
  	v->a = 0;
  	v->b = -1;
--- 549,558 ----
  	if (sav)
  	    *s = sav;
  	*pptr = s;
  	v = (Value) hcalloc(sizeof *v);
! 	if (pm && PM_TYPE(pm->flags) == PM_ARRAY)
  	    v->isarr = isvarat ? -1 : 1;
! 	v->pm = (pm && !(pm->flags & PM_UNSET)) ? pm : NULL;
  	v->inv = 0;
  	v->a = 0;
  	v->b = -1;
***************
*** 602,608 ****
  			while (*s != ']' && *s != Outbrack)
  			    s++;
  			*pptr = s;
! 			return v;
  		    }
  		} else {
  		    if (a > 0)
--- 600,606 ----
  			while (*s != ']' && *s != Outbrack)
  			    s++;
  			*pptr = s;
! 			return v->pm ? v : NULL;
  		    }
  		} else {
  		    if (a > 0)
***************
*** 642,648 ****
  	zerr("subscript to %s: %d", (v->b < 0) ? "small" : "big", v->b);
  	return NULL;
      }
!     return v;
  }
  
  /**/
--- 640,646 ----
  	zerr("subscript to %s: %d", (v->b < 0) ? "small" : "big", v->b);
  	return NULL;
      }
!     return v->pm ? v : NULL;
  }
  
  /**/
***************
*** 652,658 ****
      char *s, **ss;
      static char buf[(SIZEOF_LONG * 8) + 4];
  
!     if (!v)
  	return "";
      if (v->inv) {
  	sprintf(buf, "%d", v->a);
--- 650,656 ----
      char *s, **ss;
      static char buf[(SIZEOF_LONG * 8) + 4];
  
!     if (!v || !v->pm)
  	return "";
      if (v->inv) {
  	sprintf(buf, "%d", v->a);
***************
*** 704,710 ****
  {
      char **s;
  
!     if (!v)
  	return arrdup(nular);
      if (v->inv) {
  	char buf[DIGBUFSIZE];
--- 702,708 ----
  {
      char **s;
  
!     if (!v || !v->pm)
  	return arrdup(nular);
      if (v->inv) {
  	char buf[DIGBUFSIZE];
***************
*** 736,742 ****
  long
  getintvalue(Value v)
  {
!     if (!v || v->isarr)
  	return 0;
      if (v->inv)
  	return v->a;
--- 734,740 ----
  long
  getintvalue(Value v)
  {
!     if (!v || !v->pm || v->isarr)
  	return 0;
      if (v->inv)
  	return v->a;

-- 
Peter Stephenson <pws@ifh.de>       Tel: +49 33762 77366
WWW:  http://www.ifh.de/~pws/       Fax: +49 33762 77330
Deutches Electronen-Synchrotron --- Institut fuer Hochenergiephysik Zeuthen
DESY-IfH, 15735 Zeuthen, Germany.



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

end of thread, other threads:[~1996-07-23 12:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-07-22 19:35 "Array" variables that aren't set, and NO_UNSET Bart Schaefer
1996-07-23 12:17 ` 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).