/* File autogenerated by gengetopt @generator_version@ generated with the following command: @command_line@ 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 #ifndef FIX_UNUSED #define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */ #endif @IF@ not include_getopt @THEN@ #include @ENDIF@ #include "@source_name@.@header_file_ext@" const char *@args_info@_purpose = "@purpose@"; const char *@args_info@_usage = "Usage: @if@ no_package @then@" @package_var_name@ "@endif@@usage_string@"; const char *@args_info@_versiontext = "@versiontext@"; const char *@args_info@_description = "@description@"; @IF@ has_hidden or has_details @THEN@ @IF@ has_details @THEN@ const char *@args_info@_detailed_help[] = { @detailed_help_option_print:method@ 0 }; @IF@ has_hidden @THEN@ static void init_full_help_array(void) { @full_help_option_print:method@ } const char *@args_info@_full_help[@help_string_num@]; @ENDIF@ @ELSEIF@ has_hidden @THEN@ const char *@args_info@_full_help[] = { @full_help_option_print:method@ 0 }; @ENDIF@ static void init_help_array(void) { @help_option_print:method@ } const char *@args_info@_help[@help_string_num@]; @ELSE@ const char *@args_info@_help[] = { @help_option_print:method@ 0 }; @ENDIF@ typedef enum {ARG_NO @IF@ has_arg_flag @THEN@ , ARG_FLAG @ENDIF@ @IF@ has_arg_string @THEN@ , ARG_STRING @ENDIF@ @IF@ has_arg_int @THEN@ , ARG_INT @ENDIF@ @IF@ has_arg_short @THEN@ , ARG_SHORT @ENDIF@ @IF@ has_arg_long @THEN@ , ARG_LONG @ENDIF@ @IF@ has_arg_float @THEN@ , ARG_FLOAT @ENDIF@ @IF@ has_arg_double @THEN@ , ARG_DOUBLE @ENDIF@ @IF@ has_arg_longdouble @THEN@ , ARG_LONGDOUBLE @ENDIF@ @IF@ has_arg_longlong @THEN@ , ARG_LONGLONG @ENDIF@ @IF@ has_arg_enum @THEN@ , ARG_ENUM @ENDIF@} @parser_name@_arg_type; static void clear_given (struct @args_info@ *args_info); static void clear_args (struct @args_info@ *args_info); static int @parser_name@_internal (int argc, char **argv, struct @args_info@ *args_info, struct @parser_name@_params *params, const char *additional_error); @IF@ check_required_options @THEN@ static int @parser_name@_required2 (struct @args_info@ *args_info, const char *prog_name, const char *additional_error); @ENDIF@ @IF@ cmd_list @THEN@ 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); } } } @ENDIF@ @option_values:method{iteration=true}@ @IF@ do_generate_strdup @THEN@ static char * gengetopt_strdup (const char *s); @ENDIF@ static void clear_given (struct @args_info@ *args_info) { @given_init:method{iteration=true}@ } static void clear_args (struct @args_info@ *args_info) { FIX_UNUSED (args_info); @clear_arg:method@ } static void init_args_info(struct @args_info@ *args_info) { @IF@ has_hidden and has_details @then@ init_full_help_array(); @endif@ @IF@ has_hidden or has_details @then@ init_help_array(); @endif@ @init_args_info:method@ } void @parser_name@_print_version (void) { printf ("%s %s\n", (strlen(@package_var_name@_NAME) ? @package_var_name@_NAME : @package_var_name@), @version_var_name@); if (strlen(@args_info@_versiontext) > 0) printf("\n%s\n", @args_info@_versiontext); } static void print_help_common(void) { size_t len_purpose = strlen(@args_info@_purpose); size_t len_usage = strlen(@args_info@_usage); if (len_usage > 0) { printf("%s\n", @args_info@_usage); } if (len_purpose > 0) { printf("%s\n", @args_info@_purpose); } if (len_usage || len_purpose) { printf("\n"); } if (strlen(@args_info@_description) > 0) { printf("%s\n\n", @args_info@_description); } } void @parser_name@_print_help (void) { int i = 0; print_help_common(); while (@args_info@_help[i]) printf("%s\n", @args_info@_help[i++]); } @IF@ has_hidden @THEN@ void @parser_name@_print_full_help (void) { int i = 0; print_help_common(); while (@args_info@_full_help[i]) printf("%s\n", @args_info@_full_help[i++]); } @ENDIF@ @IF@ has_details @THEN@ void @parser_name@_print_detailed_help (void) { int i = 0; print_help_common(); while (@args_info@_detailed_help[i]) printf("%s\n", @args_info@_detailed_help[i++]); } @ENDIF@ void @parser_name@_init (struct @args_info@ *args_info) { clear_given (args_info); clear_args (args_info); init_args_info (args_info); @IF@ handle_unnamed @THEN@ args_info->inputs = 0; args_info->inputs_num = 0; @ENDIF@ } void @parser_name@_params_init(struct @parser_name@_params *params) { if (params) { params->override = 0; params->initialize = 1; params->check_required = 1; params->check_ambiguity = 0; params->print_errors = 1; } } struct @parser_name@_params * @parser_name@_params_create(void) { struct @parser_name@_params *params = (struct @parser_name@_params *)malloc(sizeof(struct @parser_name@_params)); @parser_name@_params_init(params); return params; } @IF@ has_typed_options @THEN@ static void free_string_field (char **s) { if (*s) { free (*s); *s = 0; } } @ENDIF@ @IF@ multiple_token_functions @THEN@ /** @@brief generic value variable */ union generic_value { @IF@ has_arg_int or has_arg_enum @THEN@ int int_arg; @ENDIF@ @IF@ has_arg_short @THEN@ short short_arg; @ENDIF@ @IF@ has_arg_long @THEN@ long long_arg; @ENDIF@ @IF@ has_arg_float @THEN@ float float_arg; @ENDIF@ @IF@ has_arg_double @THEN@ double double_arg; @ENDIF@ @IF@ has_arg_longdouble @THEN@ long double longdouble_arg; @ENDIF@ @IF@ has_arg_longlong @THEN@ #if defined(HAVE_LONG_LONG) || defined(HAVE_LONG_LONG_INT) long long int longlong_arg; #else long longlong_arg; #endif @ENDIF@ char *string_arg; const char *default_string_arg; }; /** @@brief holds temporary values for multiple options */ struct generic_list { union generic_value arg; char *orig; struct generic_list *next; }; /** * @@brief add a node at the head of the list */ static void add_node(struct generic_list **list) { struct generic_list *new_node = (struct generic_list *) malloc (sizeof (struct generic_list)); new_node->next = *list; *list = new_node; new_node->arg.string_arg = 0; new_node->orig = 0; } @IF@ (not multiple_options_all_string) and multiple_token_functions @THEN@ /** * The passed arg parameter is NOT set to 0 from this function */ static void free_multiple_field(unsigned int len, void *arg, char ***orig) { unsigned int i; if (arg) { for (i = 0; i < len; ++i) { free_string_field(&((*orig)[i])); } free (arg); free (*orig); *orig = 0; } } @ENDIF@ @IF@ multiple_options_string @THEN@ static void free_multiple_string_field(unsigned int len, char ***arg, char ***orig) { unsigned int i; if (*arg) { for (i = 0; i < len; ++i) { free_string_field(&((*arg)[i])); free_string_field(&((*orig)[i])); } free_string_field(&((*arg)[0])); /* free default string */ free (*arg); *arg = 0; free (*orig); *orig = 0; } } @ENDIF@ @ENDIF@ static void @parser_name@_release (struct @args_info@ *args_info) { @IF@ handle_unnamed @THEN@ unsigned int i;@ENDIF@ @free:method@ @IF@ handle_unnamed @THEN@ for (i = 0; i < args_info->inputs_num; ++i) free (args_info->inputs [i]); if (args_info->inputs_num) free (args_info->inputs); @ENDIF@ clear_given (args_info); } @IF@ check_possible_values @THEN@ /** * @@param val the value to check * @@param values the possible values * @@return the index of the matched value: * -1 if no value matched, * -2 if more than one value has matched */ static int check_possible_values(const char *val, const char *values[]) { int i, found, last; size_t len; if (!val) /* otherwise strlen() crashes below */ return -1; /* -1 means no argument for the option */ found = last = 0; for (i = 0, len = strlen(val); values[i]; ++i) { if (strncmp(val, values[i], len) == 0) { ++found; last = i; if (strlen(values[i]) == len) return i; /* exact macth no need to check more */ } } if (found == 1) /* one match: OK */ return last; return (found ? -2 : -1); /* return many values or none matched */ } @ENDIF@ static void write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[]) { @IF@ check_possible_values @THEN@ int found = -1; @ELSE@ FIX_UNUSED (values); @ENDIF@ if (arg) { @IF@ check_possible_values @THEN@ if (values) { found = check_possible_values(arg, values); } if (found >= 0) fprintf(outfile, "%s=\"%s\" # %s\n", opt, arg, values[found]); else fprintf(outfile, "%s=\"%s\"\n", opt, arg); @ELSE@ fprintf(outfile, "%s=\"%s\"\n", opt, arg); @ENDIF@ } else { fprintf(outfile, "%s\n", opt); } } @IF@ multiple_options @THEN@ static void write_multiple_into_file(FILE *outfile, int len, const char *opt, char **arg, const char *values[]) { int i; for (i = 0; i < len; ++i) write_into_file(outfile, opt, (arg ? arg[i] : 0), values); } @ENDIF@ int @parser_name@_dump(FILE *outfile, struct @args_info@ *args_info) { int i = 0; if (!outfile) { fprintf (stderr, "%s: cannot dump options to stream\n", @package_var_name@); return EXIT_FAILURE; } @file_save_loop:method@ i = EXIT_SUCCESS; return i; } int @parser_name@_file_save(const char *filename, struct @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", @package_var_name@, filename); return EXIT_FAILURE; } i = @parser_name@_dump(outfile, args_info); fclose (outfile); return i; } void @parser_name@_free (struct @args_info@ *args_info) { @parser_name@_release (args_info); } @IF@ do_generate_strdup @THEN@ /** @@brief replacement of strdup, which is not standard */ char * gengetopt_strdup (const char *s) { char *result = 0; if (!s) return result; result = (char*)malloc(strlen(s) + 1); if (result == (char*)0) return (char*)0; strcpy(result, s); return result; } @ENDIF@ @IF@ multiple_token_functions @THEN@ static char * get_multiple_arg_token(const char *arg) { const char *tok; char *ret; size_t len, num_of_escape, i, j; if (!arg) return 0; 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] == '\\' && arg[ i + 1 ] && arg[ i + 1 ] == ',') ++i; ret[j++] = arg[i++]; } ret[len-1] = '\0'; return ret; } static const char * get_multiple_arg_token_next(const char *arg) { const char *tok; if (!arg) return 0; 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; } @ENDIF@ @IF@ multiple_options @THEN@ static int check_multiple_option_occurrences(const char *prog_name, unsigned int option_given, unsigned int min, unsigned int max, const char *option_desc); int check_multiple_option_occurrences(const char *prog_name, unsigned int option_given, unsigned int min, unsigned int max, const char *option_desc) { int error_occurred = 0; if (option_given && (min > 0 || max > 0)) { if (min > 0 && max > 0) { if (min == max) { /* specific occurrences */ if (option_given != (unsigned int) min) { fprintf (stderr, "%s: %s option occurrences must be %d\n", prog_name, option_desc, min); error_occurred = 1; } } else if (option_given < (unsigned int) min || option_given > (unsigned int) max) { /* range occurrences */ fprintf (stderr, "%s: %s option occurrences must be between %d and %d\n", prog_name, option_desc, min, max); error_occurred = 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_occurred = 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_occurred = 1; } } } return error_occurred; } @ENDIF@ @reset_groups:method{iteration=true}@ int @parser_name@ (int argc, char **argv, struct @args_info@ *args_info) { return @parser_name@2 (argc, argv, args_info, 0, 1, 1); } int @parser_name@_ext (int argc, char **argv, struct @args_info@ *args_info, struct @parser_name@_params *params) { int result; result = @parser_name@_internal (argc, argv, args_info, params, 0); @final_exit@ } int @parser_name@2 (int argc, char **argv, struct @args_info@ *args_info, int override, int initialize, int check_required) { int result; struct @parser_name@_params params; params.override = override; params.initialize = initialize; params.check_required = check_required; params.check_ambiguity = 0; params.print_errors = 1; result = @parser_name@_internal (argc, argv, args_info, ¶ms, 0); @final_exit@ } int @parser_name@_required (struct @args_info@ *args_info, const char *prog_name) { @IF@ check_required_options @THEN@ int result = EXIT_SUCCESS; if (@parser_name@_required2(args_info, prog_name, 0) > 0) result = EXIT_FAILURE; @final_exit@ @ELSE@ FIX_UNUSED (args_info); FIX_UNUSED (prog_name); return EXIT_SUCCESS; @ENDIF@ } @IF@ check_required_options @THEN@ int @parser_name@_required2 (struct @args_info@ *args_info, const char *prog_name, const char *additional_error) { int error_occurred = 0; FIX_UNUSED (additional_error); /* checks for required options */ @handle_required:method@ /* checks for dependences among options */ @handle_dependencies:method{iteration=true}@ return error_occurred; } @ENDIF@ @IF@ include_getopt @THEN@ @custom_getopt:method@ @ENDIF@ static char *package_name = 0; @IF@ not no_options @THEN@ /** * @@brief updates an option * @@param field the generic pointer to the field to update * @@param orig_field the pointer to the orig field * @@param field_given the pointer to the number of occurrence of this option * @@param prev_given the pointer to the number of occurrence already seen * @@param value the argument for this option (if null no arg was specified) * @@param possible_values the possible values for this option (if specified) * @@param default_value the default value (in case the option only accepts fixed values) * @@param arg_type the type of this option * @@param check_ambiguity @@see @parser_name@_params.check_ambiguity * @@param override @@see @parser_name@_params.override * @@param no_free whether to free a possible previous value * @@param multiple_option whether this is a multiple option * @@param long_opt the corresponding long option * @@param short_opt the corresponding short option (or '-' if none) * @@param additional_error possible further error specification */ static int update_arg(void *field, char **orig_field, unsigned int *field_given, unsigned int *prev_given, char *value, const char *possible_values[], const char *default_value, @parser_name@_arg_type arg_type, int check_ambiguity, int override, int no_free, int multiple_option, const char *long_opt, char short_opt, const char *additional_error) { char *stop_char = 0; const char *val = value; int found; @IF@ has_arg_string @THEN@ char **string_field; @ENDIF@ FIX_UNUSED (field); stop_char = 0; found = 0; if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given))) { if (short_opt != '-') fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n", package_name, long_opt, short_opt, (additional_error ? additional_error : "")); else fprintf (stderr, "%s: `--%s' option given more than once%s\n", package_name, long_opt, (additional_error ? additional_error : "")); return 1; /* failure */ } @IF@ check_possible_values @THEN@ if (possible_values && (found = check_possible_values((value ? value : default_value), possible_values)) < 0) { if (short_opt != '-') fprintf (stderr, "%s: %s argument, \"%s\", for option `--%s' (`-%c')%s\n", package_name, (found == -2) ? "ambiguous" : "invalid", value, long_opt, short_opt, (additional_error ? additional_error : "")); else fprintf (stderr, "%s: %s argument, \"%s\", for option `--%s'%s\n", package_name, (found == -2) ? "ambiguous" : "invalid", value, long_opt, (additional_error ? additional_error : "")); return 1; /* failure */ } @ELSE@ FIX_UNUSED (default_value); @ENDIF@ if (field_given && *field_given && ! override) return 0; if (prev_given) (*prev_given)++; if (field_given) (*field_given)++; if (possible_values) val = possible_values[found]; switch(arg_type) { @IF@ has_arg_flag @THEN@ case ARG_FLAG: *((int *)field) = !*((int *)field); break; @ENDIF@ @IF@ has_arg_int @THEN@ case ARG_INT: if (val) *((int *)field) = strtol (val, &stop_char, 0); break; @ENDIF@ @IF@ has_arg_short @THEN@ case ARG_SHORT: if (val) *((short *)field) = (short)strtol (val, &stop_char, 0); break; @ENDIF@ @IF@ has_arg_long @THEN@ case ARG_LONG: if (val) *((long *)field) = (long)strtol (val, &stop_char, 0); break; @ENDIF@ @IF@ has_arg_float @THEN@ case ARG_FLOAT: if (val) *((float *)field) = (float)strtod (val, &stop_char); break; @ENDIF@ @IF@ has_arg_double @THEN@ case ARG_DOUBLE: if (val) *((double *)field) = strtod (val, &stop_char); break; @ENDIF@ @IF@ has_arg_longdouble @THEN@ case ARG_LONGDOUBLE: if (val) *((long double *)field) = (long double)strtod (val, &stop_char); break; @ENDIF@ @IF@ has_arg_longlong @THEN@ case ARG_LONGLONG: #if defined(HAVE_LONG_LONG) || defined(HAVE_LONG_LONG_INT) if (val) *((long long int*)field) = (long long int) strtoll (val, &stop_char, 0); #else if (val) *((long *)field) = (long)strtol (val, &stop_char, 0); #endif break; @ENDIF@ @IF@ has_arg_enum @THEN@ case ARG_ENUM: if (val) *((int *)field) = found; break; @ENDIF@ @IF@ has_arg_string @THEN@ case ARG_STRING: if (val) { string_field = (char **)field; if (!no_free && *string_field) free (*string_field); /* free previous string */ *string_field = gengetopt_strdup (val); } break; @ENDIF@ default: break; }; @IF@ has_arg_int or has_arg_short or has_arg_long or has_arg_float or has_arg_double or has_arg_longdouble or has_arg_longlong @THEN@ /* check numeric conversion */ switch(arg_type) { @IF@ has_arg_int @THEN@ case ARG_INT: @ENDIF@ @IF@ has_arg_short @THEN@ case ARG_SHORT: @ENDIF@ @IF@ has_arg_long @THEN@ case ARG_LONG: @ENDIF@ @IF@ has_arg_float @THEN@ case ARG_FLOAT: @ENDIF@ @IF@ has_arg_double @THEN@ case ARG_DOUBLE: @ENDIF@ @IF@ has_arg_longdouble @THEN@ case ARG_LONGDOUBLE: @ENDIF@ @IF@ has_arg_longlong @THEN@ case ARG_LONGLONG: @ENDIF@ if (val && !(stop_char && *stop_char == '\0')) { fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val); return 1; /* failure */ } break; default: ; }; @ELSE@ FIX_UNUSED(stop_char); @IF@ not (has_arg_enum or has_arg_string) @THEN@ FIX_UNUSED(val); @ENDIF@ @ENDIF@ /* store the original value */ switch(arg_type) { case ARG_NO: @IF@ has_arg_flag @THEN@ case ARG_FLAG: @ENDIF@ break; default: if (value && orig_field) { if (no_free) { *orig_field = value; } else { if (*orig_field) free (*orig_field); /* free previous string */ *orig_field = gengetopt_strdup (value); } } }; return 0; /* OK */ } @ENDIF@ @IF@ multiple_token_functions @THEN@ /** * @@brief store information about a multiple option in a temporary list * @@param list where to (temporarily) store multiple options */ static int update_multiple_arg_temp(struct generic_list **list, unsigned int *prev_given, const char *val, const char *possible_values[], const char *default_value, @parser_name@_arg_type arg_type, const char *long_opt, char short_opt, const char *additional_error) { /* store single arguments */ char *multi_token; const char *multi_next; if (arg_type == ARG_NO) { (*prev_given)++; return 0; /* OK */ } multi_token = get_multiple_arg_token(val); multi_next = get_multiple_arg_token_next (val); while (1) { add_node (list); if (update_arg((void *)&((*list)->arg), &((*list)->orig), 0, prev_given, multi_token, possible_values, default_value, arg_type, 0, 1, 1, 1, long_opt, short_opt, additional_error)) { if (multi_token) free(multi_token); return 1; /* failure */ } if (multi_next) { multi_token = get_multiple_arg_token(multi_next); multi_next = get_multiple_arg_token_next (multi_next); } else break; } return 0; /* OK */ } /** * @@brief free the passed list (including possible string argument) */ static void free_list(struct generic_list *list, short string_arg) { if (list) { struct generic_list *tmp; while (list) { tmp = list; if (string_arg && list->arg.string_arg) free (list->arg.string_arg); if (list->orig) free (list->orig); list = list->next; free (tmp); } } } /** * @@brief updates a multiple option starting from the passed list */ static void update_multiple_arg(void *field, char ***orig_field, unsigned int field_given, unsigned int prev_given, union generic_value *default_value, @parser_name@_arg_type arg_type, struct generic_list *list) { int i; struct generic_list *tmp; if (prev_given && list) { *orig_field = (char **) realloc (*orig_field, (field_given + prev_given) * sizeof (char *)); switch(arg_type) { @IF@ has_arg_int or has_arg_enum @THEN@ @IF@ has_arg_int @THEN@ case ARG_INT: @ENDIF@ @IF@ has_arg_enum @THEN@ case ARG_ENUM: @ENDIF@ *((int **)field) = (int *)realloc (*((int **)field), (field_given + prev_given) * sizeof (int)); break; @ENDIF@ @IF@ has_arg_short @THEN@ case ARG_SHORT: *((short **)field) = (short *)realloc (*((short **)field), (field_given + prev_given) * sizeof (short)); break; @ENDIF@ @IF@ has_arg_long @THEN@ case ARG_LONG: *((long **)field) = (long *)realloc (*((long **)field), (field_given + prev_given) * sizeof (long)); break; @ENDIF@ @IF@ has_arg_float @THEN@ case ARG_FLOAT: *((float **)field) = (float *)realloc (*((float **)field), (field_given + prev_given) * sizeof (float)); break; @ENDIF@ @IF@ has_arg_double @THEN@ case ARG_DOUBLE: *((double **)field) = (double *)realloc (*((double **)field), (field_given + prev_given) * sizeof (double)); break; @ENDIF@ @IF@ has_arg_longdouble @THEN@ case ARG_LONGDOUBLE: *((long double **)field) = (long double *)realloc (*((long double **)field), (field_given + prev_given) * sizeof (long double)); break; @ENDIF@ @IF@ has_arg_longlong @THEN@ case ARG_LONGLONG: #if defined(HAVE_LONG_LONG) || defined(HAVE_LONG_LONG_INT) *((long long int **)field) = (long long int *)realloc (*((long long int **)field), (field_given + prev_given) * sizeof (long long int)); break; #else *((long **)field) = (long *)realloc (*((long **)field), (field_given + prev_given) * sizeof (long)); break; #endif @ENDIF@ @IF@ has_arg_string @THEN@ case ARG_STRING: *((char ***)field) = (char **)realloc (*((char ***)field), (field_given + prev_given) * sizeof (char *)); break; @ENDIF@ default: break; }; for (i = (prev_given - 1); i >= 0; --i) { tmp = list; switch(arg_type) { @IF@ has_arg_int @THEN@ case ARG_INT: (*((int **)field))[i + field_given] = tmp->arg.int_arg; break; @ENDIF@ @IF@ has_arg_short @THEN@ case ARG_SHORT: (*((short **)field))[i + field_given] = tmp->arg.short_arg; break; @ENDIF@ @IF@ has_arg_long @THEN@ case ARG_LONG: (*((long **)field))[i + field_given] = tmp->arg.long_arg; break; @ENDIF@ @IF@ has_arg_float @THEN@ case ARG_FLOAT: (*((float **)field))[i + field_given] = tmp->arg.float_arg; break; @ENDIF@ @IF@ has_arg_double @THEN@ case ARG_DOUBLE: (*((double **)field))[i + field_given] = tmp->arg.double_arg; break; @ENDIF@ @IF@ has_arg_longdouble @THEN@ case ARG_LONGDOUBLE: (*((long double **)field))[i + field_given] = tmp->arg.longdouble_arg; break; @ENDIF@ @IF@ has_arg_longlong @THEN@ case ARG_LONGLONG: #if defined(HAVE_LONG_LONG) || defined(HAVE_LONG_LONG_INT) (*((long long int **)field))[i + field_given] = tmp->arg.longlong_arg; break; #else (*((long **)field))[i + field_given] = tmp->arg.longlong_arg; break; #endif @ENDIF@ @IF@ has_arg_enum @THEN@ case ARG_ENUM: (*((int **)field))[i + field_given] = tmp->arg.int_arg; break; @ENDIF@ @IF@ has_arg_string @THEN@ case ARG_STRING: (*((char ***)field))[i + field_given] = tmp->arg.string_arg; break; @ENDIF@ default: break; } (*orig_field) [i + field_given] = list->orig; list = list->next; free (tmp); } } else { /* set the default value */ if (default_value && ! field_given) { switch(arg_type) { @IF@ has_arg_int or has_arg_enum @THEN@ @IF@ has_arg_int @THEN@ case ARG_INT: @ENDIF@ @IF@ has_arg_enum @THEN@ case ARG_ENUM: @ENDIF@ if (! *((int **)field)) { *((int **)field) = (int *)malloc (sizeof (int)); (*((int **)field))[0] = default_value->int_arg; } break; @ENDIF@ @IF@ has_arg_short @THEN@ case ARG_SHORT: if (! *((short **)field)) { *((short **)field) = (short *)malloc (sizeof (short)); (*((short **)field))[0] = default_value->short_arg; } break; @ENDIF@ @IF@ has_arg_long @THEN@ case ARG_LONG: if (! *((long **)field)) { *((long **)field) = (long *)malloc (sizeof (long)); (*((long **)field))[0] = default_value->long_arg; } break; @ENDIF@ @IF@ has_arg_float @THEN@ case ARG_FLOAT: if (! *((float **)field)) { *((float **)field) = (float *)malloc (sizeof (float)); (*((float **)field))[0] = default_value->float_arg; } break; @ENDIF@ @IF@ has_arg_double @THEN@ case ARG_DOUBLE: if (! *((double **)field)) { *((double **)field) = (double *)malloc (sizeof (double)); (*((double **)field))[0] = default_value->double_arg; } break; @ENDIF@ @IF@ has_arg_longdouble @THEN@ case ARG_LONGDOUBLE: if (! *((long double **)field)) { *((long double **)field) = (long double *)malloc (sizeof (long double)); (*((long double **)field))[0] = default_value->longdouble_arg; } break; @ENDIF@ @IF@ has_arg_longlong @THEN@ case ARG_LONGLONG: #if defined(HAVE_LONG_LONG) || defined(HAVE_LONG_LONG_INT) if (! *((long long int **)field)) { *((long long int **)field) = (long long int *)malloc (sizeof (long long int)); (*((long long int **)field))[0] = default_value->longlong_arg; } #else if (! *((long **)field)) { *((long **)field) = (long *)malloc (sizeof (long)); (*((long **)field))[0] = default_value->longlong_arg; } #endif break; @ENDIF@ @IF@ has_arg_string @THEN@ case ARG_STRING: if (! *((char ***)field)) { *((char ***)field) = (char **)malloc (sizeof (char *)); (*((char ***)field))[0] = gengetopt_strdup(default_value->string_arg); } break; @ENDIF@ default: break; } if (!(*orig_field)) { *orig_field = (char **) malloc (sizeof (char *)); (*orig_field)[0] = 0; } } } } @ENDIF@ @IF@ has_modes @THEN@ static int check_modes( int given1[], const char *options1[], int given2[], const char *options2[]) { int i = 0, j = 0, errors = 0; while (given1[i] >= 0) { if (given1[i]) { while (given2[j] >= 0) { if (given2[j]) { ++errors; fprintf(stderr, "%s: option %s conflicts with option %s\n", package_name, options1[i], options2[j]); } ++j; } } ++i; } return errors; } @ENDIF@ int @parser_name@_internal ( int argc, char **argv, struct @args_info@ *args_info, struct @parser_name@_params *params, const char *additional_error) { int c; /* Character of the parsed option. */ @IF@ multiple_options_with_default @THEN@ union generic_value multiple_default_value; @endif@ @list_def:method{iteration=true}@ int error_occurred = 0; struct @args_info@ local_args_info; int override; int initialize; int check_required; int check_ambiguity; @IF@ include_getopt @THEN@ char *optarg; int optind; int opterr; int optopt; @ENDIF@ package_name = argv[0]; /* TODO: Why is this here? It is not used anywhere. */ override = params->override; FIX_UNUSED(override); initialize = params->initialize; check_required = params->check_required; /* TODO: Why is this here? It is not used anywhere. */ check_ambiguity = params->check_ambiguity; FIX_UNUSED(check_ambiguity); if (initialize) @parser_name@_init (args_info); @parser_name@_init (&local_args_info); optarg = 0; optind = 0; opterr = params->print_errors; optopt = '?'; while (1) { int option_index = 0; static struct option long_options[] = { @long_option_struct:method{iteration=true}@ { 0, 0, 0, 0 } }; @IF@ include_getopt @THEN@ custom_optarg = optarg; custom_optind = optind; custom_opterr = opterr; custom_optopt = optopt; c = custom_getopt_long (argc, argv, "@getopt_string@", long_options, &option_index); optarg = custom_optarg; optind = custom_optind; opterr = custom_opterr; optopt = custom_optopt; @ELSE@ c = getopt_long (argc, argv, "@getopt_string@", long_options, &option_index); @ENDIF@ if (c == -1) break; /* Exit from `while (1)' loop. */ switch (c) { @handle_option:method{iteration=true}@ case 0: /* Long option with no short option */ @handle_no_short_option:method{iteration=true}@ @IF@ handle_question_mark @THEN@ case '?': /* Invalid option. */ /* `getopt_long' already printed an error message. */ goto failure; @ENDIF@ default: /* bug: option not considered. */ fprintf (stderr, "%s: option unknown: %c%s\n", @package_var_name@, c, (additional_error ? additional_error : "")); abort (); } /* switch */ } /* while */ @handle_group:method{iteration=true}@ @multiple_fill_array:method{iteration=true}@ @update_multiple_given:method{iteration=true}@ @check_modes:method{iteration=true}@ @IF@ check_required_options @THEN@ if (check_required) { error_occurred += @parser_name@_required2 (args_info, argv[0], additional_error); } @ELSE@ FIX_UNUSED(check_required); @ENDIF@ @parser_name@_release (&local_args_info); if ( error_occurred ) return (EXIT_FAILURE); @IF@ handle_unnamed @THEN@ 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) */ @IF@ include_getopt @THEN@ @ELSE@ i = optind; while (i < argc) if (argv[i++] == argv[0]) { found_prog_name = 1; break; } i = 0; @ENDIF@ args_info->inputs_num = argc - optind - found_prog_name; args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; while (optind < argc) @IF@ include_getopt @THEN@ args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind++]) ; @ELSE@ if (argv[optind++] != argv[0]) args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ; @ENDIF@ } @ENDIF@ return 0; failure: @list_free:method@ @parser_name@_release (&local_args_info); return (EXIT_FAILURE); } @IF@ conf_parser @THEN@ #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 _@parser_name@_configfile (const char *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")) == 0) { fprintf (stderr, "%s: Error opening configuration file '%s'\n", @package_var_name@, filename); return EXIT_FAILURE; } while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != 0) { ++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", @package_var_name@, 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 = 0; 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", @package_var_name@, 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", @package_var_name@, filename, line_num); result = EXIT_FAILURE; break; } } noarg: if (!strcmp(fopt,"include")) { if (farg && *farg) { result = _@parser_name@_configfile(farg, my_argc); } else { fprintf(stderr, "%s:%s:%d: include requires a filename argument.\n", @package_var_name@, 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 @parser_name@_configfile ( const char *filename, struct @args_info@ *args_info, int override, int initialize, int check_required) { struct @parser_name@_params params; params.override = override; params.initialize = initialize; params.check_required = check_required; params.check_ambiguity = 0; params.print_errors = 1; return @parser_name@_config_file (filename, args_info, ¶ms); } int @parser_name@_config_file (const char *filename, struct @args_info@ *args_info, struct @parser_name@_params *params) { 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 (@package_var_name@); result = _@parser_name@_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 = @parser_name@_internal (my_argc, my_argv_arg, args_info, params, additional_error); free (additional_error); free (my_argv_arg); } free_cmd_list(); @final_exit@ } @ENDIF@ @IF@ generate_string_parser @THEN@ static unsigned int @parser_name@_create_argv(const char *cmdline_, char ***argv_ptr, const char *prog_name) { char *cmdline, *p; size_t n = 0, j; int i; if (prog_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 (prog_name); ++n; } cmdline = gengetopt_strdup(cmdline_); p = cmdline; while (p && strlen(p)) { j = strcspn(p, " \t"); ++n; if (j && j < strlen(p)) { p[j] = '\0'; 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 (p); p += (j+1); p += strspn(p, " \t"); } else { 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 (p); break; } } *argv_ptr = (char **) malloc((n + 1) * sizeof(char *)); cmd_line_list_tmp = cmd_line_list; for (i = (n-1); i >= 0; --i) { (*argv_ptr)[i] = cmd_line_list_tmp->string_arg; cmd_line_list_tmp = cmd_line_list_tmp->next; } (*argv_ptr)[n] = 0; free(cmdline); return n; } int @parser_name@_string(const char *cmdline, struct @args_info@ *args_info, const char *prog_name) { return @parser_name@_string2(cmdline, args_info, prog_name, 0, 1, 1); } int @parser_name@_string2(const char *cmdline, struct @args_info@ *args_info, const char *prog_name, int override, int initialize, int check_required) { struct @parser_name@_params params; params.override = override; params.initialize = initialize; params.check_required = check_required; params.check_ambiguity = 0; params.print_errors = 1; return @parser_name@_string_ext(cmdline, args_info, prog_name, ¶ms); } int @parser_name@_string_ext(const char *cmdline, struct @args_info@ *args_info, const char *prog_name, struct @parser_name@_params *params) { char **argv_ptr = 0; int result; unsigned int argc; argc = @parser_name@_create_argv(cmdline, &argv_ptr, prog_name); result = @parser_name@_internal (argc, argv_ptr, args_info, params, 0); if (argv_ptr) { free (argv_ptr); } free_cmd_list(); @final_exit@ } @ENDIF@ /* vim: set ft=c noet ts=8 sts=8 sw=8 tw=80 nojs spell : */