diff options
Diffstat (limited to 'src/lib/libc/stdlib/getopt_long.3')
-rw-r--r-- | src/lib/libc/stdlib/getopt_long.3 | 366 |
1 files changed, 366 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..6226dfe99f --- /dev/null +++ b/src/lib/libc/stdlib/getopt_long.3 | |||
@@ -0,0 +1,366 @@ | |||
1 | .\" $OpenBSD: getopt_long.3,v 1.9 2003/09/02 18:24:21 jmc 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. Neither the name of the University nor the names of its contributors | ||
16 | .\" may be used to endorse or promote products derived from this software | ||
17 | .\" without specific prior written permission. | ||
18 | .\" | ||
19 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | .\" SUCH DAMAGE. | ||
30 | .\" | ||
31 | .\" @(#)getopt.3 8.5 (Berkeley) 4/27/95 | ||
32 | .\" | ||
33 | .Dd April 1, 2000 | ||
34 | .Dt GETOPT_LONG 3 | ||
35 | .Os | ||
36 | .Sh NAME | ||
37 | .Nm getopt_long , | ||
38 | .Nm getopt_long_only | ||
39 | .Nd get long options from command line argument list | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <getopt.h> | ||
42 | .Vt extern char *optarg; | ||
43 | .Vt extern int optind; | ||
44 | .Vt extern int optopt; | ||
45 | .Vt extern int opterr; | ||
46 | .Vt extern int optreset; | ||
47 | .Ft int | ||
48 | .Fn getopt_long "int argc" "char * const *argv" "const char *optstring" "const struct option *longopts" "int *index" | ||
49 | .Ft int | ||
50 | .Fn getopt_long_only "int argc" "char * const *argv" "const char *optstring" "const struct option *longopts" "int *index" | ||
51 | .Sh DESCRIPTION | ||
52 | The | ||
53 | .Fn getopt_long | ||
54 | function is similar to | ||
55 | .Xr getopt 3 | ||
56 | but it accepts options in two forms: words and characters. | ||
57 | The | ||
58 | .Fn getopt_long | ||
59 | function provides a superset of the functionality of | ||
60 | .Xr getopt 3 . | ||
61 | .Fn getopt_long | ||
62 | can be used in two ways. | ||
63 | In the first way, every long option understood by the program has a | ||
64 | corresponding short option, and the option structure is only used to | ||
65 | translate from long options to short options. | ||
66 | When used in this fashion, | ||
67 | .Fn getopt_long | ||
68 | behaves identically to | ||
69 | .Xr getopt 3 . | ||
70 | This is a good way to add long option processing to an existing program | ||
71 | with the minimum of rewriting. | ||
72 | .Pp | ||
73 | In the second mechanism, a long option sets a flag in the | ||
74 | .Fa option | ||
75 | structure passed, or will store a pointer to the command line argument | ||
76 | in the | ||
77 | .Fa option | ||
78 | structure passed to it for options that take arguments. | ||
79 | Additionally, the long option's argument may be specified as a single | ||
80 | argument with an equal sign, e.g. | ||
81 | .Bd -literal | ||
82 | myprogram --myoption=somevalue | ||
83 | .Ed | ||
84 | .Pp | ||
85 | When a long option is processed the call to | ||
86 | .Fn getopt_long | ||
87 | will return 0. | ||
88 | For this reason, long option processing without | ||
89 | shortcuts is not backwards compatible with | ||
90 | .Xr getopt 3 . | ||
91 | .Pp | ||
92 | It is possible to combine these methods, providing for long options | ||
93 | processing with short option equivalents for some options. | ||
94 | Less frequently used options would be processed as long options only. | ||
95 | .Pp | ||
96 | The | ||
97 | .Fn getopt_long | ||
98 | call requires a structure to be initialized describing the long | ||
99 | options. | ||
100 | The structure is: | ||
101 | .Bd -literal | ||
102 | struct option { | ||
103 | char *name; | ||
104 | int has_arg; | ||
105 | int *flag; | ||
106 | int val; | ||
107 | }; | ||
108 | .Ed | ||
109 | .Pp | ||
110 | The | ||
111 | .Fa name | ||
112 | field should contain the option name without the leading double dash. | ||
113 | .Pp | ||
114 | The | ||
115 | .Fa has_arg | ||
116 | field should be one of: | ||
117 | .Bl -tag -width "optional_argument" | ||
118 | .It Li no_argument | ||
119 | no argument to the option is expect. | ||
120 | .It Li required_argument | ||
121 | an argument to the option is required. | ||
122 | .It Li optional_argument | ||
123 | an argument to the option may be presented. | ||
124 | .El | ||
125 | .Pp | ||
126 | If | ||
127 | .Fa flag | ||
128 | is not | ||
129 | .Dv NULL , | ||
130 | then the integer pointed to by it will be set to the value in the | ||
131 | .Fa val | ||
132 | field. | ||
133 | If the | ||
134 | .Fa flag | ||
135 | field is | ||
136 | .Dv NULL , | ||
137 | then the | ||
138 | .Fa val | ||
139 | field will be returned. | ||
140 | Setting | ||
141 | .Fa flag | ||
142 | to | ||
143 | .Dv NULL | ||
144 | and setting | ||
145 | .Fa val | ||
146 | to the corresponding short option will make this function act just | ||
147 | like | ||
148 | .Xr getopt 3 . | ||
149 | .Pp | ||
150 | The | ||
151 | .Fn getopt_long_only | ||
152 | function behaves identically to | ||
153 | .Fn getopt_long | ||
154 | with the exception that long options may start with | ||
155 | .Sq - | ||
156 | in addition to | ||
157 | .Sq -- . | ||
158 | If an option starting with | ||
159 | .Sq - | ||
160 | does not match a long option but does match a single-character option, | ||
161 | the single-character option is returned. | ||
162 | .Sh RETURN VALUES | ||
163 | If the | ||
164 | .Fa flag | ||
165 | field in | ||
166 | .Li struct option | ||
167 | is | ||
168 | .Dv NULL , | ||
169 | .Fn getopt_long | ||
170 | and | ||
171 | .Fn getopt_long_only | ||
172 | return the value specified in the | ||
173 | .Fa val | ||
174 | field, which is usually just the corresponding short option. | ||
175 | If | ||
176 | .Fa flag | ||
177 | is not | ||
178 | .Dv NULL , | ||
179 | these functions return 0 and store | ||
180 | .Fa val | ||
181 | in the location pointed to by | ||
182 | .Fa flag . | ||
183 | These functions return | ||
184 | .Sq \: | ||
185 | if there was a missing option argument, | ||
186 | .Sq \&? | ||
187 | if the user specified an unknown or ambiguous option, and | ||
188 | \-1 when the argument list has been exhausted. | ||
189 | .Sh EXAMPLES | ||
190 | .Bd -literal -compact | ||
191 | int bflag, ch, fd; | ||
192 | int daggerset; | ||
193 | |||
194 | /* options descriptor */ | ||
195 | static struct option longopts[] = { | ||
196 | { "buffy", no_argument, 0, 'b' }, | ||
197 | { "fluoride", required_argument, 0, 'f' }, | ||
198 | { "daggerset", no_argument, &daggerset, 1 }, | ||
199 | { 0, 0, 0, 0 } | ||
200 | }; | ||
201 | |||
202 | bflag = 0; | ||
203 | while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1) | ||
204 | switch(ch) { | ||
205 | case 'b': | ||
206 | bflag = 1; | ||
207 | break; | ||
208 | case 'f': | ||
209 | if ((fd = open(optarg, O_RDONLY, 0)) == -1) | ||
210 | err(1, "unable to open %s", optarg); | ||
211 | break; | ||
212 | case 0: | ||
213 | if (daggerset) { | ||
214 | fprintf(stderr,"Buffy will use her dagger to " | ||
215 | "apply fluoride to dracula's teeth\en"); | ||
216 | } | ||
217 | break; | ||
218 | case '?': | ||
219 | default: | ||
220 | usage(); | ||
221 | } | ||
222 | argc -= optind; | ||
223 | argv += optind; | ||
224 | .Ed | ||
225 | .Sh IMPLEMENTATION DIFFERENCES | ||
226 | This section describes differences to the GNU implementation | ||
227 | found in glibc-2.1.3: | ||
228 | .Bl -tag -width "xxx" | ||
229 | .It Li o | ||
230 | handling of - as first char of option string in presence of | ||
231 | environment variable POSIXLY_CORRECT: | ||
232 | .Bl -tag -width "OpenBSD" | ||
233 | .It Li GNU | ||
234 | ignores POSIXLY_CORRECT and returns non-options as | ||
235 | arguments to option '\e1'. | ||
236 | .It Li OpenBSD | ||
237 | honors POSIXLY_CORRECT and stops at the first non-option. | ||
238 | .El | ||
239 | .It Li o | ||
240 | handling of - within the option string (not the first character): | ||
241 | .Bl -tag -width "OpenBSD" | ||
242 | .It Li GNU | ||
243 | treats a | ||
244 | .Ql - | ||
245 | on the command line as a non-argument. | ||
246 | .It Li OpenBSD | ||
247 | a | ||
248 | .Ql - | ||
249 | within the option string matches a | ||
250 | .Ql - | ||
251 | (single dash) on the command line. | ||
252 | This functionality is provided for backward compatibility with | ||
253 | programs, such as | ||
254 | .Xr su 1 , | ||
255 | that use | ||
256 | .Ql - | ||
257 | as an option flag. | ||
258 | This practice is wrong, and should not be used in any current development. | ||
259 | .El | ||
260 | .It Li o | ||
261 | handling of :: in options string in presence of POSIXLY_CORRECT: | ||
262 | .Bl -tag -width "OpenBSD" | ||
263 | .It Li Both | ||
264 | GNU and OpenBSD ignore POSIXLY_CORRECT here and take :: to | ||
265 | mean the preceding option takes an optional argument. | ||
266 | .El | ||
267 | .It Li o | ||
268 | return value in case of missing argument if first character | ||
269 | (after + or -) in option string is not ':': | ||
270 | .Bl -tag -width "OpenBSD" | ||
271 | .It Li GNU | ||
272 | returns '?' | ||
273 | .It OpenBSD | ||
274 | returns ':' (since OpenBSD's getopt does). | ||
275 | .El | ||
276 | .It Li o | ||
277 | handling of --a in getopt: | ||
278 | .Bl -tag -width "OpenBSD" | ||
279 | .It Li GNU | ||
280 | parses this as option '-', option 'a'. | ||
281 | .It Li OpenBSD | ||
282 | parses this as '--', and returns \-1 (ignoring the a). | ||
283 | (Because the original getopt does.) | ||
284 | .El | ||
285 | .It Li o | ||
286 | setting of optopt for long options with flag != | ||
287 | .Dv NULL : | ||
288 | .Bl -tag -width "OpenBSD" | ||
289 | .It Li GNU | ||
290 | sets optopt to val. | ||
291 | .It Li OpenBSD | ||
292 | sets optopt to 0 (since val would never be returned). | ||
293 | .El | ||
294 | .It Li o | ||
295 | handling of -W with W; in option string in getopt (not getopt_long): | ||
296 | .Bl -tag -width "OpenBSD" | ||
297 | .It Li GNU | ||
298 | causes a segfault. | ||
299 | .It Li OpenBSD | ||
300 | no special handling is done; | ||
301 | .Dq W; | ||
302 | is interpreted as two separate options, neither of which take an argument. | ||
303 | .El | ||
304 | .It Li o | ||
305 | setting of optarg for long options without an argument that are | ||
306 | invoked via -W (W; in option string): | ||
307 | .Bl -tag -width "OpenBSD" | ||
308 | .It Li GNU | ||
309 | sets optarg to the option name (the argument of -W). | ||
310 | .It Li OpenBSD | ||
311 | sets optarg to | ||
312 | .Dv NULL | ||
313 | (the argument of the long option). | ||
314 | .El | ||
315 | .It Li o | ||
316 | handling of -W with an argument that is not (a prefix to) a known | ||
317 | long option (W; in option string): | ||
318 | .Bl -tag -width "OpenBSD" | ||
319 | .It Li GNU | ||
320 | returns -W with optarg set to the unknown option. | ||
321 | .It Li OpenBSD | ||
322 | treats this as an error (unknown option) and returns '?' with | ||
323 | optopt set to 0 and optarg set to | ||
324 | .Dv NULL | ||
325 | (as GNU's man page documents). | ||
326 | .El | ||
327 | .It Li o | ||
328 | The error messages are different. | ||
329 | .It Li o | ||
330 | OpenBSD does not permute the argument vector at the same points in | ||
331 | the calling sequence as GNU does. | ||
332 | The aspects normally used by the caller | ||
333 | (ordering after \-1 is returned, value of optind relative | ||
334 | to current positions) are the same, though. | ||
335 | (We do fewer variable swaps.) | ||
336 | .El | ||
337 | .Sh ENVIRONMENT | ||
338 | .Bl -tag -width POSIXLY_CORRECT | ||
339 | .It Ev POSIXLY_CORRECT | ||
340 | If set, option processing stops when the first non-option is found and | ||
341 | a leading | ||
342 | .Sq - | ||
343 | or | ||
344 | .Sq + | ||
345 | in the | ||
346 | .Ar optstring | ||
347 | is ignored. | ||
348 | .El | ||
349 | .Sh SEE ALSO | ||
350 | .Xr getopt 3 | ||
351 | .Sh HISTORY | ||
352 | The | ||
353 | .Fn getopt_long | ||
354 | and | ||
355 | .Fn getopt_long_only | ||
356 | functions first appeared in GNU libiberty. | ||
357 | This implementation first appeared in | ||
358 | .Ox 3.3 . | ||
359 | .Sh BUGS | ||
360 | The | ||
361 | .Ar argv | ||
362 | argument is not really | ||
363 | .Dv const | ||
364 | as its elements may be permuted (unless | ||
365 | .Ev POSIXLY_CORRECT | ||
366 | is set). | ||