DPsim
Loading...
Searching...
No Matches
getopt.h
1// editorconfig-checker-disable-file
2#ifndef __GETOPT_H__
12/*
13 * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>
14 *
15 * Permission to use, copy, modify, and distribute this software for any
16 * purpose with or without fee is hereby granted, provided that the above
17 * copyright notice and this permission notice appear in all copies.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
20 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
22 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
23 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
24 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
25 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 *
27 * Sponsored in part by the Defense Advanced Research Projects
28 * Agency (DARPA) and Air Force Research Laboratory, Air Force
29 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
30 */
31/*-
32 * Copyright (c) 2000 The NetBSD Foundation, Inc.
33 * All rights reserved.
34 *
35 * This code is derived from software contributed to The NetBSD Foundation
36 * by Dieter Baron and Thomas Klausner.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
48 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
49 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
50 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
51 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
53 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57 * POSSIBILITY OF SUCH DAMAGE.
58 */
59
60#pragma warning(disable : 4996)
61
62#define __GETOPT_H__
63
64/* All the headers include this file. */
65#include <crtdefs.h>
66#include <errno.h>
67#include <stdarg.h>
68#include <stdio.h>
69#include <stdlib.h>
70#include <string.h>
71#include <windows.h>
72
73#ifdef __cplusplus
74extern "C" {
75#endif
76
77#define REPLACE_GETOPT /* use this getopt as the system getopt(3) */
78
79#ifdef REPLACE_GETOPT
80int opterr = 1; /* if error message should be printed */
81int optind = 1; /* index into parent argv vector */
82int optopt = '?'; /* character checked for validity */
83#undef optreset /* see getopt.h */
84#define optreset __mingw_optreset
85int optreset; /* reset getopt */
86char *optarg; /* argument associated with option */
87#endif
88
89#define PRINT_ERROR ((opterr) && (*options != ':'))
90
91#define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */
92#define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */
93#define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */
94
95/* return values */
96#define BADCH (int)'?'
97#define BADARG ((*options == ':') ? (int)':' : (int)'?')
98#define INORDER (int)1
99
100#ifndef __CYGWIN__
101#define __progname __argv[0]
102#else
103extern char __declspec(dllimport) * __progname;
104#endif
105
106#ifdef __CYGWIN__
107static char EMSG[] = "";
108#else
109#define EMSG ""
110#endif
111
112static int getopt_internal(int, char *const *, const char *,
113 const struct option *, int *, int);
114static int parse_long_options(char *const *, const char *,
115 const struct option *, int *, int);
116static int gcd(int, int);
117static void permute_args(int, int, int, char *const *);
118
119static char *place = EMSG; /* option letter processing */
120
121/* XXX: set optreset to 1 rather than these two */
122static int nonopt_start = -1; /* first non option argument (for permute) */
123static int nonopt_end = -1; /* first option after non options (for permute) */
124
125/* Error messages */
126static const char recargchar[] = "option requires an argument -- %c";
127static const char recargstring[] = "option requires an argument -- %s";
128static const char ambig[] = "ambiguous option -- %.*s";
129static const char noarg[] = "option doesn't take an argument -- %.*s";
130static const char illoptchar[] = "unknown option -- %c";
131static const char illoptstring[] = "unknown option -- %s";
132
133static void _vwarnx(const char *fmt, va_list ap) {
134 (void)fprintf(stderr, "%s: ", __progname);
135 if (fmt != NULL)
136 (void)vfprintf(stderr, fmt, ap);
137 (void)fprintf(stderr, "\n");
138}
139
140static void warnx(const char *fmt, ...) {
141 va_list ap;
142 va_start(ap, fmt);
143 _vwarnx(fmt, ap);
144 va_end(ap);
145}
146
147/*
148 * Compute the greatest common divisor of a and b.
149 */
150static int gcd(int a, int b) {
151 int c;
152
153 c = a % b;
154 while (c != 0) {
155 a = b;
156 b = c;
157 c = a % b;
158 }
159
160 return (b);
161}
162
163/*
164 * Exchange the block from nonopt_start to nonopt_end with the block
165 * from nonopt_end to opt_end (keeping the same order of arguments
166 * in each block).
167 */
168static void permute_args(int panonopt_start, int panonopt_end, int opt_end,
169 char *const *nargv) {
170 int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
171 char *swap;
172
173 /*
174 * compute lengths of blocks and number and size of cycles
175 */
176 nnonopts = panonopt_end - panonopt_start;
177 nopts = opt_end - panonopt_end;
178 ncycle = gcd(nnonopts, nopts);
179 cyclelen = (opt_end - panonopt_start) / ncycle;
180
181 for (i = 0; i < ncycle; i++) {
182 cstart = panonopt_end + i;
183 pos = cstart;
184 for (j = 0; j < cyclelen; j++) {
185 if (pos >= panonopt_end)
186 pos -= nnonopts;
187 else
188 pos += nopts;
189 swap = nargv[pos];
190 /* LINTED const cast */
191 ((char **)nargv)[pos] = nargv[cstart];
192 /* LINTED const cast */
193 ((char **)nargv)[cstart] = swap;
194 }
195 }
196}
197
198#ifdef REPLACE_GETOPT
199/*
200 * getopt --
201 * Parse argc/argv argument vector.
202 *
203 * [eventually this will replace the BSD getopt]
204 */
205int getopt(int nargc, char *const *nargv, const char *options) {
206
207 /*
208 * We don't pass FLAG_PERMUTE to getopt_internal() since
209 * the BSD getopt(3) (unlike GNU) has never done this.
210 *
211 * Furthermore, since many privileged programs call getopt()
212 * before dropping privileges it makes sense to keep things
213 * as simple (and bug-free) as possible.
214 */
215 return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
216}
217#endif /* REPLACE_GETOPT */
218
219//extern int getopt(int nargc, char * const *nargv, const char *options);
220
221#ifdef _BSD_SOURCE
222/*
223 * BSD adds the non-standard `optreset' feature, for reinitialisation
224 * of `getopt' parsing. We support this feature, for applications which
225 * proclaim their BSD heritage, before including this header; however,
226 * to maintain portability, developers are advised to avoid it.
227 */
228#define optreset __mingw_optreset
229extern int optreset;
230#endif
231#ifdef __cplusplus
232}
233#endif
234/*
235 * POSIX requires the `getopt' API to be specified in `unistd.h';
236 * thus, `unistd.h' includes this header. However, we do not want
237 * to expose the `getopt_long' or `getopt_long_only' APIs, when
238 * included in this manner. Thus, close the standard __GETOPT_H__
239 * declarations block, and open an additional __GETOPT_LONG_H__
240 * specific block, only when *not* __UNISTD_H_SOURCED__, in which
241 * to declare the extended API.
242 */
243#endif /* !defined(__GETOPT_H__) */
244
245#if !defined(__UNISTD_H_SOURCED__) && !defined(__GETOPT_LONG_H__)
246#define __GETOPT_LONG_H__
247
248#ifdef __cplusplus
249extern "C" {
250#endif
251
252struct option /* specification for a long form option... */
253{
254 const char *name; /* option name, without leading hyphens */
255 int has_arg; /* does it take an argument? */
256 int *flag; /* where to save its status, or NULL */
257 int val; /* its associated status value */
258};
259
260enum /* permitted values for its `has_arg' field... */
261{
262 no_argument = 0, /* option never takes an argument */
263 required_argument, /* option always requires an argument */
264 optional_argument /* option may take an argument */
265};
266
267/*
268 * parse_long_options --
269 * Parse long options in argc/argv argument vector.
270 * Returns -1 if short_too is set and the option does not match long_options.
271 */
272static int parse_long_options(char *const *nargv, const char *options,
273 const struct option *long_options, int *idx,
274 int short_too) {
275 char *current_argv, *has_equal;
276 size_t current_argv_len;
277 int i, ambiguous, match;
278
279#define IDENTICAL_INTERPRETATION(_x, _y) \
280 (long_options[(_x)].has_arg == long_options[(_y)].has_arg && \
281 long_options[(_x)].flag == long_options[(_y)].flag && \
282 long_options[(_x)].val == long_options[(_y)].val)
283
284 current_argv = place;
285 match = -1;
286 ambiguous = 0;
287
288 optind++;
289
290 if ((has_equal = strchr(current_argv, '=')) != NULL) {
291 /* argument found (--option=arg) */
292 current_argv_len = has_equal - current_argv;
293 has_equal++;
294 } else
295 current_argv_len = strlen(current_argv);
296
297 for (i = 0; long_options[i].name; i++) {
298 /* find matching long option */
299 if (strncmp(current_argv, long_options[i].name, current_argv_len))
300 continue;
301
302 if (strlen(long_options[i].name) == current_argv_len) {
303 /* exact match */
304 match = i;
305 ambiguous = 0;
306 break;
307 }
308 /*
309 * If this is a known short option, don't allow
310 * a partial match of a single character.
311 */
312 if (short_too && current_argv_len == 1)
313 continue;
314
315 if (match == -1) /* partial match */
316 match = i;
317 else if (!IDENTICAL_INTERPRETATION(i, match))
318 ambiguous = 1;
319 }
320 if (ambiguous) {
321 /* ambiguous abbreviation */
322 if (PRINT_ERROR)
323 warnx(ambig, (int)current_argv_len, current_argv);
324 optopt = 0;
325 return (BADCH);
326 }
327 if (match != -1) { /* option found */
328 if (long_options[match].has_arg == no_argument && has_equal) {
329 if (PRINT_ERROR)
330 warnx(noarg, (int)current_argv_len, current_argv);
331 /*
332 * XXX: GNU sets optopt to val regardless of flag
333 */
334 if (long_options[match].flag == NULL)
335 optopt = long_options[match].val;
336 else
337 optopt = 0;
338 return (BADARG);
339 }
340 if (long_options[match].has_arg == required_argument ||
341 long_options[match].has_arg == optional_argument) {
342 if (has_equal)
343 optarg = has_equal;
344 else if (long_options[match].has_arg == required_argument) {
345 /*
346 * optional argument doesn't use next nargv
347 */
348 optarg = nargv[optind++];
349 }
350 }
351 if ((long_options[match].has_arg == required_argument) &&
352 (optarg == NULL)) {
353 /*
354 * Missing argument; leading ':' indicates no error
355 * should be generated.
356 */
357 if (PRINT_ERROR)
358 warnx(recargstring, current_argv);
359 /*
360 * XXX: GNU sets optopt to val regardless of flag
361 */
362 if (long_options[match].flag == NULL)
363 optopt = long_options[match].val;
364 else
365 optopt = 0;
366 --optind;
367 return (BADARG);
368 }
369 } else { /* unknown option */
370 if (short_too) {
371 --optind;
372 return (-1);
373 }
374 if (PRINT_ERROR)
375 warnx(illoptstring, current_argv);
376 optopt = 0;
377 return (BADCH);
378 }
379 if (idx)
380 *idx = match;
381 if (long_options[match].flag) {
382 *long_options[match].flag = long_options[match].val;
383 return (0);
384 } else
385 return (long_options[match].val);
386#undef IDENTICAL_INTERPRETATION
387}
388
389/*
390 * getopt_internal --
391 * Parse argc/argv argument vector. Called by user level routines.
392 */
393static int getopt_internal(int nargc, char *const *nargv, const char *options,
394 const struct option *long_options, int *idx,
395 int flags) {
396 char *oli; /* option letter list index */
397 int optchar, short_too;
398 static int posixly_correct = -1;
399
400 if (options == NULL)
401 return (-1);
402
403 /*
404 * XXX Some GNU programs (like cvs) set optind to 0 instead of
405 * XXX using optreset. Work around this braindamage.
406 */
407 if (optind == 0)
408 optind = optreset = 1;
409
410 /*
411 * Disable GNU extensions if POSIXLY_CORRECT is set or options
412 * string begins with a '+'.
413 *
414 * CV, 2009-12-14: Check POSIXLY_CORRECT anew if optind == 0 or
415 * optreset != 0 for GNU compatibility.
416 */
417 if (posixly_correct == -1 || optreset != 0)
418 posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
419 if (*options == '-')
420 flags |= FLAG_ALLARGS;
421 else if (posixly_correct || *options == '+')
422 flags &= ~FLAG_PERMUTE;
423 if (*options == '+' || *options == '-')
424 options++;
425
426 optarg = NULL;
427 if (optreset)
428 nonopt_start = nonopt_end = -1;
429start:
430 if (optreset || !*place) { /* update scanning pointer */
431 optreset = 0;
432 if (optind >= nargc) { /* end of argument vector */
433 place = EMSG;
434 if (nonopt_end != -1) {
435 /* do permutation, if we have to */
436 permute_args(nonopt_start, nonopt_end, optind, nargv);
437 optind -= nonopt_end - nonopt_start;
438 } else if (nonopt_start != -1) {
439 /*
440 * If we skipped non-options, set optind
441 * to the first of them.
442 */
443 optind = nonopt_start;
444 }
445 nonopt_start = nonopt_end = -1;
446 return (-1);
447 }
448 if (*(place = nargv[optind]) != '-' ||
449 (place[1] == '\0' && strchr(options, '-') == NULL)) {
450 place = EMSG; /* found non-option */
451 if (flags & FLAG_ALLARGS) {
452 /*
453 * GNU extension:
454 * return non-option as argument to option 1
455 */
456 optarg = nargv[optind++];
457 return (INORDER);
458 }
459 if (!(flags & FLAG_PERMUTE)) {
460 /*
461 * If no permutation wanted, stop parsing
462 * at first non-option.
463 */
464 return (-1);
465 }
466 /* do permutation */
467 if (nonopt_start == -1)
468 nonopt_start = optind;
469 else if (nonopt_end != -1) {
470 permute_args(nonopt_start, nonopt_end, optind, nargv);
471 nonopt_start = optind - (nonopt_end - nonopt_start);
472 nonopt_end = -1;
473 }
474 optind++;
475 /* process next argument */
476 goto start;
477 }
478 if (nonopt_start != -1 && nonopt_end == -1)
479 nonopt_end = optind;
480
481 /*
482 * If we have "-" do nothing, if "--" we are done.
483 */
484 if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
485 optind++;
486 place = EMSG;
487 /*
488 * We found an option (--), so if we skipped
489 * non-options, we have to permute.
490 */
491 if (nonopt_end != -1) {
492 permute_args(nonopt_start, nonopt_end, optind, nargv);
493 optind -= nonopt_end - nonopt_start;
494 }
495 nonopt_start = nonopt_end = -1;
496 return (-1);
497 }
498 }
499
500 /*
501 * Check long options if:
502 * 1) we were passed some
503 * 2) the arg is not just "-"
504 * 3) either the arg starts with -- we are getopt_long_only()
505 */
506 if (long_options != NULL && place != nargv[optind] &&
507 (*place == '-' || (flags & FLAG_LONGONLY))) {
508 short_too = 0;
509 if (*place == '-')
510 place++; /* --foo long option */
511 else if (*place != ':' && strchr(options, *place) != NULL)
512 short_too = 1; /* could be short option too */
513
514 optchar = parse_long_options(nargv, options, long_options, idx, short_too);
515 if (optchar != -1) {
516 place = EMSG;
517 return (optchar);
518 }
519 }
520
521 if ((optchar = (int)*place++) == (int)':' ||
522 (optchar == (int)'-' && *place != '\0') ||
523 (oli = (char *)strchr(options, optchar)) == NULL) {
524 /*
525 * If the user specified "-" and '-' isn't listed in
526 * options, return -1 (non-option) as per POSIX.
527 * Otherwise, it is an unknown option character (or ':').
528 */
529 if (optchar == (int)'-' && *place == '\0')
530 return (-1);
531 if (!*place)
532 ++optind;
533 if (PRINT_ERROR)
534 warnx(illoptchar, optchar);
535 optopt = optchar;
536 return (BADCH);
537 }
538 if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
539 /* -W long-option */
540 if (*place) /* no space */
541 /* NOTHING */;
542 else if (++optind >= nargc) { /* no arg */
543 place = EMSG;
544 if (PRINT_ERROR)
545 warnx(recargchar, optchar);
546 optopt = optchar;
547 return (BADARG);
548 } else /* white space */
549 place = nargv[optind];
550 optchar = parse_long_options(nargv, options, long_options, idx, 0);
551 place = EMSG;
552 return (optchar);
553 }
554 if (*++oli != ':') { /* doesn't take argument */
555 if (!*place)
556 ++optind;
557 } else { /* takes (optional) argument */
558 optarg = NULL;
559 if (*place) /* no white space */
560 optarg = place;
561 else if (oli[1] != ':') { /* arg not optional */
562 if (++optind >= nargc) { /* no arg */
563 place = EMSG;
564 if (PRINT_ERROR)
565 warnx(recargchar, optchar);
566 optopt = optchar;
567 return (BADARG);
568 } else
569 optarg = nargv[optind];
570 }
571 place = EMSG;
572 ++optind;
573 }
574 /* dump back option letter */
575 return (optchar);
576}
577
578/*
579 * getopt_long --
580 * Parse argc/argv argument vector.
581 */
582int getopt_long(int nargc, char *const *nargv, const char *options,
583 const struct option *long_options, int *idx) {
584
585 return (
586 getopt_internal(nargc, nargv, options, long_options, idx, FLAG_PERMUTE));
587}
588
589/*
590 * getopt_long_only --
591 * Parse argc/argv argument vector.
592 */
593int getopt_long_only(int nargc, char *const *nargv, const char *options,
594 const struct option *long_options, int *idx) {
595
596 return (getopt_internal(nargc, nargv, options, long_options, idx,
597 FLAG_PERMUTE | FLAG_LONGONLY));
598}
599
600//extern int getopt_long(int nargc, char * const *nargv, const char *options,
601// const struct option *long_options, int *idx);
602//extern int getopt_long_only(int nargc, char * const *nargv, const char *options,
603// const struct option *long_options, int *idx);
604/*
605 * Previous MinGW implementation had...
606 */
607#ifndef HAVE_DECL_GETOPT
608/*
609 * ...for the long form API only; keep this for compatibility.
610 */
611#define HAVE_DECL_GETOPT 1
612#endif
613
614#ifdef __cplusplus
615}
616#endif
617
618#endif /* !defined(__UNISTD_H_SOURCED__) && !defined(__GETOPT_LONG_H__) */