1 1.1 christos /* $NetBSD: getopt.c,v 1.1.1.1 2016/01/13 03:15:30 christos Exp $ */ 2 1.1 christos 3 1.1 christos /* Getopt for GNU. 4 1.1 christos NOTE: getopt is now part of the C library, so if you don't know what 5 1.1 christos "Keep this file name-space clean" means, talk to drepper (at) gnu.org 6 1.1 christos before changing it! 7 1.1 christos Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001 8 1.1 christos Free Software Foundation, Inc. 9 1.1 christos This file is part of the GNU C Library. 10 1.1 christos 11 1.1 christos This program is free software; you can redistribute it and/or modify 12 1.1 christos it under the terms of the GNU General Public License as published by 13 1.1 christos the Free Software Foundation; either version 2, or (at your option) 14 1.1 christos any later version. 15 1.1 christos 16 1.1 christos This program is distributed in the hope that it will be useful, 17 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 18 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 1.1 christos GNU General Public License for more details. 20 1.1 christos 21 1.1 christos You should have received a copy of the GNU General Public License 22 1.1 christos along with this program; if not, write to the Free Software Foundation, 23 1.1 christos Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 24 1.1 christos 25 1.1 christos /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>. 27 1.1 christos Ditto for AIX 3.2 and <stdlib.h>. */ 28 1.1 christos #ifndef _NO_PROTO 29 1.1 christos # define _NO_PROTO 30 1.1 christos #endif 31 1.1 christos 32 1.1 christos #ifdef HAVE_CONFIG_H 33 1.1 christos # include <config.h> 34 1.1 christos #endif 35 1.1 christos 36 1.1 christos #if !defined __STDC__ || !__STDC__ 37 1.1 christos /* This is a separate conditional since some stdc systems 38 1.1 christos reject `defined (const)'. */ 39 1.1 christos # ifndef const 40 1.1 christos # define const 41 1.1 christos # endif 42 1.1 christos #endif 43 1.1 christos 44 1.1 christos #include <stdio.h> 45 1.1 christos 46 1.1 christos /* Comment out all this code if we are using the GNU C Library, and are not 47 1.1 christos actually compiling the library itself. This code is part of the GNU C 48 1.1 christos Library, but also included in many other GNU distributions. Compiling 49 1.1 christos and linking in this code is a waste when using the GNU C library 50 1.1 christos (especially if it is a shared library). Rather than having every GNU 51 1.1 christos program understand `configure --with-gnu-libc' and omit the object files, 52 1.1 christos it is simpler to just do this in the source for each such file. */ 53 1.1 christos 54 1.1 christos #define GETOPT_INTERFACE_VERSION 2 55 1.1 christos #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2 56 1.1 christos # include <gnu-versions.h> 57 1.1 christos # if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION 58 1.1 christos # define ELIDE_CODE 59 1.1 christos # endif 60 1.1 christos #endif 61 1.1 christos 62 1.1 christos #ifndef ELIDE_CODE 63 1.1 christos 64 1.1 christos 65 1.1 christos /* This needs to come after some library #include 66 1.1 christos to get __GNU_LIBRARY__ defined. */ 67 1.1 christos #ifdef __GNU_LIBRARY__ 68 1.1 christos /* Don't include stdlib.h for non-GNU C libraries because some of them 69 1.1 christos contain conflicting prototypes for getopt. */ 70 1.1 christos # include <stdlib.h> 71 1.1 christos # include <unistd.h> 72 1.1 christos #endif /* GNU C library. */ 73 1.1 christos 74 1.1 christos #ifdef VMS 75 1.1 christos # include <unixlib.h> 76 1.1 christos # if HAVE_STRING_H - 0 77 1.1 christos # include <string.h> 78 1.1 christos # endif 79 1.1 christos #endif 80 1.1 christos 81 1.1 christos #ifndef _ 82 1.1 christos /* This is for other GNU distributions with internationalized messages. */ 83 1.1 christos # if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC 84 1.1 christos # include <libintl.h> 85 1.1 christos # ifndef _ 86 1.1 christos # define _(msgid) gettext (msgid) 87 1.1 christos # endif 88 1.1 christos # else 89 1.1 christos # define _(msgid) (msgid) 90 1.1 christos # endif 91 1.1 christos #endif 92 1.1 christos 93 1.1 christos /* This version of `getopt' appears to the caller like standard Unix `getopt' 94 1.1 christos but it behaves differently for the user, since it allows the user 95 1.1 christos to intersperse the options with the other arguments. 96 1.1 christos 97 1.1 christos As `getopt' works, it permutes the elements of ARGV so that, 98 1.1 christos when it is done, all the options precede everything else. Thus 99 1.1 christos all application programs are extended to handle flexible argument order. 100 1.1 christos 101 1.1 christos Setting the environment variable POSIXLY_CORRECT disables permutation. 102 1.1 christos Then the behavior is completely standard. 103 1.1 christos 104 1.1 christos GNU application programs can use a third alternative mode in which 105 1.1 christos they can distinguish the relative order of options and other arguments. */ 106 1.1 christos 107 1.1 christos #include "getopt.h" 108 1.1 christos 109 1.1 christos /* For communication from `getopt' to the caller. 110 1.1 christos When `getopt' finds an option that takes an argument, 111 1.1 christos the argument value is returned here. 112 1.1 christos Also, when `ordering' is RETURN_IN_ORDER, 113 1.1 christos each non-option ARGV-element is returned here. */ 114 1.1 christos 115 1.1 christos char *optarg; 116 1.1 christos 117 1.1 christos /* Index in ARGV of the next element to be scanned. 118 1.1 christos This is used for communication to and from the caller 119 1.1 christos and for communication between successive calls to `getopt'. 120 1.1 christos 121 1.1 christos On entry to `getopt', zero means this is the first call; initialize. 122 1.1 christos 123 1.1 christos When `getopt' returns -1, this is the index of the first of the 124 1.1 christos non-option elements that the caller should itself scan. 125 1.1 christos 126 1.1 christos Otherwise, `optind' communicates from one call to the next 127 1.1 christos how much of ARGV has been scanned so far. */ 128 1.1 christos 129 1.1 christos /* 1003.2 says this must be 1 before any call. */ 130 1.1 christos int optind = 1; 131 1.1 christos 132 1.1 christos /* Formerly, initialization of getopt depended on optind==0, which 133 1.1 christos causes problems with re-calling getopt as programs generally don't 134 1.1 christos know that. */ 135 1.1 christos 136 1.1 christos int __getopt_initialized; 137 1.1 christos 138 1.1 christos /* The next char to be scanned in the option-element 139 1.1 christos in which the last option character we returned was found. 140 1.1 christos This allows us to pick up the scan where we left off. 141 1.1 christos 142 1.1 christos If this is zero, or a null string, it means resume the scan 143 1.1 christos by advancing to the next ARGV-element. */ 144 1.1 christos 145 1.1 christos static char *nextchar; 146 1.1 christos 147 1.1 christos /* Callers store zero here to inhibit the error message 148 1.1 christos for unrecognized options. */ 149 1.1 christos 150 1.1 christos int opterr = 1; 151 1.1 christos 152 1.1 christos /* Set to an option character which was unrecognized. 153 1.1 christos This must be initialized on some systems to avoid linking in the 154 1.1 christos system's own getopt implementation. */ 155 1.1 christos 156 1.1 christos int optopt = '?'; 157 1.1 christos 158 1.1 christos /* Describe how to deal with options that follow non-option ARGV-elements. 159 1.1 christos 160 1.1 christos If the caller did not specify anything, 161 1.1 christos the default is REQUIRE_ORDER if the environment variable 162 1.1 christos POSIXLY_CORRECT is defined, PERMUTE otherwise. 163 1.1 christos 164 1.1 christos REQUIRE_ORDER means don't recognize them as options; 165 1.1 christos stop option processing when the first non-option is seen. 166 1.1 christos This is what Unix does. 167 1.1 christos This mode of operation is selected by either setting the environment 168 1.1 christos variable POSIXLY_CORRECT, or using `+' as the first character 169 1.1 christos of the list of option characters. 170 1.1 christos 171 1.1 christos PERMUTE is the default. We permute the contents of ARGV as we scan, 172 1.1 christos so that eventually all the non-options are at the end. This allows options 173 1.1 christos to be given in any order, even with programs that were not written to 174 1.1 christos expect this. 175 1.1 christos 176 1.1 christos RETURN_IN_ORDER is an option available to programs that were written 177 1.1 christos to expect options and other ARGV-elements in any order and that care about 178 1.1 christos the ordering of the two. We describe each non-option ARGV-element 179 1.1 christos as if it were the argument of an option with character code 1. 180 1.1 christos Using `-' as the first character of the list of option characters 181 1.1 christos selects this mode of operation. 182 1.1 christos 183 1.1 christos The special argument `--' forces an end of option-scanning regardless 184 1.1 christos of the value of `ordering'. In the case of RETURN_IN_ORDER, only 185 1.1 christos `--' can cause `getopt' to return -1 with `optind' != ARGC. */ 186 1.1 christos 187 1.1 christos static enum 188 1.1 christos { 189 1.1 christos REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER 190 1.1 christos } ordering; 191 1.1 christos 192 1.1 christos /* Value of POSIXLY_CORRECT environment variable. */ 193 1.1 christos static char *posixly_correct; 194 1.1 christos 195 1.1 christos #ifdef __GNU_LIBRARY__ 197 1.1 christos /* We want to avoid inclusion of string.h with non-GNU libraries 198 1.1 christos because there are many ways it can cause trouble. 199 1.1 christos On some systems, it contains special magic macros that don't work 200 1.1 christos in GCC. */ 201 1.1 christos # include <string.h> 202 1.1 christos # define my_index strchr 203 1.1 christos #else 204 1.1 christos 205 1.1 christos # if HAVE_STRING_H 206 1.1 christos # include <string.h> 207 1.1 christos # else 208 1.1 christos # include <strings.h> 209 1.1 christos # endif 210 1.1 christos 211 1.1 christos /* Avoid depending on library functions or files 212 1.1 christos whose names are inconsistent. */ 213 1.1 christos 214 1.1 christos #ifndef getenv 215 1.1 christos extern char *getenv (); 216 1.1 christos #endif 217 1.1 christos 218 1.1 christos static char * 219 1.1 christos my_index (str, chr) 220 1.1 christos const char *str; 221 1.1 christos int chr; 222 1.1 christos { 223 1.1 christos while (*str) 224 1.1 christos { 225 1.1 christos if (*str == chr) 226 1.1 christos return (char *) str; 227 1.1 christos str++; 228 1.1 christos } 229 1.1 christos return 0; 230 1.1 christos } 231 1.1 christos 232 1.1 christos /* If using GCC, we can safely declare strlen this way. 233 1.1 christos If not using GCC, it is ok not to declare it. */ 234 1.1 christos #ifdef __GNUC__ 235 1.1 christos /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h. 236 1.1 christos That was relevant to code that was here before. */ 237 1.1 christos # if (!defined __STDC__ || !__STDC__) && !defined strlen 238 1.1 christos /* gcc with -traditional declares the built-in strlen to return int, 239 1.1 christos and has done so at least since version 2.4.5. -- rms. */ 240 1.1 christos extern int strlen (const char *); 241 1.1 christos # endif /* not __STDC__ */ 242 1.1 christos #endif /* __GNUC__ */ 243 1.1 christos 244 1.1 christos #endif /* not __GNU_LIBRARY__ */ 245 1.1 christos 246 1.1 christos /* Handle permutation of arguments. */ 248 1.1 christos 249 1.1 christos /* Describe the part of ARGV that contains non-options that have 250 1.1 christos been skipped. `first_nonopt' is the index in ARGV of the first of them; 251 1.1 christos `last_nonopt' is the index after the last of them. */ 252 1.1 christos 253 1.1 christos static int first_nonopt; 254 1.1 christos static int last_nonopt; 255 1.1 christos 256 1.1 christos #ifdef _LIBC 257 1.1 christos /* Bash 2.0 gives us an environment variable containing flags 258 1.1 christos indicating ARGV elements that should not be considered arguments. */ 259 1.1 christos 260 1.1 christos #ifdef USE_NONOPTION_FLAGS 261 1.1 christos /* Defined in getopt_init.c */ 262 1.1 christos extern char *__getopt_nonoption_flags; 263 1.1 christos 264 1.1 christos static int nonoption_flags_max_len; 265 1.1 christos static int nonoption_flags_len; 266 1.1 christos #endif 267 1.1 christos 268 1.1 christos static int original_argc; 269 1.1 christos static char *const *original_argv; 270 1.1 christos 271 1.1 christos /* Make sure the environment variable bash 2.0 puts in the environment 272 1.1 christos is valid for the getopt call we must make sure that the ARGV passed 273 1.1 christos to getopt is that one passed to the process. */ 274 1.1 christos static void 275 1.1 christos __attribute__ ((unused)) 276 1.1 christos store_args_and_env (int argc, char *const *argv) 277 1.1 christos { 278 1.1 christos /* XXX This is no good solution. We should rather copy the args so 279 1.1 christos that we can compare them later. But we must not use malloc(3). */ 280 1.1 christos original_argc = argc; 281 1.1 christos original_argv = argv; 282 1.1 christos } 283 1.1 christos # ifdef text_set_element 284 1.1 christos text_set_element (__libc_subinit, store_args_and_env); 285 1.1 christos # endif /* text_set_element */ 286 1.1 christos 287 1.1 christos # ifdef USE_NONOPTION_FLAGS 288 1.1 christos # define SWAP_FLAGS(ch1, ch2) \ 289 1.1 christos if (nonoption_flags_len > 0) \ 290 1.1 christos { \ 291 1.1 christos char __tmp = __getopt_nonoption_flags[ch1]; \ 292 1.1 christos __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \ 293 1.1 christos __getopt_nonoption_flags[ch2] = __tmp; \ 294 1.1 christos } 295 1.1 christos # else 296 1.1 christos # define SWAP_FLAGS(ch1, ch2) 297 1.1 christos # endif 298 1.1 christos #else /* !_LIBC */ 299 1.1 christos # define SWAP_FLAGS(ch1, ch2) 300 1.1 christos #endif /* _LIBC */ 301 1.1 christos 302 1.1 christos /* Exchange two adjacent subsequences of ARGV. 303 1.1 christos One subsequence is elements [first_nonopt,last_nonopt) 304 1.1 christos which contains all the non-options that have been skipped so far. 305 1.1 christos The other is elements [last_nonopt,optind), which contains all 306 1.1 christos the options processed since those non-options were skipped. 307 1.1 christos 308 1.1 christos `first_nonopt' and `last_nonopt' are relocated so that they describe 309 1.1 christos the new indices of the non-options in ARGV after they are moved. */ 310 1.1 christos 311 1.1 christos #if defined __STDC__ && __STDC__ 312 1.1 christos static void exchange (char **); 313 1.1 christos #endif 314 1.1 christos 315 1.1 christos static void 316 1.1 christos exchange (argv) 317 1.1 christos char **argv; 318 1.1 christos { 319 1.1 christos int bottom = first_nonopt; 320 1.1 christos int middle = last_nonopt; 321 1.1 christos int top = optind; 322 1.1 christos char *tem; 323 1.1 christos 324 1.1 christos /* Exchange the shorter segment with the far end of the longer segment. 325 1.1 christos That puts the shorter segment into the right place. 326 1.1 christos It leaves the longer segment in the right place overall, 327 1.1 christos but it consists of two parts that need to be swapped next. */ 328 1.1 christos 329 1.1 christos #if defined _LIBC && defined USE_NONOPTION_FLAGS 330 1.1 christos /* First make sure the handling of the `__getopt_nonoption_flags' 331 1.1 christos string can work normally. Our top argument must be in the range 332 1.1 christos of the string. */ 333 1.1 christos if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len) 334 1.1 christos { 335 1.1 christos /* We must extend the array. The user plays games with us and 336 1.1 christos presents new arguments. */ 337 1.1 christos char *new_str = malloc (top + 1); 338 1.1 christos if (new_str == NULL) 339 1.1 christos nonoption_flags_len = nonoption_flags_max_len = 0; 340 1.1 christos else 341 1.1 christos { 342 1.1 christos memset (__mempcpy (new_str, __getopt_nonoption_flags, 343 1.1 christos nonoption_flags_max_len), 344 1.1 christos '\0', top + 1 - nonoption_flags_max_len); 345 1.1 christos nonoption_flags_max_len = top + 1; 346 1.1 christos __getopt_nonoption_flags = new_str; 347 1.1 christos } 348 1.1 christos } 349 1.1 christos #endif 350 1.1 christos 351 1.1 christos while (top > middle && middle > bottom) 352 1.1 christos { 353 1.1 christos if (top - middle > middle - bottom) 354 1.1 christos { 355 1.1 christos /* Bottom segment is the short one. */ 356 1.1 christos int len = middle - bottom; 357 1.1 christos register int i; 358 1.1 christos 359 1.1 christos /* Swap it with the top part of the top segment. */ 360 1.1 christos for (i = 0; i < len; i++) 361 1.1 christos { 362 1.1 christos tem = argv[bottom + i]; 363 1.1 christos argv[bottom + i] = argv[top - (middle - bottom) + i]; 364 1.1 christos argv[top - (middle - bottom) + i] = tem; 365 1.1 christos SWAP_FLAGS (bottom + i, top - (middle - bottom) + i); 366 1.1 christos } 367 1.1 christos /* Exclude the moved bottom segment from further swapping. */ 368 1.1 christos top -= len; 369 1.1 christos } 370 1.1 christos else 371 1.1 christos { 372 1.1 christos /* Top segment is the short one. */ 373 1.1 christos int len = top - middle; 374 1.1 christos register int i; 375 1.1 christos 376 1.1 christos /* Swap it with the bottom part of the bottom segment. */ 377 1.1 christos for (i = 0; i < len; i++) 378 1.1 christos { 379 1.1 christos tem = argv[bottom + i]; 380 1.1 christos argv[bottom + i] = argv[middle + i]; 381 1.1 christos argv[middle + i] = tem; 382 1.1 christos SWAP_FLAGS (bottom + i, middle + i); 383 1.1 christos } 384 1.1 christos /* Exclude the moved top segment from further swapping. */ 385 1.1 christos bottom += len; 386 1.1 christos } 387 1.1 christos } 388 1.1 christos 389 1.1 christos /* Update records for the slots the non-options now occupy. */ 390 1.1 christos 391 1.1 christos first_nonopt += (optind - last_nonopt); 392 1.1 christos last_nonopt = optind; 393 1.1 christos } 394 1.1 christos 395 1.1 christos /* Initialize the internal data when the first call is made. */ 396 1.1 christos 397 1.1 christos #if defined __STDC__ && __STDC__ 398 1.1 christos static const char *_getopt_initialize (int, char *const *, const char *); 399 1.1 christos #endif 400 1.1 christos static const char * 401 1.1 christos _getopt_initialize (argc, argv, optstring) 402 1.1 christos int argc; 403 1.1 christos char *const *argv; 404 1.1 christos const char *optstring; 405 1.1 christos { 406 1.1 christos /* Start processing options with ARGV-element 1 (since ARGV-element 0 407 1.1 christos is the program name); the sequence of previously skipped 408 1.1 christos non-option ARGV-elements is empty. */ 409 1.1 christos 410 1.1 christos first_nonopt = last_nonopt = optind; 411 1.1 christos 412 1.1 christos nextchar = NULL; 413 1.1 christos 414 1.1 christos posixly_correct = getenv ("POSIXLY_CORRECT"); 415 1.1 christos 416 1.1 christos /* Determine how to handle the ordering of options and nonoptions. */ 417 1.1 christos 418 1.1 christos if (optstring[0] == '-') 419 1.1 christos { 420 1.1 christos ordering = RETURN_IN_ORDER; 421 1.1 christos ++optstring; 422 1.1 christos } 423 1.1 christos else if (optstring[0] == '+') 424 1.1 christos { 425 1.1 christos ordering = REQUIRE_ORDER; 426 1.1 christos ++optstring; 427 1.1 christos } 428 1.1 christos else if (posixly_correct != NULL) 429 1.1 christos ordering = REQUIRE_ORDER; 430 1.1 christos else 431 1.1 christos ordering = PERMUTE; 432 1.1 christos 433 1.1 christos #if defined _LIBC && defined USE_NONOPTION_FLAGS 434 1.1 christos if (posixly_correct == NULL 435 1.1 christos && argc == original_argc && argv == original_argv) 436 1.1 christos { 437 1.1 christos if (nonoption_flags_max_len == 0) 438 1.1 christos { 439 1.1 christos if (__getopt_nonoption_flags == NULL 440 1.1 christos || __getopt_nonoption_flags[0] == '\0') 441 1.1 christos nonoption_flags_max_len = -1; 442 1.1 christos else 443 1.1 christos { 444 1.1 christos const char *orig_str = __getopt_nonoption_flags; 445 1.1 christos int len = nonoption_flags_max_len = strlen (orig_str); 446 1.1 christos if (nonoption_flags_max_len < argc) 447 1.1 christos nonoption_flags_max_len = argc; 448 1.1 christos __getopt_nonoption_flags = 449 1.1 christos (char *) malloc (nonoption_flags_max_len); 450 1.1 christos if (__getopt_nonoption_flags == NULL) 451 1.1 christos nonoption_flags_max_len = -1; 452 1.1 christos else 453 1.1 christos memset (__mempcpy (__getopt_nonoption_flags, orig_str, len), 454 1.1 christos '\0', nonoption_flags_max_len - len); 455 1.1 christos } 456 1.1 christos } 457 1.1 christos nonoption_flags_len = nonoption_flags_max_len; 458 1.1 christos } 459 1.1 christos else 460 1.1 christos nonoption_flags_len = 0; 461 1.1 christos #endif 462 1.1 christos 463 1.1 christos return optstring; 464 1.1 christos } 465 1.1 christos 466 1.1 christos /* Scan elements of ARGV (whose length is ARGC) for option characters 468 1.1 christos given in OPTSTRING. 469 1.1 christos 470 1.1 christos If an element of ARGV starts with '-', and is not exactly "-" or "--", 471 1.1 christos then it is an option element. The characters of this element 472 1.1 christos (aside from the initial '-') are option characters. If `getopt' 473 1.1 christos is called repeatedly, it returns successively each of the option characters 474 1.1 christos from each of the option elements. 475 1.1 christos 476 1.1 christos If `getopt' finds another option character, it returns that character, 477 1.1 christos updating `optind' and `nextchar' so that the next call to `getopt' can 478 1.1 christos resume the scan with the following option character or ARGV-element. 479 1.1 christos 480 1.1 christos If there are no more option characters, `getopt' returns -1. 481 1.1 christos Then `optind' is the index in ARGV of the first ARGV-element 482 1.1 christos that is not an option. (The ARGV-elements have been permuted 483 1.1 christos so that those that are not options now come last.) 484 1.1 christos 485 1.1 christos OPTSTRING is a string containing the legitimate option characters. 486 1.1 christos If an option character is seen that is not listed in OPTSTRING, 487 1.1 christos return '?' after printing an error message. If you set `opterr' to 488 1.1 christos zero, the error message is suppressed but we still return '?'. 489 1.1 christos 490 1.1 christos If a char in OPTSTRING is followed by a colon, that means it wants an arg, 491 1.1 christos so the following text in the same ARGV-element, or the text of the following 492 1.1 christos ARGV-element, is returned in `optarg'. Two colons mean an option that 493 1.1 christos wants an optional arg; if there is text in the current ARGV-element, 494 1.1 christos it is returned in `optarg', otherwise `optarg' is set to zero. 495 1.1 christos 496 1.1 christos If OPTSTRING starts with `-' or `+', it requests different methods of 497 1.1 christos handling the non-option ARGV-elements. 498 1.1 christos See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above. 499 1.1 christos 500 1.1 christos Long-named options begin with `--' instead of `-'. 501 1.1 christos Their names may be abbreviated as long as the abbreviation is unique 502 1.1 christos or is an exact match for some defined option. If they have an 503 1.1 christos argument, it follows the option name in the same ARGV-element, separated 504 1.1 christos from the option name by a `=', or else the in next ARGV-element. 505 1.1 christos When `getopt' finds a long-named option, it returns 0 if that option's 506 1.1 christos `flag' field is nonzero, the value of the option's `val' field 507 1.1 christos if the `flag' field is zero. 508 1.1 christos 509 1.1 christos The elements of ARGV aren't really const, because we permute them. 510 1.1 christos But we pretend they're const in the prototype to be compatible 511 1.1 christos with other systems. 512 1.1 christos 513 1.1 christos LONGOPTS is a vector of `struct option' terminated by an 514 1.1 christos element containing a name which is zero. 515 1.1 christos 516 1.1 christos LONGIND returns the index in LONGOPT of the long-named option found. 517 1.1 christos It is only valid when a long-named option has been found by the most 518 1.1 christos recent call. 519 1.1 christos 520 1.1 christos If LONG_ONLY is nonzero, '-' as well as '--' can introduce 521 1.1 christos long-named options. */ 522 1.1 christos 523 1.1 christos int 524 1.1 christos _getopt_internal (argc, argv, optstring, longopts, longind, long_only) 525 1.1 christos int argc; 526 1.1 christos char *const *argv; 527 1.1 christos const char *optstring; 528 1.1 christos const struct option *longopts; 529 1.1 christos int *longind; 530 1.1 christos int long_only; 531 1.1 christos { 532 1.1 christos int print_errors = opterr; 533 1.1 christos if (optstring[0] == ':') 534 1.1 christos print_errors = 0; 535 1.1 christos 536 1.1 christos if (argc < 1) 537 1.1 christos return -1; 538 1.1 christos 539 1.1 christos optarg = NULL; 540 1.1 christos 541 1.1 christos if (optind == 0 || !__getopt_initialized) 542 1.1 christos { 543 1.1 christos if (optind == 0) 544 1.1 christos optind = 1; /* Don't scan ARGV[0], the program name. */ 545 1.1 christos optstring = _getopt_initialize (argc, argv, optstring); 546 1.1 christos __getopt_initialized = 1; 547 1.1 christos } 548 1.1 christos 549 1.1 christos /* Test whether ARGV[optind] points to a non-option argument. 550 1.1 christos Either it does not have option syntax, or there is an environment flag 551 1.1 christos from the shell indicating it is not an option. The later information 552 1.1 christos is only used when the used in the GNU libc. */ 553 1.1 christos #if defined _LIBC && defined USE_NONOPTION_FLAGS 554 1.1 christos # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \ 555 1.1 christos || (optind < nonoption_flags_len \ 556 1.1 christos && __getopt_nonoption_flags[optind] == '1')) 557 1.1 christos #else 558 1.1 christos # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0') 559 1.1 christos #endif 560 1.1 christos 561 1.1 christos if (nextchar == NULL || *nextchar == '\0') 562 1.1 christos { 563 1.1 christos /* Advance to the next ARGV-element. */ 564 1.1 christos 565 1.1 christos /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been 566 1.1 christos moved back by the user (who may also have changed the arguments). */ 567 1.1 christos if (last_nonopt > optind) 568 1.1 christos last_nonopt = optind; 569 1.1 christos if (first_nonopt > optind) 570 1.1 christos first_nonopt = optind; 571 1.1 christos 572 1.1 christos if (ordering == PERMUTE) 573 1.1 christos { 574 1.1 christos /* If we have just processed some options following some non-options, 575 1.1 christos exchange them so that the options come first. */ 576 1.1 christos 577 1.1 christos if (first_nonopt != last_nonopt && last_nonopt != optind) 578 1.1 christos exchange ((char **) argv); 579 1.1 christos else if (last_nonopt != optind) 580 1.1 christos first_nonopt = optind; 581 1.1 christos 582 1.1 christos /* Skip any additional non-options 583 1.1 christos and extend the range of non-options previously skipped. */ 584 1.1 christos 585 1.1 christos while (optind < argc && NONOPTION_P) 586 1.1 christos optind++; 587 1.1 christos last_nonopt = optind; 588 1.1 christos } 589 1.1 christos 590 1.1 christos /* The special ARGV-element `--' means premature end of options. 591 1.1 christos Skip it like a null option, 592 1.1 christos then exchange with previous non-options as if it were an option, 593 1.1 christos then skip everything else like a non-option. */ 594 1.1 christos 595 1.1 christos if (optind != argc && !strcmp (argv[optind], "--")) 596 1.1 christos { 597 1.1 christos optind++; 598 1.1 christos 599 1.1 christos if (first_nonopt != last_nonopt && last_nonopt != optind) 600 1.1 christos exchange ((char **) argv); 601 1.1 christos else if (first_nonopt == last_nonopt) 602 1.1 christos first_nonopt = optind; 603 1.1 christos last_nonopt = argc; 604 1.1 christos 605 1.1 christos optind = argc; 606 1.1 christos } 607 1.1 christos 608 1.1 christos /* If we have done all the ARGV-elements, stop the scan 609 1.1 christos and back over any non-options that we skipped and permuted. */ 610 1.1 christos 611 1.1 christos if (optind == argc) 612 1.1 christos { 613 1.1 christos /* Set the next-arg-index to point at the non-options 614 1.1 christos that we previously skipped, so the caller will digest them. */ 615 1.1 christos if (first_nonopt != last_nonopt) 616 1.1 christos optind = first_nonopt; 617 1.1 christos return -1; 618 1.1 christos } 619 1.1 christos 620 1.1 christos /* If we have come to a non-option and did not permute it, 621 1.1 christos either stop the scan or describe it to the caller and pass it by. */ 622 1.1 christos 623 1.1 christos if (NONOPTION_P) 624 1.1 christos { 625 1.1 christos if (ordering == REQUIRE_ORDER) 626 1.1 christos return -1; 627 1.1 christos optarg = argv[optind++]; 628 1.1 christos return 1; 629 1.1 christos } 630 1.1 christos 631 1.1 christos /* We have found another option-ARGV-element. 632 1.1 christos Skip the initial punctuation. */ 633 1.1 christos 634 1.1 christos nextchar = (argv[optind] + 1 635 1.1 christos + (longopts != NULL && argv[optind][1] == '-')); 636 1.1 christos } 637 1.1 christos 638 1.1 christos /* Decode the current option-ARGV-element. */ 639 1.1 christos 640 1.1 christos /* Check whether the ARGV-element is a long option. 641 1.1 christos 642 1.1 christos If long_only and the ARGV-element has the form "-f", where f is 643 1.1 christos a valid short option, don't consider it an abbreviated form of 644 1.1 christos a long option that starts with f. Otherwise there would be no 645 1.1 christos way to give the -f short option. 646 1.1 christos 647 1.1 christos On the other hand, if there's a long option "fubar" and 648 1.1 christos the ARGV-element is "-fu", do consider that an abbreviation of 649 1.1 christos the long option, just like "--fu", and not "-f" with arg "u". 650 1.1 christos 651 1.1 christos This distinction seems to be the most useful approach. */ 652 1.1 christos 653 1.1 christos if (longopts != NULL 654 1.1 christos && (argv[optind][1] == '-' 655 1.1 christos || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1]))))) 656 1.1 christos { 657 1.1 christos char *nameend; 658 1.1 christos const struct option *p; 659 1.1 christos const struct option *pfound = NULL; 660 1.1 christos int exact = 0; 661 1.1 christos int ambig = 0; 662 1.1 christos int indfound = -1; 663 1.1 christos int option_index; 664 1.1 christos 665 1.1 christos for (nameend = nextchar; *nameend && *nameend != '='; nameend++) 666 1.1 christos /* Do nothing. */ ; 667 1.1 christos 668 1.1 christos /* Test all long options for either exact match 669 1.1 christos or abbreviated matches. */ 670 1.1 christos for (p = longopts, option_index = 0; p->name; p++, option_index++) 671 1.1 christos if (!strncmp (p->name, nextchar, nameend - nextchar)) 672 1.1 christos { 673 1.1 christos if ((unsigned int) (nameend - nextchar) 674 1.1 christos == (unsigned int) strlen (p->name)) 675 1.1 christos { 676 1.1 christos /* Exact match found. */ 677 1.1 christos pfound = p; 678 1.1 christos indfound = option_index; 679 1.1 christos exact = 1; 680 1.1 christos break; 681 1.1 christos } 682 1.1 christos else if (pfound == NULL) 683 1.1 christos { 684 1.1 christos /* First nonexact match found. */ 685 1.1 christos pfound = p; 686 1.1 christos indfound = option_index; 687 1.1 christos } 688 1.1 christos else if (long_only 689 1.1 christos || pfound->has_arg != p->has_arg 690 1.1 christos || pfound->flag != p->flag 691 1.1 christos || pfound->val != p->val) 692 1.1 christos /* Second or later nonexact match found. */ 693 1.1 christos ambig = 1; 694 1.1 christos } 695 1.1 christos 696 1.1 christos if (ambig && !exact) 697 1.1 christos { 698 1.1 christos if (print_errors) 699 1.1 christos fprintf (stderr, _("%s: option `%s' is ambiguous\n"), 700 1.1 christos argv[0], argv[optind]); 701 1.1 christos nextchar += strlen (nextchar); 702 1.1 christos optind++; 703 1.1 christos optopt = 0; 704 1.1 christos return '?'; 705 1.1 christos } 706 1.1 christos 707 1.1 christos if (pfound != NULL) 708 1.1 christos { 709 1.1 christos option_index = indfound; 710 1.1 christos optind++; 711 1.1 christos if (*nameend) 712 1.1 christos { 713 1.1 christos /* Don't test has_arg with >, because some C compilers don't 714 1.1 christos allow it to be used on enums. */ 715 1.1 christos if (pfound->has_arg) 716 1.1 christos optarg = nameend + 1; 717 1.1 christos else 718 1.1 christos { 719 1.1 christos if (print_errors) 720 1.1 christos { 721 1.1 christos if (argv[optind - 1][1] == '-') 722 1.1 christos /* --option */ 723 1.1 christos fprintf (stderr, 724 1.1 christos _("%s: option `--%s' doesn't allow an argument\n"), 725 1.1 christos argv[0], pfound->name); 726 1.1 christos else 727 1.1 christos /* +option or -option */ 728 1.1 christos fprintf (stderr, 729 1.1 christos _("%s: option `%c%s' doesn't allow an argument\n"), 730 1.1 christos argv[0], argv[optind - 1][0], pfound->name); 731 1.1 christos } 732 1.1 christos 733 1.1 christos nextchar += strlen (nextchar); 734 1.1 christos 735 1.1 christos optopt = pfound->val; 736 1.1 christos return '?'; 737 1.1 christos } 738 1.1 christos } 739 1.1 christos else if (pfound->has_arg == 1) 740 1.1 christos { 741 1.1 christos if (optind < argc) 742 1.1 christos optarg = argv[optind++]; 743 1.1 christos else 744 1.1 christos { 745 1.1 christos if (print_errors) 746 1.1 christos fprintf (stderr, 747 1.1 christos _("%s: option `%s' requires an argument\n"), 748 1.1 christos argv[0], argv[optind - 1]); 749 1.1 christos nextchar += strlen (nextchar); 750 1.1 christos optopt = pfound->val; 751 1.1 christos return optstring[0] == ':' ? ':' : '?'; 752 1.1 christos } 753 1.1 christos } 754 1.1 christos nextchar += strlen (nextchar); 755 1.1 christos if (longind != NULL) 756 1.1 christos *longind = option_index; 757 1.1 christos if (pfound->flag) 758 1.1 christos { 759 1.1 christos *(pfound->flag) = pfound->val; 760 1.1 christos return 0; 761 1.1 christos } 762 1.1 christos return pfound->val; 763 1.1 christos } 764 1.1 christos 765 1.1 christos /* Can't find it as a long option. If this is not getopt_long_only, 766 1.1 christos or the option starts with '--' or is not a valid short 767 1.1 christos option, then it's an error. 768 1.1 christos Otherwise interpret it as a short option. */ 769 1.1 christos if (!long_only || argv[optind][1] == '-' 770 1.1 christos || my_index (optstring, *nextchar) == NULL) 771 1.1 christos { 772 1.1 christos if (print_errors) 773 1.1 christos { 774 1.1 christos if (argv[optind][1] == '-') 775 1.1 christos /* --option */ 776 1.1 christos fprintf (stderr, _("%s: unrecognized option `--%s'\n"), 777 1.1 christos argv[0], nextchar); 778 1.1 christos else 779 1.1 christos /* +option or -option */ 780 1.1 christos fprintf (stderr, _("%s: unrecognized option `%c%s'\n"), 781 1.1 christos argv[0], argv[optind][0], nextchar); 782 1.1 christos } 783 1.1 christos nextchar = (char *) ""; 784 1.1 christos optind++; 785 1.1 christos optopt = 0; 786 1.1 christos return '?'; 787 1.1 christos } 788 1.1 christos } 789 1.1 christos 790 1.1 christos /* Look at and handle the next short option-character. */ 791 1.1 christos 792 1.1 christos { 793 1.1 christos char c = *nextchar++; 794 1.1 christos char *temp = my_index (optstring, c); 795 1.1 christos 796 1.1 christos /* Increment `optind' when we start to process its last character. */ 797 1.1 christos if (*nextchar == '\0') 798 1.1 christos ++optind; 799 1.1 christos 800 1.1 christos if (temp == NULL || c == ':') 801 1.1 christos { 802 1.1 christos if (print_errors) 803 1.1 christos { 804 1.1 christos if (posixly_correct) 805 1.1 christos /* 1003.2 specifies the format of this message. */ 806 1.1 christos fprintf (stderr, _("%s: illegal option -- %c\n"), 807 1.1 christos argv[0], c); 808 1.1 christos else 809 1.1 christos fprintf (stderr, _("%s: invalid option -- %c\n"), 810 1.1 christos argv[0], c); 811 1.1 christos } 812 1.1 christos optopt = c; 813 1.1 christos return '?'; 814 1.1 christos } 815 1.1 christos /* Convenience. Treat POSIX -W foo same as long option --foo */ 816 1.1 christos if (temp[0] == 'W' && temp[1] == ';') 817 1.1 christos { 818 1.1 christos char *nameend; 819 1.1 christos const struct option *p; 820 1.1 christos const struct option *pfound = NULL; 821 1.1 christos int exact = 0; 822 1.1 christos int ambig = 0; 823 1.1 christos int indfound = 0; 824 1.1 christos int option_index; 825 1.1 christos 826 1.1 christos /* This is an option that requires an argument. */ 827 1.1 christos if (*nextchar != '\0') 828 1.1 christos { 829 1.1 christos optarg = nextchar; 830 1.1 christos /* If we end this ARGV-element by taking the rest as an arg, 831 1.1 christos we must advance to the next element now. */ 832 1.1 christos optind++; 833 1.1 christos } 834 1.1 christos else if (optind == argc) 835 1.1 christos { 836 1.1 christos if (print_errors) 837 1.1 christos { 838 1.1 christos /* 1003.2 specifies the format of this message. */ 839 1.1 christos fprintf (stderr, _("%s: option requires an argument -- %c\n"), 840 1.1 christos argv[0], c); 841 1.1 christos } 842 1.1 christos optopt = c; 843 1.1 christos if (optstring[0] == ':') 844 1.1 christos c = ':'; 845 1.1 christos else 846 1.1 christos c = '?'; 847 1.1 christos return c; 848 1.1 christos } 849 1.1 christos else 850 1.1 christos /* We already incremented `optind' once; 851 1.1 christos increment it again when taking next ARGV-elt as argument. */ 852 1.1 christos optarg = argv[optind++]; 853 1.1 christos 854 1.1 christos /* optarg is now the argument, see if it's in the 855 1.1 christos table of longopts. */ 856 1.1 christos 857 1.1 christos for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++) 858 1.1 christos /* Do nothing. */ ; 859 1.1 christos 860 1.1 christos /* Test all long options for either exact match 861 1.1 christos or abbreviated matches. */ 862 1.1 christos for (p = longopts, option_index = 0; p->name; p++, option_index++) 863 1.1 christos if (!strncmp (p->name, nextchar, nameend - nextchar)) 864 1.1 christos { 865 1.1 christos if ((unsigned int) (nameend - nextchar) == strlen (p->name)) 866 1.1 christos { 867 1.1 christos /* Exact match found. */ 868 1.1 christos pfound = p; 869 1.1 christos indfound = option_index; 870 1.1 christos exact = 1; 871 1.1 christos break; 872 1.1 christos } 873 1.1 christos else if (pfound == NULL) 874 1.1 christos { 875 1.1 christos /* First nonexact match found. */ 876 1.1 christos pfound = p; 877 1.1 christos indfound = option_index; 878 1.1 christos } 879 1.1 christos else 880 1.1 christos /* Second or later nonexact match found. */ 881 1.1 christos ambig = 1; 882 1.1 christos } 883 1.1 christos if (ambig && !exact) 884 1.1 christos { 885 1.1 christos if (print_errors) 886 1.1 christos fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"), 887 1.1 christos argv[0], argv[optind]); 888 1.1 christos nextchar += strlen (nextchar); 889 1.1 christos optind++; 890 1.1 christos return '?'; 891 1.1 christos } 892 1.1 christos if (pfound != NULL) 893 1.1 christos { 894 1.1 christos option_index = indfound; 895 1.1 christos if (*nameend) 896 1.1 christos { 897 1.1 christos /* Don't test has_arg with >, because some C compilers don't 898 1.1 christos allow it to be used on enums. */ 899 1.1 christos if (pfound->has_arg) 900 1.1 christos optarg = nameend + 1; 901 1.1 christos else 902 1.1 christos { 903 1.1 christos if (print_errors) 904 1.1 christos fprintf (stderr, _("\ 905 1.1 christos %s: option `-W %s' doesn't allow an argument\n"), 906 1.1 christos argv[0], pfound->name); 907 1.1 christos 908 1.1 christos nextchar += strlen (nextchar); 909 1.1 christos return '?'; 910 1.1 christos } 911 1.1 christos } 912 1.1 christos else if (pfound->has_arg == 1) 913 1.1 christos { 914 1.1 christos if (optind < argc) 915 1.1 christos optarg = argv[optind++]; 916 1.1 christos else 917 1.1 christos { 918 1.1 christos if (print_errors) 919 1.1 christos fprintf (stderr, 920 1.1 christos _("%s: option `%s' requires an argument\n"), 921 1.1 christos argv[0], argv[optind - 1]); 922 1.1 christos nextchar += strlen (nextchar); 923 1.1 christos return optstring[0] == ':' ? ':' : '?'; 924 1.1 christos } 925 1.1 christos } 926 1.1 christos nextchar += strlen (nextchar); 927 1.1 christos if (longind != NULL) 928 1.1 christos *longind = option_index; 929 1.1 christos if (pfound->flag) 930 1.1 christos { 931 1.1 christos *(pfound->flag) = pfound->val; 932 1.1 christos return 0; 933 1.1 christos } 934 1.1 christos return pfound->val; 935 1.1 christos } 936 1.1 christos nextchar = NULL; 937 1.1 christos return 'W'; /* Let the application handle it. */ 938 1.1 christos } 939 1.1 christos if (temp[1] == ':') 940 1.1 christos { 941 1.1 christos if (temp[2] == ':') 942 1.1 christos { 943 1.1 christos /* This is an option that accepts an argument optionally. */ 944 1.1 christos if (*nextchar != '\0') 945 1.1 christos { 946 1.1 christos optarg = nextchar; 947 1.1 christos optind++; 948 1.1 christos } 949 1.1 christos else 950 1.1 christos optarg = NULL; 951 1.1 christos nextchar = NULL; 952 1.1 christos } 953 1.1 christos else 954 1.1 christos { 955 1.1 christos /* This is an option that requires an argument. */ 956 1.1 christos if (*nextchar != '\0') 957 1.1 christos { 958 1.1 christos optarg = nextchar; 959 1.1 christos /* If we end this ARGV-element by taking the rest as an arg, 960 1.1 christos we must advance to the next element now. */ 961 1.1 christos optind++; 962 1.1 christos } 963 1.1 christos else if (optind == argc) 964 1.1 christos { 965 1.1 christos if (print_errors) 966 1.1 christos { 967 1.1 christos /* 1003.2 specifies the format of this message. */ 968 1.1 christos fprintf (stderr, 969 1.1 christos _("%s: option requires an argument -- %c\n"), 970 1.1 christos argv[0], c); 971 1.1 christos } 972 1.1 christos optopt = c; 973 1.1 christos if (optstring[0] == ':') 974 1.1 christos c = ':'; 975 1.1 christos else 976 1.1 christos c = '?'; 977 1.1 christos } 978 1.1 christos else 979 1.1 christos /* We already incremented `optind' once; 980 1.1 christos increment it again when taking next ARGV-elt as argument. */ 981 1.1 christos optarg = argv[optind++]; 982 1.1 christos nextchar = NULL; 983 1.1 christos } 984 1.1 christos } 985 1.1 christos return c; 986 1.1 christos } 987 1.1 christos } 988 1.1 christos 989 1.1 christos int 990 1.1 christos getopt (argc, argv, optstring) 991 1.1 christos int argc; 992 1.1 christos char *const *argv; 993 1.1 christos const char *optstring; 994 1.1 christos { 995 1.1 christos return _getopt_internal (argc, argv, optstring, 996 1.1 christos (const struct option *) 0, 997 1.1 christos (int *) 0, 998 1.1 christos 0); 999 1.1 christos } 1000 1.1 christos 1001 1.1 christos #endif /* Not ELIDE_CODE. */ 1002 1.1 christos 1003 1.1 christos #ifdef TEST 1005 1.1 christos 1006 1.1 christos /* Compile with -DTEST to make an executable for use in testing 1007 1.1 christos the above definition of `getopt'. */ 1008 1.1 christos 1009 1.1 christos int 1010 1.1 christos main (argc, argv) 1011 1.1 christos int argc; 1012 1.1 christos char **argv; 1013 1.1 christos { 1014 1.1 christos int c; 1015 1.1 christos int digit_optind = 0; 1016 1.1 christos 1017 1.1 christos while (1) 1018 1.1 christos { 1019 1.1 christos int this_option_optind = optind ? optind : 1; 1020 1.1 christos 1021 1.1 christos c = getopt (argc, argv, "abc:d:0123456789"); 1022 1.1 christos if (c == -1) 1023 1.1 christos break; 1024 1.1 christos 1025 1.1 christos switch (c) 1026 1.1 christos { 1027 1.1 christos case '0': 1028 1.1 christos case '1': 1029 1.1 christos case '2': 1030 1.1 christos case '3': 1031 1.1 christos case '4': 1032 1.1 christos case '5': 1033 1.1 christos case '6': 1034 1.1 christos case '7': 1035 1.1 christos case '8': 1036 1.1 christos case '9': 1037 1.1 christos if (digit_optind != 0 && digit_optind != this_option_optind) 1038 1.1 christos printf ("digits occur in two different argv-elements.\n"); 1039 1.1 christos digit_optind = this_option_optind; 1040 1.1 christos printf ("option %c\n", c); 1041 1.1 christos break; 1042 1.1 christos 1043 1.1 christos case 'a': 1044 1.1 christos printf ("option a\n"); 1045 1.1 christos break; 1046 1.1 christos 1047 1.1 christos case 'b': 1048 1.1 christos printf ("option b\n"); 1049 1.1 christos break; 1050 1.1 christos 1051 1.1 christos case 'c': 1052 1.1 christos printf ("option c with value `%s'\n", optarg); 1053 1.1 christos break; 1054 1.1 christos 1055 1.1 christos case '?': 1056 1.1 christos break; 1057 1.1 christos 1058 1.1 christos default: 1059 1.1 christos printf ("?? getopt returned character code 0%o ??\n", c); 1060 1.1 christos } 1061 1.1 christos } 1062 1.1 christos 1063 1.1 christos if (optind < argc) 1064 1.1 christos { 1065 1.1 christos printf ("non-option ARGV-elements: "); 1066 1.1 christos while (optind < argc) 1067 1.1 christos printf ("%s ", argv[optind++]); 1068 1.1 christos printf ("\n"); 1069 1.1 christos } 1070 1071 exit (0); 1072 } 1073 1074 #endif /* TEST */ 1075