-
Notifications
You must be signed in to change notification settings - Fork 29
/
my_getopt.c
298 lines (277 loc) · 9.29 KB
/
my_getopt.c
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* my_getopt.c - my re-implementation of getopt.
* Copyright 1997, 2000, 2001, 2002, 2006, Benjamin Sittler
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifdef WIN32
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "my_getopt.h"
int optind=1, opterr=1, optopt=0;
char *optarg=0;
/* reset argument parser to start-up values */
int getopt_reset(void)
{
optind = 1;
opterr = 1;
optopt = 0;
optarg = 0;
return 0;
}
/* this is the plain old UNIX getopt, with GNU-style extensions. */
/* if you're porting some piece of UNIX software, this is all you need. */
/* this supports GNU-style permution and optional arguments */
static int _getopt(int argc, char * argv[], const char *opts)
{
static int charind=0;
const char *s;
char mode, colon_mode;
int off = 0, opt = -1;
if(getenv("POSIXLY_CORRECT")) colon_mode = mode = '+';
else {
if((colon_mode = *opts) == ':') off ++;
if(((mode = opts[off]) == '+') || (mode == '-')) {
off++;
if((colon_mode != ':') && ((colon_mode = opts[off]) == ':'))
off ++;
}
}
optarg = 0;
if(charind) {
optopt = argv[optind][charind];
for(s=opts+off; *s; s++) if(optopt == *s) {
charind++;
if((*(++s) == ':') || ((optopt == 'W') && (*s == ';'))) {
if(argv[optind][charind]) {
optarg = &(argv[optind++][charind]);
charind = 0;
} else if(*(++s) != ':') {
charind = 0;
if(++optind >= argc) {
if(opterr) fprintf(stderr,
"%s: option requires an argument -- %c\n",
argv[0], optopt);
opt = (colon_mode == ':') ? ':' : '?';
goto my_getopt_ok;
}
optarg = argv[optind++];
}
}
opt = optopt;
goto my_getopt_ok;
}
if(opterr) fprintf(stderr,
"%s: illegal option -- %c\n",
argv[0], optopt);
opt = '?';
if(argv[optind][++charind] == '\0') {
optind++;
charind = 0;
}
my_getopt_ok:
if(charind && ! argv[optind][charind]) {
optind++;
charind = 0;
}
} else if((optind >= argc) ||
((argv[optind][0] == '-') &&
(argv[optind][1] == '-') &&
(argv[optind][2] == '\0'))) {
optind++;
opt = -1;
} else if((argv[optind][0] != '-') ||
(argv[optind][1] == '\0')) {
char *tmp;
int i, j, k;
if(mode == '+') opt = -1;
else if(mode == '-') {
optarg = argv[optind++];
charind = 0;
opt = 1;
} else {
for(i=j=optind; i<argc; i++) if((argv[i][0] == '-') &&
(argv[i][1] != '\0')) {
optind=i;
opt=_getopt(argc, argv, opts);
while(i > j) {
tmp=argv[--i];
for(k=i; k+1<optind; k++) argv[k]=argv[k+1];
argv[--optind]=tmp;
}
break;
}
if(i == argc) opt = -1;
}
} else {
charind++;
opt = _getopt(argc, argv, opts);
}
if (optind > argc) optind = argc;
return opt;
}
/* this is the extended getopt_long{,_only}, with some GNU-like
* extensions. Implements _getopt_internal in case any programs
* expecting GNU libc getopt call it.
*/
int _getopt_internal(int argc, char * argv[], const char *shortopts,
const struct option *longopts, int *longind,
int long_only)
{
char mode, colon_mode = *shortopts;
int shortoff = 0, opt = -1;
if(getenv("POSIXLY_CORRECT")) colon_mode = mode = '+';
else {
if((colon_mode = *shortopts) == ':') shortoff ++;
if(((mode = shortopts[shortoff]) == '+') || (mode == '-')) {
shortoff++;
if((colon_mode != ':') && ((colon_mode = shortopts[shortoff]) == ':'))
shortoff ++;
}
}
optarg = 0;
if((optind >= argc) ||
((argv[optind][0] == '-') &&
(argv[optind][1] == '-') &&
(argv[optind][2] == '\0'))) {
optind++;
opt = -1;
} else if((argv[optind][0] != '-') ||
(argv[optind][1] == '\0')) {
char *tmp;
int i, j, k;
opt = -1;
if(mode == '+') return -1;
else if(mode == '-') {
optarg = argv[optind++];
return 1;
}
for(i=j=optind; i<argc; i++) if((argv[i][0] == '-') &&
(argv[i][1] != '\0')) {
optind=i;
opt=_getopt_internal(argc, argv, shortopts,
longopts, longind,
long_only);
while(i > j) {
tmp=argv[--i];
for(k=i; k+1<optind; k++)
argv[k]=argv[k+1];
argv[--optind]=tmp;
}
break;
}
} else if((!long_only) && (argv[optind][1] != '-'))
opt = _getopt(argc, argv, shortopts);
else {
int charind, offset;
int found = 0, ind, hits = 0;
if(((optopt = argv[optind][1]) != '-') && ! argv[optind][2]) {
int c;
ind = shortoff;
while((c = shortopts[ind++])) {
if(((shortopts[ind] == ':') ||
((c == 'W') && (shortopts[ind] == ';'))) &&
(shortopts[++ind] == ':'))
ind ++;
if(optopt == c) return _getopt(argc, argv, shortopts);
}
}
offset = 2 - (argv[optind][1] != '-');
for(charind = offset;
(argv[optind][charind] != '\0') &&
(argv[optind][charind] != '=');
charind++);
for(ind = 0; longopts[ind].name && !hits; ind++)
if((strlen(longopts[ind].name) == (size_t) (charind - offset)) &&
(strncmp(longopts[ind].name,
argv[optind] + offset, charind - offset) == 0))
found = ind, hits++;
if(!hits) for(ind = 0; longopts[ind].name; ind++)
if(strncmp(longopts[ind].name,
argv[optind] + offset, charind - offset) == 0)
found = ind, hits++;
if(hits == 1) {
opt = 0;
if(argv[optind][charind] == '=') {
if(longopts[found].has_arg == 0) {
opt = '?';
if(opterr) fprintf(stderr,
"%s: option `--%s' doesn't allow an argument\n",
argv[0], longopts[found].name);
} else {
optarg = argv[optind] + ++charind;
charind = 0;
}
} else if(longopts[found].has_arg == 1) {
if(++optind >= argc) {
opt = (colon_mode == ':') ? ':' : '?';
if(opterr) fprintf(stderr,
"%s: option `--%s' requires an argument\n",
argv[0], longopts[found].name);
} else optarg = argv[optind];
}
if(!opt) {
if (longind) *longind = found;
if(!longopts[found].flag) opt = longopts[found].val;
else *(longopts[found].flag) = longopts[found].val;
}
optind++;
} else if(!hits) {
if(offset == 1) opt = _getopt(argc, argv, shortopts);
else {
opt = '?';
if(opterr) fprintf(stderr,
"%s: unrecognized option `%s'\n",
argv[0], argv[optind++]);
}
} else {
opt = '?';
if(opterr) fprintf(stderr,
"%s: option `%s' is ambiguous\n",
argv[0], argv[optind++]);
}
}
if (optind > argc) optind = argc;
return opt;
}
/* This function is kinda problematic because most getopt() nowadays
seem to use char * const argv[] (they DON'T permute the options list),
but this one does. So we remove it as long as HAVE_GETOPT is define, so
people can use the version from their platform instead */
#ifndef HAVE_GETOPT
int getopt(int argc, char * argv[], const char *opts)
{
return _getopt(argc, argv, opts);
}
#endif /* HAVE_GETOPT */
int getopt_long(int argc, char * argv[], const char *shortopts,
const struct option *longopts, int *longind)
{
return _getopt_internal(argc, argv, shortopts, longopts, longind, 0);
}
int getopt_long_only(int argc, char * argv[], const char *shortopts,
const struct option *longopts, int *longind)
{
return _getopt_internal(argc, argv, shortopts, longopts, longind, 1);
}
#endif