diff options
Diffstat (limited to 'src/lib/libc/stdlib/getopt.3')
| -rw-r--r-- | src/lib/libc/stdlib/getopt.3 | 252 |
1 files changed, 166 insertions, 86 deletions
diff --git a/src/lib/libc/stdlib/getopt.3 b/src/lib/libc/stdlib/getopt.3 index f843881afd..d210852c6b 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,11 +25,11 @@ | |||
| 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.28 2003/09/22 23:47:26 millert Exp $ |
| 33 | .\" | 29 | .\" |
| 34 | .Dd April 19, 1994 | 30 | .Dd December 17, 2002 |
| 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 |
| @@ -61,20 +57,24 @@ 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 | ||
| 66 | .Px . | ||
| 67 | For example, an option string | 67 | For example, an option string |
| 68 | .Li "\&""x"" | 68 | .Qq x |
| 69 | recognizes an option | 69 | recognizes an option |
| 70 | .Dq Fl x , | 70 | .Fl x , |
| 71 | and an option string | 71 | and an option string |
| 72 | .Li "\&""x:"" | 72 | .Qq Li x: |
| 73 | recognizes an option and argument | 73 | recognizes an option and argument |
| 74 | .Dq Fl x Ar argument . | 74 | .Fl x Ar argument . |
| 75 | It does not matter to | 75 | It does not matter to |
| 76 | .Fn getopt | 76 | .Fn getopt |
| 77 | if a following argument has leading white space. | 77 | if a following argument has leading whitespace. |
| 78 | .Pp | 78 | .Pp |
| 79 | On return from | 79 | On return from |
| 80 | .Fn getopt , | 80 | .Fn getopt , |
| @@ -87,14 +87,8 @@ contains the index to the next | |||
| 87 | argument for a subsequent call | 87 | argument for a subsequent call |
| 88 | to | 88 | to |
| 89 | .Fn getopt . | 89 | .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 | 90 | .Pp |
| 97 | The variable | 91 | The variables |
| 98 | .Va opterr | 92 | .Va opterr |
| 99 | and | 93 | and |
| 100 | .Va optind | 94 | .Va optind |
| @@ -119,63 +113,62 @@ must be reinitialized. | |||
| 119 | .Pp | 113 | .Pp |
| 120 | The | 114 | The |
| 121 | .Fn getopt | 115 | .Fn getopt |
| 122 | function | 116 | 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 | 117 | The interpretation of options in the argument list may be cancelled |
| 127 | by the option | 118 | by the option |
| 128 | .Ql -- | 119 | .Ql -- |
| 129 | (double dash) which causes | 120 | (double dash) which causes |
| 130 | .Fn getopt | 121 | .Fn getopt |
| 131 | to signal the end of argument processing and returns \-1. | 122 | 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 | 123 | When all options have been processed (i.e., up to the first non-option |
| 133 | argument), | 124 | argument), |
| 134 | .Fn getopt | 125 | .Fn getopt |
| 135 | returns \-1. | 126 | returns \-1. |
| 136 | .Sh DIAGNOSTICS | 127 | .Sh RETURN VALUES |
| 137 | If the | 128 | The |
| 138 | .Fn getopt | 129 | .Fn getopt |
| 139 | function encounters a character not found in the string | 130 | function returns the next known option character in |
| 140 | .Va optarg | 131 | .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 | 132 | If |
| 150 | .Va optstring | 133 | .Fn getopt |
| 151 | has a leading | 134 | encounters a character not found in |
| 152 | .Ql \&: | 135 | .Fa optstring |
| 153 | then a missing option argument causes a | 136 | or if it detects a missing option argument, |
| 154 | .Ql \&: | 137 | it returns |
| 155 | to be returned in addition to suppressing any error messages. | 138 | .Sq \&? |
| 156 | .Pp | 139 | (question mark). |
| 157 | Option arguments are allowed to begin with | 140 | If |
| 158 | .Dq Li \- ; | 141 | .Fa optstring |
| 159 | this is reasonable but | 142 | has a leading |
| 160 | reduces the amount of error checking possible. | 143 | .Sq \&: |
| 161 | .Sh EXTENSIONS | 144 | then a missing option argument causes |
| 145 | .Sq \&: | ||
| 146 | to be returned instead of | ||
| 147 | .Sq \&? . | ||
| 148 | In either case, the variable | ||
| 149 | .Va optopt | ||
| 150 | is set to the character that caused the error. | ||
| 162 | The | 151 | The |
| 163 | .Va optreset | ||
| 164 | variable was added to make it possible to call the | ||
| 165 | .Fn getopt | 152 | .Fn getopt |
| 166 | function multiple times. | 153 | function returns \-1 when the argument list is exhausted. |
| 167 | This is an extension to the | 154 | .Sh ENVIRONMENT |
| 168 | .St -p1003.2 | 155 | .Bl -tag -width POSIXLY_CORRECTXX |
| 169 | specification. | 156 | .It Ev POSIXLY_CORRECT |
| 170 | .Sh EXAMPLE | 157 | If set, a leading |
| 158 | .Sq - | ||
| 159 | in | ||
| 160 | .Ar optstring | ||
| 161 | is ignored. | ||
| 162 | .El | ||
| 163 | .Sh EXAMPLES | ||
| 171 | .Bd -literal -compact | 164 | .Bd -literal -compact |
| 172 | extern char *optarg; | 165 | extern char *optarg; |
| 173 | extern int optind; | 166 | extern int optind; |
| 174 | int bflag, ch, fd; | 167 | int bflag, ch, fd; |
| 175 | 168 | ||
| 176 | bflag = 0; | 169 | bflag = 0; |
| 177 | while ((ch = getopt(argc, argv, "bf:")) != -1) | 170 | while ((ch = getopt(argc, argv, "bf:")) != -1) { |
| 178 | switch(ch) { | 171 | switch (ch) { |
| 179 | case 'b': | 172 | case 'b': |
| 180 | bflag = 1; | 173 | bflag = 1; |
| 181 | break; | 174 | break; |
| @@ -189,31 +182,76 @@ while ((ch = getopt(argc, argv, "bf:")) != -1) | |||
| 189 | case '?': | 182 | case '?': |
| 190 | default: | 183 | default: |
| 191 | usage(); | 184 | usage(); |
| 185 | } | ||
| 192 | } | 186 | } |
| 193 | argc -= optind; | 187 | argc -= optind; |
| 194 | argv += optind; | 188 | argv += optind; |
| 195 | .Ed | 189 | .Ed |
| 196 | .Sh HISTORY | 190 | .Sh DIAGNOSTICS |
| 197 | The | 191 | If the |
| 198 | .Fn getopt | 192 | .Fn getopt |
| 199 | function appeared | 193 | function encounters a character not found in the string |
| 200 | .Bx 4.3 . | 194 | .Fa optstring |
| 201 | .Sh BUGS | 195 | or detects |
| 196 | a missing option argument it writes an error message to | ||
| 197 | .Em stderr | ||
| 198 | and returns | ||
| 199 | .Ql \&? . | ||
| 200 | Setting | ||
| 201 | .Va opterr | ||
| 202 | to a zero will disable these error messages. | ||
| 203 | If | ||
| 204 | .Fa optstring | ||
| 205 | has a leading | ||
| 206 | .Ql \&: | ||
| 207 | then a missing option argument causes a | ||
| 208 | .Ql \&: | ||
| 209 | to be returned in addition to suppressing any error messages. | ||
| 210 | .Pp | ||
| 211 | Option arguments are allowed to begin with | ||
| 212 | .Ql - ; | ||
| 213 | this is reasonable but reduces the amount of error checking possible. | ||
| 214 | .Sh SEE ALSO | ||
| 215 | .Xr getopt 1 , | ||
| 216 | .Xr getopt_long 3 , | ||
| 217 | .Xr getsubopt 3 | ||
| 218 | .Sh STANDARDS | ||
| 202 | The | 219 | The |
| 203 | .Fn getopt | 220 | .Fn getopt |
| 204 | function was once specified to return | 221 | function implements a superset of the functionality specified by |
| 205 | .Dv EOF | 222 | .St -p1003.1 . |
| 206 | instead of \-1. | ||
| 207 | This was changed by | ||
| 208 | .St -p1003.2-92 | ||
| 209 | to decouple | ||
| 210 | .Fn getopt | ||
| 211 | from | ||
| 212 | .Pa <stdio.h> . | ||
| 213 | .Pp | 223 | .Pp |
| 224 | The following extensions are supported: | ||
| 225 | .Bl -tag -width "xxx" | ||
| 226 | .It Li o | ||
| 227 | The | ||
| 228 | .Va optreset | ||
| 229 | variable was added to make it possible to call the | ||
| 230 | .Fn getopt | ||
| 231 | function multiple times. | ||
| 232 | .It Li o | ||
| 233 | If the first character of | ||
| 234 | .Fa optstring | ||
| 235 | is a plus sign | ||
| 236 | .Pq Ql + , | ||
| 237 | it will be ignored. | ||
| 238 | This is for compatibility with | ||
| 239 | .Tn GNU | ||
| 240 | .Fn getopt . | ||
| 241 | .It Li o | ||
| 242 | If the first character of | ||
| 243 | .Fa optstring | ||
| 244 | is a dash | ||
| 245 | .Pq Ql - , | ||
| 246 | non-options will be returned as arguments to the option character | ||
| 247 | .Ql \e1 . | ||
| 248 | This is for compatibility with | ||
| 249 | .Tn GNU | ||
| 250 | .Fn getopt . | ||
| 251 | .It Li o | ||
| 214 | A single dash | 252 | A single dash |
| 215 | .Dq Li - | 253 | .Pq Ql - |
| 216 | may be specified as an character in | 254 | may be specified as a character in |
| 217 | .Fa optstring , | 255 | .Fa optstring , |
| 218 | however it should | 256 | however it should |
| 219 | .Em never | 257 | .Em never |
| @@ -221,39 +259,81 @@ have an argument associated with it. | |||
| 221 | This allows | 259 | This allows |
| 222 | .Fn getopt | 260 | .Fn getopt |
| 223 | to be used with programs that expect | 261 | to be used with programs that expect |
| 224 | .Dq Li - | 262 | .Ql - |
| 225 | as an option flag. | 263 | as an option flag. |
| 226 | This practice is wrong, and should not be used in any current development. | 264 | This practice is wrong, and should not be used in any current development. |
| 227 | It is provided for backward compatibility | 265 | It is provided for backward compatibility |
| 228 | .Em only . | 266 | .Em only . |
| 267 | Care should be taken not to use | ||
| 268 | .Ql - | ||
| 269 | as the first character in | ||
| 270 | .Fa optstring | ||
| 271 | to avoid a semantic conflict with | ||
| 272 | .Tn GNU | ||
| 273 | .Fn getopt | ||
| 274 | semantics (see above). | ||
| 229 | By default, a single dash causes | 275 | By default, a single dash causes |
| 230 | .Fn getopt | 276 | .Fn getopt |
| 231 | to return \-1. | 277 | to return \-1. |
| 232 | This is, we believe, compatible with System V. | 278 | .El |
| 279 | .Pp | ||
| 280 | Unlike | ||
| 281 | .Tn GNU | ||
| 282 | .Fn getopt , | ||
| 283 | .Ox | ||
| 284 | does not permute the argument vector to allow non-options to be | ||
| 285 | interspersed with options on the command line. | ||
| 286 | Programs requiring this behavior should use | ||
| 287 | .Xr getopt_long 3 | ||
| 288 | instead. | ||
| 289 | Because of this (and unlike | ||
| 290 | .Tn GNU ) , | ||
| 291 | the | ||
| 292 | .Ox | ||
| 293 | .Fn getopt | ||
| 294 | supports optional arguments separated by whitespace. | ||
| 295 | .Sh HISTORY | ||
| 296 | The | ||
| 297 | .Fn getopt | ||
| 298 | function appeared in | ||
| 299 | .Bx 4.3 . | ||
| 300 | .Sh BUGS | ||
| 301 | The | ||
| 302 | .Fn getopt | ||
| 303 | function was once specified to return | ||
| 304 | .Dv EOF | ||
| 305 | instead of \-1. | ||
| 306 | This was changed by | ||
| 307 | .St -p1003.2-92 | ||
| 308 | to decouple | ||
| 309 | .Fn getopt | ||
| 310 | from | ||
| 311 | .Aq Pa stdio.h . | ||
| 233 | .Pp | 312 | .Pp |
| 234 | It is also possible to handle digits as option letters. | 313 | It is possible to handle digits as option letters. |
| 235 | This allows | 314 | This allows |
| 236 | .Fn getopt | 315 | .Fn getopt |
| 237 | to be used with programs that expect a number | 316 | to be used with programs that expect a number |
| 238 | .Pq Dq Li \&-\&3 | 317 | .Pq Dq Li \-3 |
| 239 | as an option. | 318 | as an option. |
| 240 | This practice is wrong, and should not be used in any current development. | 319 | This practice is wrong, and should not be used in any current development. |
| 241 | It is provided for backward compatibility | 320 | It is provided for backward compatibility |
| 242 | .Em only . | 321 | .Em only . |
| 243 | The following code fragment works in most cases. | 322 | The following code fragment works in most cases. |
| 244 | .Bd -literal -offset indent | 323 | .Bd -literal -offset indent |
| 245 | int length; | 324 | int ch; |
| 325 | long length; | ||
| 246 | char *p; | 326 | char *p; |
| 247 | 327 | ||
| 248 | while ((c = getopt(argc, argv, "0123456789")) != -1) | 328 | while ((ch = getopt(argc, argv, "0123456789")) != -1) { |
| 249 | switch (c) { | 329 | switch (ch) { |
| 250 | case '0': case '1': case '2': case '3': case '4': | 330 | case '0': case '1': case '2': case '3': case '4': |
| 251 | case '5': case '6': case '7': case '8': case '9': | 331 | case '5': case '6': case '7': case '8': case '9': |
| 252 | p = argv[optind - 1]; | 332 | p = argv[optind - 1]; |
| 253 | if (p[0] == '-' && p[1] == ch && !p[2]) | 333 | if (p[0] == '-' && p[1] == ch && !p[2]) |
| 254 | length = atoi(++p); | 334 | length = ch - '0'; |
| 255 | else | 335 | else |
| 256 | length = atoi(argv[optind] + 1); | 336 | length = strtol(argv[optind] + 1, NULL, 10); |
| 257 | break; | 337 | break; |
| 258 | } | 338 | } |
| 259 | } | 339 | } |
