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

argzero while sourcing standard scripts



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@xxxxxx>       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.



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