]> 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.22
3   generated with the following command:
4   gengetopt --file-name=/home/guigues/coding/CreaTools/creaImageIO/appli/gimmick/gimmick_ggo -u --conf-parser 
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 #include "getopt.h"
21
22 #include "/home/guigues/coding/CreaTools/creaImageIO/appli/gimmick/gimmick_ggo.h"
23
24 const char *gengetopt_args_info_purpose = "";
25
26 const char *gengetopt_args_info_usage = "Usage: gimmick COMMAND [OPTIONS]";
27
28 const char *gengetopt_args_info_description = "";
29
30 const char *gengetopt_args_info_help[] = {
31   "  -h, --help              Print help and exit",
32   "  -V, --version           Print version and exit",
33   "\nCOMMANDS:",
34   "  -p, --print             Prints the local database tree  (default=off)",
35   "  -f, --file=<file name>  Adds the file to local database",
36   "  -d, --dir=<path>        Adds the directory to local database",
37   "  -r, --recurse           Recurse into sub-directories  (default=off)",
38   "\nOPTIONS:",
39   "  -v, --verbose=INT       Verbosity level  (default=`1')",
40   "  -D, --debug=INT         Debug messages level  (default=`0')",
41     0
42 };
43
44 typedef enum {ARG_NO
45   , ARG_FLAG
46   , ARG_STRING
47   , ARG_INT
48 } cmdline_parser_arg_type;
49
50 static
51 void clear_given (struct gengetopt_args_info *args_info);
52 static
53 void clear_args (struct gengetopt_args_info *args_info);
54
55 static int
56 cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info,
57                         struct cmdline_parser_params *params, const char *additional_error);
58
59 static int
60 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error);
61 struct line_list
62 {
63   char * string_arg;
64   struct line_list * next;
65 };
66
67 static struct line_list *cmd_line_list = 0;
68 static struct line_list *cmd_line_list_tmp = 0;
69
70 static void
71 free_cmd_list(void)
72 {
73   /* free the list of a previous call */
74   if (cmd_line_list)
75     {
76       while (cmd_line_list) {
77         cmd_line_list_tmp = cmd_line_list;
78         cmd_line_list = cmd_line_list->next;
79         free (cmd_line_list_tmp->string_arg);
80         free (cmd_line_list_tmp);
81       }
82     }
83 }
84
85
86 static char *
87 gengetopt_strdup (const char *s);
88
89 static
90 void clear_given (struct gengetopt_args_info *args_info)
91 {
92   args_info->help_given = 0 ;
93   args_info->version_given = 0 ;
94   args_info->print_given = 0 ;
95   args_info->file_given = 0 ;
96   args_info->dir_given = 0 ;
97   args_info->recurse_given = 0 ;
98   args_info->verbose_given = 0 ;
99   args_info->debug_given = 0 ;
100 }
101
102 static
103 void clear_args (struct gengetopt_args_info *args_info)
104 {
105   args_info->print_flag = 0;
106   args_info->file_arg = NULL;
107   args_info->file_orig = NULL;
108   args_info->dir_arg = NULL;
109   args_info->dir_orig = NULL;
110   args_info->recurse_flag = 0;
111   args_info->verbose_arg = 1;
112   args_info->verbose_orig = NULL;
113   args_info->debug_arg = 0;
114   args_info->debug_orig = NULL;
115   
116 }
117
118 static
119 void init_args_info(struct gengetopt_args_info *args_info)
120 {
121
122
123   args_info->help_help = gengetopt_args_info_help[0] ;
124   args_info->version_help = gengetopt_args_info_help[1] ;
125   args_info->print_help = gengetopt_args_info_help[3] ;
126   args_info->file_help = gengetopt_args_info_help[4] ;
127   args_info->dir_help = gengetopt_args_info_help[5] ;
128   args_info->recurse_help = gengetopt_args_info_help[6] ;
129   args_info->verbose_help = gengetopt_args_info_help[8] ;
130   args_info->debug_help = gengetopt_args_info_help[9] ;
131   
132 }
133
134 void
135 cmdline_parser_print_version (void)
136 {
137   printf ("%s %s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
138 }
139
140 static void print_help_common(void) {
141   cmdline_parser_print_version ();
142
143   if (strlen(gengetopt_args_info_purpose) > 0)
144     printf("\n%s\n", gengetopt_args_info_purpose);
145
146   if (strlen(gengetopt_args_info_usage) > 0)
147     printf("\n%s\n", gengetopt_args_info_usage);
148
149   printf("\n");
150
151   if (strlen(gengetopt_args_info_description) > 0)
152     printf("%s\n", gengetopt_args_info_description);
153 }
154
155 void
156 cmdline_parser_print_help (void)
157 {
158   int i = 0;
159   print_help_common();
160   while (gengetopt_args_info_help[i])
161     printf("%s\n", gengetopt_args_info_help[i++]);
162 }
163
164 void
165 cmdline_parser_init (struct gengetopt_args_info *args_info)
166 {
167   clear_given (args_info);
168   clear_args (args_info);
169   init_args_info (args_info);
170
171   args_info->inputs = NULL;
172   args_info->inputs_num = 0;
173 }
174
175 void
176 cmdline_parser_params_init(struct cmdline_parser_params *params)
177 {
178   if (params)
179     { 
180       params->override = 0;
181       params->initialize = 1;
182       params->check_required = 1;
183       params->check_ambiguity = 0;
184       params->print_errors = 1;
185     }
186 }
187
188 struct cmdline_parser_params *
189 cmdline_parser_params_create(void)
190 {
191   struct cmdline_parser_params *params = 
192     (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
193   cmdline_parser_params_init(params);  
194   return params;
195 }
196
197 static void
198 free_string_field (char **s)
199 {
200   if (*s)
201     {
202       free (*s);
203       *s = 0;
204     }
205 }
206
207
208 static void
209 cmdline_parser_release (struct gengetopt_args_info *args_info)
210 {
211   unsigned int i;
212   free_string_field (&(args_info->file_arg));
213   free_string_field (&(args_info->file_orig));
214   free_string_field (&(args_info->dir_arg));
215   free_string_field (&(args_info->dir_orig));
216   free_string_field (&(args_info->verbose_orig));
217   free_string_field (&(args_info->debug_orig));
218   
219   
220   for (i = 0; i < args_info->inputs_num; ++i)
221     free (args_info->inputs [i]);
222
223   if (args_info->inputs_num)
224     free (args_info->inputs);
225
226   clear_given (args_info);
227 }
228
229
230 static void
231 write_into_file(FILE *outfile, const char *opt, const char *arg, char *values[])
232 {
233   if (arg) {
234     fprintf(outfile, "%s=\"%s\"\n", opt, arg);
235   } else {
236     fprintf(outfile, "%s\n", opt);
237   }
238 }
239
240
241 int
242 cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
243 {
244   int i = 0;
245
246   if (!outfile)
247     {
248       fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
249       return EXIT_FAILURE;
250     }
251
252   if (args_info->help_given)
253     write_into_file(outfile, "help", 0, 0 );
254   if (args_info->version_given)
255     write_into_file(outfile, "version", 0, 0 );
256   if (args_info->print_given)
257     write_into_file(outfile, "print", 0, 0 );
258   if (args_info->file_given)
259     write_into_file(outfile, "file", args_info->file_orig, 0);
260   if (args_info->dir_given)
261     write_into_file(outfile, "dir", args_info->dir_orig, 0);
262   if (args_info->recurse_given)
263     write_into_file(outfile, "recurse", 0, 0 );
264   if (args_info->verbose_given)
265     write_into_file(outfile, "verbose", args_info->verbose_orig, 0);
266   if (args_info->debug_given)
267     write_into_file(outfile, "debug", args_info->debug_orig, 0);
268   
269
270   i = EXIT_SUCCESS;
271   return i;
272 }
273
274 int
275 cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
276 {
277   FILE *outfile;
278   int i = 0;
279
280   outfile = fopen(filename, "w");
281
282   if (!outfile)
283     {
284       fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
285       return EXIT_FAILURE;
286     }
287
288   i = cmdline_parser_dump(outfile, args_info);
289   fclose (outfile);
290
291   return i;
292 }
293
294 void
295 cmdline_parser_free (struct gengetopt_args_info *args_info)
296 {
297   cmdline_parser_release (args_info);
298 }
299
300 /** @brief replacement of strdup, which is not standard */
301 char *
302 gengetopt_strdup (const char *s)
303 {
304   char *result = NULL;
305   if (!s)
306     return result;
307
308   result = (char*)malloc(strlen(s) + 1);
309   if (result == (char*)0)
310     return (char*)0;
311   strcpy(result, s);
312   return result;
313 }
314
315 int
316 cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info)
317 {
318   return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
319 }
320
321 int
322 cmdline_parser_ext (int argc, char * const *argv, struct gengetopt_args_info *args_info,
323                    struct cmdline_parser_params *params)
324 {
325   int result;
326   result = cmdline_parser_internal (argc, argv, args_info, params, NULL);
327
328   if (result == EXIT_FAILURE)
329     {
330       cmdline_parser_free (args_info);
331       exit (EXIT_FAILURE);
332     }
333   
334   return result;
335 }
336
337 int
338 cmdline_parser2 (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
339 {
340   int result;
341   struct cmdline_parser_params params;
342   
343   params.override = override;
344   params.initialize = initialize;
345   params.check_required = check_required;
346   params.check_ambiguity = 0;
347   params.print_errors = 1;
348
349   result = cmdline_parser_internal (argc, argv, args_info, &params, NULL);
350
351   if (result == EXIT_FAILURE)
352     {
353       cmdline_parser_free (args_info);
354       exit (EXIT_FAILURE);
355     }
356   
357   return result;
358 }
359
360 int
361 cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
362 {
363   int result = EXIT_SUCCESS;
364
365   if (cmdline_parser_required2(args_info, prog_name, NULL) > 0)
366     result = EXIT_FAILURE;
367
368   if (result == EXIT_FAILURE)
369     {
370       cmdline_parser_free (args_info);
371       exit (EXIT_FAILURE);
372     }
373   
374   return result;
375 }
376
377 int
378 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error)
379 {
380   int error = 0;
381
382   /* checks for required options */
383   
384   /* checks for dependences among options */
385   if (args_info->recurse_given && ! args_info->dir_given)
386     {
387       fprintf (stderr, "%s: '--recurse' ('-r') option depends on option 'dir'%s\n", prog_name, (additional_error ? additional_error : ""));
388       error = 1;
389     }
390
391   return error;
392 }
393
394
395 static char *package_name = 0;
396
397 /**
398  * @brief updates an option
399  * @param field the generic pointer to the field to update
400  * @param orig_field the pointer to the orig field
401  * @param field_given the pointer to the number of occurrence of this option
402  * @param prev_given the pointer to the number of occurrence already seen
403  * @param value the argument for this option (if null no arg was specified)
404  * @param possible_values the possible values for this option (if specified)
405  * @param default_value the default value (in case the option only accepts fixed values)
406  * @param arg_type the type of this option
407  * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
408  * @param override @see cmdline_parser_params.override
409  * @param no_free whether to free a possible previous value
410  * @param multiple_option whether this is a multiple option
411  * @param long_opt the corresponding long option
412  * @param short_opt the corresponding short option (or '-' if none)
413  * @param additional_error possible further error specification
414  */
415 static
416 int update_arg(void *field, char **orig_field,
417                unsigned int *field_given, unsigned int *prev_given, 
418                char *value, char *possible_values[], const char *default_value,
419                cmdline_parser_arg_type arg_type,
420                int check_ambiguity, int override,
421                int no_free, int multiple_option,
422                const char *long_opt, char short_opt,
423                const char *additional_error)
424 {
425   char *stop_char = 0;
426   const char *val = value;
427   int found;
428   char **string_field;
429
430   stop_char = 0;
431   found = 0;
432
433   if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
434     {
435       if (short_opt != '-')
436         fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n", 
437                package_name, long_opt, short_opt,
438                (additional_error ? additional_error : ""));
439       else
440         fprintf (stderr, "%s: `--%s' option given more than once%s\n", 
441                package_name, long_opt,
442                (additional_error ? additional_error : ""));
443       return 1; /* failure */
444     }
445
446     
447   if (field_given && *field_given && ! override)
448     return 0;
449   if (prev_given)
450     (*prev_given)++;
451   if (field_given)
452     (*field_given)++;
453   if (possible_values)
454     val = possible_values[found];
455
456   switch(arg_type) {
457   case ARG_FLAG:
458     *((int *)field) = !*((int *)field);
459     break;
460   case ARG_INT:
461     if (val) *((int *)field) = strtol (val, &stop_char, 0);
462     break;
463   case ARG_STRING:
464     if (val) {
465       string_field = (char **)field;
466       if (!no_free && *string_field)
467         free (*string_field); /* free previous string */
468       *string_field = gengetopt_strdup (val);
469     }
470     break;
471   default:
472     break;
473   };
474
475   /* check numeric conversion */
476   switch(arg_type) {
477   case ARG_INT:
478     if (val && !(stop_char && *stop_char == '\0')) {
479       fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
480       return 1; /* failure */
481     }
482     break;
483   default:
484     ;
485   };
486
487   /* store the original value */
488   switch(arg_type) {
489   case ARG_NO:
490   case ARG_FLAG:
491     break;
492   default:
493     if (value && orig_field) {
494       if (no_free) {
495         *orig_field = value;
496       } else {
497         if (*orig_field)
498           free (*orig_field); /* free previous string */
499         *orig_field = gengetopt_strdup (value);
500       }
501     }
502   };
503
504   return 0; /* OK */
505 }
506
507
508 int
509 cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info,
510                         struct cmdline_parser_params *params, const char *additional_error)
511 {
512   int c;        /* Character of the parsed option.  */
513
514   int error = 0;
515   struct gengetopt_args_info local_args_info;
516   
517   int override;
518   int initialize;
519   int check_required;
520   int check_ambiguity;
521   
522   package_name = argv[0];
523   
524   override = params->override;
525   initialize = params->initialize;
526   check_required = params->check_required;
527   check_ambiguity = params->check_ambiguity;
528
529   if (initialize)
530     cmdline_parser_init (args_info);
531
532   cmdline_parser_init (&local_args_info);
533
534   optarg = 0;
535   optind = 0;
536   opterr = params->print_errors;
537   optopt = '?';
538
539   while (1)
540     {
541       int option_index = 0;
542
543       static struct option long_options[] = {
544         { "help",       0, NULL, 'h' },
545         { "version",    0, NULL, 'V' },
546         { "print",      0, NULL, 'p' },
547         { "file",       1, NULL, 'f' },
548         { "dir",        1, NULL, 'd' },
549         { "recurse",    0, NULL, 'r' },
550         { "verbose",    1, NULL, 'v' },
551         { "debug",      1, NULL, 'D' },
552         { NULL, 0, NULL, 0 }
553       };
554
555       c = getopt_long (argc, argv, "hVpf:d:rv:D:", long_options, &option_index);
556
557       if (c == -1) break;       /* Exit from `while (1)' loop.  */
558
559       switch (c)
560         {
561         case 'h':       /* Print help and exit.  */
562           cmdline_parser_print_help ();
563           cmdline_parser_free (&local_args_info);
564           exit (EXIT_SUCCESS);
565
566         case 'V':       /* Print version and exit.  */
567           cmdline_parser_print_version ();
568           cmdline_parser_free (&local_args_info);
569           exit (EXIT_SUCCESS);
570
571         case 'p':       /* Prints the local database tree.  */
572         
573         
574           if (update_arg((void *)&(args_info->print_flag), 0, &(args_info->print_given),
575               &(local_args_info.print_given), optarg, 0, 0, ARG_FLAG,
576               check_ambiguity, override, 1, 0, "print", 'p',
577               additional_error))
578             goto failure;
579         
580           break;
581         case 'f':       /* Adds the file to local database.  */
582         
583         
584           if (update_arg( (void *)&(args_info->file_arg), 
585                &(args_info->file_orig), &(args_info->file_given),
586               &(local_args_info.file_given), optarg, 0, 0, ARG_STRING,
587               check_ambiguity, override, 0, 0,
588               "file", 'f',
589               additional_error))
590             goto failure;
591         
592           break;
593         case 'd':       /* Adds the directory to local database.  */
594         
595         
596           if (update_arg( (void *)&(args_info->dir_arg), 
597                &(args_info->dir_orig), &(args_info->dir_given),
598               &(local_args_info.dir_given), optarg, 0, 0, ARG_STRING,
599               check_ambiguity, override, 0, 0,
600               "dir", 'd',
601               additional_error))
602             goto failure;
603         
604           break;
605         case 'r':       /* Recurse into sub-directories.  */
606         
607         
608           if (update_arg((void *)&(args_info->recurse_flag), 0, &(args_info->recurse_given),
609               &(local_args_info.recurse_given), optarg, 0, 0, ARG_FLAG,
610               check_ambiguity, override, 1, 0, "recurse", 'r',
611               additional_error))
612             goto failure;
613         
614           break;
615         case 'v':       /* Verbosity level.  */
616         
617         
618           if (update_arg( (void *)&(args_info->verbose_arg), 
619                &(args_info->verbose_orig), &(args_info->verbose_given),
620               &(local_args_info.verbose_given), optarg, 0, "1", ARG_INT,
621               check_ambiguity, override, 0, 0,
622               "verbose", 'v',
623               additional_error))
624             goto failure;
625         
626           break;
627         case 'D':       /* Debug messages level.  */
628         
629         
630           if (update_arg( (void *)&(args_info->debug_arg), 
631                &(args_info->debug_orig), &(args_info->debug_given),
632               &(local_args_info.debug_given), optarg, 0, "0", ARG_INT,
633               check_ambiguity, override, 0, 0,
634               "debug", 'D',
635               additional_error))
636             goto failure;
637         
638           break;
639
640         case 0: /* Long option with no short option */
641         case '?':       /* Invalid option.  */
642           /* `getopt_long' already printed an error message.  */
643           goto failure;
644
645         default:        /* bug: option not considered.  */
646           fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
647           abort ();
648         } /* switch */
649     } /* while */
650
651
652
653   if (check_required)
654     {
655       error += cmdline_parser_required2 (args_info, argv[0], additional_error);
656     }
657
658   cmdline_parser_release (&local_args_info);
659
660   if ( error )
661     return (EXIT_FAILURE);
662
663   if (optind < argc)
664     {
665       int i = 0 ;
666       int found_prog_name = 0;
667       /* whether program name, i.e., argv[0], is in the remaining args
668          (this may happen with some implementations of getopt,
669           but surely not with the one included by gengetopt) */
670
671       i = optind;
672       while (i < argc)
673         if (argv[i++] == argv[0]) {
674           found_prog_name = 1;
675           break;
676         }
677       i = 0;
678
679       args_info->inputs_num = argc - optind - found_prog_name;
680       args_info->inputs =
681         (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
682       while (optind < argc)
683         if (argv[optind++] != argv[0])
684           args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ;
685     }
686
687   return 0;
688
689 failure:
690   
691   cmdline_parser_release (&local_args_info);
692   return (EXIT_FAILURE);
693 }
694
695 #ifndef CONFIG_FILE_LINE_SIZE
696 #define CONFIG_FILE_LINE_SIZE 2048
697 #endif
698 #define ADDITIONAL_ERROR " in configuration file "
699
700 #define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)
701 /* 3 is for "--" and "=" */
702
703 static int
704 _cmdline_parser_configfile (char * const filename, int *my_argc)
705 {
706   FILE* file;
707   char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE+1];
708   char linebuf[CONFIG_FILE_LINE_SIZE];
709   int line_num = 0;
710   int result = 0, equal;
711   char *fopt, *farg;
712   char *str_index;
713   size_t len, next_token;
714   char delimiter;
715
716   if ((file = fopen(filename, "r")) == NULL)
717     {
718       fprintf (stderr, "%s: Error opening configuration file '%s'\n",
719                CMDLINE_PARSER_PACKAGE, filename);
720       return EXIT_FAILURE;
721     }
722
723   while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != NULL)
724     {
725       ++line_num;
726       my_argv[0] = '\0';
727       len = strlen(linebuf);
728       if (len > (CONFIG_FILE_LINE_BUFFER_SIZE-1))
729         {
730           fprintf (stderr, "%s:%s:%d: Line too long in configuration file\n",
731                    CMDLINE_PARSER_PACKAGE, filename, line_num);
732           result = EXIT_FAILURE;
733           break;
734         }
735
736       /* find first non-whitespace character in the line */
737       next_token = strspn (linebuf, " \t\r\n");
738       str_index  = linebuf + next_token;
739
740       if ( str_index[0] == '\0' || str_index[0] == '#')
741         continue; /* empty line or comment line is skipped */
742
743       fopt = str_index;
744
745       /* truncate fopt at the end of the first non-valid character */
746       next_token = strcspn (fopt, " \t\r\n=");
747
748       if (fopt[next_token] == '\0') /* the line is over */
749         {
750           farg  = NULL;
751           equal = 0;
752           goto noarg;
753         }
754
755       /* remember if equal sign is present */
756       equal = (fopt[next_token] == '=');
757       fopt[next_token++] = '\0';
758
759       /* advance pointers to the next token after the end of fopt */
760       next_token += strspn (fopt + next_token, " \t\r\n");
761
762       /* check for the presence of equal sign, and if so, skip it */
763       if ( !equal )
764         if ((equal = (fopt[next_token] == '=')))
765           {
766             next_token++;
767             next_token += strspn (fopt + next_token, " \t\r\n");
768           }
769       str_index  += next_token;
770
771       /* find argument */
772       farg = str_index;
773       if ( farg[0] == '\"' || farg[0] == '\'' )
774         { /* quoted argument */
775           str_index = strchr (++farg, str_index[0] ); /* skip opening quote */
776           if (! str_index)
777             {
778               fprintf
779                 (stderr,
780                  "%s:%s:%d: unterminated string in configuration file\n",
781                  CMDLINE_PARSER_PACKAGE, filename, line_num);
782               result = EXIT_FAILURE;
783               break;
784             }
785         }
786       else
787         { /* read up the remaining part up to a delimiter */
788           next_token = strcspn (farg, " \t\r\n#\'\"");
789           str_index += next_token;
790         }
791
792       /* truncate farg at the delimiter and store it for further check */
793       delimiter = *str_index, *str_index++ = '\0';
794
795       /* everything but comment is illegal at the end of line */
796       if (delimiter != '\0' && delimiter != '#')
797         {
798           str_index += strspn(str_index, " \t\r\n");
799           if (*str_index != '\0' && *str_index != '#')
800             {
801               fprintf
802                 (stderr,
803                  "%s:%s:%d: malformed string in configuration file\n",
804                  CMDLINE_PARSER_PACKAGE, filename, line_num);
805               result = EXIT_FAILURE;
806               break;
807             }
808         }
809
810     noarg:
811       if (!strcmp(fopt,"include")) {
812         if (farg && *farg) {
813           result = _cmdline_parser_configfile(farg, my_argc);
814         } else {
815           fprintf(stderr, "%s:%s:%d: include requires a filename argument.\n",
816                   CMDLINE_PARSER_PACKAGE, filename, line_num);
817         }
818         continue;
819       }
820       len = strlen(fopt);
821       strcat (my_argv, len > 1 ? "--" : "-");
822       strcat (my_argv, fopt);
823       if (len > 1 && ((farg && *farg) || equal))
824         strcat (my_argv, "=");
825       if (farg && *farg)
826         strcat (my_argv, farg);
827       ++(*my_argc);
828
829       cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
830       cmd_line_list_tmp->next = cmd_line_list;
831       cmd_line_list = cmd_line_list_tmp;
832       cmd_line_list->string_arg = gengetopt_strdup(my_argv);
833     } /* while */
834
835   if (file)
836     fclose(file);
837   return result;
838 }
839
840 int
841 cmdline_parser_configfile (char * const filename,
842                            struct gengetopt_args_info *args_info,
843                            int override, int initialize, int check_required)
844 {
845   struct cmdline_parser_params params;
846
847   params.override = override;
848   params.initialize = initialize;
849   params.check_required = check_required;
850   params.check_ambiguity = 0;
851   params.print_errors = 1;
852   
853   return cmdline_parser_config_file (filename, args_info, &params);
854 }
855
856 int
857 cmdline_parser_config_file (char * const filename,
858                            struct gengetopt_args_info *args_info,
859                            struct cmdline_parser_params *params)
860 {
861   int i, result;
862   int my_argc = 1;
863   char **my_argv_arg;
864   char *additional_error;
865
866   /* store the program name */
867   cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
868   cmd_line_list_tmp->next = cmd_line_list;
869   cmd_line_list = cmd_line_list_tmp;
870   cmd_line_list->string_arg = gengetopt_strdup (CMDLINE_PARSER_PACKAGE);
871
872   result = _cmdline_parser_configfile(filename, &my_argc);
873
874   if (result != EXIT_FAILURE) {
875     my_argv_arg = (char **) malloc((my_argc+1) * sizeof(char *));
876     cmd_line_list_tmp = cmd_line_list;
877
878     for (i = my_argc - 1; i >= 0; --i) {
879       my_argv_arg[i] = cmd_line_list_tmp->string_arg;
880       cmd_line_list_tmp = cmd_line_list_tmp->next;
881     }
882
883     my_argv_arg[my_argc] = 0;
884
885     additional_error = (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) + 1);
886     strcpy (additional_error, ADDITIONAL_ERROR);
887     strcat (additional_error, filename);
888     result =
889       cmdline_parser_internal (my_argc, my_argv_arg, args_info,
890                               params,
891                               additional_error);
892
893     free (additional_error);
894     free (my_argv_arg);
895   }
896
897   free_cmd_list();
898   if (result == EXIT_FAILURE)
899     {
900       cmdline_parser_free (args_info);
901       exit (EXIT_FAILURE);
902     }
903   
904   return result;
905 }