]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
718e3744 | 2 | /* getopt_long and getopt_long_only entry points for GNU getopt. |
896014f4 DL |
3 | * Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98 |
4 | * Free Software Foundation, Inc. | |
5 | * | |
6 | * NOTE: The canonical source of this file is maintained with the GNU C Library. | |
7 | * Bugs can be reported to bug-glibc@gnu.org. | |
896014f4 | 8 | */ |
6b0655a2 | 9 | |
024a7f06 | 10 | #include <zebra.h> |
718e3744 | 11 | #include "getopt.h" |
12 | ||
13 | #if !defined __STDC__ || !__STDC__ | |
14 | /* This is a separate conditional since some stdc systems | |
15 | reject `defined (const)'. */ | |
16 | #ifndef const | |
17 | #define const | |
18 | #endif | |
19 | #endif | |
20 | ||
21 | #include <stdio.h> | |
22 | ||
23 | /* Comment out all this code if we are using the GNU C Library, and are not | |
24 | actually compiling the library itself. This code is part of the GNU C | |
25 | Library, but also included in many other GNU distributions. Compiling | |
26 | and linking in this code is a waste when using the GNU C library | |
27 | (especially if it is a shared library). Rather than having every GNU | |
28 | program understand `configure --with-gnu-libc' and omit the object files, | |
29 | it is simpler to just do this in the source for each such file. */ | |
30 | ||
31 | #define GETOPT_INTERFACE_VERSION 2 | |
32 | #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2 | |
33 | #include <gnu-versions.h> | |
34 | #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION | |
35 | #define ELIDE_CODE | |
36 | #endif | |
37 | #endif | |
38 | ||
39 | #ifndef ELIDE_CODE | |
40 | ||
41 | ||
42 | /* This needs to come after some library #include | |
43 | to get __GNU_LIBRARY__ defined. */ | |
44 | #ifdef __GNU_LIBRARY__ | |
45 | #include <stdlib.h> | |
46 | #endif | |
47 | ||
d62a17ae | 48 | #ifndef NULL |
718e3744 | 49 | #define NULL 0 |
50 | #endif | |
51 | ||
d62a17ae | 52 | int getopt_long(argc, argv, options, long_options, opt_index) int argc; |
53 | char *const *argv; | |
54 | const char *options; | |
55 | const struct option *long_options; | |
56 | int *opt_index; | |
718e3744 | 57 | { |
d62a17ae | 58 | return _getopt_internal(argc, argv, options, long_options, opt_index, |
59 | 0); | |
718e3744 | 60 | } |
61 | ||
62 | /* Like getopt_long, but '-' as well as '--' can indicate a long option. | |
63 | If an option that starts with '-' (not '--') doesn't match a long option, | |
64 | but does match a short option, it is parsed as a short option | |
65 | instead. */ | |
66 | ||
d62a17ae | 67 | int getopt_long_only(argc, argv, options, long_options, opt_index) int argc; |
68 | char *const *argv; | |
69 | const char *options; | |
70 | const struct option *long_options; | |
71 | int *opt_index; | |
718e3744 | 72 | { |
d62a17ae | 73 | return _getopt_internal(argc, argv, options, long_options, opt_index, |
74 | 1); | |
718e3744 | 75 | } |
76 | ||
77 | ||
d62a17ae | 78 | #endif /* Not ELIDE_CODE. */ |
6b0655a2 | 79 | |
718e3744 | 80 | #ifdef TEST |
81 | ||
82 | #include <stdio.h> | |
83 | ||
d62a17ae | 84 | int main(argc, argv) int argc; |
85 | char **argv; | |
718e3744 | 86 | { |
d62a17ae | 87 | int c; |
88 | int digit_optind = 0; | |
89 | ||
90 | while (1) { | |
91 | int this_option_optind = optind ? optind : 1; | |
92 | int option_index = 0; | |
93 | static struct option long_options[] = { | |
94 | {"add", 1, 0, 0}, {"append", 0, 0, 0}, | |
95 | {"delete", 1, 0, 0}, {"verbose", 0, 0, 0}, | |
96 | {"create", 0, 0, 0}, {"file", 1, 0, 0}, | |
97 | {0, 0, 0, 0}}; | |
98 | ||
99 | c = getopt_long(argc, argv, "abc:d:0123456789", long_options, | |
100 | &option_index); | |
101 | if (c == -1) | |
102 | break; | |
103 | ||
104 | switch (c) { | |
105 | case 0: | |
106 | printf("option %s", long_options[option_index].name); | |
107 | if (optarg) | |
108 | printf(" with arg %s", optarg); | |
109 | printf("\n"); | |
110 | break; | |
111 | ||
112 | case '0': | |
113 | case '1': | |
114 | case '2': | |
115 | case '3': | |
116 | case '4': | |
117 | case '5': | |
118 | case '6': | |
119 | case '7': | |
120 | case '8': | |
121 | case '9': | |
122 | if (digit_optind != 0 | |
123 | && digit_optind != this_option_optind) | |
124 | printf("digits occur in two different argv-elements.\n"); | |
125 | digit_optind = this_option_optind; | |
126 | printf("option %c\n", c); | |
127 | break; | |
128 | ||
129 | case 'a': | |
130 | printf("option a\n"); | |
131 | break; | |
132 | ||
133 | case 'b': | |
134 | printf("option b\n"); | |
135 | break; | |
136 | ||
137 | case 'c': | |
138 | printf("option c with value `%s'\n", optarg); | |
139 | break; | |
140 | ||
141 | case 'd': | |
142 | printf("option d with value `%s'\n", optarg); | |
143 | break; | |
144 | ||
145 | case '?': | |
146 | break; | |
147 | ||
148 | default: | |
149 | printf("?? getopt returned character code 0%o ??\n", c); | |
150 | } | |
718e3744 | 151 | } |
718e3744 | 152 | |
d62a17ae | 153 | if (optind < argc) { |
154 | printf("non-option ARGV-elements: "); | |
155 | while (optind < argc) | |
156 | printf("%s ", argv[optind++]); | |
157 | printf("\n"); | |
158 | } | |
718e3744 | 159 | |
d62a17ae | 160 | exit(0); |
718e3744 | 161 | } |
162 | ||
163 | #endif /* TEST */ |