diff options
Diffstat (limited to 'src/lib/libc/stdlib/getopt.3')
-rw-r--r-- | src/lib/libc/stdlib/getopt.3 | 364 |
1 files changed, 364 insertions, 0 deletions
diff --git a/src/lib/libc/stdlib/getopt.3 b/src/lib/libc/stdlib/getopt.3 new file mode 100644 index 0000000000..ecdf42ab76 --- /dev/null +++ b/src/lib/libc/stdlib/getopt.3 | |||
@@ -0,0 +1,364 @@ | |||
1 | .\" Copyright (c) 1988, 1991, 1993 | ||
2 | .\" The Regents of the University of California. All rights reserved. | ||
3 | .\" | ||
4 | .\" Redistribution and use in source and binary forms, with or without | ||
5 | .\" modification, are permitted provided that the following conditions | ||
6 | .\" are met: | ||
7 | .\" 1. Redistributions of source code must retain the above copyright | ||
8 | .\" notice, this list of conditions and the following disclaimer. | ||
9 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer in the | ||
11 | .\" documentation and/or other materials provided with the distribution. | ||
12 | .\" 3. Neither the name of the University nor the names of its contributors | ||
13 | .\" may be used to endorse or promote products derived from this software | ||
14 | .\" without specific prior written permission. | ||
15 | .\" | ||
16 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
17 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
18 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
19 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
20 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
21 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
22 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
23 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
24 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
25 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
26 | .\" SUCH DAMAGE. | ||
27 | .\" | ||
28 | .\" $OpenBSD: getopt.3,v 1.42 2011/03/05 22:10:11 guenther Exp $ | ||
29 | .\" | ||
30 | .Dd $Mdocdate: March 5 2011 $ | ||
31 | .Dt GETOPT 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm getopt | ||
35 | .Nd get option character from command line argument list | ||
36 | .Sh SYNOPSIS | ||
37 | .Fd #include <unistd.h> | ||
38 | .Vt extern char *optarg; | ||
39 | .Vt extern int opterr; | ||
40 | .Vt extern int optind; | ||
41 | .Vt extern int optopt; | ||
42 | .Vt extern int optreset; | ||
43 | .Ft int | ||
44 | .Fn getopt "int argc" "char * const *argv" "const char *optstring" | ||
45 | .Sh DESCRIPTION | ||
46 | The | ||
47 | .Fn getopt | ||
48 | function incrementally parses a command line argument list | ||
49 | .Fa argv | ||
50 | and returns the next | ||
51 | .Em known | ||
52 | option character. | ||
53 | An option character is | ||
54 | .Em known | ||
55 | if it has been specified in the string of accepted option characters, | ||
56 | .Fa optstring . | ||
57 | .Pp | ||
58 | The option string | ||
59 | .Fa optstring | ||
60 | may contain the following elements: individual characters, | ||
61 | characters followed by a colon, and characters followed by two colons. | ||
62 | A character followed by a single colon indicates that an argument | ||
63 | is to follow the option on the command line. | ||
64 | Two colons indicates that the argument is optional \- this is an | ||
65 | extension not covered by POSIX. | ||
66 | For example, an option string | ||
67 | .Qq x | ||
68 | recognizes an option | ||
69 | .Fl x , | ||
70 | and an option string | ||
71 | .Qq Li x: | ||
72 | recognizes an option and argument | ||
73 | .Fl x Ar argument . | ||
74 | It does not matter to | ||
75 | .Fn getopt | ||
76 | if a following argument has leading whitespace; except in the case where | ||
77 | the argument is optional, denoted with two colons, no leading whitespace | ||
78 | is permitted. | ||
79 | .Pp | ||
80 | On return from | ||
81 | .Fn getopt , | ||
82 | .Va optarg | ||
83 | points to an option argument, if it is anticipated, | ||
84 | and the variable | ||
85 | .Va optind | ||
86 | contains the index to the next | ||
87 | .Fa argv | ||
88 | argument for a subsequent call | ||
89 | to | ||
90 | .Fn getopt . | ||
91 | .Pp | ||
92 | The variables | ||
93 | .Va opterr | ||
94 | and | ||
95 | .Va optind | ||
96 | are both initialized to 1. | ||
97 | The | ||
98 | .Va optind | ||
99 | variable may be set to another value larger than 0 before a set of calls to | ||
100 | .Fn getopt | ||
101 | in order to skip over more or less | ||
102 | .Fa argv | ||
103 | entries. | ||
104 | An | ||
105 | .Va optind | ||
106 | value of 0 is reserved for compatibility with GNU | ||
107 | .Fn getopt . | ||
108 | .Pp | ||
109 | In order to use | ||
110 | .Fn getopt | ||
111 | to evaluate multiple sets of arguments, or to evaluate a single set of | ||
112 | arguments multiple times, | ||
113 | the variable | ||
114 | .Va optreset | ||
115 | must be set to 1 before the second and each additional set of calls to | ||
116 | .Fn getopt , | ||
117 | and the variable | ||
118 | .Va optind | ||
119 | must be reinitialized. | ||
120 | .Pp | ||
121 | The | ||
122 | .Fn getopt | ||
123 | function returns \-1 when the argument list is exhausted. | ||
124 | The interpretation of options in the argument list may be cancelled | ||
125 | by the option | ||
126 | .Ql -- | ||
127 | (double dash) which causes | ||
128 | .Fn getopt | ||
129 | to signal the end of argument processing and return \-1. | ||
130 | When all options have been processed (i.e., up to the first non-option | ||
131 | argument), | ||
132 | .Fn getopt | ||
133 | returns \-1. | ||
134 | .Sh RETURN VALUES | ||
135 | The | ||
136 | .Fn getopt | ||
137 | function returns the next known option character in | ||
138 | .Fa optstring . | ||
139 | If | ||
140 | .Fn getopt | ||
141 | encounters a character not found in | ||
142 | .Fa optstring | ||
143 | or if it detects a missing option argument, | ||
144 | it returns | ||
145 | .Sq \&? | ||
146 | (question mark). | ||
147 | If | ||
148 | .Fa optstring | ||
149 | has a leading | ||
150 | .Sq \&: | ||
151 | then a missing option argument causes | ||
152 | .Sq \&: | ||
153 | to be returned instead of | ||
154 | .Sq \&? . | ||
155 | In either case, the variable | ||
156 | .Va optopt | ||
157 | is set to the character that caused the error. | ||
158 | The | ||
159 | .Fn getopt | ||
160 | function returns \-1 when the argument list is exhausted. | ||
161 | .Sh EXAMPLES | ||
162 | The following code accepts the options | ||
163 | .Fl b | ||
164 | and | ||
165 | .Fl f Ar argument | ||
166 | and adjusts | ||
167 | .Va argc | ||
168 | and | ||
169 | .Va argv | ||
170 | after option argument processing has completed. | ||
171 | .Bd -literal -offset indent | ||
172 | int bflag, ch, fd; | ||
173 | |||
174 | bflag = 0; | ||
175 | while ((ch = getopt(argc, argv, "bf:")) != -1) { | ||
176 | switch (ch) { | ||
177 | case 'b': | ||
178 | bflag = 1; | ||
179 | break; | ||
180 | case 'f': | ||
181 | if ((fd = open(optarg, O_RDONLY, 0)) == -1) | ||
182 | err(1, "%s", optarg); | ||
183 | break; | ||
184 | default: | ||
185 | usage(); | ||
186 | /* NOTREACHED */ | ||
187 | } | ||
188 | } | ||
189 | argc -= optind; | ||
190 | argv += optind; | ||
191 | .Ed | ||
192 | .Sh DIAGNOSTICS | ||
193 | If the | ||
194 | .Fn getopt | ||
195 | function encounters a character not found in the string | ||
196 | .Fa optstring | ||
197 | or detects | ||
198 | a missing option argument, it writes an error message to | ||
199 | .Em stderr | ||
200 | and returns | ||
201 | .Ql \&? . | ||
202 | Setting | ||
203 | .Va opterr | ||
204 | to a zero will disable these error messages. | ||
205 | If | ||
206 | .Fa optstring | ||
207 | has a leading | ||
208 | .Ql \&: | ||
209 | then a missing option argument causes a | ||
210 | .Ql \&: | ||
211 | to be returned in addition to suppressing any error messages. | ||
212 | .Pp | ||
213 | Option arguments are allowed to begin with | ||
214 | .Ql - ; | ||
215 | this is reasonable but reduces the amount of error checking possible. | ||
216 | .Sh SEE ALSO | ||
217 | .Xr getopt 1 , | ||
218 | .Xr getopt_long 3 , | ||
219 | .Xr getsubopt 3 | ||
220 | .Sh STANDARDS | ||
221 | The | ||
222 | .Fn getopt | ||
223 | function implements a superset of the functionality specified by | ||
224 | .St -p1003.1 . | ||
225 | .Pp | ||
226 | The following extensions are supported: | ||
227 | .Bl -tag -width "xxx" | ||
228 | .It Li o | ||
229 | The | ||
230 | .Va optreset | ||
231 | variable was added to make it possible to call the | ||
232 | .Fn getopt | ||
233 | function multiple times. | ||
234 | .It Li o | ||
235 | If the | ||
236 | .Va optind | ||
237 | variable is set to 0, | ||
238 | .Fn getopt | ||
239 | will behave as if the | ||
240 | .Va optreset | ||
241 | variable has been set. | ||
242 | This is for compatibility with | ||
243 | .Tn GNU | ||
244 | .Fn getopt . | ||
245 | New code should use | ||
246 | .Va optreset | ||
247 | instead. | ||
248 | .It Li o | ||
249 | If the first character of | ||
250 | .Fa optstring | ||
251 | is a plus sign | ||
252 | .Pq Ql + , | ||
253 | it will be ignored. | ||
254 | This is for compatibility with | ||
255 | .Tn GNU | ||
256 | .Fn getopt . | ||
257 | .It Li o | ||
258 | If the first character of | ||
259 | .Fa optstring | ||
260 | is a dash | ||
261 | .Pq Ql - , | ||
262 | non-options will be returned as arguments to the option character | ||
263 | .Ql \e1 . | ||
264 | This is for compatibility with | ||
265 | .Tn GNU | ||
266 | .Fn getopt . | ||
267 | .It Li o | ||
268 | A single dash | ||
269 | .Pq Ql - | ||
270 | may be specified as a character in | ||
271 | .Fa optstring , | ||
272 | however it should | ||
273 | .Em never | ||
274 | have an argument associated with it. | ||
275 | This allows | ||
276 | .Fn getopt | ||
277 | to be used with programs that expect | ||
278 | .Ql - | ||
279 | as an option flag. | ||
280 | This practice is wrong, and should not be used in any current development. | ||
281 | It is provided for backward compatibility | ||
282 | .Em only . | ||
283 | Care should be taken not to use | ||
284 | .Ql - | ||
285 | as the first character in | ||
286 | .Fa optstring | ||
287 | to avoid a semantic conflict with | ||
288 | .Tn GNU | ||
289 | .Fn getopt | ||
290 | semantics (see above). | ||
291 | By default, a single dash causes | ||
292 | .Fn getopt | ||
293 | to return \-1. | ||
294 | .El | ||
295 | .Pp | ||
296 | Historic | ||
297 | .Bx | ||
298 | versions of | ||
299 | .Fn getopt | ||
300 | set | ||
301 | .Fa optopt | ||
302 | to the last option character processed. | ||
303 | However, this conflicts with | ||
304 | .St -p1003.1 | ||
305 | which stipulates that | ||
306 | .Fa optopt | ||
307 | be set to the last character that caused an error. | ||
308 | .Sh HISTORY | ||
309 | The | ||
310 | .Fn getopt | ||
311 | function appeared in | ||
312 | .Bx 4.3 . | ||
313 | .Sh BUGS | ||
314 | The | ||
315 | .Fn getopt | ||
316 | function was once specified to return | ||
317 | .Dv EOF | ||
318 | instead of \-1. | ||
319 | This was changed by | ||
320 | .St -p1003.2-92 | ||
321 | to decouple | ||
322 | .Fn getopt | ||
323 | from | ||
324 | .Aq Pa stdio.h . | ||
325 | .Pp | ||
326 | It is possible to handle digits as option letters. | ||
327 | This allows | ||
328 | .Fn getopt | ||
329 | to be used with programs that expect a number | ||
330 | .Pq Dq Li \-3 | ||
331 | as an option. | ||
332 | This practice is wrong, and should not be used in any current development. | ||
333 | It is provided for backward compatibility | ||
334 | .Em only . | ||
335 | The following code fragment works in most cases and can handle mixed | ||
336 | number and letter arguments. | ||
337 | .Bd -literal -offset indent | ||
338 | int aflag = 0, bflag = 0, ch, lastch = '\e0'; | ||
339 | int length = -1, newarg = 1, prevoptind = 1; | ||
340 | |||
341 | while ((ch = getopt(argc, argv, "0123456789ab")) != -1) { | ||
342 | switch (ch) { | ||
343 | case '0': case '1': case '2': case '3': case '4': | ||
344 | case '5': case '6': case '7': case '8': case '9': | ||
345 | if (newarg || !isdigit(lastch)) | ||
346 | length = 0; | ||
347 | else if (length > INT_MAX / 10) | ||
348 | usage(); | ||
349 | length = (length * 10) + (ch - '0'); | ||
350 | break; | ||
351 | case 'a': | ||
352 | aflag = 1; | ||
353 | break; | ||
354 | case 'b': | ||
355 | bflag = 1; | ||
356 | break; | ||
357 | default: | ||
358 | usage(); | ||
359 | } | ||
360 | lastch = ch; | ||
361 | newarg = optind != prevoptind; | ||
362 | prevoptind = optind; | ||
363 | } | ||
364 | .Ed | ||