Commit | Line | Data |
---|---|---|
3013fe88 NW |
1 | /* Getopt for GNU. |
2 | NOTE: getopt is now part of the C library, so if you don't know what | |
3 | "Keep this file name-space clean" means, talk to roland@gnu.ai.mit.edu | |
4 | before changing it! | |
5 | ||
6 | Copyright (C) 1987, 88, 89, 90, 91, 1992 Free Software Foundation, Inc. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by the | |
10 | Free Software Foundation; either version 2, or (at your option) any | |
11 | later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
21 | \f | |
22 | /* AIX requires this to be the first thing in the file. */ | |
23 | #ifdef __GNUC__ | |
24 | #define alloca __builtin_alloca | |
25 | #else /* not __GNUC__ */ | |
26 | #if defined (HAVE_ALLOCA_H) || (defined(sparc) && (defined(sun) || (!defined(USG) && !defined(SVR4) && !defined(__svr4__)))) | |
27 | #include <alloca.h> | |
28 | #else | |
29 | #ifdef _AIX | |
30 | #pragma alloca | |
31 | #else | |
32 | char *alloca (); | |
33 | #endif | |
34 | #endif /* alloca.h */ | |
35 | #endif /* not __GNUC__ */ | |
36 | ||
caed0dfe NW |
37 | /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>. */ |
38 | #ifndef _NO_PROTO | |
39 | # define _NO_PROTO | |
40 | #endif | |
41 | ||
3013fe88 NW |
42 | #include <stdio.h> |
43 | ||
44 | #if defined(USG) || defined(STDC_HEADERS) || defined(__GNU_LIBRARY__) | |
45 | #include <string.h> | |
46 | #endif | |
47 | ||
48 | /* This needs to come after some library #include | |
49 | to get __GNU_LIBRARY__ defined. */ | |
50 | #ifdef __GNU_LIBRARY__ | |
51 | #undef alloca | |
52 | /* Don't include stdlib.h for non-GNU C libraries because some of them | |
53 | contain conflicting prototypes for getopt. */ | |
54 | #include <stdlib.h> | |
55 | #else /* Not GNU C library. */ | |
56 | #define __alloca alloca | |
57 | #endif /* GNU C library. */ | |
58 | ||
59 | #if !__STDC__ | |
60 | #define const | |
61 | #endif | |
62 | ||
63 | /* If GETOPT_COMPAT is defined, `+' as well as `--' can introduce a | |
64 | long-named option. Because this is not POSIX.2 compliant, it is | |
65 | being phased out. */ | |
66 | #define GETOPT_COMPAT | |
67 | ||
68 | /* This version of `getopt' appears to the caller like standard Unix `getopt' | |
69 | but it behaves differently for the user, since it allows the user | |
70 | to intersperse the options with the other arguments. | |
71 | ||
72 | As `getopt' works, it permutes the elements of ARGV so that, | |
73 | when it is done, all the options precede everything else. Thus | |
74 | all application programs are extended to handle flexible argument order. | |
75 | ||
76 | Setting the environment variable POSIXLY_CORRECT disables permutation. | |
77 | Then the behavior is completely standard. | |
78 | ||
79 | GNU application programs can use a third alternative mode in which | |
80 | they can distinguish the relative order of options and other arguments. */ | |
81 | ||
82 | #include "getopt.h" | |
83 | ||
84 | /* For communication from `getopt' to the caller. | |
85 | When `getopt' finds an option that takes an argument, | |
86 | the argument value is returned here. | |
87 | Also, when `ordering' is RETURN_IN_ORDER, | |
88 | each non-option ARGV-element is returned here. */ | |
89 | ||
90 | char *optarg = 0; | |
91 | ||
92 | /* Index in ARGV of the next element to be scanned. | |
93 | This is used for communication to and from the caller | |
94 | and for communication between successive calls to `getopt'. | |
95 | ||
96 | On entry to `getopt', zero means this is the first call; initialize. | |
97 | ||
98 | When `getopt' returns EOF, this is the index of the first of the | |
99 | non-option elements that the caller should itself scan. | |
100 | ||
101 | Otherwise, `optind' communicates from one call to the next | |
102 | how much of ARGV has been scanned so far. */ | |
103 | ||
104 | int optind = 0; | |
105 | ||
106 | /* The next char to be scanned in the option-element | |
107 | in which the last option character we returned was found. | |
108 | This allows us to pick up the scan where we left off. | |
109 | ||
110 | If this is zero, or a null string, it means resume the scan | |
111 | by advancing to the next ARGV-element. */ | |
112 | ||
113 | static char *nextchar; | |
114 | ||
115 | /* Callers store zero here to inhibit the error message | |
116 | for unrecognized options. */ | |
117 | ||
118 | int opterr = 1; | |
119 | ||
120 | /* Describe how to deal with options that follow non-option ARGV-elements. | |
121 | ||
122 | If the caller did not specify anything, | |
123 | the default is REQUIRE_ORDER if the environment variable | |
124 | POSIXLY_CORRECT is defined, PERMUTE otherwise. | |
125 | ||
126 | REQUIRE_ORDER means don't recognize them as options; | |
127 | stop option processing when the first non-option is seen. | |
128 | This is what Unix does. | |
129 | This mode of operation is selected by either setting the environment | |
130 | variable POSIXLY_CORRECT, or using `+' as the first character | |
131 | of the list of option characters. | |
132 | ||
133 | PERMUTE is the default. We permute the contents of ARGV as we scan, | |
134 | so that eventually all the non-options are at the end. This allows options | |
135 | to be given in any order, even with programs that were not written to | |
136 | expect this. | |
137 | ||
138 | RETURN_IN_ORDER is an option available to programs that were written | |
139 | to expect options and other ARGV-elements in any order and that care about | |
140 | the ordering of the two. We describe each non-option ARGV-element | |
141 | as if it were the argument of an option with character code 1. | |
142 | Using `-' as the first character of the list of option characters | |
143 | selects this mode of operation. | |
144 | ||
145 | The special argument `--' forces an end of option-scanning regardless | |
146 | of the value of `ordering'. In the case of RETURN_IN_ORDER, only | |
147 | `--' can cause `getopt' to return EOF with `optind' != ARGC. */ | |
148 | ||
149 | static enum | |
150 | { | |
151 | REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER | |
152 | } ordering; | |
153 | \f | |
154 | #ifdef __GNU_LIBRARY__ | |
155 | #include <string.h> | |
156 | #define my_index strchr | |
157 | #define my_bcopy(src, dst, n) memcpy ((dst), (src), (n)) | |
158 | #else | |
159 | ||
160 | /* Avoid depending on library functions or files | |
161 | whose names are inconsistent. */ | |
162 | ||
163 | char *getenv (); | |
164 | ||
165 | static char * | |
166 | my_index (string, chr) | |
167 | char *string; | |
168 | int chr; | |
169 | { | |
170 | while (*string) | |
171 | { | |
172 | if (*string == chr) | |
173 | return string; | |
174 | string++; | |
175 | } | |
176 | return 0; | |
177 | } | |
178 | ||
179 | static void | |
180 | my_bcopy (from, to, size) | |
181 | char *from, *to; | |
182 | int size; | |
183 | { | |
184 | int i; | |
185 | for (i = 0; i < size; i++) | |
186 | to[i] = from[i]; | |
187 | } | |
188 | #endif /* GNU C library. */ | |
189 | \f | |
190 | /* Handle permutation of arguments. */ | |
191 | ||
192 | /* Describe the part of ARGV that contains non-options that have | |
193 | been skipped. `first_nonopt' is the index in ARGV of the first of them; | |
194 | `last_nonopt' is the index after the last of them. */ | |
195 | ||
196 | static int first_nonopt; | |
197 | static int last_nonopt; | |
198 | ||
199 | /* Exchange two adjacent subsequences of ARGV. | |
200 | One subsequence is elements [first_nonopt,last_nonopt) | |
201 | which contains all the non-options that have been skipped so far. | |
202 | The other is elements [last_nonopt,optind), which contains all | |
203 | the options processed since those non-options were skipped. | |
204 | ||
205 | `first_nonopt' and `last_nonopt' are relocated so that they describe | |
206 | the new indices of the non-options in ARGV after they are moved. */ | |
207 | ||
208 | static void | |
209 | exchange (argv) | |
210 | char **argv; | |
211 | { | |
212 | int nonopts_size = (last_nonopt - first_nonopt) * sizeof (char *); | |
213 | #ifdef _CRAY | |
214 | char *temp[last_nonopt - first_nonopt]; | |
215 | #else | |
216 | char **temp = (char **) __alloca (nonopts_size); | |
217 | #endif | |
218 | ||
219 | /* Interchange the two blocks of data in ARGV. */ | |
220 | ||
221 | my_bcopy ((char *) &argv[first_nonopt], (char *) temp, nonopts_size); | |
222 | my_bcopy ((char *) &argv[last_nonopt], (char *) &argv[first_nonopt], | |
223 | (optind - last_nonopt) * sizeof (char *)); | |
224 | my_bcopy ((char *) temp, | |
225 | (char *) &argv[first_nonopt + optind - last_nonopt], | |
226 | nonopts_size); | |
227 | ||
228 | /* Update records for the slots the non-options now occupy. */ | |
229 | ||
230 | first_nonopt += (optind - last_nonopt); | |
231 | last_nonopt = optind; | |
232 | } | |
233 | \f | |
234 | /* Scan elements of ARGV (whose length is ARGC) for option characters | |
235 | given in OPTSTRING. | |
236 | ||
237 | If an element of ARGV starts with '-', and is not exactly "-" or "--", | |
238 | then it is an option element. The characters of this element | |
239 | (aside from the initial '-') are option characters. If `getopt' | |
240 | is called repeatedly, it returns successively each of the option characters | |
241 | from each of the option elements. | |
242 | ||
243 | If `getopt' finds another option character, it returns that character, | |
244 | updating `optind' and `nextchar' so that the next call to `getopt' can | |
245 | resume the scan with the following option character or ARGV-element. | |
246 | ||
247 | If there are no more option characters, `getopt' returns `EOF'. | |
248 | Then `optind' is the index in ARGV of the first ARGV-element | |
249 | that is not an option. (The ARGV-elements have been permuted | |
250 | so that those that are not options now come last.) | |
251 | ||
252 | OPTSTRING is a string containing the legitimate option characters. | |
253 | If an option character is seen that is not listed in OPTSTRING, | |
254 | return '?' after printing an error message. If you set `opterr' to | |
255 | zero, the error message is suppressed but we still return '?'. | |
256 | ||
257 | If a char in OPTSTRING is followed by a colon, that means it wants an arg, | |
258 | so the following text in the same ARGV-element, or the text of the following | |
259 | ARGV-element, is returned in `optarg'. Two colons mean an option that | |
260 | wants an optional arg; if there is text in the current ARGV-element, | |
261 | it is returned in `optarg', otherwise `optarg' is set to zero. | |
262 | ||
263 | If OPTSTRING starts with `-' or `+', it requests different methods of | |
264 | handling the non-option ARGV-elements. | |
265 | See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above. | |
266 | ||
267 | Long-named options begin with `--' instead of `-'. | |
268 | Their names may be abbreviated as long as the abbreviation is unique | |
269 | or is an exact match for some defined option. If they have an | |
270 | argument, it follows the option name in the same ARGV-element, separated | |
271 | from the option name by a `=', or else the in next ARGV-element. | |
272 | When `getopt' finds a long-named option, it returns 0 if that option's | |
273 | `flag' field is nonzero, the value of the option's `val' field | |
274 | if the `flag' field is zero. | |
275 | ||
276 | The elements of ARGV aren't really const, because we permute them. | |
277 | But we pretend they're const in the prototype to be compatible | |
278 | with other systems. | |
279 | ||
280 | LONGOPTS is a vector of `struct option' terminated by an | |
281 | element containing a name which is zero. | |
282 | ||
283 | LONGIND returns the index in LONGOPT of the long-named option found. | |
284 | It is only valid when a long-named option has been found by the most | |
285 | recent call. | |
286 | ||
287 | If LONG_ONLY is nonzero, '-' as well as '--' can introduce | |
288 | long-named options. */ | |
289 | ||
290 | int | |
291 | _getopt_internal (argc, argv, optstring, longopts, longind, long_only) | |
292 | int argc; | |
293 | char *const *argv; | |
294 | const char *optstring; | |
295 | const struct option *longopts; | |
296 | int *longind; | |
297 | int long_only; | |
298 | { | |
299 | int option_index; | |
300 | ||
301 | optarg = 0; | |
302 | ||
303 | /* Initialize the internal data when the first call is made. | |
304 | Start processing options with ARGV-element 1 (since ARGV-element 0 | |
305 | is the program name); the sequence of previously skipped | |
306 | non-option ARGV-elements is empty. */ | |
307 | ||
308 | if (optind == 0) | |
309 | { | |
310 | first_nonopt = last_nonopt = optind = 1; | |
311 | ||
312 | nextchar = NULL; | |
313 | ||
314 | /* Determine how to handle the ordering of options and nonoptions. */ | |
315 | ||
316 | if (optstring[0] == '-') | |
317 | { | |
318 | ordering = RETURN_IN_ORDER; | |
319 | ++optstring; | |
320 | } | |
321 | else if (optstring[0] == '+') | |
322 | { | |
323 | ordering = REQUIRE_ORDER; | |
324 | ++optstring; | |
325 | } | |
326 | else if (getenv ("POSIXLY_CORRECT") != NULL) | |
327 | ordering = REQUIRE_ORDER; | |
328 | else | |
329 | ordering = PERMUTE; | |
330 | } | |
331 | ||
332 | if (nextchar == NULL || *nextchar == '\0') | |
333 | { | |
334 | if (ordering == PERMUTE) | |
335 | { | |
336 | /* If we have just processed some options following some non-options, | |
337 | exchange them so that the options come first. */ | |
338 | ||
339 | if (first_nonopt != last_nonopt && last_nonopt != optind) | |
340 | exchange ((char **) argv); | |
341 | else if (last_nonopt != optind) | |
342 | first_nonopt = optind; | |
343 | ||
344 | /* Now skip any additional non-options | |
345 | and extend the range of non-options previously skipped. */ | |
346 | ||
347 | while (optind < argc | |
348 | && (argv[optind][0] != '-' || argv[optind][1] == '\0') | |
349 | #ifdef GETOPT_COMPAT | |
350 | && (longopts == NULL | |
351 | || argv[optind][0] != '+' || argv[optind][1] == '\0') | |
352 | #endif /* GETOPT_COMPAT */ | |
353 | ) | |
354 | optind++; | |
355 | last_nonopt = optind; | |
356 | } | |
357 | ||
358 | /* Special ARGV-element `--' means premature end of options. | |
359 | Skip it like a null option, | |
360 | then exchange with previous non-options as if it were an option, | |
361 | then skip everything else like a non-option. */ | |
362 | ||
363 | if (optind != argc && !strcmp (argv[optind], "--")) | |
364 | { | |
365 | optind++; | |
366 | ||
367 | if (first_nonopt != last_nonopt && last_nonopt != optind) | |
368 | exchange ((char **) argv); | |
369 | else if (first_nonopt == last_nonopt) | |
370 | first_nonopt = optind; | |
371 | last_nonopt = argc; | |
372 | ||
373 | optind = argc; | |
374 | } | |
375 | ||
376 | /* If we have done all the ARGV-elements, stop the scan | |
377 | and back over any non-options that we skipped and permuted. */ | |
378 | ||
379 | if (optind == argc) | |
380 | { | |
381 | /* Set the next-arg-index to point at the non-options | |
382 | that we previously skipped, so the caller will digest them. */ | |
383 | if (first_nonopt != last_nonopt) | |
384 | optind = first_nonopt; | |
385 | return EOF; | |
386 | } | |
387 | ||
388 | /* If we have come to a non-option and did not permute it, | |
389 | either stop the scan or describe it to the caller and pass it by. */ | |
390 | ||
391 | if ((argv[optind][0] != '-' || argv[optind][1] == '\0') | |
392 | #ifdef GETOPT_COMPAT | |
393 | && (longopts == NULL | |
394 | || argv[optind][0] != '+' || argv[optind][1] == '\0') | |
395 | #endif /* GETOPT_COMPAT */ | |
396 | ) | |
397 | { | |
398 | if (ordering == REQUIRE_ORDER) | |
399 | return EOF; | |
400 | optarg = argv[optind++]; | |
401 | return 1; | |
402 | } | |
403 | ||
404 | /* We have found another option-ARGV-element. | |
405 | Start decoding its characters. */ | |
406 | ||
407 | nextchar = (argv[optind] + 1 | |
408 | + (longopts != NULL && argv[optind][1] == '-')); | |
409 | } | |
410 | ||
411 | if (longopts != NULL | |
412 | && ((argv[optind][0] == '-' | |
413 | && (argv[optind][1] == '-' || long_only)) | |
414 | #ifdef GETOPT_COMPAT | |
415 | || argv[optind][0] == '+' | |
416 | #endif /* GETOPT_COMPAT */ | |
417 | )) | |
418 | { | |
419 | const struct option *p; | |
420 | char *s = nextchar; | |
421 | int exact = 0; | |
422 | int ambig = 0; | |
423 | const struct option *pfound = NULL; | |
424 | int indfound = 0; | |
425 | ||
426 | while (*s && *s != '=') | |
427 | s++; | |
428 | ||
429 | /* Test all options for either exact match or abbreviated matches. */ | |
430 | for (p = longopts, option_index = 0; p->name; | |
431 | p++, option_index++) | |
432 | if (!strncmp (p->name, nextchar, s - nextchar)) | |
433 | { | |
434 | if (s - nextchar == strlen (p->name)) | |
435 | { | |
436 | /* Exact match found. */ | |
437 | pfound = p; | |
438 | indfound = option_index; | |
439 | exact = 1; | |
440 | break; | |
441 | } | |
442 | else if (pfound == NULL) | |
443 | { | |
444 | /* First nonexact match found. */ | |
445 | pfound = p; | |
446 | indfound = option_index; | |
447 | } | |
448 | else | |
449 | /* Second nonexact match found. */ | |
450 | ambig = 1; | |
451 | } | |
452 | ||
453 | if (ambig && !exact) | |
454 | { | |
455 | if (opterr) | |
456 | fprintf (stderr, "%s: option `%s' is ambiguous\n", | |
457 | argv[0], argv[optind]); | |
458 | nextchar += strlen (nextchar); | |
459 | optind++; | |
460 | return '?'; | |
461 | } | |
462 | ||
463 | if (pfound != NULL) | |
464 | { | |
465 | option_index = indfound; | |
466 | optind++; | |
467 | if (*s) | |
468 | { | |
469 | /* Don't test has_arg with >, because some C compilers don't | |
470 | allow it to be used on enums. */ | |
471 | if (pfound->has_arg) | |
472 | optarg = s + 1; | |
473 | else | |
474 | { | |
475 | if (opterr) | |
476 | { | |
477 | if (argv[optind - 1][1] == '-') | |
478 | /* --option */ | |
479 | fprintf (stderr, | |
480 | "%s: option `--%s' doesn't allow an argument\n", | |
481 | argv[0], pfound->name); | |
482 | else | |
483 | /* +option or -option */ | |
484 | fprintf (stderr, | |
485 | "%s: option `%c%s' doesn't allow an argument\n", | |
486 | argv[0], argv[optind - 1][0], pfound->name); | |
487 | } | |
488 | nextchar += strlen (nextchar); | |
489 | return '?'; | |
490 | } | |
491 | } | |
492 | else if (pfound->has_arg == 1) | |
493 | { | |
494 | if (optind < argc) | |
495 | optarg = argv[optind++]; | |
496 | else | |
497 | { | |
498 | if (opterr) | |
499 | fprintf (stderr, "%s: option `%s' requires an argument\n", | |
500 | argv[0], argv[optind - 1]); | |
501 | nextchar += strlen (nextchar); | |
502 | return '?'; | |
503 | } | |
504 | } | |
505 | nextchar += strlen (nextchar); | |
506 | if (longind != NULL) | |
507 | *longind = option_index; | |
508 | if (pfound->flag) | |
509 | { | |
510 | *(pfound->flag) = pfound->val; | |
511 | return 0; | |
512 | } | |
513 | return pfound->val; | |
514 | } | |
515 | /* Can't find it as a long option. If this is not getopt_long_only, | |
516 | or the option starts with '--' or is not a valid short | |
517 | option, then it's an error. | |
518 | Otherwise interpret it as a short option. */ | |
519 | if (!long_only || argv[optind][1] == '-' | |
520 | #ifdef GETOPT_COMPAT | |
521 | || argv[optind][0] == '+' | |
522 | #endif /* GETOPT_COMPAT */ | |
523 | || my_index ((char*)optstring, *nextchar) == NULL) | |
524 | { | |
525 | if (opterr) | |
526 | { | |
527 | if (argv[optind][1] == '-') | |
528 | /* --option */ | |
529 | fprintf (stderr, "%s: unrecognized option `--%s'\n", | |
530 | argv[0], nextchar); | |
531 | else | |
532 | /* +option or -option */ | |
533 | fprintf (stderr, "%s: unrecognized option `%c%s'\n", | |
534 | argv[0], argv[optind][0], nextchar); | |
535 | } | |
536 | nextchar = (char *) ""; | |
537 | optind++; | |
538 | return '?'; | |
539 | } | |
540 | } | |
541 | ||
542 | /* Look at and handle the next option-character. */ | |
543 | ||
544 | { | |
545 | char c = *nextchar++; | |
546 | char *temp = my_index ((char*)optstring, c); | |
547 | ||
548 | /* Increment `optind' when we start to process its last character. */ | |
549 | if (*nextchar == '\0') | |
550 | ++optind; | |
551 | ||
552 | if (temp == NULL || c == ':') | |
553 | { | |
554 | if (opterr) | |
555 | { | |
556 | if (c < 040 || c >= 0177) | |
557 | fprintf (stderr, "%s: unrecognized option, character code 0%o\n", | |
558 | argv[0], c); | |
559 | else | |
560 | fprintf (stderr, "%s: unrecognized option `-%c'\n", argv[0], c); | |
561 | } | |
562 | return '?'; | |
563 | } | |
564 | if (temp[1] == ':') | |
565 | { | |
566 | if (temp[2] == ':') | |
567 | { | |
568 | /* This is an option that accepts an argument optionally. */ | |
569 | if (*nextchar != '\0') | |
570 | { | |
571 | optarg = nextchar; | |
572 | optind++; | |
573 | } | |
574 | else | |
575 | optarg = 0; | |
576 | nextchar = NULL; | |
577 | } | |
578 | else | |
579 | { | |
580 | /* This is an option that requires an argument. */ | |
581 | if (*nextchar != '\0') | |
582 | { | |
583 | optarg = nextchar; | |
584 | /* If we end this ARGV-element by taking the rest as an arg, | |
585 | we must advance to the next element now. */ | |
586 | optind++; | |
587 | } | |
588 | else if (optind == argc) | |
589 | { | |
590 | if (opterr) | |
591 | fprintf (stderr, "%s: option `-%c' requires an argument\n", | |
592 | argv[0], c); | |
593 | c = '?'; | |
594 | } | |
595 | else | |
596 | /* We already incremented `optind' once; | |
597 | increment it again when taking next ARGV-elt as argument. */ | |
598 | optarg = argv[optind++]; | |
599 | nextchar = NULL; | |
600 | } | |
601 | } | |
602 | return c; | |
603 | } | |
604 | } | |
605 | ||
606 | int | |
607 | getopt (argc, argv, optstring) | |
608 | int argc; | |
609 | char *const *argv; | |
610 | const char *optstring; | |
611 | { | |
612 | return _getopt_internal (argc, argv, optstring, | |
613 | (const struct option *) 0, | |
614 | (int *) 0, | |
615 | 0); | |
616 | } | |
617 | ||
618 | int | |
619 | getopt_long (argc, argv, options, long_options, opt_index) | |
620 | int argc; | |
621 | char *const *argv; | |
622 | const char *options; | |
623 | const struct option *long_options; | |
624 | int *opt_index; | |
625 | { | |
626 | return _getopt_internal (argc, argv, options, long_options, opt_index, 0); | |
627 | } | |
628 | ||
629 | /* Like getopt_long, but '-' as well as '--' can indicate a long option. | |
630 | If an option that starts with '-' (not '--') doesn't match a long option, | |
631 | but does match a short option, it is parsed as a short option | |
632 | instead. */ | |
633 | ||
634 | int | |
635 | getopt_long_only (argc, argv, options, long_options, opt_index) | |
636 | int argc; | |
637 | char *const *argv; | |
638 | const char *options; | |
639 | const struct option *long_options; | |
640 | int *opt_index; | |
641 | { | |
642 | return _getopt_internal (argc, argv, options, long_options, opt_index, 1); | |
643 | } | |
644 | ||
645 | \f | |
646 | #ifdef TEST | |
647 | ||
648 | /* Compile with -DTEST to make an executable for use in testing | |
649 | the above definition of `getopt'. */ | |
650 | ||
651 | int | |
652 | main (argc, argv) | |
653 | int argc; | |
654 | char **argv; | |
655 | { | |
656 | int c; | |
657 | int digit_optind = 0; | |
658 | ||
659 | while (1) | |
660 | { | |
661 | int this_option_optind = optind ? optind : 1; | |
662 | ||
663 | c = getopt (argc, argv, "abc:d:0123456789"); | |
664 | if (c == EOF) | |
665 | break; | |
666 | ||
667 | switch (c) | |
668 | { | |
669 | case '0': | |
670 | case '1': | |
671 | case '2': | |
672 | case '3': | |
673 | case '4': | |
674 | case '5': | |
675 | case '6': | |
676 | case '7': | |
677 | case '8': | |
678 | case '9': | |
679 | if (digit_optind != 0 && digit_optind != this_option_optind) | |
680 | printf ("digits occur in two different argv-elements.\n"); | |
681 | digit_optind = this_option_optind; | |
682 | printf ("option %c\n", c); | |
683 | break; | |
684 | ||
685 | case 'a': | |
686 | printf ("option a\n"); | |
687 | break; | |
688 | ||
689 | case 'b': | |
690 | printf ("option b\n"); | |
691 | break; | |
692 | ||
693 | case 'c': | |
694 | printf ("option c with value `%s'\n", optarg); | |
695 | break; | |
696 | ||
697 | case '?': | |
698 | break; | |
699 | ||
700 | default: | |
701 | printf ("?? getopt returned character code 0%o ??\n", c); | |
702 | } | |
703 | } | |
704 | ||
705 | if (optind < argc) | |
706 | { | |
707 | printf ("non-option ARGV-elements: "); | |
708 | while (optind < argc) | |
709 | printf ("%s ", argv[optind++]); | |
710 | printf ("\n"); | |
711 | } | |
712 | ||
713 | exit (0); | |
714 | } | |
715 | ||
716 | #endif /* TEST */ |