2002-01-25 22:34:37 +01:00
|
|
|
|
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
|
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
|
|
|
|
|
|
|
|
#include <my_global.h>
|
|
|
|
|
#include <m_string.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <my_getopt.h>
|
|
|
|
|
#include <assert.h>
|
2002-05-14 20:41:55 +02:00
|
|
|
|
#include <my_sys.h>
|
2002-05-29 14:07:30 +02:00
|
|
|
|
#include <mysys_err.h>
|
2002-01-25 22:34:37 +01:00
|
|
|
|
|
2002-06-11 10:20:31 +02:00
|
|
|
|
static int findopt(char *optpat, uint length,
|
|
|
|
|
const struct my_option **opt_res,
|
|
|
|
|
char **ffname);
|
2002-07-23 17:31:22 +02:00
|
|
|
|
my_bool getopt_compare_strings(const char *s,
|
|
|
|
|
const char *t,
|
2002-06-11 10:20:31 +02:00
|
|
|
|
uint length);
|
|
|
|
|
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);
|
2002-01-30 04:08:17 +01:00
|
|
|
|
static void init_variables(const struct my_option *options);
|
2004-04-28 15:33:03 +02:00
|
|
|
|
static int setval(const struct my_option *opts,char *argument,
|
2002-06-11 10:20:31 +02:00
|
|
|
|
my_bool set_maximum_value);
|
2002-02-06 16:22:43 +01:00
|
|
|
|
|
2002-06-09 22:27:07 +02:00
|
|
|
|
/*
|
|
|
|
|
The following three variables belong to same group and the number and
|
|
|
|
|
order of their arguments must correspond to each other.
|
|
|
|
|
*/
|
2002-02-06 16:22:43 +01:00
|
|
|
|
static const char *special_opt_prefix[]=
|
2002-05-11 13:36:34 +02:00
|
|
|
|
{"skip", "disable", "enable", "maximum", "loose", 0};
|
2002-06-09 22:27:07 +02:00
|
|
|
|
static const uint special_opt_prefix_lengths[]=
|
|
|
|
|
{ 4, 7, 6, 7, 5, 0};
|
2002-06-11 10:20:31 +02:00
|
|
|
|
enum enum_special_opt
|
|
|
|
|
{ OPT_SKIP, OPT_DISABLE, OPT_ENABLE, OPT_MAXIMUM, OPT_LOOSE};
|
2002-02-06 16:22:43 +01:00
|
|
|
|
|
2002-04-02 19:29:53 +02:00
|
|
|
|
char *disabled_my_option= (char*) "0";
|
2002-02-06 16:22:43 +01:00
|
|
|
|
|
2002-06-09 22:27:07 +02:00
|
|
|
|
/*
|
|
|
|
|
This is a flag that can be set in client programs. 0 means that
|
2002-05-29 14:07:30 +02:00
|
|
|
|
my_getopt will not print error messages, but the client should do
|
2002-06-09 22:27:07 +02:00
|
|
|
|
it by itself
|
|
|
|
|
*/
|
2002-02-06 16:22:43 +01:00
|
|
|
|
|
2002-05-29 14:07:30 +02:00
|
|
|
|
my_bool my_getopt_print_errors= 1;
|
2002-01-25 22:34:37 +01:00
|
|
|
|
|
2004-08-19 17:56:32 +02:00
|
|
|
|
void default_reporter( enum loglevel level, const char *format, ... )
|
mysql_priv.h:
Added declarations for print_msg_to_log and vprint_msg_to_log. sql_print_error are simple functions that wrap calls to print_msg_to_log. Define the different error types with MY_ERROR_TYPE, MY_WARNING_TYPE, and MY_INFORMATION_TYPE
gen_lex_hash.cc:
Added NULL error reporting parameter to handle_options
log.cc:
Add print_msg_to_log, print_buffer_to_log, and vprint_msg_to_log. Print_msg_to_log will write the message to the windows event log if on NT. We now have error, warning, and information versions of sql_print_xxxx. T his is a variation of a similar changeset WAX did.
mysqld.cc:
Added option_error_reporter callback function and pass that into handle_options
mysql.cc:
Added NULL as error reporter arg to the end of handle_options
Many files:
Added NULL error reporter parameter as the last paramter to handle_options
my_getopt.c:
Added second function pointer to server as an error reporting callback. Added local function report_option_error that will either write the error to stderr or to the error reporting callback. changed all calls in handle_options from fprintf(stderr, ... ) to report_option_error
my_getopt.h:
Changed declaration of handle_options to use typedefs for the two function pointers. added second function pointer to server as an error reporting callback
mysqld.dsp:
Added custom build step for compiling message file and added message resource file (output of mc)
VC++Files/sql/mysqld.dsp:
Added custom build step for compiling message file and added message resource file (output of mc)
client/mysqladmin.c:
Added NULL error reporter parameter as the last paramter to handle_options
client/mysqlcheck.c:
Added NULL error reporter parameter as the last paramter to handle_options
client/mysqldump.c:
Added NULL error reporter parameter as the last paramter to handle_options
client/mysqlimport.c:
Added NULL error reporter parameter as the last paramter to handle_options
client/mysqlmanager-pwgen.c:
Added NULL error reporter parameter as the last paramter to handle_options
client/mysqlmanagerc.c:
Added NULL error reporter parameter as the last paramter to handle_options
client/mysqlbinlog.cc:
Added NULL error reporter parameter as the last paramter to handle_options
client/mysqlshow.c:
Added NULL error reporter parameter as the last paramter to handle_options
client/mysqltest.c:
Added NULL error reporter parameter as the last paramter to handle_options
extra/my_print_defaults.c:
Added NULL error reporter parameter as the last paramter to handle_options
extra/mysql_install.c:
Added NULL error reporter parameter as the last paramter to handle_options
extra/mysql_waitpid.c:
Added NULL error reporter parameter as the last paramter to handle_options
extra/perror.c:
Added NULL error reporter parameter as the last paramter to handle_options
extra/resolve_stack_dump.c:
Added NULL error reporter parameter as the last paramter to handle_options
extra/resolveip.c:
Added NULL error reporter parameter as the last paramter to handle_options
isam/isamchk.c:
Added NULL error reporter parameter as the last paramter to handle_options
isam/pack_isam.c:
Added NULL error reporter parameter as the last paramter to handle_options
myisam/mi_test1.c:
Added NULL error reporter parameter as the last paramter to handle_options
myisam/myisam_ftdump.c:
Added NULL error reporter parameter as the last paramter to handle_options
myisam/myisamchk.c:
Added NULL error reporter parameter as the last paramter to handle_options
myisam/myisampack.c:
Added NULL error reporter parameter as the last paramter to handle_options
include/my_getopt.h:
Changed declaration of handle_options to use typedefs for the two function pointers. added second function pointer to server as an error reporting callback
mysys/my_getopt.c:
Added second function pointer to server as an error reporting callback. Added local function report_option_error that will either write the error to stderr or to the error reporting callback. changed all calls in handle_options from fprintf(stderr, ... ) to report_option_error
tools/mysqlmanager.c:
Added NULL error reporter parameter as the last paramter to handle_options
client/mysql.cc:
Added NULL as error reporter arg to the end of handle_options
sql/mysqld.cc:
Added option_error_reporter callback function and pass that into handle_options
sql/log.cc:
Add print_msg_to_log, print_buffer_to_log, and vprint_msg_to_log. Print_msg_to_log will write the message to the windows event log if on NT. We now have error, warning, and information versions of sql_print_xxxx. T his is a variation of a similar changeset WAX did.
sql/gen_lex_hash.cc:
Added NULL error reporting parameter to handle_options
sql/mysql_priv.h:
Added declarations for print_msg_to_log and vprint_msg_to_log. sql_print_error are simple functions that wrap calls to print_msg_to_log. Define the different error types with MY_ERROR_TYPE, MY_WARNING_TYPE, and MY_INFORMATION_TYPE
BitKeeper/etc/logging_ok:
Logging to logging@openlogging.org accepted
2004-08-14 03:38:37 +02:00
|
|
|
|
{
|
|
|
|
|
va_list args;
|
|
|
|
|
va_start( args, format );
|
2004-08-18 22:31:01 +02:00
|
|
|
|
vfprintf( stderr, format, args );
|
mysql_priv.h:
Added declarations for print_msg_to_log and vprint_msg_to_log. sql_print_error are simple functions that wrap calls to print_msg_to_log. Define the different error types with MY_ERROR_TYPE, MY_WARNING_TYPE, and MY_INFORMATION_TYPE
gen_lex_hash.cc:
Added NULL error reporting parameter to handle_options
log.cc:
Add print_msg_to_log, print_buffer_to_log, and vprint_msg_to_log. Print_msg_to_log will write the message to the windows event log if on NT. We now have error, warning, and information versions of sql_print_xxxx. T his is a variation of a similar changeset WAX did.
mysqld.cc:
Added option_error_reporter callback function and pass that into handle_options
mysql.cc:
Added NULL as error reporter arg to the end of handle_options
Many files:
Added NULL error reporter parameter as the last paramter to handle_options
my_getopt.c:
Added second function pointer to server as an error reporting callback. Added local function report_option_error that will either write the error to stderr or to the error reporting callback. changed all calls in handle_options from fprintf(stderr, ... ) to report_option_error
my_getopt.h:
Changed declaration of handle_options to use typedefs for the two function pointers. added second function pointer to server as an error reporting callback
mysqld.dsp:
Added custom build step for compiling message file and added message resource file (output of mc)
VC++Files/sql/mysqld.dsp:
Added custom build step for compiling message file and added message resource file (output of mc)
client/mysqladmin.c:
Added NULL error reporter parameter as the last paramter to handle_options
client/mysqlcheck.c:
Added NULL error reporter parameter as the last paramter to handle_options
client/mysqldump.c:
Added NULL error reporter parameter as the last paramter to handle_options
client/mysqlimport.c:
Added NULL error reporter parameter as the last paramter to handle_options
client/mysqlmanager-pwgen.c:
Added NULL error reporter parameter as the last paramter to handle_options
client/mysqlmanagerc.c:
Added NULL error reporter parameter as the last paramter to handle_options
client/mysqlbinlog.cc:
Added NULL error reporter parameter as the last paramter to handle_options
client/mysqlshow.c:
Added NULL error reporter parameter as the last paramter to handle_options
client/mysqltest.c:
Added NULL error reporter parameter as the last paramter to handle_options
extra/my_print_defaults.c:
Added NULL error reporter parameter as the last paramter to handle_options
extra/mysql_install.c:
Added NULL error reporter parameter as the last paramter to handle_options
extra/mysql_waitpid.c:
Added NULL error reporter parameter as the last paramter to handle_options
extra/perror.c:
Added NULL error reporter parameter as the last paramter to handle_options
extra/resolve_stack_dump.c:
Added NULL error reporter parameter as the last paramter to handle_options
extra/resolveip.c:
Added NULL error reporter parameter as the last paramter to handle_options
isam/isamchk.c:
Added NULL error reporter parameter as the last paramter to handle_options
isam/pack_isam.c:
Added NULL error reporter parameter as the last paramter to handle_options
myisam/mi_test1.c:
Added NULL error reporter parameter as the last paramter to handle_options
myisam/myisam_ftdump.c:
Added NULL error reporter parameter as the last paramter to handle_options
myisam/myisamchk.c:
Added NULL error reporter parameter as the last paramter to handle_options
myisam/myisampack.c:
Added NULL error reporter parameter as the last paramter to handle_options
include/my_getopt.h:
Changed declaration of handle_options to use typedefs for the two function pointers. added second function pointer to server as an error reporting callback
mysys/my_getopt.c:
Added second function pointer to server as an error reporting callback. Added local function report_option_error that will either write the error to stderr or to the error reporting callback. changed all calls in handle_options from fprintf(stderr, ... ) to report_option_error
tools/mysqlmanager.c:
Added NULL error reporter parameter as the last paramter to handle_options
client/mysql.cc:
Added NULL as error reporter arg to the end of handle_options
sql/mysqld.cc:
Added option_error_reporter callback function and pass that into handle_options
sql/log.cc:
Add print_msg_to_log, print_buffer_to_log, and vprint_msg_to_log. Print_msg_to_log will write the message to the windows event log if on NT. We now have error, warning, and information versions of sql_print_xxxx. T his is a variation of a similar changeset WAX did.
sql/gen_lex_hash.cc:
Added NULL error reporting parameter to handle_options
sql/mysql_priv.h:
Added declarations for print_msg_to_log and vprint_msg_to_log. sql_print_error are simple functions that wrap calls to print_msg_to_log. Define the different error types with MY_ERROR_TYPE, MY_WARNING_TYPE, and MY_INFORMATION_TYPE
BitKeeper/etc/logging_ok:
Logging to logging@openlogging.org accepted
2004-08-14 03:38:37 +02:00
|
|
|
|
va_end( args );
|
|
|
|
|
}
|
2002-01-30 04:08:17 +01:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
function: handle_options
|
|
|
|
|
|
|
|
|
|
Sort options; put options first, until special end of options (--), or
|
|
|
|
|
until end of argv. Parse options; check that the given option matches with
|
|
|
|
|
one of the options in struct 'my_option', return error in case of ambiguous
|
|
|
|
|
or unknown option. Check that option was given an argument if it requires
|
|
|
|
|
one. Call function 'get_one_option()' once for each option.
|
2002-01-25 22:34:37 +01:00
|
|
|
|
*/
|
2002-01-31 03:36:58 +01:00
|
|
|
|
|
|
|
|
|
int handle_options(int *argc, char ***argv,
|
2004-08-18 22:31:01 +02:00
|
|
|
|
const struct my_option *longopts, my_get_one_option get_one_option,
|
|
|
|
|
my_error_reporter reporter )
|
2002-01-25 22:34:37 +01:00
|
|
|
|
{
|
2002-06-09 22:27:07 +02:00
|
|
|
|
uint opt_found, argvpos= 0, length, i;
|
2002-05-11 13:36:34 +02:00
|
|
|
|
my_bool end_of_options= 0, must_be_var, set_maximum_value, special_used,
|
2002-06-11 10:20:31 +02:00
|
|
|
|
option_is_loose;
|
2002-06-28 23:16:15 +02:00
|
|
|
|
char *progname= *(*argv), **pos, **pos_end, *optend, *prev_found;
|
2002-01-25 22:34:37 +01:00
|
|
|
|
const struct my_option *optp;
|
2002-05-07 19:35:06 +02:00
|
|
|
|
int error;
|
2002-01-25 22:34:37 +01:00
|
|
|
|
|
2002-02-06 16:22:43 +01:00
|
|
|
|
LINT_INIT(opt_found);
|
2002-01-30 04:08:17 +01:00
|
|
|
|
(*argc)--; /* Skip the program name */
|
|
|
|
|
(*argv)++; /* --- || ---- */
|
|
|
|
|
init_variables(longopts);
|
2002-05-07 19:35:06 +02:00
|
|
|
|
|
2004-08-18 22:31:01 +02:00
|
|
|
|
if (! reporter) reporter = &default_reporter;
|
|
|
|
|
|
2002-06-28 23:16:15 +02:00
|
|
|
|
for (pos= *argv, pos_end=pos+ *argc; pos != pos_end ; pos++)
|
2002-01-25 22:34:37 +01:00
|
|
|
|
{
|
|
|
|
|
char *cur_arg= *pos;
|
2002-02-06 16:22:43 +01:00
|
|
|
|
if (cur_arg[0] == '-' && cur_arg[1] && !end_of_options) /* must be opt */
|
2002-01-25 22:34:37 +01:00
|
|
|
|
{
|
2002-02-06 16:22:43 +01:00
|
|
|
|
char *argument= 0;
|
|
|
|
|
must_be_var= 0;
|
2002-01-30 04:08:17 +01:00
|
|
|
|
set_maximum_value= 0;
|
2002-02-06 16:22:43 +01:00
|
|
|
|
special_used= 0;
|
2002-05-11 13:36:34 +02:00
|
|
|
|
option_is_loose= 0;
|
2002-01-25 22:34:37 +01:00
|
|
|
|
|
2002-02-06 16:22:43 +01:00
|
|
|
|
cur_arg++; /* skip '-' */
|
2002-05-13 21:03:47 +02:00
|
|
|
|
if (*cur_arg == '-' || *cur_arg == 'O') /* check for long option, */
|
|
|
|
|
{ /* --set-variable, or -O */
|
|
|
|
|
if (*cur_arg == 'O')
|
2002-02-06 16:22:43 +01:00
|
|
|
|
{
|
2002-05-13 21:03:47 +02:00
|
|
|
|
must_be_var= 1;
|
|
|
|
|
|
|
|
|
|
if (!(*++cur_arg)) /* If not -Ovar=# */
|
2002-01-25 22:34:37 +01:00
|
|
|
|
{
|
2002-05-13 21:03:47 +02:00
|
|
|
|
/* the argument must be in next argv */
|
|
|
|
|
if (!*++pos)
|
|
|
|
|
{
|
2002-05-29 14:07:30 +02:00
|
|
|
|
if (my_getopt_print_errors)
|
2004-08-18 22:31:01 +02:00
|
|
|
|
reporter( ERROR_LEVEL, "%s: Option '-O' requires an argument\n", progname );
|
2002-05-29 14:07:30 +02:00
|
|
|
|
return EXIT_ARGUMENT_REQUIRED;
|
2002-05-13 21:03:47 +02:00
|
|
|
|
}
|
|
|
|
|
cur_arg= *pos;
|
|
|
|
|
(*argc)--;
|
2002-01-25 22:34:37 +01:00
|
|
|
|
}
|
2002-02-06 16:22:43 +01:00
|
|
|
|
}
|
2002-07-23 17:31:22 +02:00
|
|
|
|
else if (!getopt_compare_strings(cur_arg, "-set-variable", 13))
|
2002-02-06 16:22:43 +01:00
|
|
|
|
{
|
|
|
|
|
must_be_var= 1;
|
|
|
|
|
if (cur_arg[13] == '=')
|
2002-01-25 22:34:37 +01:00
|
|
|
|
{
|
|
|
|
|
cur_arg+= 14;
|
2002-02-06 16:22:43 +01:00
|
|
|
|
if (!*cur_arg)
|
2002-01-25 22:34:37 +01:00
|
|
|
|
{
|
2002-05-29 14:07:30 +02:00
|
|
|
|
if (my_getopt_print_errors)
|
2004-08-18 22:31:01 +02:00
|
|
|
|
reporter( ERROR_LEVEL, "%s: Option '--set-variable' requires an argument\n", progname );
|
2002-05-29 14:07:30 +02:00
|
|
|
|
return EXIT_ARGUMENT_REQUIRED;
|
2002-01-25 22:34:37 +01:00
|
|
|
|
}
|
2002-02-06 16:22:43 +01:00
|
|
|
|
}
|
|
|
|
|
else if (cur_arg[14]) /* garbage, or another option. break out */
|
|
|
|
|
must_be_var= 0;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* the argument must be in next argv */
|
|
|
|
|
if (!*++pos)
|
2002-01-25 22:34:37 +01:00
|
|
|
|
{
|
2002-05-29 14:07:30 +02:00
|
|
|
|
if (my_getopt_print_errors)
|
2004-08-18 22:31:01 +02:00
|
|
|
|
reporter( ERROR_LEVEL, "%s: Option '--set-variable' requires an argument\n", progname );
|
2002-05-29 14:07:30 +02:00
|
|
|
|
return EXIT_ARGUMENT_REQUIRED;
|
2002-01-25 22:34:37 +01:00
|
|
|
|
}
|
2002-02-06 16:22:43 +01:00
|
|
|
|
cur_arg= *pos;
|
|
|
|
|
(*argc)--;
|
2002-01-25 22:34:37 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (!must_be_var)
|
|
|
|
|
{
|
2002-02-06 16:22:43 +01:00
|
|
|
|
if (!*++cur_arg) /* skip the double dash */
|
2002-01-25 22:34:37 +01:00
|
|
|
|
{
|
2002-02-06 16:22:43 +01:00
|
|
|
|
/* '--' means end of options, look no further */
|
2002-01-30 04:08:17 +01:00
|
|
|
|
end_of_options= 1;
|
2002-01-25 22:34:37 +01:00
|
|
|
|
(*argc)--;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-02-06 16:22:43 +01:00
|
|
|
|
optend= strcend(cur_arg, '=');
|
2002-01-30 04:08:17 +01:00
|
|
|
|
length= optend - cur_arg;
|
2002-02-06 16:22:43 +01:00
|
|
|
|
if (*optend == '=')
|
|
|
|
|
optend++;
|
|
|
|
|
else
|
|
|
|
|
optend=0;
|
|
|
|
|
|
2002-01-25 22:34:37 +01:00
|
|
|
|
/*
|
|
|
|
|
Find first the right option. Return error in case of an ambiguous,
|
|
|
|
|
or unknown option
|
|
|
|
|
*/
|
2002-01-30 04:08:17 +01:00
|
|
|
|
optp= longopts;
|
|
|
|
|
if (!(opt_found= findopt(cur_arg, length, &optp, &prev_found)))
|
2002-01-25 22:34:37 +01:00
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
Didn't find any matching option. Let's see if someone called
|
|
|
|
|
option with a special option prefix
|
|
|
|
|
*/
|
2002-01-30 04:08:17 +01:00
|
|
|
|
if (!must_be_var)
|
2002-01-25 22:34:37 +01:00
|
|
|
|
{
|
2002-02-06 16:22:43 +01:00
|
|
|
|
if (optend)
|
2002-05-11 13:36:34 +02:00
|
|
|
|
must_be_var= 1; /* option is followed by an argument */
|
2002-01-30 04:08:17 +01:00
|
|
|
|
for (i= 0; special_opt_prefix[i]; i++)
|
2002-01-25 22:34:37 +01:00
|
|
|
|
{
|
2002-07-23 17:31:22 +02:00
|
|
|
|
if (!getopt_compare_strings(special_opt_prefix[i], cur_arg,
|
|
|
|
|
special_opt_prefix_lengths[i]) &&
|
2002-06-09 22:27:07 +02:00
|
|
|
|
cur_arg[special_opt_prefix_lengths[i]] == '-')
|
2002-01-25 22:34:37 +01:00
|
|
|
|
{
|
2002-01-31 03:36:58 +01:00
|
|
|
|
/*
|
|
|
|
|
We were called with a special prefix, we can reuse opt_found
|
|
|
|
|
*/
|
2002-02-06 16:22:43 +01:00
|
|
|
|
special_used= 1;
|
2002-06-09 22:27:07 +02:00
|
|
|
|
cur_arg+= (special_opt_prefix_lengths[i] + 1);
|
|
|
|
|
if (i == OPT_LOOSE)
|
2002-05-11 13:36:34 +02:00
|
|
|
|
option_is_loose= 1;
|
2002-06-09 22:27:07 +02:00
|
|
|
|
if ((opt_found= findopt(cur_arg, length -
|
|
|
|
|
(special_opt_prefix_lengths[i] + 1),
|
2002-01-30 04:08:17 +01:00
|
|
|
|
&optp, &prev_found)))
|
2002-01-25 22:34:37 +01:00
|
|
|
|
{
|
|
|
|
|
if (opt_found > 1)
|
|
|
|
|
{
|
2002-05-29 14:07:30 +02:00
|
|
|
|
if (my_getopt_print_errors)
|
2004-08-18 22:31:01 +02:00
|
|
|
|
reporter( ERROR_LEVEL,
|
2002-05-29 14:07:30 +02:00
|
|
|
|
"%s: ambiguous option '--%s-%s' (--%s-%s)\n",
|
|
|
|
|
progname, special_opt_prefix[i], cur_arg,
|
|
|
|
|
special_opt_prefix[i], prev_found);
|
|
|
|
|
return EXIT_AMBIGUOUS_OPTION;
|
2002-01-25 22:34:37 +01:00
|
|
|
|
}
|
2002-06-09 22:27:07 +02:00
|
|
|
|
switch (i) {
|
|
|
|
|
case OPT_SKIP:
|
|
|
|
|
case OPT_DISABLE: /* fall through */
|
2002-08-28 12:14:11 +02:00
|
|
|
|
/*
|
|
|
|
|
double negation is actually enable again,
|
|
|
|
|
for example: --skip-option=0 -> option = TRUE
|
|
|
|
|
*/
|
|
|
|
|
optend= (optend && *optend == '0' && !(*(optend + 1))) ?
|
|
|
|
|
(char*) "1" : disabled_my_option;
|
2002-06-09 22:27:07 +02:00
|
|
|
|
break;
|
|
|
|
|
case OPT_ENABLE:
|
2002-08-28 12:14:11 +02:00
|
|
|
|
optend= (optend && *optend == '0' && !(*(optend + 1))) ?
|
|
|
|
|
disabled_my_option : (char*) "1";
|
2002-06-09 22:27:07 +02:00
|
|
|
|
break;
|
|
|
|
|
case OPT_MAXIMUM:
|
2002-01-30 04:08:17 +01:00
|
|
|
|
set_maximum_value= 1;
|
|
|
|
|
must_be_var= 1;
|
2002-06-09 22:27:07 +02:00
|
|
|
|
break;
|
2002-01-30 04:08:17 +01:00
|
|
|
|
}
|
2002-01-31 03:36:58 +01:00
|
|
|
|
break; /* break from the inner loop, main loop continues */
|
2002-01-25 22:34:37 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!opt_found)
|
|
|
|
|
{
|
|
|
|
|
if (must_be_var)
|
|
|
|
|
{
|
2002-05-29 14:07:30 +02:00
|
|
|
|
if (my_getopt_print_errors)
|
2004-08-18 22:31:01 +02:00
|
|
|
|
reporter( option_is_loose ? WARNING_LEVEL : ERROR_LEVEL,
|
|
|
|
|
"%s: unknown variable '%s'\n", progname, cur_arg );
|
2002-05-11 13:36:34 +02:00
|
|
|
|
if (!option_is_loose)
|
2002-05-29 14:07:30 +02:00
|
|
|
|
return EXIT_UNKNOWN_VARIABLE;
|
2002-01-25 22:34:37 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2002-05-29 14:07:30 +02:00
|
|
|
|
if (my_getopt_print_errors)
|
2004-08-18 22:31:01 +02:00
|
|
|
|
reporter( option_is_loose ? WARNING_LEVEL : ERROR_LEVEL,
|
|
|
|
|
"%s: unknown option '--%s'\n", progname, cur_arg );
|
2002-05-11 13:36:34 +02:00
|
|
|
|
if (!option_is_loose)
|
2002-05-29 14:07:30 +02:00
|
|
|
|
return EXIT_UNKNOWN_OPTION;
|
2002-05-11 13:36:34 +02:00
|
|
|
|
}
|
|
|
|
|
if (option_is_loose)
|
|
|
|
|
{
|
|
|
|
|
(*argc)--;
|
|
|
|
|
continue;
|
2002-01-25 22:34:37 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (opt_found > 1)
|
|
|
|
|
{
|
|
|
|
|
if (must_be_var)
|
|
|
|
|
{
|
2002-05-29 14:07:30 +02:00
|
|
|
|
if (my_getopt_print_errors)
|
2004-08-18 22:31:01 +02:00
|
|
|
|
reporter( ERROR_LEVEL, "%s: variable prefix '%s' is not unique\n",
|
2002-05-29 14:07:30 +02:00
|
|
|
|
progname, cur_arg);
|
|
|
|
|
return EXIT_VAR_PREFIX_NOT_UNIQUE;
|
2002-01-25 22:34:37 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2002-05-29 14:07:30 +02:00
|
|
|
|
if (my_getopt_print_errors)
|
2004-08-18 22:31:01 +02:00
|
|
|
|
reporter( ERROR_LEVEL, "%s: ambiguous option '--%s' (%s, %s)\n",
|
2002-05-29 14:07:30 +02:00
|
|
|
|
progname, cur_arg, prev_found, optp->name);
|
|
|
|
|
return EXIT_AMBIGUOUS_OPTION;
|
2002-01-25 22:34:37 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2002-09-05 13:22:52 +02:00
|
|
|
|
if (must_be_var && optp->var_type == GET_NO_ARG)
|
2002-01-25 22:34:37 +01:00
|
|
|
|
{
|
2002-05-29 14:07:30 +02:00
|
|
|
|
if (my_getopt_print_errors)
|
2004-08-18 22:31:01 +02:00
|
|
|
|
reporter( ERROR_LEVEL, "%s: option '%s' cannot take an argument\n",
|
2002-05-29 14:07:30 +02:00
|
|
|
|
progname, optp->name);
|
|
|
|
|
return EXIT_NO_ARGUMENT_ALLOWED;
|
2002-01-25 22:34:37 +01:00
|
|
|
|
}
|
2002-04-02 19:29:53 +02:00
|
|
|
|
if (optp->arg_type == NO_ARG)
|
2002-01-25 22:34:37 +01:00
|
|
|
|
{
|
2002-08-28 12:14:11 +02:00
|
|
|
|
if (optend && optp->var_type != GET_BOOL)
|
2002-04-02 19:29:53 +02:00
|
|
|
|
{
|
2002-05-29 14:07:30 +02:00
|
|
|
|
if (my_getopt_print_errors)
|
2004-08-18 22:31:01 +02:00
|
|
|
|
reporter( ERROR_LEVEL, "%s: option '--%s' cannot take an argument\n",
|
2002-05-29 14:07:30 +02:00
|
|
|
|
progname, optp->name);
|
|
|
|
|
return EXIT_NO_ARGUMENT_ALLOWED;
|
2002-04-02 19:29:53 +02:00
|
|
|
|
}
|
|
|
|
|
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
|
2002-05-13 21:03:47 +02:00
|
|
|
|
*/
|
2004-02-03 19:10:45 +01:00
|
|
|
|
my_bool tmp= (my_bool) (!optend || *optend == '1');
|
|
|
|
|
*((my_bool*) optp->value)= tmp;
|
|
|
|
|
(*argc)--;
|
|
|
|
|
get_one_option(optp->id, optp,
|
|
|
|
|
tmp ? (char*) "1" : disabled_my_option);
|
2003-01-28 19:56:35 +01:00
|
|
|
|
continue;
|
2002-04-02 19:29:53 +02:00
|
|
|
|
}
|
|
|
|
|
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;
|
2004-04-28 15:33:03 +02:00
|
|
|
|
else
|
|
|
|
|
argument= optend;
|
2002-04-02 19:29:53 +02:00
|
|
|
|
}
|
2002-01-25 22:34:37 +01:00
|
|
|
|
}
|
2002-02-06 16:22:43 +01:00
|
|
|
|
else if (optp->arg_type == REQUIRED_ARG && !optend)
|
2002-01-25 22:34:37 +01:00
|
|
|
|
{
|
|
|
|
|
/* Check if there are more arguments after this one */
|
2002-02-06 16:22:43 +01:00
|
|
|
|
if (!*++pos)
|
2002-01-25 22:34:37 +01:00
|
|
|
|
{
|
2002-05-29 14:07:30 +02:00
|
|
|
|
if (my_getopt_print_errors)
|
2004-08-18 22:31:01 +02:00
|
|
|
|
reporter( ERROR_LEVEL, "%s: option '--%s' requires an argument\n",
|
2002-05-29 14:07:30 +02:00
|
|
|
|
progname, optp->name);
|
|
|
|
|
return EXIT_ARGUMENT_REQUIRED;
|
2002-01-25 22:34:37 +01:00
|
|
|
|
}
|
2002-01-30 04:08:17 +01:00
|
|
|
|
argument= *pos;
|
2002-01-25 22:34:37 +01:00
|
|
|
|
(*argc)--;
|
|
|
|
|
}
|
2002-02-06 16:22:43 +01:00
|
|
|
|
else
|
|
|
|
|
argument= optend;
|
2002-01-25 22:34:37 +01:00
|
|
|
|
}
|
2002-01-31 03:36:58 +01:00
|
|
|
|
else /* must be short option */
|
2002-01-25 22:34:37 +01:00
|
|
|
|
{
|
2002-05-31 15:23:36 +02:00
|
|
|
|
for (optend= cur_arg; *optend; optend++)
|
2002-01-25 22:34:37 +01:00
|
|
|
|
{
|
2002-05-31 15:23:36 +02:00
|
|
|
|
opt_found= 0;
|
2002-02-06 16:22:43 +01:00
|
|
|
|
for (optp= longopts; optp->id; optp++)
|
2002-01-25 22:34:37 +01:00
|
|
|
|
{
|
|
|
|
|
if (optp->id == (int) (uchar) *optend)
|
|
|
|
|
{
|
|
|
|
|
/* Option recognized. Find next what to do with it */
|
2002-02-06 16:22:43 +01:00
|
|
|
|
opt_found= 1;
|
2002-04-03 14:44:20 +02:00
|
|
|
|
if (optp->var_type == GET_BOOL && optp->arg_type == NO_ARG)
|
|
|
|
|
{
|
|
|
|
|
*((my_bool*) optp->value)= (my_bool) 1;
|
2003-01-28 19:56:35 +01:00
|
|
|
|
get_one_option(optp->id, optp, argument);
|
|
|
|
|
continue;
|
2002-04-03 14:44:20 +02:00
|
|
|
|
}
|
|
|
|
|
else if (optp->arg_type == REQUIRED_ARG ||
|
|
|
|
|
optp->arg_type == OPT_ARG)
|
2002-01-25 22:34:37 +01:00
|
|
|
|
{
|
|
|
|
|
if (*(optend + 1))
|
|
|
|
|
{
|
2002-07-03 18:45:38 +02:00
|
|
|
|
/* The rest of the option is option argument */
|
2002-02-06 16:22:43 +01:00
|
|
|
|
argument= optend + 1;
|
2002-07-03 18:45:38 +02:00
|
|
|
|
/* This is in effect a jump out of the outer loop */
|
2002-02-06 16:22:43 +01:00
|
|
|
|
optend= (char*) " ";
|
2002-01-25 22:34:37 +01:00
|
|
|
|
}
|
2004-04-28 15:33:03 +02:00
|
|
|
|
else
|
2002-01-25 22:34:37 +01:00
|
|
|
|
{
|
2004-04-30 23:12:37 +02:00
|
|
|
|
if (optp->arg_type == OPT_ARG)
|
|
|
|
|
{
|
|
|
|
|
if (optp->var_type == GET_BOOL)
|
|
|
|
|
*((my_bool*) optp->value)= (my_bool) 1;
|
|
|
|
|
get_one_option(optp->id, optp, argument);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2002-01-25 22:34:37 +01:00
|
|
|
|
/* Check if there are more arguments after this one */
|
2004-04-28 15:33:03 +02:00
|
|
|
|
if (!pos[1])
|
2002-01-25 22:34:37 +01:00
|
|
|
|
{
|
2004-04-28 15:33:03 +02:00
|
|
|
|
if (my_getopt_print_errors)
|
2004-08-18 22:31:01 +02:00
|
|
|
|
reporter( ERROR_LEVEL,
|
2004-04-28 15:33:03 +02:00
|
|
|
|
"%s: option '-%c' requires an argument\n",
|
|
|
|
|
progname, optp->id);
|
|
|
|
|
return EXIT_ARGUMENT_REQUIRED;
|
2002-01-25 22:34:37 +01:00
|
|
|
|
}
|
2004-04-28 15:33:03 +02:00
|
|
|
|
argument= *++pos;
|
2002-01-25 22:34:37 +01:00
|
|
|
|
(*argc)--;
|
2002-02-06 16:22:43 +01:00
|
|
|
|
/* the other loop will break, because *optend + 1 == 0 */
|
2002-01-25 22:34:37 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2002-05-07 19:35:06 +02:00
|
|
|
|
if ((error= setval(optp, argument, set_maximum_value)))
|
|
|
|
|
{
|
2004-08-18 22:31:01 +02:00
|
|
|
|
reporter( ERROR_LEVEL,
|
2002-05-07 19:35:06 +02:00
|
|
|
|
"%s: Error while setting value '%s' to '%s'\n",
|
|
|
|
|
progname, argument, optp->name);
|
|
|
|
|
return error;
|
|
|
|
|
}
|
2002-02-06 16:22:43 +01:00
|
|
|
|
get_one_option(optp->id, optp, argument);
|
2002-01-25 22:34:37 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-02-06 16:22:43 +01:00
|
|
|
|
if (!opt_found)
|
|
|
|
|
{
|
2002-05-29 14:07:30 +02:00
|
|
|
|
if (my_getopt_print_errors)
|
2004-08-18 22:31:01 +02:00
|
|
|
|
reporter( ERROR_LEVEL,
|
2002-05-31 15:23:36 +02:00
|
|
|
|
"%s: unknown option '-%c'\n", progname, *optend);
|
2002-05-29 14:07:30 +02:00
|
|
|
|
return EXIT_UNKNOWN_OPTION;
|
2002-02-06 16:22:43 +01:00
|
|
|
|
}
|
2002-01-25 22:34:37 +01:00
|
|
|
|
}
|
2002-02-06 16:22:43 +01:00
|
|
|
|
(*argc)--; /* option handled (short), decrease argument count */
|
|
|
|
|
continue;
|
2002-01-25 22:34:37 +01:00
|
|
|
|
}
|
2002-05-07 19:35:06 +02:00
|
|
|
|
if ((error= setval(optp, argument, set_maximum_value)))
|
2002-01-30 04:08:17 +01:00
|
|
|
|
{
|
2004-08-18 22:31:01 +02:00
|
|
|
|
reporter( ERROR_LEVEL,
|
2002-05-07 19:35:06 +02:00
|
|
|
|
"%s: Error while setting value '%s' to '%s'\n",
|
|
|
|
|
progname, argument, optp->name);
|
|
|
|
|
return error;
|
2002-01-30 04:08:17 +01:00
|
|
|
|
}
|
2002-04-02 19:29:53 +02:00
|
|
|
|
get_one_option(optp->id, optp, argument);
|
2002-01-30 04:08:17 +01:00
|
|
|
|
|
2002-01-31 03:36:58 +01:00
|
|
|
|
(*argc)--; /* option handled (short<72>or<6F>long), decrease argument count */
|
2002-01-25 22:34:37 +01:00
|
|
|
|
}
|
2002-01-31 03:36:58 +01:00
|
|
|
|
else /* non-option found */
|
2002-01-30 04:08:17 +01:00
|
|
|
|
(*argv)[argvpos++]= cur_arg;
|
2002-01-25 22:34:37 +01:00
|
|
|
|
}
|
2002-06-11 10:20:31 +02:00
|
|
|
|
/*
|
|
|
|
|
Destroy the first, already handled option, so that programs that look
|
|
|
|
|
for arguments in 'argv', without checking 'argc', know when to stop.
|
|
|
|
|
Items in argv, before the destroyed one, are all non-option -arguments
|
|
|
|
|
to the program, yet to be (possibly) handled.
|
|
|
|
|
*/
|
|
|
|
|
(*argv)[argvpos]= 0;
|
2002-01-25 22:34:37 +01:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-07 19:35:06 +02:00
|
|
|
|
/*
|
|
|
|
|
function: setval
|
|
|
|
|
|
|
|
|
|
Arguments: opts, argument
|
|
|
|
|
Will set the option value to given value
|
|
|
|
|
*/
|
|
|
|
|
|
2003-03-10 12:54:20 +01:00
|
|
|
|
static int setval(const struct my_option *opts, char *argument,
|
|
|
|
|
my_bool set_maximum_value)
|
2002-05-07 19:35:06 +02:00
|
|
|
|
{
|
|
|
|
|
int err= 0;
|
|
|
|
|
|
|
|
|
|
if (opts->value && argument)
|
|
|
|
|
{
|
2002-06-11 10:20:31 +02:00
|
|
|
|
gptr *result_pos= ((set_maximum_value) ?
|
|
|
|
|
opts->u_max_value : opts->value);
|
2002-05-07 19:35:06 +02:00
|
|
|
|
|
|
|
|
|
if (!result_pos)
|
2002-05-29 14:07:30 +02:00
|
|
|
|
return EXIT_NO_PTR_TO_VARIABLE;
|
2002-05-07 19:35:06 +02:00
|
|
|
|
|
2002-05-22 19:45:19 +02:00
|
|
|
|
switch (opts->var_type) {
|
2004-04-28 15:33:03 +02:00
|
|
|
|
case GET_BOOL: /* If argument differs from 0, enable option, else disable */
|
|
|
|
|
*((my_bool*) result_pos)= (my_bool) atoi(argument) != 0;
|
|
|
|
|
break;
|
2002-05-22 19:45:19 +02:00
|
|
|
|
case GET_INT:
|
|
|
|
|
case GET_UINT: /* fall through */
|
2002-05-07 19:35:06 +02:00
|
|
|
|
*((int*) result_pos)= (int) getopt_ll(argument, opts, &err);
|
2002-05-22 19:45:19 +02:00
|
|
|
|
break;
|
|
|
|
|
case GET_LONG:
|
|
|
|
|
case GET_ULONG: /* fall through */
|
2002-05-07 19:35:06 +02:00
|
|
|
|
*((long*) result_pos)= (long) getopt_ll(argument, opts, &err);
|
2002-05-22 19:45:19 +02:00
|
|
|
|
break;
|
|
|
|
|
case GET_LL:
|
2002-05-07 19:35:06 +02:00
|
|
|
|
*((longlong*) result_pos)= getopt_ll(argument, opts, &err);
|
2002-05-22 19:45:19 +02:00
|
|
|
|
break;
|
|
|
|
|
case GET_ULL:
|
2002-05-07 19:35:06 +02:00
|
|
|
|
*((ulonglong*) result_pos)= getopt_ull(argument, opts, &err);
|
2002-05-22 19:45:19 +02:00
|
|
|
|
break;
|
|
|
|
|
case GET_STR:
|
2002-05-07 19:35:06 +02:00
|
|
|
|
*((char**) result_pos)= argument;
|
2002-05-22 19:45:19 +02:00
|
|
|
|
break;
|
|
|
|
|
case GET_STR_ALLOC:
|
2002-05-14 20:41:55 +02:00
|
|
|
|
if ((*((char**) result_pos)))
|
2003-08-27 01:51:39 +02:00
|
|
|
|
my_free((*(char**) result_pos), MYF(MY_WME | MY_FAE));
|
2002-05-15 19:24:00 +02:00
|
|
|
|
if (!(*((char**) result_pos)= my_strdup(argument, MYF(MY_WME))))
|
2002-05-29 14:07:30 +02:00
|
|
|
|
return EXIT_OUT_OF_MEMORY;
|
2002-05-22 19:45:19 +02:00
|
|
|
|
break;
|
|
|
|
|
default: /* dummy default to avoid compiler warnings */
|
|
|
|
|
break;
|
2002-05-14 20:41:55 +02:00
|
|
|
|
}
|
2002-05-07 19:35:06 +02:00
|
|
|
|
if (err)
|
2002-05-29 14:07:30 +02:00
|
|
|
|
return EXIT_UNKNOWN_SUFFIX;
|
2002-05-07 19:35:06 +02:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2002-01-25 22:34:37 +01:00
|
|
|
|
|
2002-01-30 04:08:17 +01:00
|
|
|
|
/*
|
|
|
|
|
function: findopt
|
|
|
|
|
|
|
|
|
|
Arguments: opt_pattern, length of opt_pattern, opt_struct, first found
|
|
|
|
|
name (ffname)
|
|
|
|
|
|
|
|
|
|
Go through all options in the my_option struct. Return number
|
|
|
|
|
of options found that match the pattern and in the argument
|
|
|
|
|
list the option found, if any. In case of ambiguous option, store
|
|
|
|
|
the name in ffname argument
|
2002-01-25 22:34:37 +01:00
|
|
|
|
*/
|
2002-02-06 16:22:43 +01:00
|
|
|
|
|
2002-07-23 17:31:22 +02:00
|
|
|
|
static int findopt(char *optpat, uint length,
|
|
|
|
|
const struct my_option **opt_res,
|
|
|
|
|
char **ffname)
|
2002-01-25 22:34:37 +01:00
|
|
|
|
{
|
|
|
|
|
int count;
|
|
|
|
|
struct my_option *opt= (struct my_option *) *opt_res;
|
|
|
|
|
|
2002-01-31 03:36:58 +01:00
|
|
|
|
for (count= 0; opt->name; opt++)
|
2002-01-25 22:34:37 +01:00
|
|
|
|
{
|
2002-07-23 17:31:22 +02:00
|
|
|
|
if (!getopt_compare_strings(opt->name, optpat, length)) /* match found */
|
2002-01-25 22:34:37 +01:00
|
|
|
|
{
|
2002-01-30 04:08:17 +01:00
|
|
|
|
(*opt_res)= opt;
|
2002-01-25 22:34:37 +01:00
|
|
|
|
if (!count)
|
2002-07-23 17:31:22 +02:00
|
|
|
|
*ffname= (char *) opt->name; /* We only need to know one prev */
|
|
|
|
|
if (!opt->name[length]) /* Exact match */
|
2002-01-25 22:34:37 +01:00
|
|
|
|
return 1;
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
2002-01-30 04:08:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
function: compare_strings
|
|
|
|
|
|
|
|
|
|
Works like strncmp, other than 1.) considers '-' and '_' the same.
|
|
|
|
|
2.) Returns -1 if strings differ, 0 if they are equal
|
|
|
|
|
*/
|
2002-02-06 16:22:43 +01:00
|
|
|
|
|
2002-07-23 17:31:22 +02:00
|
|
|
|
my_bool getopt_compare_strings(register const char *s, register const char *t,
|
2002-01-30 04:08:17 +01:00
|
|
|
|
uint length)
|
|
|
|
|
{
|
|
|
|
|
char const *end= s + length;
|
|
|
|
|
for (;s != end ; s++, t++)
|
|
|
|
|
{
|
|
|
|
|
if ((*s != '-' ? *s : '_') != (*t != '-' ? *t : '_'))
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-07 19:35:06 +02:00
|
|
|
|
/*
|
|
|
|
|
function: eval_num_suffix
|
2002-01-30 04:08:17 +01:00
|
|
|
|
|
2002-05-07 19:35:06 +02:00
|
|
|
|
Transforms a number with a suffix to real number. Suffix can
|
|
|
|
|
be k|K for kilo, m|M for mega or g|G for giga.
|
2002-01-30 04:08:17 +01:00
|
|
|
|
*/
|
2002-02-06 16:22:43 +01:00
|
|
|
|
|
2002-05-07 19:35:06 +02:00
|
|
|
|
static longlong eval_num_suffix (char *argument, int *error, char *option_name)
|
2002-01-30 04:08:17 +01:00
|
|
|
|
{
|
|
|
|
|
char *endchar;
|
|
|
|
|
longlong num;
|
|
|
|
|
|
2002-05-07 19:35:06 +02:00
|
|
|
|
*error= 0;
|
|
|
|
|
num= strtoll(argument, &endchar, 10);
|
2002-01-30 04:08:17 +01:00
|
|
|
|
if (*endchar == 'k' || *endchar == 'K')
|
|
|
|
|
num*= 1024L;
|
|
|
|
|
else if (*endchar == 'm' || *endchar == 'M')
|
|
|
|
|
num*= 1024L * 1024L;
|
|
|
|
|
else if (*endchar == 'g' || *endchar == 'G')
|
|
|
|
|
num*= 1024L * 1024L * 1024L;
|
|
|
|
|
else if (*endchar)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr,
|
|
|
|
|
"Unknown suffix '%c' used for variable '%s' (value '%s')\n",
|
2002-05-07 19:35:06 +02:00
|
|
|
|
*endchar, option_name, argument);
|
|
|
|
|
*error= 1;
|
|
|
|
|
return 0;
|
2002-01-30 04:08:17 +01:00
|
|
|
|
}
|
2002-05-07 19:35:06 +02:00
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
function: getopt_ll
|
|
|
|
|
|
|
|
|
|
Evaluates and returns the value that user gave as an argument
|
|
|
|
|
to a variable. Recognizes (case insensitive) K as KILO, M as MEGA
|
|
|
|
|
and G as GIGA bytes. Some values must be in certain blocks, as
|
|
|
|
|
defined in the given my_option struct, this function will check
|
|
|
|
|
that those values are honored.
|
|
|
|
|
In case of an error, set error value in *err.
|
|
|
|
|
*/
|
|
|
|
|
|
2003-01-05 19:18:49 +01:00
|
|
|
|
static longlong getopt_ll(char *arg, const struct my_option *optp, int *err)
|
2002-05-07 19:35:06 +02:00
|
|
|
|
{
|
|
|
|
|
longlong num;
|
2004-03-06 09:43:35 +01:00
|
|
|
|
ulonglong block_size= (optp->block_size ? (ulonglong) optp->block_size : 1L);
|
2002-05-07 19:35:06 +02:00
|
|
|
|
|
|
|
|
|
num= eval_num_suffix(arg, err, (char*) optp->name);
|
2004-03-06 09:43:35 +01:00
|
|
|
|
if (num > 0 && (ulonglong) num > (ulonglong) (ulong) optp->max_value &&
|
|
|
|
|
optp->max_value) /* if max value is not set -> no upper limit */
|
2002-01-30 04:08:17 +01:00
|
|
|
|
num= (longlong) (ulong) optp->max_value;
|
2004-03-06 09:43:35 +01:00
|
|
|
|
num= ((num - (longlong) optp->sub_size) / block_size);
|
|
|
|
|
num= (longlong) (num * block_size);
|
|
|
|
|
return max(num, optp->min_value);
|
2002-01-30 04:08:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2002-05-07 19:35:06 +02:00
|
|
|
|
/*
|
|
|
|
|
function: getopt_ull
|
|
|
|
|
|
|
|
|
|
This is the same as getopt_ll, but is meant for unsigned long long
|
|
|
|
|
values.
|
|
|
|
|
*/
|
|
|
|
|
|
2003-01-05 19:18:49 +01:00
|
|
|
|
static ulonglong getopt_ull(char *arg, const struct my_option *optp, int *err)
|
2002-05-07 19:35:06 +02:00
|
|
|
|
{
|
|
|
|
|
ulonglong num;
|
|
|
|
|
|
|
|
|
|
num= eval_num_suffix(arg, err, (char*) optp->name);
|
2002-07-23 17:31:22 +02:00
|
|
|
|
return getopt_ull_limit_value(num, optp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ulonglong getopt_ull_limit_value(ulonglong num, const struct my_option *optp)
|
|
|
|
|
{
|
2003-06-12 18:46:12 +02:00
|
|
|
|
if ((ulonglong) num > (ulonglong) optp->max_value &&
|
2002-07-23 17:31:22 +02:00
|
|
|
|
optp->max_value) /* if max value is not set -> no upper limit */
|
2003-06-12 18:46:12 +02:00
|
|
|
|
num= (ulonglong) optp->max_value;
|
2002-07-23 17:31:22 +02:00
|
|
|
|
if (optp->block_size > 1)
|
|
|
|
|
{
|
|
|
|
|
num/= (ulonglong) optp->block_size;
|
|
|
|
|
num*= (ulonglong) optp->block_size;
|
|
|
|
|
}
|
2002-05-07 19:35:06 +02:00
|
|
|
|
if (num < (ulonglong) optp->min_value)
|
|
|
|
|
num= (ulonglong) optp->min_value;
|
2002-07-23 17:31:22 +02:00
|
|
|
|
return num;
|
2002-05-07 19:35:06 +02:00
|
|
|
|
}
|
2002-01-30 04:08:17 +01:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
function: init_variables
|
|
|
|
|
|
|
|
|
|
initialize all variables to their default values
|
|
|
|
|
*/
|
2002-02-06 16:22:43 +01:00
|
|
|
|
|
2002-01-30 04:08:17 +01:00
|
|
|
|
static void init_variables(const struct my_option *options)
|
|
|
|
|
{
|
2002-02-06 16:22:43 +01:00
|
|
|
|
for (; options->name; options++)
|
2002-01-30 04:08:17 +01:00
|
|
|
|
{
|
2002-02-06 16:22:43 +01:00
|
|
|
|
if (options->value)
|
2002-01-30 04:08:17 +01:00
|
|
|
|
{
|
2002-05-22 19:45:19 +02:00
|
|
|
|
switch (options->var_type) {
|
|
|
|
|
case GET_BOOL:
|
2002-10-15 21:10:15 +02:00
|
|
|
|
if (options->u_max_value)
|
|
|
|
|
*((my_bool*) options->u_max_value)= (my_bool) options->max_value;
|
2002-06-28 18:30:09 +02:00
|
|
|
|
*((my_bool*) options->value)= (my_bool) options->def_value;
|
2002-05-22 19:45:19 +02:00
|
|
|
|
break;
|
|
|
|
|
case GET_INT:
|
2002-10-15 21:10:15 +02:00
|
|
|
|
if (options->u_max_value)
|
|
|
|
|
*((int*) options->u_max_value)= (int) options->max_value;
|
2002-06-28 18:30:09 +02:00
|
|
|
|
*((int*) options->value)= (int) options->def_value;
|
2002-05-22 19:45:19 +02:00
|
|
|
|
break;
|
|
|
|
|
case GET_UINT:
|
2002-10-15 21:10:15 +02:00
|
|
|
|
if (options->u_max_value)
|
|
|
|
|
*((uint*) options->u_max_value)= (uint) options->max_value;
|
2002-06-28 18:30:09 +02:00
|
|
|
|
*((uint*) options->value)= (uint) options->def_value;
|
2002-05-22 19:45:19 +02:00
|
|
|
|
break;
|
|
|
|
|
case GET_LONG:
|
2002-10-15 21:10:15 +02:00
|
|
|
|
if (options->u_max_value)
|
|
|
|
|
*((long*) options->u_max_value)= (long) options->max_value;
|
2002-06-28 18:30:09 +02:00
|
|
|
|
*((long*) options->value)= (long) options->def_value;
|
2002-05-22 19:45:19 +02:00
|
|
|
|
break;
|
|
|
|
|
case GET_ULONG:
|
2002-10-15 21:10:15 +02:00
|
|
|
|
if (options->u_max_value)
|
|
|
|
|
*((ulong*) options->u_max_value)= (ulong) options->max_value;
|
2002-06-28 18:30:09 +02:00
|
|
|
|
*((ulong*) options->value)= (ulong) options->def_value;
|
2002-05-22 19:45:19 +02:00
|
|
|
|
break;
|
|
|
|
|
case GET_LL:
|
2002-10-15 21:10:15 +02:00
|
|
|
|
if (options->u_max_value)
|
|
|
|
|
*((longlong*) options->u_max_value)= (longlong) options->max_value;
|
2002-06-28 18:30:09 +02:00
|
|
|
|
*((longlong*) options->value)= (longlong) options->def_value;
|
2002-05-22 19:45:19 +02:00
|
|
|
|
break;
|
|
|
|
|
case GET_ULL:
|
2002-10-15 21:10:15 +02:00
|
|
|
|
if (options->u_max_value)
|
|
|
|
|
*((ulonglong*) options->u_max_value)= (ulonglong) options->max_value;
|
2002-06-28 18:30:09 +02:00
|
|
|
|
*((ulonglong*) options->value)= (ulonglong) options->def_value;
|
2002-05-22 19:45:19 +02:00
|
|
|
|
break;
|
|
|
|
|
default: /* dummy default to avoid compiler warnings */
|
|
|
|
|
break;
|
|
|
|
|
}
|
2002-01-30 04:08:17 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-02-06 16:22:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
function: my_print_options
|
|
|
|
|
|
|
|
|
|
Print help for all options and variables.
|
|
|
|
|
*/
|
|
|
|
|
|
2004-05-25 21:00:14 +02:00
|
|
|
|
#include <help_start.h>
|
|
|
|
|
|
2002-02-06 16:22:43 +01:00
|
|
|
|
void my_print_help(const struct my_option *options)
|
|
|
|
|
{
|
|
|
|
|
uint col, name_space= 22, comment_space= 57;
|
|
|
|
|
const char *line_end;
|
|
|
|
|
const struct my_option *optp;
|
|
|
|
|
|
|
|
|
|
for (optp= options; optp->id; optp++)
|
|
|
|
|
{
|
|
|
|
|
if (optp->id < 256)
|
|
|
|
|
{
|
2002-05-24 13:06:58 +02:00
|
|
|
|
printf(" -%c%s", optp->id, strlen(optp->name) ? ", " : " ");
|
2002-02-06 16:22:43 +01:00
|
|
|
|
col= 6;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf(" ");
|
|
|
|
|
col= 2;
|
|
|
|
|
}
|
2002-05-24 13:06:58 +02:00
|
|
|
|
if (strlen(optp->name))
|
2002-02-06 16:22:43 +01:00
|
|
|
|
{
|
2002-05-24 13:06:58 +02:00
|
|
|
|
printf("--%s", optp->name);
|
|
|
|
|
col+= 2 + strlen(optp->name);
|
|
|
|
|
if (optp->var_type == GET_STR || optp->var_type == GET_STR_ALLOC)
|
|
|
|
|
{
|
|
|
|
|
printf("%s=name%s ", optp->arg_type == OPT_ARG ? "[" : "",
|
|
|
|
|
optp->arg_type == OPT_ARG ? "]" : "");
|
|
|
|
|
col+= (optp->arg_type == OPT_ARG) ? 8 : 6;
|
|
|
|
|
}
|
|
|
|
|
else if (optp->var_type == GET_NO_ARG || optp->var_type == GET_BOOL)
|
|
|
|
|
{
|
|
|
|
|
putchar(' ');
|
|
|
|
|
col++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf("%s=#%s ", optp->arg_type == OPT_ARG ? "[" : "",
|
|
|
|
|
optp->arg_type == OPT_ARG ? "]" : "");
|
|
|
|
|
col+= (optp->arg_type == OPT_ARG) ? 5 : 3;
|
|
|
|
|
}
|
|
|
|
|
if (col > name_space && optp->comment && *optp->comment)
|
|
|
|
|
{
|
|
|
|
|
putchar('\n');
|
|
|
|
|
col= 0;
|
|
|
|
|
}
|
2002-02-06 16:22:43 +01:00
|
|
|
|
}
|
|
|
|
|
for (; col < name_space; col++)
|
|
|
|
|
putchar(' ');
|
|
|
|
|
if (optp->comment && *optp->comment)
|
|
|
|
|
{
|
|
|
|
|
const char *comment= optp->comment, *end= strend(comment);
|
|
|
|
|
|
|
|
|
|
while ((uint) (end - comment) > comment_space)
|
|
|
|
|
{
|
|
|
|
|
for (line_end= comment + comment_space; *line_end != ' '; line_end--);
|
|
|
|
|
for (; comment != line_end; comment++)
|
|
|
|
|
putchar(*comment);
|
2002-07-03 18:45:38 +02:00
|
|
|
|
comment++; /* skip the space, as a newline will take it's place now */
|
2002-02-06 16:22:43 +01:00
|
|
|
|
putchar('\n');
|
|
|
|
|
for (col= 0; col < name_space; col++)
|
|
|
|
|
putchar(' ');
|
|
|
|
|
}
|
|
|
|
|
printf("%s", comment);
|
|
|
|
|
}
|
|
|
|
|
putchar('\n');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
function: my_print_options
|
|
|
|
|
|
|
|
|
|
Print variables.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
void my_print_variables(const struct my_option *options)
|
|
|
|
|
{
|
|
|
|
|
uint name_space= 34, length;
|
|
|
|
|
char buff[255];
|
|
|
|
|
const struct my_option *optp;
|
|
|
|
|
|
2002-05-22 19:45:19 +02:00
|
|
|
|
printf("\nVariables (--variable-name=value)\n");
|
|
|
|
|
printf("and boolean options {FALSE|TRUE} Value (after reading options)\n");
|
|
|
|
|
printf("--------------------------------- -----------------------------\n");
|
2002-02-06 16:22:43 +01:00
|
|
|
|
for (optp= options; optp->id; optp++)
|
|
|
|
|
{
|
2002-05-21 23:05:05 +02:00
|
|
|
|
if (optp->value)
|
2002-02-06 16:22:43 +01:00
|
|
|
|
{
|
|
|
|
|
printf("%s", optp->name);
|
|
|
|
|
length= strlen(optp->name);
|
|
|
|
|
for (; length < name_space; length++)
|
|
|
|
|
putchar(' ');
|
2002-05-22 19:45:19 +02:00
|
|
|
|
switch (optp->var_type) {
|
|
|
|
|
case GET_STR:
|
|
|
|
|
case GET_STR_ALLOC: /* fall through */
|
|
|
|
|
printf("%s\n", *((char**) optp->value) ? *((char**) optp->value) :
|
|
|
|
|
"(No default value)");
|
|
|
|
|
break;
|
|
|
|
|
case GET_BOOL:
|
|
|
|
|
printf("%s\n", *((my_bool*) optp->value) ? "TRUE" : "FALSE");
|
|
|
|
|
break;
|
|
|
|
|
case GET_INT:
|
|
|
|
|
printf("%d\n", *((int*) optp->value));
|
|
|
|
|
break;
|
|
|
|
|
case GET_UINT:
|
|
|
|
|
printf("%d\n", *((uint*) optp->value));
|
|
|
|
|
break;
|
|
|
|
|
case GET_LONG:
|
|
|
|
|
printf("%lu\n", *((long*) optp->value));
|
|
|
|
|
break;
|
|
|
|
|
case GET_ULONG:
|
|
|
|
|
printf("%lu\n", *((ulong*) optp->value));
|
|
|
|
|
break;
|
|
|
|
|
case GET_LL:
|
|
|
|
|
printf("%s\n", llstr(*((longlong*) optp->value), buff));
|
|
|
|
|
break;
|
|
|
|
|
case GET_ULL:
|
|
|
|
|
longlong2str(*((ulonglong*) optp->value), buff, 10);
|
|
|
|
|
printf("%s\n", buff);
|
|
|
|
|
break;
|
|
|
|
|
default: /* dummy default to avoid compiler warnings */
|
|
|
|
|
break;
|
2002-05-07 19:35:06 +02:00
|
|
|
|
}
|
2002-02-06 16:22:43 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-05-25 21:00:14 +02:00
|
|
|
|
|
|
|
|
|
#include <help_end.h>
|