1 /* cgetopt.cc provide access to the C getopt library.
3 Copyright (C) 2009-2023 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
34 #define EXPORT(FUNC) m2pim ## _cgetopt_ ## FUNC
35 #define M2EXPORT(FUNC) m2pim ## _M2_cgetopt_ ## FUNC
36 #define M2LIBNAME "m2pim"
42 extern "C" {char *EXPORT(optarg
);}
43 extern "C" {int EXPORT(optind
);}
44 extern "C" {int EXPORT(opterr
);}
45 extern "C" {int EXPORT(optopt
);}
48 libiberty_getopt_long_only (int argc
, char *const *argv
, const char *options
,
49 const struct option
*long_options
, int *opt_index
);
51 libiberty_getopt_long (int argc
, char *const *argv
, const char *options
,
52 const struct option
*long_options
, int *opt_index
);
55 typedef struct cgetopt_Options_s
58 unsigned int empty_slot
;
61 /* InitOptions a constructor for Options. */
63 extern "C" cgetopt_Options
*
64 EXPORT(InitOptions
) (void)
66 cgetopt_Options
*o
= (cgetopt_Options
*)malloc (sizeof (cgetopt_Options
));
67 o
->cinfo
= (struct option
*)malloc (sizeof (struct option
));
72 /* KillOptions a deconstructor for Options. Returns NULL after freeing
73 up all allocated memory associated with o. */
75 extern "C" cgetopt_Options
*
76 EXPORT(KillOptions
) (cgetopt_Options
*o
)
83 /* SetOption set option[index] with {name, has_arg, flag, val}. */
86 EXPORT(SetOption
) (cgetopt_Options
*o
, unsigned int index
, char *name
,
87 int has_arg
, int *flag
, int val
)
89 if (index
>= o
->empty_slot
)
92 = (struct option
*)realloc (o
->cinfo
,
93 sizeof (struct option
) * (index
+ 1));
94 o
->empty_slot
= index
+ 1;
96 o
->cinfo
[index
].name
= name
;
97 o
->cinfo
[index
].has_arg
= has_arg
;
98 /* Set flag to NULL if name is NULL, as flag comes from a VAR parameter
99 (which cannot be NIL in modula-2). */
102 o
->cinfo
[index
].flag
= flag
;
103 o
->cinfo
[index
].val
= val
;
106 /* GetLongOptionArray returns a pointer to the C array containing all
109 extern "C" struct option
*
110 EXPORT(GetLongOptionArray
) (cgetopt_Options
*o
)
116 /* The following code has been taken from libiberty/getopt.c and is
117 conditionally compiled if long option support is absent on the target.
118 It has been changed just to deal with the long option functions as we
119 assume all targets have getopt. This code also keeps all data and code
120 static as the interface code only comes though functions defined in
123 #if (! defined(HAVE_GETOPT_LONG_ONLY)) || (! defined(HAVE_GETOPT_LONG))
124 /* For communication from `getopt' to the caller.
125 When `getopt' finds an option that takes an argument,
126 the argument value is returned here.
127 Also, when `ordering' is RETURN_IN_ORDER,
128 each non-option ARGV-element is returned here. */
130 static char *cgetopt_optarg
= NULL
;
132 /* Index in ARGV of the next element to be scanned.
133 This is used for communication to and from the caller
134 and for communication between successive calls to `getopt'.
136 On entry to `getopt', zero means this is the first call; initialize.
138 When `getopt' returns -1, this is the index of the first of the
139 non-option elements that the caller should itself scan.
141 Otherwise, `optind' communicates from one call to the next
142 how much of ARGV has been scanned so far. */
144 /* 1003.2 says this must be 1 before any call. */
145 static int cgetopt_optind
= 1;
147 /* Formerly, initialization of getopt depended on optind==0, which
148 causes problems with re-calling getopt as programs generally don't
151 static int __getopt_initialized
= 0;
153 /* The next char to be scanned in the option-element
154 in which the last option character we returned was found.
155 This allows us to pick up the scan where we left off.
157 If this is zero, or a null string, it means resume the scan
158 by advancing to the next ARGV-element. */
160 static char *nextchar
;
162 /* Callers store zero here to inhibit the error message
163 for unrecognized options. */
165 static int cgetopt_opterr
= 1;
167 /* Set to an option character which was unrecognized.
168 This must be initialized on some systems to avoid linking in the
169 system's own getopt implementation. */
171 static int cgetopt_optopt
= '?';
173 /* Describe how to deal with options that follow non-option ARGV-elements.
175 If the caller did not specify anything,
176 the default is REQUIRE_ORDER if the environment variable
177 POSIXLY_CORRECT is defined, PERMUTE otherwise.
179 REQUIRE_ORDER means don't recognize them as options;
180 stop option processing when the first non-option is seen.
181 This is what Unix does.
182 This mode of operation is selected by either setting the environment
183 variable POSIXLY_CORRECT, or using `+' as the first character
184 of the list of option characters.
186 PERMUTE is the default. We permute the contents of ARGV as we scan,
187 so that eventually all the non-options are at the end. This allows options
188 to be given in any order, even with programs that were not written to
191 RETURN_IN_ORDER is an option available to programs that were written
192 to expect options and other ARGV-elements in any order and that care about
193 the ordering of the two. We describe each non-option ARGV-element
194 as if it were the argument of an option with character code 1.
195 Using `-' as the first character of the list of option characters
196 selects this mode of operation.
198 The special argument `--' forces an end of option-scanning regardless
199 of the value of `ordering'. In the case of RETURN_IN_ORDER, only
200 `--' can cause `getopt' to return -1 with `optind' != ARGC. */
204 REQUIRE_ORDER
, PERMUTE
, RETURN_IN_ORDER
207 /* Value of POSIXLY_CORRECT environment variable. */
208 static char *posixly_correct
;
210 #ifdef __GNU_LIBRARY__
211 /* We want to avoid inclusion of string.h with non-GNU libraries
212 because there are many ways it can cause trouble.
213 On some systems, it contains special magic macros that don't work
216 # define my_index strchr
223 # include <strings.h>
227 /* Avoid depending on library functions or files
228 whose names are inconsistent. */
230 #if HAVE_STDLIB_H && HAVE_DECL_GETENV
232 #elif !defined(getenv)
235 # endif /* __cplusplus */
236 extern char *getenv (const char *);
239 # endif /* __cplusplus */
243 my_index (const char *str
, int chr
)
254 /* If using GCC, we can safely declare strlen this way.
255 If not using GCC, it is ok not to declare it. */
257 /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
258 That was relevant to code that was here before. */
259 # if (!defined __STDC__ || !__STDC__) && !defined strlen
260 /* gcc with -traditional declares the built-in strlen to return int,
261 and has done so at least since version 2.4.5. -- rms. */
262 extern int strlen (const char *);
263 # endif /* not __STDC__ */
264 #endif /* __GNUC__ */
266 #endif /* not __GNU_LIBRARY__ */
268 /* Handle permutation of arguments. */
270 /* Describe the part of ARGV that contains non-options that have
271 been skipped. `first_nonopt' is the index in ARGV of the first of them;
272 `last_nonopt' is the index after the last of them. */
274 static int first_nonopt
;
275 static int last_nonopt
;
278 /* Bash 2.0 gives us an environment variable containing flags
279 indicating ARGV elements that should not be considered arguments. */
281 /* Defined in getopt_init.c */
282 extern char *__getopt_nonoption_flags
;
284 static int nonoption_flags_max_len
;
285 static int nonoption_flags_len
;
287 static int original_argc
;
288 static char *const *original_argv
;
290 /* Make sure the environment variable bash 2.0 puts in the environment
291 is valid for the getopt call we must make sure that the ARGV passed
292 to getopt is that one passed to the process. */
294 __attribute__ ((unused
))
295 store_args_and_env (int argc
, char *const *argv
)
297 /* XXX This is no good solution. We should rather copy the args so
298 that we can compare them later. But we must not use malloc(3). */
299 original_argc
= argc
;
300 original_argv
= argv
;
302 # ifdef text_set_element
303 text_set_element (__libc_subinit
, store_args_and_env
);
304 # endif /* text_set_element */
306 # define SWAP_FLAGS(ch1, ch2) \
307 if (nonoption_flags_len > 0) \
309 char __tmp = __getopt_nonoption_flags[ch1]; \
310 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
311 __getopt_nonoption_flags[ch2] = __tmp; \
314 # define SWAP_FLAGS(ch1, ch2)
317 /* Exchange two adjacent subsequences of ARGV.
318 One subsequence is elements [first_nonopt,last_nonopt)
319 which contains all the non-options that have been skipped so far.
320 The other is elements [last_nonopt,optind), which contains all
321 the options processed since those non-options were skipped.
323 `first_nonopt' and `last_nonopt' are relocated so that they describe
324 the new indices of the non-options in ARGV after they are moved. */
326 #if defined __STDC__ && __STDC__
327 static void exchange (char **);
331 exchange (char **argv
)
333 int bottom
= first_nonopt
;
334 int middle
= last_nonopt
;
335 int top
= cgetopt_optind
;
338 /* Exchange the shorter segment with the far end of the longer segment.
339 That puts the shorter segment into the right place.
340 It leaves the longer segment in the right place overall,
341 but it consists of two parts that need to be swapped next. */
344 /* First make sure the handling of the `__getopt_nonoption_flags'
345 string can work normally. Our top argument must be in the range
347 if (nonoption_flags_len
> 0 && top
>= nonoption_flags_max_len
)
349 /* We must extend the array. The user plays games with us and
350 presents new arguments. */
351 char *new_str
= (char *) malloc (top
+ 1);
353 nonoption_flags_len
= nonoption_flags_max_len
= 0;
356 memset (mempcpy (new_str
, __getopt_nonoption_flags
,
357 nonoption_flags_max_len
),
358 '\0', top
+ 1 - nonoption_flags_max_len
);
359 nonoption_flags_max_len
= top
+ 1;
360 __getopt_nonoption_flags
= new_str
;
365 while (top
> middle
&& middle
> bottom
)
367 if (top
- middle
> middle
- bottom
)
369 /* Bottom segment is the short one. */
370 int len
= middle
- bottom
;
373 /* Swap it with the top part of the top segment. */
374 for (i
= 0; i
< len
; i
++)
376 tem
= argv
[bottom
+ i
];
377 argv
[bottom
+ i
] = argv
[top
- (middle
- bottom
) + i
];
378 argv
[top
- (middle
- bottom
) + i
] = tem
;
379 SWAP_FLAGS (bottom
+ i
, top
- (middle
- bottom
) + i
);
381 /* Exclude the moved bottom segment from further swapping. */
386 /* Top segment is the short one. */
387 int len
= top
- middle
;
390 /* Swap it with the bottom part of the bottom segment. */
391 for (i
= 0; i
< len
; i
++)
393 tem
= argv
[bottom
+ i
];
394 argv
[bottom
+ i
] = argv
[middle
+ i
];
395 argv
[middle
+ i
] = tem
;
396 SWAP_FLAGS (bottom
+ i
, middle
+ i
);
398 /* Exclude the moved top segment from further swapping. */
403 /* Update records for the slots the non-options now occupy. */
405 first_nonopt
+= (cgetopt_optind
- last_nonopt
);
406 last_nonopt
= cgetopt_optind
;
409 /* Initialize the internal data when the first call is made. */
411 #if defined __STDC__ && __STDC__
412 static const char *_getopt_initialize (int, char *const *, const char *);
415 _getopt_initialize (int argc
, char *const *argv
, const char *optstring
)
417 /* Start processing options with ARGV-element 1 (since ARGV-element 0
418 is the program name); the sequence of previously skipped
419 non-option ARGV-elements is empty. */
421 first_nonopt
= last_nonopt
= cgetopt_optind
;
425 posixly_correct
= getenv ("POSIXLY_CORRECT");
427 /* Determine how to handle the ordering of options and nonoptions. */
429 if (optstring
[0] == '-')
431 ordering
= RETURN_IN_ORDER
;
434 else if (optstring
[0] == '+')
436 ordering
= REQUIRE_ORDER
;
439 else if (posixly_correct
!= NULL
)
440 ordering
= REQUIRE_ORDER
;
445 if (posixly_correct
== NULL
446 && argc
== original_argc
&& argv
== original_argv
)
448 if (nonoption_flags_max_len
== 0)
450 if (__getopt_nonoption_flags
== NULL
451 || __getopt_nonoption_flags
[0] == '\0')
452 nonoption_flags_max_len
= -1;
455 const char *orig_str
= __getopt_nonoption_flags
;
456 int len
= nonoption_flags_max_len
= strlen (orig_str
);
457 if (nonoption_flags_max_len
< argc
)
458 nonoption_flags_max_len
= argc
;
459 __getopt_nonoption_flags
=
460 (char *) malloc (nonoption_flags_max_len
);
461 if (__getopt_nonoption_flags
== NULL
)
462 nonoption_flags_max_len
= -1;
464 memset (mempcpy (__getopt_nonoption_flags
, orig_str
, len
),
465 '\0', nonoption_flags_max_len
- len
);
468 nonoption_flags_len
= nonoption_flags_max_len
;
471 nonoption_flags_len
= 0;
477 /* Scan elements of ARGV (whose length is ARGC) for option characters
480 If an element of ARGV starts with '-', and is not exactly "-" or "--",
481 then it is an option element. The characters of this element
482 (aside from the initial '-') are option characters. If `getopt'
483 is called repeatedly, it returns successively each of the option characters
484 from each of the option elements.
486 If `getopt' finds another option character, it returns that character,
487 updating `optind' and `nextchar' so that the next call to `getopt' can
488 resume the scan with the following option character or ARGV-element.
490 If there are no more option characters, `getopt' returns -1.
491 Then `optind' is the index in ARGV of the first ARGV-element
492 that is not an option. (The ARGV-elements have been permuted
493 so that those that are not options now come last.)
495 OPTSTRING is a string containing the legitimate option characters.
496 If an option character is seen that is not listed in OPTSTRING,
497 return '?' after printing an error message. If you set `opterr' to
498 zero, the error message is suppressed but we still return '?'.
500 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
501 so the following text in the same ARGV-element, or the text of the following
502 ARGV-element, is returned in `optarg'. Two colons mean an option that
503 wants an optional arg; if there is text in the current ARGV-element,
504 it is returned in `optarg', otherwise `optarg' is set to zero.
506 If OPTSTRING starts with `-' or `+', it requests different methods of
507 handling the non-option ARGV-elements.
508 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
510 Long-named options begin with `--' instead of `-'.
511 Their names may be abbreviated as long as the abbreviation is unique
512 or is an exact match for some defined option. If they have an
513 argument, it follows the option name in the same ARGV-element, separated
514 from the option name by a `=', or else the in next ARGV-element.
515 When `getopt' finds a long-named option, it returns 0 if that option's
516 `flag' field is nonzero, the value of the option's `val' field
517 if the `flag' field is zero.
519 The elements of ARGV aren't really const, because we permute them.
520 But we pretend they're const in the prototype to be compatible
523 LONGOPTS is a vector of `struct option' terminated by an
524 element containing a name which is zero.
526 LONGIND returns the index in LONGOPT of the long-named option found.
527 It is only valid when a long-named option has been found by the most
530 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
531 long-named options. */
535 _getopt_internal (int argc
, char *const *argv
, const char *optstring
,
536 const struct option
*longopts
,
537 int *longind
, int long_only
)
539 cgetopt_optarg
= NULL
;
541 if (cgetopt_optind
== 0 || !__getopt_initialized
)
543 if (cgetopt_optind
== 0)
544 cgetopt_optind
= 1; /* Don't scan ARGV[0], the program name. */
545 optstring
= _getopt_initialize (argc
, argv
, optstring
);
546 __getopt_initialized
= 1;
549 /* Test whether ARGV[optind] points to a non-option argument.
550 Either it does not have option syntax, or there is an environment flag
551 from the shell indicating it is not an option. The later information
552 is only used when the used in the GNU libc. */
554 # define NONOPTION_P (argv[cgetopt_optind][0] != '-' || argv[cgetopt_optind][1] == '\0' \
555 || (cgetopt_optind < nonoption_flags_len \
556 && __getopt_nonoption_flags[cgetopt_optind] == '1'))
558 # define NONOPTION_P (argv[cgetopt_optind][0] != '-' || argv[cgetopt_optind][1] == '\0')
561 if (nextchar
== NULL
|| *nextchar
== '\0')
563 /* Advance to the next ARGV-element. */
565 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
566 moved back by the user (who may also have changed the arguments). */
567 if (last_nonopt
> cgetopt_optind
)
568 last_nonopt
= cgetopt_optind
;
569 if (first_nonopt
> cgetopt_optind
)
570 first_nonopt
= cgetopt_optind
;
572 if (ordering
== PERMUTE
)
574 /* If we have just processed some options following some non-options,
575 exchange them so that the options come first. */
577 if (first_nonopt
!= last_nonopt
&& last_nonopt
!= cgetopt_optind
)
578 exchange ((char **) argv
);
579 else if (last_nonopt
!= cgetopt_optind
)
580 first_nonopt
= cgetopt_optind
;
582 /* Skip any additional non-options
583 and extend the range of non-options previously skipped. */
585 while (cgetopt_optind
< argc
&& NONOPTION_P
)
587 last_nonopt
= cgetopt_optind
;
590 /* The special ARGV-element `--' means premature end of options.
591 Skip it like a null option,
592 then exchange with previous non-options as if it were an option,
593 then skip everything else like a non-option. */
595 if (cgetopt_optind
!= argc
&& !strcmp (argv
[cgetopt_optind
], "--"))
599 if (first_nonopt
!= last_nonopt
&& last_nonopt
!= cgetopt_optind
)
600 exchange ((char **) argv
);
601 else if (first_nonopt
== last_nonopt
)
602 first_nonopt
= cgetopt_optind
;
605 cgetopt_optind
= argc
;
608 /* If we have done all the ARGV-elements, stop the scan
609 and back over any non-options that we skipped and permuted. */
611 if (cgetopt_optind
== argc
)
613 /* Set the next-arg-index to point at the non-options
614 that we previously skipped, so the caller will digest them. */
615 if (first_nonopt
!= last_nonopt
)
616 cgetopt_optind
= first_nonopt
;
620 /* If we have come to a non-option and did not permute it,
621 either stop the scan or describe it to the caller and pass it by. */
625 if (ordering
== REQUIRE_ORDER
)
627 cgetopt_optarg
= argv
[cgetopt_optind
++];
631 /* We have found another option-ARGV-element.
632 Skip the initial punctuation. */
634 nextchar
= (argv
[cgetopt_optind
] + 1
635 + (longopts
!= NULL
&& argv
[cgetopt_optind
][1] == '-'));
638 /* Decode the current option-ARGV-element. */
640 /* Check whether the ARGV-element is a long option.
642 If long_only and the ARGV-element has the form "-f", where f is
643 a valid short option, don't consider it an abbreviated form of
644 a long option that starts with f. Otherwise there would be no
645 way to give the -f short option.
647 On the other hand, if there's a long option "fubar" and
648 the ARGV-element is "-fu", do consider that an abbreviation of
649 the long option, just like "--fu", and not "-f" with arg "u".
651 This distinction seems to be the most useful approach. */
654 && (argv
[cgetopt_optind
][1] == '-'
655 || (long_only
&& (argv
[cgetopt_optind
][2] || !my_index (optstring
, argv
[cgetopt_optind
][1])))))
658 const struct option
*p
;
659 const struct option
*pfound
= NULL
;
665 for (nameend
= nextchar
; *nameend
&& *nameend
!= '='; nameend
++)
668 /* Test all long options for either exact match
669 or abbreviated matches. */
670 for (p
= longopts
, option_index
= 0; p
->name
; p
++, option_index
++)
671 if (!strncmp (p
->name
, nextchar
, nameend
- nextchar
))
673 if ((unsigned int) (nameend
- nextchar
)
674 == (unsigned int) strlen (p
->name
))
676 /* Exact match found. */
678 indfound
= option_index
;
682 else if (pfound
== NULL
)
684 /* First nonexact match found. */
686 indfound
= option_index
;
689 /* Second or later nonexact match found. */
696 fprintf (stderr
, _("%s: option `%s' is ambiguous\n"),
697 argv
[0], argv
[cgetopt_optind
]);
698 nextchar
+= strlen (nextchar
);
706 option_index
= indfound
;
710 /* Don't test has_arg with >, because some C compilers don't
711 allow it to be used on enums. */
713 cgetopt_optarg
= nameend
+ 1;
718 if (argv
[cgetopt_optind
- 1][1] == '-')
721 _("%s: option `--%s' doesn't allow an argument\n"),
722 argv
[0], pfound
->name
);
724 /* +option or -option */
726 _("%s: option `%c%s' doesn't allow an argument\n"),
727 argv
[0], argv
[cgetopt_optind
- 1][0], pfound
->name
);
729 nextchar
+= strlen (nextchar
);
731 cgetopt_optopt
= pfound
->val
;
736 else if (pfound
->has_arg
== 1)
738 if (cgetopt_optind
< argc
)
739 cgetopt_optarg
= argv
[cgetopt_optind
++];
744 _("%s: option `%s' requires an argument\n"),
745 argv
[0], argv
[cgetopt_optind
- 1]);
746 nextchar
+= strlen (nextchar
);
747 cgetopt_optopt
= pfound
->val
;
748 return optstring
[0] == ':' ? ':' : '?';
751 nextchar
+= strlen (nextchar
);
753 *longind
= option_index
;
756 *(pfound
->flag
) = pfound
->val
;
762 /* Can't find it as a long option. If this is not getopt_long_only,
763 or the option starts with '--' or is not a valid short
764 option, then it's an error.
765 Otherwise interpret it as a short option. */
766 if (!long_only
|| argv
[cgetopt_optind
][1] == '-'
767 || my_index (optstring
, *nextchar
) == NULL
)
771 if (argv
[cgetopt_optind
][1] == '-')
773 fprintf (stderr
, _("%s: unrecognized option `--%s'\n"),
776 /* +option or -option */
777 fprintf (stderr
, _("%s: unrecognized option `%c%s'\n"),
778 argv
[0], argv
[cgetopt_optind
][0], nextchar
);
780 nextchar
= (char *) "";
787 /* Look at and handle the next short option-character. */
790 char c
= *nextchar
++;
791 const char *temp
= my_index (optstring
, c
);
793 /* Increment `cgetopt_optind' when we start to process its last character. */
794 if (*nextchar
== '\0')
797 if (temp
== NULL
|| c
== ':')
802 /* 1003.2 specifies the format of this message. */
803 fprintf (stderr
, _("%s: illegal option -- %c\n"),
806 fprintf (stderr
, _("%s: invalid option -- %c\n"),
812 /* Convenience. Treat POSIX -W foo same as long option --foo */
813 if (temp
[0] == 'W' && temp
[1] == ';')
816 const struct option
*p
;
817 const struct option
*pfound
= NULL
;
823 /* This is an option that requires an argument. */
824 if (*nextchar
!= '\0')
826 cgetopt_optarg
= nextchar
;
827 /* If we end this ARGV-element by taking the rest as an arg,
828 we must advance to the next element now. */
831 else if (cgetopt_optind
== argc
)
835 /* 1003.2 specifies the format of this message. */
836 fprintf (stderr
, _("%s: option requires an argument -- %c\n"),
840 if (optstring
[0] == ':')
847 /* We already incremented `optind' once;
848 increment it again when taking next ARGV-elt as argument. */
849 cgetopt_optarg
= argv
[cgetopt_optind
++];
851 /* cgetopt_optarg is now the argument, see if it's in the
852 table of longopts. */
854 for (nextchar
= nameend
= cgetopt_optarg
; *nameend
&& *nameend
!= '='; nameend
++)
857 /* Test all long options for either exact match
858 or abbreviated matches. */
859 for (p
= longopts
, option_index
= 0; p
->name
; p
++, option_index
++)
860 if (!strncmp (p
->name
, nextchar
, nameend
- nextchar
))
862 if ((unsigned int) (nameend
- nextchar
) == strlen (p
->name
))
864 /* Exact match found. */
866 indfound
= option_index
;
870 else if (pfound
== NULL
)
872 /* First nonexact match found. */
874 indfound
= option_index
;
877 /* Second or later nonexact match found. */
883 fprintf (stderr
, _("%s: option `-W %s' is ambiguous\n"),
884 argv
[0], argv
[cgetopt_optind
]);
885 nextchar
+= strlen (nextchar
);
891 option_index
= indfound
;
894 /* Don't test has_arg with >, because some C compilers don't
895 allow it to be used on enums. */
897 cgetopt_optarg
= nameend
+ 1;
901 fprintf (stderr
, _("\
902 %s: option `-W %s' doesn't allow an argument\n"),
903 argv
[0], pfound
->name
);
905 nextchar
+= strlen (nextchar
);
909 else if (pfound
->has_arg
== 1)
911 if (cgetopt_optind
< argc
)
912 cgetopt_optarg
= argv
[cgetopt_optind
++];
917 _("%s: option `%s' requires an argument\n"),
918 argv
[0], argv
[cgetopt_optind
- 1]);
919 nextchar
+= strlen (nextchar
);
920 return optstring
[0] == ':' ? ':' : '?';
923 nextchar
+= strlen (nextchar
);
925 *longind
= option_index
;
928 *(pfound
->flag
) = pfound
->val
;
934 return 'W'; /* Let the application handle it. */
940 /* This is an option that accepts an argument optionally. */
941 if (*nextchar
!= '\0')
943 cgetopt_optarg
= nextchar
;
947 cgetopt_optarg
= NULL
;
952 /* This is an option that requires an argument. */
953 if (*nextchar
!= '\0')
955 cgetopt_optarg
= nextchar
;
956 /* If we end this ARGV-element by taking the rest as an arg,
957 we must advance to the next element now. */
960 else if (cgetopt_optind
== argc
)
964 /* 1003.2 specifies the format of this message. */
966 _("%s: option requires an argument -- %c\n"),
970 if (optstring
[0] == ':')
976 /* We already incremented `optind' once;
977 increment it again when taking next ARGV-elt as argument. */
978 cgetopt_optarg
= argv
[cgetopt_optind
++];
987 libiberty_getopt_long (int argc
, char *const *argv
, const char *options
,
988 const struct option
*long_options
, int *opt_index
)
990 return _getopt_internal (argc
, argv
, options
, long_options
, opt_index
, 0);
993 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
994 If an option that starts with '-' (not '--') doesn't match a long option,
995 but does match a short option, it is parsed as a short option
999 libiberty_getopt_long_only (int argc
, char *const *argv
, const char *options
,
1000 const struct option
*long_options
, int *opt_index
)
1002 return _getopt_internal (argc
, argv
, options
, long_options
, opt_index
, 1);
1009 EXPORT(getopt
) (int argc
, char *argv
[], char *optstring
)
1011 char r
= getopt (argc
, argv
, optstring
);
1013 EXPORT(optarg
) = optarg
;
1014 EXPORT(optind
) = optind
;
1015 EXPORT(opterr
) = opterr
;
1016 EXPORT(optopt
) = optopt
;
1024 EXPORT(getopt_long
) (int argc
, char *argv
[], char *optstring
,
1025 const struct option
*longopts
, int *longindex
)
1027 #if defined(HAVE_GETOPT_LONG)
1028 int r
= getopt_long (argc
, argv
, optstring
, longopts
, longindex
);
1030 EXPORT(optarg
) = optarg
;
1031 EXPORT(optind
) = optind
;
1032 EXPORT(opterr
) = opterr
;
1033 EXPORT(optopt
) = optopt
;
1035 int r
= libiberty_getopt_long (argc
, argv
, optstring
, longopts
, longindex
);
1037 EXPORT(optarg
) = cgetopt_optarg
;
1038 EXPORT(optind
) = cgetopt_optind
;
1039 EXPORT(opterr
) = cgetopt_opterr
;
1040 EXPORT(optopt
) = cgetopt_optopt
;
1048 EXPORT(getopt_long_only
) (int argc
, char *argv
[], char *optstring
,
1049 const struct option
*longopts
, int *longindex
)
1051 #if defined(HAVE_GETOPT_LONG_ONLY)
1052 int r
= getopt_long_only (argc
, argv
, optstring
, longopts
, longindex
);
1054 EXPORT(optarg
) = optarg
;
1055 EXPORT(optind
) = optind
;
1056 EXPORT(opterr
) = opterr
;
1057 EXPORT(optopt
) = optopt
;
1059 int r
= libiberty_getopt_long_only (argc
, argv
, optstring
, longopts
, longindex
);
1061 EXPORT(optarg
) = cgetopt_optarg
;
1062 EXPORT(optind
) = cgetopt_optind
;
1063 EXPORT(opterr
) = cgetopt_opterr
;
1064 EXPORT(optopt
) = cgetopt_optopt
;
1071 /* GNU Modula-2 linking fodder. */
1074 M2EXPORT(init
) (int, char *argv
[], char *env
[])
1079 M2EXPORT(fini
) (int, char *argv
[], char *env
[])
1084 M2EXPORT(dep
) (void)
1088 extern "C" void __attribute__((__constructor__
))
1089 M2EXPORT(ctor
) (void)
1091 m2pim_M2RTS_RegisterModule ("cgetopt", M2LIBNAME
,
1092 M2EXPORT(init
), M2EXPORT(fini
),