zsh-workers
 help / color / mirror / code / Atom feed
From: Bernd Eggink <eggink@rrz.uni-hamburg.de>
To: zsh workers mailing list <zsh-workers@math.gatech.edu>
Cc: "Christopher T. White" <chris@adei.com>
Subject: Re: bug in getopts
Date: Sun, 25 May 1997 23:57:16 +0200	[thread overview]
Message-ID: <3388B5BC.23485FC9@rrz.uni-hamburg.de> (raw)
In-Reply-To: <3.0.1.32.19970523124527.0068bae4@zin.adei.com>

[-- Attachment #1: Type: text/plain, Size: 758 bytes --]

Christopher T. White wrote:
> 
> Dear ZSH users and workers,
> 
> Forgive me if this has been addressed previously, but I haven't run into
> this particular situation before.
> 
> Getops seems to be broken in zsh version 3.1.0. 

It's broken in 3.0.2, too.

I looked into the sources and think I found the bug(s). Edit the file
zsh-3.1.0/Src/buitin.c, look for the function 'bin_getopts' and replace
that function by the attached version. I marked the changes with /*!! 
(sorry, I'm not yet familiar with the patch utility).

After rebuild ingand reinstalling zsh, getopts should work correctly.

Regards,
	Bernd

--- 
Bernd Eggink
Regionales Rechenzentrum der Universitaet Hamburg
eggink@rrz.uni-hamburg.de
http://www.rrz.uni-hamburg.de/eggink/BEggink.html

[-- Attachment #2: getopts --]
[-- Type: text/plain, Size: 2383 bytes --]

int
bin_getopts(char *name, char **argv, char *ops, int func)
{
    int lenstr, lenoptstr, i;
    char *optstr = unmetafy(*argv++, &lenoptstr), *var = *argv++;
    char **args = (*argv) ? argv : pparams;
    static int optcind = 1, quiet;
    char *str, optbuf[2], *opch = optbuf + 1;

    /* zoptind keeps count of the current argument number */
    if (zoptind < 1)
	/* first call */
	zoptind = 1;
    if (zoptind == 1)
{ /*!! added*/
	quiet = 0;
    optbuf[0] = '+';
    zsfree(zoptarg);
    zoptarg = ztrdup("");
    setsparam(var, ztrdup(""));
    if (*optstr == ':') {
	quiet = 1;
	optstr++;
    }
}	/*!! added*/
    if (zoptind > arrlen(args))
	return 1;
    str = unmetafy(args[zoptind - 1], &lenstr);
    if ((*str != '+' && *str != '-') || optcind >= lenstr ||
	(lenstr == 2 && str[0] == '-' && str[1] == '-')) {
	/* current argument doesn't contain options, or optcind is impossibly
	large */
	if (*str == '+' || *str == '-')
	    zoptind++;
	optcind = 0;
	return 1;
    }
    /* Get the option character.  optcind records the current position within
    the argument. */
    if (!optcind)
	optcind = 1;
    *opch = str[optcind++];
    if (optcind == lenstr) {
	if(args[zoptind++])
	    str = unmetafy(args[zoptind - 1], &lenstr);
	optcind = 0;
    }
    /* look for option in the provided optstr */
    for (i = 0; i != lenoptstr; i++)
	if (*opch == optstr[i])
	    break;
    if (i == lenoptstr) {
	/* not a valid option */
	setsparam(var, ztrdup("?"));
	if (quiet) {
	    zsfree(zoptarg);
	    zoptarg = metafy(opch, 1, META_DUP);
	    return 0;
	}
	zerr("bad option: -%c", NULL, *opch);
	errflag = 0;
	return 0;
    }
    /* copy option into specified parameter, with + if required */
    setsparam(var, metafy(opch - (*str == '+'), 1 + (*str == '+'), META_DUP));
    /* handle case of an expected extra argument */
    if (optstr[i + 1] == ':') { /*!! changed 1 to i + 1 */
	if (!args[zoptind - 1]) {
	    /* no extra argument was provided */
	    if (quiet) {
		zsfree(zoptarg);
		zoptarg = metafy(opch, 1, META_DUP);
		setsparam(var, ztrdup(":"));
		return 0;
	    }
	    setsparam(var, ztrdup("?"));
	    zerr("argument expected after -%c option", NULL, *opch);
	    errflag = 0;
	    return 0;
	}
	/* skip over the extra argument */
	zsfree(zoptarg);
	zoptarg = metafy(str + optcind, lenstr - optcind, META_DUP);
	zoptind++;
	optcind = 0;
    }
    return 0;
}



           reply	other threads:[~1997-05-25 22:00 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <3.0.1.32.19970523124527.0068bae4@zin.adei.com>]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3388B5BC.23485FC9@rrz.uni-hamburg.de \
    --to=eggink@rrz.uni-hamburg.de \
    --cc=chris@adei.com \
    --cc=zsh-workers@math.gatech.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).