diff options
author | 1996-11-24 17:43:02 +0000 | |
---|---|---|
committer | 1996-11-24 17:43:02 +0000 | |
commit | c5b1166be6f7f1662f1e6cce4198689c7fed031f (patch) | |
tree | 6c8981026f105e64e3b84f4cbcb9cc96f18bed5c | |
parent | removed ref to bitmask_snprintf() (diff) | |
download | wireguard-openbsd-c5b1166be6f7f1662f1e6cce4198689c7fed031f.tar.xz wireguard-openbsd-c5b1166be6f7f1662f1e6cce4198689c7fed031f.zip |
Sync w/ NetBSD (christos)
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.
-rw-r--r-- | bin/sh/eval.c | 8 | ||||
-rw-r--r-- | bin/sh/options.c | 25 | ||||
-rw-r--r-- | bin/sh/options.h | 9 |
3 files changed, 25 insertions, 17 deletions
diff --git a/bin/sh/eval.c b/bin/sh/eval.c index 3e92ad479df..6baeb19ec9f 100644 --- a/bin/sh/eval.c +++ b/bin/sh/eval.c @@ -1,5 +1,5 @@ -/* $OpenBSD: eval.c,v 1.4 1996/10/20 00:54:47 millert Exp $ */ -/* $NetBSD: eval.c,v 1.29.4.1 1996/06/10 19:36:36 jtc Exp $ */ +/* $OpenBSD: eval.c,v 1.5 1996/11/24 17:43:02 millert Exp $ */ +/* $NetBSD: eval.c,v 1.33 1996/11/09 01:04:07 christos Exp $ */ /*- * Copyright (c) 1993 @@ -41,7 +41,7 @@ #if 0 static char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95"; #else -static char sccsid[] = "$NetBSD: eval.c,v 1.29.4.1 1996/06/10 19:36:36 jtc Exp $"; +static char sccsid[] = "$OpenBSD: eval.c,v 1.5 1996/11/24 17:43:02 millert Exp $"; #endif #endif /* not lint */ @@ -745,6 +745,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; @@ -824,7 +825,6 @@ cmddone: } handler = savehandler; if (e != -1) { - outfmt(out2, "exception %d\n", e); if ((e != EXERROR && e != EXEXEC) || cmdentry.u.index == BLTINCMD || cmdentry.u.index == DOTCMD diff --git a/bin/sh/options.c b/bin/sh/options.c index 93d00e8562d..7c7f1c94606 100644 --- a/bin/sh/options.c +++ b/bin/sh/options.c @@ -1,5 +1,5 @@ -/* $OpenBSD: options.c,v 1.4 1996/11/02 05:18:27 millert Exp $ */ -/* $NetBSD: options.c,v 1.14 1995/05/11 21:29:46 christos Exp $ */ +/* $OpenBSD: options.c,v 1.5 1996/11/24 17:43:03 millert Exp $ */ +/* $NetBSD: options.c,v 1.19 1996/11/06 01:17:11 christos Exp $ */ /*- * Copyright (c) 1991, 1993 @@ -41,7 +41,7 @@ #if 0 static char sccsid[] = "@(#)options.c 8.2 (Berkeley) 5/4/95"; #else -static char rcsid[] = "$OpenBSD: options.c,v 1.4 1996/11/02 05:18:27 millert Exp $"; +static char rcsid[] = "$OpenBSD: options.c,v 1.5 1996/11/24 17:43:03 millert Exp $"; #endif #endif /* not lint */ @@ -118,6 +118,7 @@ procargs(argc, argv) arg0 = *argptr++; shellparam.p = argptr; + shellparam.reset = 1; /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */ while (*argptr) { shellparam.nparam++; @@ -361,8 +362,10 @@ void getoptsreset(value) const char *value; { - if (number(value) == 1) + if (number(value) == 1) { shellparam.optnext = NULL; + shellparam.reset = 1; + } } @@ -387,9 +390,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, @@ -414,6 +418,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: @@ -460,18 +466,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; diff --git a/bin/sh/options.h b/bin/sh/options.h index f1cb36a5763..627edc8e650 100644 --- a/bin/sh/options.h +++ b/bin/sh/options.h @@ -1,5 +1,5 @@ -/* $OpenBSD: options.h,v 1.3 1996/10/20 00:54:59 millert Exp $ */ -/* $NetBSD: options.h,v 1.8 1995/05/11 21:29:48 christos Exp $ */ +/* $OpenBSD: options.h,v 1.4 1996/11/24 17:43:03 millert Exp $ */ +/* $NetBSD: options.h,v 1.11 1996/11/06 01:17:12 christos Exp $ */ /*- * Copyright (c) 1991, 1993 @@ -40,8 +40,9 @@ */ 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 */ |