diff options
author | millert <> | 2002-12-03 20:24:30 +0000 |
---|---|---|
committer | millert <> | 2002-12-03 20:24:30 +0000 |
commit | 4bbebe1cbc72fcf121b5982aba8121a040997270 (patch) | |
tree | 4181617be2388b75004461e3be7ffcc9c1b6f922 /src/lib/libc/stdlib/getopt_long.3 | |
parent | 5b1b8dec4403c20d7a64a5a24dd566c852ddcb6b (diff) | |
download | openbsd-4bbebe1cbc72fcf121b5982aba8121a040997270.tar.gz openbsd-4bbebe1cbc72fcf121b5982aba8121a040997270.tar.bz2 openbsd-4bbebe1cbc72fcf121b5982aba8121a040997270.zip |
GNU-like getopt_long() from NetBSD with changes by me to support
getopt_long_only(). At some point this should replace the BSD
getopt(3) but we are not there yet.
While I am here add protection from the multiple getopt() definitions
due to conflicting standards.
Diffstat (limited to 'src/lib/libc/stdlib/getopt_long.3')
-rw-r--r-- | src/lib/libc/stdlib/getopt_long.3 | 326 |
1 files changed, 326 insertions, 0 deletions
diff --git a/src/lib/libc/stdlib/getopt_long.3 b/src/lib/libc/stdlib/getopt_long.3 new file mode 100644 index 0000000000..d55f4ead53 --- /dev/null +++ b/src/lib/libc/stdlib/getopt_long.3 | |||
@@ -0,0 +1,326 @@ | |||
1 | .\" $OpenBSD: getopt_long.3,v 1.1 2002/12/03 20:24:30 millert Exp $ | ||
2 | .\" $NetBSD: getopt_long.3,v 1.11 2002/10/02 10:54:19 wiz Exp $ | ||
3 | .\" | ||
4 | .\" Copyright (c) 1988, 1991, 1993 | ||
5 | .\" The Regents of the University of California. All rights reserved. | ||
6 | .\" | ||
7 | .\" Redistribution and use in source and binary forms, with or without | ||
8 | .\" modification, are permitted provided that the following conditions | ||
9 | .\" are met: | ||
10 | .\" 1. Redistributions of source code must retain the above copyright | ||
11 | .\" notice, this list of conditions and the following disclaimer. | ||
12 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
13 | .\" notice, this list of conditions and the following disclaimer in the | ||
14 | .\" documentation and/or other materials provided with the distribution. | ||
15 | .\" 3. All advertising materials mentioning features or use of this software | ||
16 | .\" must display the following acknowledgement: | ||
17 | .\" This product includes software developed by the University of | ||
18 | .\" California, Berkeley and its contributors. | ||
19 | .\" 4. Neither the name of the University nor the names of its contributors | ||
20 | .\" may be used to endorse or promote products derived from this software | ||
21 | .\" without specific prior written permission. | ||
22 | .\" | ||
23 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | .\" SUCH DAMAGE. | ||
34 | .\" | ||
35 | .\" @(#)getopt.3 8.5 (Berkeley) 4/27/95 | ||
36 | .\" | ||
37 | .Dd April 1, 2000 | ||
38 | .Dt GETOPT_LONG 3 | ||
39 | .Os | ||
40 | .Sh NAME | ||
41 | .Nm getopt_long , | ||
42 | .Nm getopt_long_only | ||
43 | .Nd get long options from command line argument list | ||
44 | .Sh SYNOPSIS | ||
45 | .Fd #include <getopt.h> | ||
46 | .Vt extern char *optarg; | ||
47 | .Vt extern int optind; | ||
48 | .Vt extern int optopt; | ||
49 | .Vt extern int opterr; | ||
50 | .Vt extern int optreset; | ||
51 | .Ft int | ||
52 | .Fn getopt_long "int argc" "char * const *argv" "const char *optstring" "struct option *long options" "int *index" | ||
53 | .Ft int | ||
54 | .Fn getopt_long_only "int argc" "char * const *argv" "const char *optstring" "struct option *long options" "int *index" | ||
55 | .Sh DESCRIPTION | ||
56 | The | ||
57 | .Fn getopt_long | ||
58 | function is similar to | ||
59 | .Xr getopt 3 | ||
60 | but it accepts options in two forms: words and characters. | ||
61 | The | ||
62 | .Fn getopt_long | ||
63 | function provides a superset of of the functionality of | ||
64 | .Xr getopt 3 . | ||
65 | .Fn getopt_long | ||
66 | can be used in two ways. | ||
67 | In the first way, every long option understood by the program has a | ||
68 | corresponding short option, and the option structure is only used to | ||
69 | translate from long options to short options. | ||
70 | When used in this fashion, | ||
71 | .Fn getopt_long | ||
72 | behaves identically to | ||
73 | .Xr getopt 3 . | ||
74 | This is a good way to add long option processing to an existing program | ||
75 | with the minimum of rewriting. | ||
76 | .Pp | ||
77 | In the second mechanism, a long option sets a flag in the | ||
78 | .Fa option | ||
79 | structure passed, or will store a pointer to the command line argument | ||
80 | in the | ||
81 | .Fa option | ||
82 | structure passed to it for options that take arguments. | ||
83 | Additionally, the long option's argument may be specified as a single | ||
84 | argument with an equal sign, e.g. | ||
85 | .Bd -literal | ||
86 | myprogram --myoption=somevalue | ||
87 | .Ed | ||
88 | .Pp | ||
89 | When a long option is processed the call to | ||
90 | .Fn getopt_long | ||
91 | will return 0. | ||
92 | For this reason, long option processing without | ||
93 | shortcuts is not backwards compatible with | ||
94 | .Xr getopt 3 . | ||
95 | .Pp | ||
96 | It is possible to combine these methods, providing for long options | ||
97 | processing with short option equivalents for some options. | ||
98 | Less frequently used options would be processed as long options only. | ||
99 | .Pp | ||
100 | The | ||
101 | .Fn getopt_long | ||
102 | call requires a structure to be initialized describing the long | ||
103 | options. | ||
104 | The structure is: | ||
105 | .Bd -literal | ||
106 | struct option { | ||
107 | char *name; | ||
108 | int has_arg; | ||
109 | int *flag; | ||
110 | int val; | ||
111 | }; | ||
112 | .Ed | ||
113 | .Pp | ||
114 | The | ||
115 | .Fa name | ||
116 | field should contain the option name without the leading double dash. | ||
117 | .Pp | ||
118 | The | ||
119 | .Fa has_arg | ||
120 | field should be one of: | ||
121 | .Bl -tag -width "optional_argument" | ||
122 | .It Li no_argument | ||
123 | no argument to the option is expect. | ||
124 | .It Li required_argument | ||
125 | an argument to the option is required. | ||
126 | .It Li optional_argument | ||
127 | an argument to the option may be presented. | ||
128 | .El | ||
129 | .Pp | ||
130 | If | ||
131 | .Fa flag | ||
132 | is not | ||
133 | .Dv NULL , | ||
134 | then the integer pointed to by it will be set to the value in the | ||
135 | .Fa val | ||
136 | field. | ||
137 | If the | ||
138 | .Fa flag | ||
139 | field is | ||
140 | .Dv NULL , | ||
141 | then the | ||
142 | .Fa val | ||
143 | field will be returned. | ||
144 | Setting | ||
145 | .Fa flag | ||
146 | to | ||
147 | .Dv NULL | ||
148 | and setting | ||
149 | .Fa val | ||
150 | to the corresponding short option will make this function act just | ||
151 | like | ||
152 | .Xr getopt 3 . | ||
153 | .Pp | ||
154 | The | ||
155 | .Fn getopt_long_only | ||
156 | function behaves identically to | ||
157 | .Fn getopt_long | ||
158 | with the exception that long options may start with | ||
159 | .Sq - | ||
160 | in addition to | ||
161 | .Sq -- . | ||
162 | If an option starting with | ||
163 | .Sq - | ||
164 | does not match a long option but does match a single-character option, | ||
165 | the single-character option is returned. | ||
166 | .Sh EXAMPLES | ||
167 | .Bd -literal -compact | ||
168 | int bflag, ch, fd; | ||
169 | int daggerset; | ||
170 | |||
171 | /* options descriptor */ | ||
172 | static struct option longopts[] = { | ||
173 | { "buffy", no_argument, 0, 'b' }, | ||
174 | { "fluoride", required_argument, 0, 'f' }, | ||
175 | { "daggerset", no_argument, &daggerset, 1 }, | ||
176 | { 0, 0, 0, 0 } | ||
177 | }; | ||
178 | |||
179 | bflag = 0; | ||
180 | while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1) | ||
181 | switch(ch) { | ||
182 | case 'b': | ||
183 | bflag = 1; | ||
184 | break; | ||
185 | case 'f': | ||
186 | if ((fd = open(optarg, O_RDONLY, 0)) == -1) | ||
187 | err(1, "unable to open %s", optarg); | ||
188 | break; | ||
189 | case 0: | ||
190 | if (daggerset) { | ||
191 | fprintf(stderr,"Buffy will use her dagger to " | ||
192 | "apply fluoride to dracula's teeth\en"); | ||
193 | } | ||
194 | break; | ||
195 | case '?': | ||
196 | default: | ||
197 | usage(); | ||
198 | } | ||
199 | argc -= optind; | ||
200 | argv += optind; | ||
201 | .Ed | ||
202 | .Sh IMPLEMENTATION DIFFERENCES | ||
203 | This section describes differences to the GNU implementation | ||
204 | found in glibc-2.1.3: | ||
205 | .Bl -tag -width "xxx" | ||
206 | .It Li o | ||
207 | handling of - as first char of option string in presence of | ||
208 | environment variable POSIXLY_CORRECT: | ||
209 | .Bl -tag -width "OpenBSD" | ||
210 | .It Li GNU | ||
211 | ignores POSIXLY_CORRECT and returns non-options as | ||
212 | arguments to option '\e1'. | ||
213 | .It Li OpenBSD | ||
214 | honors POSIXLY_CORRECT and stops at the first non-option. | ||
215 | .El | ||
216 | .It Li o | ||
217 | handling of :: in options string in presence of POSIXLY_CORRECT: | ||
218 | .Bl -tag -width "OpenBSD" | ||
219 | .It Li Both | ||
220 | GNU and OpenBSD ignore POSIXLY_CORRECT here and take :: to | ||
221 | mean the preceding option takes an optional argument. | ||
222 | .El | ||
223 | .It Li o | ||
224 | return value in case of missing argument if first character | ||
225 | (after + or -) in option string is not ':': | ||
226 | .Bl -tag -width "OpenBSD" | ||
227 | .It Li GNU | ||
228 | returns '?' | ||
229 | .It OpenBSD | ||
230 | returns ':' (since OpenBSD's getopt does). | ||
231 | .El | ||
232 | .It Li o | ||
233 | handling of --a in getopt: | ||
234 | .Bl -tag -width "OpenBSD" | ||
235 | .It Li GNU | ||
236 | parses this as option '-', option 'a'. | ||
237 | .It Li OpenBSD | ||
238 | parses this as '--', and returns -1 (ignoring the a). (Because | ||
239 | the original getopt does.) | ||
240 | .El | ||
241 | .It Li o | ||
242 | setting of optopt for long options with flag != | ||
243 | .Dv NULL : | ||
244 | .Bl -tag -width "OpenBSD" | ||
245 | .It Li GNU | ||
246 | sets optopt to val. | ||
247 | .It Li OpenBSD | ||
248 | sets optopt to 0 (since val would never be returned). | ||
249 | .El | ||
250 | .It Li o | ||
251 | handling of -W with W; in option string in getopt (not getopt_long): | ||
252 | .Bl -tag -width "OpenBSD" | ||
253 | .It Li GNU | ||
254 | causes a segfault. | ||
255 | .It Li OpenBSD | ||
256 | returns \-1, with optind pointing past the argument of -W | ||
257 | (as if `-W arg' were `--arg', and thus '--' had been found). | ||
258 | .\" How should we treat W; in the option string when called via | ||
259 | .\" getopt? Ignore the ';' or treat it as a ':'? Issue a warning? | ||
260 | .El | ||
261 | .It Li o | ||
262 | setting of optarg for long options without an argument that are | ||
263 | invoked via -W (W; in option string): | ||
264 | .Bl -tag -width "OpenBSD" | ||
265 | .It Li GNU | ||
266 | sets optarg to the option name (the argument of -W). | ||
267 | .It Li OpenBSD | ||
268 | sets optarg to | ||
269 | .Dv NULL | ||
270 | (the argument of the long option). | ||
271 | .El | ||
272 | .It Li o | ||
273 | handling of -W with an argument that is not (a prefix to) a known | ||
274 | long option (W; in option string): | ||
275 | .Bl -tag -width "OpenBSD" | ||
276 | .It Li GNU | ||
277 | returns -W with optarg set to the unknown option. | ||
278 | .It Li OpenBSD | ||
279 | treats this as an error (unknown option) and returns '?' with | ||
280 | optopt set to 0 and optarg set to | ||
281 | .Dv NULL | ||
282 | (as GNU's man page documents). | ||
283 | .El | ||
284 | .It Li o | ||
285 | The error messages are different. | ||
286 | .It Li o | ||
287 | OpenBSD does not permute the argument vector at the same points in | ||
288 | the calling sequence as GNU does. | ||
289 | The aspects normally used by the caller | ||
290 | (ordering after \-1 is returned, value of optind relative | ||
291 | to current positions) are the same, though. | ||
292 | (We do fewer variable swaps.) | ||
293 | .El | ||
294 | .Sh ENVIRONMENT | ||
295 | .Bl -tag -width POSIXLY_CORRECT | ||
296 | .It Ev POSIXLY_CORRECT | ||
297 | If set, option processing stops when the first non-option is found and | ||
298 | a leading | ||
299 | .Sq - | ||
300 | or | ||
301 | .Sq + | ||
302 | in the | ||
303 | .Ar optstring | ||
304 | is ignored. | ||
305 | .El | ||
306 | .Sh SEE ALSO | ||
307 | .Xr getopt 3 | ||
308 | .Sh HISTORY | ||
309 | The | ||
310 | .Fn getopt_long | ||
311 | and | ||
312 | .Fn getopt_long_only | ||
313 | functions first appeared in GNU libiberty. | ||
314 | This implementation first appeared in | ||
315 | .Ox 3.3 . | ||
316 | .Sh BUGS | ||
317 | The | ||
318 | .Ar argv | ||
319 | argument is not really | ||
320 | .Dv const | ||
321 | as its elements may be permuted (unless | ||
322 | .Ev POSIXLY_CORRECT | ||
323 | is set). | ||
324 | .Pp | ||
325 | In a future release, this implementation should completely replace | ||
326 | .Xr getopt 3 . | ||