MySQL 8.0.39
Source Code Documentation
my_getopt.cc File Reference
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <algorithm>
#include <array>
#include <bitset>
#include <type_traits>
#include "m_ctype.h"
#include "m_string.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_default.h"
#include "my_getopt.h"
#include "my_inttypes.h"
#include "my_io.h"
#include "my_loglevel.h"
#include "my_macros.h"
#include "mysql/service_mysql_alloc.h"
#include "mysql_version.h"
#include "mysys/mysys_priv.h"
#include "mysys_err.h"
#include "typelib.h"

Classes

union  ull_dbl
 

Typedefs

typedef void(* init_func_p) (const struct my_option *option, void *variable, longlong value)
 

Enumerations

enum  enum_special_opt {
  OPT_SKIP , OPT_DISABLE , OPT_ENABLE , OPT_MAXIMUM ,
  OPT_LOOSE
}
 

Functions

static bool getopt_compare_strings (const char *, const char *, uint)
 
static longlong getopt_ll (const char *, bool, const my_option *, int *)
 
static ulonglong getopt_ull (const char *, bool, const my_option *, int *)
 
static double getopt_double (const char *, bool, const my_option *, int *)
 
static void init_variables (const struct my_option *, init_func_p)
 
static void init_one_value (const struct my_option *, void *, longlong)
 
static void fini_one_value (const struct my_option *, void *, longlong)
 
static int setval (const struct my_option *, void *, const char *, bool, bool)
 
static void setval_source (const struct my_option *opts, void *value)
 Will set the source and file name from where this options is set in my_option struct. More...
 
static char * check_struct_option (char *cur_arg, char *key_name)
 Check for struct options. More...
 
static bool get_bool_int_argument (const char *argument, bool *error)
 Parse a boolean command line argument as the SQL interpreter does. More...
 
void my_getopt_register_get_addr (my_getopt_value func_addr)
 
bool is_key_cache_variable_suffix (std::string_view suffix)
 
int handle_options (int *argc, char ***argv, const struct my_option *longopts, my_get_one_option get_one_option)
 Wrapper around my_handle_options() for interface compatibility. More...
 
ulonglong getopt_double2ulonglong (double v)
 Returns an ulonglong value containing a raw representation of the given double value. More...
 
double getopt_ulonglong2double (ulonglong v)
 Returns the double value which corresponds to the given raw representation. More...
 
int my_handle_options2 (int *argc, char ***argv, const struct my_option *longopts, my_get_one_option get_one_option, const char **command_list, bool ignore_unknown_option, bool boolean_as_int)
 Handle command line options. More...
 
int my_handle_options (int *argc, char ***argv, const struct my_option *longopts, my_get_one_option get_one_option, const char **command_list, bool ignore_unknown_option)
 
void print_cmdline_password_warning ()
 This function should be called to print a warning message if password string is specified on the command line. More...
 
bool get_bool_argument (const char *argument, bool *error)
 Parse a boolean command line argument. More...
 
int findopt (const char *optpat, uint length, const struct my_option **opt_res)
 Find option. More...
 
template<typename LLorULL >
LLorULL eval_num_suffix (const char *argument, int *error, const char *option_name)
 
template longlong eval_num_suffix< longlong > (const char *, int *, const char *)
 
template ulonglong eval_num_suffix< ulonglong > (const char *, int *, const char *)
 
ulonglong max_of_int_range (int var_type)
 Maximum possible value for an integer GET_* variable type. More...
 
longlong getopt_ll_limit_value (longlong num, const struct my_option *optp, bool *fix)
 
static bool is_negative_num (const char *num)
 
ulonglong getopt_ull_limit_value (ulonglong num, const struct my_option *optp, bool *fix)
 
double getopt_double_limit_value (double num, const struct my_option *optp, bool *fix)
 
void my_cleanup_options (const struct my_option *options)
 
static uint print_name (const struct my_option *optp, FILE *file=stdout)
 Prints variable or option name, replacing _ with - to given file stream parameter (by default to stdout). More...
 
void my_print_help (const struct my_option *options)
 
void my_print_variables (const struct my_option *options)
 function: my_print_variables Print variables. More...
 
void my_print_variables_ex (const struct my_option *options, FILE *file)
 function: my_print_variables_ex Print variables to given file parameter stream (by default to stdout). More...
 

Variables

my_error_reporter my_getopt_error_reporter = &my_message_local
 
static const char * special_opt_prefix []
 
static const uint special_opt_prefix_lengths [] = {4, 7, 6, 7, 5, 0}
 
char * disabled_my_option = const_cast<char *>("0")
 
static char enabled_my_option [] = "1"
 
static char space_char [] = " "
 
bool my_getopt_print_errors = true
 
bool my_getopt_skip_unknown = false
 
static my_getopt_value getopt_get_addr
 

Typedef Documentation

◆ init_func_p

typedef void(* init_func_p) (const struct my_option *option, void *variable, longlong value)

Enumeration Type Documentation

◆ enum_special_opt

Enumerator
OPT_SKIP 
OPT_DISABLE 
OPT_ENABLE 
OPT_MAXIMUM 
OPT_LOOSE 

Function Documentation

◆ check_struct_option()

static char * check_struct_option ( char *  cur_arg,
char *  key_name 
)
static

Check for struct options.

Parameters
[in]cur_argCurrent argument under processing from argv
[in]key_namevariable where to store the possible key name

In case option is a struct option, returns a pointer to the current argument at the position where the struct option (key_name) ends, the next character after the dot. In case argument is not a struct option, returns a pointer to the argument. key_name will hold the name of the key, or 0 if not found.

Returns
char* If struct option Pointer to next character after dot. If no struct option Pointer to the argument

◆ eval_num_suffix()

template<typename LLorULL >
LLorULL eval_num_suffix ( const char *  argument,
int *  error,
const char *  option_name 
)

◆ eval_num_suffix< longlong >()

template longlong eval_num_suffix< longlong > ( const char *  ,
int *  ,
const char *   
)

◆ eval_num_suffix< ulonglong >()

template ulonglong eval_num_suffix< ulonglong > ( const char *  ,
int *  ,
const char *   
)

◆ findopt()

int findopt ( const char *  optpat,
uint  length,
const struct my_option **  opt_res 
)

Find option.

IMPLEMENTATION Go through all options in the my_option struct. Return true if an option is found. sets opt_res to the option found, if any.

Parameters
optpatname of option to find (with - or _)
lengthLength of optpat
[in,out]opt_resOptions
Return values
0No matching options
1Found an option

◆ fini_one_value()

static void fini_one_value ( const struct my_option option,
void *  variable,
longlong  value 
)
static

◆ get_bool_argument()

bool get_bool_argument ( const char *  argument,
bool *  error 
)

Parse a boolean command line argument.

"ON", "TRUE" and "1" will return true, other values will return false.

Parameters
argumentThe value argument
[out]errorError indicator
Returns
boolean value

◆ get_bool_int_argument()

static bool get_bool_int_argument ( const char *  argument,
bool *  error 
)
static

Parse a boolean command line argument as the SQL interpreter does.

"ON" and "TRUE" will return true, "OFF" and FALSE" will return false;

Non-zero numeric values will return true, zero will return false.

Parameters
argumentThe value argument
[out]errorError indicator
Returns
boolean value

◆ getopt_compare_strings()

bool getopt_compare_strings ( const char *  s,
const char *  t,
uint  length 
)
static

◆ getopt_double()

static double getopt_double ( const char *  arg,
bool  set_maximum_value,
const my_option optp,
int *  err 
)
static

◆ getopt_double2ulonglong()

ulonglong getopt_double2ulonglong ( double  v)

Returns an ulonglong value containing a raw representation of the given double value.

◆ getopt_double_limit_value()

double getopt_double_limit_value ( double  num,
const struct my_option optp,
bool *  fix 
)

◆ getopt_ll()

static longlong getopt_ll ( const char *  arg,
bool  set_maximum_value,
const my_option optp,
int *  err 
)
static

◆ getopt_ll_limit_value()

longlong getopt_ll_limit_value ( longlong  num,
const struct my_option optp,
bool *  fix 
)

◆ getopt_ull()

static ulonglong getopt_ull ( const char *  arg,
bool  set_maximum_value,
const my_option optp,
int *  err 
)
static

◆ getopt_ull_limit_value()

ulonglong getopt_ull_limit_value ( ulonglong  num,
const struct my_option optp,
bool *  fix 
)

◆ getopt_ulonglong2double()

double getopt_ulonglong2double ( ulonglong  v)

Returns the double value which corresponds to the given raw representation.

◆ handle_options()

int handle_options ( int *  argc,
char ***  argv,
const struct my_option longopts,
my_get_one_option  get_one_option 
)

Wrapper around my_handle_options() for interface compatibility.

Parameters
[in,out]argcCommand line options (count)
[in,out]argvCommand line options (values)
[in]longoptsDescriptor of all valid options
[in]get_one_optionOptional callback function to process each option, can be NULL.
Returns
Error in case of ambiguous or unknown options, 0 on success.

◆ init_one_value()

static void init_one_value ( const struct my_option option,
void *  variable,
longlong  value 
)
static

◆ init_variables()

static void init_variables ( const struct my_option options,
init_func_p  init_one_value 
)
static

◆ is_key_cache_variable_suffix()

bool is_key_cache_variable_suffix ( std::string_view  suffix)

◆ is_negative_num()

static bool is_negative_num ( const char *  num)
inlinestatic

◆ max_of_int_range()

ulonglong max_of_int_range ( int  var_type)

Maximum possible value for an integer GET_* variable type.

Parameters
var_typetype of integer variable (GET_*)
Returns
maximum possible value for this type

◆ my_cleanup_options()

void my_cleanup_options ( const struct my_option options)

◆ my_getopt_register_get_addr()

void my_getopt_register_get_addr ( my_getopt_value  func_addr)

◆ my_handle_options()

int my_handle_options ( int *  argc,
char ***  argv,
const struct my_option longopts,
my_get_one_option  get_one_option,
const char **  command_list,
bool  ignore_unknown_option 
)

◆ my_handle_options2()

int my_handle_options2 ( int *  argc,
char ***  argv,
const struct my_option longopts,
my_get_one_option  get_one_option,
const char **  command_list,
bool  ignore_unknown_option,
bool  boolean_as_int 
)

Handle command line options.

Sort options. Put options first, until special end of options (–), or until the end of argv. Parse options, check that the given option matches with one of the options in struct 'my_option'. Check that option was given an argument if it requires one Call the optional 'get_one_option()' function once for each option.

Note that handle_options() can be invoked multiple times to parse a command line in several steps. In this case, use the global flag my_getopt_skip_unknown to indicate that options unknown in the current step should be preserved in the command line for later parsing in subsequent steps.

For 'long' options (–a_long_option), my_getopt_skip_unknown is fully supported. Command line parameters such as:

  • "--a_long_option"
  • "--a_long_option=value"
  • "--a_long_option value" will be preserved as is when the option is not known.

For 'short' options (-S), support for my_getopt_skip_unknown comes with some limitation, because several short options can also be specified together in the same command line argument, as in "-XYZ".

The first use case supported is: all short options are declared. handle_options() will be able to interpret "-XYZ" as one of:

  • an unknown X option
  • "-X -Y -Z", three short options with no arguments
  • "-X -YZ", where Y is a short option with argument Z
  • "-XYZ", where X is a short option with argument YZ based on the full short options specifications.

The second use case supported is: no short option is declared. handle_options() will reject "-XYZ" as unknown, to be parsed later.

The use case that is explicitly not supported is to provide only a partial list of short options to handle_options(). This function can not be expected to extract some option Y in the middle of the string "-XYZ" in these conditions, without knowing if X will be declared an option later.

Note that this limitation only impacts parsing of several short options from the same command line argument, as in "mysqld -anW5". When each short option is properly separated out in the command line argument, for example in "mysqld -a -n -w5", the code would actually work even with partial options specs given at each stage.

Parameters
[in,out]argccommand line options (count)
[in,out]argvcommand line options (values)
[in]longoptsdescriptor of all valid options
[in]get_one_optionoptional callback function to process each option, can be NULL.
[in]command_listNULL-terminated list of strings (commands) which (if set) is looked up for all non-option strings found while parsing the command line parameters. The parsing terminates if a match is found. At exit, argv [out] would contain all the remaining unparsed options along with the matched command.
[in]ignore_unknown_optionWhen set to true, options are continued to be read even when unknown options are encountered.
[in]boolean_as_intParse boolean as integer value. Mimic the logic of parsing booleans at runtime: Instead of parsing
bool_val = (str_val == '1' || str_val = 'ON') ?
true :
(str_val == '0' || str_val = 'OFF') ?
false :
false;
do:
bool_val == (str_val == 'OFF') ?
false :
((str_val == 'ON') ?
true :
atoi(str_val) != 0)
Returns
error in case of ambiguous or unknown options, 0 on success.

◆ my_print_help()

void my_print_help ( const struct my_option options)

◆ my_print_variables()

void my_print_variables ( const struct my_option options)

function: my_print_variables Print variables.

Parameters
[in]optionsmy_option list

◆ my_print_variables_ex()

void my_print_variables_ex ( const struct my_option options,
FILE *  file 
)

function: my_print_variables_ex Print variables to given file parameter stream (by default to stdout).

Parameters
[in]optionsmy_options list
[in]filestream where the output goes.

◆ print_cmdline_password_warning()

void print_cmdline_password_warning ( )

This function should be called to print a warning message if password string is specified on the command line.

◆ print_name()

static uint print_name ( const struct my_option optp,
FILE *  file = stdout 
)
static

Prints variable or option name, replacing _ with - to given file stream parameter (by default to stdout).

Parameters
[in]optpmy_option parameter
[in]filestream where the output of optp parameter name goes (by default to stdout).

◆ setval()

static int setval ( const struct my_option opts,
void *  value,
const char *  argument,
bool  set_maximum_value,
bool  boolean_as_int 
)
static

◆ setval_source()

static void setval_source ( const struct my_option opts,
void *  value 
)
static

Will set the source and file name from where this options is set in my_option struct.

Variable Documentation

◆ disabled_my_option

char* disabled_my_option = const_cast<char *>("0")

◆ enabled_my_option

char enabled_my_option[] = "1"
static

◆ getopt_get_addr

my_getopt_value getopt_get_addr
static

◆ my_getopt_error_reporter

my_error_reporter my_getopt_error_reporter = &my_message_local

◆ my_getopt_print_errors

bool my_getopt_print_errors = true

◆ my_getopt_skip_unknown

bool my_getopt_skip_unknown = false

◆ space_char

char space_char[] = " "
static

◆ special_opt_prefix

const char* special_opt_prefix[]
static
Initial value:
= {"skip", "disable", "enable",
"maximum", "loose", nullptr}

◆ special_opt_prefix_lengths

const uint special_opt_prefix_lengths[] = {4, 7, 6, 7, 5, 0}
static