my_getopt.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License, version 2.0,
  5. as published by the Free Software Foundation.
  6. This program is also distributed with certain software (including
  7. but not limited to OpenSSL) that is licensed under separate terms,
  8. as designated in a particular file or component or in included license
  9. documentation. The authors of MySQL hereby grant you an additional
  10. permission to link the program and your derivative works with the
  11. separately licensed software that they have included with MySQL.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License, version 2.0, for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  19. #ifndef _my_getopt_h
  20. #define _my_getopt_h
  21. /**
  22. @file include/my_getopt.h
  23. */
  24. #include <stdio.h>
  25. #include <sys/types.h>
  26. #include <mysql/components/services/system_variable_source_type.h> /* enum_variable_source */
  27. #include "my_config.h"
  28. #include "my_inttypes.h"
  29. #include "my_io.h"
  30. #include "my_macros.h"
  31. #include "my_sys.h" /* loglevel */
  32. #define GET_NO_ARG 1
  33. #define GET_BOOL 2
  34. #define GET_INT 3
  35. #define GET_UINT 4
  36. #define GET_LONG 5
  37. #define GET_ULONG 6
  38. #define GET_LL 7
  39. #define GET_ULL 8
  40. #define GET_STR 9
  41. #define GET_STR_ALLOC 10
  42. #define GET_DISABLED 11
  43. #define GET_ENUM 12
  44. #define GET_SET 13
  45. #define GET_DOUBLE 14
  46. #define GET_FLAGSET 15
  47. #define GET_PASSWORD 16
  48. #if SIZEOF_INT == 4
  49. #define GET_INT32 GET_INT
  50. #define GET_UINT32 GET_UINT
  51. #elif SIZEOF_LONG == 4
  52. #define GET_INT32 GET_LONG
  53. #define GET_UINT32 GET_ULONG
  54. #else
  55. #error Neither int or long is of 4 bytes width
  56. #endif
  57. #define GET_ASK_ADDR 128
  58. #define GET_TYPE_MASK 127
  59. /**
  60. Enumeration of the my_option::arg_type attributes.
  61. It should be noted that for historical reasons variables with the combination
  62. arg_type=NO_ARG, my_option::var_type=GET_BOOL still accepts
  63. arguments. This is someone counter intuitive and care should be taken
  64. if the code is refactored.
  65. */
  66. enum get_opt_arg_type { NO_ARG, OPT_ARG, REQUIRED_ARG };
  67. struct get_opt_arg_source {
  68. /**
  69. config file path OR compiled default values
  70. */
  71. char m_path_name[FN_REFLEN];
  72. enum enum_variable_source m_source;
  73. };
  74. struct TYPELIB;
  75. struct my_option {
  76. const char *name; /**< Name of the option. name=NULL
  77. marks the end of the my_option[]
  78. array.
  79. */
  80. int id; /**< For 0<id<=255 it's means one
  81. character for a short option
  82. (like -A), if >255 no short option
  83. is created, but a long option still
  84. can be identified uniquely in the
  85. my_get_one_option() callback.
  86. If an opton needs neither special
  87. treatment in the my_get_one_option()
  88. nor one-letter short equivalent
  89. use id=0.
  90. id=-1 is a special case and is used
  91. to generate deprecation warnings for
  92. plugin options. It should not be
  93. used for anything else.
  94. */
  95. const char *comment; /**< option comment, for autom. --help.
  96. if it's NULL the option is not
  97. visible in --help.
  98. */
  99. void *value; /**< A pointer to the variable value */
  100. void *u_max_value; /**< The user def. max variable value */
  101. TYPELIB *typelib; /**< Pointer to possible values */
  102. ulong var_type; /**< GET_BOOL, GET_ULL, etc */
  103. enum get_opt_arg_type arg_type; /**< e.g. REQUIRED_ARG or OPT_ARG */
  104. longlong def_value; /**< Default value */
  105. longlong min_value; /**< Min allowed value (for numbers) */
  106. ulonglong max_value; /**< Max allowed value (for numbers) */
  107. struct get_opt_arg_source *arg_source; /**< Represents source/path from where
  108. this variable is set. */
  109. long block_size; /**< Value should be a mult. of this (for numbers) */
  110. void *app_type; /**< To be used by an application */
  111. };
  112. typedef bool (*my_get_one_option)(int, const struct my_option *, char *);
  113. /**
  114. Used to retrieve a reference to the object (variable) that holds the value
  115. for the given option. For example, if var_type is GET_UINT, the function
  116. must return a pointer to a variable of type uint. A argument is stored in
  117. the location pointed to by the returned pointer.
  118. */
  119. typedef void *(*my_getopt_value)(const char *, size_t, const struct my_option *,
  120. int *);
  121. extern char *disabled_my_option;
  122. extern bool my_getopt_print_errors;
  123. extern bool my_getopt_skip_unknown;
  124. extern bool log_slave_updates_supplied;
  125. extern bool slave_preserve_commit_order_supplied;
  126. extern my_error_reporter my_getopt_error_reporter;
  127. extern "C" int handle_options(int *argc, char ***argv,
  128. const struct my_option *longopts,
  129. my_get_one_option);
  130. extern int my_handle_options(int *argc, char ***argv,
  131. const struct my_option *longopts,
  132. my_get_one_option, const char **command_list,
  133. bool ignore_unknown_option);
  134. extern void print_cmdline_password_warning();
  135. extern void my_cleanup_options(const struct my_option *options);
  136. extern void my_print_help(const struct my_option *options);
  137. extern void my_print_variables(const struct my_option *options);
  138. extern void my_print_variables_ex(const struct my_option *options, FILE *file);
  139. extern void my_getopt_register_get_addr(my_getopt_value);
  140. ulonglong getopt_ull_limit_value(ulonglong num, const struct my_option *optp,
  141. bool *fix);
  142. longlong getopt_ll_limit_value(longlong, const struct my_option *, bool *fix);
  143. double getopt_double_limit_value(double num, const struct my_option *optp,
  144. bool *fix);
  145. ulonglong max_of_int_range(int var_type);
  146. ulonglong getopt_double2ulonglong(double);
  147. double getopt_ulonglong2double(ulonglong);
  148. int findopt(char *, uint, const struct my_option **);
  149. bool is_key_cache_variable_suffix(const char *suffix);
  150. // Declared here, so we can unit test it.
  151. template <typename LLorULL>
  152. LLorULL eval_num_suffix(const char *argument, int *error,
  153. const char *option_name);
  154. #endif /* _my_getopt_h */