1
/* Copyright (C) 2002-2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
#include <my_global.h>
20
#include <mysys_err.h>
21
#include <my_getopt.h>
24
typedef void (*init_func_p)(const struct my_option *option, uchar* *variable,
27
static void default_reporter(enum loglevel level, const char *format, ...);
28
my_error_reporter my_getopt_error_reporter= &default_reporter;
30
static int findopt(char *optpat, uint length,
31
const struct my_option **opt_res,
33
my_bool getopt_compare_strings(const char *s,
36
static longlong getopt_ll(char *arg, const struct my_option *optp, int *err);
37
static ulonglong getopt_ull(char *arg, const struct my_option *optp,
39
static double getopt_double(char *arg, const struct my_option *optp, int *err);
40
static void init_variables(const struct my_option *options,
41
init_func_p init_one_value);
42
static void init_one_value(const struct my_option *option, uchar* *variable,
44
static void fini_one_value(const struct my_option *option, uchar* *variable,
46
static int setval(const struct my_option *opts, uchar* *value, char *argument,
47
my_bool set_maximum_value);
48
static char *check_struct_option(char *cur_arg, char *key_name);
51
The following three variables belong to same group and the number and
52
order of their arguments must correspond to each other.
54
static const char *special_opt_prefix[]=
55
{"skip", "disable", "enable", "maximum", "loose", 0};
56
static const uint special_opt_prefix_lengths[]=
59
{ OPT_SKIP, OPT_DISABLE, OPT_ENABLE, OPT_MAXIMUM, OPT_LOOSE};
61
char *disabled_my_option= (char*) "0";
64
This is a flag that can be set in client programs. 0 means that
65
my_getopt will not print error messages, but the client should do
69
my_bool my_getopt_print_errors= 1;
72
This is a flag that can be set in client programs. 1 means that
73
my_getopt will skip over options it does not know how to handle.
76
my_bool my_getopt_skip_unknown= 0;
78
static void default_reporter(enum loglevel level,
79
const char *format, ...)
82
va_start(args, format);
83
if (level == WARNING_LEVEL)
84
fprintf(stderr, "%s", "Warning: ");
85
else if (level == INFORMATION_LEVEL)
86
fprintf(stderr, "%s", "Info: ");
87
vfprintf(stderr, format, args);
94
function: handle_options
96
Sort options; put options first, until special end of options (--), or
97
until end of argv. Parse options; check that the given option matches with
98
one of the options in struct 'my_option', return error in case of ambiguous
99
or unknown option. Check that option was given an argument if it requires
100
one. Call function 'get_one_option()' once for each option.
103
static uchar** (*getopt_get_addr)(const char *, uint, const struct my_option *);
105
void my_getopt_register_get_addr(uchar** (*func_addr)(const char *, uint,
106
const struct my_option *))
108
getopt_get_addr= func_addr;
111
int handle_options(int *argc, char ***argv,
112
const struct my_option *longopts,
113
my_get_one_option get_one_option)
115
uint opt_found, argvpos= 0, length;
116
my_bool end_of_options= 0, must_be_var, set_maximum_value,
118
char **pos, **pos_end, *optend, *prev_found,
119
*opt_str, key_name[FN_REFLEN];
120
const struct my_option *optp;
124
/* handle_options() assumes arg0 (program name) always exists */
125
DBUG_ASSERT(argc && *argc >= 1);
126
DBUG_ASSERT(argv && *argv);
127
(*argc)--; /* Skip the program name */
128
(*argv)++; /* --- || ---- */
129
init_variables(longopts, init_one_value);
131
for (pos= *argv, pos_end=pos+ *argc; pos != pos_end ; pos++)
135
if (cur_arg[0] == '-' && cur_arg[1] && !end_of_options) /* must be opt */
139
set_maximum_value= 0;
142
cur_arg++; /* skip '-' */
143
if (*cur_arg == '-' || *cur_arg == 'O') /* check for long option, */
144
{ /* --set-variable, or -O */
149
if (!(*++cur_arg)) /* If not -Ovar=# */
151
/* the argument must be in next argv */
154
if (my_getopt_print_errors)
155
my_getopt_error_reporter(ERROR_LEVEL,
156
"%s: Option '-O' requires an argument",
158
return EXIT_ARGUMENT_REQUIRED;
164
else if (!getopt_compare_strings(cur_arg, "-set-variable", 13))
167
if (cur_arg[13] == '=')
172
if (my_getopt_print_errors)
173
my_getopt_error_reporter(ERROR_LEVEL,
174
"%s: Option '--set-variable' requires an argument",
176
return EXIT_ARGUMENT_REQUIRED;
179
else if (cur_arg[14]) /* garbage, or another option. break out */
183
/* the argument must be in next argv */
186
if (my_getopt_print_errors)
187
my_getopt_error_reporter(ERROR_LEVEL,
188
"%s: Option '--set-variable' requires an argument",
190
return EXIT_ARGUMENT_REQUIRED;
196
else if (!must_be_var)
198
if (!*++cur_arg) /* skip the double dash */
200
/* '--' means end of options, look no further */
206
opt_str= check_struct_option(cur_arg, key_name);
207
optend= strcend(opt_str, '=');
208
length= (uint) (optend - opt_str);
215
Find first the right option. Return error in case of an ambiguous,
219
if (!(opt_found= findopt(opt_str, length, &optp, &prev_found)))
222
Didn't find any matching option. Let's see if someone called
223
option with a special option prefix
228
must_be_var= 1; /* option is followed by an argument */
229
for (i= 0; special_opt_prefix[i]; i++)
231
if (!getopt_compare_strings(special_opt_prefix[i], opt_str,
232
special_opt_prefix_lengths[i]) &&
233
(opt_str[special_opt_prefix_lengths[i]] == '-' ||
234
opt_str[special_opt_prefix_lengths[i]] == '_'))
237
We were called with a special prefix, we can reuse opt_found
239
opt_str+= special_opt_prefix_lengths[i] + 1;
240
length-= special_opt_prefix_lengths[i] + 1;
243
if ((opt_found= findopt(opt_str, length, &optp, &prev_found)))
247
if (my_getopt_print_errors)
248
my_getopt_error_reporter(ERROR_LEVEL,
249
"%s: ambiguous option '--%s-%s' (--%s-%s)",
250
my_progname, special_opt_prefix[i],
251
cur_arg, special_opt_prefix[i],
253
return EXIT_AMBIGUOUS_OPTION;
257
case OPT_DISABLE: /* fall through */
259
double negation is actually enable again,
260
for example: --skip-option=0 -> option = TRUE
262
optend= (optend && *optend == '0' && !(*(optend + 1))) ?
263
(char*) "1" : disabled_my_option;
266
optend= (optend && *optend == '0' && !(*(optend + 1))) ?
267
disabled_my_option : (char*) "1";
270
set_maximum_value= 1;
274
break; /* break from the inner loop, main loop continues */
276
i= -1; /* restart the loop */
282
if (my_getopt_skip_unknown)
285
preserve all the components of this unknown option, this may
286
occurr when the user provides options like: "-O foo" or
287
"--set-variable foo" (note that theres a space in there)
288
Generally, these kind of options are to be avoided
291
(*argv)[argvpos++]= *first++;
292
} while (first <= pos);
297
if (my_getopt_print_errors)
298
my_getopt_error_reporter(option_is_loose ?
299
WARNING_LEVEL : ERROR_LEVEL,
300
"%s: unknown variable '%s'",
301
my_progname, cur_arg);
302
if (!option_is_loose)
303
return EXIT_UNKNOWN_VARIABLE;
307
if (my_getopt_print_errors)
308
my_getopt_error_reporter(option_is_loose ?
309
WARNING_LEVEL : ERROR_LEVEL,
310
"%s: unknown option '--%s'",
311
my_progname, cur_arg);
312
if (!option_is_loose)
313
return EXIT_UNKNOWN_OPTION;
326
if (my_getopt_print_errors)
327
my_getopt_error_reporter(ERROR_LEVEL,
328
"%s: variable prefix '%s' is not unique",
329
my_progname, opt_str);
330
return EXIT_VAR_PREFIX_NOT_UNIQUE;
334
if (my_getopt_print_errors)
335
my_getopt_error_reporter(ERROR_LEVEL,
336
"%s: ambiguous option '--%s' (%s, %s)",
337
my_progname, opt_str, prev_found,
339
return EXIT_AMBIGUOUS_OPTION;
342
if ((optp->var_type & GET_TYPE_MASK) == GET_DISABLED)
344
if (my_getopt_print_errors)
346
"%s: %s: Option '%s' used, but is disabled\n", my_progname,
347
option_is_loose ? "WARNING" : "ERROR", opt_str);
353
return EXIT_OPTION_DISABLED;
355
if (must_be_var && (optp->var_type & GET_TYPE_MASK) == GET_NO_ARG)
357
if (my_getopt_print_errors)
358
my_getopt_error_reporter(ERROR_LEVEL,
359
"%s: option '%s' cannot take an argument",
360
my_progname, optp->name);
361
return EXIT_NO_ARGUMENT_ALLOWED;
363
value= optp->var_type & GET_ASK_ADDR ?
364
(*getopt_get_addr)(key_name, (uint) strlen(key_name), optp) : optp->value;
366
if (optp->arg_type == NO_ARG)
368
if (optend && (optp->var_type & GET_TYPE_MASK) != GET_BOOL)
370
if (my_getopt_print_errors)
371
my_getopt_error_reporter(ERROR_LEVEL,
372
"%s: option '--%s' cannot take an argument",
373
my_progname, optp->name);
374
return EXIT_NO_ARGUMENT_ALLOWED;
376
if ((optp->var_type & GET_TYPE_MASK) == GET_BOOL)
379
Set bool to 1 if no argument or if the user has used
380
--enable-'option-name'.
381
*optend was set to '0' if one used --disable-option
384
if (!optend || *optend == '1' ||
385
!my_strcasecmp(&my_charset_latin1, optend, "true"))
386
*((my_bool*) value)= (my_bool) 1;
387
else if (*optend == '0' ||
388
!my_strcasecmp(&my_charset_latin1, optend, "false"))
389
*((my_bool*) value)= (my_bool) 0;
392
my_getopt_error_reporter(WARNING_LEVEL,
393
"%s: ignoring option '--%s' due to \
395
my_progname, optp->name, optend);
398
get_one_option(optp->id, optp,
399
*((my_bool*) value) ?
400
(char*) "1" : disabled_my_option);
405
else if (optp->arg_type == OPT_ARG &&
406
(optp->var_type & GET_TYPE_MASK) == GET_BOOL)
408
if (optend == disabled_my_option)
409
*((my_bool*) value)= (my_bool) 0;
412
if (!optend) /* No argument -> enable option */
413
*((my_bool*) value)= (my_bool) 1;
418
else if (optp->arg_type == REQUIRED_ARG && !optend)
420
/* Check if there are more arguments after this one */
423
if (my_getopt_print_errors)
424
my_getopt_error_reporter(ERROR_LEVEL,
425
"%s: option '--%s' requires an argument",
426
my_progname, optp->name);
427
return EXIT_ARGUMENT_REQUIRED;
435
else /* must be short option */
437
for (optend= cur_arg; *optend; optend++)
440
for (optp= longopts; optp->id; optp++)
442
if (optp->id == (int) (uchar) *optend)
444
/* Option recognized. Find next what to do with it */
446
if ((optp->var_type & GET_TYPE_MASK) == GET_DISABLED)
448
if (my_getopt_print_errors)
450
"%s: ERROR: Option '-%c' used, but is disabled\n",
451
my_progname, optp->id);
452
return EXIT_OPTION_DISABLED;
454
if ((optp->var_type & GET_TYPE_MASK) == GET_BOOL &&
455
optp->arg_type == NO_ARG)
457
*((my_bool*) optp->value)= (my_bool) 1;
458
get_one_option(optp->id, optp, argument);
461
else if (optp->arg_type == REQUIRED_ARG ||
462
optp->arg_type == OPT_ARG)
466
/* The rest of the option is option argument */
467
argument= optend + 1;
468
/* This is in effect a jump out of the outer loop */
473
if (optp->arg_type == OPT_ARG)
475
if (optp->var_type == GET_BOOL)
476
*((my_bool*) optp->value)= (my_bool) 1;
477
get_one_option(optp->id, optp, argument);
480
/* Check if there are more arguments after this one */
483
if (my_getopt_print_errors)
484
my_getopt_error_reporter(ERROR_LEVEL,
485
"%s: option '-%c' requires an argument",
486
my_progname, optp->id);
487
return EXIT_ARGUMENT_REQUIRED;
491
/* the other loop will break, because *optend + 1 == 0 */
494
if ((error= setval(optp, optp->value, argument,
497
my_getopt_error_reporter(ERROR_LEVEL,
498
"%s: Error while setting value '%s' to '%s'",
499
my_progname, argument, optp->name);
502
get_one_option(optp->id, optp, argument);
508
if (my_getopt_print_errors)
509
my_getopt_error_reporter(ERROR_LEVEL,
510
"%s: unknown option '-%c'",
511
my_progname, *optend);
512
return EXIT_UNKNOWN_OPTION;
515
(*argc)--; /* option handled (short), decrease argument count */
518
if ((error= setval(optp, value, argument, set_maximum_value)))
520
my_getopt_error_reporter(ERROR_LEVEL,
521
"%s: Error while setting value '%s' to '%s'",
522
my_progname, argument, optp->name);
525
get_one_option(optp->id, optp, argument);
527
(*argc)--; /* option handled (short or long), decrease argument count */
529
else /* non-option found */
530
(*argv)[argvpos++]= cur_arg;
533
Destroy the first, already handled option, so that programs that look
534
for arguments in 'argv', without checking 'argc', know when to stop.
535
Items in argv, before the destroyed one, are all non-option -arguments
536
to the program, yet to be (possibly) handled.
544
function: check_struct_option
546
Arguments: Current argument under processing from argv and a variable
547
where to store the possible key name.
549
Return value: In case option is a struct option, returns a pointer to
550
the current argument at the position where the struct option (key_name)
551
ends, the next character after the dot. In case argument is not a struct
552
option, returns a pointer to the argument.
554
key_name will hold the name of the key, or 0 if not found.
557
static char *check_struct_option(char *cur_arg, char *key_name)
561
ptr= strcend(cur_arg + 1, '.'); /* Skip the first character */
562
end= strcend(cur_arg, '=');
565
If the first dot is after an equal sign, then it is part
566
of a variable value and the option is not a struct option.
567
Also, if the last character in the string before the ending
568
NULL, or the character right before equal sign is the first
569
dot found, the option is not a struct option.
573
uint len= (uint) (ptr - cur_arg);
574
set_if_smaller(len, FN_REFLEN-1);
575
strmake(key_name, cur_arg, len);
588
Arguments: opts, argument
589
Will set the option value to given value
592
static int setval(const struct my_option *opts, uchar* *value, char *argument,
593
my_bool set_maximum_value)
597
if (value && argument)
599
uchar* *result_pos= ((set_maximum_value) ?
600
opts->u_max_value : value);
603
return EXIT_NO_PTR_TO_VARIABLE;
605
switch ((opts->var_type & GET_TYPE_MASK)) {
606
case GET_BOOL: /* If argument differs from 0, enable option, else disable */
607
*((my_bool*) result_pos)= (my_bool) atoi(argument) != 0;
610
*((int*) result_pos)= (int) getopt_ll(argument, opts, &err);
613
*((uint*) result_pos)= (uint) getopt_ull(argument, opts, &err);
616
*((long*) result_pos)= (long) getopt_ll(argument, opts, &err);
619
*((long*) result_pos)= (long) getopt_ull(argument, opts, &err);
622
*((longlong*) result_pos)= getopt_ll(argument, opts, &err);
625
*((ulonglong*) result_pos)= getopt_ull(argument, opts, &err);
628
*((double*) result_pos)= getopt_double(argument, opts, &err);
631
*((char**) result_pos)= argument;
634
if ((*((char**) result_pos)))
635
my_free((*(char**) result_pos), MYF(MY_WME | MY_FAE));
636
if (!(*((char**) result_pos)= my_strdup(argument, MYF(MY_WME))))
637
return EXIT_OUT_OF_MEMORY;
640
if (((*(int*)result_pos)= find_type(argument, opts->typelib, 2) - 1) < 0)
641
return EXIT_ARGUMENT_INVALID;
644
*((ulonglong*)result_pos)= find_typeset(argument, opts->typelib, &err);
646
return EXIT_ARGUMENT_INVALID;
648
default: /* dummy default to avoid compiler warnings */
652
return EXIT_UNKNOWN_SUFFIX;
663
optpat Prefix of option to find (with - or _)
664
length Length of optpat
666
ffname Place for pointer to first found name
669
Go through all options in the my_option struct. Return number
670
of options found that match the pattern and in the argument
671
list the option found, if any. In case of ambiguous option, store
672
the name in ffname argument
675
0 No matching options
676
# Number of matching options
677
ffname points to first matching option
680
static int findopt(char *optpat, uint length,
681
const struct my_option **opt_res,
685
struct my_option *opt= (struct my_option *) *opt_res;
687
for (count= 0; opt->name; opt++)
689
if (!getopt_compare_strings(opt->name, optpat, length)) /* match found */
692
if (!opt->name[length]) /* Exact match */
697
*ffname= (char *) opt->name; /* We only need to know one prev */
699
else if (strcmp(*ffname, opt->name))
702
The above test is to not count same option twice
703
(see mysql.cc, option "help")
714
function: compare_strings
716
Works like strncmp, other than 1.) considers '-' and '_' the same.
717
2.) Returns -1 if strings differ, 0 if they are equal
720
my_bool getopt_compare_strings(register const char *s, register const char *t,
723
char const *end= s + length;
724
for (;s != end ; s++, t++)
726
if ((*s != '-' ? *s : '_') != (*t != '-' ? *t : '_'))
733
function: eval_num_suffix
735
Transforms a number with a suffix to real number. Suffix can
736
be k|K for kilo, m|M for mega or g|G for giga.
739
static longlong eval_num_suffix(char *argument, int *error, char *option_name)
746
num= strtoll(argument, &endchar, 10);
749
my_getopt_error_reporter(ERROR_LEVEL,
750
"Incorrect integer value: '%s'", argument);
754
if (*endchar == 'k' || *endchar == 'K')
756
else if (*endchar == 'm' || *endchar == 'M')
758
else if (*endchar == 'g' || *endchar == 'G')
759
num*= 1024L * 1024L * 1024L;
763
"Unknown suffix '%c' used for variable '%s' (value '%s')\n",
764
*endchar, option_name, argument);
774
Evaluates and returns the value that user gave as an argument
775
to a variable. Recognizes (case insensitive) K as KILO, M as MEGA
776
and G as GIGA bytes. Some values must be in certain blocks, as
777
defined in the given my_option struct, this function will check
778
that those values are honored.
779
In case of an error, set error value in *err.
782
static longlong getopt_ll(char *arg, const struct my_option *optp, int *err)
784
longlong num=eval_num_suffix(arg, err, (char*) optp->name);
785
return getopt_ll_limit_value(num, optp, NULL);
789
function: getopt_ll_limit_value
791
Applies min/max/block_size to a numeric value of an option.
792
Returns "fixed" value.
795
longlong getopt_ll_limit_value(longlong num, const struct my_option *optp,
799
my_bool adjusted= FALSE;
800
char buf1[255], buf2[255];
801
ulonglong block_size= (optp->block_size ? (ulonglong) optp->block_size : 1L);
803
if (num > 0 && ((ulonglong) num > (ulonglong) optp->max_value) &&
804
optp->max_value) /* if max value is not set -> no upper limit */
806
num= (ulonglong) optp->max_value;
810
switch ((optp->var_type & GET_TYPE_MASK)) {
812
if (num > (longlong) INT_MAX)
814
num= ((longlong) INT_MAX);
819
#if SIZEOF_LONG < SIZEOF_LONG_LONG
820
if (num > (longlong) LONG_MAX)
822
num= ((longlong) LONG_MAX);
828
DBUG_ASSERT((optp->var_type & GET_TYPE_MASK) == GET_LL);
832
num= ((num - optp->sub_size) / block_size);
833
num= (longlong) (num * block_size);
835
if (num < optp->min_value)
837
num= optp->min_value;
844
my_getopt_error_reporter(WARNING_LEVEL,
845
"option '%s': signed value %s adjusted to %s",
846
optp->name, llstr(old, buf1), llstr(num, buf2));
853
This is the same as getopt_ll, but is meant for unsigned long long
857
static ulonglong getopt_ull(char *arg, const struct my_option *optp, int *err)
859
ulonglong num= eval_num_suffix(arg, err, (char*) optp->name);
860
return getopt_ull_limit_value(num, optp, NULL);
864
ulonglong getopt_ull_limit_value(ulonglong num, const struct my_option *optp,
867
my_bool adjusted= FALSE;
869
char buf1[255], buf2[255];
871
if ((ulonglong) num > (ulonglong) optp->max_value &&
872
optp->max_value) /* if max value is not set -> no upper limit */
874
num= (ulonglong) optp->max_value;
878
switch ((optp->var_type & GET_TYPE_MASK)) {
880
if (num > (ulonglong) UINT_MAX)
882
num= ((ulonglong) UINT_MAX);
887
#if SIZEOF_LONG < SIZEOF_LONG_LONG
888
if (num > (ulonglong) ULONG_MAX)
890
num= ((ulonglong) ULONG_MAX);
896
DBUG_ASSERT((optp->var_type & GET_TYPE_MASK) == GET_ULL);
900
if (optp->block_size > 1)
902
num/= (ulonglong) optp->block_size;
903
num*= (ulonglong) optp->block_size;
906
if (num < (ulonglong) optp->min_value)
908
num= (ulonglong) optp->min_value;
915
my_getopt_error_reporter(WARNING_LEVEL,
916
"option '%s': unsigned value %s adjusted to %s",
917
optp->name, ullstr(old, buf1), ullstr(num, buf2));
924
Get double value withing ranges
926
Evaluates and returns the value that user gave as an argument to a variable.
931
In case of an error, prints an error message and sets *err to
932
EXIT_ARGUMENT_INVALID. Otherwise err is not touched
935
static double getopt_double(char *arg, const struct my_option *optp, int *err)
939
char *end= arg + 1000; /* Big enough as *arg is \0 terminated */
940
num= my_strtod(arg, &end, &error);
941
if (end[0] != 0 || error)
944
"%s: ERROR: Invalid decimal value for option '%s'\n",
945
my_progname, optp->name);
946
*err= EXIT_ARGUMENT_INVALID;
949
if (optp->max_value && num > (double) optp->max_value)
950
num= (double) optp->max_value;
951
return max(num, (double) optp->min_value);
955
Init one value to it's default values
959
option Option to initialize
960
value Pointer to variable
963
static void init_one_value(const struct my_option *option, uchar* *variable,
966
DBUG_ENTER("init_one_value");
967
switch ((option->var_type & GET_TYPE_MASK)) {
969
*((my_bool*) variable)= (my_bool) value;
972
*((int*) variable)= (int) value;
976
*((uint*) variable)= (uint) value;
979
*((long*) variable)= (long) value;
982
*((ulong*) variable)= (ulong) value;
985
*((longlong*) variable)= (longlong) value;
989
*((ulonglong*) variable)= (ulonglong) value;
992
*((double*) variable)= (double) value;
996
Do not clear variable value if it has no default value.
997
The default value may already be set.
998
NOTE: To avoid compiler warnings, we first cast longlong to intptr,
999
so that the value has the same size as a pointer.
1001
if ((char*) (intptr) value)
1002
*((char**) variable)= (char*) (intptr) value;
1006
Do not clear variable value if it has no default value.
1007
The default value may already be set.
1008
NOTE: To avoid compiler warnings, we first cast longlong to intptr,
1009
so that the value has the same size as a pointer.
1011
if ((char*) (intptr) value)
1013
my_free((*(char**) variable), MYF(MY_ALLOW_ZERO_PTR));
1014
*((char**) variable)= my_strdup((char*) (intptr) value, MYF(MY_WME));
1017
default: /* dummy default to avoid compiler warnings */
1025
Init one value to it's default values
1029
option Option to initialize
1030
value Pointer to variable
1033
static void fini_one_value(const struct my_option *option, uchar* *variable,
1034
longlong value __attribute__ ((unused)))
1036
DBUG_ENTER("fini_one_value");
1037
switch ((option->var_type & GET_TYPE_MASK)) {
1039
my_free((*(char**) variable), MYF(MY_ALLOW_ZERO_PTR));
1040
*((char**) variable)= NULL;
1042
default: /* dummy default to avoid compiler warnings */
1049
void my_cleanup_options(const struct my_option *options)
1051
init_variables(options, fini_one_value);
1056
initialize all variables to their default values
1060
options Array of options
1063
We will initialize the value that is pointed to by options->value.
1064
If the value is of type GET_ASK_ADDR, we will also ask for the address
1065
for a value and initialize.
1068
static void init_variables(const struct my_option *options,
1069
init_func_p init_one_value)
1071
DBUG_ENTER("init_variables");
1072
for (; options->name; options++)
1075
DBUG_PRINT("options", ("name: '%s'", options->name));
1077
We must set u_max_value first as for some variables
1078
options->u_max_value == options->value and in this case we want to
1079
set the value to default value.
1081
if (options->u_max_value)
1082
init_one_value(options, options->u_max_value, options->max_value);
1084
init_one_value(options, options->value, options->def_value);
1085
if (options->var_type & GET_ASK_ADDR &&
1086
(variable= (*getopt_get_addr)("", 0, options)))
1087
init_one_value(options, variable, options->def_value);
1094
function: my_print_options
1096
Print help for all options and variables.
1099
#include <help_start.h>
1101
void my_print_help(const struct my_option *options)
1103
uint col, name_space= 22, comment_space= 57;
1104
const char *line_end;
1105
const struct my_option *optp;
1107
for (optp= options; optp->id; optp++)
1111
printf(" -%c%s", optp->id, strlen(optp->name) ? ", " : " ");
1119
if (strlen(optp->name))
1121
printf("--%s", optp->name);
1122
col+= 2 + (uint) strlen(optp->name);
1123
if ((optp->var_type & GET_TYPE_MASK) == GET_STR ||
1124
(optp->var_type & GET_TYPE_MASK) == GET_STR_ALLOC)
1126
printf("%s=name%s ", optp->arg_type == OPT_ARG ? "[" : "",
1127
optp->arg_type == OPT_ARG ? "]" : "");
1128
col+= (optp->arg_type == OPT_ARG) ? 8 : 6;
1130
else if ((optp->var_type & GET_TYPE_MASK) == GET_NO_ARG ||
1131
(optp->var_type & GET_TYPE_MASK) == GET_BOOL)
1138
printf("%s=#%s ", optp->arg_type == OPT_ARG ? "[" : "",
1139
optp->arg_type == OPT_ARG ? "]" : "");
1140
col+= (optp->arg_type == OPT_ARG) ? 5 : 3;
1142
if (col > name_space && optp->comment && *optp->comment)
1148
for (; col < name_space; col++)
1150
if (optp->comment && *optp->comment)
1152
const char *comment= optp->comment, *end= strend(comment);
1154
while ((uint) (end - comment) > comment_space)
1156
for (line_end= comment + comment_space; *line_end != ' '; line_end--);
1157
for (; comment != line_end; comment++)
1159
comment++; /* skip the space, as a newline will take it's place now */
1161
for (col= 0; col < name_space; col++)
1164
printf("%s", comment);
1167
if ((optp->var_type & GET_TYPE_MASK) == GET_NO_ARG ||
1168
(optp->var_type & GET_TYPE_MASK) == GET_BOOL)
1170
if (optp->def_value != 0)
1172
printf("%*s(Defaults to on; use --skip-%s to disable.)\n", name_space, "", optp->name);
1180
function: my_print_options
1185
void my_print_variables(const struct my_option *options)
1187
uint name_space= 34, length, nr;
1188
ulonglong bit, llvalue;
1190
const struct my_option *optp;
1192
printf("\nVariables (--variable-name=value)\n");
1193
printf("and boolean options {FALSE|TRUE} Value (after reading options)\n");
1194
printf("--------------------------------- -----------------------------\n");
1195
for (optp= options; optp->id; optp++)
1197
uchar* *value= (optp->var_type & GET_ASK_ADDR ?
1198
(*getopt_get_addr)("", 0, optp) : optp->value);
1201
printf("%s ", optp->name);
1202
length= (uint) strlen(optp->name)+1;
1203
for (; length < name_space; length++)
1205
switch ((optp->var_type & GET_TYPE_MASK)) {
1207
if (!(llvalue= *(ulonglong*) value))
1208
printf("%s\n", "(No default value)");
1210
for (nr= 0, bit= 1; llvalue && nr < optp->typelib->count; nr++, bit<<=1)
1212
if (!(bit & llvalue))
1215
printf( llvalue ? "%s," : "%s\n", get_type(optp->typelib, nr));
1219
printf("%s\n", get_type(optp->typelib, *(uint*) value));
1222
case GET_STR_ALLOC: /* fall through */
1223
printf("%s\n", *((char**) value) ? *((char**) value) :
1224
"(No default value)");
1227
printf("%s\n", *((my_bool*) value) ? "TRUE" : "FALSE");
1230
printf("%d\n", *((int*) value));
1233
printf("%d\n", *((uint*) value));
1236
printf("%ld\n", *((long*) value));
1239
printf("%lu\n", *((ulong*) value));
1242
printf("%s\n", llstr(*((longlong*) value), buff));
1245
longlong2str(*((ulonglong*) value), buff, 10);
1246
printf("%s\n", buff);
1249
printf("%g\n", *(double*) value);
1252
printf("(Disabled)\n");
1259
#include <help_end.h>