2 File autogenerated by gengetopt version 2.22
3 generated with the following command:
4 /usr/local/bin/gengetopt --output-dir=/home/tbaudier/vv/vv_static/vv/vv_bin/tools --arg-struct-name=args_info_clitkMIP --func-name=cmdline_parser_clitkMIP --file-name=clitkMIP_ggo --unamed-opts --conf-parser --include-getopt
6 The developers of gengetopt consider the fixed text that goes in all
7 gengetopt output files to be in the public domain:
8 we make no copyright claims on it.
11 /* If we use autoconf. */
21 #include "clitkMIP_ggo.h"
23 const char *args_info_clitkMIP_purpose = "";
25 const char *args_info_clitkMIP_usage = "Usage: clitkMIP [OPTIONS]... [FILES]...";
27 const char *args_info_clitkMIP_description = "";
29 const char *args_info_clitkMIP_help[] = {
30 " -h, --help Print help and exit",
31 " -V, --version Print version and exit",
32 " --config=STRING Config file",
33 " -v, --verbose Verbose (default=off)",
34 " --imagetypes Display allowed image types (default=off)",
35 " -i, --input=STRING Input image filename",
36 " -o, --output=STRING Output image filename",
37 " -d, --dimension=INT Dimension along which to projetct",
45 } cmdline_parser_clitkMIP_arg_type;
48 void clear_given (struct args_info_clitkMIP *args_info);
50 void clear_args (struct args_info_clitkMIP *args_info);
53 cmdline_parser_clitkMIP_internal (int argc, char * const *argv, struct args_info_clitkMIP *args_info,
54 struct cmdline_parser_clitkMIP_params *params, const char *additional_error);
57 cmdline_parser_clitkMIP_required2 (struct args_info_clitkMIP *args_info, const char *prog_name, const char *additional_error);
61 struct line_list * next;
64 static struct line_list *cmd_line_list = 0;
65 static struct line_list *cmd_line_list_tmp = 0;
70 /* free the list of a previous call */
73 while (cmd_line_list) {
74 cmd_line_list_tmp = cmd_line_list;
75 cmd_line_list = cmd_line_list->next;
76 free (cmd_line_list_tmp->string_arg);
77 free (cmd_line_list_tmp);
84 gengetopt_strdup (const char *s);
87 void clear_given (struct args_info_clitkMIP *args_info)
89 args_info->help_given = 0 ;
90 args_info->version_given = 0 ;
91 args_info->config_given = 0 ;
92 args_info->verbose_given = 0 ;
93 args_info->imagetypes_given = 0 ;
94 args_info->input_given = 0 ;
95 args_info->output_given = 0 ;
96 args_info->dimension_given = 0 ;
100 void clear_args (struct args_info_clitkMIP *args_info)
102 args_info->config_arg = NULL;
103 args_info->config_orig = NULL;
104 args_info->verbose_flag = 0;
105 args_info->imagetypes_flag = 0;
106 args_info->input_arg = NULL;
107 args_info->input_orig = NULL;
108 args_info->output_arg = NULL;
109 args_info->output_orig = NULL;
110 args_info->dimension_orig = NULL;
115 void init_args_info(struct args_info_clitkMIP *args_info)
119 args_info->help_help = args_info_clitkMIP_help[0] ;
120 args_info->version_help = args_info_clitkMIP_help[1] ;
121 args_info->config_help = args_info_clitkMIP_help[2] ;
122 args_info->verbose_help = args_info_clitkMIP_help[3] ;
123 args_info->imagetypes_help = args_info_clitkMIP_help[4] ;
124 args_info->input_help = args_info_clitkMIP_help[5] ;
125 args_info->output_help = args_info_clitkMIP_help[6] ;
126 args_info->dimension_help = args_info_clitkMIP_help[7] ;
131 cmdline_parser_clitkMIP_print_version (void)
133 printf ("%s %s\n", CMDLINE_PARSER_CLITKMIP_PACKAGE, CMDLINE_PARSER_CLITKMIP_VERSION);
136 static void print_help_common(void) {
137 cmdline_parser_clitkMIP_print_version ();
139 if (strlen(args_info_clitkMIP_purpose) > 0)
140 printf("\n%s\n", args_info_clitkMIP_purpose);
142 if (strlen(args_info_clitkMIP_usage) > 0)
143 printf("\n%s\n", args_info_clitkMIP_usage);
147 if (strlen(args_info_clitkMIP_description) > 0)
148 printf("%s\n", args_info_clitkMIP_description);
152 cmdline_parser_clitkMIP_print_help (void)
156 while (args_info_clitkMIP_help[i])
157 printf("%s\n", args_info_clitkMIP_help[i++]);
161 cmdline_parser_clitkMIP_init (struct args_info_clitkMIP *args_info)
163 clear_given (args_info);
164 clear_args (args_info);
165 init_args_info (args_info);
167 args_info->inputs = NULL;
168 args_info->inputs_num = 0;
172 cmdline_parser_clitkMIP_params_init(struct cmdline_parser_clitkMIP_params *params)
176 params->override = 0;
177 params->initialize = 1;
178 params->check_required = 1;
179 params->check_ambiguity = 0;
180 params->print_errors = 1;
184 struct cmdline_parser_clitkMIP_params *
185 cmdline_parser_clitkMIP_params_create(void)
187 struct cmdline_parser_clitkMIP_params *params =
188 (struct cmdline_parser_clitkMIP_params *)malloc(sizeof(struct cmdline_parser_clitkMIP_params));
189 cmdline_parser_clitkMIP_params_init(params);
194 free_string_field (char **s)
205 cmdline_parser_clitkMIP_release (struct args_info_clitkMIP *args_info)
208 free_string_field (&(args_info->config_arg));
209 free_string_field (&(args_info->config_orig));
210 free_string_field (&(args_info->input_arg));
211 free_string_field (&(args_info->input_orig));
212 free_string_field (&(args_info->output_arg));
213 free_string_field (&(args_info->output_orig));
214 free_string_field (&(args_info->dimension_orig));
217 for (i = 0; i < args_info->inputs_num; ++i)
218 free (args_info->inputs [i]);
220 if (args_info->inputs_num)
221 free (args_info->inputs);
223 clear_given (args_info);
228 write_into_file(FILE *outfile, const char *opt, const char *arg, char *values[])
231 fprintf(outfile, "%s=\"%s\"\n", opt, arg);
233 fprintf(outfile, "%s\n", opt);
239 cmdline_parser_clitkMIP_dump(FILE *outfile, struct args_info_clitkMIP *args_info)
245 fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_CLITKMIP_PACKAGE);
249 if (args_info->help_given)
250 write_into_file(outfile, "help", 0, 0 );
251 if (args_info->version_given)
252 write_into_file(outfile, "version", 0, 0 );
253 if (args_info->config_given)
254 write_into_file(outfile, "config", args_info->config_orig, 0);
255 if (args_info->verbose_given)
256 write_into_file(outfile, "verbose", 0, 0 );
257 if (args_info->imagetypes_given)
258 write_into_file(outfile, "imagetypes", 0, 0 );
259 if (args_info->input_given)
260 write_into_file(outfile, "input", args_info->input_orig, 0);
261 if (args_info->output_given)
262 write_into_file(outfile, "output", args_info->output_orig, 0);
263 if (args_info->dimension_given)
264 write_into_file(outfile, "dimension", args_info->dimension_orig, 0);
272 cmdline_parser_clitkMIP_file_save(const char *filename, struct args_info_clitkMIP *args_info)
277 outfile = fopen(filename, "w");
281 fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_CLITKMIP_PACKAGE, filename);
285 i = cmdline_parser_clitkMIP_dump(outfile, args_info);
292 cmdline_parser_clitkMIP_free (struct args_info_clitkMIP *args_info)
294 cmdline_parser_clitkMIP_release (args_info);
297 /** @brief replacement of strdup, which is not standard */
299 gengetopt_strdup (const char *s)
305 result = (char*)malloc(strlen(s) + 1);
306 if (result == (char*)0)
313 cmdline_parser_clitkMIP (int argc, char * const *argv, struct args_info_clitkMIP *args_info)
315 return cmdline_parser_clitkMIP2 (argc, argv, args_info, 0, 1, 1);
319 cmdline_parser_clitkMIP_ext (int argc, char * const *argv, struct args_info_clitkMIP *args_info,
320 struct cmdline_parser_clitkMIP_params *params)
323 result = cmdline_parser_clitkMIP_internal (argc, argv, args_info, params, NULL);
325 if (result == EXIT_FAILURE)
327 cmdline_parser_clitkMIP_free (args_info);
335 cmdline_parser_clitkMIP2 (int argc, char * const *argv, struct args_info_clitkMIP *args_info, int override, int initialize, int check_required)
338 struct cmdline_parser_clitkMIP_params params;
340 params.override = override;
341 params.initialize = initialize;
342 params.check_required = check_required;
343 params.check_ambiguity = 0;
344 params.print_errors = 1;
346 result = cmdline_parser_clitkMIP_internal (argc, argv, args_info, ¶ms, NULL);
348 if (result == EXIT_FAILURE)
350 cmdline_parser_clitkMIP_free (args_info);
358 cmdline_parser_clitkMIP_required (struct args_info_clitkMIP *args_info, const char *prog_name)
360 int result = EXIT_SUCCESS;
362 if (cmdline_parser_clitkMIP_required2(args_info, prog_name, NULL) > 0)
363 result = EXIT_FAILURE;
365 if (result == EXIT_FAILURE)
367 cmdline_parser_clitkMIP_free (args_info);
375 cmdline_parser_clitkMIP_required2 (struct args_info_clitkMIP *args_info, const char *prog_name, const char *additional_error)
379 /* checks for required options */
380 if (! args_info->input_given)
382 fprintf (stderr, "%s: '--input' ('-i') option required%s\n", prog_name, (additional_error ? additional_error : ""));
386 if (! args_info->output_given)
388 fprintf (stderr, "%s: '--output' ('-o') option required%s\n", prog_name, (additional_error ? additional_error : ""));
392 if (! args_info->dimension_given)
394 fprintf (stderr, "%s: '--dimension' ('-d') option required%s\n", prog_name, (additional_error ? additional_error : ""));
399 /* checks for dependences among options */
405 * Extracted from the glibc source tree, version 2.3.6
407 * Licensed under the GPL as per the whole glibc source tree.
409 * This file was modified so that getopt_long can be called
410 * many times without risking previous memory to be spoiled.
412 * Modified by Andre Noll and Lorenzo Bettini for use in
413 * GNU gengetopt generated files.
418 * we must include anything we need since this file is not thought to be
419 * inserted in a file already using getopt.h
427 /* has_arg can't be an enum because some compilers complain about
428 type mismatches in all the code that assumes it is an int. */
434 /* For communication from `getopt' to the caller.
435 When `getopt' finds an option that takes an argument,
436 the argument value is returned here.
437 Also, when `ordering' is RETURN_IN_ORDER,
438 each non-option ARGV-element is returned here. */
442 /* Index in ARGV of the next element to be scanned.
443 This is used for communication to and from the caller
444 and for communication between successive calls to `getopt'.
446 On entry to `getopt', zero means this is the first call; initialize.
448 When `getopt' returns -1, this is the index of the first of the
449 non-option elements that the caller should itself scan.
451 Otherwise, `optind' communicates from one call to the next
452 how much of ARGV has been scanned so far. */
456 /* Callers store zero here to inhibit the error message `getopt' prints
457 for unrecognized options. */
461 /* Set to an option character which was unrecognized. */
465 /* This version of `getopt' appears to the caller like standard Unix `getopt'
466 but it behaves differently for the user, since it allows the user
467 to intersperse the options with the other arguments.
469 As `getopt' works, it permutes the elements of ARGV so that,
470 when it is done, all the options precede everything else. Thus
471 all application programs are extended to handle flexible argument order.
474 If the field `flag' is not NULL, it points to a variable that is set
475 to the value given in the field `val' when the option is found, but
476 left unchanged if the option is not found.
478 To have a long-named option do something other than set an `int' to
479 a compiled-in constant, such as set a value from `custom_optarg', set the
480 option's `flag' field to zero and its `val' field to a nonzero
481 value (the equivalent single-letter option character, if there is
482 one). For long options that have a zero `flag' field, `getopt'
483 returns the contents of the `val' field. */
485 /* Names for the values of the `has_arg' field of `struct option'. */
487 #define no_argument 0
490 #ifndef required_argument
491 #define required_argument 1
494 #ifndef optional_argument
495 #define optional_argument 2
498 struct custom_getopt_data {
500 * These have exactly the same meaning as the corresponding global variables,
501 * except that they are used for the reentrant versions of getopt.
508 /* True if the internal members have been initialized. */
512 * The next char to be scanned in the option-element in which the last option
513 * character we returned was found. This allows us to pick up the scan where
514 * we left off. If this is zero, or a null string, it means resume the scan by
515 * advancing to the next ARGV-element.
520 * Describe the part of ARGV that contains non-options that have been skipped.
521 * `first_nonopt' is the index in ARGV of the first of them; `last_nonopt' is
522 * the index after the last of them.
529 * the variables optarg, optind, opterr and optopt are renamed with
530 * the custom_ prefix so that they don't interfere with getopt ones.
532 * Moreover they're static so they are visible only from within the
533 * file where this very file will be included.
537 * For communication from `custom_getopt' to the caller. When `custom_getopt' finds an
538 * option that takes an argument, the argument value is returned here.
540 static char *custom_optarg;
543 * Index in ARGV of the next element to be scanned. This is used for
544 * communication to and from the caller and for communication between
545 * successive calls to `custom_getopt'.
547 * On entry to `custom_getopt', 1 means this is the first call; initialize.
549 * When `custom_getopt' returns -1, this is the index of the first of the non-option
550 * elements that the caller should itself scan.
552 * Otherwise, `custom_optind' communicates from one call to the next how much of ARGV
553 * has been scanned so far.
555 * 1003.2 says this must be 1 before any call.
557 static int custom_optind = 1;
560 * Callers store zero here to inhibit the error message for unrecognized
563 static int custom_opterr = 1;
566 * Set to an option character which was unrecognized. This must be initialized
567 * on some systems to avoid linking in the system's own getopt implementation.
569 static int custom_optopt = '?';
572 * Exchange two adjacent subsequences of ARGV. One subsequence is elements
573 * [first_nonopt,last_nonopt) which contains all the non-options that have been
574 * skipped so far. The other is elements [last_nonopt,custom_optind), which contains
575 * all the options processed since those non-options were skipped.
576 * `first_nonopt' and `last_nonopt' are relocated so that they describe the new
577 * indices of the non-options in ARGV after they are moved.
579 static void exchange(char **argv, struct custom_getopt_data *d)
581 int bottom = d->first_nonopt;
582 int middle = d->last_nonopt;
583 int top = d->custom_optind;
587 * Exchange the shorter segment with the far end of the longer segment.
588 * That puts the shorter segment into the right place. It leaves the
589 * longer segment in the right place overall, but it consists of two
590 * parts that need to be swapped next.
592 while (top > middle && middle > bottom) {
593 if (top - middle > middle - bottom) {
594 /* Bottom segment is the short one. */
595 int len = middle - bottom;
598 /* Swap it with the top part of the top segment. */
599 for (i = 0; i < len; i++) {
600 tem = argv[bottom + i];
602 argv[top - (middle - bottom) + i];
603 argv[top - (middle - bottom) + i] = tem;
605 /* Exclude the moved bottom segment from further swapping. */
608 /* Top segment is the short one. */
609 int len = top - middle;
612 /* Swap it with the bottom part of the bottom segment. */
613 for (i = 0; i < len; i++) {
614 tem = argv[bottom + i];
615 argv[bottom + i] = argv[middle + i];
616 argv[middle + i] = tem;
618 /* Exclude the moved top segment from further swapping. */
622 /* Update records for the slots the non-options now occupy. */
623 d->first_nonopt += (d->custom_optind - d->last_nonopt);
624 d->last_nonopt = d->custom_optind;
627 /* Initialize the internal data when the first call is made. */
628 static void custom_getopt_initialize(struct custom_getopt_data *d)
631 * Start processing options with ARGV-element 1 (since ARGV-element 0
632 * is the program name); the sequence of previously skipped non-option
633 * ARGV-elements is empty.
635 d->first_nonopt = d->last_nonopt = d->custom_optind;
640 #define NONOPTION_P (argv[d->custom_optind][0] != '-' || argv[d->custom_optind][1] == '\0')
642 /* return: zero: continue, nonzero: return given value to user */
643 static int shuffle_argv(int argc, char *const *argv,const struct option *longopts,
644 struct custom_getopt_data *d)
647 * Give FIRST_NONOPT & LAST_NONOPT rational values if CUSTOM_OPTIND has been
648 * moved back by the user (who may also have changed the arguments).
650 if (d->last_nonopt > d->custom_optind)
651 d->last_nonopt = d->custom_optind;
652 if (d->first_nonopt > d->custom_optind)
653 d->first_nonopt = d->custom_optind;
655 * If we have just processed some options following some
656 * non-options, exchange them so that the options come first.
658 if (d->first_nonopt != d->last_nonopt &&
659 d->last_nonopt != d->custom_optind)
660 exchange((char **) argv, d);
661 else if (d->last_nonopt != d->custom_optind)
662 d->first_nonopt = d->custom_optind;
664 * Skip any additional non-options and extend the range of
665 * non-options previously skipped.
667 while (d->custom_optind < argc && NONOPTION_P)
669 d->last_nonopt = d->custom_optind;
671 * The special ARGV-element `--' means premature end of options. Skip
672 * it like a null option, then exchange with previous non-options as if
673 * it were an option, then skip everything else like a non-option.
675 if (d->custom_optind != argc && !strcmp(argv[d->custom_optind], "--")) {
677 if (d->first_nonopt != d->last_nonopt
678 && d->last_nonopt != d->custom_optind)
679 exchange((char **) argv, d);
680 else if (d->first_nonopt == d->last_nonopt)
681 d->first_nonopt = d->custom_optind;
682 d->last_nonopt = argc;
683 d->custom_optind = argc;
686 * If we have done all the ARGV-elements, stop the scan and back over
687 * any non-options that we skipped and permuted.
689 if (d->custom_optind == argc) {
691 * Set the next-arg-index to point at the non-options that we
692 * previously skipped, so the caller will digest them.
694 if (d->first_nonopt != d->last_nonopt)
695 d->custom_optind = d->first_nonopt;
699 * If we have come to a non-option and did not permute it, either stop
700 * the scan or describe it to the caller and pass it by.
703 d->custom_optarg = argv[d->custom_optind++];
707 * We have found another option-ARGV-element. Skip the initial
710 d->nextchar = (argv[d->custom_optind] + 1 + (longopts != NULL && argv[d->custom_optind][1] == '-'));
715 * Check whether the ARGV-element is a long option.
717 * If there's a long option "fubar" and the ARGV-element is "-fu", consider
718 * that an abbreviation of the long option, just like "--fu", and not "-f" with
721 * This distinction seems to be the most useful approach.
724 static int check_long_opt(int argc, char *const *argv, const char *optstring,
725 const struct option *longopts, int *longind,
726 int print_errors, struct custom_getopt_data *d)
729 const struct option *p;
730 const struct option *pfound = NULL;
736 for (nameend = d->nextchar; *nameend && *nameend != '='; nameend++)
739 /* Test all long options for either exact match or abbreviated matches */
740 for (p = longopts, option_index = 0; p->name; p++, option_index++)
741 if (!strncmp(p->name, d->nextchar, nameend - d->nextchar)) {
742 if ((unsigned int) (nameend - d->nextchar)
743 == (unsigned int) strlen(p->name)) {
744 /* Exact match found. */
746 indfound = option_index;
749 } else if (pfound == NULL) {
750 /* First nonexact match found. */
752 indfound = option_index;
753 } else if (pfound->has_arg != p->has_arg
754 || pfound->flag != p->flag
755 || pfound->val != p->val)
756 /* Second or later nonexact match found. */
759 if (ambig && !exact) {
762 "%s: option `%s' is ambiguous\n",
763 argv[0], argv[d->custom_optind]);
765 d->nextchar += strlen(d->nextchar);
767 d->custom_optopt = 0;
771 option_index = indfound;
774 if (pfound->has_arg != no_argument)
775 d->custom_optarg = nameend + 1;
778 if (argv[d->custom_optind - 1][1] == '-') {
780 fprintf(stderr, "%s: option `--%s' doesn't allow an argument\n",
781 argv[0], pfound->name);
783 /* +option or -option */
784 fprintf(stderr, "%s: option `%c%s' doesn't allow an argument\n",
785 argv[0], argv[d->custom_optind - 1][0], pfound->name);
789 d->nextchar += strlen(d->nextchar);
790 d->custom_optopt = pfound->val;
793 } else if (pfound->has_arg == required_argument) {
794 if (d->custom_optind < argc)
795 d->custom_optarg = argv[d->custom_optind++];
799 "%s: option `%s' requires an argument\n",
801 argv[d->custom_optind - 1]);
803 d->nextchar += strlen(d->nextchar);
804 d->custom_optopt = pfound->val;
805 return optstring[0] == ':' ? ':' : '?';
808 d->nextchar += strlen(d->nextchar);
810 *longind = option_index;
812 *(pfound->flag) = pfound->val;
818 * Can't find it as a long option. If this is not getopt_long_only, or
819 * the option starts with '--' or is not a valid short option, then
820 * it's an error. Otherwise interpret it as a short option.
823 if (argv[d->custom_optind][1] == '-') {
826 "%s: unrecognized option `--%s'\n",
827 argv[0], d->nextchar);
829 /* +option or -option */
831 "%s: unrecognized option `%c%s'\n",
832 argv[0], argv[d->custom_optind][0],
836 d->nextchar = (char *) "";
838 d->custom_optopt = 0;
842 static int check_short_opt(int argc, char *const *argv, const char *optstring,
843 int print_errors, struct custom_getopt_data *d)
845 char c = *d->nextchar++;
846 char *temp = strchr(optstring, c);
848 /* Increment `custom_optind' when we start to process its last character. */
849 if (*d->nextchar == '\0')
851 if (!temp || c == ':') {
853 fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
855 d->custom_optopt = c;
858 if (temp[1] == ':') {
859 if (temp[2] == ':') {
860 /* This is an option that accepts an argument optionally. */
861 if (*d->nextchar != '\0') {
862 d->custom_optarg = d->nextchar;
865 d->custom_optarg = NULL;
868 /* This is an option that requires an argument. */
869 if (*d->nextchar != '\0') {
870 d->custom_optarg = d->nextchar;
872 * If we end this ARGV-element by taking the
873 * rest as an arg, we must advance to the next
877 } else if (d->custom_optind == argc) {
880 "%s: option requires an argument -- %c\n",
883 d->custom_optopt = c;
884 if (optstring[0] == ':')
890 * We already incremented `custom_optind' once;
891 * increment it again when taking next ARGV-elt
894 d->custom_optarg = argv[d->custom_optind++];
902 * Scan elements of ARGV for option characters given in OPTSTRING.
904 * If an element of ARGV starts with '-', and is not exactly "-" or "--",
905 * then it is an option element. The characters of this element
906 * (aside from the initial '-') are option characters. If `getopt'
907 * is called repeatedly, it returns successively each of the option characters
908 * from each of the option elements.
910 * If `getopt' finds another option character, it returns that character,
911 * updating `custom_optind' and `nextchar' so that the next call to `getopt' can
912 * resume the scan with the following option character or ARGV-element.
914 * If there are no more option characters, `getopt' returns -1.
915 * Then `custom_optind' is the index in ARGV of the first ARGV-element
916 * that is not an option. (The ARGV-elements have been permuted
917 * so that those that are not options now come last.)
919 * OPTSTRING is a string containing the legitimate option characters.
920 * If an option character is seen that is not listed in OPTSTRING,
921 * return '?' after printing an error message. If you set `custom_opterr' to
922 * zero, the error message is suppressed but we still return '?'.
924 * If a char in OPTSTRING is followed by a colon, that means it wants an arg,
925 * so the following text in the same ARGV-element, or the text of the following
926 * ARGV-element, is returned in `custom_optarg'. Two colons mean an option that
927 * wants an optional arg; if there is text in the current ARGV-element,
928 * it is returned in `custom_optarg', otherwise `custom_optarg' is set to zero.
930 * If OPTSTRING starts with `-' or `+', it requests different methods of
931 * handling the non-option ARGV-elements.
932 * See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
934 * Long-named options begin with `--' instead of `-'.
935 * Their names may be abbreviated as long as the abbreviation is unique
936 * or is an exact match for some defined option. If they have an
937 * argument, it follows the option name in the same ARGV-element, separated
938 * from the option name by a `=', or else the in next ARGV-element.
939 * When `getopt' finds a long-named option, it returns 0 if that option's
940 * `flag' field is nonzero, the value of the option's `val' field
941 * if the `flag' field is zero.
943 * The elements of ARGV aren't really const, because we permute them.
944 * But we pretend they're const in the prototype to be compatible
945 * with other systems.
947 * LONGOPTS is a vector of `struct option' terminated by an
948 * element containing a name which is zero.
950 * LONGIND returns the index in LONGOPT of the long-named option found.
951 * It is only valid when a long-named option has been found by the most
954 * Return the option character from OPTS just read. Return -1 when there are
955 * no more options. For unrecognized options, or options missing arguments,
956 * `custom_optopt' is set to the option letter, and '?' is returned.
958 * The OPTS string is a list of characters which are recognized option letters,
959 * optionally followed by colons, specifying that that letter takes an
960 * argument, to be placed in `custom_optarg'.
962 * If a letter in OPTS is followed by two colons, its argument is optional.
963 * This behavior is specific to the GNU `getopt'.
965 * The argument `--' causes premature termination of argument scanning,
966 * explicitly telling `getopt' that there are no more options. If OPTS begins
967 * with `--', then non-option arguments are treated as arguments to the option
968 * '\0'. This behavior is specific to the GNU `getopt'.
971 static int getopt_internal_r(int argc, char *const *argv, const char *optstring,
972 const struct option *longopts, int *longind,
973 struct custom_getopt_data *d)
975 int ret, print_errors = d->custom_opterr;
977 if (optstring[0] == ':')
981 d->custom_optarg = NULL;
984 * This is a big difference with GNU getopt, since optind == 0
985 * means initialization while here 1 means first call.
987 if (d->custom_optind == 0 || !d->initialized) {
988 if (d->custom_optind == 0)
989 d->custom_optind = 1; /* Don't scan ARGV[0], the program name. */
990 custom_getopt_initialize(d);
992 if (d->nextchar == NULL || *d->nextchar == '\0') {
993 ret = shuffle_argv(argc, argv, longopts, d);
997 if (longopts && (argv[d->custom_optind][1] == '-' ))
998 return check_long_opt(argc, argv, optstring, longopts,
999 longind, print_errors, d);
1000 return check_short_opt(argc, argv, optstring, print_errors, d);
1003 static int custom_getopt_internal(int argc, char *const *argv, const char *optstring,
1004 const struct option *longopts, int *longind)
1007 /* Keep a global copy of all internal members of d */
1008 static struct custom_getopt_data d;
1010 d.custom_optind = custom_optind;
1011 d.custom_opterr = custom_opterr;
1012 result = getopt_internal_r(argc, argv, optstring, longopts,
1014 custom_optind = d.custom_optind;
1015 custom_optarg = d.custom_optarg;
1016 custom_optopt = d.custom_optopt;
1020 static int custom_getopt_long (int argc, char *const *argv, const char *options,
1021 const struct option *long_options, int *opt_index)
1023 return custom_getopt_internal(argc, argv, options, long_options,
1028 static char *package_name = 0;
1031 * @brief updates an option
1032 * @param field the generic pointer to the field to update
1033 * @param orig_field the pointer to the orig field
1034 * @param field_given the pointer to the number of occurrence of this option
1035 * @param prev_given the pointer to the number of occurrence already seen
1036 * @param value the argument for this option (if null no arg was specified)
1037 * @param possible_values the possible values for this option (if specified)
1038 * @param default_value the default value (in case the option only accepts fixed values)
1039 * @param arg_type the type of this option
1040 * @param check_ambiguity @see cmdline_parser_clitkMIP_params.check_ambiguity
1041 * @param override @see cmdline_parser_clitkMIP_params.override
1042 * @param no_free whether to free a possible previous value
1043 * @param multiple_option whether this is a multiple option
1044 * @param long_opt the corresponding long option
1045 * @param short_opt the corresponding short option (or '-' if none)
1046 * @param additional_error possible further error specification
1049 int update_arg(void *field, char **orig_field,
1050 unsigned int *field_given, unsigned int *prev_given,
1051 char *value, char *possible_values[], const char *default_value,
1052 cmdline_parser_clitkMIP_arg_type arg_type,
1053 int check_ambiguity, int override,
1054 int no_free, int multiple_option,
1055 const char *long_opt, char short_opt,
1056 const char *additional_error)
1058 char *stop_char = 0;
1059 const char *val = value;
1061 char **string_field;
1066 if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
1068 if (short_opt != '-')
1069 fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
1070 package_name, long_opt, short_opt,
1071 (additional_error ? additional_error : ""));
1073 fprintf (stderr, "%s: `--%s' option given more than once%s\n",
1074 package_name, long_opt,
1075 (additional_error ? additional_error : ""));
1076 return 1; /* failure */
1080 if (field_given && *field_given && ! override)
1086 if (possible_values)
1087 val = possible_values[found];
1091 *((int *)field) = !*((int *)field);
1094 if (val) *((int *)field) = strtol (val, &stop_char, 0);
1098 string_field = (char **)field;
1099 if (!no_free && *string_field)
1100 free (*string_field); /* free previous string */
1101 *string_field = gengetopt_strdup (val);
1108 /* check numeric conversion */
1111 if (val && !(stop_char && *stop_char == '\0')) {
1112 fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
1113 return 1; /* failure */
1120 /* store the original value */
1126 if (value && orig_field) {
1128 *orig_field = value;
1131 free (*orig_field); /* free previous string */
1132 *orig_field = gengetopt_strdup (value);
1142 cmdline_parser_clitkMIP_internal (int argc, char * const *argv, struct args_info_clitkMIP *args_info,
1143 struct cmdline_parser_clitkMIP_params *params, const char *additional_error)
1145 int c; /* Character of the parsed option. */
1148 struct args_info_clitkMIP local_args_info;
1153 int check_ambiguity;
1155 package_name = argv[0];
1157 override = params->override;
1158 initialize = params->initialize;
1159 check_required = params->check_required;
1160 check_ambiguity = params->check_ambiguity;
1163 cmdline_parser_clitkMIP_init (args_info);
1165 cmdline_parser_clitkMIP_init (&local_args_info);
1169 opterr = params->print_errors;
1174 int option_index = 0;
1176 static struct option long_options[] = {
1177 { "help", 0, NULL, 'h' },
1178 { "version", 0, NULL, 'V' },
1179 { "config", 1, NULL, 0 },
1180 { "verbose", 0, NULL, 'v' },
1181 { "imagetypes", 0, NULL, 0 },
1182 { "input", 1, NULL, 'i' },
1183 { "output", 1, NULL, 'o' },
1184 { "dimension", 1, NULL, 'd' },
1185 { NULL, 0, NULL, 0 }
1188 custom_optarg = optarg;
1189 custom_optind = optind;
1190 custom_opterr = opterr;
1191 custom_optopt = optopt;
1193 c = custom_getopt_long (argc, argv, "hVvi:o:d:", long_options, &option_index);
1195 optarg = custom_optarg;
1196 optind = custom_optind;
1197 opterr = custom_opterr;
1198 optopt = custom_optopt;
1200 if (c == -1) break; /* Exit from `while (1)' loop. */
1204 case 'h': /* Print help and exit. */
1205 cmdline_parser_clitkMIP_print_help ();
1206 cmdline_parser_clitkMIP_free (&local_args_info);
1207 exit (EXIT_SUCCESS);
1209 case 'V': /* Print version and exit. */
1210 cmdline_parser_clitkMIP_print_version ();
1211 cmdline_parser_clitkMIP_free (&local_args_info);
1212 exit (EXIT_SUCCESS);
1214 case 'v': /* Verbose. */
1217 if (update_arg((void *)&(args_info->verbose_flag), 0, &(args_info->verbose_given),
1218 &(local_args_info.verbose_given), optarg, 0, 0, ARG_FLAG,
1219 check_ambiguity, override, 1, 0, "verbose", 'v',
1224 case 'i': /* Input image filename. */
1227 if (update_arg( (void *)&(args_info->input_arg),
1228 &(args_info->input_orig), &(args_info->input_given),
1229 &(local_args_info.input_given), optarg, 0, 0, ARG_STRING,
1230 check_ambiguity, override, 0, 0,
1236 case 'o': /* Output image filename. */
1239 if (update_arg( (void *)&(args_info->output_arg),
1240 &(args_info->output_orig), &(args_info->output_given),
1241 &(local_args_info.output_given), optarg, 0, 0, ARG_STRING,
1242 check_ambiguity, override, 0, 0,
1248 case 'd': /* Dimension along which to projetct. */
1251 if (update_arg( (void *)&(args_info->dimension_arg),
1252 &(args_info->dimension_orig), &(args_info->dimension_given),
1253 &(local_args_info.dimension_given), optarg, 0, 0, ARG_INT,
1254 check_ambiguity, override, 0, 0,
1261 case 0: /* Long option with no short option */
1263 if (strcmp (long_options[option_index].name, "config") == 0)
1267 if (update_arg( (void *)&(args_info->config_arg),
1268 &(args_info->config_orig), &(args_info->config_given),
1269 &(local_args_info.config_given), optarg, 0, 0, ARG_STRING,
1270 check_ambiguity, override, 0, 0,
1276 /* Display allowed image types. */
1277 else if (strcmp (long_options[option_index].name, "imagetypes") == 0)
1281 if (update_arg((void *)&(args_info->imagetypes_flag), 0, &(args_info->imagetypes_given),
1282 &(local_args_info.imagetypes_given), optarg, 0, 0, ARG_FLAG,
1283 check_ambiguity, override, 1, 0, "imagetypes", '-',
1290 case '?': /* Invalid option. */
1291 /* `getopt_long' already printed an error message. */
1294 default: /* bug: option not considered. */
1295 fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_CLITKMIP_PACKAGE, c, (additional_error ? additional_error : ""));
1304 error += cmdline_parser_clitkMIP_required2 (args_info, argv[0], additional_error);
1307 cmdline_parser_clitkMIP_release (&local_args_info);
1310 return (EXIT_FAILURE);
1315 int found_prog_name = 0;
1316 /* whether program name, i.e., argv[0], is in the remaining args
1317 (this may happen with some implementations of getopt,
1318 but surely not with the one included by gengetopt) */
1321 args_info->inputs_num = argc - optind - found_prog_name;
1323 (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
1324 while (optind < argc)
1325 args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind++]) ;
1332 cmdline_parser_clitkMIP_release (&local_args_info);
1333 return (EXIT_FAILURE);
1336 #ifndef CONFIG_FILE_LINE_SIZE
1337 #define CONFIG_FILE_LINE_SIZE 2048
1339 #define ADDITIONAL_ERROR " in configuration file "
1341 #define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)
1342 /* 3 is for "--" and "=" */
1345 _cmdline_parser_clitkMIP_configfile (char * const filename, int *my_argc)
1348 char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE+1];
1349 char linebuf[CONFIG_FILE_LINE_SIZE];
1351 int result = 0, equal;
1354 size_t len, next_token;
1357 if ((file = fopen(filename, "r")) == NULL)
1359 fprintf (stderr, "%s: Error opening configuration file '%s'\n",
1360 CMDLINE_PARSER_CLITKMIP_PACKAGE, filename);
1361 return EXIT_FAILURE;
1364 while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != NULL)
1368 len = strlen(linebuf);
1369 if (len > (CONFIG_FILE_LINE_BUFFER_SIZE-1))
1371 fprintf (stderr, "%s:%s:%d: Line too long in configuration file\n",
1372 CMDLINE_PARSER_CLITKMIP_PACKAGE, filename, line_num);
1373 result = EXIT_FAILURE;
1377 /* find first non-whitespace character in the line */
1378 next_token = strspn (linebuf, " \t\r\n");
1379 str_index = linebuf + next_token;
1381 if ( str_index[0] == '\0' || str_index[0] == '#')
1382 continue; /* empty line or comment line is skipped */
1386 /* truncate fopt at the end of the first non-valid character */
1387 next_token = strcspn (fopt, " \t\r\n=");
1389 if (fopt[next_token] == '\0') /* the line is over */
1396 /* remember if equal sign is present */
1397 equal = (fopt[next_token] == '=');
1398 fopt[next_token++] = '\0';
1400 /* advance pointers to the next token after the end of fopt */
1401 next_token += strspn (fopt + next_token, " \t\r\n");
1403 /* check for the presence of equal sign, and if so, skip it */
1405 if ((equal = (fopt[next_token] == '=')))
1408 next_token += strspn (fopt + next_token, " \t\r\n");
1410 str_index += next_token;
1414 if ( farg[0] == '\"' || farg[0] == '\'' )
1415 { /* quoted argument */
1416 str_index = strchr (++farg, str_index[0] ); /* skip opening quote */
1421 "%s:%s:%d: unterminated string in configuration file\n",
1422 CMDLINE_PARSER_CLITKMIP_PACKAGE, filename, line_num);
1423 result = EXIT_FAILURE;
1428 { /* read up the remaining part up to a delimiter */
1429 next_token = strcspn (farg, " \t\r\n#\'\"");
1430 str_index += next_token;
1433 /* truncate farg at the delimiter and store it for further check */
1434 delimiter = *str_index, *str_index++ = '\0';
1436 /* everything but comment is illegal at the end of line */
1437 if (delimiter != '\0' && delimiter != '#')
1439 str_index += strspn(str_index, " \t\r\n");
1440 if (*str_index != '\0' && *str_index != '#')
1444 "%s:%s:%d: malformed string in configuration file\n",
1445 CMDLINE_PARSER_CLITKMIP_PACKAGE, filename, line_num);
1446 result = EXIT_FAILURE;
1452 if (!strcmp(fopt,"include")) {
1453 if (farg && *farg) {
1454 result = _cmdline_parser_clitkMIP_configfile(farg, my_argc);
1456 fprintf(stderr, "%s:%s:%d: include requires a filename argument.\n",
1457 CMDLINE_PARSER_CLITKMIP_PACKAGE, filename, line_num);
1462 strcat (my_argv, len > 1 ? "--" : "-");
1463 strcat (my_argv, fopt);
1464 if (len > 1 && ((farg && *farg) || equal))
1465 strcat (my_argv, "=");
1467 strcat (my_argv, farg);
1470 cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
1471 cmd_line_list_tmp->next = cmd_line_list;
1472 cmd_line_list = cmd_line_list_tmp;
1473 cmd_line_list->string_arg = gengetopt_strdup(my_argv);
1482 cmdline_parser_clitkMIP_configfile (char * const filename,
1483 struct args_info_clitkMIP *args_info,
1484 int override, int initialize, int check_required)
1486 struct cmdline_parser_clitkMIP_params params;
1488 params.override = override;
1489 params.initialize = initialize;
1490 params.check_required = check_required;
1491 params.check_ambiguity = 0;
1492 params.print_errors = 1;
1494 return cmdline_parser_clitkMIP_config_file (filename, args_info, ¶ms);
1498 cmdline_parser_clitkMIP_config_file (char * const filename,
1499 struct args_info_clitkMIP *args_info,
1500 struct cmdline_parser_clitkMIP_params *params)
1505 char *additional_error;
1507 /* store the program name */
1508 cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
1509 cmd_line_list_tmp->next = cmd_line_list;
1510 cmd_line_list = cmd_line_list_tmp;
1511 cmd_line_list->string_arg = gengetopt_strdup (CMDLINE_PARSER_CLITKMIP_PACKAGE);
1513 result = _cmdline_parser_clitkMIP_configfile(filename, &my_argc);
1515 if (result != EXIT_FAILURE) {
1516 my_argv_arg = (char **) malloc((my_argc+1) * sizeof(char *));
1517 cmd_line_list_tmp = cmd_line_list;
1519 for (i = my_argc - 1; i >= 0; --i) {
1520 my_argv_arg[i] = cmd_line_list_tmp->string_arg;
1521 cmd_line_list_tmp = cmd_line_list_tmp->next;
1524 my_argv_arg[my_argc] = 0;
1526 additional_error = (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) + 1);
1527 strcpy (additional_error, ADDITIONAL_ERROR);
1528 strcat (additional_error, filename);
1530 cmdline_parser_clitkMIP_internal (my_argc, my_argv_arg, args_info,
1534 free (additional_error);
1539 if (result == EXIT_FAILURE)
1541 cmdline_parser_clitkMIP_free (args_info);
1542 exit (EXIT_FAILURE);