2 * File automatically generated by
3 * gengen 1.4.1 by Lorenzo Bettini
4 * http://www.gnu.org/software/gengen
7 #include "custom_getopt_gen.h"
10 custom_getopt_gen_gen_class::generate_custom_getopt_gen(ostream &stream, unsigned int indent)
12 string indent_str (indent, ' ');
18 stream << " * Extracted from the glibc source tree, version 2.3.6";
24 stream << " * Licensed under the GPL as per the whole glibc source tree.";
30 stream << " * This file was modified so that getopt_long can be called";
33 stream << " * many times without risking previous memory to be spoiled.";
39 stream << " * Modified by Andre Noll and Lorenzo Bettini for use in";
42 stream << " * GNU gengetopt generated files.";
56 stream << " * we must include anything we need since this file is not thought to be";
59 stream << " * inserted in a file already using getopt.h";
65 stream << " * Lorenzo";
73 stream << "struct option";
79 stream << " const char *name;";
82 stream << " /* has_arg can't be an enum because some compilers complain about";
85 stream << " type mismatches in all the code that assumes it is an int. */";
88 stream << " int has_arg;";
91 stream << " int *flag;";
94 stream << " int val;";
101 stream << indent_str;
102 stream << "/* This version of `getopt' appears to the caller like standard Unix `getopt'";
104 stream << indent_str;
105 stream << " but it behaves differently for the user, since it allows the user";
107 stream << indent_str;
108 stream << " to intersperse the options with the other arguments.";
110 stream << indent_str;
112 stream << indent_str;
113 stream << " As `getopt' works, it permutes the elements of ARGV so that,";
115 stream << indent_str;
116 stream << " when it is done, all the options precede everything else. Thus";
118 stream << indent_str;
119 stream << " all application programs are extended to handle flexible argument order.";
121 stream << indent_str;
124 stream << indent_str;
127 stream << indent_str;
128 stream << " If the field `flag' is not NULL, it points to a variable that is set";
130 stream << indent_str;
131 stream << " to the value given in the field `val' when the option is found, but";
133 stream << indent_str;
134 stream << " left unchanged if the option is not found.";
136 stream << indent_str;
138 stream << indent_str;
139 stream << " To have a long-named option do something other than set an `int' to";
141 stream << indent_str;
142 stream << " a compiled-in constant, such as set a value from `custom_optarg', set the";
144 stream << indent_str;
145 stream << " option's `flag' field to zero and its `val' field to a nonzero";
147 stream << indent_str;
148 stream << " value (the equivalent single-letter option character, if there is";
150 stream << indent_str;
151 stream << " one). For long options that have a zero `flag' field, `getopt'";
153 stream << indent_str;
154 stream << " returns the contents of the `val' field. */";
156 stream << indent_str;
158 stream << indent_str;
159 stream << "/* Names for the values of the `has_arg' field of `struct option'. */";
161 stream << indent_str;
162 stream << "#ifndef no_argument";
164 stream << indent_str;
165 stream << "#define no_argument 0";
167 stream << indent_str;
170 stream << indent_str;
172 stream << indent_str;
173 stream << "#ifndef required_argument";
175 stream << indent_str;
176 stream << "#define required_argument 1";
178 stream << indent_str;
181 stream << indent_str;
183 stream << indent_str;
184 stream << "#ifndef optional_argument";
186 stream << indent_str;
187 stream << "#define optional_argument 2";
189 stream << indent_str;
192 stream << indent_str;
194 stream << indent_str;
195 stream << "struct custom_getopt_data {";
197 stream << indent_str;
200 stream << indent_str;
201 stream << " * These have exactly the same meaning as the corresponding global variables,";
203 stream << indent_str;
204 stream << " * except that they are used for the reentrant versions of getopt.";
206 stream << indent_str;
209 stream << indent_str;
210 stream << " int custom_optind;";
212 stream << indent_str;
213 stream << " int custom_opterr;";
215 stream << indent_str;
216 stream << " int custom_optopt;";
218 stream << indent_str;
219 stream << " char *custom_optarg;";
221 stream << indent_str;
223 stream << indent_str;
224 stream << " /* True if the internal members have been initialized. */";
226 stream << indent_str;
227 stream << " int initialized;";
229 stream << indent_str;
231 stream << indent_str;
234 stream << indent_str;
235 stream << " * The next char to be scanned in the option-element in which the last option";
237 stream << indent_str;
238 stream << " * character we returned was found. This allows us to pick up the scan where";
240 stream << indent_str;
241 stream << " * we left off. If this is zero, or a null string, it means resume the scan by";
243 stream << indent_str;
244 stream << " * advancing to the next ARGV-element.";
246 stream << indent_str;
249 stream << indent_str;
250 stream << " char *nextchar;";
252 stream << indent_str;
254 stream << indent_str;
257 stream << indent_str;
258 stream << " * Describe the part of ARGV that contains non-options that have been skipped.";
260 stream << indent_str;
261 stream << " * `first_nonopt' is the index in ARGV of the first of them; `last_nonopt' is";
263 stream << indent_str;
264 stream << " * the index after the last of them.";
266 stream << indent_str;
269 stream << indent_str;
270 stream << " int first_nonopt;";
272 stream << indent_str;
273 stream << " int last_nonopt;";
275 stream << indent_str;
278 stream << indent_str;
280 stream << indent_str;
283 stream << indent_str;
284 stream << " * the variables optarg, optind, opterr and optopt are renamed with";
286 stream << indent_str;
287 stream << " * the custom_ prefix so that they don't interfere with getopt ones.";
289 stream << indent_str;
292 stream << indent_str;
293 stream << " * Moreover they're static so they are visible only from within the";
295 stream << indent_str;
296 stream << " * file where this very file will be included.";
298 stream << indent_str;
301 stream << indent_str;
303 stream << indent_str;
306 stream << indent_str;
307 stream << " * For communication from `custom_getopt' to the caller. When `custom_getopt' finds an";
309 stream << indent_str;
310 stream << " * option that takes an argument, the argument value is returned here.";
312 stream << indent_str;
315 stream << indent_str;
316 stream << "static char *custom_optarg;";
318 stream << indent_str;
320 stream << indent_str;
323 stream << indent_str;
324 stream << " * Index in ARGV of the next element to be scanned. This is used for";
326 stream << indent_str;
327 stream << " * communication to and from the caller and for communication between";
329 stream << indent_str;
330 stream << " * successive calls to `custom_getopt'.";
332 stream << indent_str;
335 stream << indent_str;
336 stream << " * On entry to `custom_getopt', 1 means this is the first call; initialize.";
338 stream << indent_str;
341 stream << indent_str;
342 stream << " * When `custom_getopt' returns -1, this is the index of the first of the non-option";
344 stream << indent_str;
345 stream << " * elements that the caller should itself scan.";
347 stream << indent_str;
350 stream << indent_str;
351 stream << " * Otherwise, `custom_optind' communicates from one call to the next how much of ARGV";
353 stream << indent_str;
354 stream << " * has been scanned so far.";
356 stream << indent_str;
359 stream << indent_str;
360 stream << " * 1003.2 says this must be 1 before any call.";
362 stream << indent_str;
365 stream << indent_str;
366 stream << "static int custom_optind = 1;";
368 stream << indent_str;
370 stream << indent_str;
373 stream << indent_str;
374 stream << " * Callers store zero here to inhibit the error message for unrecognized";
376 stream << indent_str;
377 stream << " * options.";
379 stream << indent_str;
382 stream << indent_str;
383 stream << "static int custom_opterr = 1;";
385 stream << indent_str;
387 stream << indent_str;
390 stream << indent_str;
391 stream << " * Set to an option character which was unrecognized. This must be initialized";
393 stream << indent_str;
394 stream << " * on some systems to avoid linking in the system's own getopt implementation.";
396 stream << indent_str;
399 stream << indent_str;
400 stream << "static int custom_optopt = '?';";
402 stream << indent_str;
404 stream << indent_str;
407 stream << indent_str;
408 stream << " * Exchange two adjacent subsequences of ARGV. One subsequence is elements";
410 stream << indent_str;
411 stream << " * [first_nonopt,last_nonopt) which contains all the non-options that have been";
413 stream << indent_str;
414 stream << " * skipped so far. The other is elements [last_nonopt,custom_optind), which contains";
416 stream << indent_str;
417 stream << " * all the options processed since those non-options were skipped.";
419 stream << indent_str;
420 stream << " * `first_nonopt' and `last_nonopt' are relocated so that they describe the new";
422 stream << indent_str;
423 stream << " * indices of the non-options in ARGV after they are moved.";
425 stream << indent_str;
428 stream << indent_str;
429 stream << "static void exchange(char **argv, struct custom_getopt_data *d)";
431 stream << indent_str;
434 stream << indent_str;
435 stream << " int bottom = d->first_nonopt;";
437 stream << indent_str;
438 stream << " int middle = d->last_nonopt;";
440 stream << indent_str;
441 stream << " int top = d->custom_optind;";
443 stream << indent_str;
444 stream << " char *tem;";
446 stream << indent_str;
448 stream << indent_str;
451 stream << indent_str;
452 stream << " * Exchange the shorter segment with the far end of the longer segment.";
454 stream << indent_str;
455 stream << " * That puts the shorter segment into the right place. It leaves the";
457 stream << indent_str;
458 stream << " * longer segment in the right place overall, but it consists of two";
460 stream << indent_str;
461 stream << " * parts that need to be swapped next.";
463 stream << indent_str;
466 stream << indent_str;
467 stream << " while (top > middle && middle > bottom) {";
469 stream << indent_str;
470 stream << " if (top - middle > middle - bottom) {";
472 stream << indent_str;
473 stream << " /* Bottom segment is the short one. */";
475 stream << indent_str;
476 stream << " int len = middle - bottom;";
478 stream << indent_str;
481 stream << indent_str;
483 stream << indent_str;
484 stream << " /* Swap it with the top part of the top segment. */";
486 stream << indent_str;
487 stream << " for (i = 0; i < len; i++) {";
489 stream << indent_str;
490 stream << " tem = argv[bottom + i];";
492 stream << indent_str;
493 stream << " argv[bottom + i] =";
495 stream << indent_str;
496 stream << " argv[top - (middle - bottom) + i];";
498 stream << indent_str;
499 stream << " argv[top - (middle - bottom) + i] = tem;";
501 stream << indent_str;
504 stream << indent_str;
505 stream << " /* Exclude the moved bottom segment from further swapping. */";
507 stream << indent_str;
508 stream << " top -= len;";
510 stream << indent_str;
511 stream << " } else {";
513 stream << indent_str;
514 stream << " /* Top segment is the short one. */";
516 stream << indent_str;
517 stream << " int len = top - middle;";
519 stream << indent_str;
522 stream << indent_str;
524 stream << indent_str;
525 stream << " /* Swap it with the bottom part of the bottom segment. */";
527 stream << indent_str;
528 stream << " for (i = 0; i < len; i++) {";
530 stream << indent_str;
531 stream << " tem = argv[bottom + i];";
533 stream << indent_str;
534 stream << " argv[bottom + i] = argv[middle + i];";
536 stream << indent_str;
537 stream << " argv[middle + i] = tem;";
539 stream << indent_str;
542 stream << indent_str;
543 stream << " /* Exclude the moved top segment from further swapping. */";
545 stream << indent_str;
546 stream << " bottom += len;";
548 stream << indent_str;
551 stream << indent_str;
554 stream << indent_str;
555 stream << " /* Update records for the slots the non-options now occupy. */";
557 stream << indent_str;
558 stream << " d->first_nonopt += (d->custom_optind - d->last_nonopt);";
560 stream << indent_str;
561 stream << " d->last_nonopt = d->custom_optind;";
563 stream << indent_str;
566 stream << indent_str;
568 stream << indent_str;
569 stream << "/* Initialize the internal data when the first call is made. */";
571 stream << indent_str;
572 stream << "static void custom_getopt_initialize(struct custom_getopt_data *d)";
574 stream << indent_str;
577 stream << indent_str;
580 stream << indent_str;
581 stream << " * Start processing options with ARGV-element 1 (since ARGV-element 0";
583 stream << indent_str;
584 stream << " * is the program name); the sequence of previously skipped non-option";
586 stream << indent_str;
587 stream << " * ARGV-elements is empty.";
589 stream << indent_str;
592 stream << indent_str;
593 stream << " d->first_nonopt = d->last_nonopt = d->custom_optind;";
595 stream << indent_str;
596 stream << " d->nextchar = NULL;";
598 stream << indent_str;
599 stream << " d->initialized = 1;";
601 stream << indent_str;
604 stream << indent_str;
606 stream << indent_str;
607 stream << "#define NONOPTION_P (argv[d->custom_optind][0] != '-' || argv[d->custom_optind][1] == '\\0')";
609 stream << indent_str;
611 stream << indent_str;
612 stream << "/* return: zero: continue, nonzero: return given value to user */";
614 stream << indent_str;
615 stream << "static int shuffle_argv(int argc, char *const *argv,const struct option *longopts,";
617 stream << indent_str;
618 stream << " struct custom_getopt_data *d)";
620 stream << indent_str;
623 stream << indent_str;
626 stream << indent_str;
627 stream << " * Give FIRST_NONOPT & LAST_NONOPT rational values if CUSTOM_OPTIND has been";
629 stream << indent_str;
630 stream << " * moved back by the user (who may also have changed the arguments).";
632 stream << indent_str;
635 stream << indent_str;
636 stream << " if (d->last_nonopt > d->custom_optind)";
638 stream << indent_str;
639 stream << " d->last_nonopt = d->custom_optind;";
641 stream << indent_str;
642 stream << " if (d->first_nonopt > d->custom_optind)";
644 stream << indent_str;
645 stream << " d->first_nonopt = d->custom_optind;";
647 stream << indent_str;
650 stream << indent_str;
651 stream << " * If we have just processed some options following some";
653 stream << indent_str;
654 stream << " * non-options, exchange them so that the options come first.";
656 stream << indent_str;
659 stream << indent_str;
660 stream << " if (d->first_nonopt != d->last_nonopt &&";
662 stream << indent_str;
663 stream << " d->last_nonopt != d->custom_optind)";
665 stream << indent_str;
666 stream << " exchange((char **) argv, d);";
668 stream << indent_str;
669 stream << " else if (d->last_nonopt != d->custom_optind)";
671 stream << indent_str;
672 stream << " d->first_nonopt = d->custom_optind;";
674 stream << indent_str;
677 stream << indent_str;
678 stream << " * Skip any additional non-options and extend the range of";
680 stream << indent_str;
681 stream << " * non-options previously skipped.";
683 stream << indent_str;
686 stream << indent_str;
687 stream << " while (d->custom_optind < argc && NONOPTION_P)";
689 stream << indent_str;
690 stream << " d->custom_optind++;";
692 stream << indent_str;
693 stream << " d->last_nonopt = d->custom_optind;";
695 stream << indent_str;
698 stream << indent_str;
699 stream << " * The special ARGV-element `--' means premature end of options. Skip";
701 stream << indent_str;
702 stream << " * it like a null option, then exchange with previous non-options as if";
704 stream << indent_str;
705 stream << " * it were an option, then skip everything else like a non-option.";
707 stream << indent_str;
710 stream << indent_str;
711 stream << " if (d->custom_optind != argc && !strcmp(argv[d->custom_optind], \"--\")) {";
713 stream << indent_str;
714 stream << " d->custom_optind++;";
716 stream << indent_str;
717 stream << " if (d->first_nonopt != d->last_nonopt";
719 stream << indent_str;
720 stream << " && d->last_nonopt != d->custom_optind)";
722 stream << indent_str;
723 stream << " exchange((char **) argv, d);";
725 stream << indent_str;
726 stream << " else if (d->first_nonopt == d->last_nonopt)";
728 stream << indent_str;
729 stream << " d->first_nonopt = d->custom_optind;";
731 stream << indent_str;
732 stream << " d->last_nonopt = argc;";
734 stream << indent_str;
735 stream << " d->custom_optind = argc;";
737 stream << indent_str;
740 stream << indent_str;
743 stream << indent_str;
744 stream << " * If we have done all the ARGV-elements, stop the scan and back over";
746 stream << indent_str;
747 stream << " * any non-options that we skipped and permuted.";
749 stream << indent_str;
752 stream << indent_str;
753 stream << " if (d->custom_optind == argc) {";
755 stream << indent_str;
758 stream << indent_str;
759 stream << " * Set the next-arg-index to point at the non-options that we";
761 stream << indent_str;
762 stream << " * previously skipped, so the caller will digest them.";
764 stream << indent_str;
767 stream << indent_str;
768 stream << " if (d->first_nonopt != d->last_nonopt)";
770 stream << indent_str;
771 stream << " d->custom_optind = d->first_nonopt;";
773 stream << indent_str;
774 stream << " return -1;";
776 stream << indent_str;
779 stream << indent_str;
782 stream << indent_str;
783 stream << " * If we have come to a non-option and did not permute it, either stop";
785 stream << indent_str;
786 stream << " * the scan or describe it to the caller and pass it by.";
788 stream << indent_str;
791 stream << indent_str;
792 stream << " if (NONOPTION_P) {";
794 stream << indent_str;
795 stream << " d->custom_optarg = argv[d->custom_optind++];";
797 stream << indent_str;
798 stream << " return 1;";
800 stream << indent_str;
803 stream << indent_str;
806 stream << indent_str;
807 stream << " * We have found another option-ARGV-element. Skip the initial";
809 stream << indent_str;
810 stream << " * punctuation.";
812 stream << indent_str;
815 stream << indent_str;
816 stream << " d->nextchar = (argv[d->custom_optind] + 1 + (longopts != NULL && argv[d->custom_optind][1] == '-'));";
818 stream << indent_str;
819 stream << " return 0;";
821 stream << indent_str;
824 stream << indent_str;
826 stream << indent_str;
829 stream << indent_str;
830 stream << " * Check whether the ARGV-element is a long option.";
832 stream << indent_str;
835 stream << indent_str;
836 stream << " * If there's a long option \"fubar\" and the ARGV-element is \"-fu\", consider";
838 stream << indent_str;
839 stream << " * that an abbreviation of the long option, just like \"--fu\", and not \"-f\" with";
841 stream << indent_str;
842 stream << " * arg \"u\".";
844 stream << indent_str;
847 stream << indent_str;
848 stream << " * This distinction seems to be the most useful approach.";
850 stream << indent_str;
853 stream << indent_str;
856 stream << indent_str;
857 stream << "static int check_long_opt(int argc, char *const *argv, const char *optstring,";
859 stream << indent_str;
860 stream << " const struct option *longopts, int *longind,";
862 stream << indent_str;
863 stream << " int print_errors, struct custom_getopt_data *d)";
865 stream << indent_str;
868 stream << indent_str;
869 stream << " char *nameend;";
871 stream << indent_str;
872 stream << " const struct option *p;";
874 stream << indent_str;
875 stream << " const struct option *pfound = NULL;";
877 stream << indent_str;
878 stream << " int exact = 0;";
880 stream << indent_str;
881 stream << " int ambig = 0;";
883 stream << indent_str;
884 stream << " int indfound = -1;";
886 stream << indent_str;
887 stream << " int option_index;";
889 stream << indent_str;
891 stream << indent_str;
892 stream << " for (nameend = d->nextchar; *nameend && *nameend != '='; nameend++)";
894 stream << indent_str;
895 stream << " /* Do nothing. */ ;";
897 stream << indent_str;
899 stream << indent_str;
900 stream << " /* Test all long options for either exact match or abbreviated matches */";
902 stream << indent_str;
903 stream << " for (p = longopts, option_index = 0; p->name; p++, option_index++)";
905 stream << indent_str;
906 stream << " if (!strncmp(p->name, d->nextchar, nameend - d->nextchar)) {";
908 stream << indent_str;
909 stream << " if ((unsigned int) (nameend - d->nextchar)";
911 stream << indent_str;
912 stream << " == (unsigned int) strlen(p->name)) {";
914 stream << indent_str;
915 stream << " /* Exact match found. */";
917 stream << indent_str;
918 stream << " pfound = p;";
920 stream << indent_str;
921 stream << " indfound = option_index;";
923 stream << indent_str;
924 stream << " exact = 1;";
926 stream << indent_str;
929 stream << indent_str;
930 stream << " } else if (pfound == NULL) {";
932 stream << indent_str;
933 stream << " /* First nonexact match found. */";
935 stream << indent_str;
936 stream << " pfound = p;";
938 stream << indent_str;
939 stream << " indfound = option_index;";
941 stream << indent_str;
942 stream << " } else if (pfound->has_arg != p->has_arg";
944 stream << indent_str;
945 stream << " || pfound->flag != p->flag";
947 stream << indent_str;
948 stream << " || pfound->val != p->val)";
950 stream << indent_str;
951 stream << " /* Second or later nonexact match found. */";
953 stream << indent_str;
954 stream << " ambig = 1;";
956 stream << indent_str;
959 stream << indent_str;
960 stream << " if (ambig && !exact) {";
962 stream << indent_str;
963 stream << " if (print_errors) {";
965 stream << indent_str;
966 stream << " fprintf(stderr,";
968 stream << indent_str;
969 stream << " \"%s: option `%s' is ambiguous\\n\",";
971 stream << indent_str;
972 stream << " argv[0], argv[d->custom_optind]);";
974 stream << indent_str;
977 stream << indent_str;
978 stream << " d->nextchar += strlen(d->nextchar);";
980 stream << indent_str;
981 stream << " d->custom_optind++;";
983 stream << indent_str;
984 stream << " d->custom_optopt = 0;";
986 stream << indent_str;
987 stream << " return '?';";
989 stream << indent_str;
992 stream << indent_str;
993 stream << " if (pfound) {";
995 stream << indent_str;
996 stream << " option_index = indfound;";
998 stream << indent_str;
999 stream << " d->custom_optind++;";
1001 stream << indent_str;
1002 stream << " if (*nameend) {";
1004 stream << indent_str;
1005 stream << " if (pfound->has_arg != no_argument)";
1007 stream << indent_str;
1008 stream << " d->custom_optarg = nameend + 1;";
1010 stream << indent_str;
1011 stream << " else {";
1013 stream << indent_str;
1014 stream << " if (print_errors) {";
1016 stream << indent_str;
1017 stream << " if (argv[d->custom_optind - 1][1] == '-') {";
1019 stream << indent_str;
1020 stream << " /* --option */";
1022 stream << indent_str;
1023 stream << " fprintf(stderr, \"%s: option `--%s' doesn't allow an argument\\n\",";
1025 stream << indent_str;
1026 stream << " argv[0], pfound->name);";
1028 stream << indent_str;
1029 stream << " } else {";
1031 stream << indent_str;
1032 stream << " /* +option or -option */";
1034 stream << indent_str;
1035 stream << " fprintf(stderr, \"%s: option `%c%s' doesn't allow an argument\\n\",";
1037 stream << indent_str;
1038 stream << " argv[0], argv[d->custom_optind - 1][0], pfound->name);";
1040 stream << indent_str;
1043 stream << indent_str;
1045 stream << indent_str;
1048 stream << indent_str;
1049 stream << " d->nextchar += strlen(d->nextchar);";
1051 stream << indent_str;
1052 stream << " d->custom_optopt = pfound->val;";
1054 stream << indent_str;
1055 stream << " return '?';";
1057 stream << indent_str;
1060 stream << indent_str;
1061 stream << " } else if (pfound->has_arg == required_argument) {";
1063 stream << indent_str;
1064 stream << " if (d->custom_optind < argc)";
1066 stream << indent_str;
1067 stream << " d->custom_optarg = argv[d->custom_optind++];";
1069 stream << indent_str;
1070 stream << " else {";
1072 stream << indent_str;
1073 stream << " if (print_errors) {";
1075 stream << indent_str;
1076 stream << " fprintf(stderr,";
1078 stream << indent_str;
1079 stream << " \"%s: option `%s' requires an argument\\n\",";
1081 stream << indent_str;
1082 stream << " argv[0],";
1084 stream << indent_str;
1085 stream << " argv[d->custom_optind - 1]);";
1087 stream << indent_str;
1090 stream << indent_str;
1091 stream << " d->nextchar += strlen(d->nextchar);";
1093 stream << indent_str;
1094 stream << " d->custom_optopt = pfound->val;";
1096 stream << indent_str;
1097 stream << " return optstring[0] == ':' ? ':' : '?';";
1099 stream << indent_str;
1102 stream << indent_str;
1105 stream << indent_str;
1106 stream << " d->nextchar += strlen(d->nextchar);";
1108 stream << indent_str;
1109 stream << " if (longind != NULL)";
1111 stream << indent_str;
1112 stream << " *longind = option_index;";
1114 stream << indent_str;
1115 stream << " if (pfound->flag) {";
1117 stream << indent_str;
1118 stream << " *(pfound->flag) = pfound->val;";
1120 stream << indent_str;
1121 stream << " return 0;";
1123 stream << indent_str;
1126 stream << indent_str;
1127 stream << " return pfound->val;";
1129 stream << indent_str;
1132 stream << indent_str;
1135 stream << indent_str;
1136 stream << " * Can't find it as a long option. If this is not getopt_long_only, or";
1138 stream << indent_str;
1139 stream << " * the option starts with '--' or is not a valid short option, then";
1141 stream << indent_str;
1142 stream << " * it's an error. Otherwise interpret it as a short option.";
1144 stream << indent_str;
1147 stream << indent_str;
1148 stream << " if (print_errors) {";
1150 stream << indent_str;
1151 stream << " if (argv[d->custom_optind][1] == '-') {";
1153 stream << indent_str;
1154 stream << " /* --option */";
1156 stream << indent_str;
1157 stream << " fprintf(stderr,";
1159 stream << indent_str;
1160 stream << " \"%s: unrecognized option `--%s'\\n\",";
1162 stream << indent_str;
1163 stream << " argv[0], d->nextchar);";
1165 stream << indent_str;
1166 stream << " } else {";
1168 stream << indent_str;
1169 stream << " /* +option or -option */";
1171 stream << indent_str;
1172 stream << " fprintf(stderr,";
1174 stream << indent_str;
1175 stream << " \"%s: unrecognized option `%c%s'\\n\",";
1177 stream << indent_str;
1178 stream << " argv[0], argv[d->custom_optind][0],";
1180 stream << indent_str;
1181 stream << " d->nextchar);";
1183 stream << indent_str;
1186 stream << indent_str;
1189 stream << indent_str;
1190 stream << " d->nextchar = (char *) \"\";";
1192 stream << indent_str;
1193 stream << " d->custom_optind++;";
1195 stream << indent_str;
1196 stream << " d->custom_optopt = 0;";
1198 stream << indent_str;
1199 stream << " return '?';";
1201 stream << indent_str;
1204 stream << indent_str;
1206 stream << indent_str;
1207 stream << "static int check_short_opt(int argc, char *const *argv, const char *optstring,";
1209 stream << indent_str;
1210 stream << " int print_errors, struct custom_getopt_data *d)";
1212 stream << indent_str;
1215 stream << indent_str;
1216 stream << " char c = *d->nextchar++;";
1218 stream << indent_str;
1219 stream << " const char *temp = strchr(optstring, c);";
1221 stream << indent_str;
1223 stream << indent_str;
1224 stream << " /* Increment `custom_optind' when we start to process its last character. */";
1226 stream << indent_str;
1227 stream << " if (*d->nextchar == '\\0')";
1229 stream << indent_str;
1230 stream << " ++d->custom_optind;";
1232 stream << indent_str;
1233 stream << " if (!temp || c == ':') {";
1235 stream << indent_str;
1236 stream << " if (print_errors)";
1238 stream << indent_str;
1239 stream << " fprintf(stderr, \"%s: invalid option -- %c\\n\", argv[0], c);";
1241 stream << indent_str;
1243 stream << indent_str;
1244 stream << " d->custom_optopt = c;";
1246 stream << indent_str;
1247 stream << " return '?';";
1249 stream << indent_str;
1252 stream << indent_str;
1253 stream << " if (temp[1] == ':') {";
1255 stream << indent_str;
1256 stream << " if (temp[2] == ':') {";
1258 stream << indent_str;
1259 stream << " /* This is an option that accepts an argument optionally. */";
1261 stream << indent_str;
1262 stream << " if (*d->nextchar != '\\0') {";
1264 stream << indent_str;
1265 stream << " d->custom_optarg = d->nextchar;";
1267 stream << indent_str;
1268 stream << " d->custom_optind++;";
1270 stream << indent_str;
1271 stream << " } else";
1273 stream << indent_str;
1274 stream << " d->custom_optarg = NULL;";
1276 stream << indent_str;
1277 stream << " d->nextchar = NULL;";
1279 stream << indent_str;
1280 stream << " } else {";
1282 stream << indent_str;
1283 stream << " /* This is an option that requires an argument. */";
1285 stream << indent_str;
1286 stream << " if (*d->nextchar != '\\0') {";
1288 stream << indent_str;
1289 stream << " d->custom_optarg = d->nextchar;";
1291 stream << indent_str;
1294 stream << indent_str;
1295 stream << " * If we end this ARGV-element by taking the";
1297 stream << indent_str;
1298 stream << " * rest as an arg, we must advance to the next";
1300 stream << indent_str;
1301 stream << " * element now.";
1303 stream << indent_str;
1306 stream << indent_str;
1307 stream << " d->custom_optind++;";
1309 stream << indent_str;
1310 stream << " } else if (d->custom_optind == argc) {";
1312 stream << indent_str;
1313 stream << " if (print_errors) {";
1315 stream << indent_str;
1316 stream << " fprintf(stderr,";
1318 stream << indent_str;
1319 stream << " \"%s: option requires an argument -- %c\\n\",";
1321 stream << indent_str;
1322 stream << " argv[0], c);";
1324 stream << indent_str;
1327 stream << indent_str;
1328 stream << " d->custom_optopt = c;";
1330 stream << indent_str;
1331 stream << " if (optstring[0] == ':')";
1333 stream << indent_str;
1334 stream << " c = ':';";
1336 stream << indent_str;
1339 stream << indent_str;
1340 stream << " c = '?';";
1342 stream << indent_str;
1343 stream << " } else";
1345 stream << indent_str;
1348 stream << indent_str;
1349 stream << " * We already incremented `custom_optind' once;";
1351 stream << indent_str;
1352 stream << " * increment it again when taking next ARGV-elt";
1354 stream << indent_str;
1355 stream << " * as argument.";
1357 stream << indent_str;
1360 stream << indent_str;
1361 stream << " d->custom_optarg = argv[d->custom_optind++];";
1363 stream << indent_str;
1364 stream << " d->nextchar = NULL;";
1366 stream << indent_str;
1369 stream << indent_str;
1372 stream << indent_str;
1373 stream << " return c;";
1375 stream << indent_str;
1378 stream << indent_str;
1380 stream << indent_str;
1383 stream << indent_str;
1384 stream << " * Scan elements of ARGV for option characters given in OPTSTRING.";
1386 stream << indent_str;
1389 stream << indent_str;
1390 stream << " * If an element of ARGV starts with '-', and is not exactly \"-\" or \"--\",";
1392 stream << indent_str;
1393 stream << " * then it is an option element. The characters of this element";
1395 stream << indent_str;
1396 stream << " * (aside from the initial '-') are option characters. If `getopt'";
1398 stream << indent_str;
1399 stream << " * is called repeatedly, it returns successively each of the option characters";
1401 stream << indent_str;
1402 stream << " * from each of the option elements.";
1404 stream << indent_str;
1407 stream << indent_str;
1408 stream << " * If `getopt' finds another option character, it returns that character,";
1410 stream << indent_str;
1411 stream << " * updating `custom_optind' and `nextchar' so that the next call to `getopt' can";
1413 stream << indent_str;
1414 stream << " * resume the scan with the following option character or ARGV-element.";
1416 stream << indent_str;
1419 stream << indent_str;
1420 stream << " * If there are no more option characters, `getopt' returns -1.";
1422 stream << indent_str;
1423 stream << " * Then `custom_optind' is the index in ARGV of the first ARGV-element";
1425 stream << indent_str;
1426 stream << " * that is not an option. (The ARGV-elements have been permuted";
1428 stream << indent_str;
1429 stream << " * so that those that are not options now come last.)";
1431 stream << indent_str;
1434 stream << indent_str;
1435 stream << " * OPTSTRING is a string containing the legitimate option characters.";
1437 stream << indent_str;
1438 stream << " * If an option character is seen that is not listed in OPTSTRING,";
1440 stream << indent_str;
1441 stream << " * return '?' after printing an error message. If you set `custom_opterr' to";
1443 stream << indent_str;
1444 stream << " * zero, the error message is suppressed but we still return '?'.";
1446 stream << indent_str;
1449 stream << indent_str;
1450 stream << " * If a char in OPTSTRING is followed by a colon, that means it wants an arg,";
1452 stream << indent_str;
1453 stream << " * so the following text in the same ARGV-element, or the text of the following";
1455 stream << indent_str;
1456 stream << " * ARGV-element, is returned in `custom_optarg'. Two colons mean an option that";
1458 stream << indent_str;
1459 stream << " * wants an optional arg; if there is text in the current ARGV-element,";
1461 stream << indent_str;
1462 stream << " * it is returned in `custom_optarg', otherwise `custom_optarg' is set to zero.";
1464 stream << indent_str;
1467 stream << indent_str;
1468 stream << " * If OPTSTRING starts with `-' or `+', it requests different methods of";
1470 stream << indent_str;
1471 stream << " * handling the non-option ARGV-elements.";
1473 stream << indent_str;
1474 stream << " * See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.";
1476 stream << indent_str;
1479 stream << indent_str;
1480 stream << " * Long-named options begin with `--' instead of `-'.";
1482 stream << indent_str;
1483 stream << " * Their names may be abbreviated as long as the abbreviation is unique";
1485 stream << indent_str;
1486 stream << " * or is an exact match for some defined option. If they have an";
1488 stream << indent_str;
1489 stream << " * argument, it follows the option name in the same ARGV-element, separated";
1491 stream << indent_str;
1492 stream << " * from the option name by a `=', or else the in next ARGV-element.";
1494 stream << indent_str;
1495 stream << " * When `getopt' finds a long-named option, it returns 0 if that option's";
1497 stream << indent_str;
1498 stream << " * `flag' field is nonzero, the value of the option's `val' field";
1500 stream << indent_str;
1501 stream << " * if the `flag' field is zero.";
1503 stream << indent_str;
1506 stream << indent_str;
1507 stream << " * The elements of ARGV aren't really const, because we permute them.";
1509 stream << indent_str;
1510 stream << " * But we pretend they're const in the prototype to be compatible";
1512 stream << indent_str;
1513 stream << " * with other systems.";
1515 stream << indent_str;
1518 stream << indent_str;
1519 stream << " * LONGOPTS is a vector of `struct option' terminated by an";
1521 stream << indent_str;
1522 stream << " * element containing a name which is zero.";
1524 stream << indent_str;
1527 stream << indent_str;
1528 stream << " * LONGIND returns the index in LONGOPT of the long-named option found.";
1530 stream << indent_str;
1531 stream << " * It is only valid when a long-named option has been found by the most";
1533 stream << indent_str;
1534 stream << " * recent call.";
1536 stream << indent_str;
1539 stream << indent_str;
1540 stream << " * Return the option character from OPTS just read. Return -1 when there are";
1542 stream << indent_str;
1543 stream << " * no more options. For unrecognized options, or options missing arguments,";
1545 stream << indent_str;
1546 stream << " * `custom_optopt' is set to the option letter, and '?' is returned.";
1548 stream << indent_str;
1551 stream << indent_str;
1552 stream << " * The OPTS string is a list of characters which are recognized option letters,";
1554 stream << indent_str;
1555 stream << " * optionally followed by colons, specifying that that letter takes an";
1557 stream << indent_str;
1558 stream << " * argument, to be placed in `custom_optarg'.";
1560 stream << indent_str;
1563 stream << indent_str;
1564 stream << " * If a letter in OPTS is followed by two colons, its argument is optional.";
1566 stream << indent_str;
1567 stream << " * This behavior is specific to the GNU `getopt'.";
1569 stream << indent_str;
1572 stream << indent_str;
1573 stream << " * The argument `--' causes premature termination of argument scanning,";
1575 stream << indent_str;
1576 stream << " * explicitly telling `getopt' that there are no more options. If OPTS begins";
1578 stream << indent_str;
1579 stream << " * with `--', then non-option arguments are treated as arguments to the option";
1581 stream << indent_str;
1582 stream << " * '\\0'. This behavior is specific to the GNU `getopt'.";
1584 stream << indent_str;
1587 stream << indent_str;
1589 stream << indent_str;
1590 stream << "static int getopt_internal_r(int argc, char *const *argv, const char *optstring,";
1592 stream << indent_str;
1593 stream << " const struct option *longopts, int *longind,";
1595 stream << indent_str;
1596 stream << " struct custom_getopt_data *d)";
1598 stream << indent_str;
1601 stream << indent_str;
1602 stream << " int ret, print_errors = d->custom_opterr;";
1604 stream << indent_str;
1606 stream << indent_str;
1607 stream << " if (optstring[0] == ':')";
1609 stream << indent_str;
1610 stream << " print_errors = 0;";
1612 stream << indent_str;
1613 stream << " if (argc < 1)";
1615 stream << indent_str;
1616 stream << " return -1;";
1618 stream << indent_str;
1619 stream << " d->custom_optarg = NULL;";
1621 stream << indent_str;
1623 stream << indent_str;
1626 stream << indent_str;
1627 stream << " * This is a big difference with GNU getopt, since optind == 0";
1629 stream << indent_str;
1630 stream << " * means initialization while here 1 means first call.";
1632 stream << indent_str;
1635 stream << indent_str;
1636 stream << " if (d->custom_optind == 0 || !d->initialized) {";
1638 stream << indent_str;
1639 stream << " if (d->custom_optind == 0)";
1641 stream << indent_str;
1642 stream << " d->custom_optind = 1; /* Don't scan ARGV[0], the program name. */";
1644 stream << indent_str;
1645 stream << " custom_getopt_initialize(d);";
1647 stream << indent_str;
1650 stream << indent_str;
1651 stream << " if (d->nextchar == NULL || *d->nextchar == '\\0') {";
1653 stream << indent_str;
1654 stream << " ret = shuffle_argv(argc, argv, longopts, d);";
1656 stream << indent_str;
1657 stream << " if (ret)";
1659 stream << indent_str;
1660 stream << " return ret;";
1662 stream << indent_str;
1665 stream << indent_str;
1666 stream << " if (longopts && (argv[d->custom_optind][1] == '-' ))";
1668 stream << indent_str;
1669 stream << " return check_long_opt(argc, argv, optstring, longopts,";
1671 stream << indent_str;
1672 stream << " longind, print_errors, d);";
1674 stream << indent_str;
1675 stream << " return check_short_opt(argc, argv, optstring, print_errors, d);";
1677 stream << indent_str;
1680 stream << indent_str;
1682 stream << indent_str;
1683 stream << "static int custom_getopt_internal(int argc, char *const *argv, const char *optstring,";
1685 stream << indent_str;
1686 stream << " const struct option *longopts, int *longind)";
1688 stream << indent_str;
1691 stream << indent_str;
1692 stream << " int result;";
1694 stream << indent_str;
1695 stream << " /* Keep a global copy of all internal members of d */";
1697 stream << indent_str;
1698 stream << " static struct custom_getopt_data d;";
1700 stream << indent_str;
1702 stream << indent_str;
1703 stream << " d.custom_optind = custom_optind;";
1705 stream << indent_str;
1706 stream << " d.custom_opterr = custom_opterr;";
1708 stream << indent_str;
1709 stream << " result = getopt_internal_r(argc, argv, optstring, longopts,";
1711 stream << indent_str;
1712 stream << " longind, &d);";
1714 stream << indent_str;
1715 stream << " custom_optind = d.custom_optind;";
1717 stream << indent_str;
1718 stream << " custom_optarg = d.custom_optarg;";
1720 stream << indent_str;
1721 stream << " custom_optopt = d.custom_optopt;";
1723 stream << indent_str;
1724 stream << " return result;";
1726 stream << indent_str;
1729 stream << indent_str;
1731 stream << indent_str;
1732 stream << "static int custom_getopt_long (int argc, char *const *argv, const char *options,";
1734 stream << indent_str;
1735 stream << " const struct option *long_options, int *opt_index)";
1737 stream << indent_str;
1740 stream << indent_str;
1741 stream << " return custom_getopt_internal(argc, argv, options, long_options,";
1743 stream << indent_str;
1744 stream << " opt_index);";
1746 stream << indent_str;
1749 stream << indent_str;