]> Creatis software - clitk.git/blob - vv/tools_ggo/clitkProfileImage_ggo.c
69414aad0ae8588b0e6e18b1924abffbe91e5062
[clitk.git] / vv / tools_ggo / clitkProfileImage_ggo.c
1 /*
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_clitkProfileImage --func-name=cmdline_parser_clitkProfileImage --file-name=clitkProfileImage_ggo --unamed-opts --conf-parser --include-getopt 
5
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.
9 */
10
11 /* If we use autoconf.  */
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19
20
21 #include "clitkProfileImage_ggo.h"
22
23 const char *args_info_clitkProfileImage_purpose = "Save the profile between 2 points of the input image in the output text file";
24
25 const char *args_info_clitkProfileImage_usage = "Usage: clitkProfileImage [OPTIONS]... [FILES]...";
26
27 const char *args_info_clitkProfileImage_description = "";
28
29 const char *args_info_clitkProfileImage_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 texte filename",
37   "  -f, --point1=DOUBLE  First point (mm)",
38   "  -s, --point2=DOUBLE  Second point (mm)",
39     0
40 };
41
42 typedef enum {ARG_NO
43   , ARG_FLAG
44   , ARG_STRING
45   , ARG_DOUBLE
46 } cmdline_parser_clitkProfileImage_arg_type;
47
48 static
49 void clear_given (struct args_info_clitkProfileImage *args_info);
50 static
51 void clear_args (struct args_info_clitkProfileImage *args_info);
52
53 static int
54 cmdline_parser_clitkProfileImage_internal (int argc, char * const *argv, struct args_info_clitkProfileImage *args_info,
55                         struct cmdline_parser_clitkProfileImage_params *params, const char *additional_error);
56
57 static int
58 cmdline_parser_clitkProfileImage_required2 (struct args_info_clitkProfileImage *args_info, const char *prog_name, const char *additional_error);
59 struct line_list
60 {
61   char * string_arg;
62   struct line_list * next;
63 };
64
65 static struct line_list *cmd_line_list = 0;
66 static struct line_list *cmd_line_list_tmp = 0;
67
68 static void
69 free_cmd_list(void)
70 {
71   /* free the list of a previous call */
72   if (cmd_line_list)
73     {
74       while (cmd_line_list) {
75         cmd_line_list_tmp = cmd_line_list;
76         cmd_line_list = cmd_line_list->next;
77         free (cmd_line_list_tmp->string_arg);
78         free (cmd_line_list_tmp);
79       }
80     }
81 }
82
83
84 static char *
85 gengetopt_strdup (const char *s);
86
87 static
88 void clear_given (struct args_info_clitkProfileImage *args_info)
89 {
90   args_info->help_given = 0 ;
91   args_info->version_given = 0 ;
92   args_info->config_given = 0 ;
93   args_info->verbose_given = 0 ;
94   args_info->imagetypes_given = 0 ;
95   args_info->input_given = 0 ;
96   args_info->output_given = 0 ;
97   args_info->point1_given = 0 ;
98   args_info->point2_given = 0 ;
99 }
100
101 static
102 void clear_args (struct args_info_clitkProfileImage *args_info)
103 {
104   args_info->config_arg = NULL;
105   args_info->config_orig = NULL;
106   args_info->verbose_flag = 0;
107   args_info->imagetypes_flag = 0;
108   args_info->input_arg = NULL;
109   args_info->input_orig = NULL;
110   args_info->output_arg = NULL;
111   args_info->output_orig = NULL;
112   args_info->point1_arg = NULL;
113   args_info->point1_orig = NULL;
114   args_info->point2_arg = NULL;
115   args_info->point2_orig = NULL;
116   
117 }
118
119 static
120 void init_args_info(struct args_info_clitkProfileImage *args_info)
121 {
122
123
124   args_info->help_help = args_info_clitkProfileImage_help[0] ;
125   args_info->version_help = args_info_clitkProfileImage_help[1] ;
126   args_info->config_help = args_info_clitkProfileImage_help[2] ;
127   args_info->verbose_help = args_info_clitkProfileImage_help[3] ;
128   args_info->imagetypes_help = args_info_clitkProfileImage_help[4] ;
129   args_info->input_help = args_info_clitkProfileImage_help[5] ;
130   args_info->output_help = args_info_clitkProfileImage_help[6] ;
131   args_info->point1_help = args_info_clitkProfileImage_help[7] ;
132   args_info->point1_min = -1;
133   args_info->point1_max = 4;
134   args_info->point2_help = args_info_clitkProfileImage_help[8] ;
135   args_info->point2_min = -1;
136   args_info->point2_max = 4;
137   
138 }
139
140 void
141 cmdline_parser_clitkProfileImage_print_version (void)
142 {
143   printf ("%s %s\n", CMDLINE_PARSER_CLITKPROFILEIMAGE_PACKAGE, CMDLINE_PARSER_CLITKPROFILEIMAGE_VERSION);
144 }
145
146 static void print_help_common(void) {
147   cmdline_parser_clitkProfileImage_print_version ();
148
149   if (strlen(args_info_clitkProfileImage_purpose) > 0)
150     printf("\n%s\n", args_info_clitkProfileImage_purpose);
151
152   if (strlen(args_info_clitkProfileImage_usage) > 0)
153     printf("\n%s\n", args_info_clitkProfileImage_usage);
154
155   printf("\n");
156
157   if (strlen(args_info_clitkProfileImage_description) > 0)
158     printf("%s\n", args_info_clitkProfileImage_description);
159 }
160
161 void
162 cmdline_parser_clitkProfileImage_print_help (void)
163 {
164   int i = 0;
165   print_help_common();
166   while (args_info_clitkProfileImage_help[i])
167     printf("%s\n", args_info_clitkProfileImage_help[i++]);
168 }
169
170 void
171 cmdline_parser_clitkProfileImage_init (struct args_info_clitkProfileImage *args_info)
172 {
173   clear_given (args_info);
174   clear_args (args_info);
175   init_args_info (args_info);
176
177   args_info->inputs = NULL;
178   args_info->inputs_num = 0;
179 }
180
181 void
182 cmdline_parser_clitkProfileImage_params_init(struct cmdline_parser_clitkProfileImage_params *params)
183 {
184   if (params)
185     { 
186       params->override = 0;
187       params->initialize = 1;
188       params->check_required = 1;
189       params->check_ambiguity = 0;
190       params->print_errors = 1;
191     }
192 }
193
194 struct cmdline_parser_clitkProfileImage_params *
195 cmdline_parser_clitkProfileImage_params_create(void)
196 {
197   struct cmdline_parser_clitkProfileImage_params *params = 
198     (struct cmdline_parser_clitkProfileImage_params *)malloc(sizeof(struct cmdline_parser_clitkProfileImage_params));
199   cmdline_parser_clitkProfileImage_params_init(params);  
200   return params;
201 }
202
203 static void
204 free_string_field (char **s)
205 {
206   if (*s)
207     {
208       free (*s);
209       *s = 0;
210     }
211 }
212
213 /** @brief generic value variable */
214 union generic_value {
215     double double_arg;
216     char *string_arg;
217 };
218
219 /** @brief holds temporary values for multiple options */
220 struct generic_list
221 {
222   union generic_value arg;
223   char *orig;
224   struct generic_list *next;
225 };
226
227 /**
228  * @brief add a node at the head of the list 
229  */
230 static void add_node(struct generic_list **list) {
231   struct generic_list *new_node = (struct generic_list *) malloc (sizeof (struct generic_list));
232   new_node->next = *list;
233   *list = new_node;
234   new_node->arg.string_arg = NULL;
235   new_node->orig = NULL;
236 }
237
238 static void
239 free_multiple_field(unsigned int len, void **arg, char ***orig)
240 {
241   unsigned int i;
242   if (*arg) {
243     for (i = 0; i < len; ++i)
244       {
245         free_string_field(&((*orig)[i]));
246       }
247
248     free (*arg);
249     *arg = 0;
250     free (*orig);
251     *orig = 0;
252   }
253 }
254
255
256 static void
257 cmdline_parser_clitkProfileImage_release (struct args_info_clitkProfileImage *args_info)
258 {
259   unsigned int i;
260   free_string_field (&(args_info->config_arg));
261   free_string_field (&(args_info->config_orig));
262   free_string_field (&(args_info->input_arg));
263   free_string_field (&(args_info->input_orig));
264   free_string_field (&(args_info->output_arg));
265   free_string_field (&(args_info->output_orig));
266   free_multiple_field (args_info->point1_given, (void **)&(args_info->point1_arg), &(args_info->point1_orig));
267   free_multiple_field (args_info->point2_given, (void **)&(args_info->point2_arg), &(args_info->point2_orig));
268   
269   
270   for (i = 0; i < args_info->inputs_num; ++i)
271     free (args_info->inputs [i]);
272
273   if (args_info->inputs_num)
274     free (args_info->inputs);
275
276   clear_given (args_info);
277 }
278
279
280 static void
281 write_into_file(FILE *outfile, const char *opt, const char *arg, char *values[])
282 {
283   if (arg) {
284     fprintf(outfile, "%s=\"%s\"\n", opt, arg);
285   } else {
286     fprintf(outfile, "%s\n", opt);
287   }
288 }
289
290 static void
291 write_multiple_into_file(FILE *outfile, int len, const char *opt, char **arg, char *values[])
292 {
293   int i;
294   
295   for (i = 0; i < len; ++i)
296     write_into_file(outfile, opt, (arg ? arg[i] : 0), values);
297 }
298
299 int
300 cmdline_parser_clitkProfileImage_dump(FILE *outfile, struct args_info_clitkProfileImage *args_info)
301 {
302   int i = 0;
303
304   if (!outfile)
305     {
306       fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_CLITKPROFILEIMAGE_PACKAGE);
307       return EXIT_FAILURE;
308     }
309
310   if (args_info->help_given)
311     write_into_file(outfile, "help", 0, 0 );
312   if (args_info->version_given)
313     write_into_file(outfile, "version", 0, 0 );
314   if (args_info->config_given)
315     write_into_file(outfile, "config", args_info->config_orig, 0);
316   if (args_info->verbose_given)
317     write_into_file(outfile, "verbose", 0, 0 );
318   if (args_info->imagetypes_given)
319     write_into_file(outfile, "imagetypes", 0, 0 );
320   if (args_info->input_given)
321     write_into_file(outfile, "input", args_info->input_orig, 0);
322   if (args_info->output_given)
323     write_into_file(outfile, "output", args_info->output_orig, 0);
324   write_multiple_into_file(outfile, args_info->point1_given, "point1", args_info->point1_orig, 0);
325   write_multiple_into_file(outfile, args_info->point2_given, "point2", args_info->point2_orig, 0);
326   
327
328   i = EXIT_SUCCESS;
329   return i;
330 }
331
332 int
333 cmdline_parser_clitkProfileImage_file_save(const char *filename, struct args_info_clitkProfileImage *args_info)
334 {
335   FILE *outfile;
336   int i = 0;
337
338   outfile = fopen(filename, "w");
339
340   if (!outfile)
341     {
342       fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_CLITKPROFILEIMAGE_PACKAGE, filename);
343       return EXIT_FAILURE;
344     }
345
346   i = cmdline_parser_clitkProfileImage_dump(outfile, args_info);
347   fclose (outfile);
348
349   return i;
350 }
351
352 void
353 cmdline_parser_clitkProfileImage_free (struct args_info_clitkProfileImage *args_info)
354 {
355   cmdline_parser_clitkProfileImage_release (args_info);
356 }
357
358 /** @brief replacement of strdup, which is not standard */
359 char *
360 gengetopt_strdup (const char *s)
361 {
362   char *result = NULL;
363   if (!s)
364     return result;
365
366   result = (char*)malloc(strlen(s) + 1);
367   if (result == (char*)0)
368     return (char*)0;
369   strcpy(result, s);
370   return result;
371 }
372
373 static char *
374 get_multiple_arg_token(const char *arg)
375 {
376   char *tok, *ret;
377   size_t len, num_of_escape, i, j;
378
379   if (!arg)
380     return NULL;
381
382   tok = strchr (arg, ',');
383   num_of_escape = 0;
384
385   /* make sure it is not escaped */
386   while (tok)
387     {
388       if (*(tok-1) == '\\')
389         {
390           /* find the next one */
391           tok = strchr (tok+1, ',');
392           ++num_of_escape;
393         }
394       else
395         break;
396     }
397
398   if (tok)
399     len = (size_t)(tok - arg + 1);
400   else
401     len = strlen (arg) + 1;
402
403   len -= num_of_escape;
404
405   ret = (char *) malloc (len);
406
407   i = 0;
408   j = 0;
409   while (arg[i] && (j < len-1))
410     {
411       if (arg[i] == '\\' && 
412           arg[ i + 1 ] && 
413           arg[ i + 1 ] == ',')
414         ++i;
415
416       ret[j++] = arg[i++];
417     }
418
419   ret[len-1] = '\0';
420
421   return ret;
422 }
423
424 static char *
425 get_multiple_arg_token_next(const char *arg)
426 {
427   char *tok;
428
429   if (!arg)
430     return NULL;
431
432   tok = strchr (arg, ',');
433
434   /* make sure it is not escaped */
435   while (tok)
436     {
437       if (*(tok-1) == '\\')
438         {
439           /* find the next one */
440           tok = strchr (tok+1, ',');
441         }
442       else
443         break;
444     }
445
446   if (! tok || strlen(tok) == 1)
447     return 0;
448
449   return tok+1;
450 }
451
452 static int
453 check_multiple_option_occurrences(const char *prog_name, unsigned int option_given, int min, int max, const char *option_desc);
454
455 int
456 check_multiple_option_occurrences(const char *prog_name, unsigned int option_given, int min, int max, const char *option_desc)
457 {
458   int error = 0;
459
460   if (option_given && ! (min < 0 && max < 0))
461     {
462       if (min >= 0 && max >= 0)
463         {
464           if (min == max)
465             {
466               /* specific occurrences */
467               if (option_given != min)
468                 {
469                   fprintf (stderr, "%s: %s option occurrences must be %d\n",
470                     prog_name, option_desc, min);
471                   error = 1;
472                 }
473             }
474           else if (option_given < min
475               || option_given > max)
476             {
477               /* range occurrences */
478               fprintf (stderr, "%s: %s option occurrences must be between %d and %d\n",
479                 prog_name, option_desc, min, max);
480               error = 1;
481             }
482         }
483       else if (min >= 0)
484         {
485           /* at least check */
486           if (option_given < min)
487             {
488               fprintf (stderr, "%s: %s option occurrences must be at least %d\n",
489                 prog_name, option_desc, min);
490               error = 1;
491             }
492         }
493       else if (max >= 0)
494         {
495           /* at most check */
496           if (option_given > max)
497             {
498               fprintf (stderr, "%s: %s option occurrences must be at most %d\n",
499                 prog_name, option_desc, max);
500               error = 1;
501             }
502         }
503     }
504     
505   return error;
506 }
507 int
508 cmdline_parser_clitkProfileImage (int argc, char * const *argv, struct args_info_clitkProfileImage *args_info)
509 {
510   return cmdline_parser_clitkProfileImage2 (argc, argv, args_info, 0, 1, 1);
511 }
512
513 int
514 cmdline_parser_clitkProfileImage_ext (int argc, char * const *argv, struct args_info_clitkProfileImage *args_info,
515                    struct cmdline_parser_clitkProfileImage_params *params)
516 {
517   int result;
518   result = cmdline_parser_clitkProfileImage_internal (argc, argv, args_info, params, NULL);
519
520   if (result == EXIT_FAILURE)
521     {
522       cmdline_parser_clitkProfileImage_free (args_info);
523       exit (EXIT_FAILURE);
524     }
525   
526   return result;
527 }
528
529 int
530 cmdline_parser_clitkProfileImage2 (int argc, char * const *argv, struct args_info_clitkProfileImage *args_info, int override, int initialize, int check_required)
531 {
532   int result;
533   struct cmdline_parser_clitkProfileImage_params params;
534   
535   params.override = override;
536   params.initialize = initialize;
537   params.check_required = check_required;
538   params.check_ambiguity = 0;
539   params.print_errors = 1;
540
541   result = cmdline_parser_clitkProfileImage_internal (argc, argv, args_info, &params, NULL);
542
543   if (result == EXIT_FAILURE)
544     {
545       cmdline_parser_clitkProfileImage_free (args_info);
546       exit (EXIT_FAILURE);
547     }
548   
549   return result;
550 }
551
552 int
553 cmdline_parser_clitkProfileImage_required (struct args_info_clitkProfileImage *args_info, const char *prog_name)
554 {
555   int result = EXIT_SUCCESS;
556
557   if (cmdline_parser_clitkProfileImage_required2(args_info, prog_name, NULL) > 0)
558     result = EXIT_FAILURE;
559
560   if (result == EXIT_FAILURE)
561     {
562       cmdline_parser_clitkProfileImage_free (args_info);
563       exit (EXIT_FAILURE);
564     }
565   
566   return result;
567 }
568
569 int
570 cmdline_parser_clitkProfileImage_required2 (struct args_info_clitkProfileImage *args_info, const char *prog_name, const char *additional_error)
571 {
572   int error = 0;
573
574   /* checks for required options */
575   if (! args_info->input_given)
576     {
577       fprintf (stderr, "%s: '--input' ('-i') option required%s\n", prog_name, (additional_error ? additional_error : ""));
578       error = 1;
579     }
580   
581   if (! args_info->output_given)
582     {
583       fprintf (stderr, "%s: '--output' ('-o') option required%s\n", prog_name, (additional_error ? additional_error : ""));
584       error = 1;
585     }
586   
587   if (! args_info->point1_given)
588     {
589       fprintf (stderr, "%s: '--point1' ('-f') option required%s\n", prog_name, (additional_error ? additional_error : ""));
590       error = 1;
591     }
592   
593   if (check_multiple_option_occurrences(prog_name, args_info->point1_given, args_info->point1_min, args_info->point1_max, "'--point1' ('-f')"))
594      error = 1;
595   
596   if (! args_info->point2_given)
597     {
598       fprintf (stderr, "%s: '--point2' ('-s') option required%s\n", prog_name, (additional_error ? additional_error : ""));
599       error = 1;
600     }
601   
602   if (check_multiple_option_occurrences(prog_name, args_info->point2_given, args_info->point2_min, args_info->point2_max, "'--point2' ('-s')"))
603      error = 1;
604   
605   
606   /* checks for dependences among options */
607
608   return error;
609 }
610
611 /*
612  * Extracted from the glibc source tree, version 2.3.6
613  *
614  * Licensed under the GPL as per the whole glibc source tree.
615  *
616  * This file was modified so that getopt_long can be called
617  * many times without risking previous memory to be spoiled.
618  *
619  * Modified by Andre Noll and Lorenzo Bettini for use in
620  * GNU gengetopt generated files.
621  *
622  */
623
624 /* 
625  * we must include anything we need since this file is not thought to be
626  * inserted in a file already using getopt.h
627  *
628  * Lorenzo
629  */
630
631 struct option
632 {
633   const char *name;
634   /* has_arg can't be an enum because some compilers complain about
635      type mismatches in all the code that assumes it is an int.  */
636   int has_arg;
637   int *flag;
638   int val;
639 };
640
641 /* For communication from `getopt' to the caller.
642    When `getopt' finds an option that takes an argument,
643    the argument value is returned here.
644    Also, when `ordering' is RETURN_IN_ORDER,
645    each non-option ARGV-element is returned here.  */
646
647 static char *optarg;
648
649 /* Index in ARGV of the next element to be scanned.
650    This is used for communication to and from the caller
651    and for communication between successive calls to `getopt'.
652
653    On entry to `getopt', zero means this is the first call; initialize.
654
655    When `getopt' returns -1, this is the index of the first of the
656    non-option elements that the caller should itself scan.
657
658    Otherwise, `optind' communicates from one call to the next
659    how much of ARGV has been scanned so far.  */
660
661 static int optind;
662
663 /* Callers store zero here to inhibit the error message `getopt' prints
664    for unrecognized options.  */
665
666 static int opterr;
667
668 /* Set to an option character which was unrecognized.  */
669
670 static int optopt;
671
672 /* This version of `getopt' appears to the caller like standard Unix `getopt'
673    but it behaves differently for the user, since it allows the user
674    to intersperse the options with the other arguments.
675
676    As `getopt' works, it permutes the elements of ARGV so that,
677    when it is done, all the options precede everything else.  Thus
678    all application programs are extended to handle flexible argument order.
679 */
680 /*
681    If the field `flag' is not NULL, it points to a variable that is set
682    to the value given in the field `val' when the option is found, but
683    left unchanged if the option is not found.
684
685    To have a long-named option do something other than set an `int' to
686    a compiled-in constant, such as set a value from `custom_optarg', set the
687    option's `flag' field to zero and its `val' field to a nonzero
688    value (the equivalent single-letter option character, if there is
689    one).  For long options that have a zero `flag' field, `getopt'
690    returns the contents of the `val' field.  */
691
692 /* Names for the values of the `has_arg' field of `struct option'.  */
693 #ifndef no_argument
694 #define no_argument             0
695 #endif
696
697 #ifndef required_argument
698 #define required_argument       1
699 #endif
700
701 #ifndef optional_argument
702 #define optional_argument       2
703 #endif
704
705 struct custom_getopt_data {
706         /*
707          * These have exactly the same meaning as the corresponding global variables,
708          * except that they are used for the reentrant versions of getopt.
709          */
710         int custom_optind;
711         int custom_opterr;
712         int custom_optopt;
713         char *custom_optarg;
714
715         /* True if the internal members have been initialized.  */
716         int initialized;
717
718         /*
719          * The next char to be scanned in the option-element in which the last option
720          * character we returned was found.  This allows us to pick up the scan where
721          * we left off.  If this is zero, or a null string, it means resume the scan by
722          * advancing to the next ARGV-element.
723          */
724         char *nextchar;
725
726         /*
727          * Describe the part of ARGV that contains non-options that have been skipped.
728          * `first_nonopt' is the index in ARGV of the first of them; `last_nonopt' is
729          * the index after the last of them.
730          */
731         int first_nonopt;
732         int last_nonopt;
733 };
734
735 /*
736  * the variables optarg, optind, opterr and optopt are renamed with
737  * the custom_ prefix so that they don't interfere with getopt ones.
738  *
739  * Moreover they're static so they are visible only from within the
740  * file where this very file will be included.
741  */
742
743 /*
744  * For communication from `custom_getopt' to the caller.  When `custom_getopt' finds an
745  * option that takes an argument, the argument value is returned here.
746  */
747 static char *custom_optarg;
748
749 /*
750  * Index in ARGV of the next element to be scanned.  This is used for
751  * communication to and from the caller and for communication between
752  * successive calls to `custom_getopt'.
753  *
754  * On entry to `custom_getopt', 1 means this is the first call; initialize.
755  *
756  * When `custom_getopt' returns -1, this is the index of the first of the non-option
757  * elements that the caller should itself scan.
758  *
759  * Otherwise, `custom_optind' communicates from one call to the next how much of ARGV
760  * has been scanned so far.
761  *
762  * 1003.2 says this must be 1 before any call.
763  */
764 static int custom_optind = 1;
765
766 /*
767  * Callers store zero here to inhibit the error message for unrecognized
768  * options.
769  */
770 static int custom_opterr = 1;
771
772 /*
773  * Set to an option character which was unrecognized.  This must be initialized
774  * on some systems to avoid linking in the system's own getopt implementation.
775  */
776 static int custom_optopt = '?';
777
778 /*
779  * Exchange two adjacent subsequences of ARGV.  One subsequence is elements
780  * [first_nonopt,last_nonopt) which contains all the non-options that have been
781  * skipped so far.  The other is elements [last_nonopt,custom_optind), which contains
782  * all the options processed since those non-options were skipped.
783  * `first_nonopt' and `last_nonopt' are relocated so that they describe the new
784  * indices of the non-options in ARGV after they are moved.
785  */
786 static void exchange(char **argv, struct custom_getopt_data *d)
787 {
788         int bottom = d->first_nonopt;
789         int middle = d->last_nonopt;
790         int top = d->custom_optind;
791         char *tem;
792
793         /*
794          * Exchange the shorter segment with the far end of the longer segment.
795          * That puts the shorter segment into the right place.  It leaves the
796          * longer segment in the right place overall, but it consists of two
797          * parts that need to be swapped next.
798          */
799         while (top > middle && middle > bottom) {
800                 if (top - middle > middle - bottom) {
801                         /* Bottom segment is the short one.  */
802                         int len = middle - bottom;
803                         int i;
804
805                         /* Swap it with the top part of the top segment.  */
806                         for (i = 0; i < len; i++) {
807                                 tem = argv[bottom + i];
808                                 argv[bottom + i] =
809                                         argv[top - (middle - bottom) + i];
810                                 argv[top - (middle - bottom) + i] = tem;
811                         }
812                         /* Exclude the moved bottom segment from further swapping.  */
813                         top -= len;
814                 } else {
815                         /* Top segment is the short one.  */
816                         int len = top - middle;
817                         int i;
818
819                         /* Swap it with the bottom part of the bottom segment.  */
820                         for (i = 0; i < len; i++) {
821                                 tem = argv[bottom + i];
822                                 argv[bottom + i] = argv[middle + i];
823                                 argv[middle + i] = tem;
824                         }
825                         /* Exclude the moved top segment from further swapping.  */
826                         bottom += len;
827                 }
828         }
829         /* Update records for the slots the non-options now occupy.  */
830         d->first_nonopt += (d->custom_optind - d->last_nonopt);
831         d->last_nonopt = d->custom_optind;
832 }
833
834 /* Initialize the internal data when the first call is made.  */
835 static void custom_getopt_initialize(struct custom_getopt_data *d)
836 {
837         /*
838          * Start processing options with ARGV-element 1 (since ARGV-element 0
839          * is the program name); the sequence of previously skipped non-option
840          * ARGV-elements is empty.
841          */
842         d->first_nonopt = d->last_nonopt = d->custom_optind;
843         d->nextchar = NULL;
844         d->initialized = 1;
845 }
846
847 #define NONOPTION_P (argv[d->custom_optind][0] != '-' || argv[d->custom_optind][1] == '\0')
848
849 /* return: zero: continue, nonzero: return given value to user */
850 static int shuffle_argv(int argc, char *const *argv,const struct option *longopts,
851         struct custom_getopt_data *d)
852 {
853         /*
854          * Give FIRST_NONOPT & LAST_NONOPT rational values if CUSTOM_OPTIND has been
855          * moved back by the user (who may also have changed the arguments).
856          */
857         if (d->last_nonopt > d->custom_optind)
858                 d->last_nonopt = d->custom_optind;
859         if (d->first_nonopt > d->custom_optind)
860                 d->first_nonopt = d->custom_optind;
861         /*
862          * If we have just processed some options following some
863          * non-options, exchange them so that the options come first.
864          */
865         if (d->first_nonopt != d->last_nonopt &&
866                         d->last_nonopt != d->custom_optind)
867                 exchange((char **) argv, d);
868         else if (d->last_nonopt != d->custom_optind)
869                 d->first_nonopt = d->custom_optind;
870         /*
871          * Skip any additional non-options and extend the range of
872          * non-options previously skipped.
873          */
874         while (d->custom_optind < argc && NONOPTION_P)
875                 d->custom_optind++;
876         d->last_nonopt = d->custom_optind;
877         /*
878          * The special ARGV-element `--' means premature end of options.  Skip
879          * it like a null option, then exchange with previous non-options as if
880          * it were an option, then skip everything else like a non-option.
881          */
882         if (d->custom_optind != argc && !strcmp(argv[d->custom_optind], "--")) {
883                 d->custom_optind++;
884                 if (d->first_nonopt != d->last_nonopt
885                                 && d->last_nonopt != d->custom_optind)
886                         exchange((char **) argv, d);
887                 else if (d->first_nonopt == d->last_nonopt)
888                         d->first_nonopt = d->custom_optind;
889                 d->last_nonopt = argc;
890                 d->custom_optind = argc;
891         }
892         /*
893          * If we have done all the ARGV-elements, stop the scan and back over
894          * any non-options that we skipped and permuted.
895          */
896         if (d->custom_optind == argc) {
897                 /*
898                  * Set the next-arg-index to point at the non-options that we
899                  * previously skipped, so the caller will digest them.
900                  */
901                 if (d->first_nonopt != d->last_nonopt)
902                         d->custom_optind = d->first_nonopt;
903                 return -1;
904         }
905         /*
906          * If we have come to a non-option and did not permute it, either stop
907          * the scan or describe it to the caller and pass it by.
908          */
909         if (NONOPTION_P) {
910                 d->custom_optarg = argv[d->custom_optind++];
911                 return 1;
912         }
913         /*
914          * We have found another option-ARGV-element. Skip the initial
915          * punctuation.
916          */
917         d->nextchar = (argv[d->custom_optind] + 1 + (longopts != NULL && argv[d->custom_optind][1] == '-'));
918         return 0;
919 }
920
921 /*
922  * Check whether the ARGV-element is a long option.
923  *
924  * If there's a long option "fubar" and the ARGV-element is "-fu", consider
925  * that an abbreviation of the long option, just like "--fu", and not "-f" with
926  * arg "u".
927  *
928  * This distinction seems to be the most useful approach.
929  *
930  */
931 static int check_long_opt(int argc, char *const *argv, const char *optstring,
932                 const struct option *longopts, int *longind,
933                 int print_errors, struct custom_getopt_data *d)
934 {
935         char *nameend;
936         const struct option *p;
937         const struct option *pfound = NULL;
938         int exact = 0;
939         int ambig = 0;
940         int indfound = -1;
941         int option_index;
942
943         for (nameend = d->nextchar; *nameend && *nameend != '='; nameend++)
944                 /* Do nothing.  */ ;
945
946         /* Test all long options for either exact match or abbreviated matches */
947         for (p = longopts, option_index = 0; p->name; p++, option_index++)
948                 if (!strncmp(p->name, d->nextchar, nameend - d->nextchar)) {
949                         if ((unsigned int) (nameend - d->nextchar)
950                                         == (unsigned int) strlen(p->name)) {
951                                 /* Exact match found.  */
952                                 pfound = p;
953                                 indfound = option_index;
954                                 exact = 1;
955                                 break;
956                         } else if (pfound == NULL) {
957                                 /* First nonexact match found.  */
958                                 pfound = p;
959                                 indfound = option_index;
960                         } else if (pfound->has_arg != p->has_arg
961                                         || pfound->flag != p->flag
962                                         || pfound->val != p->val)
963                                 /* Second or later nonexact match found.  */
964                                 ambig = 1;
965                 }
966         if (ambig && !exact) {
967                 if (print_errors) {
968                         fprintf(stderr,
969                                 "%s: option `%s' is ambiguous\n",
970                                 argv[0], argv[d->custom_optind]);
971                 }
972                 d->nextchar += strlen(d->nextchar);
973                 d->custom_optind++;
974                 d->custom_optopt = 0;
975                 return '?';
976         }
977         if (pfound) {
978                 option_index = indfound;
979                 d->custom_optind++;
980                 if (*nameend) {
981                         if (pfound->has_arg != no_argument)
982                                 d->custom_optarg = nameend + 1;
983                         else {
984                                 if (print_errors) {
985                                         if (argv[d->custom_optind - 1][1] == '-') {
986                                                 /* --option */
987                                                 fprintf(stderr, "%s: option `--%s' doesn't allow an argument\n",
988                                                         argv[0], pfound->name);
989                                         } else {
990                                                 /* +option or -option */
991                                                 fprintf(stderr, "%s: option `%c%s' doesn't allow an argument\n",
992                                                         argv[0], argv[d->custom_optind - 1][0], pfound->name);
993                                         }
994
995                                 }
996                                 d->nextchar += strlen(d->nextchar);
997                                 d->custom_optopt = pfound->val;
998                                 return '?';
999                         }
1000                 } else if (pfound->has_arg == required_argument) {
1001                         if (d->custom_optind < argc)
1002                                 d->custom_optarg = argv[d->custom_optind++];
1003                         else {
1004                                 if (print_errors) {
1005                                         fprintf(stderr,
1006                                                 "%s: option `%s' requires an argument\n",
1007                                                 argv[0],
1008                                                 argv[d->custom_optind - 1]);
1009                                 }
1010                                 d->nextchar += strlen(d->nextchar);
1011                                 d->custom_optopt = pfound->val;
1012                                 return optstring[0] == ':' ? ':' : '?';
1013                         }
1014                 }
1015                 d->nextchar += strlen(d->nextchar);
1016                 if (longind != NULL)
1017                         *longind = option_index;
1018                 if (pfound->flag) {
1019                         *(pfound->flag) = pfound->val;
1020                         return 0;
1021                 }
1022                 return pfound->val;
1023         }
1024         /*
1025          * Can't find it as a long option.  If this is not getopt_long_only, or
1026          * the option starts with '--' or is not a valid short option, then
1027          * it's an error.  Otherwise interpret it as a short option.
1028          */
1029         if (print_errors) {
1030                 if (argv[d->custom_optind][1] == '-') {
1031                         /* --option */
1032                         fprintf(stderr,
1033                                 "%s: unrecognized option `--%s'\n",
1034                                 argv[0], d->nextchar);
1035                 } else {
1036                         /* +option or -option */
1037                         fprintf(stderr,
1038                                 "%s: unrecognized option `%c%s'\n",
1039                                 argv[0], argv[d->custom_optind][0],
1040                                 d->nextchar);
1041                 }
1042         }
1043         d->nextchar = (char *) "";
1044         d->custom_optind++;
1045         d->custom_optopt = 0;
1046         return '?';
1047 }
1048
1049 static int check_short_opt(int argc, char *const *argv, const char *optstring,
1050                 int print_errors, struct custom_getopt_data *d)
1051 {
1052         char c = *d->nextchar++;
1053         char *temp = strchr(optstring, c);
1054
1055         /* Increment `custom_optind' when we start to process its last character.  */
1056         if (*d->nextchar == '\0')
1057                 ++d->custom_optind;
1058         if (!temp || c == ':') {
1059                 if (print_errors)
1060                         fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
1061
1062                 d->custom_optopt = c;
1063                 return '?';
1064         }
1065         if (temp[1] == ':') {
1066                 if (temp[2] == ':') {
1067                         /* This is an option that accepts an argument optionally.  */
1068                         if (*d->nextchar != '\0') {
1069                                 d->custom_optarg = d->nextchar;
1070                                 d->custom_optind++;
1071                         } else
1072                                 d->custom_optarg = NULL;
1073                         d->nextchar = NULL;
1074                 } else {
1075                         /* This is an option that requires an argument.  */
1076                         if (*d->nextchar != '\0') {
1077                                 d->custom_optarg = d->nextchar;
1078                                 /*
1079                                  * If we end this ARGV-element by taking the
1080                                  * rest as an arg, we must advance to the next
1081                                  * element now.
1082                                  */
1083                                 d->custom_optind++;
1084                         } else if (d->custom_optind == argc) {
1085                                 if (print_errors) {
1086                                         fprintf(stderr,
1087                                                 "%s: option requires an argument -- %c\n",
1088                                                 argv[0], c);
1089                                 }
1090                                 d->custom_optopt = c;
1091                                 if (optstring[0] == ':')
1092                                         c = ':';
1093                                 else
1094                                         c = '?';
1095                         } else
1096                                 /*
1097                                  * We already incremented `custom_optind' once;
1098                                  * increment it again when taking next ARGV-elt
1099                                  * as argument.
1100                                  */
1101                                 d->custom_optarg = argv[d->custom_optind++];
1102                         d->nextchar = NULL;
1103                 }
1104         }
1105         return c;
1106 }
1107
1108 /*
1109  * Scan elements of ARGV for option characters given in OPTSTRING.
1110  *
1111  * If an element of ARGV starts with '-', and is not exactly "-" or "--",
1112  * then it is an option element.  The characters of this element
1113  * (aside from the initial '-') are option characters.  If `getopt'
1114  * is called repeatedly, it returns successively each of the option characters
1115  * from each of the option elements.
1116  *
1117  * If `getopt' finds another option character, it returns that character,
1118  * updating `custom_optind' and `nextchar' so that the next call to `getopt' can
1119  * resume the scan with the following option character or ARGV-element.
1120  *
1121  * If there are no more option characters, `getopt' returns -1.
1122  * Then `custom_optind' is the index in ARGV of the first ARGV-element
1123  * that is not an option.  (The ARGV-elements have been permuted
1124  * so that those that are not options now come last.)
1125  *
1126  * OPTSTRING is a string containing the legitimate option characters.
1127  * If an option character is seen that is not listed in OPTSTRING,
1128  * return '?' after printing an error message.  If you set `custom_opterr' to
1129  * zero, the error message is suppressed but we still return '?'.
1130  *
1131  * If a char in OPTSTRING is followed by a colon, that means it wants an arg,
1132  * so the following text in the same ARGV-element, or the text of the following
1133  * ARGV-element, is returned in `custom_optarg'.  Two colons mean an option that
1134  * wants an optional arg; if there is text in the current ARGV-element,
1135  * it is returned in `custom_optarg', otherwise `custom_optarg' is set to zero.
1136  *
1137  * If OPTSTRING starts with `-' or `+', it requests different methods of
1138  * handling the non-option ARGV-elements.
1139  * See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
1140  *
1141  * Long-named options begin with `--' instead of `-'.
1142  * Their names may be abbreviated as long as the abbreviation is unique
1143  * or is an exact match for some defined option.  If they have an
1144  * argument, it follows the option name in the same ARGV-element, separated
1145  * from the option name by a `=', or else the in next ARGV-element.
1146  * When `getopt' finds a long-named option, it returns 0 if that option's
1147  * `flag' field is nonzero, the value of the option's `val' field
1148  * if the `flag' field is zero.
1149  *
1150  * The elements of ARGV aren't really const, because we permute them.
1151  * But we pretend they're const in the prototype to be compatible
1152  * with other systems.
1153  *
1154  * LONGOPTS is a vector of `struct option' terminated by an
1155  * element containing a name which is zero.
1156  *
1157  * LONGIND returns the index in LONGOPT of the long-named option found.
1158  * It is only valid when a long-named option has been found by the most
1159  * recent call.
1160  *
1161  * Return the option character from OPTS just read.  Return -1 when there are
1162  * no more options.  For unrecognized options, or options missing arguments,
1163  * `custom_optopt' is set to the option letter, and '?' is returned.
1164  *
1165  * The OPTS string is a list of characters which are recognized option letters,
1166  * optionally followed by colons, specifying that that letter takes an
1167  * argument, to be placed in `custom_optarg'.
1168  *
1169  * If a letter in OPTS is followed by two colons, its argument is optional.
1170  * This behavior is specific to the GNU `getopt'.
1171  *
1172  * The argument `--' causes premature termination of argument scanning,
1173  * explicitly telling `getopt' that there are no more options.  If OPTS begins
1174  * with `--', then non-option arguments are treated as arguments to the option
1175  * '\0'.  This behavior is specific to the GNU `getopt'.
1176  */
1177
1178 static int getopt_internal_r(int argc, char *const *argv, const char *optstring,
1179                 const struct option *longopts, int *longind,
1180                 struct custom_getopt_data *d)
1181 {
1182         int ret, print_errors = d->custom_opterr;
1183
1184         if (optstring[0] == ':')
1185                 print_errors = 0;
1186         if (argc < 1)
1187                 return -1;
1188         d->custom_optarg = NULL;
1189
1190         /* 
1191          * This is a big difference with GNU getopt, since optind == 0
1192          * means initialization while here 1 means first call.
1193          */
1194         if (d->custom_optind == 0 || !d->initialized) {
1195                 if (d->custom_optind == 0)
1196                         d->custom_optind = 1;   /* Don't scan ARGV[0], the program name.  */
1197                 custom_getopt_initialize(d);
1198         }
1199         if (d->nextchar == NULL || *d->nextchar == '\0') {
1200                 ret = shuffle_argv(argc, argv, longopts, d);
1201                 if (ret)
1202                         return ret;
1203         }
1204         if (longopts && (argv[d->custom_optind][1] == '-' ))
1205                 return check_long_opt(argc, argv, optstring, longopts,
1206                         longind, print_errors, d);
1207         return check_short_opt(argc, argv, optstring, print_errors, d);
1208 }
1209
1210 static int custom_getopt_internal(int argc, char *const *argv, const char *optstring,
1211         const struct option *longopts, int *longind)
1212 {
1213         int result;
1214         /* Keep a global copy of all internal members of d */
1215         static struct custom_getopt_data d;
1216
1217         d.custom_optind = custom_optind;
1218         d.custom_opterr = custom_opterr;
1219         result = getopt_internal_r(argc, argv, optstring, longopts,
1220                 longind, &d);
1221         custom_optind = d.custom_optind;
1222         custom_optarg = d.custom_optarg;
1223         custom_optopt = d.custom_optopt;
1224         return result;
1225 }
1226
1227 static int custom_getopt_long (int argc, char *const *argv, const char *options,
1228         const struct option *long_options, int *opt_index)
1229 {
1230         return custom_getopt_internal(argc, argv, options, long_options,
1231                 opt_index);
1232 }
1233
1234
1235 static char *package_name = 0;
1236
1237 /**
1238  * @brief updates an option
1239  * @param field the generic pointer to the field to update
1240  * @param orig_field the pointer to the orig field
1241  * @param field_given the pointer to the number of occurrence of this option
1242  * @param prev_given the pointer to the number of occurrence already seen
1243  * @param value the argument for this option (if null no arg was specified)
1244  * @param possible_values the possible values for this option (if specified)
1245  * @param default_value the default value (in case the option only accepts fixed values)
1246  * @param arg_type the type of this option
1247  * @param check_ambiguity @see cmdline_parser_clitkProfileImage_params.check_ambiguity
1248  * @param override @see cmdline_parser_clitkProfileImage_params.override
1249  * @param no_free whether to free a possible previous value
1250  * @param multiple_option whether this is a multiple option
1251  * @param long_opt the corresponding long option
1252  * @param short_opt the corresponding short option (or '-' if none)
1253  * @param additional_error possible further error specification
1254  */
1255 static
1256 int update_arg(void *field, char **orig_field,
1257                unsigned int *field_given, unsigned int *prev_given, 
1258                char *value, char *possible_values[], const char *default_value,
1259                cmdline_parser_clitkProfileImage_arg_type arg_type,
1260                int check_ambiguity, int override,
1261                int no_free, int multiple_option,
1262                const char *long_opt, char short_opt,
1263                const char *additional_error)
1264 {
1265   char *stop_char = 0;
1266   const char *val = value;
1267   int found;
1268   char **string_field;
1269
1270   stop_char = 0;
1271   found = 0;
1272
1273   if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
1274     {
1275       if (short_opt != '-')
1276         fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n", 
1277                package_name, long_opt, short_opt,
1278                (additional_error ? additional_error : ""));
1279       else
1280         fprintf (stderr, "%s: `--%s' option given more than once%s\n", 
1281                package_name, long_opt,
1282                (additional_error ? additional_error : ""));
1283       return 1; /* failure */
1284     }
1285
1286     
1287   if (field_given && *field_given && ! override)
1288     return 0;
1289   if (prev_given)
1290     (*prev_given)++;
1291   if (field_given)
1292     (*field_given)++;
1293   if (possible_values)
1294     val = possible_values[found];
1295
1296   switch(arg_type) {
1297   case ARG_FLAG:
1298     *((int *)field) = !*((int *)field);
1299     break;
1300   case ARG_DOUBLE:
1301     if (val) *((double *)field) = strtod (val, &stop_char);
1302     break;
1303   case ARG_STRING:
1304     if (val) {
1305       string_field = (char **)field;
1306       if (!no_free && *string_field)
1307         free (*string_field); /* free previous string */
1308       *string_field = gengetopt_strdup (val);
1309     }
1310     break;
1311   default:
1312     break;
1313   };
1314
1315   /* check numeric conversion */
1316   switch(arg_type) {
1317   case ARG_DOUBLE:
1318     if (val && !(stop_char && *stop_char == '\0')) {
1319       fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
1320       return 1; /* failure */
1321     }
1322     break;
1323   default:
1324     ;
1325   };
1326
1327   /* store the original value */
1328   switch(arg_type) {
1329   case ARG_NO:
1330   case ARG_FLAG:
1331     break;
1332   default:
1333     if (value && orig_field) {
1334       if (no_free) {
1335         *orig_field = value;
1336       } else {
1337         if (*orig_field)
1338           free (*orig_field); /* free previous string */
1339         *orig_field = gengetopt_strdup (value);
1340       }
1341     }
1342   };
1343
1344   return 0; /* OK */
1345 }
1346
1347 /**
1348  * @brief store information about a multiple option in a temporary list
1349  * @param list where to (temporarily) store multiple options
1350  */
1351 static
1352 int update_multiple_arg_temp(struct generic_list **list,
1353                unsigned int *prev_given, const char *val,
1354                char *possible_values[], const char *default_value,
1355                cmdline_parser_clitkProfileImage_arg_type arg_type,
1356                const char *long_opt, char short_opt,
1357                const char *additional_error)
1358 {
1359   char *multi_token, *multi_next; /* store single arguments */
1360
1361   if (arg_type == ARG_NO) {
1362     (*prev_given)++;
1363     return 0; /* OK */
1364   }
1365
1366   multi_token = get_multiple_arg_token(val);
1367   multi_next = get_multiple_arg_token_next (val);
1368
1369   while (1)
1370     {
1371       add_node (list);
1372       if (update_arg((void *)&((*list)->arg), &((*list)->orig), 0,
1373           prev_given, multi_token, possible_values, default_value, 
1374           arg_type, 0, 1, 1, 1, long_opt, short_opt, additional_error)) {
1375         if (multi_token) free(multi_token);
1376         return 1; /* failure */
1377       }
1378
1379       if (multi_next)
1380         {
1381           multi_token = get_multiple_arg_token(multi_next);
1382           multi_next = get_multiple_arg_token_next (multi_next);
1383         }
1384       else
1385         break;
1386     }
1387
1388   return 0; /* OK */
1389 }
1390
1391 /**
1392  * @brief free the passed list (including possible string argument)
1393  */
1394 static
1395 void free_list(struct generic_list *list, short string_arg)
1396 {
1397   if (list) {
1398     struct generic_list *tmp;
1399     while (list)
1400       {
1401         tmp = list;
1402         if (string_arg && list->arg.string_arg)
1403           free (list->arg.string_arg);
1404         if (list->orig)
1405           free (list->orig);
1406         list = list->next;
1407         free (tmp);
1408       }
1409   }
1410 }
1411
1412 /**
1413  * @brief updates a multiple option starting from the passed list
1414  */
1415 static
1416 void update_multiple_arg(void *field, char ***orig_field,
1417                unsigned int field_given, unsigned int prev_given, union generic_value *default_value,
1418                cmdline_parser_clitkProfileImage_arg_type arg_type,
1419                struct generic_list *list)
1420 {
1421   int i;
1422   struct generic_list *tmp;
1423
1424   if (prev_given && list) {
1425     *orig_field = (char **) realloc (*orig_field, (field_given + prev_given) * sizeof (char *));
1426
1427     switch(arg_type) {
1428     case ARG_DOUBLE:
1429       *((double **)field) = (double *)realloc (*((double **)field), (field_given + prev_given) * sizeof (double)); break;
1430     case ARG_STRING:
1431       *((char ***)field) = (char **)realloc (*((char ***)field), (field_given + prev_given) * sizeof (char *)); break;
1432     default:
1433       break;
1434     };
1435     
1436     for (i = (prev_given - 1); i >= 0; --i)
1437       {
1438         tmp = list;
1439         
1440         switch(arg_type) {
1441         case ARG_DOUBLE:
1442           (*((double **)field))[i + field_given] = tmp->arg.double_arg; break;
1443         case ARG_STRING:
1444           (*((char ***)field))[i + field_given] = tmp->arg.string_arg; break;
1445         default:
1446           break;
1447         }        
1448         (*orig_field) [i + field_given] = list->orig;
1449         list = list->next;
1450         free (tmp);
1451       }
1452   } else { /* set the default value */
1453     if (default_value && ! field_given) {
1454       switch(arg_type) {
1455       case ARG_DOUBLE:
1456         if (! *((double **)field)) {
1457           *((double **)field) = (double *)malloc (sizeof (double));
1458           (*((double **)field))[0] = default_value->double_arg;
1459         }
1460         break;
1461       case ARG_STRING:
1462         if (! *((char ***)field)) {
1463           *((char ***)field) = (char **)malloc (sizeof (char *));
1464           (*((char ***)field))[0] = gengetopt_strdup(default_value->string_arg);
1465         }
1466         break;
1467       default: break;
1468       }
1469       if (!(*orig_field)) {
1470         *orig_field = (char **) malloc (sizeof (char *));
1471         (*orig_field)[0] = NULL;
1472       }
1473     }
1474   }
1475 }
1476
1477 int
1478 cmdline_parser_clitkProfileImage_internal (int argc, char * const *argv, struct args_info_clitkProfileImage *args_info,
1479                         struct cmdline_parser_clitkProfileImage_params *params, const char *additional_error)
1480 {
1481   int c;        /* Character of the parsed option.  */
1482
1483   struct generic_list * point1_list = NULL;
1484   struct generic_list * point2_list = NULL;
1485   int error = 0;
1486   struct args_info_clitkProfileImage local_args_info;
1487   
1488   int override;
1489   int initialize;
1490   int check_required;
1491   int check_ambiguity;
1492   
1493   package_name = argv[0];
1494   
1495   override = params->override;
1496   initialize = params->initialize;
1497   check_required = params->check_required;
1498   check_ambiguity = params->check_ambiguity;
1499
1500   if (initialize)
1501     cmdline_parser_clitkProfileImage_init (args_info);
1502
1503   cmdline_parser_clitkProfileImage_init (&local_args_info);
1504
1505   optarg = 0;
1506   optind = 0;
1507   opterr = params->print_errors;
1508   optopt = '?';
1509
1510   while (1)
1511     {
1512       int option_index = 0;
1513
1514       static struct option long_options[] = {
1515         { "help",       0, NULL, 'h' },
1516         { "version",    0, NULL, 'V' },
1517         { "config",     1, NULL, 0 },
1518         { "verbose",    0, NULL, 'v' },
1519         { "imagetypes", 0, NULL, 0 },
1520         { "input",      1, NULL, 'i' },
1521         { "output",     1, NULL, 'o' },
1522         { "point1",     1, NULL, 'f' },
1523         { "point2",     1, NULL, 's' },
1524         { NULL, 0, NULL, 0 }
1525       };
1526
1527       custom_optarg = optarg;
1528       custom_optind = optind;
1529       custom_opterr = opterr;
1530       custom_optopt = optopt;
1531
1532       c = custom_getopt_long (argc, argv, "hVvi:o:f:s:", long_options, &option_index);
1533
1534       optarg = custom_optarg;
1535       optind = custom_optind;
1536       opterr = custom_opterr;
1537       optopt = custom_optopt;
1538
1539       if (c == -1) break;       /* Exit from `while (1)' loop.  */
1540
1541       switch (c)
1542         {
1543         case 'h':       /* Print help and exit.  */
1544           cmdline_parser_clitkProfileImage_print_help ();
1545           cmdline_parser_clitkProfileImage_free (&local_args_info);
1546           exit (EXIT_SUCCESS);
1547
1548         case 'V':       /* Print version and exit.  */
1549           cmdline_parser_clitkProfileImage_print_version ();
1550           cmdline_parser_clitkProfileImage_free (&local_args_info);
1551           exit (EXIT_SUCCESS);
1552
1553         case 'v':       /* Verbose.  */
1554         
1555         
1556           if (update_arg((void *)&(args_info->verbose_flag), 0, &(args_info->verbose_given),
1557               &(local_args_info.verbose_given), optarg, 0, 0, ARG_FLAG,
1558               check_ambiguity, override, 1, 0, "verbose", 'v',
1559               additional_error))
1560             goto failure;
1561         
1562           break;
1563         case 'i':       /* Input image filename.  */
1564         
1565         
1566           if (update_arg( (void *)&(args_info->input_arg), 
1567                &(args_info->input_orig), &(args_info->input_given),
1568               &(local_args_info.input_given), optarg, 0, 0, ARG_STRING,
1569               check_ambiguity, override, 0, 0,
1570               "input", 'i',
1571               additional_error))
1572             goto failure;
1573         
1574           break;
1575         case 'o':       /* Output texte filename.  */
1576         
1577         
1578           if (update_arg( (void *)&(args_info->output_arg), 
1579                &(args_info->output_orig), &(args_info->output_given),
1580               &(local_args_info.output_given), optarg, 0, 0, ARG_STRING,
1581               check_ambiguity, override, 0, 0,
1582               "output", 'o',
1583               additional_error))
1584             goto failure;
1585         
1586           break;
1587         case 'f':       /* First point (mm).  */
1588         
1589           if (update_multiple_arg_temp(&point1_list, 
1590               &(local_args_info.point1_given), optarg, 0, 0, ARG_DOUBLE,
1591               "point1", 'f',
1592               additional_error))
1593             goto failure;
1594         
1595           break;
1596         case 's':       /* Second point (mm).  */
1597         
1598           if (update_multiple_arg_temp(&point2_list, 
1599               &(local_args_info.point2_given), optarg, 0, 0, ARG_DOUBLE,
1600               "point2", 's',
1601               additional_error))
1602             goto failure;
1603         
1604           break;
1605
1606         case 0: /* Long option with no short option */
1607           /* Config file.  */
1608           if (strcmp (long_options[option_index].name, "config") == 0)
1609           {
1610           
1611           
1612             if (update_arg( (void *)&(args_info->config_arg), 
1613                  &(args_info->config_orig), &(args_info->config_given),
1614                 &(local_args_info.config_given), optarg, 0, 0, ARG_STRING,
1615                 check_ambiguity, override, 0, 0,
1616                 "config", '-',
1617                 additional_error))
1618               goto failure;
1619           
1620           }
1621           /* Display allowed image types.  */
1622           else if (strcmp (long_options[option_index].name, "imagetypes") == 0)
1623           {
1624           
1625           
1626             if (update_arg((void *)&(args_info->imagetypes_flag), 0, &(args_info->imagetypes_given),
1627                 &(local_args_info.imagetypes_given), optarg, 0, 0, ARG_FLAG,
1628                 check_ambiguity, override, 1, 0, "imagetypes", '-',
1629                 additional_error))
1630               goto failure;
1631           
1632           }
1633           
1634           break;
1635         case '?':       /* Invalid option.  */
1636           /* `getopt_long' already printed an error message.  */
1637           goto failure;
1638
1639         default:        /* bug: option not considered.  */
1640           fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_CLITKPROFILEIMAGE_PACKAGE, c, (additional_error ? additional_error : ""));
1641           abort ();
1642         } /* switch */
1643     } /* while */
1644
1645
1646   update_multiple_arg((void *)&(args_info->point1_arg),
1647     &(args_info->point1_orig), args_info->point1_given,
1648     local_args_info.point1_given, 0 , 
1649     ARG_DOUBLE, point1_list);
1650   update_multiple_arg((void *)&(args_info->point2_arg),
1651     &(args_info->point2_orig), args_info->point2_given,
1652     local_args_info.point2_given, 0 , 
1653     ARG_DOUBLE, point2_list);
1654
1655   args_info->point1_given += local_args_info.point1_given;
1656   local_args_info.point1_given = 0;
1657   args_info->point2_given += local_args_info.point2_given;
1658   local_args_info.point2_given = 0;
1659   
1660   if (check_required)
1661     {
1662       error += cmdline_parser_clitkProfileImage_required2 (args_info, argv[0], additional_error);
1663     }
1664
1665   cmdline_parser_clitkProfileImage_release (&local_args_info);
1666
1667   if ( error )
1668     return (EXIT_FAILURE);
1669
1670   if (optind < argc)
1671     {
1672       int i = 0 ;
1673       int found_prog_name = 0;
1674       /* whether program name, i.e., argv[0], is in the remaining args
1675          (this may happen with some implementations of getopt,
1676           but surely not with the one included by gengetopt) */
1677
1678
1679       args_info->inputs_num = argc - optind - found_prog_name;
1680       args_info->inputs =
1681         (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
1682       while (optind < argc)
1683         args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind++]) ;
1684     }
1685
1686   return 0;
1687
1688 failure:
1689   free_list (point1_list, 0 );
1690   free_list (point2_list, 0 );
1691   
1692   cmdline_parser_clitkProfileImage_release (&local_args_info);
1693   return (EXIT_FAILURE);
1694 }
1695
1696 #ifndef CONFIG_FILE_LINE_SIZE
1697 #define CONFIG_FILE_LINE_SIZE 2048
1698 #endif
1699 #define ADDITIONAL_ERROR " in configuration file "
1700
1701 #define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)
1702 /* 3 is for "--" and "=" */
1703
1704 static int
1705 _cmdline_parser_clitkProfileImage_configfile (char * const filename, int *my_argc)
1706 {
1707   FILE* file;
1708   char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE+1];
1709   char linebuf[CONFIG_FILE_LINE_SIZE];
1710   int line_num = 0;
1711   int result = 0, equal;
1712   char *fopt, *farg;
1713   char *str_index;
1714   size_t len, next_token;
1715   char delimiter;
1716
1717   if ((file = fopen(filename, "r")) == NULL)
1718     {
1719       fprintf (stderr, "%s: Error opening configuration file '%s'\n",
1720                CMDLINE_PARSER_CLITKPROFILEIMAGE_PACKAGE, filename);
1721       return EXIT_FAILURE;
1722     }
1723
1724   while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != NULL)
1725     {
1726       ++line_num;
1727       my_argv[0] = '\0';
1728       len = strlen(linebuf);
1729       if (len > (CONFIG_FILE_LINE_BUFFER_SIZE-1))
1730         {
1731           fprintf (stderr, "%s:%s:%d: Line too long in configuration file\n",
1732                    CMDLINE_PARSER_CLITKPROFILEIMAGE_PACKAGE, filename, line_num);
1733           result = EXIT_FAILURE;
1734           break;
1735         }
1736
1737       /* find first non-whitespace character in the line */
1738       next_token = strspn (linebuf, " \t\r\n");
1739       str_index  = linebuf + next_token;
1740
1741       if ( str_index[0] == '\0' || str_index[0] == '#')
1742         continue; /* empty line or comment line is skipped */
1743
1744       fopt = str_index;
1745
1746       /* truncate fopt at the end of the first non-valid character */
1747       next_token = strcspn (fopt, " \t\r\n=");
1748
1749       if (fopt[next_token] == '\0') /* the line is over */
1750         {
1751           farg  = NULL;
1752           equal = 0;
1753           goto noarg;
1754         }
1755
1756       /* remember if equal sign is present */
1757       equal = (fopt[next_token] == '=');
1758       fopt[next_token++] = '\0';
1759
1760       /* advance pointers to the next token after the end of fopt */
1761       next_token += strspn (fopt + next_token, " \t\r\n");
1762
1763       /* check for the presence of equal sign, and if so, skip it */
1764       if ( !equal )
1765         if ((equal = (fopt[next_token] == '=')))
1766           {
1767             next_token++;
1768             next_token += strspn (fopt + next_token, " \t\r\n");
1769           }
1770       str_index  += next_token;
1771
1772       /* find argument */
1773       farg = str_index;
1774       if ( farg[0] == '\"' || farg[0] == '\'' )
1775         { /* quoted argument */
1776           str_index = strchr (++farg, str_index[0] ); /* skip opening quote */
1777           if (! str_index)
1778             {
1779               fprintf
1780                 (stderr,
1781                  "%s:%s:%d: unterminated string in configuration file\n",
1782                  CMDLINE_PARSER_CLITKPROFILEIMAGE_PACKAGE, filename, line_num);
1783               result = EXIT_FAILURE;
1784               break;
1785             }
1786         }
1787       else
1788         { /* read up the remaining part up to a delimiter */
1789           next_token = strcspn (farg, " \t\r\n#\'\"");
1790           str_index += next_token;
1791         }
1792
1793       /* truncate farg at the delimiter and store it for further check */
1794       delimiter = *str_index, *str_index++ = '\0';
1795
1796       /* everything but comment is illegal at the end of line */
1797       if (delimiter != '\0' && delimiter != '#')
1798         {
1799           str_index += strspn(str_index, " \t\r\n");
1800           if (*str_index != '\0' && *str_index != '#')
1801             {
1802               fprintf
1803                 (stderr,
1804                  "%s:%s:%d: malformed string in configuration file\n",
1805                  CMDLINE_PARSER_CLITKPROFILEIMAGE_PACKAGE, filename, line_num);
1806               result = EXIT_FAILURE;
1807               break;
1808             }
1809         }
1810
1811     noarg:
1812       if (!strcmp(fopt,"include")) {
1813         if (farg && *farg) {
1814           result = _cmdline_parser_clitkProfileImage_configfile(farg, my_argc);
1815         } else {
1816           fprintf(stderr, "%s:%s:%d: include requires a filename argument.\n",
1817                   CMDLINE_PARSER_CLITKPROFILEIMAGE_PACKAGE, filename, line_num);
1818         }
1819         continue;
1820       }
1821       len = strlen(fopt);
1822       strcat (my_argv, len > 1 ? "--" : "-");
1823       strcat (my_argv, fopt);
1824       if (len > 1 && ((farg && *farg) || equal))
1825         strcat (my_argv, "=");
1826       if (farg && *farg)
1827         strcat (my_argv, farg);
1828       ++(*my_argc);
1829
1830       cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
1831       cmd_line_list_tmp->next = cmd_line_list;
1832       cmd_line_list = cmd_line_list_tmp;
1833       cmd_line_list->string_arg = gengetopt_strdup(my_argv);
1834     } /* while */
1835
1836   if (file)
1837     fclose(file);
1838   return result;
1839 }
1840
1841 int
1842 cmdline_parser_clitkProfileImage_configfile (char * const filename,
1843                            struct args_info_clitkProfileImage *args_info,
1844                            int override, int initialize, int check_required)
1845 {
1846   struct cmdline_parser_clitkProfileImage_params params;
1847
1848   params.override = override;
1849   params.initialize = initialize;
1850   params.check_required = check_required;
1851   params.check_ambiguity = 0;
1852   params.print_errors = 1;
1853   
1854   return cmdline_parser_clitkProfileImage_config_file (filename, args_info, &params);
1855 }
1856
1857 int
1858 cmdline_parser_clitkProfileImage_config_file (char * const filename,
1859                            struct args_info_clitkProfileImage *args_info,
1860                            struct cmdline_parser_clitkProfileImage_params *params)
1861 {
1862   int i, result;
1863   int my_argc = 1;
1864   char **my_argv_arg;
1865   char *additional_error;
1866
1867   /* store the program name */
1868   cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
1869   cmd_line_list_tmp->next = cmd_line_list;
1870   cmd_line_list = cmd_line_list_tmp;
1871   cmd_line_list->string_arg = gengetopt_strdup (CMDLINE_PARSER_CLITKPROFILEIMAGE_PACKAGE);
1872
1873   result = _cmdline_parser_clitkProfileImage_configfile(filename, &my_argc);
1874
1875   if (result != EXIT_FAILURE) {
1876     my_argv_arg = (char **) malloc((my_argc+1) * sizeof(char *));
1877     cmd_line_list_tmp = cmd_line_list;
1878
1879     for (i = my_argc - 1; i >= 0; --i) {
1880       my_argv_arg[i] = cmd_line_list_tmp->string_arg;
1881       cmd_line_list_tmp = cmd_line_list_tmp->next;
1882     }
1883
1884     my_argv_arg[my_argc] = 0;
1885
1886     additional_error = (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) + 1);
1887     strcpy (additional_error, ADDITIONAL_ERROR);
1888     strcat (additional_error, filename);
1889     result =
1890       cmdline_parser_clitkProfileImage_internal (my_argc, my_argv_arg, args_info,
1891                               params,
1892                               additional_error);
1893
1894     free (additional_error);
1895     free (my_argv_arg);
1896   }
1897
1898   free_cmd_list();
1899   if (result == EXIT_FAILURE)
1900     {
1901       cmdline_parser_clitkProfileImage_free (args_info);
1902       exit (EXIT_FAILURE);
1903     }
1904   
1905   return result;
1906 }