13c602fabSXin LI /* $OpenBSD: getopt_long.c,v 1.22 2006/10/04 21:29:04 jmc Exp $ */
23c602fabSXin LI /* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */
33c602fabSXin LI
43c602fabSXin LI /*
53c602fabSXin LI * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>
63c602fabSXin LI *
73c602fabSXin LI * Permission to use, copy, modify, and distribute this software for any
83c602fabSXin LI * purpose with or without fee is hereby granted, provided that the above
93c602fabSXin LI * copyright notice and this permission notice appear in all copies.
103c602fabSXin LI *
113c602fabSXin LI * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
123c602fabSXin LI * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
133c602fabSXin LI * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
143c602fabSXin LI * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
153c602fabSXin LI * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
163c602fabSXin LI * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
173c602fabSXin LI * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
183c602fabSXin LI *
193c602fabSXin LI * Sponsored in part by the Defense Advanced Research Projects
203c602fabSXin LI * Agency (DARPA) and Air Force Research Laboratory, Air Force
213c602fabSXin LI * Materiel Command, USAF, under agreement number F39502-99-1-0512.
223c602fabSXin LI */
233c602fabSXin LI /*-
243c602fabSXin LI * Copyright (c) 2000 The NetBSD Foundation, Inc.
253c602fabSXin LI * All rights reserved.
263c602fabSXin LI *
273c602fabSXin LI * This code is derived from software contributed to The NetBSD Foundation
283c602fabSXin LI * by Dieter Baron and Thomas Klausner.
293c602fabSXin LI *
303c602fabSXin LI * Redistribution and use in source and binary forms, with or without
313c602fabSXin LI * modification, are permitted provided that the following conditions
323c602fabSXin LI * are met:
333c602fabSXin LI * 1. Redistributions of source code must retain the above copyright
343c602fabSXin LI * notice, this list of conditions and the following disclaimer.
353c602fabSXin LI * 2. Redistributions in binary form must reproduce the above copyright
363c602fabSXin LI * notice, this list of conditions and the following disclaimer in the
373c602fabSXin LI * documentation and/or other materials provided with the distribution.
383c602fabSXin LI *
393c602fabSXin LI * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
403c602fabSXin LI * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
413c602fabSXin LI * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
423c602fabSXin LI * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
433c602fabSXin LI * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
443c602fabSXin LI * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
453c602fabSXin LI * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
463c602fabSXin LI * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
473c602fabSXin LI * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
483c602fabSXin LI * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
493c602fabSXin LI * POSSIBILITY OF SUCH DAMAGE.
503c602fabSXin LI */
513c602fabSXin LI
523c602fabSXin LI
533c602fabSXin LI #include <errno.h>
543c602fabSXin LI #include "getopt_long.h"
553c602fabSXin LI #include <stdlib.h>
563c602fabSXin LI #include <stdio.h>
573c602fabSXin LI #include <string.h>
583c602fabSXin LI #include <stdarg.h>
593c602fabSXin LI
60ee67461eSJoseph Mingrone #include "diag-control.h"
61ee67461eSJoseph Mingrone
623c602fabSXin LI #define GNU_COMPATIBLE /* Be more compatible, configure's use us! */
633c602fabSXin LI
643c602fabSXin LI #define PRINT_ERROR ((opterr) && (*options != ':'))
653c602fabSXin LI
663c602fabSXin LI #define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */
673c602fabSXin LI #define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */
683c602fabSXin LI #define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */
693c602fabSXin LI
703c602fabSXin LI /* return values */
713c602fabSXin LI #define BADCH (int)'?'
723c602fabSXin LI #define BADARG ((*options == ':') ? (int)':' : (int)'?')
733c602fabSXin LI #define INORDER (int)1
743c602fabSXin LI
753c602fabSXin LI #define EMSG ""
763c602fabSXin LI
773c602fabSXin LI #ifdef GNU_COMPATIBLE
783c602fabSXin LI #define NO_PREFIX (-1)
793c602fabSXin LI #define D_PREFIX 0
803c602fabSXin LI #define DD_PREFIX 1
813c602fabSXin LI #define W_PREFIX 2
823c602fabSXin LI #endif
833c602fabSXin LI
843c602fabSXin LI char *optarg;
853c602fabSXin LI int optind, opterr = 1, optopt;
863c602fabSXin LI
873c602fabSXin LI static int getopt_internal(int, char * const *, const char *,
883c602fabSXin LI const struct option *, int *, int);
893c602fabSXin LI static int parse_long_options(char * const *, const char *,
903c602fabSXin LI const struct option *, int *, int, int);
913c602fabSXin LI static int gcd(int, int);
923c602fabSXin LI static void permute_args(int, int, int, char * const *);
933c602fabSXin LI
943c602fabSXin LI static const char *place = EMSG; /* option letter processing */
953c602fabSXin LI
963c602fabSXin LI static int nonopt_start = -1; /* first non option argument (for permute) */
973c602fabSXin LI static int nonopt_end = -1; /* first option after non options (for permute) */
983c602fabSXin LI
993c602fabSXin LI /* Error messages */
1003c602fabSXin LI static const char recargchar[] = "option requires an argument -- %c";
1013c602fabSXin LI static const char illoptchar[] = "illegal option -- %c"; /* From P1003.2 */
1023c602fabSXin LI #ifdef GNU_COMPATIBLE
1033c602fabSXin LI static int dash_prefix = NO_PREFIX;
1043c602fabSXin LI static const char gnuoptchar[] = "invalid option -- %c";
1053c602fabSXin LI
1063c602fabSXin LI static const char recargstring[] = "option `%s%s' requires an argument";
1073c602fabSXin LI static const char ambig[] = "option `%s%.*s' is ambiguous";
1083c602fabSXin LI static const char noarg[] = "option `%s%.*s' doesn't allow an argument";
1093c602fabSXin LI static const char illoptstring[] = "unrecognized option `%s%s'";
1103c602fabSXin LI #else
1113c602fabSXin LI static const char recargstring[] = "option requires an argument -- %s";
1123c602fabSXin LI static const char ambig[] = "ambiguous option -- %.*s";
1133c602fabSXin LI static const char noarg[] = "option doesn't take an argument -- %.*s";
1143c602fabSXin LI static const char illoptstring[] = "unknown option -- %s";
1153c602fabSXin LI #endif
1163c602fabSXin LI
1173c602fabSXin LI /*
1183c602fabSXin LI * Compute the greatest common divisor of a and b.
1193c602fabSXin LI */
1203c602fabSXin LI static int
gcd(int a,int b)1213c602fabSXin LI gcd(int a, int b)
1223c602fabSXin LI {
1233c602fabSXin LI int c;
1243c602fabSXin LI
1253c602fabSXin LI c = a % b;
1263c602fabSXin LI while (c != 0) {
1273c602fabSXin LI a = b;
1283c602fabSXin LI b = c;
1293c602fabSXin LI c = a % b;
1303c602fabSXin LI }
1313c602fabSXin LI
1323c602fabSXin LI return (b);
1333c602fabSXin LI }
1343c602fabSXin LI
1353c602fabSXin LI /*
1363c602fabSXin LI * Exchange the block from nonopt_start to nonopt_end with the block
1373c602fabSXin LI * from nonopt_end to opt_end (keeping the same order of arguments
1383c602fabSXin LI * in each block).
1393c602fabSXin LI */
1403c602fabSXin LI static void
permute_args(int panonopt_start,int panonopt_end,int opt_end,char * const * nargv)1413c602fabSXin LI permute_args(int panonopt_start, int panonopt_end, int opt_end,
1423c602fabSXin LI char * const *nargv)
1433c602fabSXin LI {
1443c602fabSXin LI int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
1453c602fabSXin LI char *swap;
1463c602fabSXin LI
1473c602fabSXin LI /*
1483c602fabSXin LI * compute lengths of blocks and number and size of cycles
1493c602fabSXin LI */
1503c602fabSXin LI nnonopts = panonopt_end - panonopt_start;
1513c602fabSXin LI nopts = opt_end - panonopt_end;
1523c602fabSXin LI ncycle = gcd(nnonopts, nopts);
1533c602fabSXin LI cyclelen = (opt_end - panonopt_start) / ncycle;
1543c602fabSXin LI
1553c602fabSXin LI for (i = 0; i < ncycle; i++) {
1563c602fabSXin LI cstart = panonopt_end+i;
1573c602fabSXin LI pos = cstart;
1583c602fabSXin LI for (j = 0; j < cyclelen; j++) {
1593c602fabSXin LI if (pos >= panonopt_end)
1603c602fabSXin LI pos -= nnonopts;
1613c602fabSXin LI else
1623c602fabSXin LI pos += nopts;
163ee67461eSJoseph Mingrone /*
164ee67461eSJoseph Mingrone * This is annoying - I guess the
165ee67461eSJoseph Mingrone * "char * const argv[]" in the declaration
166ee67461eSJoseph Mingrone * of getopt() - and thus getopt_long() -
167ee67461eSJoseph Mingrone * means that it makes a promise not to
168ee67461eSJoseph Mingrone * shuffle the arguments, but here we are,
169ee67461eSJoseph Mingrone * shuffling the arguments.
170ee67461eSJoseph Mingrone *
171ee67461eSJoseph Mingrone * (No, it's not a promise that it won't
172ee67461eSJoseph Mingrone * modify any of the argument strings.
173ee67461eSJoseph Mingrone * It's a promise that it won't modify
174ee67461eSJoseph Mingrone * the array of pointers to the argument
175ee67461eSJoseph Mingrone * strings.)
176ee67461eSJoseph Mingrone *
177ee67461eSJoseph Mingrone * So squelch the cast warnings.
178ee67461eSJoseph Mingrone */
1793c602fabSXin LI swap = nargv[pos];
180ee67461eSJoseph Mingrone DIAG_OFF_CAST_QUAL
1813c602fabSXin LI /* LINTED const cast */
1823c602fabSXin LI ((char **) nargv)[pos] = nargv[cstart];
1833c602fabSXin LI /* LINTED const cast */
1843c602fabSXin LI ((char **)nargv)[cstart] = swap;
185ee67461eSJoseph Mingrone DIAG_ON_CAST_QUAL
1863c602fabSXin LI }
1873c602fabSXin LI }
1883c602fabSXin LI }
1893c602fabSXin LI
1903c602fabSXin LI static void
warnx(const char * fmt,...)1913c602fabSXin LI warnx(const char *fmt, ...)
1923c602fabSXin LI {
1933c602fabSXin LI extern char *program_name;
1943c602fabSXin LI va_list ap;
1953c602fabSXin LI
1963c602fabSXin LI va_start(ap, fmt);
1973c602fabSXin LI fprintf(stderr, "%s: ", program_name);
1983c602fabSXin LI vfprintf(stderr, fmt, ap);
1993c602fabSXin LI fprintf(stderr, "\n");
2003c602fabSXin LI va_end(ap);
2013c602fabSXin LI }
2023c602fabSXin LI
2033c602fabSXin LI /*
2043c602fabSXin LI * parse_long_options --
2053c602fabSXin LI * Parse long options in argc/argv argument vector.
2063c602fabSXin LI * Returns -1 if short_too is set and the option does not match long_options.
2073c602fabSXin LI */
2083c602fabSXin LI static int
parse_long_options(char * const * nargv,const char * options,const struct option * long_options,int * idx,int short_too,int flags)2093c602fabSXin LI parse_long_options(char * const *nargv, const char *options,
2103c602fabSXin LI const struct option *long_options, int *idx, int short_too, int flags)
2113c602fabSXin LI {
2123c602fabSXin LI const char *current_argv, *has_equal;
2133c602fabSXin LI #ifdef GNU_COMPATIBLE
2143c602fabSXin LI const char *current_dash;
2153c602fabSXin LI #endif
2163c602fabSXin LI size_t current_argv_len;
2173c602fabSXin LI int i, match, exact_match, second_partial_match;
2183c602fabSXin LI
2193c602fabSXin LI current_argv = place;
2203c602fabSXin LI #ifdef GNU_COMPATIBLE
2213c602fabSXin LI switch (dash_prefix) {
2223c602fabSXin LI case D_PREFIX:
2233c602fabSXin LI current_dash = "-";
2243c602fabSXin LI break;
2253c602fabSXin LI case DD_PREFIX:
2263c602fabSXin LI current_dash = "--";
2273c602fabSXin LI break;
2283c602fabSXin LI case W_PREFIX:
2293c602fabSXin LI current_dash = "-W ";
2303c602fabSXin LI break;
2313c602fabSXin LI default:
2323c602fabSXin LI current_dash = "";
2333c602fabSXin LI break;
2343c602fabSXin LI }
2353c602fabSXin LI #endif
2363c602fabSXin LI match = -1;
2373c602fabSXin LI exact_match = 0;
2383c602fabSXin LI second_partial_match = 0;
2393c602fabSXin LI
2403c602fabSXin LI optind++;
2413c602fabSXin LI
2423c602fabSXin LI if ((has_equal = strchr(current_argv, '=')) != NULL) {
2433c602fabSXin LI /* argument found (--option=arg) */
2443c602fabSXin LI current_argv_len = has_equal - current_argv;
2453c602fabSXin LI has_equal++;
2463c602fabSXin LI } else
2473c602fabSXin LI current_argv_len = strlen(current_argv);
2483c602fabSXin LI
2493c602fabSXin LI for (i = 0; long_options[i].name; i++) {
2503c602fabSXin LI /* find matching long option */
2513c602fabSXin LI if (strncmp(current_argv, long_options[i].name,
2523c602fabSXin LI current_argv_len))
2533c602fabSXin LI continue;
2543c602fabSXin LI
2553c602fabSXin LI if (strlen(long_options[i].name) == current_argv_len) {
2563c602fabSXin LI /* exact match */
2573c602fabSXin LI match = i;
2583c602fabSXin LI exact_match = 1;
2593c602fabSXin LI break;
2603c602fabSXin LI }
2613c602fabSXin LI /*
2623c602fabSXin LI * If this is a known short option, don't allow
2633c602fabSXin LI * a partial match of a single character.
2643c602fabSXin LI */
2653c602fabSXin LI if (short_too && current_argv_len == 1)
2663c602fabSXin LI continue;
2673c602fabSXin LI
2683c602fabSXin LI if (match == -1) /* first partial match */
2693c602fabSXin LI match = i;
2703c602fabSXin LI else if ((flags & FLAG_LONGONLY) ||
2713c602fabSXin LI long_options[i].has_arg !=
2723c602fabSXin LI long_options[match].has_arg ||
2733c602fabSXin LI long_options[i].flag != long_options[match].flag ||
2743c602fabSXin LI long_options[i].val != long_options[match].val)
2753c602fabSXin LI second_partial_match = 1;
2763c602fabSXin LI }
2773c602fabSXin LI if (!exact_match && second_partial_match) {
2783c602fabSXin LI /* ambiguous abbreviation */
2793c602fabSXin LI if (PRINT_ERROR)
2803c602fabSXin LI warnx(ambig,
2813c602fabSXin LI #ifdef GNU_COMPATIBLE
2823c602fabSXin LI current_dash,
2833c602fabSXin LI #endif
2843c602fabSXin LI (int)current_argv_len,
2853c602fabSXin LI current_argv);
2863c602fabSXin LI optopt = 0;
2873c602fabSXin LI return (BADCH);
2883c602fabSXin LI }
2893c602fabSXin LI if (match != -1) { /* option found */
2903c602fabSXin LI if (long_options[match].has_arg == no_argument
2913c602fabSXin LI && has_equal) {
2923c602fabSXin LI if (PRINT_ERROR)
2933c602fabSXin LI warnx(noarg,
2943c602fabSXin LI #ifdef GNU_COMPATIBLE
2953c602fabSXin LI current_dash,
2963c602fabSXin LI #endif
2973c602fabSXin LI (int)current_argv_len,
2983c602fabSXin LI current_argv);
2993c602fabSXin LI /*
3003c602fabSXin LI * XXX: GNU sets optopt to val regardless of flag
3013c602fabSXin LI */
3023c602fabSXin LI if (long_options[match].flag == NULL)
3033c602fabSXin LI optopt = long_options[match].val;
3043c602fabSXin LI else
3053c602fabSXin LI optopt = 0;
3063c602fabSXin LI #ifdef GNU_COMPATIBLE
3073c602fabSXin LI return (BADCH);
3083c602fabSXin LI #else
3093c602fabSXin LI return (BADARG);
3103c602fabSXin LI #endif
3113c602fabSXin LI }
3123c602fabSXin LI if (long_options[match].has_arg == required_argument ||
3133c602fabSXin LI long_options[match].has_arg == optional_argument) {
314ee67461eSJoseph Mingrone DIAG_OFF_CAST_QUAL
3153c602fabSXin LI if (has_equal)
3163c602fabSXin LI optarg = (char *)has_equal;
3173c602fabSXin LI else if (long_options[match].has_arg ==
3183c602fabSXin LI required_argument) {
3193c602fabSXin LI /*
3203c602fabSXin LI * optional argument doesn't use next nargv
3213c602fabSXin LI */
3223c602fabSXin LI optarg = nargv[optind++];
3233c602fabSXin LI }
324ee67461eSJoseph Mingrone DIAG_ON_CAST_QUAL
3253c602fabSXin LI }
3263c602fabSXin LI if ((long_options[match].has_arg == required_argument)
3273c602fabSXin LI && (optarg == NULL)) {
3283c602fabSXin LI /*
3293c602fabSXin LI * Missing argument; leading ':' indicates no error
3303c602fabSXin LI * should be generated.
3313c602fabSXin LI */
3323c602fabSXin LI if (PRINT_ERROR)
3333c602fabSXin LI warnx(recargstring,
3343c602fabSXin LI #ifdef GNU_COMPATIBLE
3353c602fabSXin LI current_dash,
3363c602fabSXin LI #endif
3373c602fabSXin LI current_argv);
3383c602fabSXin LI /*
3393c602fabSXin LI * XXX: GNU sets optopt to val regardless of flag
3403c602fabSXin LI */
3413c602fabSXin LI if (long_options[match].flag == NULL)
3423c602fabSXin LI optopt = long_options[match].val;
3433c602fabSXin LI else
3443c602fabSXin LI optopt = 0;
3453c602fabSXin LI --optind;
3463c602fabSXin LI return (BADARG);
3473c602fabSXin LI }
3483c602fabSXin LI } else { /* unknown option */
3493c602fabSXin LI if (short_too) {
3503c602fabSXin LI --optind;
3513c602fabSXin LI return (-1);
3523c602fabSXin LI }
3533c602fabSXin LI if (PRINT_ERROR)
3543c602fabSXin LI warnx(illoptstring,
3553c602fabSXin LI #ifdef GNU_COMPATIBLE
3563c602fabSXin LI current_dash,
3573c602fabSXin LI #endif
3583c602fabSXin LI current_argv);
3593c602fabSXin LI optopt = 0;
3603c602fabSXin LI return (BADCH);
3613c602fabSXin LI }
3623c602fabSXin LI if (idx)
3633c602fabSXin LI *idx = match;
3643c602fabSXin LI if (long_options[match].flag) {
3653c602fabSXin LI *long_options[match].flag = long_options[match].val;
3663c602fabSXin LI return (0);
3673c602fabSXin LI } else
3683c602fabSXin LI return (long_options[match].val);
3693c602fabSXin LI }
3703c602fabSXin LI
3713c602fabSXin LI /*
3723c602fabSXin LI * getopt_internal --
3733c602fabSXin LI * Parse argc/argv argument vector. Called by user level routines.
3743c602fabSXin LI */
3753c602fabSXin LI static int
getopt_internal(int nargc,char * const * nargv,const char * options,const struct option * long_options,int * idx,int flags)3763c602fabSXin LI getopt_internal(int nargc, char * const *nargv, const char *options,
3773c602fabSXin LI const struct option *long_options, int *idx, int flags)
3783c602fabSXin LI {
3793c602fabSXin LI char *oli; /* option letter list index */
3803c602fabSXin LI int optchar, short_too;
3813c602fabSXin LI int posixly_correct; /* no static, can be changed on the fly */
3823c602fabSXin LI
3833c602fabSXin LI if (options == NULL)
3843c602fabSXin LI return (-1);
3853c602fabSXin LI
3863c602fabSXin LI /*
3873c602fabSXin LI * Disable GNU extensions if POSIXLY_CORRECT is set or options
3883c602fabSXin LI * string begins with a '+'.
3893c602fabSXin LI */
3903c602fabSXin LI posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
3913c602fabSXin LI #ifdef GNU_COMPATIBLE
3923c602fabSXin LI if (*options == '-')
3933c602fabSXin LI flags |= FLAG_ALLARGS;
3943c602fabSXin LI else if (posixly_correct || *options == '+')
3953c602fabSXin LI flags &= ~FLAG_PERMUTE;
3963c602fabSXin LI #else
3973c602fabSXin LI if (posixly_correct || *options == '+')
3983c602fabSXin LI flags &= ~FLAG_PERMUTE;
3993c602fabSXin LI else if (*options == '-')
4003c602fabSXin LI flags |= FLAG_ALLARGS;
4013c602fabSXin LI #endif
4023c602fabSXin LI if (*options == '+' || *options == '-')
4033c602fabSXin LI options++;
4043c602fabSXin LI
4053c602fabSXin LI /*
4063c602fabSXin LI * XXX Some GNU programs (like cvs) set optind to 0 instead of
4073c602fabSXin LI * XXX using optreset. Work around this braindamage.
4083c602fabSXin LI */
4093c602fabSXin LI if (optind == 0)
4103c602fabSXin LI optind = 1;
4113c602fabSXin LI
4123c602fabSXin LI optarg = NULL;
4133c602fabSXin LI start:
4143c602fabSXin LI if (!*place) { /* update scanning pointer */
4153c602fabSXin LI if (optind >= nargc) { /* end of argument vector */
4163c602fabSXin LI place = EMSG;
4173c602fabSXin LI if (nonopt_end != -1) {
4183c602fabSXin LI /* do permutation, if we have to */
4193c602fabSXin LI permute_args(nonopt_start, nonopt_end,
4203c602fabSXin LI optind, nargv);
4213c602fabSXin LI optind -= nonopt_end - nonopt_start;
422*0a7e5f1fSJoseph Mingrone } else if (nonopt_start != -1) {
4233c602fabSXin LI /*
4243c602fabSXin LI * If we skipped non-options, set optind
4253c602fabSXin LI * to the first of them.
4263c602fabSXin LI */
4273c602fabSXin LI optind = nonopt_start;
4283c602fabSXin LI }
4293c602fabSXin LI nonopt_start = nonopt_end = -1;
4303c602fabSXin LI return (-1);
4313c602fabSXin LI }
4323c602fabSXin LI if (*(place = nargv[optind]) != '-' ||
4333c602fabSXin LI #ifdef GNU_COMPATIBLE
4343c602fabSXin LI place[1] == '\0') {
4353c602fabSXin LI #else
4363c602fabSXin LI (place[1] == '\0' && strchr(options, '-') == NULL)) {
4373c602fabSXin LI #endif
4383c602fabSXin LI place = EMSG; /* found non-option */
4393c602fabSXin LI if (flags & FLAG_ALLARGS) {
4403c602fabSXin LI /*
4413c602fabSXin LI * GNU extension:
4423c602fabSXin LI * return non-option as argument to option 1
4433c602fabSXin LI */
4443c602fabSXin LI optarg = nargv[optind++];
4453c602fabSXin LI return (INORDER);
4463c602fabSXin LI }
4473c602fabSXin LI if (!(flags & FLAG_PERMUTE)) {
4483c602fabSXin LI /*
4493c602fabSXin LI * If no permutation wanted, stop parsing
4503c602fabSXin LI * at first non-option.
4513c602fabSXin LI */
4523c602fabSXin LI return (-1);
4533c602fabSXin LI }
4543c602fabSXin LI /* do permutation */
4553c602fabSXin LI if (nonopt_start == -1)
4563c602fabSXin LI nonopt_start = optind;
4573c602fabSXin LI else if (nonopt_end != -1) {
4583c602fabSXin LI permute_args(nonopt_start, nonopt_end,
4593c602fabSXin LI optind, nargv);
4603c602fabSXin LI nonopt_start = optind -
4613c602fabSXin LI (nonopt_end - nonopt_start);
4623c602fabSXin LI nonopt_end = -1;
4633c602fabSXin LI }
4643c602fabSXin LI optind++;
4653c602fabSXin LI /* process next argument */
4663c602fabSXin LI goto start;
4673c602fabSXin LI }
4683c602fabSXin LI if (nonopt_start != -1 && nonopt_end == -1)
4693c602fabSXin LI nonopt_end = optind;
4703c602fabSXin LI
4713c602fabSXin LI /*
4723c602fabSXin LI * If we have "-" do nothing, if "--" we are done.
4733c602fabSXin LI */
4743c602fabSXin LI if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
4753c602fabSXin LI optind++;
4763c602fabSXin LI place = EMSG;
4773c602fabSXin LI /*
4783c602fabSXin LI * We found an option (--), so if we skipped
4793c602fabSXin LI * non-options, we have to permute.
4803c602fabSXin LI */
4813c602fabSXin LI if (nonopt_end != -1) {
4823c602fabSXin LI permute_args(nonopt_start, nonopt_end,
4833c602fabSXin LI optind, nargv);
4843c602fabSXin LI optind -= nonopt_end - nonopt_start;
4853c602fabSXin LI }
4863c602fabSXin LI nonopt_start = nonopt_end = -1;
4873c602fabSXin LI return (-1);
4883c602fabSXin LI }
4893c602fabSXin LI }
4903c602fabSXin LI
4913c602fabSXin LI /*
4923c602fabSXin LI * Check long options if:
4933c602fabSXin LI * 1) we were passed some
4943c602fabSXin LI * 2) the arg is not just "-"
4953c602fabSXin LI * 3) either the arg starts with -- we are getopt_long_only()
4963c602fabSXin LI */
4973c602fabSXin LI if (long_options != NULL && place != nargv[optind] &&
4983c602fabSXin LI (*place == '-' || (flags & FLAG_LONGONLY))) {
4993c602fabSXin LI short_too = 0;
5003c602fabSXin LI #ifdef GNU_COMPATIBLE
5013c602fabSXin LI dash_prefix = D_PREFIX;
5023c602fabSXin LI #endif
5033c602fabSXin LI if (*place == '-') {
5043c602fabSXin LI place++; /* --foo long option */
5053c602fabSXin LI #ifdef GNU_COMPATIBLE
5063c602fabSXin LI dash_prefix = DD_PREFIX;
5073c602fabSXin LI #endif
5083c602fabSXin LI } else if (*place != ':' && strchr(options, *place) != NULL)
5093c602fabSXin LI short_too = 1; /* could be short option too */
5103c602fabSXin LI
5113c602fabSXin LI optchar = parse_long_options(nargv, options, long_options,
5123c602fabSXin LI idx, short_too, flags);
5133c602fabSXin LI if (optchar != -1) {
5143c602fabSXin LI place = EMSG;
5153c602fabSXin LI return (optchar);
5163c602fabSXin LI }
5173c602fabSXin LI }
5183c602fabSXin LI
5193c602fabSXin LI if ((optchar = (int)*place++) == (int)':' ||
5203c602fabSXin LI (optchar == (int)'-' && *place != '\0') ||
5213c602fabSXin LI (oli = strchr(options, optchar)) == NULL) {
5223c602fabSXin LI /*
5233c602fabSXin LI * If the user specified "-" and '-' isn't listed in
5243c602fabSXin LI * options, return -1 (non-option) as per POSIX.
5253c602fabSXin LI * Otherwise, it is an unknown option character (or ':').
5263c602fabSXin LI */
5273c602fabSXin LI if (optchar == (int)'-' && *place == '\0')
5283c602fabSXin LI return (-1);
5293c602fabSXin LI if (!*place)
5303c602fabSXin LI ++optind;
5313c602fabSXin LI #ifdef GNU_COMPATIBLE
5323c602fabSXin LI if (PRINT_ERROR)
5333c602fabSXin LI warnx(posixly_correct ? illoptchar : gnuoptchar,
5343c602fabSXin LI optchar);
5353c602fabSXin LI #else
5363c602fabSXin LI if (PRINT_ERROR)
5373c602fabSXin LI warnx(illoptchar, optchar);
5383c602fabSXin LI #endif
5393c602fabSXin LI optopt = optchar;
5403c602fabSXin LI return (BADCH);
5413c602fabSXin LI }
5423c602fabSXin LI if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
5433c602fabSXin LI /* -W long-option */
5443c602fabSXin LI if (*place) /* no space */
5453c602fabSXin LI /* NOTHING */;
5463c602fabSXin LI else if (++optind >= nargc) { /* no arg */
5473c602fabSXin LI place = EMSG;
5483c602fabSXin LI if (PRINT_ERROR)
5493c602fabSXin LI warnx(recargchar, optchar);
5503c602fabSXin LI optopt = optchar;
5513c602fabSXin LI return (BADARG);
5523c602fabSXin LI } else /* white space */
5533c602fabSXin LI place = nargv[optind];
5543c602fabSXin LI #ifdef GNU_COMPATIBLE
5553c602fabSXin LI dash_prefix = W_PREFIX;
5563c602fabSXin LI #endif
5573c602fabSXin LI optchar = parse_long_options(nargv, options, long_options,
5583c602fabSXin LI idx, 0, flags);
5593c602fabSXin LI place = EMSG;
5603c602fabSXin LI return (optchar);
5613c602fabSXin LI }
5623c602fabSXin LI if (*++oli != ':') { /* doesn't take argument */
5633c602fabSXin LI if (!*place)
5643c602fabSXin LI ++optind;
5653c602fabSXin LI } else { /* takes (optional) argument */
5663c602fabSXin LI optarg = NULL;
567ee67461eSJoseph Mingrone if (*place) { /* no white space */
568ee67461eSJoseph Mingrone DIAG_OFF_CAST_QUAL
5693c602fabSXin LI optarg = (char *)place;
570ee67461eSJoseph Mingrone DIAG_ON_CAST_QUAL
571ee67461eSJoseph Mingrone } else if (oli[1] != ':') { /* arg not optional */
5723c602fabSXin LI if (++optind >= nargc) { /* no arg */
5733c602fabSXin LI place = EMSG;
5743c602fabSXin LI if (PRINT_ERROR)
5753c602fabSXin LI warnx(recargchar, optchar);
5763c602fabSXin LI optopt = optchar;
5773c602fabSXin LI return (BADARG);
5783c602fabSXin LI } else
5793c602fabSXin LI optarg = nargv[optind];
5803c602fabSXin LI }
5813c602fabSXin LI place = EMSG;
5823c602fabSXin LI ++optind;
5833c602fabSXin LI }
5843c602fabSXin LI /* dump back option letter */
5853c602fabSXin LI return (optchar);
5863c602fabSXin LI }
5873c602fabSXin LI
5883c602fabSXin LI #ifdef REPLACE_GETOPT
5893c602fabSXin LI /*
5903c602fabSXin LI * getopt --
5913c602fabSXin LI * Parse argc/argv argument vector.
5923c602fabSXin LI *
5933c602fabSXin LI * [eventually this will replace the BSD getopt]
5943c602fabSXin LI */
5953c602fabSXin LI int
5963c602fabSXin LI getopt(int nargc, char * const *nargv, const char *options)
5973c602fabSXin LI {
5983c602fabSXin LI
5993c602fabSXin LI /*
6003c602fabSXin LI * We don't pass FLAG_PERMUTE to getopt_internal() since
6013c602fabSXin LI * the BSD getopt(3) (unlike GNU) has never done this.
6023c602fabSXin LI *
6033c602fabSXin LI * Furthermore, since many privileged programs call getopt()
6043c602fabSXin LI * before dropping privileges it makes sense to keep things
6053c602fabSXin LI * as simple (and bug-free) as possible.
6063c602fabSXin LI */
6073c602fabSXin LI return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
6083c602fabSXin LI }
6093c602fabSXin LI #endif /* REPLACE_GETOPT */
6103c602fabSXin LI
6113c602fabSXin LI /*
6123c602fabSXin LI * getopt_long --
6133c602fabSXin LI * Parse argc/argv argument vector.
6143c602fabSXin LI */
6153c602fabSXin LI int
6163c602fabSXin LI getopt_long(int nargc, char * const *nargv, const char *options,
6173c602fabSXin LI const struct option *long_options, int *idx)
6183c602fabSXin LI {
6193c602fabSXin LI
6203c602fabSXin LI return (getopt_internal(nargc, nargv, options, long_options, idx,
6213c602fabSXin LI FLAG_PERMUTE));
6223c602fabSXin LI }
6233c602fabSXin LI
6243c602fabSXin LI /*
6253c602fabSXin LI * getopt_long_only --
6263c602fabSXin LI * Parse argc/argv argument vector.
6273c602fabSXin LI */
6283c602fabSXin LI int
6293c602fabSXin LI getopt_long_only(int nargc, char * const *nargv, const char *options,
6303c602fabSXin LI const struct option *long_options, int *idx)
6313c602fabSXin LI {
6323c602fabSXin LI
6333c602fabSXin LI return (getopt_internal(nargc, nargv, options, long_options, idx,
6343c602fabSXin LI FLAG_PERMUTE|FLAG_LONGONLY));
6353c602fabSXin LI }
636