]> Creatis software - creaImageIO.git/blob - appli/gimmick/gimmick_ggo.c
*** empty log message ***
[creaImageIO.git] / appli / gimmick / gimmick_ggo.c
1 /*
2   File autogenerated by gengetopt version 2.20
3   generated with the following command:
4   gengetopt --file-name=gimmick_ggo --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 "gimmick_ggo.h"
22
23 const char *gengetopt_args_info_purpose = "";
24
25 const char *gengetopt_args_info_usage = "Usage: gimmick COMMAND [OPTIONS]";
26
27 const char *gengetopt_args_info_description = "";
28
29 const char *gengetopt_args_info_help[] = {
30   "  -h, --help              Print help and exit",
31   "  -V, --version           Print version and exit",
32   "\nCOMMANDS:",
33   "  -p, --print             Prints the local database tree  (default=off)",
34   "  -f, --file=<file name>  Adds the file to local database",
35   "  -d, --dir=<path>        Adds the directory to local database",
36   "  -r, --recurse           Recurse into sub-directories  (default=off)",
37   "\nOPTIONS:",
38   "  -v, --verbose=INT       Verbosity level  (default=`1')",
39   "  -D, --debug=INT         Debug messages level  (default=`0')",
40     0
41 };
42
43 static
44 void clear_given (struct gengetopt_args_info *args_info);
45 static
46 void clear_args (struct gengetopt_args_info *args_info);
47
48 static int
49 cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required, const char *additional_error);
50
51 static int
52 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error);
53
54 static char *
55 gengetopt_strdup (const char *s);
56
57 static
58 void clear_given (struct gengetopt_args_info *args_info)
59 {
60   args_info->help_given = 0 ;
61   args_info->version_given = 0 ;
62   args_info->print_given = 0 ;
63   args_info->file_given = 0 ;
64   args_info->dir_given = 0 ;
65   args_info->recurse_given = 0 ;
66   args_info->verbose_given = 0 ;
67   args_info->debug_given = 0 ;
68 }
69
70 static
71 void clear_args (struct gengetopt_args_info *args_info)
72 {
73   args_info->print_flag = 0;
74   args_info->file_arg = NULL;
75   args_info->file_orig = NULL;
76   args_info->dir_arg = NULL;
77   args_info->dir_orig = NULL;
78   args_info->recurse_flag = 0;
79   args_info->verbose_arg = 1;
80   args_info->verbose_orig = NULL;
81   args_info->debug_arg = 0;
82   args_info->debug_orig = NULL;
83   
84 }
85
86 static
87 void init_args_info(struct gengetopt_args_info *args_info)
88 {
89   args_info->help_help = gengetopt_args_info_help[0] ;
90   args_info->version_help = gengetopt_args_info_help[1] ;
91   args_info->print_help = gengetopt_args_info_help[3] ;
92   args_info->file_help = gengetopt_args_info_help[4] ;
93   args_info->dir_help = gengetopt_args_info_help[5] ;
94   args_info->recurse_help = gengetopt_args_info_help[6] ;
95   args_info->verbose_help = gengetopt_args_info_help[8] ;
96   args_info->debug_help = gengetopt_args_info_help[9] ;
97   
98 }
99
100 void
101 cmdline_parser_print_version (void)
102 {
103   printf ("%s %s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
104 }
105
106 void
107 cmdline_parser_print_help (void)
108 {
109   int i = 0;
110   cmdline_parser_print_version ();
111
112   if (strlen(gengetopt_args_info_purpose) > 0)
113     printf("\n%s\n", gengetopt_args_info_purpose);
114
115   printf("\n%s\n\n", gengetopt_args_info_usage);
116
117   if (strlen(gengetopt_args_info_description) > 0)
118     printf("%s\n", gengetopt_args_info_description);
119
120   while (gengetopt_args_info_help[i])
121     printf("%s\n", gengetopt_args_info_help[i++]);
122 }
123
124 void
125 cmdline_parser_init (struct gengetopt_args_info *args_info)
126 {
127   clear_given (args_info);
128   clear_args (args_info);
129   init_args_info (args_info);
130 }
131
132 static void
133 cmdline_parser_release (struct gengetopt_args_info *args_info)
134 {
135   
136   if (args_info->file_arg)
137     {
138       free (args_info->file_arg); /* free previous argument */
139       args_info->file_arg = 0;
140     }
141   if (args_info->file_orig)
142     {
143       free (args_info->file_orig); /* free previous argument */
144       args_info->file_orig = 0;
145     }
146   if (args_info->dir_arg)
147     {
148       free (args_info->dir_arg); /* free previous argument */
149       args_info->dir_arg = 0;
150     }
151   if (args_info->dir_orig)
152     {
153       free (args_info->dir_orig); /* free previous argument */
154       args_info->dir_orig = 0;
155     }
156   if (args_info->verbose_orig)
157     {
158       free (args_info->verbose_orig); /* free previous argument */
159       args_info->verbose_orig = 0;
160     }
161   if (args_info->debug_orig)
162     {
163       free (args_info->debug_orig); /* free previous argument */
164       args_info->debug_orig = 0;
165     }
166   
167   clear_given (args_info);
168 }
169
170 int
171 cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
172 {
173   FILE *outfile;
174   int i = 0;
175
176   outfile = fopen(filename, "w");
177
178   if (!outfile)
179     {
180       fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
181       return EXIT_FAILURE;
182     }
183
184   if (args_info->help_given) {
185     fprintf(outfile, "%s\n", "help");
186   }
187   if (args_info->version_given) {
188     fprintf(outfile, "%s\n", "version");
189   }
190   if (args_info->print_given) {
191     fprintf(outfile, "%s\n", "print");
192   }
193   if (args_info->file_given) {
194     if (args_info->file_orig) {
195       fprintf(outfile, "%s=\"%s\"\n", "file", args_info->file_orig);
196     } else {
197       fprintf(outfile, "%s\n", "file");
198     }
199   }
200   if (args_info->dir_given) {
201     if (args_info->dir_orig) {
202       fprintf(outfile, "%s=\"%s\"\n", "dir", args_info->dir_orig);
203     } else {
204       fprintf(outfile, "%s\n", "dir");
205     }
206   }
207   if (args_info->recurse_given) {
208     fprintf(outfile, "%s\n", "recurse");
209   }
210   if (args_info->verbose_given) {
211     if (args_info->verbose_orig) {
212       fprintf(outfile, "%s=\"%s\"\n", "verbose", args_info->verbose_orig);
213     } else {
214       fprintf(outfile, "%s\n", "verbose");
215     }
216   }
217   if (args_info->debug_given) {
218     if (args_info->debug_orig) {
219       fprintf(outfile, "%s=\"%s\"\n", "debug", args_info->debug_orig);
220     } else {
221       fprintf(outfile, "%s\n", "debug");
222     }
223   }
224   
225   fclose (outfile);
226
227   i = EXIT_SUCCESS;
228   return i;
229 }
230
231 void
232 cmdline_parser_free (struct gengetopt_args_info *args_info)
233 {
234   cmdline_parser_release (args_info);
235 }
236
237
238 /* gengetopt_strdup() */
239 /* strdup.c replacement of strdup, which is not standard */
240 char *
241 gengetopt_strdup (const char *s)
242 {
243   char *result = NULL;
244   if (!s)
245     return result;
246
247   result = (char*)malloc(strlen(s) + 1);
248   if (result == (char*)0)
249     return (char*)0;
250   strcpy(result, s);
251   return result;
252 }
253
254 int
255 cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info)
256 {
257   return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
258 }
259
260 int
261 cmdline_parser2 (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
262 {
263   int result;
264
265   result = cmdline_parser_internal (argc, argv, args_info, override, initialize, check_required, NULL);
266
267   if (result == EXIT_FAILURE)
268     {
269       cmdline_parser_free (args_info);
270       exit (EXIT_FAILURE);
271     }
272   
273   return result;
274 }
275
276 int
277 cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
278 {
279   int result = EXIT_SUCCESS;
280
281   if (cmdline_parser_required2(args_info, prog_name, NULL) > 0)
282     result = EXIT_FAILURE;
283
284   if (result == EXIT_FAILURE)
285     {
286       cmdline_parser_free (args_info);
287       exit (EXIT_FAILURE);
288     }
289   
290   return result;
291 }
292
293 int
294 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error)
295 {
296   int error = 0;
297
298   /* checks for required options */
299   
300   /* checks for dependences among options */
301   if (args_info->recurse_given && ! args_info->dir_given)
302     {
303       fprintf (stderr, "%s: '--recurse' ('-r') option depends on option 'dir'%s\n", prog_name, (additional_error ? additional_error : ""));
304       error = 1;
305     }
306
307   return error;
308 }
309
310 /*
311  * Extracted from the glibc source tree, version 2.3.6
312  *
313  * Licensed under the GPL as per the whole glibc source tree.
314  *
315  * This file was modified so that getopt_long can be called
316  * many times without risking previous memory to be spoiled.
317  *
318  * Modified by Andre Noll and Lorenzo Bettini for use in
319  * GNU gengetopt generated files.
320  *
321  */
322
323 /* 
324  * we must include anything we need since this file is not thought to be
325  * inserted in a file already using getopt.h
326  *
327  * Lorenzo
328  */
329
330 struct option
331 {
332   const char *name;
333   /* has_arg can't be an enum because some compilers complain about
334      type mismatches in all the code that assumes it is an int.  */
335   int has_arg;
336   int *flag;
337   int val;
338 };
339
340 /* For communication from `getopt' to the caller.
341    When `getopt' finds an option that takes an argument,
342    the argument value is returned here.
343    Also, when `ordering' is RETURN_IN_ORDER,
344    each non-option ARGV-element is returned here.  */
345
346 static char *optarg;
347
348 /* Index in ARGV of the next element to be scanned.
349    This is used for communication to and from the caller
350    and for communication between successive calls to `getopt'.
351
352    On entry to `getopt', zero means this is the first call; initialize.
353
354    When `getopt' returns -1, this is the index of the first of the
355    non-option elements that the caller should itself scan.
356
357    Otherwise, `optind' communicates from one call to the next
358    how much of ARGV has been scanned so far.  */
359
360 static int optind;
361
362 /* Callers store zero here to inhibit the error message `getopt' prints
363    for unrecognized options.  */
364
365 static int opterr;
366
367 /* Set to an option character which was unrecognized.  */
368
369 static int optopt;
370
371 /* This version of `getopt' appears to the caller like standard Unix `getopt'
372    but it behaves differently for the user, since it allows the user
373    to intersperse the options with the other arguments.
374
375    As `getopt' works, it permutes the elements of ARGV so that,
376    when it is done, all the options precede everything else.  Thus
377    all application programs are extended to handle flexible argument order.
378 */
379 /*
380    If the field `flag' is not NULL, it points to a variable that is set
381    to the value given in the field `val' when the option is found, but
382    left unchanged if the option is not found.
383
384    To have a long-named option do something other than set an `int' to
385    a compiled-in constant, such as set a value from `custom_optarg', set the
386    option's `flag' field to zero and its `val' field to a nonzero
387    value (the equivalent single-letter option character, if there is
388    one).  For long options that have a zero `flag' field, `getopt'
389    returns the contents of the `val' field.  */
390
391 /* Names for the values of the `has_arg' field of `struct option'.  */
392 #ifndef no_argument
393 #define no_argument             0
394 #endif
395
396 #ifndef required_argument
397 #define required_argument       1
398 #endif
399
400 #ifndef optional_argument
401 #define optional_argument       2
402 #endif
403
404 struct custom_getopt_data {
405         /*
406          * These have exactly the same meaning as the corresponding global variables,
407          * except that they are used for the reentrant versions of getopt.
408          */
409         int custom_optind;
410         int custom_opterr;
411         int custom_optopt;
412         char *custom_optarg;
413
414         /* True if the internal members have been initialized.  */
415         int initialized;
416
417         /*
418          * The next char to be scanned in the option-element in which the last option
419          * character we returned was found.  This allows us to pick up the scan where
420          * we left off.  If this is zero, or a null string, it means resume the scan by
421          * advancing to the next ARGV-element.
422          */
423         char *nextchar;
424
425         /*
426          * Describe the part of ARGV that contains non-options that have been skipped.
427          * `first_nonopt' is the index in ARGV of the first of them; `last_nonopt' is
428          * the index after the last of them.
429          */
430         int first_nonopt;
431         int last_nonopt;
432 };
433
434 /*
435  * the variables optarg, optind, opterr and optopt are renamed with
436  * the custom_ prefix so that they don't interfere with getopt ones.
437  *
438  * Moreover they're static so they are visible only from within the
439  * file where this very file will be included.
440  */
441
442 /*
443  * For communication from `custom_getopt' to the caller.  When `custom_getopt' finds an
444  * option that takes an argument, the argument value is returned here.
445  */
446 static char *custom_optarg;
447
448 /*
449  * Index in ARGV of the next element to be scanned.  This is used for
450  * communication to and from the caller and for communication between
451  * successive calls to `custom_getopt'.
452  *
453  * On entry to `custom_getopt', 1 means this is the first call; initialize.
454  *
455  * When `custom_getopt' returns -1, this is the index of the first of the non-option
456  * elements that the caller should itself scan.
457  *
458  * Otherwise, `custom_optind' communicates from one call to the next how much of ARGV
459  * has been scanned so far.
460  *
461  * 1003.2 says this must be 1 before any call.
462  */
463 static int custom_optind = 1;
464
465 /*
466  * Callers store zero here to inhibit the error message for unrecognized
467  * options.
468  */
469 static int custom_opterr = 1;
470
471 /*
472  * Set to an option character which was unrecognized.  This must be initialized
473  * on some systems to avoid linking in the system's own getopt implementation.
474  */
475 static int custom_optopt = '?';
476
477 /*
478  * Exchange two adjacent subsequences of ARGV.  One subsequence is elements
479  * [first_nonopt,last_nonopt) which contains all the non-options that have been
480  * skipped so far.  The other is elements [last_nonopt,custom_optind), which contains
481  * all the options processed since those non-options were skipped.
482  * `first_nonopt' and `last_nonopt' are relocated so that they describe the new
483  * indices of the non-options in ARGV after they are moved.
484  */
485 static void exchange(char **argv, struct custom_getopt_data *d)
486 {
487         int bottom = d->first_nonopt;
488         int middle = d->last_nonopt;
489         int top = d->custom_optind;
490         char *tem;
491
492         /*
493          * Exchange the shorter segment with the far end of the longer segment.
494          * That puts the shorter segment into the right place.  It leaves the
495          * longer segment in the right place overall, but it consists of two
496          * parts that need to be swapped next.
497          */
498         while (top > middle && middle > bottom) {
499                 if (top - middle > middle - bottom) {
500                         /* Bottom segment is the short one.  */
501                         int len = middle - bottom;
502                         int i;
503
504                         /* Swap it with the top part of the top segment.  */
505                         for (i = 0; i < len; i++) {
506                                 tem = argv[bottom + i];
507                                 argv[bottom + i] =
508                                         argv[top - (middle - bottom) + i];
509                                 argv[top - (middle - bottom) + i] = tem;
510                         }
511                         /* Exclude the moved bottom segment from further swapping.  */
512                         top -= len;
513                 } else {
514                         /* Top segment is the short one.  */
515                         int len = top - middle;
516                         int i;
517
518                         /* Swap it with the bottom part of the bottom segment.  */
519                         for (i = 0; i < len; i++) {
520                                 tem = argv[bottom + i];
521                                 argv[bottom + i] = argv[middle + i];
522                                 argv[middle + i] = tem;
523                         }
524                         /* Exclude the moved top segment from further swapping.  */
525                         bottom += len;
526                 }
527         }
528         /* Update records for the slots the non-options now occupy.  */
529         d->first_nonopt += (d->custom_optind - d->last_nonopt);
530         d->last_nonopt = d->custom_optind;
531 }
532
533 /* Initialize the internal data when the first call is made.  */
534 static void custom_getopt_initialize(struct custom_getopt_data *d)
535 {
536         /*
537          * Start processing options with ARGV-element 1 (since ARGV-element 0
538          * is the program name); the sequence of previously skipped non-option
539          * ARGV-elements is empty.
540          */
541         d->first_nonopt = d->last_nonopt = d->custom_optind;
542         d->nextchar = NULL;
543         d->initialized = 1;
544 }
545
546 #define NONOPTION_P (argv[d->custom_optind][0] != '-' || argv[d->custom_optind][1] == '\0')
547
548 /* return: zero: continue, nonzero: return given value to user */
549 static int shuffle_argv(int argc, char *const *argv,const struct option *longopts,
550         struct custom_getopt_data *d)
551 {
552         /*
553          * Give FIRST_NONOPT & LAST_NONOPT rational values if CUSTOM_OPTIND has been
554          * moved back by the user (who may also have changed the arguments).
555          */
556         if (d->last_nonopt > d->custom_optind)
557                 d->last_nonopt = d->custom_optind;
558         if (d->first_nonopt > d->custom_optind)
559                 d->first_nonopt = d->custom_optind;
560         /*
561          * If we have just processed some options following some
562          * non-options, exchange them so that the options come first.
563          */
564         if (d->first_nonopt != d->last_nonopt &&
565                         d->last_nonopt != d->custom_optind)
566                 exchange((char **) argv, d);
567         else if (d->last_nonopt != d->custom_optind)
568                 d->first_nonopt = d->custom_optind;
569         /*
570          * Skip any additional non-options and extend the range of
571          * non-options previously skipped.
572          */
573         while (d->custom_optind < argc && NONOPTION_P)
574                 d->custom_optind++;
575         d->last_nonopt = d->custom_optind;
576         /*
577          * The special ARGV-element `--' means premature end of options.  Skip
578          * it like a null option, then exchange with previous non-options as if
579          * it were an option, then skip everything else like a non-option.
580          */
581         if (d->custom_optind != argc && !strcmp(argv[d->custom_optind], "--")) {
582                 d->custom_optind++;
583                 if (d->first_nonopt != d->last_nonopt
584                                 && d->last_nonopt != d->custom_optind)
585                         exchange((char **) argv, d);
586                 else if (d->first_nonopt == d->last_nonopt)
587                         d->first_nonopt = d->custom_optind;
588                 d->last_nonopt = argc;
589                 d->custom_optind = argc;
590         }
591         /*
592          * If we have done all the ARGV-elements, stop the scan and back over
593          * any non-options that we skipped and permuted.
594          */
595         if (d->custom_optind == argc) {
596                 /*
597                  * Set the next-arg-index to point at the non-options that we
598                  * previously skipped, so the caller will digest them.
599                  */
600                 if (d->first_nonopt != d->last_nonopt)
601                         d->custom_optind = d->first_nonopt;
602                 return -1;
603         }
604         /*
605          * If we have come to a non-option and did not permute it, either stop
606          * the scan or describe it to the caller and pass it by.
607          */
608         if (NONOPTION_P) {
609                 d->custom_optarg = argv[d->custom_optind++];
610                 return 1;
611         }
612         /*
613          * We have found another option-ARGV-element. Skip the initial
614          * punctuation.
615          */
616         d->nextchar = (argv[d->custom_optind] + 1 + (longopts != NULL && argv[d->custom_optind][1] == '-'));
617         return 0;
618 }
619
620 /*
621  * Check whether the ARGV-element is a long option.
622  *
623  * If there's a long option "fubar" and the ARGV-element is "-fu", consider
624  * that an abbreviation of the long option, just like "--fu", and not "-f" with
625  * arg "u".
626  *
627  * This distinction seems to be the most useful approach.
628  *
629  */
630 static int check_long_opt(int argc, char *const *argv, const char *optstring,
631                 const struct option *longopts, int *longind,
632                 int print_errors, struct custom_getopt_data *d)
633 {
634         char *nameend;
635         const struct option *p;
636         const struct option *pfound = NULL;
637         int exact = 0;
638         int ambig = 0;
639         int indfound = -1;
640         int option_index;
641
642         for (nameend = d->nextchar; *nameend && *nameend != '='; nameend++)
643                 /* Do nothing.  */ ;
644
645         /* Test all long options for either exact match or abbreviated matches */
646         for (p = longopts, option_index = 0; p->name; p++, option_index++)
647                 if (!strncmp(p->name, d->nextchar, nameend - d->nextchar)) {
648                         if ((unsigned int) (nameend - d->nextchar)
649                                         == (unsigned int) strlen(p->name)) {
650                                 /* Exact match found.  */
651                                 pfound = p;
652                                 indfound = option_index;
653                                 exact = 1;
654                                 break;
655                         } else if (pfound == NULL) {
656                                 /* First nonexact match found.  */
657                                 pfound = p;
658                                 indfound = option_index;
659                         } else if (pfound->has_arg != p->has_arg
660                                         || pfound->flag != p->flag
661                                         || pfound->val != p->val)
662                                 /* Second or later nonexact match found.  */
663                                 ambig = 1;
664                 }
665         if (ambig && !exact) {
666                 if (print_errors) {
667                         fprintf(stderr,
668                                 "%s: option `%s' is ambiguous\n",
669                                 argv[0], argv[d->custom_optind]);
670                 }
671                 d->nextchar += strlen(d->nextchar);
672                 d->custom_optind++;
673                 d->custom_optopt = 0;
674                 return '?';
675         }
676         if (pfound) {
677                 option_index = indfound;
678                 d->custom_optind++;
679                 if (*nameend) {
680                         if (pfound->has_arg != no_argument)
681                                 d->custom_optarg = nameend + 1;
682                         else {
683                                 if (print_errors) {
684                                         if (argv[d->custom_optind - 1][1] == '-') {
685                                                 /* --option */
686                                                 fprintf(stderr, "%s: option `--%s' doesn't allow an argument\n",
687                                                         argv[0], pfound->name);
688                                         } else {
689                                                 /* +option or -option */
690                                                 fprintf(stderr, "%s: option `%c%s' doesn't allow an argument\n",
691                                                         argv[0], argv[d->custom_optind - 1][0], pfound->name);
692                                         }
693
694                                 }
695                                 d->nextchar += strlen(d->nextchar);
696                                 d->custom_optopt = pfound->val;
697                                 return '?';
698                         }
699                 } else if (pfound->has_arg == required_argument) {
700                         if (d->custom_optind < argc)
701                                 d->custom_optarg = argv[d->custom_optind++];
702                         else {
703                                 if (print_errors) {
704                                         fprintf(stderr,
705                                                 "%s: option `%s' requires an argument\n",
706                                                 argv[0],
707                                                 argv[d->custom_optind - 1]);
708                                 }
709                                 d->nextchar += strlen(d->nextchar);
710                                 d->custom_optopt = pfound->val;
711                                 return optstring[0] == ':' ? ':' : '?';
712                         }
713                 }
714                 d->nextchar += strlen(d->nextchar);
715                 if (longind != NULL)
716                         *longind = option_index;
717                 if (pfound->flag) {
718                         *(pfound->flag) = pfound->val;
719                         return 0;
720                 }
721                 return pfound->val;
722         }
723         /*
724          * Can't find it as a long option.  If this is not getopt_long_only, or
725          * the option starts with '--' or is not a valid short option, then
726          * it's an error.  Otherwise interpret it as a short option.
727          */
728         if (print_errors) {
729                 if (argv[d->custom_optind][1] == '-') {
730                         /* --option */
731                         fprintf(stderr,
732                                 "%s: unrecognized option `--%s'\n",
733                                 argv[0], d->nextchar);
734                 } else {
735                         /* +option or -option */
736                         fprintf(stderr,
737                                 "%s: unrecognized option `%c%s'\n",
738                                 argv[0], argv[d->custom_optind][0],
739                                 d->nextchar);
740                 }
741         }
742         d->nextchar = (char *) "";
743         d->custom_optind++;
744         d->custom_optopt = 0;
745         return '?';
746 }
747
748 static int check_short_opt(int argc, char *const *argv, const char *optstring,
749                 int print_errors, struct custom_getopt_data *d)
750 {
751         char c = *d->nextchar++;
752         char *temp = strchr(optstring, c);
753
754         /* Increment `custom_optind' when we start to process its last character.  */
755         if (*d->nextchar == '\0')
756                 ++d->custom_optind;
757         if (!temp || c == ':') {
758                 if (print_errors)
759                         fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
760
761                 d->custom_optopt = c;
762                 return '?';
763         }
764         if (temp[1] == ':') {
765                 if (temp[2] == ':') {
766                         /* This is an option that accepts an argument optionally.  */
767                         if (*d->nextchar != '\0') {
768                                 d->custom_optarg = d->nextchar;
769                                 d->custom_optind++;
770                         } else
771                                 d->custom_optarg = NULL;
772                         d->nextchar = NULL;
773                 } else {
774                         /* This is an option that requires an argument.  */
775                         if (*d->nextchar != '\0') {
776                                 d->custom_optarg = d->nextchar;
777                                 /*
778                                  * If we end this ARGV-element by taking the
779                                  * rest as an arg, we must advance to the next
780                                  * element now.
781                                  */
782                                 d->custom_optind++;
783                         } else if (d->custom_optind == argc) {
784                                 if (print_errors) {
785                                         fprintf(stderr,
786                                                 "%s: option requires an argument -- %c\n",
787                                                 argv[0], c);
788                                 }
789                                 d->custom_optopt = c;
790                                 if (optstring[0] == ':')
791                                         c = ':';
792                                 else
793                                         c = '?';
794                         } else
795                                 /*
796                                  * We already incremented `custom_optind' once;
797                                  * increment it again when taking next ARGV-elt
798                                  * as argument.
799                                  */
800                                 d->custom_optarg = argv[d->custom_optind++];
801                         d->nextchar = NULL;
802                 }
803         }
804         return c;
805 }
806
807 /*
808  * Scan elements of ARGV for option characters given in OPTSTRING.
809  *
810  * If an element of ARGV starts with '-', and is not exactly "-" or "--",
811  * then it is an option element.  The characters of this element
812  * (aside from the initial '-') are option characters.  If `getopt'
813  * is called repeatedly, it returns successively each of the option characters
814  * from each of the option elements.
815  *
816  * If `getopt' finds another option character, it returns that character,
817  * updating `custom_optind' and `nextchar' so that the next call to `getopt' can
818  * resume the scan with the following option character or ARGV-element.
819  *
820  * If there are no more option characters, `getopt' returns -1.
821  * Then `custom_optind' is the index in ARGV of the first ARGV-element
822  * that is not an option.  (The ARGV-elements have been permuted
823  * so that those that are not options now come last.)
824  *
825  * OPTSTRING is a string containing the legitimate option characters.
826  * If an option character is seen that is not listed in OPTSTRING,
827  * return '?' after printing an error message.  If you set `custom_opterr' to
828  * zero, the error message is suppressed but we still return '?'.
829  *
830  * If a char in OPTSTRING is followed by a colon, that means it wants an arg,
831  * so the following text in the same ARGV-element, or the text of the following
832  * ARGV-element, is returned in `custom_optarg'.  Two colons mean an option that
833  * wants an optional arg; if there is text in the current ARGV-element,
834  * it is returned in `custom_optarg', otherwise `custom_optarg' is set to zero.
835  *
836  * If OPTSTRING starts with `-' or `+', it requests different methods of
837  * handling the non-option ARGV-elements.
838  * See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
839  *
840  * Long-named options begin with `--' instead of `-'.
841  * Their names may be abbreviated as long as the abbreviation is unique
842  * or is an exact match for some defined option.  If they have an
843  * argument, it follows the option name in the same ARGV-element, separated
844  * from the option name by a `=', or else the in next ARGV-element.
845  * When `getopt' finds a long-named option, it returns 0 if that option's
846  * `flag' field is nonzero, the value of the option's `val' field
847  * if the `flag' field is zero.
848  *
849  * The elements of ARGV aren't really const, because we permute them.
850  * But we pretend they're const in the prototype to be compatible
851  * with other systems.
852  *
853  * LONGOPTS is a vector of `struct option' terminated by an
854  * element containing a name which is zero.
855  *
856  * LONGIND returns the index in LONGOPT of the long-named option found.
857  * It is only valid when a long-named option has been found by the most
858  * recent call.
859  *
860  * Return the option character from OPTS just read.  Return -1 when there are
861  * no more options.  For unrecognized options, or options missing arguments,
862  * `custom_optopt' is set to the option letter, and '?' is returned.
863  *
864  * The OPTS string is a list of characters which are recognized option letters,
865  * optionally followed by colons, specifying that that letter takes an
866  * argument, to be placed in `custom_optarg'.
867  *
868  * If a letter in OPTS is followed by two colons, its argument is optional.
869  * This behavior is specific to the GNU `getopt'.
870  *
871  * The argument `--' causes premature termination of argument scanning,
872  * explicitly telling `getopt' that there are no more options.  If OPTS begins
873  * with `--', then non-option arguments are treated as arguments to the option
874  * '\0'.  This behavior is specific to the GNU `getopt'.
875  */
876
877 static int getopt_internal_r(int argc, char *const *argv, const char *optstring,
878                 const struct option *longopts, int *longind,
879                 struct custom_getopt_data *d)
880 {
881         int ret, print_errors = d->custom_opterr;
882
883         if (optstring[0] == ':')
884                 print_errors = 0;
885         if (argc < 1)
886                 return -1;
887         d->custom_optarg = NULL;
888
889         /* 
890          * This is a big difference with GNU getopt, since optind == 0
891          * means initialization while here 1 means first call.
892          */
893         if (d->custom_optind == 0 || !d->initialized) {
894                 if (d->custom_optind == 0)
895                         d->custom_optind = 1;   /* Don't scan ARGV[0], the program name.  */
896                 custom_getopt_initialize(d);
897         }
898         if (d->nextchar == NULL || *d->nextchar == '\0') {
899                 ret = shuffle_argv(argc, argv, longopts, d);
900                 if (ret)
901                         return ret;
902         }
903         if (longopts && (argv[d->custom_optind][1] == '-' ))
904                 return check_long_opt(argc, argv, optstring, longopts,
905                         longind, print_errors, d);
906         return check_short_opt(argc, argv, optstring, print_errors, d);
907 }
908
909 static int custom_getopt_internal(int argc, char *const *argv, const char *optstring,
910         const struct option *longopts, int *longind)
911 {
912         int result;
913         /* Keep a global copy of all internal members of d */
914         static struct custom_getopt_data d;
915
916         d.custom_optind = custom_optind;
917         d.custom_opterr = custom_opterr;
918         result = getopt_internal_r(argc, argv, optstring, longopts,
919                 longind, &d);
920         custom_optind = d.custom_optind;
921         custom_optarg = d.custom_optarg;
922         custom_optopt = d.custom_optopt;
923         return result;
924 }
925
926 static int custom_getopt_long (int argc, char *const *argv, const char *options,
927         const struct option *long_options, int *opt_index)
928 {
929         return custom_getopt_internal(argc, argv, options, long_options,
930                 opt_index);
931 }
932
933 int
934 cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required, const char *additional_error)
935 {
936   int c;        /* Character of the parsed option.  */
937
938   int error = 0;
939   struct gengetopt_args_info local_args_info;
940
941   if (initialize)
942     cmdline_parser_init (args_info);
943
944   cmdline_parser_init (&local_args_info);
945
946   optarg = 0;
947   optind = 0;
948   opterr = 1;
949   optopt = '?';
950
951   while (1)
952     {
953       int option_index = 0;
954       char *stop_char;
955
956       static struct option long_options[] = {
957         { "help",       0, NULL, 'h' },
958         { "version",    0, NULL, 'V' },
959         { "print",      0, NULL, 'p' },
960         { "file",       1, NULL, 'f' },
961         { "dir",        1, NULL, 'd' },
962         { "recurse",    0, NULL, 'r' },
963         { "verbose",    1, NULL, 'v' },
964         { "debug",      1, NULL, 'D' },
965         { NULL, 0, NULL, 0 }
966       };
967
968       stop_char = 0;
969       custom_optarg = optarg;
970       custom_optind = optind;
971       custom_opterr = opterr;
972       custom_optopt = optopt;
973
974       c = custom_getopt_long (argc, argv, "hVpf:d:rv:D:", long_options, &option_index);
975
976       optarg = custom_optarg;
977       optind = custom_optind;
978       opterr = custom_opterr;
979       optopt = custom_optopt;
980
981       if (c == -1) break;       /* Exit from `while (1)' loop.  */
982
983       switch (c)
984         {
985         case 'h':       /* Print help and exit.  */
986           cmdline_parser_print_help ();
987           cmdline_parser_free (&local_args_info);
988           exit (EXIT_SUCCESS);
989
990         case 'V':       /* Print version and exit.  */
991           cmdline_parser_print_version ();
992           cmdline_parser_free (&local_args_info);
993           exit (EXIT_SUCCESS);
994
995         case 'p':       /* Prints the local database tree.  */
996           if (local_args_info.print_given)
997             {
998               fprintf (stderr, "%s: `--print' (`-p') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
999               goto failure;
1000             }
1001           if (args_info->print_given && ! override)
1002             continue;
1003           local_args_info.print_given = 1;
1004           args_info->print_given = 1;
1005           args_info->print_flag = !(args_info->print_flag);
1006           break;
1007
1008         case 'f':       /* Adds the file to local database.  */
1009           if (local_args_info.file_given)
1010             {
1011               fprintf (stderr, "%s: `--file' (`-f') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
1012               goto failure;
1013             }
1014           if (args_info->file_given && ! override)
1015             continue;
1016           local_args_info.file_given = 1;
1017           args_info->file_given = 1;
1018           if (args_info->file_arg)
1019             free (args_info->file_arg); /* free previous string */
1020           args_info->file_arg = gengetopt_strdup (optarg);
1021           if (args_info->file_orig)
1022             free (args_info->file_orig); /* free previous string */
1023           args_info->file_orig = gengetopt_strdup (optarg);
1024           break;
1025
1026         case 'd':       /* Adds the directory to local database.  */
1027           if (local_args_info.dir_given)
1028             {
1029               fprintf (stderr, "%s: `--dir' (`-d') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
1030               goto failure;
1031             }
1032           if (args_info->dir_given && ! override)
1033             continue;
1034           local_args_info.dir_given = 1;
1035           args_info->dir_given = 1;
1036           if (args_info->dir_arg)
1037             free (args_info->dir_arg); /* free previous string */
1038           args_info->dir_arg = gengetopt_strdup (optarg);
1039           if (args_info->dir_orig)
1040             free (args_info->dir_orig); /* free previous string */
1041           args_info->dir_orig = gengetopt_strdup (optarg);
1042           break;
1043
1044         case 'r':       /* Recurse into sub-directories.  */
1045           if (local_args_info.recurse_given)
1046             {
1047               fprintf (stderr, "%s: `--recurse' (`-r') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
1048               goto failure;
1049             }
1050           if (args_info->recurse_given && ! override)
1051             continue;
1052           local_args_info.recurse_given = 1;
1053           args_info->recurse_given = 1;
1054           args_info->recurse_flag = !(args_info->recurse_flag);
1055           break;
1056
1057         case 'v':       /* Verbosity level.  */
1058           if (local_args_info.verbose_given)
1059             {
1060               fprintf (stderr, "%s: `--verbose' (`-v') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
1061               goto failure;
1062             }
1063           if (args_info->verbose_given && ! override)
1064             continue;
1065           local_args_info.verbose_given = 1;
1066           args_info->verbose_given = 1;
1067           args_info->verbose_arg = strtol (optarg, &stop_char, 0);
1068           if (!(stop_char && *stop_char == '\0')) {
1069             fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
1070             goto failure;
1071           }
1072           if (args_info->verbose_orig)
1073             free (args_info->verbose_orig); /* free previous string */
1074           args_info->verbose_orig = gengetopt_strdup (optarg);
1075           break;
1076
1077         case 'D':       /* Debug messages level.  */
1078           if (local_args_info.debug_given)
1079             {
1080               fprintf (stderr, "%s: `--debug' (`-D') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
1081               goto failure;
1082             }
1083           if (args_info->debug_given && ! override)
1084             continue;
1085           local_args_info.debug_given = 1;
1086           args_info->debug_given = 1;
1087           args_info->debug_arg = strtol (optarg, &stop_char, 0);
1088           if (!(stop_char && *stop_char == '\0')) {
1089             fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
1090             goto failure;
1091           }
1092           if (args_info->debug_orig)
1093             free (args_info->debug_orig); /* free previous string */
1094           args_info->debug_orig = gengetopt_strdup (optarg);
1095           break;
1096
1097
1098         case 0: /* Long option with no short option */
1099         case '?':       /* Invalid option.  */
1100           /* `getopt_long' already printed an error message.  */
1101           goto failure;
1102
1103         default:        /* bug: option not considered.  */
1104           fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
1105           abort ();
1106         } /* switch */
1107     } /* while */
1108
1109
1110
1111   if (check_required)
1112     {
1113       error += cmdline_parser_required2 (args_info, argv[0], additional_error);
1114     }
1115
1116   cmdline_parser_release (&local_args_info);
1117
1118   if ( error )
1119     return (EXIT_FAILURE);
1120
1121   return 0;
1122
1123 failure:
1124   
1125   cmdline_parser_release (&local_args_info);
1126   return (EXIT_FAILURE);
1127 }