17c478bd9Sstevel@tonic-gate /*
25e1c72e1SJason King * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
37c478bd9Sstevel@tonic-gate * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate */
57c478bd9Sstevel@tonic-gate
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>
87c478bd9Sstevel@tonic-gate * All rights reserved.
97c478bd9Sstevel@tonic-gate *
107c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
117c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions
127c478bd9Sstevel@tonic-gate * are met:
137c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
147c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
157c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
167c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
177c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
187c478bd9Sstevel@tonic-gate * 3. The name of the author may not be used to endorse or promote products
197c478bd9Sstevel@tonic-gate * derived from this software without specific prior written permission.
207c478bd9Sstevel@tonic-gate *
217c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
227c478bd9Sstevel@tonic-gate * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
237c478bd9Sstevel@tonic-gate * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
247c478bd9Sstevel@tonic-gate * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
257c478bd9Sstevel@tonic-gate * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
267c478bd9Sstevel@tonic-gate * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
277c478bd9Sstevel@tonic-gate * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
287c478bd9Sstevel@tonic-gate * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
297c478bd9Sstevel@tonic-gate * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
307c478bd9Sstevel@tonic-gate * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
317c478bd9Sstevel@tonic-gate */
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate * Copyright (c) 2000 The NetBSD Foundation, Inc.
347c478bd9Sstevel@tonic-gate * All rights reserved.
357c478bd9Sstevel@tonic-gate *
367c478bd9Sstevel@tonic-gate * This code is derived from software contributed to The NetBSD Foundation
377c478bd9Sstevel@tonic-gate * by Dieter Baron and Thomas Klausner.
387c478bd9Sstevel@tonic-gate *
397c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
407c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions
417c478bd9Sstevel@tonic-gate * are met:
427c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
437c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
447c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
457c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
467c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
477c478bd9Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
487c478bd9Sstevel@tonic-gate * must display the following acknowledgement:
497c478bd9Sstevel@tonic-gate * This product includes software developed by the NetBSD
507c478bd9Sstevel@tonic-gate * Foundation, Inc. and its contributors.
517c478bd9Sstevel@tonic-gate * 4. Neither the name of The NetBSD Foundation nor the names of its
527c478bd9Sstevel@tonic-gate * contributors may be used to endorse or promote products derived
537c478bd9Sstevel@tonic-gate * from this software without specific prior written permission.
547c478bd9Sstevel@tonic-gate *
557c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
567c478bd9Sstevel@tonic-gate * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
577c478bd9Sstevel@tonic-gate * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
587c478bd9Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
597c478bd9Sstevel@tonic-gate * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
607c478bd9Sstevel@tonic-gate * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
617c478bd9Sstevel@tonic-gate * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
627c478bd9Sstevel@tonic-gate * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
637c478bd9Sstevel@tonic-gate * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
647c478bd9Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
657c478bd9Sstevel@tonic-gate * POSSIBILITY OF SUCH DAMAGE.
667c478bd9Sstevel@tonic-gate */
677c478bd9Sstevel@tonic-gate
687257d1b4Sraf #pragma weak _getopt_clip = getopt_clip
697257d1b4Sraf #pragma weak _getopt_long = getopt_long
707257d1b4Sraf #pragma weak _getopt_long_only = getopt_long_only
717257d1b4Sraf
727257d1b4Sraf #include "lint.h"
737c478bd9Sstevel@tonic-gate #include <getopt.h>
747c478bd9Sstevel@tonic-gate #include <stdio.h>
757c478bd9Sstevel@tonic-gate #include <errno.h>
767c478bd9Sstevel@tonic-gate #include <unistd.h>
777c478bd9Sstevel@tonic-gate #include <stdlib.h>
787c478bd9Sstevel@tonic-gate #include <string.h>
797c478bd9Sstevel@tonic-gate #include "_libc_gettext.h"
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate static int optreset = 0; /* keep track of first entry to getopt() */
827c478bd9Sstevel@tonic-gate #define PRINT_ERROR ((opterr) && (*options != ':'))
83*4a38094cSToomas Soome #define FLAG_IS_SET(flag) ((flags & flag) != 0) /* is flag turned on? */
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate /* Determine if an argument is required for this long option */
867c478bd9Sstevel@tonic-gate #define LONGOPT_REQUIRES_ARG(longopt) \
877c478bd9Sstevel@tonic-gate ((((longopt).has_arg == optional_argument) && \
887c478bd9Sstevel@tonic-gate (!FLAG_IS_SET(FLAG_OPTIONAL_ARGS))) || \
897c478bd9Sstevel@tonic-gate ((longopt).has_arg == required_argument))
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate #define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */
927c478bd9Sstevel@tonic-gate #define FLAG_ALLARGS 0x02 /* treat non-options as args to option "1" */
937c478bd9Sstevel@tonic-gate #define FLAG_LONGONLY 0x04 /* operate as getopt_long_only() */
947c478bd9Sstevel@tonic-gate #define FLAG_OPTIONAL_ARGS 0x08 /* allow optional arguments to options */
957c478bd9Sstevel@tonic-gate #define FLAG_REQUIRE_EQUIVALENTS 0x10 /* require short<->long equivalents */
96*4a38094cSToomas Soome #define FLAG_ABBREV 0x20 /* long option abbreviation allowed. */
97*4a38094cSToomas Soome #define FLAG_W_SEMICOLON 0x40 /* Support for W; in optstring */
987c478bd9Sstevel@tonic-gate #define FLAG_PLUS_DASH_START 0x80 /* leading '+' or '-' in optstring */
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate /* return values */
1017c478bd9Sstevel@tonic-gate #define BADCH (int)'?'
1027c478bd9Sstevel@tonic-gate #define BADARG ((*options == ':') ? (int)':' : (int)'?')
103*4a38094cSToomas Soome #define INORDER (int)1
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate #define EMSG ""
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate static int getopt_internal(int, char * const *, const char *,
1087c478bd9Sstevel@tonic-gate const struct option *, int *, uint_t);
1097c478bd9Sstevel@tonic-gate static int parse_long_options(int nargc, char * const *nargv, const char *,
1107c478bd9Sstevel@tonic-gate const struct option *, int *, int,
1117c478bd9Sstevel@tonic-gate uint_t flags);
1127c478bd9Sstevel@tonic-gate static int gcd(int, int);
1137c478bd9Sstevel@tonic-gate static void permute_args(int, int, int, char * const *);
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate static char *place = EMSG; /* option letter processing */
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate /* XXX: set optreset to 1 rather than these two */
1187c478bd9Sstevel@tonic-gate static int nonopt_start = -1; /* first non option argument (for permute) */
1197c478bd9Sstevel@tonic-gate static int nonopt_end = -1; /* first option after non options (for permute) */
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate * Generalized error message output.
1237c478bd9Sstevel@tonic-gate *
1247c478bd9Sstevel@tonic-gate * NOTE ON ERROR MESSAGES: All the error messages in this file
125d362b749Svk * use %s (not %c) because they are all routed through warnx_getopt(),
1267c478bd9Sstevel@tonic-gate * which takes a string argument. Character arguments passed
1277c478bd9Sstevel@tonic-gate * to warnxchar() are converted to strings automatically before
128d362b749Svk * being passed to warnx_getopt().
1297c478bd9Sstevel@tonic-gate */
1307c478bd9Sstevel@tonic-gate static void
warnx_getopt(const char * argv0,const char * msg,const char * arg)131*4a38094cSToomas Soome warnx_getopt(const char *argv0, const char *msg, const char *arg)
132*4a38094cSToomas Soome {
1337c478bd9Sstevel@tonic-gate char errbuf[256];
1347c478bd9Sstevel@tonic-gate (void) snprintf(errbuf, sizeof (errbuf), msg, argv0, arg);
1357c478bd9Sstevel@tonic-gate (void) write(2, errbuf, strlen(errbuf));
1367c478bd9Sstevel@tonic-gate (void) write(2, "\n", 1);
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate * Generalized error message output.
1417c478bd9Sstevel@tonic-gate */
1427c478bd9Sstevel@tonic-gate static void
warnxchar(const char * argv0,const char * msg,const char c)143*4a38094cSToomas Soome warnxchar(const char *argv0, const char *msg, const char c)
144*4a38094cSToomas Soome {
1457c478bd9Sstevel@tonic-gate char charbuf[2];
1467c478bd9Sstevel@tonic-gate charbuf[0] = c;
1477c478bd9Sstevel@tonic-gate charbuf[1] = '\0';
148d362b749Svk warnx_getopt(argv0, msg, charbuf);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate /*
1527c478bd9Sstevel@tonic-gate * Generalized error message output.
1537c478bd9Sstevel@tonic-gate */
1547c478bd9Sstevel@tonic-gate static void
warnxlen(const char * argv0,const char * msg,int argLen,const char * arg)155*4a38094cSToomas Soome warnxlen(const char *argv0, const char *msg, int argLen, const char *arg)
156*4a38094cSToomas Soome {
1577c478bd9Sstevel@tonic-gate char argbuf[256];
1587c478bd9Sstevel@tonic-gate (void) strncpy(argbuf, arg, argLen);
1597c478bd9Sstevel@tonic-gate argbuf[argLen < (sizeof (argbuf)-1)? argLen:(sizeof (argbuf)-1)] = '\0';
160d362b749Svk warnx_getopt(argv0, msg, argbuf);
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate /*
1647c478bd9Sstevel@tonic-gate * Compute the greatest common divisor of a and b.
1657c478bd9Sstevel@tonic-gate */
1667c478bd9Sstevel@tonic-gate static int
gcd(int a,int b)1677c478bd9Sstevel@tonic-gate gcd(int a, int b)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate int c;
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate c = a % b;
1727c478bd9Sstevel@tonic-gate while (c != 0) {
1737c478bd9Sstevel@tonic-gate a = b;
1747c478bd9Sstevel@tonic-gate b = c;
1757c478bd9Sstevel@tonic-gate c = a % b;
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate return (b);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate * Exchange the block from nonopt_start to nonopt_end with the block
1837c478bd9Sstevel@tonic-gate * from nonopt_end to opt_end (keeping the same order of arguments
1847c478bd9Sstevel@tonic-gate * in each block).
1857c478bd9Sstevel@tonic-gate */
1867c478bd9Sstevel@tonic-gate static void
permute_args(int panonopt_start,int panonopt_end,int opt_end,char * const * nargv)1877c478bd9Sstevel@tonic-gate permute_args(int panonopt_start, int panonopt_end, int opt_end,
188*4a38094cSToomas Soome char * const *nargv)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
1917c478bd9Sstevel@tonic-gate char *swap;
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate /*
1947c478bd9Sstevel@tonic-gate * compute lengths of blocks and number and size of cycles
1957c478bd9Sstevel@tonic-gate */
1967c478bd9Sstevel@tonic-gate nnonopts = panonopt_end - panonopt_start;
1977c478bd9Sstevel@tonic-gate nopts = opt_end - panonopt_end;
1987c478bd9Sstevel@tonic-gate ncycle = gcd(nnonopts, nopts);
1997c478bd9Sstevel@tonic-gate cyclelen = (opt_end - panonopt_start) / ncycle;
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate for (i = 0; i < ncycle; i++) {
2027c478bd9Sstevel@tonic-gate cstart = panonopt_end+i;
2037c478bd9Sstevel@tonic-gate pos = cstart;
2047c478bd9Sstevel@tonic-gate for (j = 0; j < cyclelen; j++) {
2057c478bd9Sstevel@tonic-gate if (pos >= panonopt_end)
2067c478bd9Sstevel@tonic-gate pos -= nnonopts;
2077c478bd9Sstevel@tonic-gate else
2087c478bd9Sstevel@tonic-gate pos += nopts;
2097c478bd9Sstevel@tonic-gate swap = nargv[pos];
2107c478bd9Sstevel@tonic-gate ((char **)nargv)[pos] = nargv[cstart];
2117c478bd9Sstevel@tonic-gate ((char **)nargv)[cstart] = swap;
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate } /* permute_args() */
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate /*
2177c478bd9Sstevel@tonic-gate * Verify that each short option (character flag) has a long equivalent,
2187c478bd9Sstevel@tonic-gate * and that each long option has a short option equivalent. Note that
2197c478bd9Sstevel@tonic-gate * multiple long options can map to the same character.
2207c478bd9Sstevel@tonic-gate *
2217c478bd9Sstevel@tonic-gate * This behavior is defined by Sun's CLIP specification (11/12/02),
2227c478bd9Sstevel@tonic-gate * and currently getopt_clip() is the only getopt variant that
2237c478bd9Sstevel@tonic-gate * requires it.
2247c478bd9Sstevel@tonic-gate *
2257c478bd9Sstevel@tonic-gate * If error output is enabled and an error is found, this function
2267c478bd9Sstevel@tonic-gate * prints ONE error message (the first error found) and returns an
2277c478bd9Sstevel@tonic-gate * error value.
2287c478bd9Sstevel@tonic-gate *
2297c478bd9Sstevel@tonic-gate * ASSUMES: options != NULL
2307c478bd9Sstevel@tonic-gate * ASSUMES: long_options may be NULL
2317c478bd9Sstevel@tonic-gate *
2327c478bd9Sstevel@tonic-gate * Returns < 0 if an error is found
2337c478bd9Sstevel@tonic-gate * Returns >= 0 on success
2347c478bd9Sstevel@tonic-gate */
2357c478bd9Sstevel@tonic-gate static int
verify_short_long_equivalents(int nargc __unused,char * const * nargv,const char * options,const struct option * long_options,uint_t flags)236*4a38094cSToomas Soome verify_short_long_equivalents(int nargc __unused, char *const *nargv,
237*4a38094cSToomas Soome const char *options, const struct option *long_options, uint_t flags)
238*4a38094cSToomas Soome {
2397c478bd9Sstevel@tonic-gate int short_i = 0;
2407c478bd9Sstevel@tonic-gate int long_i = 0;
2417c478bd9Sstevel@tonic-gate int equivFound = 0;
2427c478bd9Sstevel@tonic-gate int ch = 0;
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate /*
2457c478bd9Sstevel@tonic-gate * Find a long option for each short option
2467c478bd9Sstevel@tonic-gate */
2477c478bd9Sstevel@tonic-gate equivFound = 1;
2487c478bd9Sstevel@tonic-gate for (short_i = 0; equivFound && (options[short_i] != 0); ++short_i) {
2497c478bd9Sstevel@tonic-gate ch = options[short_i];
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate if (ch == ':') {
2527c478bd9Sstevel@tonic-gate continue;
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate if (FLAG_IS_SET(FLAG_W_SEMICOLON) &&
255*4a38094cSToomas Soome (ch == 'W') && (options[short_i+1] == ';')) {
2567c478bd9Sstevel@tonic-gate /* W; is a special case */
2577c478bd9Sstevel@tonic-gate ++short_i;
2587c478bd9Sstevel@tonic-gate continue;
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate
2617c478bd9Sstevel@tonic-gate equivFound = 0;
2627c478bd9Sstevel@tonic-gate if (long_options != NULL) {
2637c478bd9Sstevel@tonic-gate for (long_i = 0; ((!equivFound) &&
264*4a38094cSToomas Soome (long_options[long_i].name != NULL));
265*4a38094cSToomas Soome ++long_i) {
2667c478bd9Sstevel@tonic-gate equivFound = (ch == long_options[long_i].val);
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate if ((!equivFound) && (PRINT_ERROR)) {
2707c478bd9Sstevel@tonic-gate warnxchar(nargv[0],
271*4a38094cSToomas Soome _libc_gettext(
272*4a38094cSToomas Soome "%s: equivalent long option required -- %s"),
273*4a38094cSToomas Soome ch);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate } /* short_i */
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate /*
2787c478bd9Sstevel@tonic-gate * Find a short option for each long option. Note that if we came
2797c478bd9Sstevel@tonic-gate * out of the above loop with equivFound==0, we are already done.
2807c478bd9Sstevel@tonic-gate */
2817c478bd9Sstevel@tonic-gate if (equivFound && (long_options != NULL)) {
2827c478bd9Sstevel@tonic-gate for (long_i = 0; (equivFound &&
283*4a38094cSToomas Soome (long_options[long_i].name != NULL));
284*4a38094cSToomas Soome ++long_i) {
2857c478bd9Sstevel@tonic-gate equivFound = ((long_options[long_i].val != 0) &&
286*4a38094cSToomas Soome (strchr(options, long_options[long_i].val)
287*4a38094cSToomas Soome != NULL));
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate if ((!equivFound) && (PRINT_ERROR)) {
290d362b749Svk warnx_getopt(nargv[0],
291*4a38094cSToomas Soome _libc_gettext("%s: equivalent short "
292*4a38094cSToomas Soome "option required -- %s"),
293*4a38094cSToomas Soome long_options[long_i].name);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate } /* for long_i */
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate return (equivFound? 0:-1);
2997c478bd9Sstevel@tonic-gate } /* verify_short_long_equivalents() */
3007c478bd9Sstevel@tonic-gate
3017c478bd9Sstevel@tonic-gate /*
3027c478bd9Sstevel@tonic-gate * parse_long_options --
3037c478bd9Sstevel@tonic-gate * Parse long options in argc/argv argument vector.
3047c478bd9Sstevel@tonic-gate * Returns -1 if short_too is set and the option does not match long_options.
3057c478bd9Sstevel@tonic-gate */
3067c478bd9Sstevel@tonic-gate static int
parse_long_options(int nargc,char * const * nargv,const char * options,const struct option * long_options,int * idx,int short_too,uint_t flags)3077c478bd9Sstevel@tonic-gate parse_long_options(int nargc, char * const *nargv, const char *options,
308*4a38094cSToomas Soome const struct option *long_options, int *idx, int short_too,
309*4a38094cSToomas Soome uint_t flags)
3107c478bd9Sstevel@tonic-gate {
3117c478bd9Sstevel@tonic-gate char *current_argv = NULL;
3127c478bd9Sstevel@tonic-gate char *argv_equal_ptr = NULL;
3137c478bd9Sstevel@tonic-gate size_t current_argv_len = 0;
3147c478bd9Sstevel@tonic-gate size_t long_option_len = 0;
3157c478bd9Sstevel@tonic-gate int i = 0;
3167c478bd9Sstevel@tonic-gate int match = 0;
3177c478bd9Sstevel@tonic-gate
3187c478bd9Sstevel@tonic-gate current_argv = place;
3197c478bd9Sstevel@tonic-gate match = -1;
3207c478bd9Sstevel@tonic-gate
3217c478bd9Sstevel@tonic-gate optind++;
3227c478bd9Sstevel@tonic-gate
3237c478bd9Sstevel@tonic-gate if ((argv_equal_ptr = strchr(current_argv, '=')) != NULL) {
3247c478bd9Sstevel@tonic-gate /* argument found (--option=arg) */
3257c478bd9Sstevel@tonic-gate current_argv_len = (argv_equal_ptr - current_argv);
3267c478bd9Sstevel@tonic-gate argv_equal_ptr++;
3277c478bd9Sstevel@tonic-gate } else {
3287c478bd9Sstevel@tonic-gate current_argv_len = strlen(current_argv);
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate
3317c478bd9Sstevel@tonic-gate for (i = 0; (long_options[i].name != NULL); i++) {
3327c478bd9Sstevel@tonic-gate
3337c478bd9Sstevel@tonic-gate /* find matching long option */
3347c478bd9Sstevel@tonic-gate if (strncmp(current_argv, long_options[i].name,
335d362b749Svk current_argv_len) != 0) {
3367c478bd9Sstevel@tonic-gate continue; /* no match */
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate long_option_len = strlen(long_options[i].name);
3397c478bd9Sstevel@tonic-gate if ((!FLAG_IS_SET(FLAG_ABBREV)) &&
3407c478bd9Sstevel@tonic-gate (long_option_len > current_argv_len)) {
3417c478bd9Sstevel@tonic-gate continue; /* Abbreviations are disabled */
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate
3447c478bd9Sstevel@tonic-gate if (long_option_len == current_argv_len) {
3457c478bd9Sstevel@tonic-gate /* exact match */
3467c478bd9Sstevel@tonic-gate match = i;
3477c478bd9Sstevel@tonic-gate break;
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate /*
3507c478bd9Sstevel@tonic-gate * If this is a known short option, don't allow
3517c478bd9Sstevel@tonic-gate * a partial match of a single character.
3527c478bd9Sstevel@tonic-gate */
3537c478bd9Sstevel@tonic-gate if (short_too && current_argv_len == 1)
3547c478bd9Sstevel@tonic-gate continue;
3557c478bd9Sstevel@tonic-gate
3567c478bd9Sstevel@tonic-gate if (match == -1) /* partial match */
3577c478bd9Sstevel@tonic-gate match = i;
3587c478bd9Sstevel@tonic-gate else {
3597c478bd9Sstevel@tonic-gate /* ambiguous abbreviation */
3607c478bd9Sstevel@tonic-gate if (PRINT_ERROR) {
3617c478bd9Sstevel@tonic-gate warnxlen(nargv[0],
3627c478bd9Sstevel@tonic-gate _libc_gettext(
363d362b749Svk "%s: ambiguous option -- %s"),
3647c478bd9Sstevel@tonic-gate (int)current_argv_len,
3657c478bd9Sstevel@tonic-gate current_argv);
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate optopt = 0;
3687c478bd9Sstevel@tonic-gate return (BADCH);
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate } /* for i */
3717c478bd9Sstevel@tonic-gate if (match != -1) { /* option found */
3727c478bd9Sstevel@tonic-gate if ((long_options[match].has_arg == no_argument) &&
3737c478bd9Sstevel@tonic-gate (argv_equal_ptr != NULL)) {
3747c478bd9Sstevel@tonic-gate if (PRINT_ERROR) {
3757c478bd9Sstevel@tonic-gate warnxlen(nargv[0],
376d362b749Svk _libc_gettext(
3777c478bd9Sstevel@tonic-gate "%s: option doesn't take an argument -- %s"),
378d362b749Svk (int)current_argv_len,
379d362b749Svk current_argv);
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate /*
3827c478bd9Sstevel@tonic-gate * XXX: GNU sets optopt to val regardless of flag
3837c478bd9Sstevel@tonic-gate */
3847c478bd9Sstevel@tonic-gate if (long_options[match].flag == NULL)
3857c478bd9Sstevel@tonic-gate optopt = long_options[match].val;
3867c478bd9Sstevel@tonic-gate else
3877c478bd9Sstevel@tonic-gate optopt = 0;
3887c478bd9Sstevel@tonic-gate return (BADARG);
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate if (long_options[match].has_arg == required_argument ||
3917c478bd9Sstevel@tonic-gate long_options[match].has_arg == optional_argument) {
3927c478bd9Sstevel@tonic-gate if (argv_equal_ptr != NULL) {
3937c478bd9Sstevel@tonic-gate optarg = argv_equal_ptr;
3947c478bd9Sstevel@tonic-gate } else if (LONGOPT_REQUIRES_ARG(long_options[match])) {
3957c478bd9Sstevel@tonic-gate /* The next argv must be the option argument */
3967c478bd9Sstevel@tonic-gate if (optind < nargc) {
397d362b749Svk optarg = nargv[optind];
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate ++optind; /* code below depends on this */
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate if (LONGOPT_REQUIRES_ARG(long_options[match]) &&
4037c478bd9Sstevel@tonic-gate (optarg == NULL)) {
4047c478bd9Sstevel@tonic-gate /*
4057c478bd9Sstevel@tonic-gate * Missing argument; leading ':' indicates no error
4067c478bd9Sstevel@tonic-gate * should be generated.
4077c478bd9Sstevel@tonic-gate */
4087c478bd9Sstevel@tonic-gate if (PRINT_ERROR) {
409d362b749Svk warnx_getopt(nargv[0],
410d362b749Svk _libc_gettext(
4117c478bd9Sstevel@tonic-gate "%s: option requires an argument -- %s"),
412d362b749Svk current_argv);
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate /*
4157c478bd9Sstevel@tonic-gate * XXX: GNU sets optopt to val regardless of flag
4167c478bd9Sstevel@tonic-gate */
4177c478bd9Sstevel@tonic-gate if (long_options[match].flag == NULL)
4187c478bd9Sstevel@tonic-gate optopt = long_options[match].val;
4197c478bd9Sstevel@tonic-gate else
4207c478bd9Sstevel@tonic-gate optopt = 0;
4217c478bd9Sstevel@tonic-gate --optind;
4227c478bd9Sstevel@tonic-gate return (BADARG);
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate } else { /* unknown option */
4257c478bd9Sstevel@tonic-gate if (short_too) {
4267c478bd9Sstevel@tonic-gate --optind;
4277c478bd9Sstevel@tonic-gate return (-1);
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate if (PRINT_ERROR) {
430d362b749Svk warnx_getopt(nargv[0],
431d362b749Svk _libc_gettext("%s: illegal option -- %s"),
432d362b749Svk current_argv);
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate optopt = 0;
4357c478bd9Sstevel@tonic-gate return (BADCH);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate if (idx)
4387c478bd9Sstevel@tonic-gate *idx = match;
4397c478bd9Sstevel@tonic-gate if (long_options[match].flag != NULL) {
4407c478bd9Sstevel@tonic-gate *long_options[match].flag = long_options[match].val;
4417c478bd9Sstevel@tonic-gate return (0);
4427c478bd9Sstevel@tonic-gate } else {
4437c478bd9Sstevel@tonic-gate optopt = long_options[match].val;
4447c478bd9Sstevel@tonic-gate return (optopt);
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate } /* parse_long_options() */
4477c478bd9Sstevel@tonic-gate
4487c478bd9Sstevel@tonic-gate /*
4497c478bd9Sstevel@tonic-gate * getopt_internal() --
4507c478bd9Sstevel@tonic-gate * Parse argc/argv argument vector. Called by user level routines.
4517c478bd9Sstevel@tonic-gate *
4527c478bd9Sstevel@tonic-gate * This implements all of the getopt_long(), getopt_long_only(),
4537c478bd9Sstevel@tonic-gate * getopt_clip() variants.
4547c478bd9Sstevel@tonic-gate */
4557c478bd9Sstevel@tonic-gate static int
getopt_internal(int nargc,char * const * nargv,const char * options,const struct option * long_options,int * idx,uint_t flags)4567c478bd9Sstevel@tonic-gate getopt_internal(int nargc, char * const *nargv, const char *options,
457*4a38094cSToomas Soome const struct option *long_options, int *idx, uint_t flags)
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate char *oli; /* option letter list index */
4607c478bd9Sstevel@tonic-gate int optchar, short_too;
4617c478bd9Sstevel@tonic-gate static int posixly_correct = -1;
4627c478bd9Sstevel@tonic-gate
4637c478bd9Sstevel@tonic-gate if (options == NULL)
4647c478bd9Sstevel@tonic-gate return (-1);
4657c478bd9Sstevel@tonic-gate
4667c478bd9Sstevel@tonic-gate /*
4677c478bd9Sstevel@tonic-gate * Disable GNU extensions if POSIXLY_CORRECT is set or options
4687c478bd9Sstevel@tonic-gate * string begins with a '+'.
4697c478bd9Sstevel@tonic-gate */
4707c478bd9Sstevel@tonic-gate if (posixly_correct == -1) {
4717c478bd9Sstevel@tonic-gate posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
4727c478bd9Sstevel@tonic-gate }
4737c478bd9Sstevel@tonic-gate if (FLAG_IS_SET(FLAG_PLUS_DASH_START)) {
4747c478bd9Sstevel@tonic-gate /*
4757c478bd9Sstevel@tonic-gate * + or - at start of optstring takes precedence
4767c478bd9Sstevel@tonic-gate * over POSIXLY_CORRECT.
4777c478bd9Sstevel@tonic-gate */
4787c478bd9Sstevel@tonic-gate if (*options == '+') {
4797c478bd9Sstevel@tonic-gate /*
4807c478bd9Sstevel@tonic-gate * leading + means POSIX-compliant; first non-option
4817c478bd9Sstevel@tonic-gate * ends option list. Therefore, don't permute args.
4827c478bd9Sstevel@tonic-gate */
4837c478bd9Sstevel@tonic-gate posixly_correct = 1;
4847c478bd9Sstevel@tonic-gate } else if (*options == '-') {
4857c478bd9Sstevel@tonic-gate posixly_correct = 0;
4867c478bd9Sstevel@tonic-gate flags |= FLAG_ALLARGS;
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate if ((*options == '+') || (*options == '-')) {
4897c478bd9Sstevel@tonic-gate options++;
4907c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate } /* if FLAG_PLUS_DASH_START */
4927c478bd9Sstevel@tonic-gate
4937c478bd9Sstevel@tonic-gate if (posixly_correct) {
494d362b749Svk flags &= ~FLAG_PERMUTE;
495d362b749Svk flags &= ~FLAG_ALLARGS;
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate
4987c478bd9Sstevel@tonic-gate /*
4997c478bd9Sstevel@tonic-gate * Some programs (like GNU cvs) set optind to 0 to restart
5007c478bd9Sstevel@tonic-gate * option processing. Work around this braindamage.
5017c478bd9Sstevel@tonic-gate *
5027c478bd9Sstevel@tonic-gate * The above problem comes from using global variables. We
5037c478bd9Sstevel@tonic-gate * should avoid their use in the future.
5047c478bd9Sstevel@tonic-gate */
5057c478bd9Sstevel@tonic-gate if (optind == 0) {
5067c478bd9Sstevel@tonic-gate optind = optreset = 1;
5077c478bd9Sstevel@tonic-gate }
5087c478bd9Sstevel@tonic-gate
5097c478bd9Sstevel@tonic-gate optarg = NULL;
5107c478bd9Sstevel@tonic-gate optopt = 0;
5117c478bd9Sstevel@tonic-gate
5127c478bd9Sstevel@tonic-gate if (optreset) {
5137c478bd9Sstevel@tonic-gate nonopt_start = nonopt_end = -1;
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate
5167c478bd9Sstevel@tonic-gate /*
5177c478bd9Sstevel@tonic-gate * On the first call, make sure that there is a short equivalent
5187c478bd9Sstevel@tonic-gate * for each long option, and vice versa. This is required by
5197c478bd9Sstevel@tonic-gate * Sun's CLIP specification (11/12/02).
5207c478bd9Sstevel@tonic-gate */
5217c478bd9Sstevel@tonic-gate if ((optind == 1) && FLAG_IS_SET(FLAG_REQUIRE_EQUIVALENTS)) {
522d362b749Svk if (verify_short_long_equivalents(
523d362b749Svk nargc, nargv, options, long_options, flags) < 0) {
524d362b749Svk /* function printed any necessary messages */
525d362b749Svk errno = EINVAL; /* invalid argument */
526d362b749Svk return (-1);
527d362b749Svk }
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate
5307c478bd9Sstevel@tonic-gate start:
5317c478bd9Sstevel@tonic-gate if (optreset || !*place) { /* update scanning pointer */
5327c478bd9Sstevel@tonic-gate optreset = 0;
5337c478bd9Sstevel@tonic-gate if (optind >= nargc) { /* end of argument vector */
5347c478bd9Sstevel@tonic-gate place = EMSG;
5357c478bd9Sstevel@tonic-gate if (nonopt_end != -1) {
5367c478bd9Sstevel@tonic-gate /* do permutation, if we have to */
5377c478bd9Sstevel@tonic-gate permute_args(nonopt_start, nonopt_end,
5387c478bd9Sstevel@tonic-gate optind, nargv);
5397c478bd9Sstevel@tonic-gate optind -= nonopt_end - nonopt_start;
5407c478bd9Sstevel@tonic-gate
5417c478bd9Sstevel@tonic-gate } else if (nonopt_start != -1) {
5427c478bd9Sstevel@tonic-gate /*
5437c478bd9Sstevel@tonic-gate * If we skipped non-options, set optind
5447c478bd9Sstevel@tonic-gate * to the first of them.
5457c478bd9Sstevel@tonic-gate */
5467c478bd9Sstevel@tonic-gate optind = nonopt_start;
5477c478bd9Sstevel@tonic-gate }
5487c478bd9Sstevel@tonic-gate nonopt_start = nonopt_end = -1;
5497c478bd9Sstevel@tonic-gate return (-1);
5507c478bd9Sstevel@tonic-gate }
5517c478bd9Sstevel@tonic-gate if ((*(place = nargv[optind]) != '-') || (place[1] == '\0')) {
5527c478bd9Sstevel@tonic-gate place = EMSG; /* found non-option */
5537c478bd9Sstevel@tonic-gate if (flags & FLAG_ALLARGS) {
5547c478bd9Sstevel@tonic-gate /*
5557c478bd9Sstevel@tonic-gate * GNU extension:
5567c478bd9Sstevel@tonic-gate * return non-option as argument to option '\1'
5577c478bd9Sstevel@tonic-gate */
5587c478bd9Sstevel@tonic-gate optarg = nargv[optind++];
5597c478bd9Sstevel@tonic-gate return (INORDER);
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate if (!(flags & FLAG_PERMUTE)) {
5627c478bd9Sstevel@tonic-gate /*
5637c478bd9Sstevel@tonic-gate * If no permutation wanted, stop parsing
5647c478bd9Sstevel@tonic-gate * at first non-option.
5657c478bd9Sstevel@tonic-gate */
5667c478bd9Sstevel@tonic-gate return (-1);
5677c478bd9Sstevel@tonic-gate }
5687c478bd9Sstevel@tonic-gate /* do permutation */
5697c478bd9Sstevel@tonic-gate if (nonopt_start == -1)
5707c478bd9Sstevel@tonic-gate nonopt_start = optind;
5717c478bd9Sstevel@tonic-gate else if (nonopt_end != -1) {
5727c478bd9Sstevel@tonic-gate permute_args(nonopt_start, nonopt_end,
5737c478bd9Sstevel@tonic-gate optind, nargv);
5747c478bd9Sstevel@tonic-gate nonopt_start = optind -
5757c478bd9Sstevel@tonic-gate (nonopt_end - nonopt_start);
5767c478bd9Sstevel@tonic-gate nonopt_end = -1;
5777c478bd9Sstevel@tonic-gate }
5787c478bd9Sstevel@tonic-gate optind++;
5797c478bd9Sstevel@tonic-gate /* process next argument */
5807c478bd9Sstevel@tonic-gate goto start;
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate if (nonopt_start != -1 && nonopt_end == -1)
5837c478bd9Sstevel@tonic-gate nonopt_end = optind;
5847c478bd9Sstevel@tonic-gate
5857c478bd9Sstevel@tonic-gate /*
5867c478bd9Sstevel@tonic-gate * Check for "--" or "--foo" with no long options
5877c478bd9Sstevel@tonic-gate * but if place is simply "-" leave it unmolested.
5887c478bd9Sstevel@tonic-gate */
5897c478bd9Sstevel@tonic-gate if (place[1] != '\0' && *++place == '-' &&
5907c478bd9Sstevel@tonic-gate (place[1] == '\0' || long_options == NULL)) {
5917c478bd9Sstevel@tonic-gate optind++;
5927c478bd9Sstevel@tonic-gate place = EMSG;
5937c478bd9Sstevel@tonic-gate /*
5947c478bd9Sstevel@tonic-gate * We found an option (--), so if we skipped
5957c478bd9Sstevel@tonic-gate * non-options, we have to permute.
5967c478bd9Sstevel@tonic-gate */
5977c478bd9Sstevel@tonic-gate if (nonopt_end != -1) {
5987c478bd9Sstevel@tonic-gate permute_args(nonopt_start, nonopt_end,
5997c478bd9Sstevel@tonic-gate optind, nargv);
6007c478bd9Sstevel@tonic-gate optind -= nonopt_end - nonopt_start;
6017c478bd9Sstevel@tonic-gate }
6027c478bd9Sstevel@tonic-gate nonopt_start = nonopt_end = -1;
6037c478bd9Sstevel@tonic-gate return (-1);
6047c478bd9Sstevel@tonic-gate }
6057c478bd9Sstevel@tonic-gate }
6067c478bd9Sstevel@tonic-gate
6077c478bd9Sstevel@tonic-gate /*
6087c478bd9Sstevel@tonic-gate * Check long options if:
6097c478bd9Sstevel@tonic-gate * 1) we were passed some
6107c478bd9Sstevel@tonic-gate * 2) the arg is not just "-"
6117c478bd9Sstevel@tonic-gate * 3) either the arg starts with -- or we are getopt_long_only()
6127c478bd9Sstevel@tonic-gate */
6137c478bd9Sstevel@tonic-gate if (long_options != NULL && place != nargv[optind] &&
6147c478bd9Sstevel@tonic-gate (*place == '-' || (FLAG_IS_SET(FLAG_LONGONLY)))) {
6157c478bd9Sstevel@tonic-gate short_too = 0;
6167c478bd9Sstevel@tonic-gate if (*place == '-')
6177c478bd9Sstevel@tonic-gate place++; /* --foo long option */
6187c478bd9Sstevel@tonic-gate else if (*place != ':' && strchr(options, *place) != NULL)
6197c478bd9Sstevel@tonic-gate short_too = 1; /* could be short option too */
6207c478bd9Sstevel@tonic-gate
6217c478bd9Sstevel@tonic-gate optchar = parse_long_options(nargc, nargv, options,
622d362b749Svk long_options, idx, short_too, flags);
6237c478bd9Sstevel@tonic-gate if (optchar != -1) {
6247c478bd9Sstevel@tonic-gate place = EMSG;
6257c478bd9Sstevel@tonic-gate return (optchar);
6267c478bd9Sstevel@tonic-gate }
6277c478bd9Sstevel@tonic-gate }
6287c478bd9Sstevel@tonic-gate
6297c478bd9Sstevel@tonic-gate if ((optchar = (int)*place++) == (int)':' ||
6307c478bd9Sstevel@tonic-gate (oli = strchr(options, optchar)) == NULL) {
6317c478bd9Sstevel@tonic-gate /*
6327c478bd9Sstevel@tonic-gate * If the user didn't specify '-' as an option,
6337c478bd9Sstevel@tonic-gate * assume it means -1 as POSIX specifies.
6347c478bd9Sstevel@tonic-gate */
6357c478bd9Sstevel@tonic-gate if (optchar == (int)'-')
6367c478bd9Sstevel@tonic-gate return (-1);
6377c478bd9Sstevel@tonic-gate /* option letter unknown or ':' */
6387c478bd9Sstevel@tonic-gate if (!*place)
6397c478bd9Sstevel@tonic-gate ++optind;
6407c478bd9Sstevel@tonic-gate if (PRINT_ERROR)
6417c478bd9Sstevel@tonic-gate warnxchar(nargv[0],
642d362b749Svk _libc_gettext("%s: illegal option -- %s"),
643d362b749Svk optchar);
6447c478bd9Sstevel@tonic-gate optopt = optchar;
6457c478bd9Sstevel@tonic-gate return (BADCH);
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate if (FLAG_IS_SET(FLAG_W_SEMICOLON) &&
6487c478bd9Sstevel@tonic-gate (long_options != NULL) && (optchar == 'W') && (oli[1] == ';')) {
6497c478bd9Sstevel@tonic-gate /* -W long-option */
6507c478bd9Sstevel@tonic-gate /* LINTED: statement has no consequent: if */
6517c478bd9Sstevel@tonic-gate if (*place) { /* no space */
6527c478bd9Sstevel@tonic-gate /* NOTHING */;
6537c478bd9Sstevel@tonic-gate } else if (++optind >= nargc) { /* no long-option after -W */
6547c478bd9Sstevel@tonic-gate place = EMSG;
6557c478bd9Sstevel@tonic-gate if (PRINT_ERROR)
6567c478bd9Sstevel@tonic-gate warnxchar(nargv[0],
657d362b749Svk _libc_gettext(
6587c478bd9Sstevel@tonic-gate "%s: option requires an argument -- %s"),
659d362b749Svk optchar);
6607c478bd9Sstevel@tonic-gate optopt = optchar;
6617c478bd9Sstevel@tonic-gate return (BADARG);
6627c478bd9Sstevel@tonic-gate } else { /* white space */
6637c478bd9Sstevel@tonic-gate place = nargv[optind];
6647c478bd9Sstevel@tonic-gate }
6657c478bd9Sstevel@tonic-gate optchar = parse_long_options(
666d362b749Svk nargc, nargv, options, long_options,
6677c478bd9Sstevel@tonic-gate idx, 0, flags);
6687c478bd9Sstevel@tonic-gate
6697c478bd9Sstevel@tonic-gate /*
6707c478bd9Sstevel@tonic-gate * PSARC 2003/645 - Match GNU behavior, set optarg to
6717c478bd9Sstevel@tonic-gate * the long-option.
6727c478bd9Sstevel@tonic-gate */
6737c478bd9Sstevel@tonic-gate if (optarg == NULL) {
6747c478bd9Sstevel@tonic-gate optarg = nargv[optind-1];
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate place = EMSG;
6777c478bd9Sstevel@tonic-gate return (optchar);
6787c478bd9Sstevel@tonic-gate }
6797c478bd9Sstevel@tonic-gate if (*++oli != ':') { /* doesn't take argument */
6807c478bd9Sstevel@tonic-gate if (!*place)
6817c478bd9Sstevel@tonic-gate ++optind;
6827c478bd9Sstevel@tonic-gate } else { /* takes (optional) argument */
6837c478bd9Sstevel@tonic-gate optarg = NULL;
6847c478bd9Sstevel@tonic-gate if (*place) { /* no white space */
6857c478bd9Sstevel@tonic-gate optarg = place;
6867c478bd9Sstevel@tonic-gate /* XXX: disable test for :: if PC? (GNU doesn't) */
6877c478bd9Sstevel@tonic-gate } else if (!(FLAG_IS_SET(FLAG_OPTIONAL_ARGS) &&
688d362b749Svk (oli[1] == ':'))) {
6897c478bd9Sstevel@tonic-gate /* arg is required (not optional) */
6907c478bd9Sstevel@tonic-gate
6917c478bd9Sstevel@tonic-gate if (++optind >= nargc) { /* no arg */
6927c478bd9Sstevel@tonic-gate place = EMSG;
6937c478bd9Sstevel@tonic-gate if (PRINT_ERROR) {
6947c478bd9Sstevel@tonic-gate warnxchar(nargv[0],
695d362b749Svk _libc_gettext(
6967c478bd9Sstevel@tonic-gate "%s: option requires an argument -- %s"),
697d362b749Svk optchar);
6987c478bd9Sstevel@tonic-gate }
6997c478bd9Sstevel@tonic-gate optopt = optchar;
7007c478bd9Sstevel@tonic-gate return (BADARG);
7017c478bd9Sstevel@tonic-gate } else
7027c478bd9Sstevel@tonic-gate optarg = nargv[optind];
7037c478bd9Sstevel@tonic-gate }
7047c478bd9Sstevel@tonic-gate place = EMSG;
7057c478bd9Sstevel@tonic-gate ++optind;
7067c478bd9Sstevel@tonic-gate }
7077c478bd9Sstevel@tonic-gate /* return valid option letter */
7087c478bd9Sstevel@tonic-gate optopt = optchar; /* preserve getopt() behavior */
7097c478bd9Sstevel@tonic-gate return (optchar);
7107c478bd9Sstevel@tonic-gate } /* getopt_internal() */
7117c478bd9Sstevel@tonic-gate
7127c478bd9Sstevel@tonic-gate /*
7137c478bd9Sstevel@tonic-gate * getopt_long() --
7147c478bd9Sstevel@tonic-gate * Parse argc/argv argument vector.
7157c478bd9Sstevel@tonic-gate *
7167c478bd9Sstevel@tonic-gate * Requires that long options be preceded with a two dashes
7177c478bd9Sstevel@tonic-gate * (e.g., --longoption).
7187c478bd9Sstevel@tonic-gate */
7197c478bd9Sstevel@tonic-gate int
getopt_long(int nargc,char * const * nargv,const char * optstring,const struct option * long_options,int * long_index)7207c478bd9Sstevel@tonic-gate getopt_long(int nargc, char *const *nargv,
721*4a38094cSToomas Soome const char *optstring,
722*4a38094cSToomas Soome const struct option *long_options, int *long_index)
7237c478bd9Sstevel@tonic-gate {
7247c478bd9Sstevel@tonic-gate
7257c478bd9Sstevel@tonic-gate return (getopt_internal(
726d362b749Svk nargc, nargv, optstring, long_options, long_index,
727d362b749Svk FLAG_PERMUTE
728d362b749Svk | FLAG_OPTIONAL_ARGS
729d362b749Svk | FLAG_ABBREV
730d362b749Svk | FLAG_W_SEMICOLON
731d362b749Svk | FLAG_PLUS_DASH_START));
7327c478bd9Sstevel@tonic-gate } /* getopt_long() */
7337c478bd9Sstevel@tonic-gate
7347c478bd9Sstevel@tonic-gate /*
7357c478bd9Sstevel@tonic-gate * getopt_long_only() --
7367c478bd9Sstevel@tonic-gate * Parse argc/argv argument vector.
7377c478bd9Sstevel@tonic-gate *
7387c478bd9Sstevel@tonic-gate * Long options may be preceded with a single dash (e.g., -longoption)
7397c478bd9Sstevel@tonic-gate */
7407c478bd9Sstevel@tonic-gate int
getopt_long_only(int nargc,char * const * nargv,const char * optstring,const struct option * long_options,int * long_index)7417c478bd9Sstevel@tonic-gate getopt_long_only(int nargc, char *const *nargv,
742*4a38094cSToomas Soome const char *optstring,
743*4a38094cSToomas Soome const struct option *long_options, int *long_index)
7447c478bd9Sstevel@tonic-gate {
7457c478bd9Sstevel@tonic-gate
7467c478bd9Sstevel@tonic-gate return (getopt_internal(
747d362b749Svk nargc, nargv, optstring, long_options, long_index,
748d362b749Svk FLAG_PERMUTE
749d362b749Svk | FLAG_OPTIONAL_ARGS
750d362b749Svk | FLAG_ABBREV
751d362b749Svk | FLAG_W_SEMICOLON
752d362b749Svk | FLAG_PLUS_DASH_START
753d362b749Svk | FLAG_LONGONLY));
7547c478bd9Sstevel@tonic-gate } /* getopt_long_only() */
7557c478bd9Sstevel@tonic-gate
7567c478bd9Sstevel@tonic-gate /*
7577c478bd9Sstevel@tonic-gate * getopt_clip() --
7587c478bd9Sstevel@tonic-gate * Parse argc/argv argument vector, requiring compliance with
759*4a38094cSToomas Soome * Sun's CLIP specification (11/12/02)
7607c478bd9Sstevel@tonic-gate *
7617c478bd9Sstevel@tonic-gate * o Does not allow arguments to be optional (optional_argument is
7627c478bd9Sstevel@tonic-gate * treated as required_argument).
7637c478bd9Sstevel@tonic-gate *
7647c478bd9Sstevel@tonic-gate * o Does not allow long options to be abbreviated on the command line
7657c478bd9Sstevel@tonic-gate *
7667c478bd9Sstevel@tonic-gate * o Does not allow long argument to be preceded by a single dash
7677c478bd9Sstevel@tonic-gate * (Double-dash '--' is required)
7687c478bd9Sstevel@tonic-gate *
7697c478bd9Sstevel@tonic-gate * o Stops option processing at the first non-option
7707c478bd9Sstevel@tonic-gate *
7717c478bd9Sstevel@tonic-gate * o Requires that every long option have a short-option (single
7727c478bd9Sstevel@tonic-gate * character) equivalent and vice-versa. If a short option or
7737c478bd9Sstevel@tonic-gate * long option without an equivalent is found, an error message
7747c478bd9Sstevel@tonic-gate * is printed and -1 is returned on the first call, and errno
7757c478bd9Sstevel@tonic-gate * is set to EINVAL.
7767c478bd9Sstevel@tonic-gate *
7777c478bd9Sstevel@tonic-gate * o Leading + or - in optstring is ignored, and opstring is
7787c478bd9Sstevel@tonic-gate * treated as if it began after the + or - .
7797c478bd9Sstevel@tonic-gate */
7807c478bd9Sstevel@tonic-gate int
getopt_clip(int nargc,char * const * nargv,const char * optstring,const struct option * long_options,int * long_index)781*4a38094cSToomas Soome getopt_clip(int nargc, char *const *nargv, const char *optstring,
782*4a38094cSToomas Soome const struct option *long_options, int *long_index)
7837c478bd9Sstevel@tonic-gate {
7847c478bd9Sstevel@tonic-gate return getopt_internal(
785d362b749Svk nargc, nargv, optstring, long_options, long_index,
7867c478bd9Sstevel@tonic-gate /*
7877c478bd9Sstevel@tonic-gate * no permutation,
7887c478bd9Sstevel@tonic-gate * no optional args,
7897c478bd9Sstevel@tonic-gate * no long-only,
7907c478bd9Sstevel@tonic-gate * no abbreviations
7917c478bd9Sstevel@tonic-gate * no support for +- at start of optstring
7927c478bd9Sstevel@tonic-gate * yes support for "W;" in optstring
7937c478bd9Sstevel@tonic-gate */
794d362b749Svk FLAG_W_SEMICOLON
795d362b749Svk | FLAG_REQUIRE_EQUIVALENTS);
7967c478bd9Sstevel@tonic-gate } /* getopt_clip() */
797