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