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 static
00025 void clear_given (struct gengetopt_args_info *args_info);
00026 static
00027 void clear_args (struct gengetopt_args_info *args_info);
00028
00029 static int
00030 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);
00031
00032
00033 static char *
00034 gengetopt_strdup (const char *s);
00035
00036 static
00037 void clear_given (struct gengetopt_args_info *args_info)
00038 {
00039 args_info->help_given = 0 ;
00040 args_info->version_given = 0 ;
00041 args_info->statement_req_given = 0 ;
00042 args_info->accountinfo_req_given = 0 ;
00043 args_info->fid_given = 0 ;
00044 args_info->org_given = 0 ;
00045 args_info->bank_given = 0 ;
00046 args_info->broker_given = 0 ;
00047 args_info->user_given = 0 ;
00048 args_info->pass_given = 0 ;
00049 args_info->acct_given = 0 ;
00050 args_info->type_given = 0 ;
00051 args_info->past_given = 0 ;
00052 args_info->url_given = 0 ;
00053 }
00054
00055 static
00056 void clear_args (struct gengetopt_args_info *args_info)
00057 {
00058 args_info->statement_req_flag = 0;
00059 args_info->accountinfo_req_flag = 0;
00060 args_info->fid_arg = NULL;
00061 args_info->org_arg = NULL;
00062 args_info->bank_arg = NULL;
00063 args_info->broker_arg = NULL;
00064 args_info->user_arg = NULL;
00065 args_info->pass_arg = NULL;
00066 args_info->acct_arg = NULL;
00067 args_info->url_arg = NULL;
00068 }
00069
00070 void
00071 cmdline_parser_print_version (void)
00072 {
00073 printf ("%s %s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
00074 }
00075
00076 void
00077 cmdline_parser_print_help (void)
00078 {
00079 cmdline_parser_print_version ();
00080 printf("\n%s\n", "prints to stdout the created OFX file based on the options you pass it. \ncurrently it will only create a statement request file. you can POST this to \nan OFX server to request a statement from that financial institution for that \naccount.");
00081 printf("\nUsage: %s [OPTIONS]... [FILES]...\n\n", CMDLINE_PARSER_PACKAGE);
00082 printf("%s\n"," -h, --help Print help and exit");
00083 printf("%s\n"," -V, --version Print version and exit");
00084 printf("%s\n"," -s, --statement-req Request for a statement (default=off)");
00085 printf("%s\n"," -a, --accountinfo-req Request for a list of accounts (default=off)");
00086 printf("%s\n"," --fid=STRING FI identifier");
00087 printf("%s\n"," --org=STRING FI org tag");
00088 printf("%s\n"," --bank=STRING IBAN bank identifier");
00089 printf("%s\n"," --broker=STRING Broker identifier");
00090 printf("%s\n"," --user=STRING User name");
00091 printf("%s\n"," --pass=STRING Password");
00092 printf("%s\n"," --acct=STRING Account ID");
00093 printf("%s\n"," --type=INT Account Type 1=checking 2=invest 3=ccard");
00094 printf("%s\n"," --past=LONG How far back to look from today (in days)");
00095 printf("%s\n"," --url=STRING Url to POST the data to (otherwise goes to stdout)");
00096
00097 }
00098
00099 void
00100 cmdline_parser_init (struct gengetopt_args_info *args_info)
00101 {
00102 clear_given (args_info);
00103 clear_args (args_info);
00104
00105 args_info->inputs = NULL;
00106 args_info->inputs_num = 0;
00107 }
00108
00109 void
00110 cmdline_parser_free (struct gengetopt_args_info *args_info)
00111 {
00112
00113 unsigned int i;
00114 if (args_info->fid_arg)
00115 {
00116 free (args_info->fid_arg);
00117 args_info->fid_arg = 0;
00118 }
00119 if (args_info->org_arg)
00120 {
00121 free (args_info->org_arg);
00122 args_info->org_arg = 0;
00123 }
00124 if (args_info->bank_arg)
00125 {
00126 free (args_info->bank_arg);
00127 args_info->bank_arg = 0;
00128 }
00129 if (args_info->broker_arg)
00130 {
00131 free (args_info->broker_arg);
00132 args_info->broker_arg = 0;
00133 }
00134 if (args_info->user_arg)
00135 {
00136 free (args_info->user_arg);
00137 args_info->user_arg = 0;
00138 }
00139 if (args_info->pass_arg)
00140 {
00141 free (args_info->pass_arg);
00142 args_info->pass_arg = 0;
00143 }
00144 if (args_info->acct_arg)
00145 {
00146 free (args_info->acct_arg);
00147 args_info->acct_arg = 0;
00148 }
00149 if (args_info->url_arg)
00150 {
00151 free (args_info->url_arg);
00152 args_info->url_arg = 0;
00153 }
00154
00155 for (i = 0; i < args_info->inputs_num; ++i)
00156 free (args_info->inputs [i]);
00157
00158 if (args_info->inputs_num)
00159 free (args_info->inputs);
00160
00161 clear_given (args_info);
00162 }
00163
00164
00165
00166 char *
00167 gengetopt_strdup (const char *s)
00168 {
00169 char *result = NULL;
00170 if (!s)
00171 return result;
00172
00173 result = (char*)malloc(strlen(s) + 1);
00174 if (result == (char*)0)
00175 return (char*)0;
00176 strcpy(result, s);
00177 return result;
00178 }
00179
00180 int
00181 cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info)
00182 {
00183 return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
00184 }
00185
00186 int
00187 cmdline_parser2 (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
00188 {
00189 int result;
00190
00191 result = cmdline_parser_internal (argc, argv, args_info, override, initialize, check_required, NULL);
00192
00193 if (result == EXIT_FAILURE)
00194 {
00195 cmdline_parser_free (args_info);
00196 exit (EXIT_FAILURE);
00197 }
00198
00199 return result;
00200 }
00201
00202 int
00203 cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
00204 {
00205 return EXIT_SUCCESS;
00206 }
00207
00208 int
00209 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)
00210 {
00211 int c;
00212
00213 int error = 0;
00214 struct gengetopt_args_info local_args_info;
00215
00216 if (initialize)
00217 cmdline_parser_init (args_info);
00218
00219 cmdline_parser_init (&local_args_info);
00220
00221 optarg = 0;
00222 optind = 1;
00223 opterr = 1;
00224 optopt = '?';
00225
00226 while (1)
00227 {
00228 int option_index = 0;
00229 char *stop_char;
00230
00231 static struct option long_options[] = {
00232 { "help", 0, NULL, 'h' },
00233 { "version", 0, NULL, 'V' },
00234 { "statement-req", 0, NULL, 's' },
00235 { "accountinfo-req", 0, NULL, 'a' },
00236 { "fid", 1, NULL, 0 },
00237 { "org", 1, NULL, 0 },
00238 { "bank", 1, NULL, 0 },
00239 { "broker", 1, NULL, 0 },
00240 { "user", 1, NULL, 0 },
00241 { "pass", 1, NULL, 0 },
00242 { "acct", 1, NULL, 0 },
00243 { "type", 1, NULL, 0 },
00244 { "past", 1, NULL, 0 },
00245 { "url", 1, NULL, 0 },
00246 { NULL, 0, NULL, 0 }
00247 };
00248
00249 stop_char = 0;
00250 c = getopt_long (argc, argv, "hVsa", long_options, &option_index);
00251
00252 if (c == -1) break;
00253
00254 switch (c)
00255 {
00256 case 'h':
00257 cmdline_parser_print_help ();
00258 exit (EXIT_SUCCESS);
00259
00260 case 'V':
00261 cmdline_parser_print_version ();
00262 exit (EXIT_SUCCESS);
00263
00264 case 's':
00265 if (local_args_info.statement_req_given)
00266 {
00267 fprintf (stderr, "%s: `--statement-req' (`-s') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00268 goto failure;
00269 }
00270 if (args_info->statement_req_given && ! override)
00271 continue;
00272 local_args_info.statement_req_given = 1;
00273 args_info->statement_req_given = 1;
00274 args_info->statement_req_flag = !(args_info->statement_req_flag);
00275 break;
00276
00277 case 'a':
00278 if (local_args_info.accountinfo_req_given)
00279 {
00280 fprintf (stderr, "%s: `--accountinfo-req' (`-a') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00281 goto failure;
00282 }
00283 if (args_info->accountinfo_req_given && ! override)
00284 continue;
00285 local_args_info.accountinfo_req_given = 1;
00286 args_info->accountinfo_req_given = 1;
00287 args_info->accountinfo_req_flag = !(args_info->accountinfo_req_flag);
00288 break;
00289
00290
00291 case 0:
00292
00293 if (strcmp (long_options[option_index].name, "fid") == 0)
00294 {
00295 if (local_args_info.fid_given)
00296 {
00297 fprintf (stderr, "%s: `--fid' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00298 goto failure;
00299 }
00300 if (args_info->fid_given && ! override)
00301 continue;
00302 local_args_info.fid_given = 1;
00303 args_info->fid_given = 1;
00304 if (args_info->fid_arg)
00305 free (args_info->fid_arg);
00306 args_info->fid_arg = gengetopt_strdup (optarg);
00307 }
00308
00309 else if (strcmp (long_options[option_index].name, "org") == 0)
00310 {
00311 if (local_args_info.org_given)
00312 {
00313 fprintf (stderr, "%s: `--org' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00314 goto failure;
00315 }
00316 if (args_info->org_given && ! override)
00317 continue;
00318 local_args_info.org_given = 1;
00319 args_info->org_given = 1;
00320 if (args_info->org_arg)
00321 free (args_info->org_arg);
00322 args_info->org_arg = gengetopt_strdup (optarg);
00323 }
00324
00325 else if (strcmp (long_options[option_index].name, "bank") == 0)
00326 {
00327 if (local_args_info.bank_given)
00328 {
00329 fprintf (stderr, "%s: `--bank' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00330 goto failure;
00331 }
00332 if (args_info->bank_given && ! override)
00333 continue;
00334 local_args_info.bank_given = 1;
00335 args_info->bank_given = 1;
00336 if (args_info->bank_arg)
00337 free (args_info->bank_arg);
00338 args_info->bank_arg = gengetopt_strdup (optarg);
00339 }
00340
00341 else if (strcmp (long_options[option_index].name, "broker") == 0)
00342 {
00343 if (local_args_info.broker_given)
00344 {
00345 fprintf (stderr, "%s: `--broker' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00346 goto failure;
00347 }
00348 if (args_info->broker_given && ! override)
00349 continue;
00350 local_args_info.broker_given = 1;
00351 args_info->broker_given = 1;
00352 if (args_info->broker_arg)
00353 free (args_info->broker_arg);
00354 args_info->broker_arg = gengetopt_strdup (optarg);
00355 }
00356
00357 else if (strcmp (long_options[option_index].name, "user") == 0)
00358 {
00359 if (local_args_info.user_given)
00360 {
00361 fprintf (stderr, "%s: `--user' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00362 goto failure;
00363 }
00364 if (args_info->user_given && ! override)
00365 continue;
00366 local_args_info.user_given = 1;
00367 args_info->user_given = 1;
00368 if (args_info->user_arg)
00369 free (args_info->user_arg);
00370 args_info->user_arg = gengetopt_strdup (optarg);
00371 }
00372
00373 else if (strcmp (long_options[option_index].name, "pass") == 0)
00374 {
00375 if (local_args_info.pass_given)
00376 {
00377 fprintf (stderr, "%s: `--pass' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00378 goto failure;
00379 }
00380 if (args_info->pass_given && ! override)
00381 continue;
00382 local_args_info.pass_given = 1;
00383 args_info->pass_given = 1;
00384 if (args_info->pass_arg)
00385 free (args_info->pass_arg);
00386 args_info->pass_arg = gengetopt_strdup (optarg);
00387 }
00388
00389 else if (strcmp (long_options[option_index].name, "acct") == 0)
00390 {
00391 if (local_args_info.acct_given)
00392 {
00393 fprintf (stderr, "%s: `--acct' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00394 goto failure;
00395 }
00396 if (args_info->acct_given && ! override)
00397 continue;
00398 local_args_info.acct_given = 1;
00399 args_info->acct_given = 1;
00400 if (args_info->acct_arg)
00401 free (args_info->acct_arg);
00402 args_info->acct_arg = gengetopt_strdup (optarg);
00403 }
00404
00405 else if (strcmp (long_options[option_index].name, "type") == 0)
00406 {
00407 if (local_args_info.type_given)
00408 {
00409 fprintf (stderr, "%s: `--type' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00410 goto failure;
00411 }
00412 if (args_info->type_given && ! override)
00413 continue;
00414 local_args_info.type_given = 1;
00415 args_info->type_given = 1;
00416 args_info->type_arg = strtol (optarg,&stop_char,0);
00417 }
00418
00419 else if (strcmp (long_options[option_index].name, "past") == 0)
00420 {
00421 if (local_args_info.past_given)
00422 {
00423 fprintf (stderr, "%s: `--past' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00424 goto failure;
00425 }
00426 if (args_info->past_given && ! override)
00427 continue;
00428 local_args_info.past_given = 1;
00429 args_info->past_given = 1;
00430 args_info->past_arg = strtol (optarg,&stop_char,0);
00431 }
00432
00433 else if (strcmp (long_options[option_index].name, "url") == 0)
00434 {
00435 if (local_args_info.url_given)
00436 {
00437 fprintf (stderr, "%s: `--url' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00438 goto failure;
00439 }
00440 if (args_info->url_given && ! override)
00441 continue;
00442 local_args_info.url_given = 1;
00443 args_info->url_given = 1;
00444 if (args_info->url_arg)
00445 free (args_info->url_arg);
00446 args_info->url_arg = gengetopt_strdup (optarg);
00447 }
00448
00449 break;
00450 case '?':
00451
00452 goto failure;
00453
00454 default:
00455 fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
00456 abort ();
00457 }
00458 }
00459
00460
00461
00462
00463 cmdline_parser_free (&local_args_info);
00464
00465 if ( error )
00466 return (EXIT_FAILURE);
00467
00468 if (optind < argc)
00469 {
00470 int i = 0 ;
00471
00472 args_info->inputs_num = argc - optind ;
00473 args_info->inputs =
00474 (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
00475 while (optind < argc)
00476 args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind++]) ;
00477 }
00478
00479 return 0;
00480
00481 failure:
00482
00483 cmdline_parser_free (&local_args_info);
00484 return (EXIT_FAILURE);
00485 }