/* File autogenerated by gengetopt generated with the following command: ../src/gengetopt --gen-version --input=test_conf_parser_cmd.ggoconf --func-name=test_conf_parser_cmd_parser --file-name=test_conf_parser_cmd --unamed-opt=OTHER NAMES --conf-parser --arg-struct-name=my_args_info The developers of gengetopt consider the fixed text that goes in all gengetopt output files to be in the public domain: we make no copyright claims on it. */ /* If we use autoconf. */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include #include #include #include "getopt.h" #include "test_conf_parser_cmd.h" const char *my_args_info_purpose = ""; const char *my_args_info_usage = "Usage: test_conf_parser [OPTIONS]... [OTHER NAMES]..."; const char *my_args_info_description = ""; const char *my_args_info_help[] = { " -h, --help Print help and exit", " -V, --version Print version and exit", "\n Group: grp1\n an option of this group is required", " -a, --opta=STRING string a", " -b, --optb=STRING string b", "\n Group: my grp2", " --optc string c", " -C, --optd string d", " -s, --string=STRING string option", " -i, --int=INT int option", " -S, --short=SHORT short option", " -l, --long=LONG long option", " -f, --float=FLOAT float option", " -d, --double=DOUBLE double option", " -D, --longdouble=LONGDOUBLE longdouble option", " -L, --longlong=LONGLONG longlong option", " -F, --flag flag option (default=on)", " -u, --function function option", " --no-short no short option", " -r, --required=STRING required option", " -c, --conf-file=STRING config file (default=`test_conf.conf')", " -M, --multi-string=STRING multiple string option", " --multi-string-def=STRING multiple string option with default \n (default=`this default')", " -n, --noarg multiple option with no arg", " --noarg-noshort multiple option with no arg and no short", " --opt-arg[=STRING] option with optional arg", " --file-save=STRING save the passed options into a file", 0 }; static void clear_given (struct my_args_info *args_info); static void clear_args (struct my_args_info *args_info); static int test_conf_parser_cmd_parser_internal (int argc, char * const *argv, struct my_args_info *args_info, int override, int initialize, int check_required, const char *additional_error); static int test_conf_parser_cmd_parser_required2 (struct my_args_info *args_info, const char *prog_name, const char *additional_error); struct line_list { char * string_arg; struct line_list * next; }; static struct line_list *cmd_line_list = 0; static struct line_list *cmd_line_list_tmp = 0; static void free_cmd_list(void) { /* free the list of a previous call */ if (cmd_line_list) { while (cmd_line_list) { cmd_line_list_tmp = cmd_line_list; cmd_line_list = cmd_line_list->next; free (cmd_line_list_tmp->string_arg); free (cmd_line_list_tmp); } } } struct string_list { char * arg; char *orig; struct string_list * next; }; static char * gengetopt_strdup (const char *s); static void clear_given (struct my_args_info *args_info) { args_info->help_given = 0 ; args_info->version_given = 0 ; args_info->opta_given = 0 ; args_info->optb_given = 0 ; args_info->optc_given = 0 ; args_info->optd_given = 0 ; args_info->string_given = 0 ; args_info->int_given = 0 ; args_info->short_given = 0 ; args_info->long_given = 0 ; args_info->float_given = 0 ; args_info->double_given = 0 ; args_info->longdouble_given = 0 ; args_info->longlong_given = 0 ; args_info->flag_given = 0 ; args_info->function_given = 0 ; args_info->no_short_given = 0 ; args_info->required_given = 0 ; args_info->conf_file_given = 0 ; args_info->multi_string_given = 0 ; args_info->multi_string_def_given = 0 ; args_info->noarg_given = 0 ; args_info->noarg_noshort_given = 0 ; args_info->opt_arg_given = 0 ; args_info->file_save_given = 0 ; args_info->grp1_group_counter = 0 ; args_info->my_grp2_group_counter = 0 ; } static void clear_args (struct my_args_info *args_info) { args_info->opta_arg = NULL; args_info->opta_orig = NULL; args_info->optb_arg = NULL; args_info->optb_orig = NULL; args_info->string_arg = NULL; args_info->string_orig = NULL; args_info->int_orig = NULL; args_info->short_orig = NULL; args_info->long_orig = NULL; args_info->float_orig = NULL; args_info->double_orig = NULL; args_info->longdouble_orig = NULL; args_info->longlong_orig = NULL; args_info->flag_flag = 1; args_info->required_arg = NULL; args_info->required_orig = NULL; args_info->conf_file_arg = gengetopt_strdup ("test_conf.conf"); args_info->conf_file_orig = NULL; args_info->multi_string_arg = NULL; args_info->multi_string_orig = NULL; args_info->multi_string_def_arg = NULL; args_info->multi_string_def_orig = NULL; args_info->opt_arg_arg = NULL; args_info->opt_arg_orig = NULL; args_info->file_save_arg = NULL; args_info->file_save_orig = NULL; } static void init_args_info(struct my_args_info *args_info) { args_info->help_help = my_args_info_help[0] ; args_info->version_help = my_args_info_help[1] ; args_info->opta_help = my_args_info_help[2] ; args_info->optb_help = my_args_info_help[3] ; args_info->optc_help = my_args_info_help[4] ; args_info->optd_help = my_args_info_help[5] ; args_info->string_help = my_args_info_help[6] ; args_info->int_help = my_args_info_help[7] ; args_info->short_help = my_args_info_help[8] ; args_info->long_help = my_args_info_help[9] ; args_info->float_help = my_args_info_help[10] ; args_info->double_help = my_args_info_help[11] ; args_info->longdouble_help = my_args_info_help[12] ; args_info->longlong_help = my_args_info_help[13] ; args_info->flag_help = my_args_info_help[14] ; args_info->function_help = my_args_info_help[15] ; args_info->no_short_help = my_args_info_help[16] ; args_info->required_help = my_args_info_help[17] ; args_info->conf_file_help = my_args_info_help[18] ; args_info->multi_string_help = my_args_info_help[19] ; args_info->multi_string_min = -1; args_info->multi_string_max = -1; args_info->multi_string_def_help = my_args_info_help[20] ; args_info->multi_string_def_min = -1; args_info->multi_string_def_max = -1; args_info->noarg_help = my_args_info_help[21] ; args_info->noarg_min = -1; args_info->noarg_max = -1; args_info->noarg_noshort_help = my_args_info_help[22] ; args_info->noarg_noshort_min = -1; args_info->noarg_noshort_max = -1; args_info->opt_arg_help = my_args_info_help[23] ; args_info->file_save_help = my_args_info_help[24] ; } void test_conf_parser_cmd_parser_print_version (void) { printf ("%s %s\n", TEST_CONF_PARSER_CMD_PARSER_PACKAGE, TEST_CONF_PARSER_CMD_PARSER_VERSION); } void test_conf_parser_cmd_parser_print_help (void) { int i = 0; test_conf_parser_cmd_parser_print_version (); if (strlen(my_args_info_purpose) > 0) printf("\n%s\n", my_args_info_purpose); printf("\n%s\n\n", my_args_info_usage); if (strlen(my_args_info_description) > 0) printf("%s\n", my_args_info_description); while (my_args_info_help[i]) printf("%s\n", my_args_info_help[i++]); } void test_conf_parser_cmd_parser_init (struct my_args_info *args_info) { clear_given (args_info); clear_args (args_info); init_args_info (args_info); args_info->inputs = NULL; args_info->inputs_num = 0; } static void test_conf_parser_cmd_parser_release (struct my_args_info *args_info) { unsigned int i; if (args_info->opta_arg) { free (args_info->opta_arg); /* free previous argument */ args_info->opta_arg = 0; } if (args_info->opta_orig) { free (args_info->opta_orig); /* free previous argument */ args_info->opta_orig = 0; } if (args_info->optb_arg) { free (args_info->optb_arg); /* free previous argument */ args_info->optb_arg = 0; } if (args_info->optb_orig) { free (args_info->optb_orig); /* free previous argument */ args_info->optb_orig = 0; } if (args_info->string_arg) { free (args_info->string_arg); /* free previous argument */ args_info->string_arg = 0; } if (args_info->string_orig) { free (args_info->string_orig); /* free previous argument */ args_info->string_orig = 0; } if (args_info->int_orig) { free (args_info->int_orig); /* free previous argument */ args_info->int_orig = 0; } if (args_info->short_orig) { free (args_info->short_orig); /* free previous argument */ args_info->short_orig = 0; } if (args_info->long_orig) { free (args_info->long_orig); /* free previous argument */ args_info->long_orig = 0; } if (args_info->float_orig) { free (args_info->float_orig); /* free previous argument */ args_info->float_orig = 0; } if (args_info->double_orig) { free (args_info->double_orig); /* free previous argument */ args_info->double_orig = 0; } if (args_info->longdouble_orig) { free (args_info->longdouble_orig); /* free previous argument */ args_info->longdouble_orig = 0; } if (args_info->longlong_orig) { free (args_info->longlong_orig); /* free previous argument */ args_info->longlong_orig = 0; } if (args_info->required_arg) { free (args_info->required_arg); /* free previous argument */ args_info->required_arg = 0; } if (args_info->required_orig) { free (args_info->required_orig); /* free previous argument */ args_info->required_orig = 0; } if (args_info->conf_file_arg) { free (args_info->conf_file_arg); /* free previous argument */ args_info->conf_file_arg = 0; } if (args_info->conf_file_orig) { free (args_info->conf_file_orig); /* free previous argument */ args_info->conf_file_orig = 0; } if (args_info->multi_string_arg) { for (i = 0; i < args_info->multi_string_given; ++i) { if (args_info->multi_string_arg [i]) { free (args_info->multi_string_arg [i]); /* free previous argument */ args_info->multi_string_arg [i] = 0; } if (args_info->multi_string_orig [i]) { free (args_info->multi_string_orig [i]); /* free previous argument */ args_info->multi_string_orig [i] = 0; } } if (args_info->multi_string_arg [0]) free (args_info->multi_string_arg [0]); /* free default string */ free (args_info->multi_string_arg); /* free previous argument */ args_info->multi_string_arg = 0; free (args_info->multi_string_orig); /* free previous argument */ args_info->multi_string_orig = 0; } if (args_info->multi_string_def_arg) { for (i = 0; i < args_info->multi_string_def_given; ++i) { if (args_info->multi_string_def_arg [i]) { free (args_info->multi_string_def_arg [i]); /* free previous argument */ args_info->multi_string_def_arg [i] = 0; } if (args_info->multi_string_def_orig [i]) { free (args_info->multi_string_def_orig [i]); /* free previous argument */ args_info->multi_string_def_orig [i] = 0; } } if (args_info->multi_string_def_arg [0]) free (args_info->multi_string_def_arg [0]); /* free default string */ free (args_info->multi_string_def_arg); /* free previous argument */ args_info->multi_string_def_arg = 0; free (args_info->multi_string_def_orig); /* free previous argument */ args_info->multi_string_def_orig = 0; } if (args_info->opt_arg_arg) { free (args_info->opt_arg_arg); /* free previous argument */ args_info->opt_arg_arg = 0; } if (args_info->opt_arg_orig) { free (args_info->opt_arg_orig); /* free previous argument */ args_info->opt_arg_orig = 0; } if (args_info->file_save_arg) { free (args_info->file_save_arg); /* free previous argument */ args_info->file_save_arg = 0; } if (args_info->file_save_orig) { free (args_info->file_save_orig); /* free previous argument */ args_info->file_save_orig = 0; } for (i = 0; i < args_info->inputs_num; ++i) free (args_info->inputs [i]); if (args_info->inputs_num) free (args_info->inputs); clear_given (args_info); } int test_conf_parser_cmd_parser_file_save(const char *filename, struct my_args_info *args_info) { FILE *outfile; int i = 0; outfile = fopen(filename, "w"); if (!outfile) { fprintf (stderr, "%s: cannot open file for writing: %s\n", TEST_CONF_PARSER_CMD_PARSER_PACKAGE, filename); return EXIT_FAILURE; } if (args_info->help_given) { fprintf(outfile, "%s\n", "help"); } if (args_info->version_given) { fprintf(outfile, "%s\n", "version"); } if (args_info->opta_given) { if (args_info->opta_orig) { fprintf(outfile, "%s=\"%s\"\n", "opta", args_info->opta_orig); } else { fprintf(outfile, "%s\n", "opta"); } } if (args_info->optb_given) { if (args_info->optb_orig) { fprintf(outfile, "%s=\"%s\"\n", "optb", args_info->optb_orig); } else { fprintf(outfile, "%s\n", "optb"); } } if (args_info->optc_given) { fprintf(outfile, "%s\n", "optc"); } if (args_info->optd_given) { fprintf(outfile, "%s\n", "optd"); } if (args_info->string_given) { if (args_info->string_orig) { fprintf(outfile, "%s=\"%s\"\n", "string", args_info->string_orig); } else { fprintf(outfile, "%s\n", "string"); } } if (args_info->int_given) { if (args_info->int_orig) { fprintf(outfile, "%s=\"%s\"\n", "int", args_info->int_orig); } else { fprintf(outfile, "%s\n", "int"); } } if (args_info->short_given) { if (args_info->short_orig) { fprintf(outfile, "%s=\"%s\"\n", "short", args_info->short_orig); } else { fprintf(outfile, "%s\n", "short"); } } if (args_info->long_given) { if (args_info->long_orig) { fprintf(outfile, "%s=\"%s\"\n", "long", args_info->long_orig); } else { fprintf(outfile, "%s\n", "long"); } } if (args_info->float_given) { if (args_info->float_orig) { fprintf(outfile, "%s=\"%s\"\n", "float", args_info->float_orig); } else { fprintf(outfile, "%s\n", "float"); } } if (args_info->double_given) { if (args_info->double_orig) { fprintf(outfile, "%s=\"%s\"\n", "double", args_info->double_orig); } else { fprintf(outfile, "%s\n", "double"); } } if (args_info->longdouble_given) { if (args_info->longdouble_orig) { fprintf(outfile, "%s=\"%s\"\n", "longdouble", args_info->longdouble_orig); } else { fprintf(outfile, "%s\n", "longdouble"); } } if (args_info->longlong_given) { if (args_info->longlong_orig) { fprintf(outfile, "%s=\"%s\"\n", "longlong", args_info->longlong_orig); } else { fprintf(outfile, "%s\n", "longlong"); } } if (args_info->flag_given) { fprintf(outfile, "%s\n", "flag"); } if (args_info->function_given) { fprintf(outfile, "%s\n", "function"); } if (args_info->no_short_given) { fprintf(outfile, "%s\n", "no-short"); } if (args_info->required_given) { if (args_info->required_orig) { fprintf(outfile, "%s=\"%s\"\n", "required", args_info->required_orig); } else { fprintf(outfile, "%s\n", "required"); } } if (args_info->conf_file_given) { if (args_info->conf_file_orig) { fprintf(outfile, "%s=\"%s\"\n", "conf-file", args_info->conf_file_orig); } else { fprintf(outfile, "%s\n", "conf-file"); } } if (args_info->multi_string_orig) { for (i = 0; i < args_info->multi_string_given; ++i) { if (args_info->multi_string_orig [i]) { fprintf(outfile, "%s=\"%s\"\n", "multi-string", args_info->multi_string_orig [i]); } } } if (args_info->multi_string_def_orig) { for (i = 0; i < args_info->multi_string_def_given; ++i) { if (args_info->multi_string_def_orig [i]) { fprintf(outfile, "%s=\"%s\"\n", "multi-string-def", args_info->multi_string_def_orig [i]); } } } for (i = 0; i < args_info->noarg_given; ++i) { fprintf(outfile, "%s\n", "noarg"); } for (i = 0; i < args_info->noarg_noshort_given; ++i) { fprintf(outfile, "%s\n", "noarg-noshort"); } if (args_info->opt_arg_given) { if (args_info->opt_arg_orig) { fprintf(outfile, "%s=\"%s\"\n", "opt-arg", args_info->opt_arg_orig); } else { fprintf(outfile, "%s\n", "opt-arg"); } } if (args_info->file_save_given) { if (args_info->file_save_orig) { fprintf(outfile, "%s=\"%s\"\n", "file-save", args_info->file_save_orig); } else { fprintf(outfile, "%s\n", "file-save"); } } fclose (outfile); i = EXIT_SUCCESS; return i; } void test_conf_parser_cmd_parser_free (struct my_args_info *args_info) { test_conf_parser_cmd_parser_release (args_info); } /* gengetopt_strdup() */ /* strdup.c replacement of strdup, which is not standard */ char * gengetopt_strdup (const char *s) { char *result = NULL; if (!s) return result; result = (char*)malloc(strlen(s) + 1); if (result == (char*)0) return (char*)0; strcpy(result, s); return result; } static char * get_multiple_arg_token(const char *arg) { char *tok, *ret; size_t len, num_of_escape; int i, j; if (!arg) return NULL; tok = strchr (arg, ','); num_of_escape = 0; /* make sure it is not escaped */ while (tok) { if (*(tok-1) == '\\') { /* find the next one */ tok = strchr (tok+1, ','); ++num_of_escape; } else break; } if (tok) len = (size_t)(tok - arg + 1); else len = strlen (arg) + 1; len -= num_of_escape; ret = (char *) malloc (len); i = 0; j = 0; while (arg[i] && (j < len-1)) { if (arg[i] == '\\') ++i; ret[j++] = arg[i++]; } ret[len-1] = '\0'; return ret; } static char * get_multiple_arg_token_next(const char *arg) { char *tok; if (!arg) return NULL; tok = strchr (arg, ','); /* make sure it is not escaped */ while (tok) { if (*(tok-1) == '\\') { /* find the next one */ tok = strchr (tok+1, ','); } else break; } if (! tok || strlen(tok) == 1) return 0; return tok+1; } static int check_multiple_option_occurrences(const char *prog_name, unsigned int option_given, int min, int max, const char *option_desc); int check_multiple_option_occurrences(const char *prog_name, unsigned int option_given, int min, int max, const char *option_desc) { int error = 0; if (option_given && ! (min < 0 && max < 0)) { if (min >= 0 && max >= 0) { if (min == max) { /* specific occurrences */ if (option_given != min) { fprintf (stderr, "%s: %s option occurrences must be %d\n", prog_name, option_desc, min); error = 1; } } else if (option_given < min || option_given > max) { /* range occurrences */ fprintf (stderr, "%s: %s option occurrences must be between %d and %d\n", prog_name, option_desc, min, max); error = 1; } } else if (min >= 0) { /* at least check */ if (option_given < min) { fprintf (stderr, "%s: %s option occurrences must be at least %d\n", prog_name, option_desc, min); error = 1; } } else if (max >= 0) { /* at most check */ if (option_given > max) { fprintf (stderr, "%s: %s option occurrences must be at most %d\n", prog_name, option_desc, max); error = 1; } } } return error; } static void reset_group_grp1(struct my_args_info *args_info); static void reset_group_grp1(struct my_args_info *args_info) { if (! args_info->grp1_group_counter) return; args_info->opta_given = 0 ; if (args_info->opta_arg) { free (args_info->opta_arg); /* free previous argument */ args_info->opta_arg = 0; } if (args_info->opta_orig) { free (args_info->opta_orig); /* free previous argument */ args_info->opta_orig = 0; } args_info->optb_given = 0 ; if (args_info->optb_arg) { free (args_info->optb_arg); /* free previous argument */ args_info->optb_arg = 0; } if (args_info->optb_orig) { free (args_info->optb_orig); /* free previous argument */ args_info->optb_orig = 0; } args_info->grp1_group_counter = 0; } static void reset_group_my_grp2(struct my_args_info *args_info); static void reset_group_my_grp2(struct my_args_info *args_info) { if (! args_info->my_grp2_group_counter) return; args_info->optc_given = 0 ; args_info->optd_given = 0 ; args_info->my_grp2_group_counter = 0; } int test_conf_parser_cmd_parser (int argc, char * const *argv, struct my_args_info *args_info) { return test_conf_parser_cmd_parser2 (argc, argv, args_info, 0, 1, 1); } int test_conf_parser_cmd_parser2 (int argc, char * const *argv, struct my_args_info *args_info, int override, int initialize, int check_required) { int result; result = test_conf_parser_cmd_parser_internal (argc, argv, args_info, override, initialize, check_required, NULL); if (result == EXIT_FAILURE) { test_conf_parser_cmd_parser_free (args_info); exit (EXIT_FAILURE); } return result; } int test_conf_parser_cmd_parser_required (struct my_args_info *args_info, const char *prog_name) { int result = EXIT_SUCCESS; if (test_conf_parser_cmd_parser_required2(args_info, prog_name, NULL) > 0) result = EXIT_FAILURE; if (result == EXIT_FAILURE) { test_conf_parser_cmd_parser_free (args_info); exit (EXIT_FAILURE); } return result; } int test_conf_parser_cmd_parser_required2 (struct my_args_info *args_info, const char *prog_name, const char *additional_error) { int error = 0; /* checks for required options */ if (! args_info->required_given) { fprintf (stderr, "%s: '--required' ('-r') option required%s\n", prog_name, (additional_error ? additional_error : "")); error = 1; } if (check_multiple_option_occurrences(prog_name, args_info->multi_string_given, args_info->multi_string_min, args_info->multi_string_max, "'--multi-string' ('-M')")) error = 1; if (check_multiple_option_occurrences(prog_name, args_info->multi_string_def_given, args_info->multi_string_def_min, args_info->multi_string_def_max, "'--multi-string-def'")) error = 1; if (check_multiple_option_occurrences(prog_name, args_info->noarg_given, args_info->noarg_min, args_info->noarg_max, "'--noarg' ('-n')")) error = 1; if (check_multiple_option_occurrences(prog_name, args_info->noarg_noshort_given, args_info->noarg_noshort_min, args_info->noarg_noshort_max, "'--noarg-noshort'")) error = 1; if (args_info->grp1_group_counter == 0) { fprintf (stderr, "%s: %d options of group grp1 were given. One is required.%s\n", prog_name, args_info->grp1_group_counter, (additional_error ? additional_error : "")); error = 1; } /* checks for dependences among options */ return error; } int test_conf_parser_cmd_parser_internal (int argc, char * const *argv, struct my_args_info *args_info, int override, int initialize, int check_required, const char *additional_error) { int c; /* Character of the parsed option. */ char *multi_token, *multi_next; /* for multiple options */ int i; /* Counter */ struct string_list * multi_string_list = NULL,* multi_string_new = NULL; struct string_list * multi_string_def_list = NULL,* multi_string_def_new = NULL; int error = 0; struct my_args_info local_args_info; if (initialize) test_conf_parser_cmd_parser_init (args_info); test_conf_parser_cmd_parser_init (&local_args_info); optarg = 0; optind = 0; opterr = 1; optopt = '?'; while (1) { int option_index = 0; char *stop_char; static struct option long_options[] = { { "help", 0, NULL, 'h' }, { "version", 0, NULL, 'V' }, { "opta", 1, NULL, 'a' }, { "optb", 1, NULL, 'b' }, { "optc", 0, NULL, 0 }, { "optd", 0, NULL, 'C' }, { "string", 1, NULL, 's' }, { "int", 1, NULL, 'i' }, { "short", 1, NULL, 'S' }, { "long", 1, NULL, 'l' }, { "float", 1, NULL, 'f' }, { "double", 1, NULL, 'd' }, { "longdouble", 1, NULL, 'D' }, { "longlong", 1, NULL, 'L' }, { "flag", 0, NULL, 'F' }, { "function", 0, NULL, 'u' }, { "no-short", 0, NULL, 0 }, { "required", 1, NULL, 'r' }, { "conf-file", 1, NULL, 'c' }, { "multi-string", 1, NULL, 'M' }, { "multi-string-def", 1, NULL, 0 }, { "noarg", 0, NULL, 'n' }, { "noarg-noshort", 0, NULL, 0 }, { "opt-arg", 2, NULL, 0 }, { "file-save", 1, NULL, 0 }, { NULL, 0, NULL, 0 } }; stop_char = 0; c = getopt_long (argc, argv, "hVa:b:Cs:i:S:l:f:d:D:L:Fur:c:M:n", long_options, &option_index); if (c == -1) break; /* Exit from `while (1)' loop. */ switch (c) { case 'h': /* Print help and exit. */ test_conf_parser_cmd_parser_print_help (); test_conf_parser_cmd_parser_free (&local_args_info); exit (EXIT_SUCCESS); case 'V': /* Print version and exit. */ test_conf_parser_cmd_parser_print_version (); test_conf_parser_cmd_parser_free (&local_args_info); exit (EXIT_SUCCESS); case 'a': /* string a. */ if (local_args_info.opta_given) { fprintf (stderr, "%s: `--opta' (`-a') option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); goto failure; } if (args_info->opta_given && ! override) continue; local_args_info.opta_given = 1; args_info->opta_given = 1; if (args_info->grp1_group_counter && override) reset_group_grp1 (args_info); args_info->grp1_group_counter += 1; if (args_info->opta_arg) free (args_info->opta_arg); /* free previous string */ args_info->opta_arg = gengetopt_strdup (optarg); if (args_info->opta_orig) free (args_info->opta_orig); /* free previous string */ args_info->opta_orig = gengetopt_strdup (optarg); break; case 'b': /* string b. */ if (local_args_info.optb_given) { fprintf (stderr, "%s: `--optb' (`-b') option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); goto failure; } if (args_info->optb_given && ! override) continue; local_args_info.optb_given = 1; args_info->optb_given = 1; if (args_info->grp1_group_counter && override) reset_group_grp1 (args_info); args_info->grp1_group_counter += 1; if (args_info->optb_arg) free (args_info->optb_arg); /* free previous string */ args_info->optb_arg = gengetopt_strdup (optarg); if (args_info->optb_orig) free (args_info->optb_orig); /* free previous string */ args_info->optb_orig = gengetopt_strdup (optarg); break; case 'C': /* string d. */ if (local_args_info.optd_given) { fprintf (stderr, "%s: `--optd' (`-C') option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); goto failure; } if (args_info->optd_given && ! override) continue; local_args_info.optd_given = 1; args_info->optd_given = 1; if (args_info->my_grp2_group_counter && override) reset_group_my_grp2 (args_info); args_info->my_grp2_group_counter += 1; break; case 's': /* string option. */ if (local_args_info.string_given) { fprintf (stderr, "%s: `--string' (`-s') option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); goto failure; } if (args_info->string_given && ! override) continue; local_args_info.string_given = 1; args_info->string_given = 1; if (args_info->string_arg) free (args_info->string_arg); /* free previous string */ args_info->string_arg = gengetopt_strdup (optarg); if (args_info->string_orig) free (args_info->string_orig); /* free previous string */ args_info->string_orig = gengetopt_strdup (optarg); break; case 'i': /* int option. */ if (local_args_info.int_given) { fprintf (stderr, "%s: `--int' (`-i') option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); goto failure; } if (args_info->int_given && ! override) continue; local_args_info.int_given = 1; args_info->int_given = 1; args_info->int_arg = strtol (optarg, &stop_char, 0); if (!(stop_char && *stop_char == '\0')) { fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg); goto failure; } if (args_info->int_orig) free (args_info->int_orig); /* free previous string */ args_info->int_orig = gengetopt_strdup (optarg); break; case 'S': /* short option. */ if (local_args_info.short_given) { fprintf (stderr, "%s: `--short' (`-S') option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); goto failure; } if (args_info->short_given && ! override) continue; local_args_info.short_given = 1; args_info->short_given = 1; args_info->short_arg = (short)strtol (optarg, &stop_char, 0); if (!(stop_char && *stop_char == '\0')) { fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg); goto failure; } if (args_info->short_orig) free (args_info->short_orig); /* free previous string */ args_info->short_orig = gengetopt_strdup (optarg); break; case 'l': /* long option. */ if (local_args_info.long_given) { fprintf (stderr, "%s: `--long' (`-l') option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); goto failure; } if (args_info->long_given && ! override) continue; local_args_info.long_given = 1; args_info->long_given = 1; args_info->long_arg = strtol (optarg, &stop_char, 0); if (!(stop_char && *stop_char == '\0')) { fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg); goto failure; } if (args_info->long_orig) free (args_info->long_orig); /* free previous string */ args_info->long_orig = gengetopt_strdup (optarg); break; case 'f': /* float option. */ if (local_args_info.float_given) { fprintf (stderr, "%s: `--float' (`-f') option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); goto failure; } if (args_info->float_given && ! override) continue; local_args_info.float_given = 1; args_info->float_given = 1; args_info->float_arg = (float)strtod (optarg, &stop_char); if (!(stop_char && *stop_char == '\0')) { fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg); goto failure; } if (args_info->float_orig) free (args_info->float_orig); /* free previous string */ args_info->float_orig = gengetopt_strdup (optarg); break; case 'd': /* double option. */ if (local_args_info.double_given) { fprintf (stderr, "%s: `--double' (`-d') option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); goto failure; } if (args_info->double_given && ! override) continue; local_args_info.double_given = 1; args_info->double_given = 1; args_info->double_arg = strtod (optarg, &stop_char); if (!(stop_char && *stop_char == '\0')) { fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg); goto failure; } if (args_info->double_orig) free (args_info->double_orig); /* free previous string */ args_info->double_orig = gengetopt_strdup (optarg); break; case 'D': /* longdouble option. */ if (local_args_info.longdouble_given) { fprintf (stderr, "%s: `--longdouble' (`-D') option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); goto failure; } if (args_info->longdouble_given && ! override) continue; local_args_info.longdouble_given = 1; args_info->longdouble_given = 1; args_info->longdouble_arg = (long double)strtod (optarg, &stop_char); if (!(stop_char && *stop_char == '\0')) { fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg); goto failure; } if (args_info->longdouble_orig) free (args_info->longdouble_orig); /* free previous string */ args_info->longdouble_orig = gengetopt_strdup (optarg); break; case 'L': /* longlong option. */ if (local_args_info.longlong_given) { fprintf (stderr, "%s: `--longlong' (`-L') option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); goto failure; } if (args_info->longlong_given && ! override) continue; local_args_info.longlong_given = 1; args_info->longlong_given = 1; #ifdef HAVE_LONG_LONG args_info->longlong_arg = (long long int) strtol (optarg, &stop_char, 0); #else args_info->longlong_arg = (long) strtol (optarg, &stop_char, 0); #endif if (!(stop_char && *stop_char == '\0')) { fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg); goto failure; } if (args_info->longlong_orig) free (args_info->longlong_orig); /* free previous string */ args_info->longlong_orig = gengetopt_strdup (optarg); break; case 'F': /* flag option. */ if (local_args_info.flag_given) { fprintf (stderr, "%s: `--flag' (`-F') option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); goto failure; } if (args_info->flag_given && ! override) continue; local_args_info.flag_given = 1; args_info->flag_given = 1; args_info->flag_flag = !(args_info->flag_flag); break; case 'u': /* function option. */ if (local_args_info.function_given) { fprintf (stderr, "%s: `--function' (`-u') option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); goto failure; } if (args_info->function_given && ! override) continue; local_args_info.function_given = 1; args_info->function_given = 1; break; case 'r': /* required option. */ if (local_args_info.required_given) { fprintf (stderr, "%s: `--required' (`-r') option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); goto failure; } if (args_info->required_given && ! override) continue; local_args_info.required_given = 1; args_info->required_given = 1; if (args_info->required_arg) free (args_info->required_arg); /* free previous string */ args_info->required_arg = gengetopt_strdup (optarg); if (args_info->required_orig) free (args_info->required_orig); /* free previous string */ args_info->required_orig = gengetopt_strdup (optarg); break; case 'c': /* config file. */ if (local_args_info.conf_file_given) { fprintf (stderr, "%s: `--conf-file' (`-c') option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); goto failure; } if (args_info->conf_file_given && ! override) continue; local_args_info.conf_file_given = 1; args_info->conf_file_given = 1; if (args_info->conf_file_arg) free (args_info->conf_file_arg); /* free previous string */ args_info->conf_file_arg = gengetopt_strdup (optarg); if (args_info->conf_file_orig) free (args_info->conf_file_orig); /* free previous string */ args_info->conf_file_orig = gengetopt_strdup (optarg); break; case 'M': /* multiple string option. */ local_args_info.multi_string_given++; multi_token = get_multiple_arg_token(optarg); multi_next = get_multiple_arg_token_next (optarg); while (1) { multi_string_new = (struct string_list *) malloc (sizeof (struct string_list)); multi_string_new->next = multi_string_list; multi_string_list = multi_string_new; multi_string_new->arg = gengetopt_strdup (multi_token); multi_string_new->orig = multi_token; if (multi_next) { multi_token = get_multiple_arg_token(multi_next); multi_next = get_multiple_arg_token_next (multi_next); local_args_info.multi_string_given++; } else break; } break; case 'n': /* multiple option with no arg. */ local_args_info.noarg_given++; break; case 0: /* Long option with no short option */ /* string c. */ if (strcmp (long_options[option_index].name, "optc") == 0) { if (local_args_info.optc_given) { fprintf (stderr, "%s: `--optc' option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); goto failure; } if (args_info->optc_given && ! override) continue; local_args_info.optc_given = 1; args_info->optc_given = 1; if (args_info->my_grp2_group_counter && override) reset_group_my_grp2 (args_info); args_info->my_grp2_group_counter += 1; break; } /* no short option. */ else if (strcmp (long_options[option_index].name, "no-short") == 0) { if (local_args_info.no_short_given) { fprintf (stderr, "%s: `--no-short' option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); goto failure; } if (args_info->no_short_given && ! override) continue; local_args_info.no_short_given = 1; args_info->no_short_given = 1; break; } /* multiple string option with default. */ else if (strcmp (long_options[option_index].name, "multi-string-def") == 0) { local_args_info.multi_string_def_given++; multi_token = get_multiple_arg_token(optarg); multi_next = get_multiple_arg_token_next (optarg); while (1) { multi_string_def_new = (struct string_list *) malloc (sizeof (struct string_list)); multi_string_def_new->next = multi_string_def_list; multi_string_def_list = multi_string_def_new; multi_string_def_new->arg = gengetopt_strdup (multi_token); multi_string_def_new->orig = multi_token; if (multi_next) { multi_token = get_multiple_arg_token(multi_next); multi_next = get_multiple_arg_token_next (multi_next); local_args_info.multi_string_def_given++; } else break; } break; } /* multiple option with no arg and no short. */ else if (strcmp (long_options[option_index].name, "noarg-noshort") == 0) { local_args_info.noarg_noshort_given++; break; } /* option with optional arg. */ else if (strcmp (long_options[option_index].name, "opt-arg") == 0) { if (local_args_info.opt_arg_given) { fprintf (stderr, "%s: `--opt-arg' option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); goto failure; } if (args_info->opt_arg_given && ! override) continue; local_args_info.opt_arg_given = 1; args_info->opt_arg_given = 1; if (optarg) { if (args_info->opt_arg_arg) free (args_info->opt_arg_arg); /* free previous string */ args_info->opt_arg_arg = gengetopt_strdup (optarg); if (args_info->opt_arg_orig) free (args_info->opt_arg_orig); /* free previous string */ args_info->opt_arg_orig = gengetopt_strdup (optarg); } } /* save the passed options into a file. */ else if (strcmp (long_options[option_index].name, "file-save") == 0) { if (local_args_info.file_save_given) { fprintf (stderr, "%s: `--file-save' option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); goto failure; } if (args_info->file_save_given && ! override) continue; local_args_info.file_save_given = 1; args_info->file_save_given = 1; if (args_info->file_save_arg) free (args_info->file_save_arg); /* free previous string */ args_info->file_save_arg = gengetopt_strdup (optarg); if (args_info->file_save_orig) free (args_info->file_save_orig); /* free previous string */ args_info->file_save_orig = gengetopt_strdup (optarg); } break; case '?': /* Invalid option. */ /* `getopt_long' already printed an error message. */ goto failure; default: /* bug: option not considered. */ fprintf (stderr, "%s: option unknown: %c%s\n", TEST_CONF_PARSER_CMD_PARSER_PACKAGE, c, (additional_error ? additional_error : "")); abort (); } /* switch */ } /* while */ if (args_info->grp1_group_counter > 1) { fprintf (stderr, "%s: %d options of group grp1 were given. One is required.%s\n", argv[0], args_info->grp1_group_counter, (additional_error ? additional_error : "")); error = 1; } if (args_info->my_grp2_group_counter > 1) { fprintf (stderr, "%s: %d options of group my grp2 were given. At most one is required.%s\n", argv[0], args_info->my_grp2_group_counter, (additional_error ? additional_error : "")); error = 1; } if (local_args_info.multi_string_given && multi_string_list) { struct string_list *tmp; args_info->multi_string_arg = (char * *) realloc (args_info->multi_string_arg, (args_info->multi_string_given + local_args_info.multi_string_given) * sizeof (char *)); args_info->multi_string_orig = (char **) realloc (args_info->multi_string_orig, (args_info->multi_string_given + local_args_info.multi_string_given) * sizeof (char *)); for (i = (local_args_info.multi_string_given - 1); i >= 0; --i) { tmp = multi_string_list; args_info->multi_string_arg [i + args_info->multi_string_given] = multi_string_list->arg; args_info->multi_string_orig [i + args_info->multi_string_given] = multi_string_list->orig; multi_string_list = multi_string_list->next; free (tmp); } } if (local_args_info.multi_string_def_given && multi_string_def_list) { struct string_list *tmp; args_info->multi_string_def_arg = (char * *) realloc (args_info->multi_string_def_arg, (args_info->multi_string_def_given + local_args_info.multi_string_def_given) * sizeof (char *)); args_info->multi_string_def_orig = (char **) realloc (args_info->multi_string_def_orig, (args_info->multi_string_def_given + local_args_info.multi_string_def_given) * sizeof (char *)); for (i = (local_args_info.multi_string_def_given - 1); i >= 0; --i) { tmp = multi_string_def_list; args_info->multi_string_def_arg [i + args_info->multi_string_def_given] = multi_string_def_list->arg; args_info->multi_string_def_orig [i + args_info->multi_string_def_given] = multi_string_def_list->orig; multi_string_def_list = multi_string_def_list->next; free (tmp); } } else /* set the default value */ { if (! args_info->multi_string_def_arg && ! args_info->multi_string_def_given) { args_info->multi_string_def_arg = (char * *) malloc (sizeof (char *)); args_info->multi_string_def_arg [0] = gengetopt_strdup("this default"); args_info->multi_string_def_orig = (char **) malloc (sizeof (char *)); args_info->multi_string_def_orig [0] = NULL; } } args_info->multi_string_given += local_args_info.multi_string_given; local_args_info.multi_string_given = 0; args_info->multi_string_def_given += local_args_info.multi_string_def_given; local_args_info.multi_string_def_given = 0; args_info->noarg_given += local_args_info.noarg_given; local_args_info.noarg_given = 0; args_info->noarg_noshort_given += local_args_info.noarg_noshort_given; local_args_info.noarg_noshort_given = 0; if (check_required) { error += test_conf_parser_cmd_parser_required2 (args_info, argv[0], additional_error); } test_conf_parser_cmd_parser_release (&local_args_info); if ( error ) return (EXIT_FAILURE); if (optind < argc) { int i = 0 ; int found_prog_name = 0; /* whether program name, i.e., argv[0], is in the remaining args (this may happen with some implementations of getopt, but surely not with the one included by gengetopt) */ i = optind; while (i < argc) if (argv[i++] == argv[0]) { found_prog_name = 1; break; } i = 0; args_info->inputs_num = argc - optind - found_prog_name; args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; while (optind < argc) if (argv[optind++] != argv[0]) args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ; } return 0; failure: if (multi_string_list) { struct string_list *tmp; while (multi_string_list) { tmp = multi_string_list; free (multi_string_list->arg); free (multi_string_list->orig); multi_string_list = multi_string_list->next; free (tmp); } } if (multi_string_def_list) { struct string_list *tmp; while (multi_string_def_list) { tmp = multi_string_def_list; free (multi_string_def_list->arg); free (multi_string_def_list->orig); multi_string_def_list = multi_string_def_list->next; free (tmp); } } test_conf_parser_cmd_parser_release (&local_args_info); return (EXIT_FAILURE); } #ifndef CONFIG_FILE_LINE_SIZE #define CONFIG_FILE_LINE_SIZE 2048 #endif #define ADDITIONAL_ERROR " in configuration file " #define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3) /* 3 is for "--" and "=" */ static int _test_conf_parser_cmd_parser_configfile (char * const filename, int *my_argc) { FILE* file; char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE+1]; char linebuf[CONFIG_FILE_LINE_SIZE]; int line_num = 0; int result = 0, equal; char *fopt, *farg; char *str_index; size_t len, next_token; char delimiter; if ((file = fopen(filename, "r")) == NULL) { fprintf (stderr, "%s: Error opening configuration file '%s'\n", TEST_CONF_PARSER_CMD_PARSER_PACKAGE, filename); return EXIT_FAILURE; } while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != NULL) { ++line_num; my_argv[0] = '\0'; len = strlen(linebuf); if (len > (CONFIG_FILE_LINE_BUFFER_SIZE-1)) { fprintf (stderr, "%s:%s:%d: Line too long in configuration file\n", TEST_CONF_PARSER_CMD_PARSER_PACKAGE, filename, line_num); result = EXIT_FAILURE; break; } /* find first non-whitespace character in the line */ next_token = strspn (linebuf, " \t\r\n"); str_index = linebuf + next_token; if ( str_index[0] == '\0' || str_index[0] == '#') continue; /* empty line or comment line is skipped */ fopt = str_index; /* truncate fopt at the end of the first non-valid character */ next_token = strcspn (fopt, " \t\r\n="); if (fopt[next_token] == '\0') /* the line is over */ { farg = NULL; equal = 0; goto noarg; } /* remember if equal sign is present */ equal = (fopt[next_token] == '='); fopt[next_token++] = '\0'; /* advance pointers to the next token after the end of fopt */ next_token += strspn (fopt + next_token, " \t\r\n"); /* check for the presence of equal sign, and if so, skip it */ if ( !equal ) if ((equal = (fopt[next_token] == '='))) { next_token++; next_token += strspn (fopt + next_token, " \t\r\n"); } str_index += next_token; /* find argument */ farg = str_index; if ( farg[0] == '\"' || farg[0] == '\'' ) { /* quoted argument */ str_index = strchr (++farg, str_index[0] ); /* skip opening quote */ if (! str_index) { fprintf (stderr, "%s:%s:%d: unterminated string in configuration file\n", TEST_CONF_PARSER_CMD_PARSER_PACKAGE, filename, line_num); result = EXIT_FAILURE; break; } } else { /* read up the remaining part up to a delimiter */ next_token = strcspn (farg, " \t\r\n#\'\""); str_index += next_token; } /* truncate farg at the delimiter and store it for further check */ delimiter = *str_index, *str_index++ = '\0'; /* everything but comment is illegal at the end of line */ if (delimiter != '\0' && delimiter != '#') { str_index += strspn(str_index, " \t\r\n"); if (*str_index != '\0' && *str_index != '#') { fprintf (stderr, "%s:%s:%d: malformed string in configuration file\n", TEST_CONF_PARSER_CMD_PARSER_PACKAGE, filename, line_num); result = EXIT_FAILURE; break; } } noarg: if (!strcmp(fopt,"include")) { if (farg && *farg) { result = _test_conf_parser_cmd_parser_configfile(farg, my_argc); } else { fprintf(stderr, "%s:%s:%d: include requires a filename argument.\n", TEST_CONF_PARSER_CMD_PARSER_PACKAGE, filename, line_num); } continue; } len = strlen(fopt); strcat (my_argv, len > 1 ? "--" : "-"); strcat (my_argv, fopt); if (len > 1 && ((farg && *farg) || equal)) strcat (my_argv, "="); if (farg && *farg) strcat (my_argv, farg); ++(*my_argc); cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list)); cmd_line_list_tmp->next = cmd_line_list; cmd_line_list = cmd_line_list_tmp; cmd_line_list->string_arg = gengetopt_strdup(my_argv); } /* while */ if (file) fclose(file); return result; } int test_conf_parser_cmd_parser_configfile (char * const filename, struct my_args_info *args_info, int override, int initialize, int check_required) { int i, result; int my_argc = 1; char **my_argv_arg; char *additional_error; /* store the program name */ cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list)); cmd_line_list_tmp->next = cmd_line_list; cmd_line_list = cmd_line_list_tmp; cmd_line_list->string_arg = gengetopt_strdup (TEST_CONF_PARSER_CMD_PARSER_PACKAGE); result = _test_conf_parser_cmd_parser_configfile(filename, &my_argc); if (result != EXIT_FAILURE) { my_argv_arg = (char **) malloc((my_argc+1) * sizeof(char *)); cmd_line_list_tmp = cmd_line_list; for (i = my_argc - 1; i >= 0; --i) { my_argv_arg[i] = cmd_line_list_tmp->string_arg; cmd_line_list_tmp = cmd_line_list_tmp->next; } my_argv_arg[my_argc] = 0; additional_error = (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) + 1); strcpy (additional_error, ADDITIONAL_ERROR); strcat (additional_error, filename); result = test_conf_parser_cmd_parser_internal (my_argc, my_argv_arg, args_info, override, initialize, check_required, additional_error); free (additional_error); free (my_argv_arg); } free_cmd_list(); if (result == EXIT_FAILURE) { test_conf_parser_cmd_parser_free (args_info); exit (EXIT_FAILURE); } return result; }