summaryrefslogtreecommitdiff
path: root/mysys/my_getopt.c
diff options
context:
space:
mode:
authorunknown <jani@hynda.mysql.fi>2002-04-02 20:29:53 +0300
committerunknown <jani@hynda.mysql.fi>2002-04-02 20:29:53 +0300
commitd828523602e619cb66043fee579f09db392d87f7 (patch)
tree1646f32146d233c71f082b8d74b469e6a1c1e70f /mysys/my_getopt.c
parent08e155acab6f6ca7497f65553ae8b59def40dd87 (diff)
downloadmariadb-git-d828523602e619cb66043fee579f09db392d87f7.tar.gz
Changed mysql, mysqladmin, mysqlshow, mysqldump, mysqlimport,
mysqlcheck and myisamchk to use new my_getopt struct. client/client_priv.h: Changes for my_getopt client/mysql.cc: Uses new my_getopt client/mysqladmin.c: Uses new my_getopt client/mysqlcheck.c: Uses new my_getopt client/mysqldump.c: Uses new my_getopt client/mysqlimport.c: Uses new my_getopt client/mysqlshow.c: Uses new my_getopt include/my_getopt.h: Added GET_BOOL type include/sslopt-case.h: This is shouldn't be needed anymore. include/sslopt-longopts.h: Uses new my_getopt myisam/myisamchk.c: Uses new my_getopt mysys/my_getopt.c: Some bug fixes and small feature adds to my_getopt sql/mysqld.cc: mysqld.cc will be changed next. sslopt-* had to be removed temporarily.
Diffstat (limited to 'mysys/my_getopt.c')
-rw-r--r--mysys/my_getopt.c70
1 files changed, 51 insertions, 19 deletions
diff --git a/mysys/my_getopt.c b/mysys/my_getopt.c
index a28d3dd4e3c..6eccd73bce1 100644
--- a/mysys/my_getopt.c
+++ b/mysys/my_getopt.c
@@ -35,6 +35,7 @@ static void init_variables(const struct my_option *options);
static const char *special_opt_prefix[]=
{"skip", "disable", "enable", "maximum", 0};
+char *disabled_my_option= (char*) "0";
/* Return error values from handle_options */
@@ -66,7 +67,7 @@ int handle_options(int *argc, char ***argv,
char *))
{
uint opt_found, argvpos= 0, length, spec_len, i;
- int err;
+ int err= 0;
my_bool end_of_options= 0, must_be_var, set_maximum_value, special_used;
char *progname= *(*argv), **pos, *optend, *prev_found;
const struct my_option *optp;
@@ -177,7 +178,7 @@ int handle_options(int *argc, char ***argv,
We were called with a special prefix, we can reuse opt_found
*/
special_used= 1;
- cur_arg += (spec_len + 1);
+ cur_arg+= (spec_len + 1);
if ((opt_found= findopt(cur_arg, length - (spec_len + 1),
&optp, &prev_found)))
{
@@ -190,7 +191,7 @@ int handle_options(int *argc, char ***argv,
return ERR_AMBIGUOUS_OPTION;
}
if (i < DISABLE_OPTION_COUNT)
- optend= (char*) "0";
+ optend= disabled_my_option;
else if (!compare_strings(special_opt_prefix[i],"enable",6))
optend= (char*) "1";
else if (!compare_strings(special_opt_prefix[i],"maximum",7))
@@ -236,15 +237,44 @@ int handle_options(int *argc, char ***argv,
}
if (must_be_var && !optp->value)
{
- fprintf(stderr, "%s: the argument '%s' is not an variable\n",
+ fprintf(stderr, "%s: argument '%s' is not a variable\n",
progname, *pos);
return ERR_MUST_BE_VARIABLE;
}
- if (optp->arg_type == NO_ARG && optend && !special_used)
+ if (optp->arg_type == NO_ARG)
{
- fprintf(stderr, "%s: option '--%s' cannot take an argument\n",
- progname, optp->name);
- return ERR_NO_ARGUMENT_ALLOWED;
+ if (optend && !special_used)
+ {
+ fprintf(stderr, "%s: option '--%s' cannot take an argument\n",
+ progname, optp->name);
+ return ERR_NO_ARGUMENT_ALLOWED;
+ }
+ if (optp->var_type == GET_BOOL)
+ {
+ /*
+ Set bool to 1 if no argument or if the user has used
+ --enable-'option-name'.
+ *optend was set to '0' if one used --disable-option
+ */
+ *((my_bool*) optp->value)= (my_bool) (!optend || *optend == '1');
+ (*argc)--;
+ continue;
+ }
+ argument= optend;
+ }
+ else if (optp->arg_type == OPT_ARG && optp->var_type == GET_BOOL)
+ {
+ if (optend == disabled_my_option)
+ *((my_bool*) optp->value)= (my_bool) 0;
+ else
+ {
+ if (!optend) /* No argument -> enable option */
+ *((my_bool*) optp->value)= (my_bool) 1;
+ else /* If argument differs from 0, enable option, else disable */
+ *((my_bool*) optp->value)= (my_bool) atoi(optend) != 0;
+ }
+ (*argc)--;
+ continue;
}
else if (optp->arg_type == REQUIRED_ARG && !optend)
{
@@ -312,6 +342,7 @@ int handle_options(int *argc, char ***argv,
{
gptr *result_pos= (set_maximum_value) ?
optp->u_max_value : optp->value;
+
if (!result_pos)
{
fprintf(stderr,
@@ -321,14 +352,13 @@ int handle_options(int *argc, char ***argv,
if (optp->var_type == GET_LONG)
*((long*) result_pos)= (long) getopt_ll(argument, optp, &err);
else if (optp->var_type == GET_LL)
- *((longlong*) result_pos)= getopt_ll(argument, optp, &err);
+ *((longlong*) result_pos)= getopt_ll(argument, optp, &err);
else if (optp->var_type == GET_STR)
*((char**) result_pos)= argument;
if (err)
return ERR_UNKNOWN_SUFFIX;
}
- else
- get_one_option(optp->id, optp, argument);
+ get_one_option(optp->id, optp, argument);
(*argc)--; /* option handled (short or long), decrease argument count */
}
@@ -489,17 +519,19 @@ void my_print_help(const struct my_option *options)
col+= 2 + strlen(optp->name);
if (optp->var_type == GET_STR)
{
- printf("=name ");
+ printf("%s=name%s ", optp->arg_type == OPT_ARG ? "[" : "",
+ optp->arg_type == OPT_ARG ? "]" : "");
col+= 6;
}
- else if (optp->var_type == GET_NO_ARG)
+ else if (optp->var_type == GET_NO_ARG || optp->var_type == GET_BOOL)
{
putchar(' ');
col++;
}
else
{
- printf("=# ");
+ printf("%s=#%s ", optp->arg_type == OPT_ARG ? "[" : "",
+ optp->arg_type == OPT_ARG ? "]" : "");
col+= 3;
}
if (col > name_space)
@@ -546,7 +578,7 @@ void my_print_variables(const struct my_option *options)
printf("--------------------------------- -------------\n");
for (optp= options; optp->id; optp++)
{
- if (optp->value)
+ if (optp->value && optp->var_type != GET_BOOL)
{
printf("%s", optp->name);
length= strlen(optp->name);
@@ -554,10 +586,10 @@ void my_print_variables(const struct my_option *options)
putchar(' ');
if (optp->var_type == GET_STR)
{
- if (!optp->def_value && !*((char**) optp->value))
- printf("(No default value)\n");
- else
+ if (*((char**) optp->value))
printf("%s\n", *((char**) optp->value));
+ else
+ printf("(No default value)\n");
}
else if (optp->var_type == GET_LONG)
{
@@ -566,7 +598,7 @@ void my_print_variables(const struct my_option *options)
else
printf("%lu\n", *((long*) optp->value));
}
- else
+ else
{
if (!optp->def_value && !*((longlong*) optp->value))
printf("(No default value)\n");