11#if defined(REPLACE_GETOPT) || defined(REPLACE_GETOPT_LONG) || \
12 defined(REPLACE_GETOPT_LONG_ONLY)
19#if defined(REPLACE_GETOPT)
27#define PRINT_ERROR ((opterr) && (*options != ':'))
29#define FLAG_PERMUTE 0x01
30#define FLAG_ALLARGS 0x02
31#define FLAG_LONGONLY 0x04
35#define BADARG ((*options == ':') ? (int)':' : (int)'?')
40static int getopt_internal(
int,
char *
const *,
const char *,
41 const struct option *,
int *,
int);
42static int parse_long_options(
char *
const *,
const char *,
43 const struct option *,
int *,
int);
44static int gcd(
int,
int);
45static void permute_args(
int,
int,
int,
char *
const *);
47static const char *place = EMSG;
50static int nonopt_start = -1;
51static int nonopt_end = -1;
56static int gcd(
int a,
int b) {
70#define warnx(a, ...) pass();
77static void permute_args(
int panonopt_start,
int panonopt_end,
int opt_end,
79 int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
85 nnonopts = panonopt_end - panonopt_start;
86 nopts = opt_end - panonopt_end;
87 ncycle = gcd(nnonopts, nopts);
88 cyclelen = (opt_end - panonopt_start) / ncycle;
90 for (i = 0; i < ncycle; i++) {
91 cstart = panonopt_end + i;
93 for (j = 0; j < cyclelen; j++) {
94 if (pos >= panonopt_end)
100 const_cast<char **
>(nargv)[pos] = nargv[cstart];
102 const_cast<char **
>(nargv)[cstart] = swap;
112static int parse_long_options(
char *
const *nargv,
const char *options,
113 const struct option *long_options,
int *idx,
115 char *current_argv, *has_equal;
116 size_t current_argv_len;
119 current_argv =
const_cast<char *
>(place);
124 if ((has_equal = strchr(current_argv,
'=')) != NULL) {
126 current_argv_len = has_equal - current_argv;
129 current_argv_len = strlen(current_argv);
131 for (i = 0; long_options[i].name; i++) {
133 if (strncmp(current_argv, long_options[i].name, current_argv_len))
136 if (strlen(long_options[i].name) == current_argv_len) {
145 if (short_too && current_argv_len == 1)
153 warnx(ambig, (
int)current_argv_len, current_argv);
159 if (long_options[match].has_arg == no_argument && has_equal) {
161 warnx(noarg, (
int)current_argv_len, current_argv);
165 if (long_options[match].flag == NULL)
166 optopt = long_options[match].val;
171 if (long_options[match].has_arg == required_argument ||
172 long_options[match].has_arg == optional_argument) {
175 else if (long_options[match].has_arg == required_argument) {
179 optarg = nargv[optind++];
182 if ((long_options[match].has_arg == required_argument) &&
189 warnx(recargstring, current_argv);
193 if (long_options[match].flag == NULL)
194 optopt = long_options[match].val;
206 warnx(illoptstring, current_argv);
212 if (long_options[match].flag) {
213 *long_options[match].flag = long_options[match].val;
216 return (long_options[match].val);
223static int getopt_internal(
int nargc,
char *
const *nargv,
const char *options,
224 const struct option *long_options,
int *idx,
227 int optchar, short_too;
228 static int posixly_correct = -1;
238 optind = optreset = 1;
244 if (posixly_correct == -1 || optreset)
245 posixly_correct = (getenv(
"POSIXLY_CORRECT") != NULL);
247 flags |= FLAG_ALLARGS;
248 else if (posixly_correct || *options ==
'+')
249 flags &= ~FLAG_PERMUTE;
250 if (*options ==
'+' || *options ==
'-')
255 nonopt_start = nonopt_end = -1;
257 if (optreset || !*place) {
259 if (optind >= nargc) {
261 if (nonopt_end != -1) {
263 permute_args(nonopt_start, nonopt_end, optind, nargv);
264 optind -= nonopt_end - nonopt_start;
265 }
else if (nonopt_start != -1) {
270 optind = nonopt_start;
272 nonopt_start = nonopt_end = -1;
275 if (*(place = nargv[optind]) !=
'-' ||
276 (place[1] ==
'\0' && strchr(options,
'-') == NULL)) {
278 if (flags & FLAG_ALLARGS) {
283 optarg = nargv[optind++];
286 if (!(flags & FLAG_PERMUTE)) {
294 if (nonopt_start == -1)
295 nonopt_start = optind;
296 else if (nonopt_end != -1) {
297 permute_args(nonopt_start, nonopt_end, optind, nargv);
298 nonopt_start = optind - (nonopt_end - nonopt_start);
305 if (nonopt_start != -1 && nonopt_end == -1)
311 if (place[1] !=
'\0' && *++place ==
'-' && place[1] ==
'\0') {
318 if (nonopt_end != -1) {
319 permute_args(nonopt_start, nonopt_end, optind, nargv);
320 optind -= nonopt_end - nonopt_start;
322 nonopt_start = nonopt_end = -1;
333 if (long_options != NULL && place != nargv[optind] &&
334 (*place ==
'-' || (flags & FLAG_LONGONLY))) {
338 else if (*place !=
':' && strchr(options, *place) != NULL)
341 optchar = parse_long_options(nargv, options, long_options, idx, short_too);
348 if ((optchar = (
int)*place++) == (
int)
':' ||
349 (optchar == (
int)
'-' && *place !=
'\0') ||
350 (oli = strchr(options, optchar)) == NULL) {
356 if (optchar == (
int)
'-' && *place ==
'\0')
361 warnx(illoptchar, optchar);
365 if (long_options != NULL && optchar ==
'W' && oli[1] ==
';') {
369 else if (++optind >= nargc) {
372 warnx(recargchar, optchar);
376 place = nargv[optind];
377 optchar = parse_long_options(nargv, options, long_options, idx, 0);
387 optarg =
const_cast<char *
>(place);
388 else if (oli[1] !=
':') {
389 if (++optind >= nargc) {
392 warnx(recargchar, optchar);
396 optarg = nargv[optind];
411#if defined(REPLACE_GETOPT)
412int getopt(
int nargc,
char *
const *nargv,
const char *options) {
422 return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
430#if defined(REPLACE_GETOPT_LONG)
431int getopt_long(
int nargc,
char *
const *nargv,
const char *options,
432 const struct option *long_options,
int *idx) {
434 getopt_internal(nargc, nargv, options, long_options, idx, FLAG_PERMUTE));
442#if defined(REPLACE_GETOPT_LONG_ONLY)
443int getopt_long_only(
int nargc,
char *
const *nargv,
const char *options,
444 const struct option *long_options,
int *idx) {
446 return (getopt_internal(nargc, nargv, options, long_options, idx,
447 FLAG_PERMUTE | FLAG_LONGONLY));
void swap(AdaptedConstIterator< C, E, A > &lhs, AdaptedConstIterator< C, E, A > &rhs)