Debian Codegraph @

src/cdogs/XGetopt.c

raw symbols SCIP
1 /*
2 getopt.c - implementation of getopt(3) and getopt_long(3)
3 Copyright Keristor Systems and Chris Croughton 1997
4 Internet: [email protected]
5
6 Permission is granted to anyone to use this software for any purpose,
7 including commercial applications, and to alter it and redistribute it
8 freely, subject to the following restrictions:
9
10 1. The origin of this software must not be misrepresented; you must not
11 claim that you wrote the original software. If you use this software
12 in a product, an acknowledgment in the product documentation would be
13 appreciated but is not required.
14 2. Altered source versions must be plainly marked as such, and must not be
15 misrepresented as being the original software.
16 3. This notice may not be removed or altered from any source distribution.
17
18 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 POSSIBILITY OF SUCH DAMAGE.
29 */
30 #ifdef _WIN32
31
32 #include <stdlib.h>
33 #include <string.h>
34 #include <stdio.h>
35
36 #define XGETOPT_NAMES
37 #include "XGetopt.h"
38
39 /*
40 * This is the published interface for the GNU getopt module
41 */
42
43 char *optarg = NULL;
44 int optind = 0;
45 int opterr = 1;
46 int optopt = 0;
47
48 /************************************************************************
49 *
50 * Everything from here down is a rewrite without looking at the GNU
51 * implementation; it is therefore free of GPL and LGPL contraints, see
52 * above for licencing information.
53 *
54 ************************************************************************/
55
56 #define true 1
57 #define false 0
58 #define bool char
59
60 enum action
61 {
62 E_PERMUTE,
63 E_POSIX,
64 E_INPLACE,
65 E_NUM_OF_ACTIONS
66 };
67
68 static bool initialised = false;
69 static bool endargs = false;
70 static char *nextchar = NULL;
71
72 /* Note that the lookup routines use and modify the global variables - not
73 * nice, but better than passing everything as reference parameters.
74 */
75
76 static int
77 lookup_shortopt(int argc, char * const *argv, const char *opts)
78 {
79 int c = *optarg++;
80 /* remember the Alamo - a dodgy character if ever there was one */
81 nextchar = optarg;
82 /* look at the options - like any you see? */
83 while (*opts)
84 {
85 if (c == *opts++)
86 {
87 /* OK, it's a fair (c)op, guv */
88 if (*opts == ':')
89 {
90 /* we can do arguments, but there will be nothing left of this arg */
91 nextchar = NULL;
92 if (*++opts == ':')
93 {
94 /* but it's optional, so either a 5 minute argument or nothing */
95 if (!*optarg)
96 optarg = NULL;
97 return c;
98 }
99 else
100 {
101 /* this argument's not optional, you get the full half hour */
102 if (*optarg)
103 {
104 /* We've got an argument on our hands! */
105 return c;
106 }
107 else
108 {
109 /* try the next door down */
110 if (++optind < argc)
111 {
112 /* OK, we've got it ("Oh no you haven't!") */
113 optarg = argv[optind];
114 return c;
115 }
116 else
117 {
118 /* Th-Th-Th-That's All, Folks! */
119 optopt = c;
120 if (opterr)
121 fprintf(stderr, "required parameter for -%c missing\n", c);
122 /* we've got an option, but it's dud */
123 return '?';
124 }
125 }
126 }
127 }
128 else
129 {
130 /* no arguments please, we're British */
131 nextchar = optarg;
132 optarg = NULL;
133 return c;
134 }
135 }
136 }
137 /* return to sender, address unknown */
138 optopt = c;
139 optarg = NULL;
140 return -1;
141 }
142
143 static int lookup_longopt(
144 int argc, char *const *argv,
145 const struct option *longopts,
146 int *longindex)
147 {
148 char *eqp;
149 bool ambig = false;
150 int match = -1;
151 int exact = -1;
152 int min;
153 int len;
154 int i;
155
156 /* if we have no long options, any we get are wrong */
157 if (!longopts)
158 {
159 if (opterr)
160 fprintf(stderr, "long options not supported\n");
161 return '?';
162 }
163 /* look for equality or fraternity */
164 eqp = strchr(optarg, '=');
165 min = (eqp ? (int)(eqp - optarg) : (int)strlen(optarg));
166 for (i = 0; longopts[i].name; i++)
167 {
168 len = (int)strlen(longopts[i].name);
169 /* is it exact? */
170 if (len == min && strncmp(longopts[i].name, optarg, len) == 0)
171 {
172 /* an exact match - go no further, we can't get better than this */
173 exact = i;
174 break;
175 }
176 /* a match as far as the argument goes? */
177 if (strncmp(longopts[i].name, optarg, min) == 0)
178 {
179 /* if we've already found one of those, it's ambiguous */
180 if (match < 0)
181 match = i;
182 else
183 ambig = true;
184 }
185 }
186 /* we've looked at everything, now what? */
187 if (exact < 0)
188 {
189 /* no exact match, if it's ambiguous then fail othersise use what
190 * (if anything) we have */
191 if (ambig)
192 {
193 if (opterr)
194 fprintf(stderr, "option --%s is ambiguous\n", optarg);
195 return '?';
196 }
197 else
198 exact = match;
199 }
200 /* if we don't have anything, return -1 (/not/ fail, this could be a test */
201 if (exact < 0)
202 return -1;
203 switch (longopts[exact].has_arg)
204 {
205 case no_argument:
206 /* we have nothing to fight about... */
207 if (eqp)
208 {
209 /* ... except equality */
210 if (opterr)
211 fprintf(stderr, "option --%s does not take an argument\n", optarg);
212 return '?';
213 }
214 optarg = NULL;
215 break;
216 case required_argument:
217 /* you have to fight, so there! */
218 if (eqp)
219 optarg = eqp + 1;
220 else if (++optind < argc)
221 optarg = argv[optind];
222 else
223 {
224 if (opterr)
225 fprintf(stderr, "missing argument for option --%s\n", optarg);
226 return '?';
227 }
228 break;
229 case optional_argument:
230 /* you don't have to fight if you don't want to */
231 optarg = (eqp ? eqp + 1 : NULL);
232 break;
233 }
234 /* tell the caller what he's got */
235 if (longindex)
236 *longindex = exact;
237 if (longopts[exact].flag)
238 {
239 /* set a flag rather than returning the value */
240 *longopts[exact].flag = longopts[exact].val;
241 return 0;
242 }
243 return longopts[exact].val;
244 }
245
246 /*
247 * Note that this internal function drops the const on the arg list. That's
248 * because optarg etc. are defined without const.
249 */
250 static int getnextopt(
251 int argc, char *const *argv, const char *optstring,
252 const struct option *longopts, int *longindex,
253 bool longonly, enum action act)
254 {
255 static int thisind = 0;
256 static int oldind = 0;
257 int c = 0;
258
259 /* if we haven't started, make sure we're not in Denmark */
260 if (!initialised)
261 {
262 initialised = true;
263 nextchar = NULL;
264 endargs = false;
265 thisind = 0;
266 oldind = 0;
267 optind = 0;
268 }
269
270 /* check for previous sequence of short options */
271 if (nextchar && *nextchar)
272 {
273 optarg = nextchar;
274 c = lookup_shortopt(argc, argv, optstring);
275 /* now shuffle the arguments if necessary */
276 if (act == E_PERMUTE && oldind != thisind && (!nextchar || !*nextchar))
277 {
278 int optlen = optind - thisind; /* the length of the option */
279 int optnum = thisind - oldind; /* number of args in between */
280 memmove(argv+oldind+optlen+1, argv+oldind, sizeof(*argv) * optnum);
281 optind = oldind + optlen;
282 }
283 return c;
284 }
285
286 /* check for end of argument list */
287 if (++optind >= argc || !argv[optind])
288 {
289 optarg = NULL;
290 return EOF; /* no arguments left */
291 }
292
293 /* get next argument from list */
294 optarg = argv[optind];
295
296 /* if we've had "--" then treat as normal argument */
297 if (endargs)
298 return (act == E_INPLACE ? '\1' : EOF);
299
300 /* if we're permuting the arguments, look for the next option */
301 if (act == E_PERMUTE)
302 {
303 int i;
304 /* save where we are, we'll need this later */
305 oldind = optind;
306 for (i = optind; i < argc; i++)
307 {
308 char *p = argv[i];
309 /* remember that "--" ends options? Ignore anything past it... */
310 if (strcmp(p, "--") == 0)
311 {
312 int optnum = i - oldind; /* number of args in between */
313 if (optnum > 0)
314 {
315 /* shuffle the arguments up over it */
316 memmove(argv+oldind+1, argv+oldind, sizeof(*argv) * optnum);
317 optind = ++oldind;
318 optarg = argv[optind];
319 }
320 break;
321 }
322 if (*p == '-' && strcmp(p, "-"))
323 {
324 /* we found a honest-to-goodness option! Set up shop here... */
325 optind = i;
326 optarg = p;
327 break;
328 }
329 }
330 }
331
332 /* test for "--" meaning "end of options" - everything after that is an
333 * argument not an option */
334 if (strcmp(optarg, "--") == 0)
335 {
336 endargs = true;
337 if (++optind < argc)
338 {
339 optarg = argv[optind];
340 return (act == E_INPLACE ? '\1' : EOF);
341 }
342 else
343 {
344 optarg = NULL;
345 return EOF;
346 }
347 }
348
349 /* an argument of "-" or not starting with '-'is not an option */
350 if (strcmp(optarg, "-") == 0 || *optarg != '-')
351 {
352 /* if we're being POSIXLY_CORRECT (ugh!) then the first non-option ends
353 * the options. I don't like it but that's what the spec. says... */
354 if (act == E_POSIX)
355 endargs = true;
356 return (act == E_INPLACE ? '\1' : EOF);
357 }
358
359 thisind = optind; /* remember this position, lookup changes it */
360
361 /* OK, it's some sort of option, junk the first '-' */
362 optarg++;
363
364 /* test the next character - another '-' means it's a long option */
365 if (longopts && *optarg == '-')
366 {
367 /* it's a long option, skip the next '-' */
368 optarg++;
369 c = lookup_longopt(argc, argv, longopts, longindex);
370 if (c < 0)
371 {
372 fprintf(stderr, "unknown option --%s\n", optarg);
373 c = '?';
374 }
375 }
376 else if (longopts && longonly)
377 {
378 int tmperr = opterr;
379 opterr = 0;
380 /* try a long option first, if that doesn't work try a short one */
381 c = lookup_longopt(argc, argv, longopts, longindex);
382 opterr = tmperr;
383 if (c < 0)
384 c = lookup_shortopt(argc, argv, optstring);
385 }
386 else
387 {
388 /* just try short options */
389 c = lookup_shortopt(argc, argv, optstring);
390 }
391
392 /* if not found give (optional) error and return '?' */
393 if (c < 0)
394 {
395 if (opterr)
396 fprintf(stderr, "unknown option -%c\n", optopt);
397 optarg = 0;
398 c = '?';
399 }
400
401 /* now shuffle the arguments if necessary - I told you we'd need to save
402 * that old index! (I wonder if there's a more elegant way to do this?) */
403 if (act == E_PERMUTE && oldind != thisind && (!nextchar || !*nextchar))
404 {
405 int optlen = optind - thisind; /* the length of the option */
406 int optnum = thisind - oldind; /* number of args in between */
407 memmove(argv+oldind+optlen+1, argv+oldind, sizeof(*argv) * optnum);
408 optind = oldind + optlen;
409 oldind = 0;
410 thisind = 0;
411 }
412 return c;
413 }
414
415
416 /***********************************************************************
417 * At last, the real routines we wanted to call!
418 **********************************************************************/
419
420 /**
421 * Scan the command-line parameters for options in the form \b -x.
422 */
423 int getopt(int argc, char * const argv[], const char *optstring)
424 {
425 return getnextopt(
426 argc, (char *const *)argv, optstring, NULL, NULL,
427 false, E_PERMUTE);
428 }
429
430 /**
431 * Scan the command-line parameters for options, allowing both the short
432 * (single character) options and long (string) options.
433 */
434 int getopt_long(int argc, char * argv[], const char *optstring,
435 const struct option *longopts, int *longindex)
436 {
437 return getnextopt(
438 argc, (char *const *)argv, optstring, longopts, longindex,
439 false, E_PERMUTE);
440 }
441
442 /**
443 * Scan the command-line parameters for long options only
444 */
445 int getopt_long_only(int argc, char * argv[], const char *optstring,
446 const struct option *longopts, int *longindex)
447 {
448 return getnextopt(
449 argc, (char *const *)argv, optstring, longopts, longindex,
450 true, E_PERMUTE);
451 }
452
453 #endif