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

PATCH: 3.1.4: getopts not reset properly for new function



There have been various getopts() patches, but this hadn't been fixed
up to the last I saw, Bernd Eggink's in
http://www.zsh.org/mla/workers/1998/msg00132.html
which looks like it hasn't appeared yet.

However, my link to www.zsh.org is awful, involving 22 hops and more
delays than Gatwick Airport, so I couldn't check for anything more
recent.  We could do with a mirror north of the Equator, even if it
was only the index.

Strange, though, because it's a fairly blatant bug...

% zsh -f
% fn() { local f; while getopts abcd f; do print $f; done; }
% fn -a
a
% fn -abc
b
c
% fn -abc
%

(Workaround, if you get very frustrated in the meanwhile:  finish all
options with a -:
% fn -abcd -
a
b
c
d
% fn -a -   
a
% fn -abc -
a
b
c
% fn -abc -
a
b
c
.)

*** Src/builtin.c.opt	Tue Oct  6 11:57:57 1998
--- Src/builtin.c	Tue Oct  6 12:04:37 1998
***************
*** 2537,2542 ****
--- 2537,2545 ----
      return ret;
  }
  
+ /**/
+ int optcind;
+ 
  /* getopts: automagical option handling for shell scripts */
  
  /**/
***************
*** 2546,2552 ****
      int lenstr, lenoptstr, quiet, lenoptbuf;
      char *optstr = unmetafy(*argv++, &lenoptstr), *var = *argv++;
      char **args = (*argv) ? argv : pparams;
-     static int optcind = 0;
      char *str, optbuf[2], *p, opch;
  
      /* zoptind keeps count of the current argument number.  The *
--- 2549,2554 ----
*** Src/exec.c.opt	Mon Sep 28 11:37:19 1998
--- Src/exec.c	Tue Oct  6 12:06:13 1998
***************
*** 2654,2660 ****
   * was executed.                                            */
  {
      char **tab, **x, *oargv0 = NULL;
!     int xexittr, newexittr, oldzoptind, oldlastval;
      char *ou;
      void *xexitfn, *newexitfn;
      char saveopts[OPT_SIZE];
--- 2654,2660 ----
   * was executed.                                            */
  {
      char **tab, **x, *oargv0 = NULL;
!     int xexittr, newexittr, oldzoptind, oldlastval, oldoptcind;
      char *ou;
      void *xexitfn, *newexitfn;
      char saveopts[OPT_SIZE];
***************
*** 2673,2680 ****
--- 2673,2683 ----
  	sigtrapped[SIGEXIT] = 0;
  	sigfuncs[SIGEXIT] = NULL;
  	tab = pparams;
+ 	/* Reset getopts variables for new function */
  	oldzoptind = zoptind;
  	zoptind = 1;
+ 	oldoptcind = optcind;
+ 	optcind = 0;
  
  	/* We need to save the current options even if LOCALOPTIONS is *
  	 * not currently set.  That's because if it gets set in the    *
***************
*** 2721,2726 ****
--- 2724,2730 ----
  	    argzero = oargv0;
  	}
  	zoptind = oldzoptind;
+ 	optcind = oldoptcind;
  	pparams = tab;
  
  	if (isset(LOCALOPTIONS)) {

-- 
Peter Stephenson <pws@xxxxxxxxxxxxxxxxx>       Tel: +39 050 844536
WWW:  http://www.ifh.de/~pws/
Gruppo Teorico, Dipartimento di Fisica
Piazza Torricelli 2, 56100 Pisa, Italy



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