34 #if !defined __STDC__ || !__STDC__
52 #define GETOPT_INTERFACE_VERSION 2
53 #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
54 # include <gnu-versions.h>
55 # if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
65 #ifdef __GNU_LIBRARY__
74 # if HAVE_STRING_H - 0
81 # if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
84 # define _(msgid) gettext (msgid)
87 # define _(msgid) (msgid)
89 # if defined _LIBC && defined USE_IN_LIBIO
96 #ifndef attribute_hidden
97 # define attribute_hidden
143 int __getopt_initialized attribute_hidden;
152 static char *nextchar;
196 REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
200 static char *posixly_correct;
202 #ifdef __GNU_LIBRARY__
208 # define my_index strchr
214 # include <strings.h>
221 extern char *getenv ();
243 # if (!defined __STDC__ || !__STDC__) && !defined strlen
246 extern int strlen (
const char *);
258 static int first_nonopt;
259 static int last_nonopt;
265 extern int __libc_argc;
266 extern char **__libc_argv;
271 # ifdef USE_NONOPTION_FLAGS
273 extern char *__getopt_nonoption_flags;
275 static int nonoption_flags_max_len;
276 static int nonoption_flags_len;
279 # ifdef USE_NONOPTION_FLAGS
280 # define SWAP_FLAGS(ch1, ch2) \
281 if (nonoption_flags_len > 0) \
283 char __tmp = __getopt_nonoption_flags[ch1]; \
284 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
285 __getopt_nonoption_flags[ch2] = __tmp; \
288 # define SWAP_FLAGS(ch1, ch2)
291 # define SWAP_FLAGS(ch1, ch2)
303 #if defined __STDC__ && __STDC__
304 static void exchange (
char **);
311 int bottom = first_nonopt;
312 int middle = last_nonopt;
321 #if defined _LIBC && defined USE_NONOPTION_FLAGS
325 if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
329 char *new_str = malloc (top + 1);
331 nonoption_flags_len = nonoption_flags_max_len = 0;
334 memset (__mempcpy (new_str, __getopt_nonoption_flags,
335 nonoption_flags_max_len),
336 '\0', top + 1 - nonoption_flags_max_len);
337 nonoption_flags_max_len = top + 1;
338 __getopt_nonoption_flags = new_str;
343 while (top > middle && middle > bottom)
345 if (top - middle > middle - bottom)
348 int len = middle - bottom;
352 for (i = 0; i < len; i++)
354 tem = argv[bottom + i];
355 argv[bottom + i] = argv[top - (middle - bottom) + i];
356 argv[top - (middle - bottom) + i] = tem;
357 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
365 int len = top - middle;
369 for (i = 0; i < len; i++)
371 tem = argv[bottom + i];
372 argv[bottom + i] = argv[middle + i];
373 argv[middle + i] = tem;
374 SWAP_FLAGS (bottom + i, middle + i);
383 first_nonopt += (optind - last_nonopt);
384 last_nonopt = optind;
389 #if defined __STDC__ && __STDC__
390 static const char *_getopt_initialize (
int,
char *
const *,
const char *);
393 _getopt_initialize (argc, argv, optstring)
396 const
char *optstring;
402 first_nonopt = last_nonopt = optind;
406 posixly_correct = getenv (
"POSIXLY_CORRECT");
410 if (optstring[0] ==
'-')
412 ordering = RETURN_IN_ORDER;
415 else if (optstring[0] ==
'+')
417 ordering = REQUIRE_ORDER;
420 else if (posixly_correct != NULL)
421 ordering = REQUIRE_ORDER;
425 #if defined _LIBC && defined USE_NONOPTION_FLAGS
426 if (posixly_correct == NULL
427 && argc == __libc_argc && argv == __libc_argv)
429 if (nonoption_flags_max_len == 0)
431 if (__getopt_nonoption_flags == NULL
432 || __getopt_nonoption_flags[0] ==
'\0')
433 nonoption_flags_max_len = -1;
436 const char *orig_str = __getopt_nonoption_flags;
437 int len = nonoption_flags_max_len = strlen (orig_str);
438 if (nonoption_flags_max_len < argc)
439 nonoption_flags_max_len = argc;
440 __getopt_nonoption_flags =
441 (
char *) malloc (nonoption_flags_max_len);
442 if (__getopt_nonoption_flags == NULL)
443 nonoption_flags_max_len = -1;
445 memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
446 '\0', nonoption_flags_max_len - len);
449 nonoption_flags_len = nonoption_flags_max_len;
452 nonoption_flags_len = 0;
515 _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
518 const
char *optstring;
519 const struct
option *longopts;
523 int print_errors = opterr;
524 if (optstring[0] ==
':')
532 if (optind == 0 || !__getopt_initialized)
536 optstring = _getopt_initialize (argc, argv, optstring);
537 __getopt_initialized = 1;
544 #if defined _LIBC && defined USE_NONOPTION_FLAGS
545 # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \
546 || (optind < nonoption_flags_len \
547 && __getopt_nonoption_flags[optind] == '1'))
549 # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
552 if (nextchar == NULL || *nextchar ==
'\0')
558 if (last_nonopt > optind)
559 last_nonopt = optind;
560 if (first_nonopt > optind)
561 first_nonopt = optind;
563 if (ordering == PERMUTE)
568 if (first_nonopt != last_nonopt && last_nonopt != optind)
569 exchange ((
char **) argv);
570 else if (last_nonopt != optind)
571 first_nonopt = optind;
576 while (optind < argc && NONOPTION_P)
578 last_nonopt = optind;
586 if (optind != argc && !strcmp (argv[optind],
"--"))
590 if (first_nonopt != last_nonopt && last_nonopt != optind)
591 exchange ((
char **) argv);
592 else if (first_nonopt == last_nonopt)
593 first_nonopt = optind;
606 if (first_nonopt != last_nonopt)
607 optind = first_nonopt;
616 if (ordering == REQUIRE_ORDER)
618 optarg = argv[optind++];
625 nextchar = (argv[optind] + 1
626 + (longopts != NULL && argv[optind][1] ==
'-'));
645 && (argv[optind][1] ==
'-'
646 || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
650 const struct option *pfound = NULL;
656 for (nameend = nextchar; *nameend && *nameend !=
'='; nameend++)
661 for (p = longopts, option_index = 0; p->name; p++, option_index++)
662 if (!strncmp (p->name, nextchar, nameend - nextchar))
664 if ((
unsigned int) (nameend - nextchar)
665 == (
unsigned int) strlen (p->name))
669 indfound = option_index;
673 else if (pfound == NULL)
677 indfound = option_index;
680 || pfound->has_arg != p->has_arg
681 || pfound->flag != p->flag
682 || pfound->val != p->val)
691 #if defined _LIBC && defined USE_IN_LIBIO
694 if (__asprintf (&buf, _(
"%s: option `%s' is ambiguous\n"),
695 argv[0], argv[optind]) >= 0)
698 if (_IO_fwide (stderr, 0) > 0)
699 __fwprintf (stderr, L
"%s", buf);
706 fprintf (stderr, _(
"%s: option `%s' is ambiguous\n"),
707 argv[0], argv[optind]);
710 nextchar += strlen (nextchar);
718 option_index = indfound;
725 optarg = nameend + 1;
730 #if defined _LIBC && defined USE_IN_LIBIO
735 if (argv[optind - 1][1] ==
'-')
738 #if defined _LIBC && defined USE_IN_LIBIO
739 n = __asprintf (&buf, _(
"\
740 %s: option `--%s' doesn't allow an argument\n"),
741 argv[0], pfound->name);
743 fprintf (stderr, _(
"\
744 %s: option `--%s' doesn't allow an argument\n"),
745 argv[0], pfound->name);
751 #if defined _LIBC && defined USE_IN_LIBIO
752 n = __asprintf (&buf, _(
"\
753 %s: option `%c%s' doesn't allow an argument\n"),
754 argv[0], argv[optind - 1][0],
757 fprintf (stderr, _(
"\
758 %s: option `%c%s' doesn't allow an argument\n"),
759 argv[0], argv[optind - 1][0], pfound->name);
763 #if defined _LIBC && defined USE_IN_LIBIO
766 if (_IO_fwide (stderr, 0) > 0)
767 __fwprintf (stderr, L
"%s", buf);
776 nextchar += strlen (nextchar);
778 optopt = pfound->val;
782 else if (pfound->has_arg == 1)
785 optarg = argv[optind++];
790 #if defined _LIBC && defined USE_IN_LIBIO
793 if (__asprintf (&buf, _(
"\
794 %s: option `%s' requires an argument\n"),
795 argv[0], argv[optind - 1]) >= 0)
797 if (_IO_fwide (stderr, 0) > 0)
798 __fwprintf (stderr, L
"%s", buf);
806 _(
"%s: option `%s' requires an argument\n"),
807 argv[0], argv[optind - 1]);
810 nextchar += strlen (nextchar);
811 optopt = pfound->val;
812 return optstring[0] ==
':' ?
':' :
'?';
815 nextchar += strlen (nextchar);
817 *longind = option_index;
820 *(pfound->flag) = pfound->val;
830 if (!long_only || argv[optind][1] ==
'-'
831 || my_index (optstring, *nextchar) == NULL)
835 #if defined _LIBC && defined USE_IN_LIBIO
840 if (argv[optind][1] ==
'-')
843 #if defined _LIBC && defined USE_IN_LIBIO
844 n = __asprintf (&buf, _(
"%s: unrecognized option `--%s'\n"),
847 fprintf (stderr, _(
"%s: unrecognized option `--%s'\n"),
854 #if defined _LIBC && defined USE_IN_LIBIO
855 n = __asprintf (&buf, _(
"%s: unrecognized option `%c%s'\n"),
856 argv[0], argv[optind][0], nextchar);
858 fprintf (stderr, _(
"%s: unrecognized option `%c%s'\n"),
859 argv[0], argv[optind][0], nextchar);
863 #if defined _LIBC && defined USE_IN_LIBIO
866 if (_IO_fwide (stderr, 0) > 0)
867 __fwprintf (stderr, L
"%s", buf);
875 nextchar = (
char *)
"";
885 char c = *nextchar++;
886 char *temp = my_index (optstring, c);
889 if (*nextchar ==
'\0')
892 if (temp == NULL || c ==
':')
896 #if defined _LIBC && defined USE_IN_LIBIO
904 #if defined _LIBC && defined USE_IN_LIBIO
905 n = __asprintf (&buf, _(
"%s: illegal option -- %c\n"),
908 fprintf (stderr, _(
"%s: illegal option -- %c\n"), argv[0], c);
913 #if defined _LIBC && defined USE_IN_LIBIO
914 n = __asprintf (&buf, _(
"%s: invalid option -- %c\n"),
917 fprintf (stderr, _(
"%s: invalid option -- %c\n"), argv[0], c);
921 #if defined _LIBC && defined USE_IN_LIBIO
924 if (_IO_fwide (stderr, 0) > 0)
925 __fwprintf (stderr, L
"%s", buf);
937 if (temp[0] ==
'W' && temp[1] ==
';')
941 const struct option *pfound = NULL;
948 if (*nextchar !=
'\0')
955 else if (optind == argc)
960 #if defined _LIBC && defined USE_IN_LIBIO
963 if (__asprintf (&buf,
964 _(
"%s: option requires an argument -- %c\n"),
967 if (_IO_fwide (stderr, 0) > 0)
968 __fwprintf (stderr, L
"%s", buf);
975 fprintf (stderr, _(
"%s: option requires an argument -- %c\n"),
980 if (optstring[0] ==
':')
989 optarg = argv[optind++];
994 for (nextchar = nameend = optarg; *nameend && *nameend !=
'='; nameend++)
999 for (p = longopts, option_index = 0; p->name; p++, option_index++)
1000 if (!strncmp (p->name, nextchar, nameend - nextchar))
1002 if ((
unsigned int) (nameend - nextchar) == strlen (p->name))
1006 indfound = option_index;
1010 else if (pfound == NULL)
1014 indfound = option_index;
1020 if (ambig && !exact)
1024 #if defined _LIBC && defined USE_IN_LIBIO
1027 if (__asprintf (&buf, _(
"%s: option `-W %s' is ambiguous\n"),
1028 argv[0], argv[optind]) >= 0)
1030 if (_IO_fwide (stderr, 0) > 0)
1031 __fwprintf (stderr, L
"%s", buf);
1033 fputs (buf, stderr);
1038 fprintf (stderr, _(
"%s: option `-W %s' is ambiguous\n"),
1039 argv[0], argv[optind]);
1042 nextchar += strlen (nextchar);
1048 option_index = indfound;
1053 if (pfound->has_arg)
1054 optarg = nameend + 1;
1059 #if defined _LIBC && defined USE_IN_LIBIO
1062 if (__asprintf (&buf, _(
"\
1063 %s: option `-W %s' doesn't allow an argument\n"),
1064 argv[0], pfound->name) >= 0)
1066 if (_IO_fwide (stderr, 0) > 0)
1067 __fwprintf (stderr, L
"%s", buf);
1069 fputs (buf, stderr);
1074 fprintf (stderr, _(
"\
1075 %s: option `-W %s' doesn't allow an argument\n"),
1076 argv[0], pfound->name);
1080 nextchar += strlen (nextchar);
1084 else if (pfound->has_arg == 1)
1087 optarg = argv[optind++];
1092 #if defined _LIBC && defined USE_IN_LIBIO
1095 if (__asprintf (&buf, _(
"\
1096 %s: option `%s' requires an argument\n"),
1097 argv[0], argv[optind - 1]) >= 0)
1099 if (_IO_fwide (stderr, 0) > 0)
1100 __fwprintf (stderr, L
"%s", buf);
1102 fputs (buf, stderr);
1108 _(
"%s: option `%s' requires an argument\n"),
1109 argv[0], argv[optind - 1]);
1112 nextchar += strlen (nextchar);
1113 return optstring[0] ==
':' ?
':' :
'?';
1116 nextchar += strlen (nextchar);
1117 if (longind != NULL)
1118 *longind = option_index;
1121 *(pfound->flag) = pfound->val;
1134 if (*nextchar !=
'\0')
1146 if (*nextchar !=
'\0')
1153 else if (optind == argc)
1158 #if defined _LIBC && defined USE_IN_LIBIO
1161 if (__asprintf (&buf, _(
"\
1162 %s: option requires an argument -- %c\n"),
1165 if (_IO_fwide (stderr, 0) > 0)
1166 __fwprintf (stderr, L
"%s", buf);
1168 fputs (buf, stderr);
1174 _(
"%s: option requires an argument -- %c\n"),
1179 if (optstring[0] ==
':')
1187 optarg = argv[optind++];
1196 getopt (argc, argv, optstring)
1199 const
char *optstring;
1201 return _getopt_internal (argc, argv, optstring,
1202 (
const struct option *) 0,
1220 int digit_optind = 0;
1224 int this_option_optind = optind ? optind : 1;
1226 c = getopt (argc, argv,
"abc:d:0123456789");
1242 if (digit_optind != 0 && digit_optind != this_option_optind)
1243 printf (
"digits occur in two different argv-elements.\n");
1244 digit_optind = this_option_optind;
1245 printf (
"option %c\n", c);
1249 printf (
"option a\n");
1253 printf (
"option b\n");
1257 printf (
"option c with value `%s'\n", optarg);
1264 printf (
"?? getopt returned character code 0%o ??\n", c);
1270 printf (
"non-option ARGV-elements: ");
1271 while (optind < argc)
1272 printf (
"%s ", argv[optind++]);
Definitions for the configuration of the cbm4win driver.
int ARCH_MAINDECL main(int argc, char **argv)
Initialize the xum1541 device This function tries to find and identify the xum1541 device...
Define makros and functions which account for differences between the different architectures.