1
0
Fork 0
mirror of git://slackware.nl/current.git synced 2025-02-15 08:50:09 +01:00
slackware-current/source/ap/ash/patches/ash-getopt.patch
Patrick J Volkerding 5a12e7c134 Slackware 13.0
Wed Aug 26 10:00:38 CDT 2009
Slackware 13.0 x86_64 is released as stable!  Thanks to everyone who
helped make this release possible -- see the RELEASE_NOTES for the
credits.  The ISOs are off to the replicator.  This time it will be a
6 CD-ROM 32-bit set and a dual-sided 32-bit/64-bit x86/x86_64 DVD.
We're taking pre-orders now at store.slackware.com.  Please consider
picking up a copy to help support the project.  Once again, thanks to
the entire Slackware community for all the help testing and fixing
things and offering suggestions during this development cycle.
As always, have fun and enjoy!  -P.
2018-05-31 22:41:17 +02:00

198 lines
4.5 KiB
Diff

diff -urN netbsd-sh/options.c ash-0.3.7.orig/options.c
--- netbsd-sh/options.c Fri Jul 9 13:02:07 1999
+++ ash-0.3.7.orig/options.c Mon Apr 23 22:16:46 2001
@@ -79,7 +79,7 @@
STATIC void options __P((int));
STATIC void minus_o __P((char *, int));
STATIC void setoption __P((int, int));
-STATIC int getopts __P((char *, char *, char **, char ***, char **));
+STATIC int getopts __P((char *, char *, char **, int *, int *));
/*
@@ -118,7 +118,8 @@
arg0 = *argptr++;
shellparam.p = argptr;
- shellparam.reset = 1;
+ shellparam.optind = 1;
+ shellparam.optoff = -1;
/* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
while (*argptr) {
shellparam.nparam++;
@@ -282,7 +283,8 @@
shellparam.malloc = 1;
shellparam.nparam = nparam;
shellparam.p = newparam;
- shellparam.optnext = NULL;
+ shellparam.optind = 1;
+ shellparam.optoff = -1;
}
@@ -330,7 +332,8 @@
}
ap2 = shellparam.p;
while ((*ap2++ = *ap1++) != NULL);
- shellparam.optnext = NULL;
+ shellparam.optind = 1;
+ shellparam.optoff = -1;
INTON;
return 0;
}
@@ -363,10 +366,8 @@
getoptsreset(value)
const char *value;
{
- if (number(value) == 1) {
- shellparam.optnext = NULL;
- shellparam.reset = 1;
- }
+ shellparam.optind = number(value);
+ shellparam.optoff = -1;
}
/*
@@ -385,50 +386,58 @@
if (argc < 3)
error("Usage: getopts optstring var [arg]");
- else if (argc == 3)
+ else if (argc == 3) {
optbase = shellparam.p;
- else
+ if (shellparam.optind > shellparam.nparam + 1) {
+ shellparam.optind = 1;
+ shellparam.optoff = -1;
+ }
+ }
+ else {
optbase = &argv[3];
-
- if (shellparam.reset == 1) {
- shellparam.optnext = optbase;
- shellparam.optptr = NULL;
- shellparam.reset = 0;
+ if (shellparam.optind > argc - 2) {
+ shellparam.optind = 1;
+ shellparam.optoff = -1;
+ }
}
- return getopts(argv[1], argv[2], optbase, &shellparam.optnext,
- &shellparam.optptr);
+ return getopts(argv[1], argv[2], optbase, &shellparam.optind,
+ &shellparam.optoff);
}
STATIC int
-getopts(optstr, optvar, optfirst, optnext, optpptr)
+getopts(optstr, optvar, optfirst, optind, optoff)
char *optstr;
char *optvar;
char **optfirst;
- char ***optnext;
- char **optpptr;
+ int *optind;
+ int *optoff;
{
char *p, *q;
char c = '?';
int done = 0;
- int ind = 0;
int err = 0;
char s[10];
+ char **optnext = optfirst + *optind - 1;
- if ((p = *optpptr) == NULL || *p == '\0') {
+ if (*optind <= 1 || *optoff < 0 || !(*(optnext - 1)) ||
+ strlen(*(optnext - 1)) < *optoff)
+ p = NULL;
+ else
+ p = *(optnext - 1) + *optoff;
+ if (p == NULL || *p == '\0') {
/* Current word is done, advance */
- if (*optnext == NULL)
+ if (optnext == NULL)
return 1;
- p = **optnext;
+ p = *optnext;
if (p == NULL || *p != '-' || *++p == '\0') {
atend:
- ind = *optnext - optfirst + 1;
- *optnext = NULL;
+ *optind = optnext - optfirst + 1;
p = NULL;
done = 1;
goto out;
}
- (*optnext)++;
+ optnext++;
if (p[0] == '-' && p[1] == '\0') /* check for "--" */
goto atend;
}
@@ -453,7 +462,7 @@
}
if (*++q == ':') {
- if (*p == '\0' && (p = **optnext) == NULL) {
+ if (*p == '\0' && (p = *optnext) == NULL) {
if (optstr[0] == ':') {
s[0] = c;
s[1] = '\0';
@@ -468,30 +477,29 @@
goto bad;
}
- if (p == **optnext)
- (*optnext)++;
+ if (p == *optnext)
+ optnext++;
setvarsafe("OPTARG", p, 0);
p = NULL;
}
else
setvarsafe("OPTARG", "", 0);
- ind = *optnext - optfirst + 1;
+ *optind = optnext - optfirst + 1;
goto out;
bad:
- ind = 1;
- *optnext = NULL;
+ *optind = 1;
p = NULL;
out:
- *optpptr = p;
- fmtstr(s, sizeof(s), "%d", ind);
+ *optoff = p ? p - *(optnext - 1) : -1;
+ fmtstr(s, sizeof(s), "%d", *optind);
err |= setvarsafe("OPTIND", s, VNOFUNC);
s[0] = c;
s[1] = '\0';
err |= setvarsafe(optvar, s, 0);
if (err) {
- *optnext = NULL;
- *optpptr = NULL;
+ *optind = 1;
+ *optoff = -1;
flushall();
exraise(EXERROR);
}
diff -urN netbsd-sh/options.h ash-0.3.7.orig/options.h
--- netbsd-sh/options.h Fri Jul 9 13:02:07 1999
+++ ash-0.3.7.orig/options.h Mon Apr 23 22:16:46 2001
@@ -41,10 +41,9 @@
struct shparam {
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 */
+ int optind; /* next parameter to be processed by getopts */
+ int optoff; /* used by getopts */
};