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

Re: argzero while sourcing standard scripts



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