zsh-workers
 help / color / mirror / code / Atom feed
* argzero while sourcing standard scripts
@ 1996-09-16 10:09 Peter Stephenson
  1996-09-16 11:45 ` Peter Stephenson
  1996-09-24 19:42 ` Zoltan Hidvegi
  0 siblings, 2 replies; 4+ messages in thread
From: Peter Stephenson @ 1996-09-16 10:09 UTC (permalink / raw)
  To: Zsh hackers list

Zsh doesn't set argzero (i.e. $0) while sourcing standard scripts,
although it does when sourcing anything else so long as
FUNCTIONARGZERO is set (which it is by default for native emulation
and I can't think of a good reason for turning it off for a `real'
zsh).  This means the error messages from standard scripts are rather
unhelpful in that you don't get the name of the script with the error.

By moving the FUNCTIONARGZERO handling for source/dot down into the
lower level source() function this can be rectified.  

A consequent change is that $0 is the full name of the script found,
so `. foo' will set $0 to the full path to foo unless foo was found in
the current directory.  Maybe this is not what is wanted in this case.
Since it is what is wanted when a full path is given to . or source,
or in any case with the standard scripts, putting this back the way it
was would require passing more information to source().  I am happy to
do this if the older behaviour is preferable.  With ksh there would be
the argument that 'ksh foo', which searches the path for foo, leaves
$0 as foo even if it's in another directory, but zsh doesn't search
the path in that case anyway.

*** Src/builtin.c.source	Mon Sep 16 11:33:26 1996
--- Src/builtin.c	Mon Sep 16 11:37:58 1996
***************
*** 4684,4690 ****
  int
  bin_dot(char *name, char **argv, char *ops, int func)
  {
!     char **old, *old0 = NULL;
      int ret, diddot = 0, dotdot = 0;
      char buf[PATH_MAX];
      char *s, **t, *enam, *arg0;
--- 4684,4690 ----
  int
  bin_dot(char *name, char **argv, char *ops, int func)
  {
!     char **old;
      int ret, diddot = 0, dotdot = 0;
      char buf[PATH_MAX];
      char *s, **t, *enam, *arg0;
***************
*** 4700,4709 ****
  	} LASTALLOC;
      }
      enam = arg0 = ztrdup(*argv);
-     if (isset(FUNCTIONARGZERO)) {
- 	old0 = argzero;
- 	argzero = arg0;
-     }
      s = unmeta(enam);
      errno = ENOENT;
      ret = 1;
--- 4700,4705 ----
***************
*** 4756,4763 ****
      if (ret)
  	zwarnnam(name, "%e: %s", enam, errno);
      zsfree(arg0);
-     if (old0)
- 	argzero = old0;
      return ret ? ret : lastval;
  }
  
--- 4752,4757 ----
*** Src/init.c.source	Mon Sep 16 11:33:30 1996
--- Src/init.c	Mon Sep 16 11:49:33 1996
***************
*** 767,772 ****
--- 767,773 ----
      int tempfd, fd, cj, oldlineno;
      int oldshst, osubsh, oloops;
      FILE *obshin;
+     char *old0 = NULL;
  
      if (!s || (tempfd = movefd(open(unmeta(s), O_RDONLY))) == -1) {
  	return 1;
***************
*** 780,785 ****
--- 781,790 ----
      oldlineno = lineno;          /* store our current lineno                  */
      oloops    = loops;           /* stored the # of nested loops we are in    */
      oldshst   = opts[SHINSTDIN]; /* store current value of this option        */
+     if (isset(FUNCTIONARGZERO)) {
+ 	old0 = argzero;		/* store current name shell is running under  */
+ 	argzero = s;		/* pretend shell is called s                  */
+     }
  
      SHIN = tempfd;
      bshin = fdopen(SHIN, "r");
***************
*** 802,807 ****
--- 807,814 ----
      lineno = oldlineno;              /* our current lineno                   */
      loops = oloops;                  /* the # of nested loops we are in      */
      dosetopt(SHINSTDIN, oldshst, 1); /* SHINSTDIN option                     */
+     if (old0)
+ 	argzero = old0;              /* name under which shell is running    */
      errflag = 0;
      retflag = 0;
      return 0;

-- 
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] 4+ messages in thread

* Re: argzero while sourcing standard scripts
  1996-09-16 10:09 argzero while sourcing standard scripts Peter Stephenson
@ 1996-09-16 11:45 ` Peter Stephenson
  1996-09-24 19:42 ` Zoltan Hidvegi
  1 sibling, 0 replies; 4+ messages in thread
From: Peter Stephenson @ 1996-09-16 11:45 UTC (permalink / raw)
  To: Zsh hackers list

Here's the alternative version of the patch keeping the old behaviour
where $0 is the argument to . or source rather than the file actually
found.  One call to source() gets the original name passed down as
the second argument, the others just get NULL and use the first
argument.

*** Src/builtin.c.source	Mon Sep 16 11:33:26 1996
--- Src/builtin.c	Mon Sep 16 13:37:18 1996
***************
*** 4666,4672 ****
  	if (islogin && !subsh) {
  	    sourcehome(".zlogout");
  #ifdef GLOBAL_ZLOGOUT
!             source(GLOBAL_ZLOGOUT);
  #endif
          }
      }
--- 4666,4672 ----
  	if (islogin && !subsh) {
  	    sourcehome(".zlogout");
  #ifdef GLOBAL_ZLOGOUT
!             source(GLOBAL_ZLOGOUT, NULL);
  #endif
          }
      }
***************
*** 4684,4690 ****
  int
  bin_dot(char *name, char **argv, char *ops, int func)
  {
!     char **old, *old0 = NULL;
      int ret, diddot = 0, dotdot = 0;
      char buf[PATH_MAX];
      char *s, **t, *enam, *arg0;
--- 4684,4690 ----
  int
  bin_dot(char *name, char **argv, char *ops, int func)
  {
!     char **old;
      int ret, diddot = 0, dotdot = 0;
      char buf[PATH_MAX];
      char *s, **t, *enam, *arg0;
***************
*** 4700,4709 ****
  	} LASTALLOC;
      }
      enam = arg0 = ztrdup(*argv);
-     if (isset(FUNCTIONARGZERO)) {
- 	old0 = argzero;
- 	argzero = arg0;
-     }
      s = unmeta(enam);
      errno = ENOENT;
      ret = 1;
--- 4700,4705 ----
***************
*** 4711,4717 ****
      if (*name != '.' && access(s, F_OK) == 0
  	&& stat(s, &st) >= 0 && !S_ISDIR(st.st_mode)) {
  	diddot = 1;
! 	ret = source(enam);
      }
      if (ret) {
  	/* use a path with / in it */
--- 4707,4713 ----
      if (*name != '.' && access(s, F_OK) == 0
  	&& stat(s, &st) >= 0 && !S_ISDIR(st.st_mode)) {
  	diddot = 1;
! 	ret = source(enam, NULL);
      }
      if (ret) {
  	/* use a path with / in it */
***************
*** 4723,4729 ****
  		    else if (arg0[1] == '.' && arg0 + 2 == s)
  			++dotdot;
  		}
! 		ret = source(arg0);
  		break;
  	    }
  	if (!*s || (ret && isset(PATHDIRS) && diddot < 2 && dotdot == 0)) {
--- 4719,4725 ----
  		    else if (arg0[1] == '.' && arg0 + 2 == s)
  			++dotdot;
  		}
! 		ret = source(arg0, NULL);
  		break;
  	    }
  	if (!*s || (ret && isset(PATHDIRS) && diddot < 2 && dotdot == 0)) {
***************
*** 4742,4748 ****
  		s = unmeta(buf);
  		if (access(s, F_OK) == 0 && stat(s, &st) >= 0
  		    && !S_ISDIR(st.st_mode)) {
! 		    ret = source(enam = buf);
  		    break;
  		}
  	    }
--- 4738,4744 ----
  		s = unmeta(buf);
  		if (access(s, F_OK) == 0 && stat(s, &st) >= 0
  		    && !S_ISDIR(st.st_mode)) {
! 		    ret = source(enam = buf, arg0);
  		    break;
  		}
  	    }
***************
*** 4756,4763 ****
      if (ret)
  	zwarnnam(name, "%e: %s", enam, errno);
      zsfree(arg0);
-     if (old0)
- 	argzero = old0;
      return ret ? ret : lastval;
  }
  
--- 4752,4757 ----
*** Src/init.c.source	Mon Sep 16 11:33:30 1996
--- Src/init.c	Mon Sep 16 13:38:17 1996
***************
*** 689,695 ****
  
      if (emulation == EMULATE_KSH || emulation == EMULATE_SH) {
  	if (islogin)
! 	    source("/etc/profile");
  	if (unset(PRIVILEGED)) {
  	    char *s = getsparam("ENV");
  	    if (islogin)
--- 689,695 ----
  
      if (emulation == EMULATE_KSH || emulation == EMULATE_SH) {
  	if (islogin)
! 	    source("/etc/profile", NULL);
  	if (unset(PRIVILEGED)) {
  	    char *s = getsparam("ENV");
  	    if (islogin)
***************
*** 698,711 ****
  	    if (s && !parsestr(s)) {
  		singsub(&s);
  		noerrs = 0;
! 		source(s);
  	    }
  	    noerrs = 0;
  	} else
! 	    source("/etc/suid_profile");
      } else {
  #ifdef GLOBAL_ZSHENV
! 	source(GLOBAL_ZSHENV);
  #endif
  	if (isset(RCS)) {
  	    if (unset(PRIVILEGED))
--- 698,711 ----
  	    if (s && !parsestr(s)) {
  		singsub(&s);
  		noerrs = 0;
! 		source(s, NULL);
  	    }
  	    noerrs = 0;
  	} else
! 	    source("/etc/suid_profile", NULL);
      } else {
  #ifdef GLOBAL_ZSHENV
! 	source(GLOBAL_ZSHENV, NULL);
  #endif
  	if (isset(RCS)) {
  	    if (unset(PRIVILEGED))
***************
*** 712,718 ****
  		sourcehome(".zshenv");
  	    if (islogin) {
  #ifdef GLOBAL_ZPROFILE
! 		source(GLOBAL_ZPROFILE);
  #endif
  		if (unset(PRIVILEGED))
  		    sourcehome(".zprofile");
--- 712,718 ----
  		sourcehome(".zshenv");
  	    if (islogin) {
  #ifdef GLOBAL_ZPROFILE
! 		source(GLOBAL_ZPROFILE, NULL);
  #endif
  		if (unset(PRIVILEGED))
  		    sourcehome(".zprofile");
***************
*** 719,725 ****
  	    }
  	    if (interact) {
  #ifdef GLOBAL_ZSHRC
! 		source(GLOBAL_ZSHRC);
  #endif
  		if (unset(PRIVILEGED))
  		    sourcehome(".zshrc");
--- 719,725 ----
  	    }
  	    if (interact) {
  #ifdef GLOBAL_ZSHRC
! 		source(GLOBAL_ZSHRC, NULL);
  #endif
  		if (unset(PRIVILEGED))
  		    sourcehome(".zshrc");
***************
*** 726,732 ****
  	    }
  	    if (islogin) {
  #ifdef GLOBAL_ZLOGIN
! 		source(GLOBAL_ZLOGIN);
  #endif
  		if (unset(PRIVILEGED))
  		    sourcehome(".zlogin");
--- 726,732 ----
  	    }
  	    if (islogin) {
  #ifdef GLOBAL_ZLOGIN
! 		source(GLOBAL_ZLOGIN, NULL);
  #endif
  		if (unset(PRIVILEGED))
  		    sourcehome(".zlogin");
***************
*** 762,772 ****
  
  /**/
  int
! source(char *s)
  {
      int tempfd, fd, cj, oldlineno;
      int oldshst, osubsh, oloops;
      FILE *obshin;
  
      if (!s || (tempfd = movefd(open(unmeta(s), O_RDONLY))) == -1) {
  	return 1;
--- 762,773 ----
  
  /**/
  int
! source(char *s, char *arg0)
  {
      int tempfd, fd, cj, oldlineno;
      int oldshst, osubsh, oloops;
      FILE *obshin;
+     char *old0 = NULL;
  
      if (!s || (tempfd = movefd(open(unmeta(s), O_RDONLY))) == -1) {
  	return 1;
***************
*** 780,785 ****
--- 781,790 ----
      oldlineno = lineno;          /* store our current lineno                  */
      oloops    = loops;           /* stored the # of nested loops we are in    */
      oldshst   = opts[SHINSTDIN]; /* store current value of this option        */
+     if (isset(FUNCTIONARGZERO)) {
+ 	old0 = argzero;		/* store current name shell is running under  */
+ 	argzero = arg0 ? arg0 : s; /* pretend shell is called arg0 or s       */
+     }
  
      SHIN = tempfd;
      bshin = fdopen(SHIN, "r");
***************
*** 802,807 ****
--- 807,814 ----
      lineno = oldlineno;              /* our current lineno                   */
      loops = oloops;                  /* the # of nested loops we are in      */
      dosetopt(SHINSTDIN, oldshst, 1); /* SHINSTDIN option                     */
+     if (old0)
+ 	argzero = old0;              /* name under which shell is running    */
      errflag = 0;
      retflag = 0;
      return 0;
***************
*** 824,830 ****
  	return;
      }
      sprintf(buf, "%s/%s", h, s);
!     source(buf);
  }
  
  /**/
--- 831,837 ----
  	return;
      }
      sprintf(buf, "%s/%s", h, s);
!     source(buf, NULL);
  }
  
  /**/

-- 
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] 4+ messages in thread

* Re: argzero while sourcing standard scripts
  1996-09-16 10:09 argzero while sourcing standard scripts Peter Stephenson
  1996-09-16 11:45 ` Peter Stephenson
@ 1996-09-24 19:42 ` Zoltan Hidvegi
  1996-09-25  9:59   ` Peter Stephenson
  1 sibling, 1 reply; 4+ messages in thread
From: Zoltan Hidvegi @ 1996-09-24 19:42 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-workers

Peter Stephenson wrote:
> Zsh doesn't set argzero (i.e. $0) while sourcing standard scripts,
> although it does when sourcing anything else so long as
> FUNCTIONARGZERO is set (which it is by default for native emulation
> and I can't think of a good reason for turning it off for a `real'
> zsh).  This means the error messages from standard scripts are rather
> unhelpful in that you don't get the name of the script with the error.

It is certainly wrong that the filename isn't mentioned in the error
message but setting argzero it probably the wrong solution for that.  One
may want to invoke zsh under different names to get customized behaviour.
In the startup files $0 can be used to decide the prefered option settings
etc.  OK, the ZSH_NAME parameter can be used for that.  But when you
execute a script $0 is set to the script name which can be used in zshenv.
Setting $0 to zshenv would make it impossible to get the name of the script
we are executing.

Zoltan


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

* Re: argzero while sourcing standard scripts
  1996-09-24 19:42 ` Zoltan Hidvegi
@ 1996-09-25  9:59   ` Peter Stephenson
  0 siblings, 0 replies; 4+ messages in thread
From: Peter Stephenson @ 1996-09-25  9:59 UTC (permalink / raw)
  To: Zsh hackers list

Zoltan Hidvegi wrote:
> Peter Stephenson wrote:
> > Zsh doesn't set argzero (i.e. $0) while sourcing standard scripts,
> > although it does when sourcing anything else so long as
> > FUNCTIONARGZERO is set (which it is by default for native emulation
> > and I can't think of a good reason for turning it off for a `real'
> > zsh).  This means the error messages from standard scripts are rather
> > unhelpful in that you don't get the name of the script with the error.
> 
> It is certainly wrong that the filename isn't mentioned in the error
> message but setting argzero it probably the wrong solution for that.

Here's another solution in which source() sets a new global variable,
scriptname, whenever it is called and restores it at the end.
scriptname is used by zerr() and zerrnam() (which I missed last time)
if scriptname is set and the SHINSTDIN option isn't, which it won't be
if source() was called.  It has the feature that the error message
contains the path under which the script was found (i.e. a full path
unless relative to the current directory) for any sourced script even
though $0 is the same as what it always has been.  Nothing else should
have changed.

*** Src/globals.h.scrnam	Wed Sep 25 10:56:30 1996
--- Src/globals.h	Wed Sep 25 10:58:32 1996
***************
*** 345,350 ****
--- 345,351 ----
  EXTERN char *argzero;           /* $0 */
   
  EXTERN char *hackzero;
+ EXTERN char *scriptname;        /* name of script being sourced */
  
  EXTERN long lineno;             /* $LINENO       */
  EXTERN long shlvl;              /* $SHLVL        */
*** Src/init.c.scrnam	Wed Sep 25 11:09:29 1996
--- Src/init.c	Wed Sep 25 11:14:27 1996
***************
*** 767,772 ****
--- 767,773 ----
      int tempfd, fd, cj, oldlineno;
      int oldshst, osubsh, oloops;
      FILE *obshin;
+     char *old_scriptname = scriptname;
  
      if (!s || (tempfd = movefd(open(unmeta(s), O_RDONLY))) == -1) {
  	return 1;
***************
*** 787,792 ****
--- 788,794 ----
      lineno = 0;
      loops  = 0;
      dosetopt(SHINSTDIN, 0, 1);
+     scriptname = s;
  
      sourcelevel++;
      loop(0);			/* loop through the file to be sourced        */
***************
*** 804,809 ****
--- 806,813 ----
      dosetopt(SHINSTDIN, oldshst, 1); /* SHINSTDIN option                     */
      errflag = 0;
      retflag = 0;
+     scriptname = old_scriptname;
+ 
      return 0;
  }
  
*** Src/utils.c.scrnam	Wed Sep 25 10:59:43 1996
--- Src/utils.c	Wed Sep 25 11:38:28 1996
***************
*** 52,58 ****
  	return;
      errflag = 1;
      trashzle();
!     nicezputs(isset(SHINSTDIN) ? "zsh" : argzero, stderr);
      fputs(": ", stderr);
      zerrnam(NULL, fmt, str, num);
  }
--- 52,64 ----
  	return;
      errflag = 1;
      trashzle();
!     /*
!      * scriptname is set when sourcing scripts, so that we get the
!      * correct name instead of the generic name of whatever
!      * program/script is running.
!      */
!     nicezputs(isset(SHINSTDIN) ? "zsh" :
! 	      scriptname ? scriptname : argzero, stderr);
      fputs(": ", stderr);
      zerrnam(NULL, fmt, str, num);
  }
***************
*** 67,73 ****
  	errflag = 1;
  	trashzle();
  	if(unset(SHINSTDIN)) {
! 	    nicezputs(argzero, stderr);
  	    fputs(": ", stderr);
  	}
  	nicezputs(cmd, stderr);
--- 73,79 ----
  	errflag = 1;
  	trashzle();
  	if(unset(SHINSTDIN)) {
! 	    nicezputs(scriptname ? scriptname : argzero, stderr);
  	    fputs(": ", stderr);
  	}
  	nicezputs(cmd, stderr);

-- 
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] 4+ messages in thread

end of thread, other threads:[~1996-09-25 10:14 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-09-16 10:09 argzero while sourcing standard scripts Peter Stephenson
1996-09-16 11:45 ` Peter Stephenson
1996-09-24 19:42 ` Zoltan Hidvegi
1996-09-25  9:59   ` 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).