diff options
Diffstat (limited to 'src/lib/libc/stdlib/getopt.3')
-rw-r--r-- | src/lib/libc/stdlib/getopt.3 | 308 |
1 files changed, 206 insertions, 102 deletions
diff --git a/src/lib/libc/stdlib/getopt.3 b/src/lib/libc/stdlib/getopt.3 index f843881afd..ecdf42ab76 100644 --- a/src/lib/libc/stdlib/getopt.3 +++ b/src/lib/libc/stdlib/getopt.3 | |||
@@ -9,11 +9,7 @@ | |||
9 | .\" 2. Redistributions in binary form must reproduce the above copyright | 9 | .\" 2. Redistributions in binary form must reproduce the above copyright |
10 | .\" notice, this list of conditions and the following disclaimer in the | 10 | .\" notice, this list of conditions and the following disclaimer in the |
11 | .\" documentation and/or other materials provided with the distribution. | 11 | .\" documentation and/or other materials provided with the distribution. |
12 | .\" 3. All advertising materials mentioning features or use of this software | 12 | .\" 3. Neither the name of the University nor the names of its contributors |
13 | .\" must display the following acknowledgement: | ||
14 | .\" This product includes software developed by the University of | ||
15 | .\" California, Berkeley and its contributors. | ||
16 | .\" 4. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | 13 | .\" may be used to endorse or promote products derived from this software |
18 | .\" without specific prior written permission. | 14 | .\" without specific prior written permission. |
19 | .\" | 15 | .\" |
@@ -29,20 +25,20 @@ | |||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 25 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
30 | .\" SUCH DAMAGE. | 26 | .\" SUCH DAMAGE. |
31 | .\" | 27 | .\" |
32 | .\" @(#)getopt.3 8.4 (Berkeley) 4/19/94 | 28 | .\" $OpenBSD: getopt.3,v 1.42 2011/03/05 22:10:11 guenther Exp $ |
33 | .\" | 29 | .\" |
34 | .Dd April 19, 1994 | 30 | .Dd $Mdocdate: March 5 2011 $ |
35 | .Dt GETOPT 3 | 31 | .Dt GETOPT 3 |
36 | .Os BSD 4.3 | 32 | .Os |
37 | .Sh NAME | 33 | .Sh NAME |
38 | .Nm getopt | 34 | .Nm getopt |
39 | .Nd get option character from command line argument list | 35 | .Nd get option character from command line argument list |
40 | .Sh SYNOPSIS | 36 | .Sh SYNOPSIS |
41 | .Fd #include <unistd.h> | 37 | .Fd #include <unistd.h> |
42 | .Vt extern char *optarg; | 38 | .Vt extern char *optarg; |
39 | .Vt extern int opterr; | ||
43 | .Vt extern int optind; | 40 | .Vt extern int optind; |
44 | .Vt extern int optopt; | 41 | .Vt extern int optopt; |
45 | .Vt extern int opterr; | ||
46 | .Vt extern int optreset; | 42 | .Vt extern int optreset; |
47 | .Ft int | 43 | .Ft int |
48 | .Fn getopt "int argc" "char * const *argv" "const char *optstring" | 44 | .Fn getopt "int argc" "char * const *argv" "const char *optstring" |
@@ -61,20 +57,25 @@ if it has been specified in the string of accepted option characters, | |||
61 | .Pp | 57 | .Pp |
62 | The option string | 58 | The option string |
63 | .Fa optstring | 59 | .Fa optstring |
64 | may contain the following elements: individual characters, and | 60 | may contain the following elements: individual characters, |
65 | characters followed by a colon to indicate an option argument | 61 | characters followed by a colon, and characters followed by two colons. |
66 | is to follow. | 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. | ||
67 | For example, an option string | 66 | For example, an option string |
68 | .Li "\&""x"" | 67 | .Qq x |
69 | recognizes an option | 68 | recognizes an option |
70 | .Dq Fl x , | 69 | .Fl x , |
71 | and an option string | 70 | and an option string |
72 | .Li "\&""x:"" | 71 | .Qq Li x: |
73 | recognizes an option and argument | 72 | recognizes an option and argument |
74 | .Dq Fl x Ar argument . | 73 | .Fl x Ar argument . |
75 | It does not matter to | 74 | It does not matter to |
76 | .Fn getopt | 75 | .Fn getopt |
77 | if a following argument has leading white space. | 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. | ||
78 | .Pp | 79 | .Pp |
79 | On return from | 80 | On return from |
80 | .Fn getopt , | 81 | .Fn getopt , |
@@ -87,23 +88,23 @@ contains the index to the next | |||
87 | argument for a subsequent call | 88 | argument for a subsequent call |
88 | to | 89 | to |
89 | .Fn getopt . | 90 | .Fn getopt . |
90 | The variable | ||
91 | .Va optopt | ||
92 | saves the last | ||
93 | .Em known | ||
94 | option character returned by | ||
95 | .Fn getopt . | ||
96 | .Pp | 91 | .Pp |
97 | The variable | 92 | The variables |
98 | .Va opterr | 93 | .Va opterr |
99 | and | 94 | and |
100 | .Va optind | 95 | .Va optind |
101 | are both initialized to 1. | 96 | are both initialized to 1. |
102 | The | 97 | The |
103 | .Va optind | 98 | .Va optind |
104 | variable may be set to another value before a set of calls to | 99 | variable may be set to another value larger than 0 before a set of calls to |
105 | .Fn getopt | 100 | .Fn getopt |
106 | in order to skip over more or less argv entries. | 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 . | ||
107 | .Pp | 108 | .Pp |
108 | In order to use | 109 | In order to use |
109 | .Fn getopt | 110 | .Fn getopt |
@@ -119,101 +120,154 @@ must be reinitialized. | |||
119 | .Pp | 120 | .Pp |
120 | The | 121 | The |
121 | .Fn getopt | 122 | .Fn getopt |
122 | function | 123 | function returns \-1 when the argument list is exhausted. |
123 | returns \-1 | ||
124 | when the argument list is exhausted, or a non-recognized | ||
125 | option is encountered. | ||
126 | The interpretation of options in the argument list may be cancelled | 124 | The interpretation of options in the argument list may be cancelled |
127 | by the option | 125 | by the option |
128 | .Ql -- | 126 | .Ql -- |
129 | (double dash) which causes | 127 | (double dash) which causes |
130 | .Fn getopt | 128 | .Fn getopt |
131 | to signal the end of argument processing and returns \-1. | 129 | to signal the end of argument processing and return \-1. |
132 | When all options have been processed (i.e., up to the first non-option | 130 | When all options have been processed (i.e., up to the first non-option |
133 | argument), | 131 | argument), |
134 | .Fn getopt | 132 | .Fn getopt |
135 | returns \-1. | 133 | returns \-1. |
136 | .Sh DIAGNOSTICS | 134 | .Sh RETURN VALUES |
137 | If the | 135 | The |
138 | .Fn getopt | 136 | .Fn getopt |
139 | function encounters a character not found in the string | 137 | function returns the next known option character in |
140 | .Va optarg | 138 | .Fa optstring . |
141 | or detects | ||
142 | a missing option argument it writes an error message and returns | ||
143 | .Ql ? | ||
144 | to the | ||
145 | .Em stderr . | ||
146 | Setting | ||
147 | .Va opterr | ||
148 | to a zero will disable these error messages. | ||
149 | If | 139 | If |
150 | .Va optstring | 140 | .Fn getopt |
151 | has a leading | 141 | encounters a character not found in |
152 | .Ql \&: | 142 | .Fa optstring |
153 | then a missing option argument causes a | 143 | or if it detects a missing option argument, |
154 | .Ql \&: | 144 | it returns |
155 | to be returned in addition to suppressing any error messages. | 145 | .Sq \&? |
156 | .Pp | 146 | (question mark). |
157 | Option arguments are allowed to begin with | 147 | If |
158 | .Dq Li \- ; | 148 | .Fa optstring |
159 | this is reasonable but | 149 | has a leading |
160 | reduces the amount of error checking possible. | 150 | .Sq \&: |
161 | .Sh EXTENSIONS | 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. | ||
162 | The | 158 | The |
163 | .Va optreset | ||
164 | variable was added to make it possible to call the | ||
165 | .Fn getopt | 159 | .Fn getopt |
166 | function multiple times. | 160 | function returns \-1 when the argument list is exhausted. |
167 | This is an extension to the | 161 | .Sh EXAMPLES |
168 | .St -p1003.2 | 162 | The following code accepts the options |
169 | specification. | 163 | .Fl b |
170 | .Sh EXAMPLE | 164 | and |
171 | .Bd -literal -compact | 165 | .Fl f Ar argument |
172 | extern char *optarg; | 166 | and adjusts |
173 | extern int optind; | 167 | .Va argc |
168 | and | ||
169 | .Va argv | ||
170 | after option argument processing has completed. | ||
171 | .Bd -literal -offset indent | ||
174 | int bflag, ch, fd; | 172 | int bflag, ch, fd; |
175 | 173 | ||
176 | bflag = 0; | 174 | bflag = 0; |
177 | while ((ch = getopt(argc, argv, "bf:")) != -1) | 175 | while ((ch = getopt(argc, argv, "bf:")) != -1) { |
178 | switch(ch) { | 176 | switch (ch) { |
179 | case 'b': | 177 | case 'b': |
180 | bflag = 1; | 178 | bflag = 1; |
181 | break; | 179 | break; |
182 | case 'f': | 180 | case 'f': |
183 | if ((fd = open(optarg, O_RDONLY, 0)) < 0) { | 181 | if ((fd = open(optarg, O_RDONLY, 0)) == -1) |
184 | (void)fprintf(stderr, | 182 | err(1, "%s", optarg); |
185 | "myname: %s: %s\en", optarg, strerror(errno)); | ||
186 | exit(1); | ||
187 | } | ||
188 | break; | 183 | break; |
189 | case '?': | ||
190 | default: | 184 | default: |
191 | usage(); | 185 | usage(); |
186 | /* NOTREACHED */ | ||
187 | } | ||
192 | } | 188 | } |
193 | argc -= optind; | 189 | argc -= optind; |
194 | argv += optind; | 190 | argv += optind; |
195 | .Ed | 191 | .Ed |
196 | .Sh HISTORY | 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 | ||
197 | The | 221 | The |
198 | .Fn getopt | 222 | .Fn getopt |
199 | function appeared | 223 | function implements a superset of the functionality specified by |
200 | .Bx 4.3 . | 224 | .St -p1003.1 . |
201 | .Sh BUGS | 225 | .Pp |
226 | The following extensions are supported: | ||
227 | .Bl -tag -width "xxx" | ||
228 | .It Li o | ||
202 | The | 229 | The |
230 | .Va optreset | ||
231 | variable was added to make it possible to call the | ||
203 | .Fn getopt | 232 | .Fn getopt |
204 | function was once specified to return | 233 | function multiple times. |
205 | .Dv EOF | 234 | .It Li o |
206 | instead of \-1. | 235 | If the |
207 | This was changed by | 236 | .Va optind |
208 | .St -p1003.2-92 | 237 | variable is set to 0, |
209 | to decouple | ||
210 | .Fn getopt | 238 | .Fn getopt |
211 | from | 239 | will behave as if the |
212 | .Pa <stdio.h> . | 240 | .Va optreset |
213 | .Pp | 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 | ||
214 | A single dash | 268 | A single dash |
215 | .Dq Li - | 269 | .Pq Ql - |
216 | may be specified as an character in | 270 | may be specified as a character in |
217 | .Fa optstring , | 271 | .Fa optstring , |
218 | however it should | 272 | however it should |
219 | .Em never | 273 | .Em never |
@@ -221,40 +275,90 @@ have an argument associated with it. | |||
221 | This allows | 275 | This allows |
222 | .Fn getopt | 276 | .Fn getopt |
223 | to be used with programs that expect | 277 | to be used with programs that expect |
224 | .Dq Li - | 278 | .Ql - |
225 | as an option flag. | 279 | as an option flag. |
226 | This practice is wrong, and should not be used in any current development. | 280 | This practice is wrong, and should not be used in any current development. |
227 | It is provided for backward compatibility | 281 | It is provided for backward compatibility |
228 | .Em only . | 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). | ||
229 | By default, a single dash causes | 291 | By default, a single dash causes |
230 | .Fn getopt | 292 | .Fn getopt |
231 | to return \-1. | 293 | to return \-1. |
232 | This is, we believe, compatible with System V. | 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 . | ||
233 | .Pp | 325 | .Pp |
234 | It is also possible to handle digits as option letters. | 326 | It is possible to handle digits as option letters. |
235 | This allows | 327 | This allows |
236 | .Fn getopt | 328 | .Fn getopt |
237 | to be used with programs that expect a number | 329 | to be used with programs that expect a number |
238 | .Pq Dq Li \&-\&3 | 330 | .Pq Dq Li \-3 |
239 | as an option. | 331 | as an option. |
240 | This practice is wrong, and should not be used in any current development. | 332 | This practice is wrong, and should not be used in any current development. |
241 | It is provided for backward compatibility | 333 | It is provided for backward compatibility |
242 | .Em only . | 334 | .Em only . |
243 | The following code fragment works in most cases. | 335 | The following code fragment works in most cases and can handle mixed |
336 | number and letter arguments. | ||
244 | .Bd -literal -offset indent | 337 | .Bd -literal -offset indent |
245 | int length; | 338 | int aflag = 0, bflag = 0, ch, lastch = '\e0'; |
246 | char *p; | 339 | int length = -1, newarg = 1, prevoptind = 1; |
247 | 340 | ||
248 | while ((c = getopt(argc, argv, "0123456789")) != -1) | 341 | while ((ch = getopt(argc, argv, "0123456789ab")) != -1) { |
249 | switch (c) { | 342 | switch (ch) { |
250 | case '0': case '1': case '2': case '3': case '4': | 343 | case '0': case '1': case '2': case '3': case '4': |
251 | case '5': case '6': case '7': case '8': case '9': | 344 | case '5': case '6': case '7': case '8': case '9': |
252 | p = argv[optind - 1]; | 345 | if (newarg || !isdigit(lastch)) |
253 | if (p[0] == '-' && p[1] == ch && !p[2]) | 346 | length = 0; |
254 | length = atoi(++p); | 347 | else if (length > INT_MAX / 10) |
255 | else | 348 | usage(); |
256 | length = atoi(argv[optind] + 1); | 349 | length = (length * 10) + (ch - '0'); |
257 | break; | 350 | break; |
351 | case 'a': | ||
352 | aflag = 1; | ||
353 | break; | ||
354 | case 'b': | ||
355 | bflag = 1; | ||
356 | break; | ||
357 | default: | ||
358 | usage(); | ||
258 | } | 359 | } |
360 | lastch = ch; | ||
361 | newarg = optind != prevoptind; | ||
362 | prevoptind = optind; | ||
259 | } | 363 | } |
260 | .Ed | 364 | .Ed |