1 | /* Reentrant version of getopt_long and getopt_long_only.
|
---|
2 |
|
---|
3 | Based on ../getopt*.*:
|
---|
4 |
|
---|
5 | getopt_long and getopt_long_only entry points for GNU getopt.
|
---|
6 | Copyright (C) 1987-1994, 1996-2016 Free Software Foundation, Inc.
|
---|
7 |
|
---|
8 | NOTE: The canonical source of this file is maintained with the GNU C Library.
|
---|
9 | Bugs can be reported to [email protected].
|
---|
10 |
|
---|
11 | GNU Make is free software; you can redistribute it and/or modify it under the
|
---|
12 | terms of the GNU General Public License as published by the Free Software
|
---|
13 | Foundation; either version 3 of the License, or (at your option) any later
|
---|
14 | version.
|
---|
15 |
|
---|
16 | GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
---|
17 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
---|
18 | A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
---|
19 |
|
---|
20 | You should have received a copy of the GNU General Public License along with
|
---|
21 | this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
22 |
|
---|
23 | Modifications:
|
---|
24 | Copyright (c) 2018 knut st. osmundsen <[email protected]>
|
---|
25 | */
|
---|
26 | |
---|
27 |
|
---|
28 | #define FAKES_NO_GETOPT_H /* bird */
|
---|
29 | #ifdef HAVE_CONFIG_H
|
---|
30 | #include <config.h>
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | #include "getopt_r.h"
|
---|
34 |
|
---|
35 | #if !defined __STDC__ || !__STDC__
|
---|
36 | /* This is a separate conditional since some stdc systems
|
---|
37 | reject `defined (const)'. */
|
---|
38 | #ifndef const
|
---|
39 | #define const
|
---|
40 | #endif
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | #include <stdio.h>
|
---|
44 |
|
---|
45 | #if 0
|
---|
46 | /* Comment out all this code if we are using the GNU C Library, and are not
|
---|
47 | actually compiling the library itself. This code is part of the GNU C
|
---|
48 | Library, but also included in many other GNU distributions. Compiling
|
---|
49 | and linking in this code is a waste when using the GNU C library
|
---|
50 | (especially if it is a shared library). Rather than having every GNU
|
---|
51 | program understand `configure --with-gnu-libc' and omit the object files,
|
---|
52 | it is simpler to just do this in the source for each such file. */
|
---|
53 |
|
---|
54 | #define GETOPT_INTERFACE_VERSION 2
|
---|
55 | #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
|
---|
56 | #include <gnu-versions.h>
|
---|
57 | #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
|
---|
58 | #define ELIDE_CODE
|
---|
59 | #endif
|
---|
60 | #endif
|
---|
61 | #endif
|
---|
62 |
|
---|
63 | #if 1 //ndef ELIDE_CODE
|
---|
64 |
|
---|
65 |
|
---|
66 | /* This needs to come after some library #include
|
---|
67 | to get __GNU_LIBRARY__ defined. */
|
---|
68 | #ifdef __GNU_LIBRARY__
|
---|
69 | #include <stdlib.h>
|
---|
70 | #endif
|
---|
71 |
|
---|
72 | #ifndef NULL
|
---|
73 | #define NULL 0
|
---|
74 | #endif
|
---|
75 |
|
---|
76 | int
|
---|
77 | getopt_long_r (struct getopt_state_r *gos, int *opt_index)
|
---|
78 | {
|
---|
79 | return _getopt_internal_r (gos, gos->long_options, opt_index, 0);
|
---|
80 | }
|
---|
81 |
|
---|
82 | /* Like getopt_long, but '-' as well as '--' can indicate a long option.
|
---|
83 | If an option that starts with '-' (not '--') doesn't match a long option,
|
---|
84 | but does match a short option, it is parsed as a short option
|
---|
85 | instead. */
|
---|
86 |
|
---|
87 | int
|
---|
88 | getopt_long_only_r (struct getopt_state_r *gos, int *opt_index)
|
---|
89 | {
|
---|
90 | return _getopt_internal_r (gos, gos->long_options, opt_index, 0);
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | #endif /* #if 1 */ /* Not ELIDE_CODE. */
|
---|
95 | |
---|
96 |
|
---|
97 | #ifdef TEST
|
---|
98 |
|
---|
99 | #include <stdio.h>
|
---|
100 |
|
---|
101 | int
|
---|
102 | main (int argc, char **argv)
|
---|
103 | {
|
---|
104 | int c;
|
---|
105 | int digit_optind = 0;
|
---|
106 |
|
---|
107 | while (1)
|
---|
108 | {
|
---|
109 | int this_option_optind = optind ? optind : 1;
|
---|
110 | int option_index = 0;
|
---|
111 | struct getopt_state_r gos;
|
---|
112 | static struct option long_options[] =
|
---|
113 | {
|
---|
114 | {"add", 1, 0, 0},
|
---|
115 | {"append", 0, 0, 0},
|
---|
116 | {"delete", 1, 0, 0},
|
---|
117 | {"verbose", 0, 0, 0},
|
---|
118 | {"create", 0, 0, 0},
|
---|
119 | {"file", 1, 0, 0},
|
---|
120 | {0, 0, 0, 0}
|
---|
121 | };
|
---|
122 |
|
---|
123 | getopt_initialize_r (&gos, argc, argv, "abc:d:0123456789", long_options, NULL, NULL);
|
---|
124 | c = getopt_long_r (&geo, &option_index);
|
---|
125 | if (c == -1)
|
---|
126 | break;
|
---|
127 |
|
---|
128 | switch (c)
|
---|
129 | {
|
---|
130 | case 0:
|
---|
131 | printf ("option %s", long_options[option_index].name);
|
---|
132 | if (geo.optarg)
|
---|
133 | printf (" with arg %s", geo.optarg);
|
---|
134 | printf ("\n");
|
---|
135 | break;
|
---|
136 |
|
---|
137 | case '0':
|
---|
138 | case '1':
|
---|
139 | case '2':
|
---|
140 | case '3':
|
---|
141 | case '4':
|
---|
142 | case '5':
|
---|
143 | case '6':
|
---|
144 | case '7':
|
---|
145 | case '8':
|
---|
146 | case '9':
|
---|
147 | if (digit_optind != 0 && digit_optind != this_option_optind)
|
---|
148 | printf ("digits occur in two different argv-elements.\n");
|
---|
149 | digit_optind = this_option_optind;
|
---|
150 | printf ("option %c\n", c);
|
---|
151 | break;
|
---|
152 |
|
---|
153 | case 'a':
|
---|
154 | printf ("option a\n");
|
---|
155 | break;
|
---|
156 |
|
---|
157 | case 'b':
|
---|
158 | printf ("option b\n");
|
---|
159 | break;
|
---|
160 |
|
---|
161 | case 'c':
|
---|
162 | printf ("option c with value '%s'\n", geo.optarg);
|
---|
163 | break;
|
---|
164 |
|
---|
165 | case 'd':
|
---|
166 | printf ("option d with value '%s'\n", geo.optarg);
|
---|
167 | break;
|
---|
168 |
|
---|
169 | case '?':
|
---|
170 | break;
|
---|
171 |
|
---|
172 | default:
|
---|
173 | printf ("?? getopt returned character code 0%o ??\n", c);
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | if (geo.optind < argc)
|
---|
178 | {
|
---|
179 | printf ("non-option ARGV-elements: ");
|
---|
180 | while (optind < argc)
|
---|
181 | printf ("%s ", argv[geo.optind++]);
|
---|
182 | printf ("\n");
|
---|
183 | }
|
---|
184 |
|
---|
185 | exit (0);
|
---|
186 | }
|
---|
187 |
|
---|
188 | #endif /* TEST */
|
---|