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