00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
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 #include "getopt.h"
00021
00022 #include "cmdline.h"
00023
00024 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.";
00025
00026 const char *gengetopt_args_info_usage = "Usage: " CMDLINE_PARSER_PACKAGE " [OPTIONS]... [FILES]...";
00027
00028 const char *gengetopt_args_info_description = "";
00029
00030 const char *gengetopt_args_info_help[] = {
00031 " -h, --help Print help and exit",
00032 " -V, --version Print version and exit",
00033 " -f, --import-format=STRING Force the file format of the file(s) specified \n (default=`AUTODETECT')",
00034 " --list-import-formats List available import file formats \n 'import-format' command",
00035 " --msg_parser Output file parsing messages (default=off)",
00036 " --msg_debug Output messages meant for debuging (default=off)",
00037 " --msg_warning Output warning messages about abnormal conditions \n and unknown constructs (default=on)",
00038 " --msg_error Output error messages (default=on)",
00039 " --msg_info Output informational messages about the progress \n of the library (default=on)",
00040 " --msg_status Output status messages (default=on)",
00041 0
00042 };
00043
00044 static
00045 void clear_given (struct gengetopt_args_info *args_info);
00046 static
00047 void clear_args (struct gengetopt_args_info *args_info);
00048
00049 static int
00050 cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info,
00051 struct cmdline_parser_params *params, const char *additional_error);
00052
00053
00054 static char *
00055 gengetopt_strdup (const char *s);
00056
00057 static
00058 void clear_given (struct gengetopt_args_info *args_info)
00059 {
00060 args_info->help_given = 0 ;
00061 args_info->version_given = 0 ;
00062 args_info->import_format_given = 0 ;
00063 args_info->list_import_formats_given = 0 ;
00064 args_info->msg_parser_given = 0 ;
00065 args_info->msg_debug_given = 0 ;
00066 args_info->msg_warning_given = 0 ;
00067 args_info->msg_error_given = 0 ;
00068 args_info->msg_info_given = 0 ;
00069 args_info->msg_status_given = 0 ;
00070 }
00071
00072 static
00073 void clear_args (struct gengetopt_args_info *args_info)
00074 {
00075 args_info->import_format_arg = gengetopt_strdup ("AUTODETECT");
00076 args_info->import_format_orig = NULL;
00077 args_info->msg_parser_flag = 0;
00078 args_info->msg_debug_flag = 0;
00079 args_info->msg_warning_flag = 1;
00080 args_info->msg_error_flag = 1;
00081 args_info->msg_info_flag = 1;
00082 args_info->msg_status_flag = 1;
00083
00084 }
00085
00086 static
00087 void init_args_info(struct gengetopt_args_info *args_info)
00088 {
00089 args_info->help_help = gengetopt_args_info_help[0] ;
00090 args_info->version_help = gengetopt_args_info_help[1] ;
00091 args_info->import_format_help = gengetopt_args_info_help[2] ;
00092 args_info->list_import_formats_help = gengetopt_args_info_help[3] ;
00093 args_info->msg_parser_help = gengetopt_args_info_help[4] ;
00094 args_info->msg_debug_help = gengetopt_args_info_help[5] ;
00095 args_info->msg_warning_help = gengetopt_args_info_help[6] ;
00096 args_info->msg_error_help = gengetopt_args_info_help[7] ;
00097 args_info->msg_info_help = gengetopt_args_info_help[8] ;
00098 args_info->msg_status_help = gengetopt_args_info_help[9] ;
00099
00100 }
00101
00102 void
00103 cmdline_parser_print_version (void)
00104 {
00105 printf ("%s %s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
00106 }
00107
00108 void
00109 cmdline_parser_print_help (void)
00110 {
00111 int i = 0;
00112 cmdline_parser_print_version ();
00113
00114 if (strlen(gengetopt_args_info_purpose) > 0)
00115 printf("\n%s\n", gengetopt_args_info_purpose);
00116
00117 printf("\n%s\n\n", gengetopt_args_info_usage);
00118
00119 if (strlen(gengetopt_args_info_description) > 0)
00120 printf("%s\n", gengetopt_args_info_description);
00121
00122 while (gengetopt_args_info_help[i])
00123 printf("%s\n", gengetopt_args_info_help[i++]);
00124 }
00125
00126 void
00127 cmdline_parser_init (struct gengetopt_args_info *args_info)
00128 {
00129 clear_given (args_info);
00130 clear_args (args_info);
00131 init_args_info (args_info);
00132
00133 args_info->inputs = NULL;
00134 args_info->inputs_num = 0;
00135 }
00136
00137 struct cmdline_parser_params *
00138 cmdline_parser_params_init()
00139 {
00140 struct cmdline_parser_params *params =
00141 (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
00142
00143 if (params)
00144 {
00145 params->override = 0;
00146 params->initialize = 0;
00147 params->check_required = 0;
00148 params->check_ambiguity = 0;
00149 }
00150
00151 return params;
00152 }
00153
00154 static void
00155 cmdline_parser_release (struct gengetopt_args_info *args_info)
00156 {
00157
00158 unsigned int i;
00159 if (args_info->import_format_arg)
00160 {
00161 free (args_info->import_format_arg);
00162 args_info->import_format_arg = 0;
00163 }
00164 if (args_info->import_format_orig)
00165 {
00166 free (args_info->import_format_orig);
00167 args_info->import_format_orig = 0;
00168 }
00169
00170 for (i = 0; i < args_info->inputs_num; ++i)
00171 free (args_info->inputs [i]);
00172
00173 if (args_info->inputs_num)
00174 free (args_info->inputs);
00175
00176 clear_given (args_info);
00177 }
00178
00179 int
00180 cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
00181 {
00182 FILE *outfile;
00183 int i = 0;
00184
00185 outfile = fopen(filename, "w");
00186
00187 if (!outfile)
00188 {
00189 fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
00190 return EXIT_FAILURE;
00191 }
00192
00193 if (args_info->help_given) {
00194 fprintf(outfile, "%s\n", "help");
00195 }
00196 if (args_info->version_given) {
00197 fprintf(outfile, "%s\n", "version");
00198 }
00199 if (args_info->import_format_given) {
00200 if (args_info->import_format_orig) {
00201 fprintf(outfile, "%s=\"%s\"\n", "import-format", args_info->import_format_orig);
00202 } else {
00203 fprintf(outfile, "%s\n", "import-format");
00204 }
00205 }
00206 if (args_info->list_import_formats_given) {
00207 fprintf(outfile, "%s\n", "list-import-formats");
00208 }
00209 if (args_info->msg_parser_given) {
00210 fprintf(outfile, "%s\n", "msg_parser");
00211 }
00212 if (args_info->msg_debug_given) {
00213 fprintf(outfile, "%s\n", "msg_debug");
00214 }
00215 if (args_info->msg_warning_given) {
00216 fprintf(outfile, "%s\n", "msg_warning");
00217 }
00218 if (args_info->msg_error_given) {
00219 fprintf(outfile, "%s\n", "msg_error");
00220 }
00221 if (args_info->msg_info_given) {
00222 fprintf(outfile, "%s\n", "msg_info");
00223 }
00224 if (args_info->msg_status_given) {
00225 fprintf(outfile, "%s\n", "msg_status");
00226 }
00227
00228 fclose (outfile);
00229
00230 i = EXIT_SUCCESS;
00231 return i;
00232 }
00233
00234 void
00235 cmdline_parser_free (struct gengetopt_args_info *args_info)
00236 {
00237 cmdline_parser_release (args_info);
00238 }
00239
00240
00241
00242
00243 char *
00244 gengetopt_strdup (const char *s)
00245 {
00246 char *result = NULL;
00247 if (!s)
00248 return result;
00249
00250 result = (char*)malloc(strlen(s) + 1);
00251 if (result == (char*)0)
00252 return (char*)0;
00253 strcpy(result, s);
00254 return result;
00255 }
00256
00257 int
00258 cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info)
00259 {
00260 return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
00261 }
00262
00263 int
00264 cmdline_parser_ext (int argc, char * const *argv, struct gengetopt_args_info *args_info,
00265 struct cmdline_parser_params *params)
00266 {
00267 int result;
00268 result = cmdline_parser_internal (argc, argv, args_info, params, NULL);
00269
00270 if (result == EXIT_FAILURE)
00271 {
00272 cmdline_parser_free (args_info);
00273 exit (EXIT_FAILURE);
00274 }
00275
00276 return result;
00277 }
00278
00279 int
00280 cmdline_parser2 (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
00281 {
00282 int result;
00283 struct cmdline_parser_params params;
00284
00285 params.override = override;
00286 params.initialize = initialize;
00287 params.check_required = check_required;
00288 params.check_ambiguity = 0;
00289
00290 result = cmdline_parser_internal (argc, argv, args_info, ¶ms, NULL);
00291
00292 if (result == EXIT_FAILURE)
00293 {
00294 cmdline_parser_free (args_info);
00295 exit (EXIT_FAILURE);
00296 }
00297
00298 return result;
00299 }
00300
00301 int
00302 cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
00303 {
00304 return EXIT_SUCCESS;
00305 }
00306
00307 int
00308 cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info,
00309 struct cmdline_parser_params *params, const char *additional_error)
00310 {
00311 int c;
00312
00313 int error = 0;
00314 struct gengetopt_args_info local_args_info;
00315
00316 int override;
00317 int initialize;
00318 int check_required;
00319 int check_ambiguity;
00320
00321 override = params->override;
00322 initialize = params->initialize;
00323 check_required = params->check_required;
00324 check_ambiguity = params->check_ambiguity;
00325
00326 if (initialize)
00327 cmdline_parser_init (args_info);
00328
00329 cmdline_parser_init (&local_args_info);
00330
00331 optarg = 0;
00332 optind = 0;
00333 opterr = 1;
00334 optopt = '?';
00335
00336 while (1)
00337 {
00338 int option_index = 0;
00339 char *stop_char;
00340
00341 static struct option long_options[] = {
00342 { "help", 0, NULL, 'h' },
00343 { "version", 0, NULL, 'V' },
00344 { "import-format", 1, NULL, 'f' },
00345 { "list-import-formats", 0, NULL, 0 },
00346 { "msg_parser", 0, NULL, 0 },
00347 { "msg_debug", 0, NULL, 0 },
00348 { "msg_warning", 0, NULL, 0 },
00349 { "msg_error", 0, NULL, 0 },
00350 { "msg_info", 0, NULL, 0 },
00351 { "msg_status", 0, NULL, 0 },
00352 { NULL, 0, NULL, 0 }
00353 };
00354
00355 stop_char = 0;
00356 c = getopt_long (argc, argv, "hVf:", long_options, &option_index);
00357
00358 if (c == -1) break;
00359
00360 switch (c)
00361 {
00362 case 'h':
00363 cmdline_parser_print_help ();
00364 cmdline_parser_free (&local_args_info);
00365 exit (EXIT_SUCCESS);
00366
00367 case 'V':
00368 cmdline_parser_print_version ();
00369 cmdline_parser_free (&local_args_info);
00370 exit (EXIT_SUCCESS);
00371
00372 case 'f':
00373 if (local_args_info.import_format_given || (check_ambiguity && args_info->import_format_given))
00374 {
00375 fprintf (stderr, "%s: `--import-format' (`-f') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00376 goto failure;
00377 }
00378 if (args_info->import_format_given && ! override)
00379 continue;
00380 local_args_info.import_format_given = 1;
00381 args_info->import_format_given = 1;
00382 if (args_info->import_format_arg)
00383 free (args_info->import_format_arg);
00384 args_info->import_format_arg = gengetopt_strdup (optarg);
00385 if (args_info->import_format_orig)
00386 free (args_info->import_format_orig);
00387 args_info->import_format_orig = gengetopt_strdup (optarg);
00388 break;
00389
00390
00391 case 0:
00392
00393 if (strcmp (long_options[option_index].name, "list-import-formats") == 0)
00394 {
00395 if (local_args_info.list_import_formats_given || (check_ambiguity && args_info->list_import_formats_given))
00396 {
00397 fprintf (stderr, "%s: `--list-import-formats' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00398 goto failure;
00399 }
00400 if (args_info->list_import_formats_given && ! override)
00401 continue;
00402 local_args_info.list_import_formats_given = 1;
00403 args_info->list_import_formats_given = 1;
00404 break;
00405 }
00406
00407 else if (strcmp (long_options[option_index].name, "msg_parser") == 0)
00408 {
00409 if (local_args_info.msg_parser_given || (check_ambiguity && args_info->msg_parser_given))
00410 {
00411 fprintf (stderr, "%s: `--msg_parser' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00412 goto failure;
00413 }
00414 if (args_info->msg_parser_given && ! override)
00415 continue;
00416 local_args_info.msg_parser_given = 1;
00417 args_info->msg_parser_given = 1;
00418 args_info->msg_parser_flag = !(args_info->msg_parser_flag);
00419 }
00420
00421 else if (strcmp (long_options[option_index].name, "msg_debug") == 0)
00422 {
00423 if (local_args_info.msg_debug_given || (check_ambiguity && args_info->msg_debug_given))
00424 {
00425 fprintf (stderr, "%s: `--msg_debug' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00426 goto failure;
00427 }
00428 if (args_info->msg_debug_given && ! override)
00429 continue;
00430 local_args_info.msg_debug_given = 1;
00431 args_info->msg_debug_given = 1;
00432 args_info->msg_debug_flag = !(args_info->msg_debug_flag);
00433 }
00434
00435 else if (strcmp (long_options[option_index].name, "msg_warning") == 0)
00436 {
00437 if (local_args_info.msg_warning_given || (check_ambiguity && args_info->msg_warning_given))
00438 {
00439 fprintf (stderr, "%s: `--msg_warning' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00440 goto failure;
00441 }
00442 if (args_info->msg_warning_given && ! override)
00443 continue;
00444 local_args_info.msg_warning_given = 1;
00445 args_info->msg_warning_given = 1;
00446 args_info->msg_warning_flag = !(args_info->msg_warning_flag);
00447 }
00448
00449 else if (strcmp (long_options[option_index].name, "msg_error") == 0)
00450 {
00451 if (local_args_info.msg_error_given || (check_ambiguity && args_info->msg_error_given))
00452 {
00453 fprintf (stderr, "%s: `--msg_error' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00454 goto failure;
00455 }
00456 if (args_info->msg_error_given && ! override)
00457 continue;
00458 local_args_info.msg_error_given = 1;
00459 args_info->msg_error_given = 1;
00460 args_info->msg_error_flag = !(args_info->msg_error_flag);
00461 }
00462
00463 else if (strcmp (long_options[option_index].name, "msg_info") == 0)
00464 {
00465 if (local_args_info.msg_info_given || (check_ambiguity && args_info->msg_info_given))
00466 {
00467 fprintf (stderr, "%s: `--msg_info' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00468 goto failure;
00469 }
00470 if (args_info->msg_info_given && ! override)
00471 continue;
00472 local_args_info.msg_info_given = 1;
00473 args_info->msg_info_given = 1;
00474 args_info->msg_info_flag = !(args_info->msg_info_flag);
00475 }
00476
00477 else if (strcmp (long_options[option_index].name, "msg_status") == 0)
00478 {
00479 if (local_args_info.msg_status_given || (check_ambiguity && args_info->msg_status_given))
00480 {
00481 fprintf (stderr, "%s: `--msg_status' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00482 goto failure;
00483 }
00484 if (args_info->msg_status_given && ! override)
00485 continue;
00486 local_args_info.msg_status_given = 1;
00487 args_info->msg_status_given = 1;
00488 args_info->msg_status_flag = !(args_info->msg_status_flag);
00489 }
00490
00491 break;
00492 case '?':
00493
00494 goto failure;
00495
00496 default:
00497 fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
00498 abort ();
00499 }
00500 }
00501
00502
00503
00504
00505 cmdline_parser_release (&local_args_info);
00506
00507 if ( error )
00508 return (EXIT_FAILURE);
00509
00510 if (optind < argc)
00511 {
00512 int i = 0 ;
00513 int found_prog_name = 0;
00514
00515
00516
00517
00518 i = optind;
00519 while (i < argc)
00520 if (argv[i++] == argv[0]) {
00521 found_prog_name = 1;
00522 break;
00523 }
00524 i = 0;
00525
00526 args_info->inputs_num = argc - optind - found_prog_name;
00527 args_info->inputs =
00528 (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
00529 while (optind < argc)
00530 if (argv[optind++] != argv[0])
00531 args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ;
00532 }
00533
00534 return 0;
00535
00536 failure:
00537
00538 cmdline_parser_release (&local_args_info);
00539 return (EXIT_FAILURE);
00540 }