13
13
along with this program; if not, write to the Free Software
14
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
#include "mysys_priv.h"
17
#include <libdrizzle/gettext.h>
19
#include <mystrings/m_string.h>
16
#include <my_global.h>
20
18
#include <stdlib.h>
21
19
#include <my_sys.h>
22
20
#include <mysys_err.h>
29
27
static void default_reporter(enum loglevel level, const char *format, ...);
30
28
my_error_reporter my_getopt_error_reporter= &default_reporter;
32
static int findopt(char *optpat, uint32_t length,
30
static int findopt(char *optpat, uint length,
33
31
const struct my_option **opt_res,
35
33
static int64_t getopt_ll(char *arg, const struct my_option *optp, int *err);
53
51
static const char *special_opt_prefix[]=
54
52
{"skip", "disable", "enable", "maximum", "loose", 0};
55
static const uint32_t special_opt_prefix_lengths[]=
53
static const uint special_opt_prefix_lengths[]=
56
54
{ 4, 7, 6, 7, 5, 0};
57
55
enum enum_special_opt
58
56
{ OPT_SKIP, OPT_DISABLE, OPT_ENABLE, OPT_MAXIMUM, OPT_LOOSE};
81
79
va_start(args, format);
82
80
if (level == WARNING_LEVEL)
83
fprintf(stderr, "%s", _("Warning: "));
81
fprintf(stderr, "%s", "Warning: ");
84
82
else if (level == INFORMATION_LEVEL)
85
fprintf(stderr, "%s", _("Info: "));
83
fprintf(stderr, "%s", "Info: ");
86
84
vfprintf(stderr, format, args);
88
86
fputc('\n', stderr);
111
109
const struct my_option *longopts,
112
110
my_get_one_option get_one_option)
114
uint32_t opt_found, argvpos= 0, length;
115
bool end_of_options= 0, must_be_var, set_maximum_value=false,
112
uint opt_found, argvpos= 0, length;
113
bool end_of_options= 0, must_be_var, set_maximum_value,
117
115
char **pos, **pos_end, *optend, *prev_found=NULL,
118
116
*opt_str, key_name[FN_REFLEN];
123
121
/* handle_options() assumes arg0 (program name) always exists */
124
assert(argc && *argc >= 1);
125
assert(argv && *argv);
122
DBUG_ASSERT(argc && *argc >= 1);
123
DBUG_ASSERT(argv && *argv);
126
124
(*argc)--; /* Skip the program name */
127
125
(*argv)++; /* --- || ---- */
128
126
init_variables(longopts, init_one_value);
205
203
opt_str= check_struct_option(cur_arg, key_name);
206
optend= strrchr(opt_str, '=');
209
length= (uint) (optend - opt_str);
204
optend= strcend(opt_str, '=');
205
length= (uint) (optend - opt_str);
214
length= strlen(opt_str);
219
212
Find first the right option. Return error in case of an ambiguous,
261
254
case OPT_DISABLE: /* fall through */
263
256
double negation is actually enable again,
264
for example: --skip-option=0 -> option = true
257
for example: --skip-option=0 -> option = TRUE
266
259
optend= (optend && *optend == '0' && !(*(optend + 1))) ?
267
260
(char*) "1" : disabled_my_option;
348
341
if (my_getopt_print_errors)
350
_("%s: %s: Option '%s' used, but is disabled\n"), my_progname,
351
option_is_loose ? _("WARNING") : _("ERROR"), opt_str);
343
"%s: %s: Option '%s' used, but is disabled\n", my_progname,
344
option_is_loose ? "WARNING" : "ERROR", opt_str);
352
345
if (option_is_loose)
388
381
if (!optend || *optend == '1' ||
389
!my_strcasecmp(&my_charset_utf8_general_ci, optend, "true"))
382
!my_strcasecmp(&my_charset_latin1, optend, "true"))
390
383
*((bool*) value)= (bool) 1;
391
384
else if (*optend == '0' ||
392
!my_strcasecmp(&my_charset_utf8_general_ci, optend, "false"))
385
!my_strcasecmp(&my_charset_latin1, optend, "false"))
393
386
*((bool*) value)= (bool) 0;
452
445
if (my_getopt_print_errors)
454
_("%s: ERROR: Option '-%c' used, but is disabled\n"),
447
"%s: ERROR: Option '-%c' used, but is disabled\n",
455
448
my_progname, optp->id);
456
449
return EXIT_OPTION_DISABLED;
498
491
if ((error= setval(optp, optp->value, argument,
501
494
my_getopt_error_reporter(ERROR_LEVEL,
502
495
"%s: Error while setting value '%s' to '%s'",
503
496
my_progname, argument, optp->name);
506
get_one_option(optp->id, optp, argument);
512
if (my_getopt_print_errors)
499
get_one_option(optp->id, optp, argument);
505
if (my_getopt_print_errors)
513
506
my_getopt_error_reporter(ERROR_LEVEL,
514
507
"%s: unknown option '-%c'",
515
508
my_progname, *optend);
516
return EXIT_UNKNOWN_OPTION;
519
(*argc)--; /* option handled (short), decrease argument count */
509
return EXIT_UNKNOWN_OPTION;
512
(*argc)--; /* option handled (short), decrease argument count */
522
515
if ((error= setval(optp, value, argument, set_maximum_value)))
524
517
my_getopt_error_reporter(ERROR_LEVEL,
525
518
"%s: Error while setting value '%s' to '%s'",
526
519
my_progname, argument, optp->name);
529
522
get_one_option(optp->id, optp, argument);
565
ptr= strrchr(cur_arg + 1, '.'); /* Skip the first character */
566
end= strrchr(cur_arg, '=');
558
ptr= strcend(cur_arg + 1, '.'); /* Skip the first character */
559
end= strcend(cur_arg, '=');
569
562
If the first dot is after an equal sign, then it is part
572
565
NULL, or the character right before equal sign is the first
573
566
dot found, the option is not a struct option.
575
if ((ptr != NULL) && (end != NULL) && (end - ptr > 1))
577
uint32_t len= (uint) (ptr - cur_arg);
570
uint len= (uint) (ptr - cur_arg);
578
571
set_if_smaller(len, FN_REFLEN-1);
579
572
strmake(key_name, cur_arg, len);
593
586
Will set the option value to given value
596
static int setval(const struct my_option *opts, char **value, char *argument,
597
bool set_maximum_value)
589
static int setval(const struct my_option *opts, char* *value, char *argument,
590
bool set_maximum_value)
637
630
case GET_STR_ALLOC:
638
631
if ((*((char**) result_pos)))
639
free((*(char**) result_pos));
632
my_free((*(char**) result_pos), MYF(MY_WME | MY_FAE));
640
633
if (!(*((char**) result_pos)= my_strdup(argument, MYF(MY_WME))))
641
634
return EXIT_OUT_OF_MEMORY;
681
674
ffname points to first matching option
684
static int findopt(char *optpat, uint32_t length,
677
static int findopt(char *optpat, uint length,
685
678
const struct my_option **opt_res,
689
682
struct my_option *opt= (struct my_option *) *opt_res;
691
684
for (count= 0; opt->name; opt++)
724
717
bool getopt_compare_strings(register const char *s, register const char *t,
727
720
char const *end= s + length;
728
721
for (;s != end ; s++, t++)
764
757
else if (*endchar)
767
_("Unknown suffix '%c' used for variable '%s' (value '%s')\n"),
760
"Unknown suffix '%c' used for variable '%s' (value '%s')\n",
768
761
*endchar, option_name, argument);
945
938
if (end[0] != 0 || error)
948
_("%s: ERROR: Invalid decimal value for option '%s'\n"),
941
"%s: ERROR: Invalid decimal value for option '%s'\n",
949
942
my_progname, optp->name);
950
943
*err= EXIT_ARGUMENT_INVALID;
953
946
if (optp->max_value && num > (double) optp->max_value)
954
947
num= (double) optp->max_value;
955
return cmax(num, (double) optp->min_value);
948
return max(num, (double) optp->min_value);
967
960
static void init_one_value(const struct my_option *option, char** variable,
963
DBUG_ENTER("init_one_value");
970
964
switch ((option->var_type & GET_TYPE_MASK)) {
972
966
*((bool*) variable)= (bool) value;
1014
1008
if ((char*) (intptr_t) value)
1016
free((*(char**) variable));
1010
my_free((*(char**) variable), MYF(MY_ALLOW_ZERO_PTR));
1017
1011
*((char**) variable)= my_strdup((char*) (intptr_t) value, MYF(MY_WME));
1020
1014
default: /* dummy default to avoid compiler warnings */
1036
1030
static void fini_one_value(const struct my_option *option, char **variable,
1037
1031
int64_t value __attribute__ ((unused)))
1033
DBUG_ENTER("fini_one_value");
1039
1034
switch ((option->var_type & GET_TYPE_MASK)) {
1040
1035
case GET_STR_ALLOC:
1041
free((*(char**) variable));
1036
my_free((*(char**) variable), MYF(MY_ALLOW_ZERO_PTR));
1042
1037
*((char**) variable)= NULL;
1044
1039
default: /* dummy default to avoid compiler warnings */
1070
1065
static void init_variables(const struct my_option *options,
1071
1066
init_func_p init_one_value)
1068
DBUG_ENTER("init_variables");
1073
1069
for (; options->name; options++)
1075
1071
char* *variable;
1072
DBUG_PRINT("options", ("name: '%s'", options->name));
1077
1074
We must set u_max_value first as for some variables
1078
1075
options->u_max_value == options->value and in this case we want to
1096
1093
Print help for all options and variables.
1096
#include <help_start.h>
1099
1098
void my_print_help(const struct my_option *options)
1101
uint32_t col, name_space= 22, comment_space= 57;
1100
uint col, name_space= 22, comment_space= 57;
1102
1101
const char *line_end;
1103
1102
const struct my_option *optp;
1148
1147
if (optp->comment && *optp->comment)
1150
const char *comment= _(optp->comment), *end= strchr(comment, '\0');
1149
const char *comment= optp->comment, *end= strend(comment);
1152
1151
while ((uint) (end - comment) > comment_space)
1168
1167
if (optp->def_value != 0)
1170
printf(_("%*s(Defaults to on; use --skip-%s to disable.)\n"), name_space, "", optp->name);
1169
printf("%*s(Defaults to on; use --skip-%s to disable.)\n", name_space, "", optp->name);
1183
1182
void my_print_variables(const struct my_option *options)
1185
uint32_t name_space= 34, length, nr;
1184
uint name_space= 34, length, nr;
1186
1185
uint64_t bit, llvalue;
1187
1186
char buff[255];
1188
1187
const struct my_option *optp;
1190
printf(_("\nVariables (--variable-name=value)\n"
1191
"and boolean options {false|true} Value (after reading options)\n"
1192
"--------------------------------- -----------------------------\n"));
1189
printf("\nVariables (--variable-name=value)\n");
1190
printf("and boolean options {FALSE|TRUE} Value (after reading options)\n");
1191
printf("--------------------------------- -----------------------------\n");
1193
1192
for (optp= options; optp->id; optp++)
1195
1194
char* *value= (optp->var_type & GET_ASK_ADDR ?
1203
1202
switch ((optp->var_type & GET_TYPE_MASK)) {
1205
1204
if (!(llvalue= *(uint64_t*) value))
1206
printf("%s\n", _("(No default value)"));
1205
printf("%s\n", "(No default value)");
1208
1207
for (nr= 0, bit= 1; llvalue && nr < optp->typelib->count; nr++, bit<<=1)
1220
1219
case GET_STR_ALLOC: /* fall through */
1221
1220
printf("%s\n", *((char**) value) ? *((char**) value) :
1222
_("(No default value)"));
1221
"(No default value)");
1225
printf("%s\n", *((bool*) value) ? _("true") : _("false"));
1224
printf("%s\n", *((bool*) value) ? "TRUE" : "FALSE");
1228
1227
printf("%d\n", *((int*) value));
1234
1233
printf("%ld\n", *((long*) value));
1236
1235
case GET_ULONG:
1237
printf("%u\n", *((uint32_t*) value));
1236
printf("%lu\n", *((ulong*) value));
1240
1239
printf("%s\n", llstr(*((int64_t*) value), buff));