summaryrefslogtreecommitdiffstats
path: root/source/ap/ash/patches/ash-getopt.patch
blob: df88ba0df2307e9f774f81371113071272c80bcd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
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 */
 };