comparison fluid-mnf/getopt.c @ 311:9cecc930d78f

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