1 //===-- GetOptInc.cpp -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Host/common/GetOptInc.h"
12 #if defined(REPLACE_GETOPT) || defined(REPLACE_GETOPT_LONG) || \
13 defined(REPLACE_GETOPT_LONG_ONLY)
20 #if defined(REPLACE_GETOPT)
21 int opterr = 1; /* if error message should be printed */
22 int optind = 1; /* index into parent argv vector */
23 int optopt = '?'; /* character checked for validity */
24 int optreset; /* reset getopt */
25 char *optarg; /* argument associated with option */
28 #define PRINT_ERROR ((opterr) && (*options != ':'))
30 #define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */
31 #define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */
32 #define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */
35 #define BADCH (int)'?'
36 #define BADARG ((*options == ':') ? (int)':' : (int)'?')
37 #define INORDER (int)1
41 static int getopt_internal(int, char *const *, const char *,
42 const struct option *, int *, int);
43 static int parse_long_options(char *const *, const char *,
44 const struct option *, int *, int);
45 static int gcd(int, int);
46 static void permute_args(int, int, int, char *const *);
48 static const char *place = EMSG; /* option letter processing */
50 /* XXX: set optreset to 1 rather than these two */
51 static int nonopt_start = -1; /* first non option argument (for permute) */
52 static int nonopt_end = -1; /* first option after non options (for permute) */
55 * Compute the greatest common divisor of a and b.
57 static int gcd(int a, int b) {
71 #define warnx(a, ...) pass();
74 * Exchange the block from nonopt_start to nonopt_end with the block
75 * from nonopt_end to opt_end (keeping the same order of arguments
78 static void permute_args(int panonopt_start, int panonopt_end, int opt_end,
80 int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
84 * compute lengths of blocks and number and size of cycles
86 nnonopts = panonopt_end - panonopt_start;
87 nopts = opt_end - panonopt_end;
88 ncycle = gcd(nnonopts, nopts);
89 cyclelen = (opt_end - panonopt_start) / ncycle;
91 for (i = 0; i < ncycle; i++) {
92 cstart = panonopt_end + i;
94 for (j = 0; j < cyclelen; j++) {
95 if (pos >= panonopt_end)
100 /* LINTED const cast */
101 ((char **)nargv)[pos] = nargv[cstart];
102 /* LINTED const cast */
103 ((char **)nargv)[cstart] = swap;
109 * parse_long_options --
110 * Parse long options in argc/argv argument vector.
111 * Returns -1 if short_too is set and the option does not match long_options.
113 static int parse_long_options(char *const *nargv, const char *options,
114 const struct option *long_options, int *idx,
116 char *current_argv, *has_equal;
117 size_t current_argv_len;
120 current_argv = const_cast<char *>(place);
125 if ((has_equal = strchr(current_argv, '=')) != NULL) {
126 /* argument found (--option=arg) */
127 current_argv_len = has_equal - current_argv;
130 current_argv_len = strlen(current_argv);
132 for (i = 0; long_options[i].name; i++) {
133 /* find matching long option */
134 if (strncmp(current_argv, long_options[i].name, current_argv_len))
137 if (strlen(long_options[i].name) == current_argv_len) {
143 * If this is a known short option, don't allow
144 * a partial match of a single character.
146 if (short_too && current_argv_len == 1)
149 if (match == -1) /* partial match */
152 /* ambiguous abbreviation */
154 warnx(ambig, (int)current_argv_len, current_argv);
159 if (match != -1) { /* option found */
160 if (long_options[match].has_arg == no_argument && has_equal) {
162 warnx(noarg, (int)current_argv_len, current_argv);
164 * XXX: GNU sets optopt to val regardless of flag
166 if (long_options[match].flag == NULL)
167 optopt = long_options[match].val;
172 if (long_options[match].has_arg == required_argument ||
173 long_options[match].has_arg == optional_argument) {
176 else if (long_options[match].has_arg == required_argument) {
178 * optional argument doesn't use next nargv
180 optarg = nargv[optind++];
183 if ((long_options[match].has_arg == required_argument) &&
186 * Missing argument; leading ':' indicates no error
187 * should be generated.
190 warnx(recargstring, current_argv);
192 * XXX: GNU sets optopt to val regardless of flag
194 if (long_options[match].flag == NULL)
195 optopt = long_options[match].val;
201 } else { /* unknown option */
207 warnx(illoptstring, current_argv);
213 if (long_options[match].flag) {
214 *long_options[match].flag = long_options[match].val;
217 return (long_options[match].val);
222 * Parse argc/argv argument vector. Called by user level routines.
224 static int getopt_internal(int nargc, char *const *nargv, const char *options,
225 const struct option *long_options, int *idx,
227 const char *oli; /* option letter list index */
228 int optchar, short_too;
229 static int posixly_correct = -1;
235 * XXX Some GNU programs (like cvs) set optind to 0 instead of
236 * XXX using optreset. Work around this braindamage.
239 optind = optreset = 1;
242 * Disable GNU extensions if POSIXLY_CORRECT is set or options
243 * string begins with a '+'.
245 if (posixly_correct == -1 || optreset)
246 posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
248 flags |= FLAG_ALLARGS;
249 else if (posixly_correct || *options == '+')
250 flags &= ~FLAG_PERMUTE;
251 if (*options == '+' || *options == '-')
256 nonopt_start = nonopt_end = -1;
258 if (optreset || !*place) { /* update scanning pointer */
260 if (optind >= nargc) { /* end of argument vector */
262 if (nonopt_end != -1) {
263 /* do permutation, if we have to */
264 permute_args(nonopt_start, nonopt_end, optind, nargv);
265 optind -= nonopt_end - nonopt_start;
266 } else if (nonopt_start != -1) {
268 * If we skipped non-options, set optind
269 * to the first of them.
271 optind = nonopt_start;
273 nonopt_start = nonopt_end = -1;
276 if (*(place = nargv[optind]) != '-' ||
277 (place[1] == '\0' && strchr(options, '-') == NULL)) {
278 place = EMSG; /* found non-option */
279 if (flags & FLAG_ALLARGS) {
282 * return non-option as argument to option 1
284 optarg = nargv[optind++];
287 if (!(flags & FLAG_PERMUTE)) {
289 * If no permutation wanted, stop parsing
290 * at first non-option.
295 if (nonopt_start == -1)
296 nonopt_start = optind;
297 else if (nonopt_end != -1) {
298 permute_args(nonopt_start, nonopt_end, optind, nargv);
299 nonopt_start = optind - (nonopt_end - nonopt_start);
303 /* process next argument */
306 if (nonopt_start != -1 && nonopt_end == -1)
310 * If we have "-" do nothing, if "--" we are done.
312 if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
316 * We found an option (--), so if we skipped
317 * non-options, we have to permute.
319 if (nonopt_end != -1) {
320 permute_args(nonopt_start, nonopt_end, optind, nargv);
321 optind -= nonopt_end - nonopt_start;
323 nonopt_start = nonopt_end = -1;
329 * Check long options if:
330 * 1) we were passed some
331 * 2) the arg is not just "-"
332 * 3) either the arg starts with -- we are getopt_long_only()
334 if (long_options != NULL && place != nargv[optind] &&
335 (*place == '-' || (flags & FLAG_LONGONLY))) {
338 place++; /* --foo long option */
339 else if (*place != ':' && strchr(options, *place) != NULL)
340 short_too = 1; /* could be short option too */
342 optchar = parse_long_options(nargv, options, long_options, idx, short_too);
349 if ((optchar = (int)*place++) == (int)':' ||
350 (optchar == (int)'-' && *place != '\0') ||
351 (oli = strchr(options, optchar)) == NULL) {
353 * If the user specified "-" and '-' isn't listed in
354 * options, return -1 (non-option) as per POSIX.
355 * Otherwise, it is an unknown option character (or ':').
357 if (optchar == (int)'-' && *place == '\0')
362 warnx(illoptchar, optchar);
366 if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
368 if (*place) /* no space */
370 else if (++optind >= nargc) { /* no arg */
373 warnx(recargchar, optchar);
376 } else /* white space */
377 place = nargv[optind];
378 optchar = parse_long_options(nargv, options, long_options, idx, 0);
382 if (*++oli != ':') { /* doesn't take argument */
385 } else { /* takes (optional) argument */
387 if (*place) /* no white space */
388 optarg = const_cast<char *>(place);
389 else if (oli[1] != ':') { /* arg not optional */
390 if (++optind >= nargc) { /* no arg */
393 warnx(recargchar, optchar);
397 optarg = nargv[optind];
402 /* dump back option letter */
408 * Parse argc/argv argument vector.
410 * [eventually this will replace the BSD getopt]
412 #if defined(REPLACE_GETOPT)
413 int getopt(int nargc, char *const *nargv, const char *options) {
416 * We don't pass FLAG_PERMUTE to getopt_internal() since
417 * the BSD getopt(3) (unlike GNU) has never done this.
419 * Furthermore, since many privileged programs call getopt()
420 * before dropping privileges it makes sense to keep things
421 * as simple (and bug-free) as possible.
423 return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
429 * Parse argc/argv argument vector.
431 #if defined(REPLACE_GETOPT_LONG)
432 int getopt_long(int nargc, char *const *nargv, const char *options,
433 const struct option *long_options, int *idx) {
435 getopt_internal(nargc, nargv, options, long_options, idx, FLAG_PERMUTE));
440 * getopt_long_only --
441 * Parse argc/argv argument vector.
443 #if defined(REPLACE_GETOPT_LONG_ONLY)
444 int getopt_long_only(int nargc, char *const *nargv, const char *options,
445 const struct option *long_options, int *idx) {
447 return (getopt_internal(nargc, nargv, options, long_options, idx,
448 FLAG_PERMUTE | FLAG_LONGONLY));