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