diff options
Diffstat (limited to 'mysys')
-rw-r--r-- | mysys/my_getopt.c | 97 |
1 files changed, 57 insertions, 40 deletions
diff --git a/mysys/my_getopt.c b/mysys/my_getopt.c index 07a8f199b3b..53e46932167 100644 --- a/mysys/my_getopt.c +++ b/mysys/my_getopt.c @@ -21,6 +21,9 @@ #include <mysys_err.h> #include <my_getopt.h> +static void default_reporter(enum loglevel level, const char *format, ...); +my_error_reporter my_getopt_error_reporter= &default_reporter; + static int findopt(char *optpat, uint length, const struct my_option **opt_res, char **ffname); @@ -56,7 +59,8 @@ char *disabled_my_option= (char*) "0"; my_bool my_getopt_print_errors= 1; -void default_reporter(enum loglevel level, const char *format, ...) +static void default_reporter(enum loglevel level __attribute__((unused)), + const char *format, ...) { va_list args; va_start(args, format); @@ -83,8 +87,8 @@ void my_getopt_register_get_addr(gptr* (*func_addr)(const char *, uint, } int handle_options(int *argc, char ***argv, - const struct my_option *longopts, my_get_one_option get_one_option, - my_error_reporter reporter) + const struct my_option *longopts, + my_get_one_option get_one_option) { uint opt_found, argvpos= 0, length, i; my_bool end_of_options= 0, must_be_var, set_maximum_value, @@ -100,8 +104,6 @@ int handle_options(int *argc, char ***argv, (*argv)++; /* --- || ---- */ init_variables(longopts); - if (! reporter) reporter = &default_reporter; - for (pos= *argv, pos_end=pos+ *argc; pos != pos_end ; pos++) { char *cur_arg= *pos; @@ -125,8 +127,9 @@ int handle_options(int *argc, char ***argv, if (!*++pos) { if (my_getopt_print_errors) - reporter(ERROR_LEVEL, "%s: Option '-O' requires an argument\n", - my_progname); + my_getopt_error_reporter(ERROR_LEVEL, + "%s: Option '-O' requires an argument\n", + my_progname); return EXIT_ARGUMENT_REQUIRED; } cur_arg= *pos; @@ -142,8 +145,9 @@ int handle_options(int *argc, char ***argv, if (!*cur_arg) { if (my_getopt_print_errors) - reporter(ERROR_LEVEL, "%s: Option '--set-variable' requires an argument\n", - my_progname); + my_getopt_error_reporter(ERROR_LEVEL, + "%s: Option '--set-variable' requires an argument\n", + my_progname); return EXIT_ARGUMENT_REQUIRED; } } @@ -155,8 +159,9 @@ int handle_options(int *argc, char ***argv, if (!*++pos) { if (my_getopt_print_errors) - reporter(ERROR_LEVEL, "%s: Option '--set-variable' requires an argument\n", - my_progname); + my_getopt_error_reporter(ERROR_LEVEL, + "%s: Option '--set-variable' requires an argument\n", + my_progname); return EXIT_ARGUMENT_REQUIRED; } cur_arg= *pos; @@ -215,10 +220,11 @@ int handle_options(int *argc, char ***argv, if (opt_found > 1) { if (my_getopt_print_errors) - reporter(ERROR_LEVEL, - "%s: ambiguous option '--%s-%s' (--%s-%s)\n", - my_progname, special_opt_prefix[i], opt_str, - special_opt_prefix[i], prev_found); + my_getopt_error_reporter(ERROR_LEVEL, + "%s: ambiguous option '--%s-%s' (--%s-%s)\n", + my_progname, special_opt_prefix[i], + cur_arg, special_opt_prefix[i], + prev_found); return EXIT_AMBIGUOUS_OPTION; } switch (i) { @@ -250,16 +256,20 @@ int handle_options(int *argc, char ***argv, if (must_be_var) { if (my_getopt_print_errors) - reporter(option_is_loose ? WARNING_LEVEL : ERROR_LEVEL, - "%s: unknown variable '%s'\n", my_progname, cur_arg); + my_getopt_error_reporter(option_is_loose ? + WARNING_LEVEL : ERROR_LEVEL, + "%s: unknown variable '%s'\n", + my_progname, cur_arg); if (!option_is_loose) return EXIT_UNKNOWN_VARIABLE; } else { if (my_getopt_print_errors) - reporter(option_is_loose ? WARNING_LEVEL : ERROR_LEVEL, - "%s: unknown option '--%s'\n", my_progname, cur_arg); + my_getopt_error_reporter(option_is_loose ? + WARNING_LEVEL : ERROR_LEVEL, + "%s: unknown option '--%s'\n", + my_progname, cur_arg); if (!option_is_loose) return EXIT_UNKNOWN_OPTION; } @@ -275,15 +285,18 @@ int handle_options(int *argc, char ***argv, if (must_be_var) { if (my_getopt_print_errors) - reporter(ERROR_LEVEL, "%s: variable prefix '%s' is not unique\n", - my_progname, opt_str); + my_getopt_error_reporter(ERROR_LEVEL, + "%s: variable prefix '%s' is not unique\n", + my_progname, opt_str); return EXIT_VAR_PREFIX_NOT_UNIQUE; } else { if (my_getopt_print_errors) - reporter(ERROR_LEVEL, "%s: ambiguous option '--%s' (%s, %s)\n", - my_progname, opt_str, prev_found, optp->name); + my_getopt_error_reporter(ERROR_LEVEL, + "%s: ambiguous option '--%s' (%s, %s)\n", + my_progname, opt_str, prev_found, + optp->name); return EXIT_AMBIGUOUS_OPTION; } } @@ -303,8 +316,9 @@ int handle_options(int *argc, char ***argv, if (must_be_var && (optp->var_type & GET_TYPE_MASK) == GET_NO_ARG) { if (my_getopt_print_errors) - reporter(ERROR_LEVEL, "%s: option '%s' cannot take an argument\n", - my_progname, optp->name); + my_getopt_error_reporter(ERROR_LEVEL, + "%s: option '%s' cannot take an argument\n", + my_progname, optp->name); return EXIT_NO_ARGUMENT_ALLOWED; } value= optp->var_type & GET_ASK_ADDR ? @@ -315,8 +329,9 @@ int handle_options(int *argc, char ***argv, if (optend && (optp->var_type & GET_TYPE_MASK) != GET_BOOL) { if (my_getopt_print_errors) - reporter(ERROR_LEVEL, "%s: option '--%s' cannot take an argument\n", - my_progname, optp->name); + my_getopt_error_reporter(ERROR_LEVEL, + "%s: option '--%s' cannot take an argument\n", + my_progname, optp->name); return EXIT_NO_ARGUMENT_ALLOWED; } if ((optp->var_type & GET_TYPE_MASK) == GET_BOOL) @@ -354,8 +369,9 @@ int handle_options(int *argc, char ***argv, if (!*++pos) { if (my_getopt_print_errors) - reporter(ERROR_LEVEL, "%s: option '--%s' requires an argument\n", - my_progname, optp->name); + my_getopt_error_reporter(ERROR_LEVEL, + "%s: option '--%s' requires an argument\n", + my_progname, optp->name); return EXIT_ARGUMENT_REQUIRED; } argument= *pos; @@ -413,9 +429,9 @@ int handle_options(int *argc, char ***argv, if (!pos[1]) { if (my_getopt_print_errors) - reporter(ERROR_LEVEL, - "%s: option '-%c' requires an argument\n", - my_progname, optp->id); + my_getopt_error_reporter(ERROR_LEVEL, + "%s: option '-%c' requires an argument\n", + my_progname, optp->id); return EXIT_ARGUMENT_REQUIRED; } argument= *++pos; @@ -426,9 +442,9 @@ int handle_options(int *argc, char ***argv, if ((error= setval(optp, optp->value, argument, set_maximum_value))) { - reporter(ERROR_LEVEL, - "%s: Error while setting value '%s' to '%s'\n", - my_progname, argument, optp->name); + my_getopt_error_reporter(ERROR_LEVEL, + "%s: Error while setting value '%s' to '%s'\n", + my_progname, argument, optp->name); return error; } get_one_option(optp->id, optp, argument); @@ -438,8 +454,9 @@ int handle_options(int *argc, char ***argv, if (!opt_found) { if (my_getopt_print_errors) - reporter(ERROR_LEVEL, - "%s: unknown option '-%c'\n", my_progname, *optend); + my_getopt_error_reporter(ERROR_LEVEL, + "%s: unknown option '-%c'\n", + my_progname, *optend); return EXIT_UNKNOWN_OPTION; } } @@ -448,9 +465,9 @@ int handle_options(int *argc, char ***argv, } if ((error= setval(optp, value, argument, set_maximum_value))) { - reporter(ERROR_LEVEL, - "%s: Error while setting value '%s' to '%s'\n", - my_progname, argument, optp->name); + my_getopt_error_reporter(ERROR_LEVEL, + "%s: Error while setting value '%s' to '%s'\n", + my_progname, argument, optp->name); return error; } get_one_option(optp->id, optp, argument); |