| 1 | /* getopt_long and getopt_long_only entry points for GNU getopt.
|
|---|
| 2 | Copyright (C) 1987-1994, 1996-1998, 2004, 2006, 2009-2012 Free Software
|
|---|
| 3 | Foundation, Inc.
|
|---|
| 4 | This file is part of the GNU C Library.
|
|---|
| 5 |
|
|---|
| 6 | This program is free software: you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 3 of the License, or
|
|---|
| 9 | (at your option) any later version.
|
|---|
| 10 |
|
|---|
| 11 | This program is distributed in the hope that it will be useful,
|
|---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 14 | GNU General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|---|
| 18 | |
|---|
| 19 |
|
|---|
| 20 | #ifdef _LIBC
|
|---|
| 21 | # include <getopt.h>
|
|---|
| 22 | #else
|
|---|
| 23 | # include <config.h>
|
|---|
| 24 | # include "getopt.h"
|
|---|
| 25 | #endif
|
|---|
| 26 | #include "getopt_int.h"
|
|---|
| 27 |
|
|---|
| 28 | #include <stdio.h>
|
|---|
| 29 |
|
|---|
| 30 | /* This needs to come after some library #include
|
|---|
| 31 | to get __GNU_LIBRARY__ defined. */
|
|---|
| 32 | #ifdef __GNU_LIBRARY__
|
|---|
| 33 | #include <stdlib.h>
|
|---|
| 34 | #endif
|
|---|
| 35 |
|
|---|
| 36 | #ifndef NULL
|
|---|
| 37 | #define NULL 0
|
|---|
| 38 | #endif
|
|---|
| 39 |
|
|---|
| 40 | int
|
|---|
| 41 | getopt_long (int argc, char *__getopt_argv_const *argv, const char *options,
|
|---|
| 42 | const struct option *long_options, int *opt_index)
|
|---|
| 43 | {
|
|---|
| 44 | return _getopt_internal (argc, (char **) argv, options, long_options,
|
|---|
| 45 | opt_index, 0, 0);
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | int
|
|---|
| 49 | _getopt_long_r (int argc, char **argv, const char *options,
|
|---|
| 50 | const struct option *long_options, int *opt_index,
|
|---|
| 51 | struct _getopt_data *d)
|
|---|
| 52 | {
|
|---|
| 53 | return _getopt_internal_r (argc, argv, options, long_options, opt_index,
|
|---|
| 54 | 0, d, 0);
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | /* Like getopt_long, but '-' as well as '--' can indicate a long option.
|
|---|
| 58 | If an option that starts with '-' (not '--') doesn't match a long option,
|
|---|
| 59 | but does match a short option, it is parsed as a short option
|
|---|
| 60 | instead. */
|
|---|
| 61 |
|
|---|
| 62 | int
|
|---|
| 63 | getopt_long_only (int argc, char *__getopt_argv_const *argv,
|
|---|
| 64 | const char *options,
|
|---|
| 65 | const struct option *long_options, int *opt_index)
|
|---|
| 66 | {
|
|---|
| 67 | return _getopt_internal (argc, (char **) argv, options, long_options,
|
|---|
| 68 | opt_index, 1, 0);
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | int
|
|---|
| 72 | _getopt_long_only_r (int argc, char **argv, const char *options,
|
|---|
| 73 | const struct option *long_options, int *opt_index,
|
|---|
| 74 | struct _getopt_data *d)
|
|---|
| 75 | {
|
|---|
| 76 | return _getopt_internal_r (argc, argv, options, long_options, opt_index,
|
|---|
| 77 | 1, d, 0);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | |
|---|
| 81 |
|
|---|
| 82 | #ifdef TEST
|
|---|
| 83 |
|
|---|
| 84 | #include <stdio.h>
|
|---|
| 85 |
|
|---|
| 86 | int
|
|---|
| 87 | main (int argc, char **argv)
|
|---|
| 88 | {
|
|---|
| 89 | int c;
|
|---|
| 90 | int digit_optind = 0;
|
|---|
| 91 |
|
|---|
| 92 | while (1)
|
|---|
| 93 | {
|
|---|
| 94 | int this_option_optind = optind ? optind : 1;
|
|---|
| 95 | int option_index = 0;
|
|---|
| 96 | static const struct option long_options[] =
|
|---|
| 97 | {
|
|---|
| 98 | {"add", 1, 0, 0},
|
|---|
| 99 | {"append", 0, 0, 0},
|
|---|
| 100 | {"delete", 1, 0, 0},
|
|---|
| 101 | {"verbose", 0, 0, 0},
|
|---|
| 102 | {"create", 0, 0, 0},
|
|---|
| 103 | {"file", 1, 0, 0},
|
|---|
| 104 | {0, 0, 0, 0}
|
|---|
| 105 | };
|
|---|
| 106 |
|
|---|
| 107 | c = getopt_long (argc, argv, "abc:d:0123456789",
|
|---|
| 108 | long_options, &option_index);
|
|---|
| 109 | if (c == -1)
|
|---|
| 110 | break;
|
|---|
| 111 |
|
|---|
| 112 | switch (c)
|
|---|
| 113 | {
|
|---|
| 114 | case 0:
|
|---|
| 115 | printf ("option %s", long_options[option_index].name);
|
|---|
| 116 | if (optarg)
|
|---|
| 117 | printf (" with arg %s", optarg);
|
|---|
| 118 | printf ("\n");
|
|---|
| 119 | break;
|
|---|
| 120 |
|
|---|
| 121 | case '0':
|
|---|
| 122 | case '1':
|
|---|
| 123 | case '2':
|
|---|
| 124 | case '3':
|
|---|
| 125 | case '4':
|
|---|
| 126 | case '5':
|
|---|
| 127 | case '6':
|
|---|
| 128 | case '7':
|
|---|
| 129 | case '8':
|
|---|
| 130 | case '9':
|
|---|
| 131 | if (digit_optind != 0 && digit_optind != this_option_optind)
|
|---|
| 132 | printf ("digits occur in two different argv-elements.\n");
|
|---|
| 133 | digit_optind = this_option_optind;
|
|---|
| 134 | printf ("option %c\n", c);
|
|---|
| 135 | break;
|
|---|
| 136 |
|
|---|
| 137 | case 'a':
|
|---|
| 138 | printf ("option a\n");
|
|---|
| 139 | break;
|
|---|
| 140 |
|
|---|
| 141 | case 'b':
|
|---|
| 142 | printf ("option b\n");
|
|---|
| 143 | break;
|
|---|
| 144 |
|
|---|
| 145 | case 'c':
|
|---|
| 146 | printf ("option c with value '%s'\n", optarg);
|
|---|
| 147 | break;
|
|---|
| 148 |
|
|---|
| 149 | case 'd':
|
|---|
| 150 | printf ("option d with value '%s'\n", optarg);
|
|---|
| 151 | break;
|
|---|
| 152 |
|
|---|
| 153 | case '?':
|
|---|
| 154 | break;
|
|---|
| 155 |
|
|---|
| 156 | default:
|
|---|
| 157 | printf ("?? getopt returned character code 0%o ??\n", c);
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | if (optind < argc)
|
|---|
| 162 | {
|
|---|
| 163 | printf ("non-option ARGV-elements: ");
|
|---|
| 164 | while (optind < argc)
|
|---|
| 165 | printf ("%s ", argv[optind++]);
|
|---|
| 166 | printf ("\n");
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | exit (0);
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | #endif /* TEST */
|
|---|