diff options
Diffstat (limited to 'src/lib/libc/stdlib/getopt_long.3')
| -rw-r--r-- | src/lib/libc/stdlib/getopt_long.3 | 464 |
1 files changed, 464 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..2bab963734 --- /dev/null +++ b/src/lib/libc/stdlib/getopt_long.3 | |||
| @@ -0,0 +1,464 @@ | |||
| 1 | .\" $OpenBSD: getopt_long.3,v 1.15 2007/07/03 12:06:07 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 $Mdocdate: July 3 2007 $ | ||
| 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 *longindex" | ||
| 49 | .Ft int | ||
| 50 | .Fn getopt_long_only "int argc" "char * const *argv" "const char *optstring" "const struct option *longopts" "int *longindex" | ||
| 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 -offset indent | ||
| 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 | Abbreviated long option names are accepted when | ||
| 97 | .Fn getopt_long | ||
| 98 | processes long options if the abbreviation is unique. | ||
| 99 | An exact match is always preferred for a defined long option. | ||
| 100 | .Pp | ||
| 101 | The | ||
| 102 | .Fn getopt_long | ||
| 103 | call requires a structure to be initialized describing the long | ||
| 104 | options. | ||
| 105 | The structure is: | ||
| 106 | .Bd -literal -offset indent | ||
| 107 | struct option { | ||
| 108 | char *name; | ||
| 109 | int has_arg; | ||
| 110 | int *flag; | ||
| 111 | int val; | ||
| 112 | }; | ||
| 113 | .Ed | ||
| 114 | .Pp | ||
| 115 | The | ||
| 116 | .Fa name | ||
| 117 | field should contain the option name without the leading double dash. | ||
| 118 | .Pp | ||
| 119 | The | ||
| 120 | .Fa has_arg | ||
| 121 | field should be one of: | ||
| 122 | .Pp | ||
| 123 | .Bl -tag -width "optional_argument" -compact -offset indent | ||
| 124 | .It Dv no_argument | ||
| 125 | no argument to the option is expected. | ||
| 126 | .It Dv required_argument | ||
| 127 | an argument to the option is required. | ||
| 128 | .It Dv optional_argument | ||
| 129 | an argument to the option may be presented. | ||
| 130 | .El | ||
| 131 | .Pp | ||
| 132 | If | ||
| 133 | .Fa flag | ||
| 134 | is not | ||
| 135 | .Dv NULL , | ||
| 136 | then the integer pointed to by it will be set to the value in the | ||
| 137 | .Fa val | ||
| 138 | field. | ||
| 139 | If the | ||
| 140 | .Fa flag | ||
| 141 | field is | ||
| 142 | .Dv NULL , | ||
| 143 | then the | ||
| 144 | .Fa val | ||
| 145 | field will be returned. | ||
| 146 | Setting | ||
| 147 | .Fa flag | ||
| 148 | to | ||
| 149 | .Dv NULL | ||
| 150 | and setting | ||
| 151 | .Fa val | ||
| 152 | to the corresponding short option will make this function act just | ||
| 153 | like | ||
| 154 | .Xr getopt 3 . | ||
| 155 | .Pp | ||
| 156 | If the | ||
| 157 | .Fa longindex | ||
| 158 | field is not | ||
| 159 | .Dv NULL , | ||
| 160 | then the integer pointed to by it will be set to the index of the long | ||
| 161 | option relative to | ||
| 162 | .Fa longopts . | ||
| 163 | .Pp | ||
| 164 | The last element of the | ||
| 165 | .Fa longopts | ||
| 166 | array has to be filled with zeroes. | ||
| 167 | .Pp | ||
| 168 | The | ||
| 169 | .Fn getopt_long_only | ||
| 170 | function behaves identically to | ||
| 171 | .Fn getopt_long | ||
| 172 | with the exception that long options may start with | ||
| 173 | .Sq - | ||
| 174 | in addition to | ||
| 175 | .Sq -- . | ||
| 176 | If an option starting with | ||
| 177 | .Sq - | ||
| 178 | does not match a long option but does match a single-character option, | ||
| 179 | the single-character option is returned. | ||
| 180 | .Sh RETURN VALUES | ||
| 181 | If the | ||
| 182 | .Fa flag | ||
| 183 | field in | ||
| 184 | .Li struct option | ||
| 185 | is | ||
| 186 | .Dv NULL , | ||
| 187 | .Fn getopt_long | ||
| 188 | and | ||
| 189 | .Fn getopt_long_only | ||
| 190 | return the value specified in the | ||
| 191 | .Fa val | ||
| 192 | field, which is usually just the corresponding short option. | ||
| 193 | If | ||
| 194 | .Fa flag | ||
| 195 | is not | ||
| 196 | .Dv NULL , | ||
| 197 | these functions return 0 and store | ||
| 198 | .Fa val | ||
| 199 | in the location pointed to by | ||
| 200 | .Fa flag . | ||
| 201 | These functions return | ||
| 202 | .Sq \: | ||
| 203 | if there was a missing option argument, | ||
| 204 | .Sq \&? | ||
| 205 | if the user specified an unknown or ambiguous option, and | ||
| 206 | \-1 when the argument list has been exhausted. | ||
| 207 | .Sh EXAMPLES | ||
| 208 | .Bd -literal | ||
| 209 | int bflag, ch, fd; | ||
| 210 | int daggerset; | ||
| 211 | |||
| 212 | /* options descriptor */ | ||
| 213 | static struct option longopts[] = { | ||
| 214 | { "buffy", no_argument, NULL, 'b' }, | ||
| 215 | { "fluoride", required_argument, NULL, 'f' }, | ||
| 216 | { "daggerset", no_argument, &daggerset, 1 }, | ||
| 217 | { NULL, 0, NULL, 0 } | ||
| 218 | }; | ||
| 219 | |||
| 220 | bflag = 0; | ||
| 221 | while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1) | ||
| 222 | switch (ch) { | ||
| 223 | case 'b': | ||
| 224 | bflag = 1; | ||
| 225 | break; | ||
| 226 | case 'f': | ||
| 227 | if ((fd = open(optarg, O_RDONLY, 0)) == -1) | ||
| 228 | err(1, "unable to open %s", optarg); | ||
| 229 | break; | ||
| 230 | case 0: | ||
| 231 | if (daggerset) | ||
| 232 | fprintf(stderr, "Buffy will use her dagger to " | ||
| 233 | "apply fluoride to dracula's teeth\en"); | ||
| 234 | break; | ||
| 235 | default: | ||
| 236 | usage(); | ||
| 237 | /* NOTREACHED */ | ||
| 238 | } | ||
| 239 | argc -= optind; | ||
| 240 | argv += optind; | ||
| 241 | .Ed | ||
| 242 | .Sh IMPLEMENTATION DIFFERENCES | ||
| 243 | This section describes differences to the GNU implementation | ||
| 244 | found in glibc-2.1.3: | ||
| 245 | .Bl -bullet | ||
| 246 | .It | ||
| 247 | handling of | ||
| 248 | .Ql - | ||
| 249 | as the first character of the option string in the presence of the | ||
| 250 | environment variable | ||
| 251 | .Ev POSIXLY_CORRECT : | ||
| 252 | .Bl -tag -width "OpenBSD" | ||
| 253 | .It GNU | ||
| 254 | ignores | ||
| 255 | .Ev POSIXLY_CORRECT | ||
| 256 | and returns non-options as arguments to option | ||
| 257 | .Ql \e1 . | ||
| 258 | .It OpenBSD | ||
| 259 | honors | ||
| 260 | .Ev POSIXLY_CORRECT | ||
| 261 | and stops at the first non-option. | ||
| 262 | .El | ||
| 263 | .It | ||
| 264 | handling of | ||
| 265 | .Ql - | ||
| 266 | within the option string (not the first character): | ||
| 267 | .Bl -tag -width "OpenBSD" | ||
| 268 | .It GNU | ||
| 269 | treats a | ||
| 270 | .Ql - | ||
| 271 | on the command line as a non-argument. | ||
| 272 | .It OpenBSD | ||
| 273 | a | ||
| 274 | .Ql - | ||
| 275 | within the option string matches a | ||
| 276 | .Ql - | ||
| 277 | (single dash) on the command line. | ||
| 278 | This functionality is provided for backward compatibility with | ||
| 279 | programs, such as | ||
| 280 | .Xr su 1 , | ||
| 281 | that use | ||
| 282 | .Ql - | ||
| 283 | as an option flag. | ||
| 284 | This practice is wrong, and should not be used in any current development. | ||
| 285 | .El | ||
| 286 | .It | ||
| 287 | handling of | ||
| 288 | .Ql :: | ||
| 289 | in the option string in the presence of | ||
| 290 | .Ev POSIXLY_CORRECT : | ||
| 291 | .Bl -tag -width "OpenBSD" | ||
| 292 | .It Both | ||
| 293 | GNU and | ||
| 294 | .Ox | ||
| 295 | ignore | ||
| 296 | .Ev POSIXLY_CORRECT | ||
| 297 | here and take | ||
| 298 | .Ql :: | ||
| 299 | to mean the preceding option takes an optional argument. | ||
| 300 | .El | ||
| 301 | .It | ||
| 302 | return value in case of missing argument if first character | ||
| 303 | (after | ||
| 304 | .Ql + | ||
| 305 | or | ||
| 306 | .Ql - ) | ||
| 307 | in the option string is not | ||
| 308 | .Ql \&: : | ||
| 309 | .Bl -tag -width "OpenBSD" | ||
| 310 | .It GNU | ||
| 311 | returns | ||
| 312 | .Ql \&? | ||
| 313 | .It OpenBSD | ||
| 314 | returns | ||
| 315 | .Ql \&: | ||
| 316 | (since | ||
| 317 | .Ox Ns 's | ||
| 318 | .Xr getopt 3 | ||
| 319 | does). | ||
| 320 | .El | ||
| 321 | .It | ||
| 322 | handling of | ||
| 323 | .Ql --a | ||
| 324 | in | ||
| 325 | .Xr getopt 3 : | ||
| 326 | .Bl -tag -width "OpenBSD" | ||
| 327 | .It GNU | ||
| 328 | parses this as option | ||
| 329 | .Ql - , | ||
| 330 | option | ||
| 331 | .Ql a . | ||
| 332 | .It OpenBSD | ||
| 333 | parses this as | ||
| 334 | .Ql -- , | ||
| 335 | and returns \-1 (ignoring the | ||
| 336 | .Ql a ) | ||
| 337 | (because the original | ||
| 338 | .Fn getopt | ||
| 339 | did.) | ||
| 340 | .El | ||
| 341 | .It | ||
| 342 | setting of | ||
| 343 | .Va optopt | ||
| 344 | for long options with | ||
| 345 | .Va flag | ||
| 346 | .No non- Ns Dv NULL : | ||
| 347 | .Bl -tag -width "OpenBSD" | ||
| 348 | .It GNU | ||
| 349 | sets | ||
| 350 | .Va optopt | ||
| 351 | to | ||
| 352 | .Va val . | ||
| 353 | .It OpenBSD | ||
| 354 | sets | ||
| 355 | .Va optopt | ||
| 356 | to 0 (since | ||
| 357 | .Va val | ||
| 358 | would never be returned). | ||
| 359 | .El | ||
| 360 | .It | ||
| 361 | handling of | ||
| 362 | .Ql -W | ||
| 363 | with | ||
| 364 | .Ql W; | ||
| 365 | in the option string in | ||
| 366 | .Xr getopt 3 | ||
| 367 | (not | ||
| 368 | .Fn getopt_long ) : | ||
| 369 | .Bl -tag -width "OpenBSD" | ||
| 370 | .It GNU | ||
| 371 | causes a segmentation fault. | ||
| 372 | .It OpenBSD | ||
| 373 | no special handling is done; | ||
| 374 | .Ql W; | ||
| 375 | is interpreted as two separate options, neither of which take an argument. | ||
| 376 | .El | ||
| 377 | .It | ||
| 378 | setting of | ||
| 379 | .Va optarg | ||
| 380 | for long options without an argument that are invoked via | ||
| 381 | .Ql -W | ||
| 382 | (with | ||
| 383 | .Ql W; | ||
| 384 | in the option string): | ||
| 385 | .Bl -tag -width "OpenBSD" | ||
| 386 | .It GNU | ||
| 387 | sets | ||
| 388 | .Va optarg | ||
| 389 | to the option name (the argument of | ||
| 390 | .Ql -W ) . | ||
| 391 | .It OpenBSD | ||
| 392 | sets | ||
| 393 | .Va optarg | ||
| 394 | to | ||
| 395 | .Dv NULL | ||
| 396 | (the argument of the long option). | ||
| 397 | .El | ||
| 398 | .It | ||
| 399 | handling of | ||
| 400 | .Ql -W | ||
| 401 | with an argument that is not (a prefix to) a known long option | ||
| 402 | (with | ||
| 403 | .Ql W; | ||
| 404 | in the option string): | ||
| 405 | .Bl -tag -width "OpenBSD" | ||
| 406 | .It GNU | ||
| 407 | returns | ||
| 408 | .Ql -W | ||
| 409 | with | ||
| 410 | .Va optarg | ||
| 411 | set to the unknown option. | ||
| 412 | .It OpenBSD | ||
| 413 | treats this as an error (unknown option) and returns | ||
| 414 | .Ql \&? | ||
| 415 | with | ||
| 416 | .Va optopt | ||
| 417 | set to 0 and | ||
| 418 | .Va optarg | ||
| 419 | set to | ||
| 420 | .Dv NULL | ||
| 421 | (as GNU's man page documents). | ||
| 422 | .El | ||
| 423 | .It | ||
| 424 | The error messages are different. | ||
| 425 | .It | ||
| 426 | .Ox | ||
| 427 | does not permute the argument vector at the same points in | ||
| 428 | the calling sequence as GNU does. | ||
| 429 | The aspects normally used by the caller | ||
| 430 | (ordering after \-1 is returned, value of | ||
| 431 | .Va optind | ||
| 432 | relative to current positions) are the same, though. | ||
| 433 | (We do fewer variable swaps.) | ||
| 434 | .El | ||
| 435 | .Sh ENVIRONMENT | ||
| 436 | .Bl -tag -width Ev | ||
| 437 | .It Ev POSIXLY_CORRECT | ||
| 438 | If set, option processing stops when the first non-option is found and | ||
| 439 | a leading | ||
| 440 | .Sq - | ||
| 441 | or | ||
| 442 | .Sq + | ||
| 443 | in the | ||
| 444 | .Ar optstring | ||
| 445 | is ignored. | ||
| 446 | .El | ||
| 447 | .Sh SEE ALSO | ||
| 448 | .Xr getopt 3 | ||
| 449 | .Sh HISTORY | ||
| 450 | The | ||
| 451 | .Fn getopt_long | ||
| 452 | and | ||
| 453 | .Fn getopt_long_only | ||
| 454 | functions first appeared in GNU libiberty. | ||
| 455 | This implementation first appeared in | ||
| 456 | .Ox 3.3 . | ||
| 457 | .Sh BUGS | ||
| 458 | The | ||
| 459 | .Ar argv | ||
| 460 | argument is not really | ||
| 461 | .Dv const | ||
| 462 | as its elements may be permuted (unless | ||
| 463 | .Ev POSIXLY_CORRECT | ||
| 464 | is set). | ||
