Fix miscellaneous getopts problems:

- the 3 argument version of getopts would not reset properly
- OPTARG did not get cleared after a non argument option was found
- OPTIND was not set properly after a non argument option.
This commit is contained in:
christos 1996-11-06 01:17:10 +00:00
parent b2fad7ebc5
commit ccce082dd2
3 changed files with 24 additions and 15 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: eval.c,v 1.31 1996/10/16 14:31:57 christos Exp $ */
/* $NetBSD: eval.c,v 1.32 1996/11/06 01:17:10 christos Exp $ */
/*-
* Copyright (c) 1993
@ -40,7 +40,7 @@
#if 0
static char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95";
#else
static char sccsid[] = "$NetBSD: eval.c,v 1.31 1996/10/16 14:31:57 christos Exp $";
static char sccsid[] = "$NetBSD: eval.c,v 1.32 1996/11/06 01:17:10 christos Exp $";
#endif
#endif /* not lint */
@ -744,6 +744,7 @@ evalcommand(cmd, flags, backcmd)
redirect(cmd->ncmd.redirect, REDIR_PUSH);
saveparam = shellparam;
shellparam.malloc = 0;
shellparam.reset = 1;
shellparam.nparam = argc - 1;
shellparam.p = argv + 1;
shellparam.optnext = NULL;

View File

@ -1,4 +1,4 @@
/* $NetBSD: options.c,v 1.18 1996/11/02 18:26:05 christos Exp $ */
/* $NetBSD: options.c,v 1.19 1996/11/06 01:17:11 christos Exp $ */
/*-
* Copyright (c) 1991, 1993
@ -40,7 +40,7 @@
#if 0
static char sccsid[] = "@(#)options.c 8.2 (Berkeley) 5/4/95";
#else
static char rcsid[] = "$NetBSD: options.c,v 1.18 1996/11/02 18:26:05 christos Exp $";
static char rcsid[] = "$NetBSD: options.c,v 1.19 1996/11/06 01:17:11 christos Exp $";
#endif
#endif /* not lint */
@ -117,6 +117,7 @@ procargs(argc, argv)
arg0 = *argptr++;
shellparam.p = argptr;
shellparam.reset = 1;
/* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
while (*argptr) {
shellparam.nparam++;
@ -360,8 +361,10 @@ void
getoptsreset(value)
const char *value;
{
if (number(value) == 1)
if (number(value) == 1) {
shellparam.optnext = NULL;
shellparam.reset = 1;
}
}
/*
@ -385,9 +388,10 @@ getoptscmd(argc, argv)
else
optbase = &argv[3];
if (shellparam.optnext == NULL) {
if (shellparam.reset == 1) {
shellparam.optnext = optbase;
shellparam.optptr = NULL;
shellparam.reset = 0;
}
return getopts(argv[1], argv[2], optbase, &shellparam.optnext,
@ -411,6 +415,8 @@ getopts(optstr, optvar, optfirst, optnext, optptr)
if ((p = *optptr) == NULL || *p == '\0') {
/* Current word is done, advance */
if (*optnext == NULL)
return 1;
p = **optnext;
if (p == NULL || *p != '-' || *++p == '\0') {
atend:
@ -456,18 +462,19 @@ atend:
(void) unsetvar("OPTARG");
c = '?';
}
ind = 1;
*optnext = NULL;
goto bad;
}
if (p == **optnext)
(*optnext)++;
ind = *optnext - optfirst + 1;
setvarsafe("OPTARG", p, 0);
p = NULL;
goto out;
}
else
setvarsafe("OPTARG", "", 0);
ind = *optnext - optfirst + 1;
goto out;
bad:
ind = 1;
*optnext = NULL;

View File

@ -1,4 +1,4 @@
/* $NetBSD: options.h,v 1.10 1996/10/16 15:45:13 christos Exp $ */
/* $NetBSD: options.h,v 1.11 1996/11/06 01:17:12 christos Exp $ */
/*-
* Copyright (c) 1991, 1993
@ -39,11 +39,12 @@
*/
struct shparam {
int nparam; /* number of positional parameters (without $0) */
char malloc; /* true if parameter list dynamicly allocated */
int nparam; /* # of positional parameters (without $0) */
unsigned char malloc; /* if parameter list dynamically allocated */
unsigned char reset; /* if getopts has been reset */
char **p; /* parameter list */
char **optnext; /* next parameter to be processed by getopts */
char *optptr; /* used by getopts */
char **optnext; /* next parameter to be processed by getopts */
char *optptr; /* used by getopts */
};