LibOFX

cmdline.c

00001 /*
00002   File autogenerated by gengetopt version 2.22.4
00003   generated with the following command:
00004   gengetopt --unamed-opts 
00005 
00006   The developers of gengetopt consider the fixed text that goes in all
00007   gengetopt output files to be in the public domain:
00008   we make no copyright claims on it.
00009 */
00010 
00011 /* If we use autoconf.  */
00012 #ifdef HAVE_CONFIG_H
00013 #include "config.h"
00014 #endif
00015 
00016 #include <stdio.h>
00017 #include <stdlib.h>
00018 #include <string.h>
00019 
00020 #ifndef FIX_UNUSED
00021 #define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */
00022 #endif
00023 
00024 #include <getopt.h>
00025 
00026 #include "cmdline.h"
00027 
00028 const char *gengetopt_args_info_purpose = "ofxdump prints to stdout, in human readable form, everything the library \n understands about a particular file or response, and sends errors to \n stderr.  To know exactly what the library understands about of a particular\n ofx response file, just call ofxdump on that file.";
00029 
00030 const char *gengetopt_args_info_usage = "Usage: " CMDLINE_PARSER_PACKAGE " [OPTIONS]... [FILES]...";
00031 
00032 const char *gengetopt_args_info_description = "";
00033 
00034 const char *gengetopt_args_info_help[] = {
00035   "  -h, --help                  Print help and exit",
00036   "  -V, --version               Print version and exit",
00037   "  -f, --import-format=STRING  Force the file format of the file(s) specified  \n                                (default=`AUTODETECT')",
00038   "      --list-import-formats   List available import file formats \n                                'import-format' command",
00039   "      --msg_parser            Output file parsing messages  (default=off)",
00040   "      --msg_debug             Output messages meant for debuging  (default=off)",
00041   "      --msg_warning           Output warning messages about abnormal conditions \n                                and unknown constructs  (default=on)",
00042   "      --msg_error             Output error messages  (default=on)",
00043   "      --msg_info              Output informational messages about the progress \n                                of the library  (default=on)",
00044   "      --msg_status            Output status messages  (default=on)",
00045     0
00046 };
00047 
00048 typedef enum {ARG_NO
00049   , ARG_FLAG
00050   , ARG_STRING
00051 } cmdline_parser_arg_type;
00052 
00053 static
00054 void clear_given (struct gengetopt_args_info *args_info);
00055 static
00056 void clear_args (struct gengetopt_args_info *args_info);
00057 
00058 static int
00059 cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
00060                         struct cmdline_parser_params *params, const char *additional_error);
00061 
00062 
00063 static char *
00064 gengetopt_strdup (const char *s);
00065 
00066 static
00067 void clear_given (struct gengetopt_args_info *args_info)
00068 {
00069   args_info->help_given = 0 ;
00070   args_info->version_given = 0 ;
00071   args_info->import_format_given = 0 ;
00072   args_info->list_import_formats_given = 0 ;
00073   args_info->msg_parser_given = 0 ;
00074   args_info->msg_debug_given = 0 ;
00075   args_info->msg_warning_given = 0 ;
00076   args_info->msg_error_given = 0 ;
00077   args_info->msg_info_given = 0 ;
00078   args_info->msg_status_given = 0 ;
00079 }
00080 
00081 static
00082 void clear_args (struct gengetopt_args_info *args_info)
00083 {
00084   FIX_UNUSED (args_info);
00085   args_info->import_format_arg = gengetopt_strdup ("AUTODETECT");
00086   args_info->import_format_orig = NULL;
00087   args_info->msg_parser_flag = 0;
00088   args_info->msg_debug_flag = 0;
00089   args_info->msg_warning_flag = 1;
00090   args_info->msg_error_flag = 1;
00091   args_info->msg_info_flag = 1;
00092   args_info->msg_status_flag = 1;
00093   
00094 }
00095 
00096 static
00097 void init_args_info(struct gengetopt_args_info *args_info)
00098 {
00099 
00100 
00101   args_info->help_help = gengetopt_args_info_help[0] ;
00102   args_info->version_help = gengetopt_args_info_help[1] ;
00103   args_info->import_format_help = gengetopt_args_info_help[2] ;
00104   args_info->list_import_formats_help = gengetopt_args_info_help[3] ;
00105   args_info->msg_parser_help = gengetopt_args_info_help[4] ;
00106   args_info->msg_debug_help = gengetopt_args_info_help[5] ;
00107   args_info->msg_warning_help = gengetopt_args_info_help[6] ;
00108   args_info->msg_error_help = gengetopt_args_info_help[7] ;
00109   args_info->msg_info_help = gengetopt_args_info_help[8] ;
00110   args_info->msg_status_help = gengetopt_args_info_help[9] ;
00111   
00112 }
00113 
00114 void
00115 cmdline_parser_print_version (void)
00116 {
00117   printf ("%s %s\n",
00118      (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
00119      CMDLINE_PARSER_VERSION);
00120 }
00121 
00122 static void print_help_common(void) {
00123   cmdline_parser_print_version ();
00124 
00125   if (strlen(gengetopt_args_info_purpose) > 0)
00126     printf("\n%s\n", gengetopt_args_info_purpose);
00127 
00128   if (strlen(gengetopt_args_info_usage) > 0)
00129     printf("\n%s\n", gengetopt_args_info_usage);
00130 
00131   printf("\n");
00132 
00133   if (strlen(gengetopt_args_info_description) > 0)
00134     printf("%s\n\n", gengetopt_args_info_description);
00135 }
00136 
00137 void
00138 cmdline_parser_print_help (void)
00139 {
00140   int i = 0;
00141   print_help_common();
00142   while (gengetopt_args_info_help[i])
00143     printf("%s\n", gengetopt_args_info_help[i++]);
00144 }
00145 
00146 void
00147 cmdline_parser_init (struct gengetopt_args_info *args_info)
00148 {
00149   clear_given (args_info);
00150   clear_args (args_info);
00151   init_args_info (args_info);
00152 
00153   args_info->inputs = 0;
00154   args_info->inputs_num = 0;
00155 }
00156 
00157 void
00158 cmdline_parser_params_init(struct cmdline_parser_params *params)
00159 {
00160   if (params)
00161     { 
00162       params->override = 0;
00163       params->initialize = 1;
00164       params->check_required = 1;
00165       params->check_ambiguity = 0;
00166       params->print_errors = 1;
00167     }
00168 }
00169 
00170 struct cmdline_parser_params *
00171 cmdline_parser_params_create(void)
00172 {
00173   struct cmdline_parser_params *params = 
00174     (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
00175   cmdline_parser_params_init(params);  
00176   return params;
00177 }
00178 
00179 static void
00180 free_string_field (char **s)
00181 {
00182   if (*s)
00183     {
00184       free (*s);
00185       *s = 0;
00186     }
00187 }
00188 
00189 
00190 static void
00191 cmdline_parser_release (struct gengetopt_args_info *args_info)
00192 {
00193   unsigned int i;
00194   free_string_field (&(args_info->import_format_arg));
00195   free_string_field (&(args_info->import_format_orig));
00196   
00197   
00198   for (i = 0; i < args_info->inputs_num; ++i)
00199     free (args_info->inputs [i]);
00200 
00201   if (args_info->inputs_num)
00202     free (args_info->inputs);
00203 
00204   clear_given (args_info);
00205 }
00206 
00207 
00208 static void
00209 write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
00210 {
00211   FIX_UNUSED (values);
00212   if (arg) {
00213     fprintf(outfile, "%s=\"%s\"\n", opt, arg);
00214   } else {
00215     fprintf(outfile, "%s\n", opt);
00216   }
00217 }
00218 
00219 
00220 int
00221 cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
00222 {
00223   int i = 0;
00224 
00225   if (!outfile)
00226     {
00227       fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
00228       return EXIT_FAILURE;
00229     }
00230 
00231   if (args_info->help_given)
00232     write_into_file(outfile, "help", 0, 0 );
00233   if (args_info->version_given)
00234     write_into_file(outfile, "version", 0, 0 );
00235   if (args_info->import_format_given)
00236     write_into_file(outfile, "import-format", args_info->import_format_orig, 0);
00237   if (args_info->list_import_formats_given)
00238     write_into_file(outfile, "list-import-formats", 0, 0 );
00239   if (args_info->msg_parser_given)
00240     write_into_file(outfile, "msg_parser", 0, 0 );
00241   if (args_info->msg_debug_given)
00242     write_into_file(outfile, "msg_debug", 0, 0 );
00243   if (args_info->msg_warning_given)
00244     write_into_file(outfile, "msg_warning", 0, 0 );
00245   if (args_info->msg_error_given)
00246     write_into_file(outfile, "msg_error", 0, 0 );
00247   if (args_info->msg_info_given)
00248     write_into_file(outfile, "msg_info", 0, 0 );
00249   if (args_info->msg_status_given)
00250     write_into_file(outfile, "msg_status", 0, 0 );
00251   
00252 
00253   i = EXIT_SUCCESS;
00254   return i;
00255 }
00256 
00257 int
00258 cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
00259 {
00260   FILE *outfile;
00261   int i = 0;
00262 
00263   outfile = fopen(filename, "w");
00264 
00265   if (!outfile)
00266     {
00267       fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
00268       return EXIT_FAILURE;
00269     }
00270 
00271   i = cmdline_parser_dump(outfile, args_info);
00272   fclose (outfile);
00273 
00274   return i;
00275 }
00276 
00277 void
00278 cmdline_parser_free (struct gengetopt_args_info *args_info)
00279 {
00280   cmdline_parser_release (args_info);
00281 }
00282 
00284 char *
00285 gengetopt_strdup (const char *s)
00286 {
00287   char *result = 0;
00288   if (!s)
00289     return result;
00290 
00291   result = (char*)malloc(strlen(s) + 1);
00292   if (result == (char*)0)
00293     return (char*)0;
00294   strcpy(result, s);
00295   return result;
00296 }
00297 
00298 int
00299 cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
00300 {
00301   return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
00302 }
00303 
00304 int
00305 cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
00306                    struct cmdline_parser_params *params)
00307 {
00308   int result;
00309   result = cmdline_parser_internal (argc, argv, args_info, params, 0);
00310 
00311   if (result == EXIT_FAILURE)
00312     {
00313       cmdline_parser_free (args_info);
00314       exit (EXIT_FAILURE);
00315     }
00316   
00317   return result;
00318 }
00319 
00320 int
00321 cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
00322 {
00323   int result;
00324   struct cmdline_parser_params params;
00325   
00326   params.override = override;
00327   params.initialize = initialize;
00328   params.check_required = check_required;
00329   params.check_ambiguity = 0;
00330   params.print_errors = 1;
00331 
00332   result = cmdline_parser_internal (argc, argv, args_info, &params, 0);
00333 
00334   if (result == EXIT_FAILURE)
00335     {
00336       cmdline_parser_free (args_info);
00337       exit (EXIT_FAILURE);
00338     }
00339   
00340   return result;
00341 }
00342 
00343 int
00344 cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
00345 {
00346   FIX_UNUSED (args_info);
00347   FIX_UNUSED (prog_name);
00348   return EXIT_SUCCESS;
00349 }
00350 
00351 
00352 static char *package_name = 0;
00353 
00372 static
00373 int update_arg(void *field, char **orig_field,
00374                unsigned int *field_given, unsigned int *prev_given, 
00375                char *value, const char *possible_values[],
00376                const char *default_value,
00377                cmdline_parser_arg_type arg_type,
00378                int check_ambiguity, int override,
00379                int no_free, int multiple_option,
00380                const char *long_opt, char short_opt,
00381                const char *additional_error)
00382 {
00383   char *stop_char = 0;
00384   const char *val = value;
00385   int found;
00386   char **string_field;
00387   FIX_UNUSED (field);
00388 
00389   stop_char = 0;
00390   found = 0;
00391 
00392   if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
00393     {
00394       if (short_opt != '-')
00395         fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n", 
00396                package_name, long_opt, short_opt,
00397                (additional_error ? additional_error : ""));
00398       else
00399         fprintf (stderr, "%s: `--%s' option given more than once%s\n", 
00400                package_name, long_opt,
00401                (additional_error ? additional_error : ""));
00402       return 1; /* failure */
00403     }
00404 
00405   FIX_UNUSED (default_value);
00406     
00407   if (field_given && *field_given && ! override)
00408     return 0;
00409   if (prev_given)
00410     (*prev_given)++;
00411   if (field_given)
00412     (*field_given)++;
00413   if (possible_values)
00414     val = possible_values[found];
00415 
00416   switch(arg_type) {
00417   case ARG_FLAG:
00418     *((int *)field) = !*((int *)field);
00419     break;
00420   case ARG_STRING:
00421     if (val) {
00422       string_field = (char **)field;
00423       if (!no_free && *string_field)
00424         free (*string_field); /* free previous string */
00425       *string_field = gengetopt_strdup (val);
00426     }
00427     break;
00428   default:
00429     break;
00430   };
00431 
00432 
00433   /* store the original value */
00434   switch(arg_type) {
00435   case ARG_NO:
00436   case ARG_FLAG:
00437     break;
00438   default:
00439     if (value && orig_field) {
00440       if (no_free) {
00441         *orig_field = value;
00442       } else {
00443         if (*orig_field)
00444           free (*orig_field); /* free previous string */
00445         *orig_field = gengetopt_strdup (value);
00446       }
00447     }
00448   };
00449 
00450   return 0; /* OK */
00451 }
00452 
00453 
00454 int
00455 cmdline_parser_internal (
00456   int argc, char **argv, struct gengetopt_args_info *args_info,
00457                         struct cmdline_parser_params *params, const char *additional_error)
00458 {
00459   int c;        /* Character of the parsed option.  */
00460 
00461   int error = 0;
00462   struct gengetopt_args_info local_args_info;
00463   
00464   int override;
00465   int initialize;
00466   int check_required;
00467   int check_ambiguity;
00468   
00469   package_name = argv[0];
00470   
00471   override = params->override;
00472   initialize = params->initialize;
00473   check_required = params->check_required;
00474   check_ambiguity = params->check_ambiguity;
00475 
00476   if (initialize)
00477     cmdline_parser_init (args_info);
00478 
00479   cmdline_parser_init (&local_args_info);
00480 
00481   optarg = 0;
00482   optind = 0;
00483   opterr = params->print_errors;
00484   optopt = '?';
00485 
00486   while (1)
00487     {
00488       int option_index = 0;
00489 
00490       static struct option long_options[] = {
00491         { "help",       0, NULL, 'h' },
00492         { "version",    0, NULL, 'V' },
00493         { "import-format",      1, NULL, 'f' },
00494         { "list-import-formats",        0, NULL, 0 },
00495         { "msg_parser", 0, NULL, 0 },
00496         { "msg_debug",  0, NULL, 0 },
00497         { "msg_warning",        0, NULL, 0 },
00498         { "msg_error",  0, NULL, 0 },
00499         { "msg_info",   0, NULL, 0 },
00500         { "msg_status", 0, NULL, 0 },
00501         { 0,  0, 0, 0 }
00502       };
00503 
00504       c = getopt_long (argc, argv, "hVf:", long_options, &option_index);
00505 
00506       if (c == -1) break;       /* Exit from `while (1)' loop.  */
00507 
00508       switch (c)
00509         {
00510         case 'h':       /* Print help and exit.  */
00511           cmdline_parser_print_help ();
00512           cmdline_parser_free (&local_args_info);
00513           exit (EXIT_SUCCESS);
00514 
00515         case 'V':       /* Print version and exit.  */
00516           cmdline_parser_print_version ();
00517           cmdline_parser_free (&local_args_info);
00518           exit (EXIT_SUCCESS);
00519 
00520         case 'f':       /* Force the file format of the file(s) specified.  */
00521         
00522         
00523           if (update_arg( (void *)&(args_info->import_format_arg), 
00524                &(args_info->import_format_orig), &(args_info->import_format_given),
00525               &(local_args_info.import_format_given), optarg, 0, "AUTODETECT", ARG_STRING,
00526               check_ambiguity, override, 0, 0,
00527               "import-format", 'f',
00528               additional_error))
00529             goto failure;
00530         
00531           break;
00532 
00533         case 0: /* Long option with no short option */
00534           /* List available import file formats 'import-format' command.  */
00535           if (strcmp (long_options[option_index].name, "list-import-formats") == 0)
00536           {
00537           
00538           
00539             if (update_arg( 0 , 
00540                  0 , &(args_info->list_import_formats_given),
00541                 &(local_args_info.list_import_formats_given), optarg, 0, 0, ARG_NO,
00542                 check_ambiguity, override, 0, 0,
00543                 "list-import-formats", '-',
00544                 additional_error))
00545               goto failure;
00546           
00547           }
00548           /* Output file parsing messages.  */
00549           else if (strcmp (long_options[option_index].name, "msg_parser") == 0)
00550           {
00551           
00552           
00553             if (update_arg((void *)&(args_info->msg_parser_flag), 0, &(args_info->msg_parser_given),
00554                 &(local_args_info.msg_parser_given), optarg, 0, 0, ARG_FLAG,
00555                 check_ambiguity, override, 1, 0, "msg_parser", '-',
00556                 additional_error))
00557               goto failure;
00558           
00559           }
00560           /* Output messages meant for debuging.  */
00561           else if (strcmp (long_options[option_index].name, "msg_debug") == 0)
00562           {
00563           
00564           
00565             if (update_arg((void *)&(args_info->msg_debug_flag), 0, &(args_info->msg_debug_given),
00566                 &(local_args_info.msg_debug_given), optarg, 0, 0, ARG_FLAG,
00567                 check_ambiguity, override, 1, 0, "msg_debug", '-',
00568                 additional_error))
00569               goto failure;
00570           
00571           }
00572           /* Output warning messages about abnormal conditions and unknown constructs.  */
00573           else if (strcmp (long_options[option_index].name, "msg_warning") == 0)
00574           {
00575           
00576           
00577             if (update_arg((void *)&(args_info->msg_warning_flag), 0, &(args_info->msg_warning_given),
00578                 &(local_args_info.msg_warning_given), optarg, 0, 0, ARG_FLAG,
00579                 check_ambiguity, override, 1, 0, "msg_warning", '-',
00580                 additional_error))
00581               goto failure;
00582           
00583           }
00584           /* Output error messages.  */
00585           else if (strcmp (long_options[option_index].name, "msg_error") == 0)
00586           {
00587           
00588           
00589             if (update_arg((void *)&(args_info->msg_error_flag), 0, &(args_info->msg_error_given),
00590                 &(local_args_info.msg_error_given), optarg, 0, 0, ARG_FLAG,
00591                 check_ambiguity, override, 1, 0, "msg_error", '-',
00592                 additional_error))
00593               goto failure;
00594           
00595           }
00596           /* Output informational messages about the progress of the library.  */
00597           else if (strcmp (long_options[option_index].name, "msg_info") == 0)
00598           {
00599           
00600           
00601             if (update_arg((void *)&(args_info->msg_info_flag), 0, &(args_info->msg_info_given),
00602                 &(local_args_info.msg_info_given), optarg, 0, 0, ARG_FLAG,
00603                 check_ambiguity, override, 1, 0, "msg_info", '-',
00604                 additional_error))
00605               goto failure;
00606           
00607           }
00608           /* Output status messages.  */
00609           else if (strcmp (long_options[option_index].name, "msg_status") == 0)
00610           {
00611           
00612           
00613             if (update_arg((void *)&(args_info->msg_status_flag), 0, &(args_info->msg_status_given),
00614                 &(local_args_info.msg_status_given), optarg, 0, 0, ARG_FLAG,
00615                 check_ambiguity, override, 1, 0, "msg_status", '-',
00616                 additional_error))
00617               goto failure;
00618           
00619           }
00620           
00621           break;
00622         case '?':       /* Invalid option.  */
00623           /* `getopt_long' already printed an error message.  */
00624           goto failure;
00625 
00626         default:        /* bug: option not considered.  */
00627           fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
00628           abort ();
00629         } /* switch */
00630     } /* while */
00631 
00632 
00633 
00634 
00635   cmdline_parser_release (&local_args_info);
00636 
00637   if ( error )
00638     return (EXIT_FAILURE);
00639 
00640   if (optind < argc)
00641     {
00642       int i = 0 ;
00643       int found_prog_name = 0;
00644       /* whether program name, i.e., argv[0], is in the remaining args
00645          (this may happen with some implementations of getopt,
00646           but surely not with the one included by gengetopt) */
00647 
00648       i = optind;
00649       while (i < argc)
00650         if (argv[i++] == argv[0]) {
00651           found_prog_name = 1;
00652           break;
00653         }
00654       i = 0;
00655 
00656       args_info->inputs_num = argc - optind - found_prog_name;
00657       args_info->inputs =
00658         (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
00659       while (optind < argc)
00660         if (argv[optind++] != argv[0])
00661           args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ;
00662     }
00663 
00664   return 0;
00665 
00666 failure:
00667   
00668   cmdline_parser_release (&local_args_info);
00669   return (EXIT_FAILURE);
00670 }