rlm@1: /* getopt_long and getopt_long_only entry points for GNU getopt. rlm@1: Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98 rlm@1: Free Software Foundation, Inc. rlm@1: rlm@1: NOTE: This source is derived from an old version taken from the GNU C rlm@1: Library (glibc). rlm@1: rlm@1: This program is free software; you can redistribute it and/or modify it rlm@1: under the terms of the GNU General Public License as published by the rlm@1: Free Software Foundation; either version 2, or (at your option) any rlm@1: later version. rlm@1: rlm@1: This program is distributed in the hope that it will be useful, rlm@1: but WITHOUT ANY WARRANTY; without even the implied warranty of rlm@1: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the rlm@1: GNU General Public License for more details. rlm@1: rlm@1: You should have received a copy of the GNU General Public License rlm@1: along with this program; if not, write to the Free Software rlm@1: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, rlm@1: USA. */ rlm@1: rlm@1: #ifdef HAVE_CONFIG_H rlm@1: #include rlm@1: #endif rlm@1: rlm@1: #include "getopt.h" rlm@1: rlm@1: #if !defined __STDC__ || !__STDC__ rlm@1: /* This is a separate conditional since some stdc systems rlm@1: reject `defined (const)'. */ rlm@1: #ifndef const rlm@1: #define const rlm@1: #endif rlm@1: #endif rlm@1: rlm@1: #include rlm@1: rlm@1: /* Comment out all this code if we are using the GNU C Library, and are not rlm@1: actually compiling the library itself. This code is part of the GNU C rlm@1: Library, but also included in many other GNU distributions. Compiling rlm@1: and linking in this code is a waste when using the GNU C library rlm@1: (especially if it is a shared library). Rather than having every GNU rlm@1: program understand `configure --with-gnu-libc' and omit the object files, rlm@1: it is simpler to just do this in the source for each such file. */ rlm@1: rlm@1: #define GETOPT_INTERFACE_VERSION 2 rlm@1: #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2 rlm@1: #include rlm@1: #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION rlm@1: #define ELIDE_CODE rlm@1: #endif rlm@1: #endif rlm@1: rlm@1: #ifndef ELIDE_CODE rlm@1: rlm@1: rlm@1: /* This needs to come after some library #include rlm@1: to get __GNU_LIBRARY__ defined. */ rlm@1: #ifdef __GNU_LIBRARY__ rlm@1: #include rlm@1: #endif rlm@1: rlm@1: #ifndef NULL rlm@1: #define NULL 0 rlm@1: #endif rlm@1: rlm@1: int rlm@1: getopt_long (argc, argv, options, long_options, opt_index) rlm@1: int argc; rlm@1: char *const *argv; rlm@1: const char *options; rlm@1: const struct option *long_options; rlm@1: int *opt_index; rlm@1: { rlm@1: return _getopt_internal (argc, argv, options, long_options, opt_index, 0); rlm@1: } rlm@1: rlm@1: /* Like getopt_long, but '-' as well as '--' can indicate a long option. rlm@1: If an option that starts with '-' (not '--') doesn't match a long option, rlm@1: but does match a short option, it is parsed as a short option rlm@1: instead. */ rlm@1: rlm@1: int rlm@1: getopt_long_only (argc, argv, options, long_options, opt_index) rlm@1: int argc; rlm@1: char *const *argv; rlm@1: const char *options; rlm@1: const struct option *long_options; rlm@1: int *opt_index; rlm@1: { rlm@1: return _getopt_internal (argc, argv, options, long_options, opt_index, 1); rlm@1: } rlm@1: rlm@1: rlm@1: #endif /* Not ELIDE_CODE. */ rlm@1: rlm@1: #ifdef TEST rlm@1: rlm@1: #include rlm@1: rlm@1: int rlm@1: main (argc, argv) rlm@1: int argc; rlm@1: char **argv; rlm@1: { rlm@1: int c; rlm@1: int digit_optind = 0; rlm@1: rlm@1: while (1) rlm@1: { rlm@1: int this_option_optind = optind ? optind : 1; rlm@1: int option_index = 0; rlm@1: static struct option long_options[] = rlm@1: { rlm@1: {"add", 1, 0, 0}, rlm@1: {"append", 0, 0, 0}, rlm@1: {"delete", 1, 0, 0}, rlm@1: {"verbose", 0, 0, 0}, rlm@1: {"create", 0, 0, 0}, rlm@1: {"file", 1, 0, 0}, rlm@1: {0, 0, 0, 0} rlm@1: }; rlm@1: rlm@1: c = getopt_long (argc, argv, "abc:d:0123456789", rlm@1: long_options, &option_index); rlm@1: if (c == -1) rlm@1: break; rlm@1: rlm@1: switch (c) rlm@1: { rlm@1: case 0: rlm@1: printf ("option %s", long_options[option_index].name); rlm@1: if (optarg) rlm@1: printf (" with arg %s", optarg); rlm@1: printf ("\n"); rlm@1: break; rlm@1: rlm@1: case '0': rlm@1: case '1': rlm@1: case '2': rlm@1: case '3': rlm@1: case '4': rlm@1: case '5': rlm@1: case '6': rlm@1: case '7': rlm@1: case '8': rlm@1: case '9': rlm@1: if (digit_optind != 0 && digit_optind != this_option_optind) rlm@1: printf ("digits occur in two different argv-elements.\n"); rlm@1: digit_optind = this_option_optind; rlm@1: printf ("option %c\n", c); rlm@1: break; rlm@1: rlm@1: case 'a': rlm@1: printf ("option a\n"); rlm@1: break; rlm@1: rlm@1: case 'b': rlm@1: printf ("option b\n"); rlm@1: break; rlm@1: rlm@1: case 'c': rlm@1: printf ("option c with value `%s'\n", optarg); rlm@1: break; rlm@1: rlm@1: case 'd': rlm@1: printf ("option d with value `%s'\n", optarg); rlm@1: break; rlm@1: rlm@1: case '?': rlm@1: break; rlm@1: rlm@1: default: rlm@1: printf ("?? getopt returned character code 0%o ??\n", c); rlm@1: } rlm@1: } rlm@1: rlm@1: if (optind < argc) rlm@1: { rlm@1: printf ("non-option ARGV-elements: "); rlm@1: while (optind < argc) rlm@1: printf ("%s ", argv[optind++]); rlm@1: printf ("\n"); rlm@1: } rlm@1: rlm@1: exit (0); rlm@1: } rlm@1: rlm@1: #endif /* TEST */