my_getopt.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. Copyright (c) 2002, 2014, 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. #include "my_sys.h" /* loglevel */
  22. C_MODE_START
  23. #define GET_NO_ARG 1
  24. #define GET_BOOL 2
  25. #define GET_INT 3
  26. #define GET_UINT 4
  27. #define GET_LONG 5
  28. #define GET_ULONG 6
  29. #define GET_LL 7
  30. #define GET_ULL 8
  31. #define GET_STR 9
  32. #define GET_STR_ALLOC 10
  33. #define GET_DISABLED 11
  34. #define GET_ENUM 12
  35. #define GET_SET 13
  36. #define GET_DOUBLE 14
  37. #define GET_FLAGSET 15
  38. #define GET_PASSWORD 16
  39. #if SIZEOF_INT == 4
  40. #define GET_INT32 GET_INT
  41. #define GET_UINT32 GET_UINT
  42. #elif SIZEOF_LONG == 4
  43. #define GET_INT32 GET_LONG
  44. #define GET_UINT32 GET_ULONG
  45. #else
  46. #error Neither int or long is of 4 bytes width
  47. #endif
  48. #define GET_ASK_ADDR 128
  49. #define GET_TYPE_MASK 127
  50. /**
  51. Enumeration of the my_option::arg_type attributes.
  52. It should be noted that for historical reasons variables with the combination
  53. arg_type=NO_ARG, my_option::var_type=GET_BOOL still accepts
  54. arguments. This is someone counter intuitive and care should be taken
  55. if the code is refactored.
  56. */
  57. enum get_opt_arg_type { NO_ARG, OPT_ARG, REQUIRED_ARG };
  58. struct st_typelib;
  59. struct my_option
  60. {
  61. const char *name; /**< Name of the option. name=NULL
  62. marks the end of the my_option[]
  63. array.
  64. */
  65. int id; /**< For 0<id<=255 it's means one
  66. character for a short option
  67. (like -A), if >255 no short option
  68. is created, but a long option still
  69. can be identified uniquely in the
  70. my_get_one_option() callback.
  71. If an opton needs neither special
  72. treatment in the my_get_one_option()
  73. nor one-letter short equivalent
  74. use id=0.
  75. id=-1 is a special case and is used
  76. to generate deprecation warnings for
  77. plugin options. It should not be
  78. used for anything else.
  79. */
  80. const char *comment; /**< option comment, for autom. --help.
  81. if it's NULL the option is not
  82. visible in --help.
  83. */
  84. void *value; /**< A pointer to the variable value */
  85. void *u_max_value; /**< The user def. max variable value */
  86. struct st_typelib *typelib; /**< Pointer to possible values */
  87. ulong var_type; /**< GET_BOOL, GET_ULL, etc */
  88. enum get_opt_arg_type arg_type; /**< e.g. REQUIRED_ARG or OPT_ARG */
  89. longlong def_value; /**< Default value */
  90. longlong min_value; /**< Min allowed value (for numbers) */
  91. ulonglong max_value; /**< Max allowed value (for numbers) */
  92. longlong sub_size; /**< Unused */
  93. long block_size; /**< Value should be a mult. of this (for numbers) */
  94. void *app_type; /**< To be used by an application */
  95. };
  96. typedef my_bool (*my_get_one_option)(int, const struct my_option *, char *);
  97. /**
  98. Used to retrieve a reference to the object (variable) that holds the value
  99. for the given option. For example, if var_type is GET_UINT, the function
  100. must return a pointer to a variable of type uint. A argument is stored in
  101. the location pointed to by the returned pointer.
  102. */
  103. typedef void *(*my_getopt_value)(const char *, size_t, const struct my_option *,
  104. int *);
  105. extern char *disabled_my_option;
  106. extern my_bool my_getopt_print_errors;
  107. extern my_bool my_getopt_skip_unknown;
  108. extern my_error_reporter my_getopt_error_reporter;
  109. extern int handle_options (int *argc, char ***argv,
  110. const struct my_option *longopts, my_get_one_option);
  111. extern int my_handle_options (int *argc, char ***argv,
  112. const struct my_option *longopts,
  113. my_get_one_option,
  114. const char **command_list, my_bool ignore_unknown_option);
  115. extern void print_cmdline_password_warning();
  116. extern void my_cleanup_options(const struct my_option *options);
  117. extern void my_cleanup_options(const struct my_option *options);
  118. extern void my_print_help(const struct my_option *options);
  119. extern void my_print_variables(const struct my_option *options);
  120. extern void my_print_variables_ex(const struct my_option *options, FILE* file);
  121. extern void my_getopt_register_get_addr(my_getopt_value);
  122. ulonglong getopt_ull_limit_value(ulonglong num, const struct my_option *optp,
  123. my_bool *fix);
  124. longlong getopt_ll_limit_value(longlong, const struct my_option *,
  125. my_bool *fix);
  126. double getopt_double_limit_value(double num, const struct my_option *optp,
  127. my_bool *fix);
  128. my_bool getopt_compare_strings(const char *s, const char *t, uint length);
  129. ulonglong max_of_int_range(int var_type);
  130. ulonglong getopt_double2ulonglong(double);
  131. double getopt_ulonglong2double(ulonglong);
  132. C_MODE_END
  133. #endif /* _my_getopt_h */