/* 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 #include "getopt.h" #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@_description = "@description@"; const char *@args_info@_help[] = { @help_option_print:method@ 0 }; @IF@ has_hidden @THEN@ const char *@args_info@_full_help[] = { @full_help_option_print:method@ 0 }; @ENDIF@ 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 * const *argv, struct @args_info@ *args_info, int override, int initialize, int check_required, 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@ @struct_def:method{iteration=true}@ @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) { @clear_arg:method@ } static void init_args_info(struct @args_info@ *args_info) { @init_args_info:method@ } void @parser_name@_print_version (void) { printf ("%s %s\n", @package_var_name@, @version_var_name@); } void @parser_name@_print_help (void) { int i = 0; @parser_name@_print_version (); if (strlen(@args_info@_purpose) > 0) printf("\n%s\n", @args_info@_purpose); printf("\n%s\n\n", @args_info@_usage); if (strlen(@args_info@_description) > 0) printf("%s\n", @args_info@_description); 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; @parser_name@_print_version (); if (strlen(@args_info@_purpose) > 0) printf("\n%s\n", @args_info@_purpose); printf("\n%s\n\n", @args_info@_usage); if (strlen(@args_info@_description) > 0) printf("%s\n", @args_info@_description); while (@args_info@_full_help[i]) printf("%s\n", @args_info@_full_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_unamed @THEN@ args_info->inputs = NULL; args_info->inputs_num = 0; @ENDIF@ } static void @parser_name@_release (struct @args_info@ *args_info) { @free:method@ clear_given (args_info); } 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; } @file_save_loop:method@ fclose (outfile); i = EXIT_SUCCESS; return i; } void @parser_name@_free (struct @args_info@ *args_info) { @parser_name@_release (args_info); } @IF@ check_possible_values @THEN@ /* * Returns: * - the index of the matched value * - -1 if no argument has been specified * - -2 if more than one value has matched */ static int check_possible_values(const char *val, 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 are matched */ } @ENDIF@ @IF@ do_generate_strdup @THEN@ /* 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; } @ENDIF@ @IF@ multiple_token_functions @THEN@ 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; } @ENDIF@ @IF@ multiple_options @THEN@ 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; } @ENDIF@ @reset_groups:method{iteration=true}@ int @parser_name@ (int argc, char * const *argv, struct @args_info@ *args_info) { return @parser_name@2 (argc, argv, args_info, 0, 1, 1); } int @parser_name@2 (int argc, char * const *argv, struct @args_info@ *args_info, int override, int initialize, int check_required) { int result; result = @parser_name@_internal (argc, argv, args_info, override, initialize, check_required, NULL); @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, NULL) > 0) result = EXIT_FAILURE; @final_exit@ @ELSE@ 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 = 0; /* checks for required options */ @handle_required:method@ /* checks for dependences among options */ @handle_dependencies:method{iteration=true}@ return error; } @ENDIF@ @IF@ include_getopt @THEN@ @custom_getopt:method@ @ENDIF@ int @parser_name@_internal (int argc, char * const *argv, struct @args_info@ *args_info, int override, int initialize, int check_required, const char *additional_error) { int c; /* Character of the parsed option. */ @IF@ multiple_token_vars @THEN@ char *multi_token, *multi_next; /* for multiple options */ @endif@ @list_def:method{iteration=true}@ int error = 0; struct @args_info@ local_args_info; if (initialize) @parser_name@_init (args_info); @parser_name@_init (&local_args_info); optarg = 0; optind = 0; opterr = 1; optopt = '?'; while (1) { @IF@ check_possible_values @THEN@ int found = 0; @ENDIF@ int option_index = 0; char *stop_char; static struct option long_options[] = { @long_option_struct:method{iteration=true}@ { NULL, 0, NULL, 0 } }; stop_char = 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}@ 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", @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}@ @IF@ check_required_options @THEN@ if (check_required) { error += @parser_name@_required2 (args_info, argv[0], additional_error); } @ENDIF@ @parser_name@_release (&local_args_info); if ( error ) return (EXIT_FAILURE); @IF@ handle_unamed @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 (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", @package_var_name@, 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", @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 = 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", @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 (char * const filename, struct @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 (@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, override, initialize, check_required, 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] = NULL; 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) { 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, override, initialize, check_required, 0); if (argv_ptr) { free (argv_ptr); } free_cmd_list(); @final_exit@ } @ENDIF@