LibOFX
ofxdump/cmdline.c
1 /*
2  File autogenerated by gengetopt version 2.22.6
3  generated with the following command:
4  gengetopt --unamed-opts
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 #ifndef FIX_UNUSED
21 #define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */
22 #endif
23 
24 #include <getopt.h>
25 
26 #include "cmdline.h"
27 
28 const char *gengetopt_args_info_purpose = "ofxdump prints to stdout, in human readable form, everything the library \nunderstands about a particular file or response, and sends errors to\nstderr. To know exactly what the library understands about of a particular\nofx response file, just call ofxdump on that file.";
29 
30 const char *gengetopt_args_info_usage = "Usage: " CMDLINE_PARSER_PACKAGE " [OPTIONS]... [FILES]...";
31 
32 const char *gengetopt_args_info_versiontext = "";
33 
34 const char *gengetopt_args_info_description = "";
35 
36 const char *gengetopt_args_info_help[] = {
37  " -h, --help Print help and exit",
38  " -V, --version Print version and exit",
39  " -f, --import-format=STRING Force the file format of the file(s) specified\n (default=`AUTODETECT')",
40  " --list-import-formats List available import file formats\n 'import-format' command",
41  " --msg_parser Output file parsing messages (default=off)",
42  " --msg_debug Output messages meant for debugging\n (default=off)",
43  " --msg_warning Output warning messages about abnormal conditions\n and unknown constructs (default=on)",
44  " --msg_error Output error messages (default=on)",
45  " --msg_info Output informational messages about the progress\n of the library (default=on)",
46  " --msg_status Output status messages (default=on)",
47  0
48 };
49 
50 typedef enum {ARG_NO
51  , ARG_FLAG
52  , ARG_STRING
53 } cmdline_parser_arg_type;
54 
55 static
56 void clear_given (struct gengetopt_args_info *args_info);
57 static
58 void clear_args (struct gengetopt_args_info *args_info);
59 
60 static int
61 cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
62  struct cmdline_parser_params *params, const char *additional_error);
63 
64 
65 static char *
66 gengetopt_strdup (const char *s);
67 
68 static
69 void clear_given (struct gengetopt_args_info *args_info)
70 {
71  args_info->help_given = 0 ;
72  args_info->version_given = 0 ;
73  args_info->import_format_given = 0 ;
74  args_info->list_import_formats_given = 0 ;
75  args_info->msg_parser_given = 0 ;
76  args_info->msg_debug_given = 0 ;
77  args_info->msg_warning_given = 0 ;
78  args_info->msg_error_given = 0 ;
79  args_info->msg_info_given = 0 ;
80  args_info->msg_status_given = 0 ;
81 }
82 
83 static
84 void clear_args (struct gengetopt_args_info *args_info)
85 {
86  FIX_UNUSED (args_info);
87  args_info->import_format_arg = gengetopt_strdup ("AUTODETECT");
88  args_info->import_format_orig = NULL;
89  args_info->msg_parser_flag = 0;
90  args_info->msg_debug_flag = 0;
91  args_info->msg_warning_flag = 1;
92  args_info->msg_error_flag = 1;
93  args_info->msg_info_flag = 1;
94  args_info->msg_status_flag = 1;
95 
96 }
97 
98 static
99 void init_args_info(struct gengetopt_args_info *args_info)
100 {
101 
102 
103  args_info->help_help = gengetopt_args_info_help[0] ;
104  args_info->version_help = gengetopt_args_info_help[1] ;
105  args_info->import_format_help = gengetopt_args_info_help[2] ;
106  args_info->list_import_formats_help = gengetopt_args_info_help[3] ;
107  args_info->msg_parser_help = gengetopt_args_info_help[4] ;
108  args_info->msg_debug_help = gengetopt_args_info_help[5] ;
109  args_info->msg_warning_help = gengetopt_args_info_help[6] ;
110  args_info->msg_error_help = gengetopt_args_info_help[7] ;
111  args_info->msg_info_help = gengetopt_args_info_help[8] ;
112  args_info->msg_status_help = gengetopt_args_info_help[9] ;
113 
114 }
115 
116 void
117 cmdline_parser_print_version (void)
118 {
119  printf ("%s %s\n",
120  (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
121  CMDLINE_PARSER_VERSION);
122 
123  if (strlen(gengetopt_args_info_versiontext) > 0)
124  printf("\n%s\n", gengetopt_args_info_versiontext);
125 }
126 
127 static void print_help_common(void) {
128  cmdline_parser_print_version ();
129 
130  if (strlen(gengetopt_args_info_purpose) > 0)
131  printf("\n%s\n", gengetopt_args_info_purpose);
132 
133  if (strlen(gengetopt_args_info_usage) > 0)
134  printf("\n%s\n", gengetopt_args_info_usage);
135 
136  printf("\n");
137 
138  if (strlen(gengetopt_args_info_description) > 0)
139  printf("%s\n\n", gengetopt_args_info_description);
140 }
141 
142 void
143 cmdline_parser_print_help (void)
144 {
145  int i = 0;
146  print_help_common();
147  while (gengetopt_args_info_help[i])
148  printf("%s\n", gengetopt_args_info_help[i++]);
149 }
150 
151 void
152 cmdline_parser_init (struct gengetopt_args_info *args_info)
153 {
154  clear_given (args_info);
155  clear_args (args_info);
156  init_args_info (args_info);
157 
158  args_info->inputs = 0;
159  args_info->inputs_num = 0;
160 }
161 
162 void
163 cmdline_parser_params_init(struct cmdline_parser_params *params)
164 {
165  if (params)
166  {
167  params->override = 0;
168  params->initialize = 1;
169  params->check_required = 1;
170  params->check_ambiguity = 0;
171  params->print_errors = 1;
172  }
173 }
174 
175 struct cmdline_parser_params *
176 cmdline_parser_params_create(void)
177 {
178  struct cmdline_parser_params *params =
179  (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
180  cmdline_parser_params_init(params);
181  return params;
182 }
183 
184 static void
185 free_string_field (char **s)
186 {
187  if (*s)
188  {
189  free (*s);
190  *s = 0;
191  }
192 }
193 
194 
195 static void
196 cmdline_parser_release (struct gengetopt_args_info *args_info)
197 {
198  unsigned int i;
199  free_string_field (&(args_info->import_format_arg));
200  free_string_field (&(args_info->import_format_orig));
201 
202 
203  for (i = 0; i < args_info->inputs_num; ++i)
204  free (args_info->inputs [i]);
205 
206  if (args_info->inputs_num)
207  free (args_info->inputs);
208 
209  clear_given (args_info);
210 }
211 
212 
213 static void
214 write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
215 {
216  FIX_UNUSED (values);
217  if (arg) {
218  fprintf(outfile, "%s=\"%s\"\n", opt, arg);
219  } else {
220  fprintf(outfile, "%s\n", opt);
221  }
222 }
223 
224 
225 int
226 cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
227 {
228  int i = 0;
229 
230  if (!outfile)
231  {
232  fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
233  return EXIT_FAILURE;
234  }
235 
236  if (args_info->help_given)
237  write_into_file(outfile, "help", 0, 0 );
238  if (args_info->version_given)
239  write_into_file(outfile, "version", 0, 0 );
240  if (args_info->import_format_given)
241  write_into_file(outfile, "import-format", args_info->import_format_orig, 0);
242  if (args_info->list_import_formats_given)
243  write_into_file(outfile, "list-import-formats", 0, 0 );
244  if (args_info->msg_parser_given)
245  write_into_file(outfile, "msg_parser", 0, 0 );
246  if (args_info->msg_debug_given)
247  write_into_file(outfile, "msg_debug", 0, 0 );
248  if (args_info->msg_warning_given)
249  write_into_file(outfile, "msg_warning", 0, 0 );
250  if (args_info->msg_error_given)
251  write_into_file(outfile, "msg_error", 0, 0 );
252  if (args_info->msg_info_given)
253  write_into_file(outfile, "msg_info", 0, 0 );
254  if (args_info->msg_status_given)
255  write_into_file(outfile, "msg_status", 0, 0 );
256 
257 
258  i = EXIT_SUCCESS;
259  return i;
260 }
261 
262 int
263 cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
264 {
265  FILE *outfile;
266  int i = 0;
267 
268  outfile = fopen(filename, "w");
269 
270  if (!outfile)
271  {
272  fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
273  return EXIT_FAILURE;
274  }
275 
276  i = cmdline_parser_dump(outfile, args_info);
277  fclose (outfile);
278 
279  return i;
280 }
281 
282 void
283 cmdline_parser_free (struct gengetopt_args_info *args_info)
284 {
285  cmdline_parser_release (args_info);
286 }
287 
289 char *
290 gengetopt_strdup (const char *s)
291 {
292  char *result = 0;
293  if (!s)
294  return result;
295 
296  result = (char*)malloc(strlen(s) + 1);
297  if (result == (char*)0)
298  return (char*)0;
299  strcpy(result, s);
300  return result;
301 }
302 
303 int
304 cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
305 {
306  return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
307 }
308 
309 int
310 cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
311  struct cmdline_parser_params *params)
312 {
313  int result;
314  result = cmdline_parser_internal (argc, argv, args_info, params, 0);
315 
316  if (result == EXIT_FAILURE)
317  {
318  cmdline_parser_free (args_info);
319  exit (EXIT_FAILURE);
320  }
321 
322  return result;
323 }
324 
325 int
326 cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
327 {
328  int result;
329  struct cmdline_parser_params params;
330 
331  params.override = override;
332  params.initialize = initialize;
334  params.check_ambiguity = 0;
335  params.print_errors = 1;
336 
337  result = cmdline_parser_internal (argc, argv, args_info, &params, 0);
338 
339  if (result == EXIT_FAILURE)
340  {
341  cmdline_parser_free (args_info);
342  exit (EXIT_FAILURE);
343  }
344 
345  return result;
346 }
347 
348 int
349 cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
350 {
351  FIX_UNUSED (args_info);
352  FIX_UNUSED (prog_name);
353  return EXIT_SUCCESS;
354 }
355 
356 
357 static char *package_name = 0;
358 
377 static
378 int update_arg(void *field, char **orig_field,
379  unsigned int *field_given, unsigned int *prev_given,
380  char *value, const char *possible_values[],
381  const char *default_value,
382  cmdline_parser_arg_type arg_type,
383  int check_ambiguity, int override,
384  int no_free, int multiple_option,
385  const char *long_opt, char short_opt,
386  const char *additional_error)
387 {
388  char *stop_char = 0;
389  const char *val = value;
390  int found;
391  char **string_field;
392  FIX_UNUSED (field);
393 
394  stop_char = 0;
395  found = 0;
396 
397  if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
398  {
399  if (short_opt != '-')
400  fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
401  package_name, long_opt, short_opt,
402  (additional_error ? additional_error : ""));
403  else
404  fprintf (stderr, "%s: `--%s' option given more than once%s\n",
405  package_name, long_opt,
406  (additional_error ? additional_error : ""));
407  return 1; /* failure */
408  }
409 
410  FIX_UNUSED (default_value);
411 
412  if (field_given && *field_given && ! override)
413  return 0;
414  if (prev_given)
415  (*prev_given)++;
416  if (field_given)
417  (*field_given)++;
418  if (possible_values)
419  val = possible_values[found];
420 
421  switch(arg_type) {
422  case ARG_FLAG:
423  *((int *)field) = !*((int *)field);
424  break;
425  case ARG_STRING:
426  if (val) {
427  string_field = (char **)field;
428  if (!no_free && *string_field)
429  free (*string_field); /* free previous string */
430  *string_field = gengetopt_strdup (val);
431  }
432  break;
433  default:
434  break;
435  };
436 
437 
438  /* store the original value */
439  switch(arg_type) {
440  case ARG_NO:
441  case ARG_FLAG:
442  break;
443  default:
444  if (value && orig_field) {
445  if (no_free) {
446  *orig_field = value;
447  } else {
448  if (*orig_field)
449  free (*orig_field); /* free previous string */
450  *orig_field = gengetopt_strdup (value);
451  }
452  }
453  };
454 
455  return 0; /* OK */
456 }
457 
458 
459 int
460 cmdline_parser_internal (
461  int argc, char **argv, struct gengetopt_args_info *args_info,
462  struct cmdline_parser_params *params, const char *additional_error)
463 {
464  int c; /* Character of the parsed option. */
465 
466  int error_occurred = 0;
467  struct gengetopt_args_info local_args_info;
468 
469  int override;
470  int initialize;
471  int check_required;
472  int check_ambiguity;
473 
474  package_name = argv[0];
475 
476  override = params->override;
477  initialize = params->initialize;
478  check_required = params->check_required;
479  check_ambiguity = params->check_ambiguity;
480 
481  if (initialize)
482  cmdline_parser_init (args_info);
483 
484  cmdline_parser_init (&local_args_info);
485 
486  optarg = 0;
487  optind = 0;
488  opterr = params->print_errors;
489  optopt = '?';
490 
491  while (1)
492  {
493  int option_index = 0;
494 
495  static struct option long_options[] = {
496  { "help", 0, NULL, 'h' },
497  { "version", 0, NULL, 'V' },
498  { "import-format", 1, NULL, 'f' },
499  { "list-import-formats", 0, NULL, 0 },
500  { "msg_parser", 0, NULL, 0 },
501  { "msg_debug", 0, NULL, 0 },
502  { "msg_warning", 0, NULL, 0 },
503  { "msg_error", 0, NULL, 0 },
504  { "msg_info", 0, NULL, 0 },
505  { "msg_status", 0, NULL, 0 },
506  { 0, 0, 0, 0 }
507  };
508 
509  c = getopt_long (argc, argv, "hVf:", long_options, &option_index);
510 
511  if (c == -1) break; /* Exit from `while (1)' loop. */
512 
513  switch (c)
514  {
515  case 'h': /* Print help and exit. */
516  cmdline_parser_print_help ();
517  cmdline_parser_free (&local_args_info);
518  exit (EXIT_SUCCESS);
519 
520  case 'V': /* Print version and exit. */
521  cmdline_parser_print_version ();
522  cmdline_parser_free (&local_args_info);
523  exit (EXIT_SUCCESS);
524 
525  case 'f': /* Force the file format of the file(s) specified. */
526 
527 
528  if (update_arg( (void *)&(args_info->import_format_arg),
529  &(args_info->import_format_orig), &(args_info->import_format_given),
530  &(local_args_info.import_format_given), optarg, 0, "AUTODETECT", ARG_STRING,
531  check_ambiguity, override, 0, 0,
532  "import-format", 'f',
533  additional_error))
534  goto failure;
535 
536  break;
537 
538  case 0: /* Long option with no short option */
539  /* List available import file formats 'import-format' command. */
540  if (strcmp (long_options[option_index].name, "list-import-formats") == 0)
541  {
542 
543 
544  if (update_arg( 0 ,
545  0 , &(args_info->list_import_formats_given),
546  &(local_args_info.list_import_formats_given), optarg, 0, 0, ARG_NO,
547  check_ambiguity, override, 0, 0,
548  "list-import-formats", '-',
549  additional_error))
550  goto failure;
551 
552  }
553  /* Output file parsing messages. */
554  else if (strcmp (long_options[option_index].name, "msg_parser") == 0)
555  {
556 
557 
558  if (update_arg((void *)&(args_info->msg_parser_flag), 0, &(args_info->msg_parser_given),
559  &(local_args_info.msg_parser_given), optarg, 0, 0, ARG_FLAG,
560  check_ambiguity, override, 1, 0, "msg_parser", '-',
561  additional_error))
562  goto failure;
563 
564  }
565  /* Output messages meant for debugging. */
566  else if (strcmp (long_options[option_index].name, "msg_debug") == 0)
567  {
568 
569 
570  if (update_arg((void *)&(args_info->msg_debug_flag), 0, &(args_info->msg_debug_given),
571  &(local_args_info.msg_debug_given), optarg, 0, 0, ARG_FLAG,
572  check_ambiguity, override, 1, 0, "msg_debug", '-',
573  additional_error))
574  goto failure;
575 
576  }
577  /* Output warning messages about abnormal conditions and unknown constructs. */
578  else if (strcmp (long_options[option_index].name, "msg_warning") == 0)
579  {
580 
581 
582  if (update_arg((void *)&(args_info->msg_warning_flag), 0, &(args_info->msg_warning_given),
583  &(local_args_info.msg_warning_given), optarg, 0, 0, ARG_FLAG,
584  check_ambiguity, override, 1, 0, "msg_warning", '-',
585  additional_error))
586  goto failure;
587 
588  }
589  /* Output error messages. */
590  else if (strcmp (long_options[option_index].name, "msg_error") == 0)
591  {
592 
593 
594  if (update_arg((void *)&(args_info->msg_error_flag), 0, &(args_info->msg_error_given),
595  &(local_args_info.msg_error_given), optarg, 0, 0, ARG_FLAG,
596  check_ambiguity, override, 1, 0, "msg_error", '-',
597  additional_error))
598  goto failure;
599 
600  }
601  /* Output informational messages about the progress of the library. */
602  else if (strcmp (long_options[option_index].name, "msg_info") == 0)
603  {
604 
605 
606  if (update_arg((void *)&(args_info->msg_info_flag), 0, &(args_info->msg_info_given),
607  &(local_args_info.msg_info_given), optarg, 0, 0, ARG_FLAG,
608  check_ambiguity, override, 1, 0, "msg_info", '-',
609  additional_error))
610  goto failure;
611 
612  }
613  /* Output status messages. */
614  else if (strcmp (long_options[option_index].name, "msg_status") == 0)
615  {
616 
617 
618  if (update_arg((void *)&(args_info->msg_status_flag), 0, &(args_info->msg_status_given),
619  &(local_args_info.msg_status_given), optarg, 0, 0, ARG_FLAG,
620  check_ambiguity, override, 1, 0, "msg_status", '-',
621  additional_error))
622  goto failure;
623 
624  }
625 
626  break;
627  case '?': /* Invalid option. */
628  /* `getopt_long' already printed an error message. */
629  goto failure;
630 
631  default: /* bug: option not considered. */
632  fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
633  abort ();
634  } /* switch */
635  } /* while */
636 
637 
638 
639 
640  cmdline_parser_release (&local_args_info);
641 
642  if ( error_occurred )
643  return (EXIT_FAILURE);
644 
645  if (optind < argc)
646  {
647  int i = 0 ;
648  int found_prog_name = 0;
649  /* whether program name, i.e., argv[0], is in the remaining args
650  (this may happen with some implementations of getopt,
651  but surely not with the one included by gengetopt) */
652 
653  i = optind;
654  while (i < argc)
655  if (argv[i++] == argv[0]) {
656  found_prog_name = 1;
657  break;
658  }
659  i = 0;
660 
661  args_info->inputs_num = argc - optind - found_prog_name;
662  args_info->inputs =
663  (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
664  while (optind < argc)
665  if (argv[optind++] != argv[0])
666  args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ;
667  }
668 
669  return 0;
670 
671 failure:
672 
673  cmdline_parser_release (&local_args_info);
674  return (EXIT_FAILURE);
675 }
int override
whether to override possibly already present options (default 0)
int msg_info_flag
Output informational messages about the progress of the library (default=on).
int initialize
whether to initialize the option structure gengetopt_args_info (default 1)
const char * msg_status_help
Output status messages help description.
int msg_warning_flag
Output warning messages about abnormal conditions and unknown constructs (default=on).
char * import_format_arg
Force the file format of the file(s) specified (default='AUTODETECT').
unsigned int msg_warning_given
Whether msg_warning was given.
Where the command line options are stored.
unsigned int msg_error_given
Whether msg_error was given.
unsigned int help_given
Whether help was given.
unsigned int msg_debug_given
Whether msg_debug was given.
unsigned int msg_status_given
Whether msg_status was given.
unsigned int msg_parser_given
Whether msg_parser was given.
int msg_debug_flag
Output messages meant for debugging (default=off).
unsigned int list_import_formats_given
Whether list-import-formats was given.
const char * msg_parser_help
Output file parsing messages help description.
The additional parameters to pass to parser functions.
const char * list_import_formats_help
List available import file formats 'import-format' command help description.
unsigned int import_format_given
Whether import-format was given.
const char * version_help
Print version and exit help description.
char * import_format_orig
Force the file format of the file(s) specified original value given at command line.
const char * msg_error_help
Output error messages help description.
const char * import_format_help
Force the file format of the file(s) specified help description.
const char * help_help
Print help and exit help description.
int msg_error_flag
Output error messages (default=on).
const char * msg_debug_help
Output messages meant for debugging help description.
unsigned inputs_num
unamed options number
int msg_status_flag
Output status messages (default=on).
int msg_parser_flag
Output file parsing messages (default=off).
int print_errors
whether getopt_long should print an error message for a bad option (default 1)
int check_ambiguity
whether to check for options already specified in the option structure gengetopt_args_info (default 0...
int check_required
whether to check that all required options were provided (default 1)
const char * msg_info_help
Output informational messages about the progress of the library help description. ...
unsigned int msg_info_given
Whether msg_info was given.
char ** inputs
unamed options (options without names)
const char * msg_warning_help
Output warning messages about abnormal conditions and unknown constructs help description.
unsigned int version_given
Whether version was given.