diff options
Diffstat (limited to 'mysys/my_getopt.c')
-rw-r--r-- | mysys/my_getopt.c | 159 |
1 files changed, 126 insertions, 33 deletions
diff --git a/mysys/my_getopt.c b/mysys/my_getopt.c index 3aad6152dfd..5132ac820b8 100644 --- a/mysys/my_getopt.c +++ b/mysys/my_getopt.c @@ -86,6 +86,7 @@ static void default_reporter(enum loglevel level, fprintf(stderr, "%s", "Info: "); vfprintf(stderr, format, args); va_end(args); + fputc('\n', stderr); fflush(stderr); } @@ -153,7 +154,7 @@ int handle_options(int *argc, char ***argv, { if (my_getopt_print_errors) my_getopt_error_reporter(ERROR_LEVEL, - "%s: Option '-O' requires an argument\n", + "%s: Option '-O' requires an argument", my_progname); return EXIT_ARGUMENT_REQUIRED; } @@ -171,7 +172,7 @@ int handle_options(int *argc, char ***argv, { if (my_getopt_print_errors) my_getopt_error_reporter(ERROR_LEVEL, - "%s: Option '--set-variable' requires an argument\n", + "%s: Option '--set-variable' requires an argument", my_progname); return EXIT_ARGUMENT_REQUIRED; } @@ -185,7 +186,7 @@ int handle_options(int *argc, char ***argv, { if (my_getopt_print_errors) my_getopt_error_reporter(ERROR_LEVEL, - "%s: Option '--set-variable' requires an argument\n", + "%s: Option '--set-variable' requires an argument", my_progname); return EXIT_ARGUMENT_REQUIRED; } @@ -247,7 +248,7 @@ int handle_options(int *argc, char ***argv, { if (my_getopt_print_errors) my_getopt_error_reporter(ERROR_LEVEL, - "%s: ambiguous option '--%s-%s' (--%s-%s)\n", + "%s: ambiguous option '--%s-%s' (--%s-%s)", my_progname, special_opt_prefix[i], cur_arg, special_opt_prefix[i], prev_found); @@ -298,7 +299,7 @@ int handle_options(int *argc, char ***argv, if (my_getopt_print_errors) my_getopt_error_reporter(option_is_loose ? WARNING_LEVEL : ERROR_LEVEL, - "%s: unknown variable '%s'\n", + "%s: unknown variable '%s'", my_progname, cur_arg); if (!option_is_loose) return EXIT_UNKNOWN_VARIABLE; @@ -308,7 +309,7 @@ int handle_options(int *argc, char ***argv, if (my_getopt_print_errors) my_getopt_error_reporter(option_is_loose ? WARNING_LEVEL : ERROR_LEVEL, - "%s: unknown option '--%s'\n", + "%s: unknown option '--%s'", my_progname, cur_arg); if (!option_is_loose) return EXIT_UNKNOWN_OPTION; @@ -326,7 +327,7 @@ int handle_options(int *argc, char ***argv, { if (my_getopt_print_errors) my_getopt_error_reporter(ERROR_LEVEL, - "%s: variable prefix '%s' is not unique\n", + "%s: variable prefix '%s' is not unique", my_progname, opt_str); return EXIT_VAR_PREFIX_NOT_UNIQUE; } @@ -334,7 +335,7 @@ int handle_options(int *argc, char ***argv, { if (my_getopt_print_errors) my_getopt_error_reporter(ERROR_LEVEL, - "%s: ambiguous option '--%s' (%s, %s)\n", + "%s: ambiguous option '--%s' (%s, %s)", my_progname, opt_str, prev_found, optp->name); return EXIT_AMBIGUOUS_OPTION; @@ -357,7 +358,7 @@ int handle_options(int *argc, char ***argv, { if (my_getopt_print_errors) my_getopt_error_reporter(ERROR_LEVEL, - "%s: option '%s' cannot take an argument\n", + "%s: option '%s' cannot take an argument", my_progname, optp->name); return EXIT_NO_ARGUMENT_ALLOWED; } @@ -370,7 +371,7 @@ int handle_options(int *argc, char ***argv, { if (my_getopt_print_errors) my_getopt_error_reporter(ERROR_LEVEL, - "%s: option '--%s' cannot take an argument\n", + "%s: option '--%s' cannot take an argument", my_progname, optp->name); return EXIT_NO_ARGUMENT_ALLOWED; } @@ -392,7 +393,7 @@ int handle_options(int *argc, char ***argv, { my_getopt_error_reporter(WARNING_LEVEL, "%s: ignoring option '--%s' due to \ -invalid value '%s'\n", +invalid value '%s'", my_progname, optp->name, optend); continue; } @@ -423,7 +424,7 @@ invalid value '%s'\n", { if (my_getopt_print_errors) my_getopt_error_reporter(ERROR_LEVEL, - "%s: option '--%s' requires an argument\n", + "%s: option '--%s' requires an argument", my_progname, optp->name); return EXIT_ARGUMENT_REQUIRED; } @@ -483,7 +484,7 @@ invalid value '%s'\n", { if (my_getopt_print_errors) my_getopt_error_reporter(ERROR_LEVEL, - "%s: option '-%c' requires an argument\n", + "%s: option '-%c' requires an argument", my_progname, optp->id); return EXIT_ARGUMENT_REQUIRED; } @@ -496,7 +497,7 @@ invalid value '%s'\n", set_maximum_value))) { my_getopt_error_reporter(ERROR_LEVEL, - "%s: Error while setting value '%s' to '%s'\n", + "%s: Error while setting value '%s' to '%s'", my_progname, argument, optp->name); return error; } @@ -508,7 +509,7 @@ invalid value '%s'\n", { if (my_getopt_print_errors) my_getopt_error_reporter(ERROR_LEVEL, - "%s: unknown option '-%c'\n", + "%s: unknown option '-%c'", my_progname, *optend); return EXIT_UNKNOWN_OPTION; } @@ -519,7 +520,7 @@ invalid value '%s'\n", if ((error= setval(optp, value, argument, set_maximum_value))) { my_getopt_error_reporter(ERROR_LEVEL, - "%s: Error while setting value '%s' to '%s'\n", + "%s: Error while setting value '%s' to '%s'", my_progname, argument, optp->name); return error; } @@ -608,13 +609,17 @@ static int setval(const struct my_option *opts, uchar* *value, char *argument, *((my_bool*) result_pos)= (my_bool) atoi(argument) != 0; break; case GET_INT: - case GET_UINT: /* fall through */ *((int*) result_pos)= (int) getopt_ll(argument, opts, &err); break; + case GET_UINT: + *((uint*) result_pos)= (uint) getopt_ull(argument, opts, &err); + break; case GET_LONG: - case GET_ULONG: /* fall through */ *((long*) result_pos)= (long) getopt_ll(argument, opts, &err); break; + case GET_ULONG: + *((long*) result_pos)= (long) getopt_ull(argument, opts, &err); + break; case GET_LL: *((longlong*) result_pos)= getopt_ll(argument, opts, &err); break; @@ -778,23 +783,70 @@ static longlong eval_num_suffix(char *argument, int *error, char *option_name) static longlong getopt_ll(char *arg, const struct my_option *optp, int *err) { - longlong num; + longlong num=eval_num_suffix(arg, err, (char*) optp->name); + return getopt_ll_limit_value(num, optp, NULL); +} + +/* + function: getopt_ll_limit_value + + Applies min/max/block_size to a numeric value of an option. + Returns "fixed" value. +*/ + +longlong getopt_ll_limit_value(longlong num, const struct my_option *optp, + bool *fix) +{ + longlong old= num; + bool adjusted= FALSE; + char buf1[255], buf2[255]; ulonglong block_size= (optp->block_size ? (ulonglong) optp->block_size : 1L); - - num= eval_num_suffix(arg, err, (char*) optp->name); - if (num > 0 && (ulonglong) num > (ulonglong) optp->max_value && + + if (num > 0 && ((ulonglong) num > (ulonglong) optp->max_value) && optp->max_value) /* if max value is not set -> no upper limit */ { - char buf[22]; - my_getopt_error_reporter(WARNING_LEVEL, - "Truncated incorrect %s value: '%s'", - optp->name, llstr(num, buf)); - num= (ulonglong) optp->max_value; + adjusted= TRUE; } + + switch ((optp->var_type & GET_TYPE_MASK)) { + case GET_INT: + if (num > (longlong) INT_MAX) + { + num= ((longlong) INT_MAX); + adjusted= TRUE; + } + break; + case GET_LONG: +#if SIZEOF_LONG < SIZEOF_LONG_LONG + if (num > (longlong) LONG_MAX) + { + num= ((longlong) LONG_MAX); + adjusted= TRUE; + } +#endif + break; + default: + DBUG_ASSERT((optp->var_type & GET_TYPE_MASK) == GET_LL); + break; + } + num= ((num - optp->sub_size) / block_size); num= (longlong) (num * block_size); - return max(num, optp->min_value); + + if (num < optp->min_value) + { + num= optp->min_value; + adjusted= TRUE; + } + + if (fix) + *fix= adjusted; + else if (adjusted) + my_getopt_error_reporter(WARNING_LEVEL, + "option '%s': signed value %s adjusted to %s", + optp->name, llstr(old, buf1), llstr(num, buf2)); + return num; } /* @@ -806,25 +858,66 @@ static longlong getopt_ll(char *arg, const struct my_option *optp, int *err) static ulonglong getopt_ull(char *arg, const struct my_option *optp, int *err) { - ulonglong num; - - num= eval_num_suffix(arg, err, (char*) optp->name); - return getopt_ull_limit_value(num, optp); + ulonglong num= eval_num_suffix(arg, err, (char*) optp->name); + return getopt_ull_limit_value(num, optp, NULL); } -ulonglong getopt_ull_limit_value(ulonglong num, const struct my_option *optp) +ulonglong getopt_ull_limit_value(ulonglong num, const struct my_option *optp, + bool *fix) { + bool adjusted= FALSE; + ulonglong old= num; + char buf1[255], buf2[255]; + if ((ulonglong) num > (ulonglong) optp->max_value && optp->max_value) /* if max value is not set -> no upper limit */ + { num= (ulonglong) optp->max_value; + adjusted= TRUE; + } + + switch ((optp->var_type & GET_TYPE_MASK)) { + case GET_UINT: + if (num > (ulonglong) UINT_MAX) + { + num= ((ulonglong) UINT_MAX); + adjusted= TRUE; + } + break; + case GET_ULONG: +#if SIZEOF_LONG < SIZEOF_LONG_LONG + if (num > (ulonglong) ULONG_MAX) + { + num= ((ulonglong) ULONG_MAX); + adjusted= TRUE; + } +#endif + break; + default: + DBUG_ASSERT((optp->var_type & GET_TYPE_MASK) == GET_ULL); + break; + } + if (optp->block_size > 1) { num/= (ulonglong) optp->block_size; num*= (ulonglong) optp->block_size; } + if (num < (ulonglong) optp->min_value) + { num= (ulonglong) optp->min_value; + adjusted= TRUE; + } + + if (fix) + *fix= adjusted; + else if (adjusted) + my_getopt_error_reporter(WARNING_LEVEL, + "option '%s': unsigned value %s adjusted to %s", + optp->name, ullstr(old, buf1), ullstr(num, buf2)); + return num; } |