zbopt.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. /*
  2. File autogenerated by gengetopt version 2.22.6
  3. generated with the following command:
  4. gengetopt -C --no-help --no-version -i /home/mike/folder/studium_ms/semester_5/master_thesis/scm-ssi-probe-response/attack_implementation/zmap/src/zbopt.ggo -F /home/mike/folder/studium_ms/semester_5/master_thesis/scm-ssi-probe-response/attack_implementation/zmap/src/zbopt
  5. The developers of gengetopt consider the fixed text that goes in all
  6. gengetopt output files to be in the public domain:
  7. we make no copyright claims on it.
  8. */
  9. /* If we use autoconf. */
  10. #ifdef HAVE_CONFIG_H
  11. #include "config.h"
  12. #endif
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #ifndef FIX_UNUSED
  17. #define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */
  18. #endif
  19. #include <getopt.h>
  20. #include "/home/mike/folder/studium_ms/semester_5/master_thesis/scm-ssi-probe-response/attack_implementation/zmap/src/zbopt.h"
  21. const char *gengetopt_args_info_purpose = "A tool for limiting and deduplicating a list of IP addresses";
  22. const char *gengetopt_args_info_usage = "Usage: zblacklist [OPTIONS]...";
  23. const char *gengetopt_args_info_versiontext = "";
  24. const char *gengetopt_args_info_description = "";
  25. const char *gengetopt_args_info_help[] = {
  26. "Basic arguments:",
  27. " -b, --blacklist-file=STRING File of subnets to exclude, in CIDR notation,\n one-per line.",
  28. " -w, --whitelist-file=STRING File of subnets to include, in CIDR notation,\n one-per line.",
  29. " -l, --log-file=STRING File to log to",
  30. " -v, --verbosity=INT Set log level verbosity (0-5, default 3)\n (default=`3')",
  31. " --no-duplicate-checking Don't deduplicate IP addresses (default false)",
  32. " --ignore-blacklist-errors Ignore invalid entires in the\n blacklist/whitelist (default false)",
  33. "\nAdditional options:",
  34. " -h, --help Print help and exit",
  35. " -V, --version Print version and exit",
  36. "At least one of --whitelist-file or --blacklist-file must be specified.\nBlacklist files take precedence over whitelist files when both are specified.\nThis results in an output of {whitelist - blacklist}.",
  37. 0
  38. };
  39. typedef enum {ARG_NO
  40. , ARG_STRING
  41. , ARG_INT
  42. } cmdline_parser_arg_type;
  43. static
  44. void clear_given (struct gengetopt_args_info *args_info);
  45. static
  46. void clear_args (struct gengetopt_args_info *args_info);
  47. static int
  48. cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
  49. struct cmdline_parser_params *params, const char *additional_error);
  50. struct line_list
  51. {
  52. char * string_arg;
  53. struct line_list * next;
  54. };
  55. static struct line_list *cmd_line_list = 0;
  56. static struct line_list *cmd_line_list_tmp = 0;
  57. static void
  58. free_cmd_list(void)
  59. {
  60. /* free the list of a previous call */
  61. if (cmd_line_list)
  62. {
  63. while (cmd_line_list) {
  64. cmd_line_list_tmp = cmd_line_list;
  65. cmd_line_list = cmd_line_list->next;
  66. free (cmd_line_list_tmp->string_arg);
  67. free (cmd_line_list_tmp);
  68. }
  69. }
  70. }
  71. static char *
  72. gengetopt_strdup (const char *s);
  73. static
  74. void clear_given (struct gengetopt_args_info *args_info)
  75. {
  76. args_info->blacklist_file_given = 0 ;
  77. args_info->whitelist_file_given = 0 ;
  78. args_info->log_file_given = 0 ;
  79. args_info->verbosity_given = 0 ;
  80. args_info->no_duplicate_checking_given = 0 ;
  81. args_info->ignore_blacklist_errors_given = 0 ;
  82. args_info->help_given = 0 ;
  83. args_info->version_given = 0 ;
  84. }
  85. static
  86. void clear_args (struct gengetopt_args_info *args_info)
  87. {
  88. FIX_UNUSED (args_info);
  89. args_info->blacklist_file_arg = NULL;
  90. args_info->blacklist_file_orig = NULL;
  91. args_info->whitelist_file_arg = NULL;
  92. args_info->whitelist_file_orig = NULL;
  93. args_info->log_file_arg = NULL;
  94. args_info->log_file_orig = NULL;
  95. args_info->verbosity_arg = 3;
  96. args_info->verbosity_orig = NULL;
  97. }
  98. static
  99. void init_args_info(struct gengetopt_args_info *args_info)
  100. {
  101. args_info->blacklist_file_help = gengetopt_args_info_help[1] ;
  102. args_info->whitelist_file_help = gengetopt_args_info_help[2] ;
  103. args_info->log_file_help = gengetopt_args_info_help[3] ;
  104. args_info->verbosity_help = gengetopt_args_info_help[4] ;
  105. args_info->no_duplicate_checking_help = gengetopt_args_info_help[5] ;
  106. args_info->ignore_blacklist_errors_help = gengetopt_args_info_help[6] ;
  107. args_info->help_help = gengetopt_args_info_help[8] ;
  108. args_info->version_help = gengetopt_args_info_help[9] ;
  109. }
  110. void
  111. cmdline_parser_print_version (void)
  112. {
  113. printf ("%s %s\n",
  114. (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
  115. CMDLINE_PARSER_VERSION);
  116. if (strlen(gengetopt_args_info_versiontext) > 0)
  117. printf("\n%s\n", gengetopt_args_info_versiontext);
  118. }
  119. static void print_help_common(void) {
  120. cmdline_parser_print_version ();
  121. if (strlen(gengetopt_args_info_purpose) > 0)
  122. printf("\n%s\n", gengetopt_args_info_purpose);
  123. if (strlen(gengetopt_args_info_usage) > 0)
  124. printf("\n%s\n", gengetopt_args_info_usage);
  125. printf("\n");
  126. if (strlen(gengetopt_args_info_description) > 0)
  127. printf("%s\n\n", gengetopt_args_info_description);
  128. }
  129. void
  130. cmdline_parser_print_help (void)
  131. {
  132. int i = 0;
  133. print_help_common();
  134. while (gengetopt_args_info_help[i])
  135. printf("%s\n", gengetopt_args_info_help[i++]);
  136. }
  137. void
  138. cmdline_parser_init (struct gengetopt_args_info *args_info)
  139. {
  140. clear_given (args_info);
  141. clear_args (args_info);
  142. init_args_info (args_info);
  143. }
  144. void
  145. cmdline_parser_params_init(struct cmdline_parser_params *params)
  146. {
  147. if (params)
  148. {
  149. params->override = 0;
  150. params->initialize = 1;
  151. params->check_required = 1;
  152. params->check_ambiguity = 0;
  153. params->print_errors = 1;
  154. }
  155. }
  156. struct cmdline_parser_params *
  157. cmdline_parser_params_create(void)
  158. {
  159. struct cmdline_parser_params *params =
  160. (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
  161. cmdline_parser_params_init(params);
  162. return params;
  163. }
  164. static void
  165. free_string_field (char **s)
  166. {
  167. if (*s)
  168. {
  169. free (*s);
  170. *s = 0;
  171. }
  172. }
  173. static void
  174. cmdline_parser_release (struct gengetopt_args_info *args_info)
  175. {
  176. free_string_field (&(args_info->blacklist_file_arg));
  177. free_string_field (&(args_info->blacklist_file_orig));
  178. free_string_field (&(args_info->whitelist_file_arg));
  179. free_string_field (&(args_info->whitelist_file_orig));
  180. free_string_field (&(args_info->log_file_arg));
  181. free_string_field (&(args_info->log_file_orig));
  182. free_string_field (&(args_info->verbosity_orig));
  183. clear_given (args_info);
  184. }
  185. static void
  186. write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
  187. {
  188. FIX_UNUSED (values);
  189. if (arg) {
  190. fprintf(outfile, "%s=\"%s\"\n", opt, arg);
  191. } else {
  192. fprintf(outfile, "%s\n", opt);
  193. }
  194. }
  195. int
  196. cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
  197. {
  198. int i = 0;
  199. if (!outfile)
  200. {
  201. fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
  202. return EXIT_FAILURE;
  203. }
  204. if (args_info->blacklist_file_given)
  205. write_into_file(outfile, "blacklist-file", args_info->blacklist_file_orig, 0);
  206. if (args_info->whitelist_file_given)
  207. write_into_file(outfile, "whitelist-file", args_info->whitelist_file_orig, 0);
  208. if (args_info->log_file_given)
  209. write_into_file(outfile, "log-file", args_info->log_file_orig, 0);
  210. if (args_info->verbosity_given)
  211. write_into_file(outfile, "verbosity", args_info->verbosity_orig, 0);
  212. if (args_info->no_duplicate_checking_given)
  213. write_into_file(outfile, "no-duplicate-checking", 0, 0 );
  214. if (args_info->ignore_blacklist_errors_given)
  215. write_into_file(outfile, "ignore-blacklist-errors", 0, 0 );
  216. if (args_info->help_given)
  217. write_into_file(outfile, "help", 0, 0 );
  218. if (args_info->version_given)
  219. write_into_file(outfile, "version", 0, 0 );
  220. i = EXIT_SUCCESS;
  221. return i;
  222. }
  223. int
  224. cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
  225. {
  226. FILE *outfile;
  227. int i = 0;
  228. outfile = fopen(filename, "w");
  229. if (!outfile)
  230. {
  231. fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
  232. return EXIT_FAILURE;
  233. }
  234. i = cmdline_parser_dump(outfile, args_info);
  235. fclose (outfile);
  236. return i;
  237. }
  238. void
  239. cmdline_parser_free (struct gengetopt_args_info *args_info)
  240. {
  241. cmdline_parser_release (args_info);
  242. }
  243. /** @brief replacement of strdup, which is not standard */
  244. char *
  245. gengetopt_strdup (const char *s)
  246. {
  247. char *result = 0;
  248. if (!s)
  249. return result;
  250. result = (char*)malloc(strlen(s) + 1);
  251. if (result == (char*)0)
  252. return (char*)0;
  253. strcpy(result, s);
  254. return result;
  255. }
  256. int
  257. cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
  258. {
  259. return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
  260. }
  261. int
  262. cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
  263. struct cmdline_parser_params *params)
  264. {
  265. int result;
  266. result = cmdline_parser_internal (argc, argv, args_info, params, 0);
  267. if (result == EXIT_FAILURE)
  268. {
  269. cmdline_parser_free (args_info);
  270. exit (EXIT_FAILURE);
  271. }
  272. return result;
  273. }
  274. int
  275. cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
  276. {
  277. int result;
  278. struct cmdline_parser_params params;
  279. params.override = override;
  280. params.initialize = initialize;
  281. params.check_required = check_required;
  282. params.check_ambiguity = 0;
  283. params.print_errors = 1;
  284. result = cmdline_parser_internal (argc, argv, args_info, &params, 0);
  285. if (result == EXIT_FAILURE)
  286. {
  287. cmdline_parser_free (args_info);
  288. exit (EXIT_FAILURE);
  289. }
  290. return result;
  291. }
  292. int
  293. cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
  294. {
  295. FIX_UNUSED (args_info);
  296. FIX_UNUSED (prog_name);
  297. return EXIT_SUCCESS;
  298. }
  299. static char *package_name = 0;
  300. /**
  301. * @brief updates an option
  302. * @param field the generic pointer to the field to update
  303. * @param orig_field the pointer to the orig field
  304. * @param field_given the pointer to the number of occurrence of this option
  305. * @param prev_given the pointer to the number of occurrence already seen
  306. * @param value the argument for this option (if null no arg was specified)
  307. * @param possible_values the possible values for this option (if specified)
  308. * @param default_value the default value (in case the option only accepts fixed values)
  309. * @param arg_type the type of this option
  310. * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
  311. * @param override @see cmdline_parser_params.override
  312. * @param no_free whether to free a possible previous value
  313. * @param multiple_option whether this is a multiple option
  314. * @param long_opt the corresponding long option
  315. * @param short_opt the corresponding short option (or '-' if none)
  316. * @param additional_error possible further error specification
  317. */
  318. static
  319. int update_arg(void *field, char **orig_field,
  320. unsigned int *field_given, unsigned int *prev_given,
  321. char *value, const char *possible_values[],
  322. const char *default_value,
  323. cmdline_parser_arg_type arg_type,
  324. int check_ambiguity, int override,
  325. int no_free, int multiple_option,
  326. const char *long_opt, char short_opt,
  327. const char *additional_error)
  328. {
  329. char *stop_char = 0;
  330. const char *val = value;
  331. int found;
  332. char **string_field;
  333. FIX_UNUSED (field);
  334. stop_char = 0;
  335. found = 0;
  336. if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
  337. {
  338. if (short_opt != '-')
  339. fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
  340. package_name, long_opt, short_opt,
  341. (additional_error ? additional_error : ""));
  342. else
  343. fprintf (stderr, "%s: `--%s' option given more than once%s\n",
  344. package_name, long_opt,
  345. (additional_error ? additional_error : ""));
  346. return 1; /* failure */
  347. }
  348. FIX_UNUSED (default_value);
  349. if (field_given && *field_given && ! override)
  350. return 0;
  351. if (prev_given)
  352. (*prev_given)++;
  353. if (field_given)
  354. (*field_given)++;
  355. if (possible_values)
  356. val = possible_values[found];
  357. switch(arg_type) {
  358. case ARG_INT:
  359. if (val) *((int *)field) = strtol (val, &stop_char, 0);
  360. break;
  361. case ARG_STRING:
  362. if (val) {
  363. string_field = (char **)field;
  364. if (!no_free && *string_field)
  365. free (*string_field); /* free previous string */
  366. *string_field = gengetopt_strdup (val);
  367. }
  368. break;
  369. default:
  370. break;
  371. };
  372. /* check numeric conversion */
  373. switch(arg_type) {
  374. case ARG_INT:
  375. if (val && !(stop_char && *stop_char == '\0')) {
  376. fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
  377. return 1; /* failure */
  378. }
  379. break;
  380. default:
  381. ;
  382. };
  383. /* store the original value */
  384. switch(arg_type) {
  385. case ARG_NO:
  386. break;
  387. default:
  388. if (value && orig_field) {
  389. if (no_free) {
  390. *orig_field = value;
  391. } else {
  392. if (*orig_field)
  393. free (*orig_field); /* free previous string */
  394. *orig_field = gengetopt_strdup (value);
  395. }
  396. }
  397. };
  398. return 0; /* OK */
  399. }
  400. int
  401. cmdline_parser_internal (
  402. int argc, char **argv, struct gengetopt_args_info *args_info,
  403. struct cmdline_parser_params *params, const char *additional_error)
  404. {
  405. int c; /* Character of the parsed option. */
  406. int error_occurred = 0;
  407. struct gengetopt_args_info local_args_info;
  408. int override;
  409. int initialize;
  410. int check_required;
  411. int check_ambiguity;
  412. package_name = argv[0];
  413. override = params->override;
  414. initialize = params->initialize;
  415. check_required = params->check_required;
  416. check_ambiguity = params->check_ambiguity;
  417. if (initialize)
  418. cmdline_parser_init (args_info);
  419. cmdline_parser_init (&local_args_info);
  420. optarg = 0;
  421. optind = 0;
  422. opterr = params->print_errors;
  423. optopt = '?';
  424. while (1)
  425. {
  426. int option_index = 0;
  427. static struct option long_options[] = {
  428. { "blacklist-file", 1, NULL, 'b' },
  429. { "whitelist-file", 1, NULL, 'w' },
  430. { "log-file", 1, NULL, 'l' },
  431. { "verbosity", 1, NULL, 'v' },
  432. { "no-duplicate-checking", 0, NULL, 0 },
  433. { "ignore-blacklist-errors", 0, NULL, 0 },
  434. { "help", 0, NULL, 'h' },
  435. { "version", 0, NULL, 'V' },
  436. { 0, 0, 0, 0 }
  437. };
  438. c = getopt_long (argc, argv, "b:w:l:v:hV", long_options, &option_index);
  439. if (c == -1) break; /* Exit from `while (1)' loop. */
  440. switch (c)
  441. {
  442. case 'b': /* File of subnets to exclude, in CIDR notation, one-per line.. */
  443. if (update_arg( (void *)&(args_info->blacklist_file_arg),
  444. &(args_info->blacklist_file_orig), &(args_info->blacklist_file_given),
  445. &(local_args_info.blacklist_file_given), optarg, 0, 0, ARG_STRING,
  446. check_ambiguity, override, 0, 0,
  447. "blacklist-file", 'b',
  448. additional_error))
  449. goto failure;
  450. break;
  451. case 'w': /* File of subnets to include, in CIDR notation, one-per line.. */
  452. if (update_arg( (void *)&(args_info->whitelist_file_arg),
  453. &(args_info->whitelist_file_orig), &(args_info->whitelist_file_given),
  454. &(local_args_info.whitelist_file_given), optarg, 0, 0, ARG_STRING,
  455. check_ambiguity, override, 0, 0,
  456. "whitelist-file", 'w',
  457. additional_error))
  458. goto failure;
  459. break;
  460. case 'l': /* File to log to. */
  461. if (update_arg( (void *)&(args_info->log_file_arg),
  462. &(args_info->log_file_orig), &(args_info->log_file_given),
  463. &(local_args_info.log_file_given), optarg, 0, 0, ARG_STRING,
  464. check_ambiguity, override, 0, 0,
  465. "log-file", 'l',
  466. additional_error))
  467. goto failure;
  468. break;
  469. case 'v': /* Set log level verbosity (0-5, default 3). */
  470. if (update_arg( (void *)&(args_info->verbosity_arg),
  471. &(args_info->verbosity_orig), &(args_info->verbosity_given),
  472. &(local_args_info.verbosity_given), optarg, 0, "3", ARG_INT,
  473. check_ambiguity, override, 0, 0,
  474. "verbosity", 'v',
  475. additional_error))
  476. goto failure;
  477. break;
  478. case 'h': /* Print help and exit. */
  479. if (update_arg( 0 ,
  480. 0 , &(args_info->help_given),
  481. &(local_args_info.help_given), optarg, 0, 0, ARG_NO,
  482. check_ambiguity, override, 0, 0,
  483. "help", 'h',
  484. additional_error))
  485. goto failure;
  486. break;
  487. case 'V': /* Print version and exit. */
  488. if (update_arg( 0 ,
  489. 0 , &(args_info->version_given),
  490. &(local_args_info.version_given), optarg, 0, 0, ARG_NO,
  491. check_ambiguity, override, 0, 0,
  492. "version", 'V',
  493. additional_error))
  494. goto failure;
  495. break;
  496. case 0: /* Long option with no short option */
  497. /* Don't deduplicate IP addresses (default false). */
  498. if (strcmp (long_options[option_index].name, "no-duplicate-checking") == 0)
  499. {
  500. if (update_arg( 0 ,
  501. 0 , &(args_info->no_duplicate_checking_given),
  502. &(local_args_info.no_duplicate_checking_given), optarg, 0, 0, ARG_NO,
  503. check_ambiguity, override, 0, 0,
  504. "no-duplicate-checking", '-',
  505. additional_error))
  506. goto failure;
  507. }
  508. /* Ignore invalid entires in the blacklist/whitelist (default false). */
  509. else if (strcmp (long_options[option_index].name, "ignore-blacklist-errors") == 0)
  510. {
  511. if (update_arg( 0 ,
  512. 0 , &(args_info->ignore_blacklist_errors_given),
  513. &(local_args_info.ignore_blacklist_errors_given), optarg, 0, 0, ARG_NO,
  514. check_ambiguity, override, 0, 0,
  515. "ignore-blacklist-errors", '-',
  516. additional_error))
  517. goto failure;
  518. }
  519. break;
  520. case '?': /* Invalid option. */
  521. /* `getopt_long' already printed an error message. */
  522. goto failure;
  523. default: /* bug: option not considered. */
  524. fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
  525. abort ();
  526. } /* switch */
  527. } /* while */
  528. cmdline_parser_release (&local_args_info);
  529. if ( error_occurred )
  530. return (EXIT_FAILURE);
  531. return 0;
  532. failure:
  533. cmdline_parser_release (&local_args_info);
  534. return (EXIT_FAILURE);
  535. }
  536. #ifndef CONFIG_FILE_LINE_SIZE
  537. #define CONFIG_FILE_LINE_SIZE 2048
  538. #endif
  539. #define ADDITIONAL_ERROR " in configuration file "
  540. #define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)
  541. /* 3 is for "--" and "=" */
  542. static int
  543. _cmdline_parser_configfile (const char *filename, int *my_argc)
  544. {
  545. FILE* file;
  546. char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE+1];
  547. char linebuf[CONFIG_FILE_LINE_SIZE];
  548. int line_num = 0;
  549. int result = 0, equal;
  550. char *fopt, *farg;
  551. char *str_index;
  552. size_t len, next_token;
  553. char delimiter;
  554. if ((file = fopen(filename, "r")) == 0)
  555. {
  556. fprintf (stderr, "%s: Error opening configuration file '%s'\n",
  557. CMDLINE_PARSER_PACKAGE, filename);
  558. return EXIT_FAILURE;
  559. }
  560. while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != 0)
  561. {
  562. ++line_num;
  563. my_argv[0] = '\0';
  564. len = strlen(linebuf);
  565. if (len > (CONFIG_FILE_LINE_BUFFER_SIZE-1))
  566. {
  567. fprintf (stderr, "%s:%s:%d: Line too long in configuration file\n",
  568. CMDLINE_PARSER_PACKAGE, filename, line_num);
  569. result = EXIT_FAILURE;
  570. break;
  571. }
  572. /* find first non-whitespace character in the line */
  573. next_token = strspn (linebuf, " \t\r\n");
  574. str_index = linebuf + next_token;
  575. if ( str_index[0] == '\0' || str_index[0] == '#')
  576. continue; /* empty line or comment line is skipped */
  577. fopt = str_index;
  578. /* truncate fopt at the end of the first non-valid character */
  579. next_token = strcspn (fopt, " \t\r\n=");
  580. if (fopt[next_token] == '\0') /* the line is over */
  581. {
  582. farg = 0;
  583. equal = 0;
  584. goto noarg;
  585. }
  586. /* remember if equal sign is present */
  587. equal = (fopt[next_token] == '=');
  588. fopt[next_token++] = '\0';
  589. /* advance pointers to the next token after the end of fopt */
  590. next_token += strspn (fopt + next_token, " \t\r\n");
  591. /* check for the presence of equal sign, and if so, skip it */
  592. if ( !equal )
  593. if ((equal = (fopt[next_token] == '=')))
  594. {
  595. next_token++;
  596. next_token += strspn (fopt + next_token, " \t\r\n");
  597. }
  598. str_index += next_token;
  599. /* find argument */
  600. farg = str_index;
  601. if ( farg[0] == '\"' || farg[0] == '\'' )
  602. { /* quoted argument */
  603. str_index = strchr (++farg, str_index[0] ); /* skip opening quote */
  604. if (! str_index)
  605. {
  606. fprintf
  607. (stderr,
  608. "%s:%s:%d: unterminated string in configuration file\n",
  609. CMDLINE_PARSER_PACKAGE, filename, line_num);
  610. result = EXIT_FAILURE;
  611. break;
  612. }
  613. }
  614. else
  615. { /* read up the remaining part up to a delimiter */
  616. next_token = strcspn (farg, " \t\r\n#\'\"");
  617. str_index += next_token;
  618. }
  619. /* truncate farg at the delimiter and store it for further check */
  620. delimiter = *str_index, *str_index++ = '\0';
  621. /* everything but comment is illegal at the end of line */
  622. if (delimiter != '\0' && delimiter != '#')
  623. {
  624. str_index += strspn(str_index, " \t\r\n");
  625. if (*str_index != '\0' && *str_index != '#')
  626. {
  627. fprintf
  628. (stderr,
  629. "%s:%s:%d: malformed string in configuration file\n",
  630. CMDLINE_PARSER_PACKAGE, filename, line_num);
  631. result = EXIT_FAILURE;
  632. break;
  633. }
  634. }
  635. noarg:
  636. if (!strcmp(fopt,"include")) {
  637. if (farg && *farg) {
  638. result = _cmdline_parser_configfile(farg, my_argc);
  639. } else {
  640. fprintf(stderr, "%s:%s:%d: include requires a filename argument.\n",
  641. CMDLINE_PARSER_PACKAGE, filename, line_num);
  642. }
  643. continue;
  644. }
  645. len = strlen(fopt);
  646. strcat (my_argv, len > 1 ? "--" : "-");
  647. strcat (my_argv, fopt);
  648. if (len > 1 && ((farg && *farg) || equal))
  649. strcat (my_argv, "=");
  650. if (farg && *farg)
  651. strcat (my_argv, farg);
  652. ++(*my_argc);
  653. cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
  654. cmd_line_list_tmp->next = cmd_line_list;
  655. cmd_line_list = cmd_line_list_tmp;
  656. cmd_line_list->string_arg = gengetopt_strdup(my_argv);
  657. } /* while */
  658. if (file)
  659. fclose(file);
  660. return result;
  661. }
  662. int
  663. cmdline_parser_configfile (
  664. const char *filename,
  665. struct gengetopt_args_info *args_info,
  666. int override, int initialize, int check_required)
  667. {
  668. struct cmdline_parser_params params;
  669. params.override = override;
  670. params.initialize = initialize;
  671. params.check_required = check_required;
  672. params.check_ambiguity = 0;
  673. params.print_errors = 1;
  674. return cmdline_parser_config_file (filename, args_info, &params);
  675. }
  676. int
  677. cmdline_parser_config_file (const char *filename,
  678. struct gengetopt_args_info *args_info,
  679. struct cmdline_parser_params *params)
  680. {
  681. int i, result;
  682. int my_argc = 1;
  683. char **my_argv_arg;
  684. char *additional_error;
  685. /* store the program name */
  686. cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
  687. cmd_line_list_tmp->next = cmd_line_list;
  688. cmd_line_list = cmd_line_list_tmp;
  689. cmd_line_list->string_arg = gengetopt_strdup (CMDLINE_PARSER_PACKAGE);
  690. result = _cmdline_parser_configfile(filename, &my_argc);
  691. if (result != EXIT_FAILURE) {
  692. my_argv_arg = (char **) malloc((my_argc+1) * sizeof(char *));
  693. cmd_line_list_tmp = cmd_line_list;
  694. for (i = my_argc - 1; i >= 0; --i) {
  695. my_argv_arg[i] = cmd_line_list_tmp->string_arg;
  696. cmd_line_list_tmp = cmd_line_list_tmp->next;
  697. }
  698. my_argv_arg[my_argc] = 0;
  699. additional_error = (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) + 1);
  700. strcpy (additional_error, ADDITIONAL_ERROR);
  701. strcat (additional_error, filename);
  702. result =
  703. cmdline_parser_internal (my_argc, my_argv_arg, args_info,
  704. params,
  705. additional_error);
  706. free (additional_error);
  707. free (my_argv_arg);
  708. }
  709. free_cmd_list();
  710. if (result == EXIT_FAILURE)
  711. {
  712. cmdline_parser_free (args_info);
  713. exit (EXIT_FAILURE);
  714. }
  715. return result;
  716. }