You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

392 lines
10 KiB

26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
20 years ago
26 years ago
26 years ago
19 years ago
26 years ago
WL#3817: Simplify string / memory area types and make things more consistent (first part) The following type conversions was done: - Changed byte to uchar - Changed gptr to uchar* - Change my_string to char * - Change my_size_t to size_t - Change size_s to size_t Removed declaration of byte, gptr, my_string, my_size_t and size_s. Following function parameter changes was done: - All string functions in mysys/strings was changed to use size_t instead of uint for string lengths. - All read()/write() functions changed to use size_t (including vio). - All protocoll functions changed to use size_t instead of uint - Functions that used a pointer to a string length was changed to use size_t* - Changed malloc(), free() and related functions from using gptr to use void * as this requires fewer casts in the code and is more in line with how the standard functions work. - Added extra length argument to dirname_part() to return the length of the created string. - Changed (at least) following functions to take uchar* as argument: - db_dump() - my_net_write() - net_write_command() - net_store_data() - DBUG_DUMP() - decimal2bin() & bin2decimal() - Changed my_compress() and my_uncompress() to use size_t. Changed one argument to my_uncompress() from a pointer to a value as we only return one value (makes function easier to use). - Changed type of 'pack_data' argument to packfrm() to avoid casts. - Changed in readfrm() and writefrom(), ha_discover and handler::discover() the type for argument 'frmdata' to uchar** to avoid casts. - Changed most Field functions to use uchar* instead of char* (reduced a lot of casts). - Changed field->val_xxx(xxx, new_ptr) to take const pointers. Other changes: - Removed a lot of not needed casts - Added a few new cast required by other changes - Added some cast to my_multi_malloc() arguments for safety (as string lengths needs to be uint, not size_t). - Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done explicitely as this conflict was often hided by casting the function to hash_get_key). - Changed some buffers to memory regions to uchar* to avoid casts. - Changed some string lengths from uint to size_t. - Changed field->ptr to be uchar* instead of char*. This allowed us to get rid of a lot of casts. - Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar - Include zlib.h in some files as we needed declaration of crc32() - Changed MY_FILE_ERROR to be (size_t) -1. - Changed many variables to hold the result of my_read() / my_write() to be size_t. This was needed to properly detect errors (which are returned as (size_t) -1). - Removed some very old VMS code - Changed packfrm()/unpackfrm() to not be depending on uint size (portability fix) - Removed windows specific code to restore cursor position as this causes slowdown on windows and we should not mix read() and pread() calls anyway as this is not thread safe. Updated function comment to reflect this. Changed function that depended on original behavior of my_pwrite() to itself restore the cursor position (one such case). - Added some missing checking of return value of malloc(). - Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow. - Changed type of table_def::m_size from my_size_t to ulong to reflect that m_size is the number of elements in the array, not a string/memory length. - Moved THD::max_row_length() to table.cc (as it's not depending on THD). Inlined max_row_length_blob() into this function. - More function comments - Fixed some compiler warnings when compiled without partitions. - Removed setting of LEX_STRING() arguments in declaration (portability fix). - Some trivial indentation/variable name changes. - Some trivial code simplifications: - Replaced some calls to alloc_root + memcpy to use strmake_root()/strdup_root(). - Changed some calls from memdup() to strmake() (Safety fix) - Simpler loops in client-simple.c
19 years ago
WL#3817: Simplify string / memory area types and make things more consistent (first part) The following type conversions was done: - Changed byte to uchar - Changed gptr to uchar* - Change my_string to char * - Change my_size_t to size_t - Change size_s to size_t Removed declaration of byte, gptr, my_string, my_size_t and size_s. Following function parameter changes was done: - All string functions in mysys/strings was changed to use size_t instead of uint for string lengths. - All read()/write() functions changed to use size_t (including vio). - All protocoll functions changed to use size_t instead of uint - Functions that used a pointer to a string length was changed to use size_t* - Changed malloc(), free() and related functions from using gptr to use void * as this requires fewer casts in the code and is more in line with how the standard functions work. - Added extra length argument to dirname_part() to return the length of the created string. - Changed (at least) following functions to take uchar* as argument: - db_dump() - my_net_write() - net_write_command() - net_store_data() - DBUG_DUMP() - decimal2bin() & bin2decimal() - Changed my_compress() and my_uncompress() to use size_t. Changed one argument to my_uncompress() from a pointer to a value as we only return one value (makes function easier to use). - Changed type of 'pack_data' argument to packfrm() to avoid casts. - Changed in readfrm() and writefrom(), ha_discover and handler::discover() the type for argument 'frmdata' to uchar** to avoid casts. - Changed most Field functions to use uchar* instead of char* (reduced a lot of casts). - Changed field->val_xxx(xxx, new_ptr) to take const pointers. Other changes: - Removed a lot of not needed casts - Added a few new cast required by other changes - Added some cast to my_multi_malloc() arguments for safety (as string lengths needs to be uint, not size_t). - Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done explicitely as this conflict was often hided by casting the function to hash_get_key). - Changed some buffers to memory regions to uchar* to avoid casts. - Changed some string lengths from uint to size_t. - Changed field->ptr to be uchar* instead of char*. This allowed us to get rid of a lot of casts. - Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar - Include zlib.h in some files as we needed declaration of crc32() - Changed MY_FILE_ERROR to be (size_t) -1. - Changed many variables to hold the result of my_read() / my_write() to be size_t. This was needed to properly detect errors (which are returned as (size_t) -1). - Removed some very old VMS code - Changed packfrm()/unpackfrm() to not be depending on uint size (portability fix) - Removed windows specific code to restore cursor position as this causes slowdown on windows and we should not mix read() and pread() calls anyway as this is not thread safe. Updated function comment to reflect this. Changed function that depended on original behavior of my_pwrite() to itself restore the cursor position (one such case). - Added some missing checking of return value of malloc(). - Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow. - Changed type of table_def::m_size from my_size_t to ulong to reflect that m_size is the number of elements in the array, not a string/memory length. - Moved THD::max_row_length() to table.cc (as it's not depending on THD). Inlined max_row_length_blob() into this function. - More function comments - Fixed some compiler warnings when compiled without partitions. - Removed setting of LEX_STRING() arguments in declaration (portability fix). - Some trivial indentation/variable name changes. - Some trivial code simplifications: - Replaced some calls to alloc_root + memcpy to use strmake_root()/strdup_root(). - Changed some calls from memdup() to strmake() (Safety fix) - Simpler loops in client-simple.c
19 years ago
WL#3817: Simplify string / memory area types and make things more consistent (first part) The following type conversions was done: - Changed byte to uchar - Changed gptr to uchar* - Change my_string to char * - Change my_size_t to size_t - Change size_s to size_t Removed declaration of byte, gptr, my_string, my_size_t and size_s. Following function parameter changes was done: - All string functions in mysys/strings was changed to use size_t instead of uint for string lengths. - All read()/write() functions changed to use size_t (including vio). - All protocoll functions changed to use size_t instead of uint - Functions that used a pointer to a string length was changed to use size_t* - Changed malloc(), free() and related functions from using gptr to use void * as this requires fewer casts in the code and is more in line with how the standard functions work. - Added extra length argument to dirname_part() to return the length of the created string. - Changed (at least) following functions to take uchar* as argument: - db_dump() - my_net_write() - net_write_command() - net_store_data() - DBUG_DUMP() - decimal2bin() & bin2decimal() - Changed my_compress() and my_uncompress() to use size_t. Changed one argument to my_uncompress() from a pointer to a value as we only return one value (makes function easier to use). - Changed type of 'pack_data' argument to packfrm() to avoid casts. - Changed in readfrm() and writefrom(), ha_discover and handler::discover() the type for argument 'frmdata' to uchar** to avoid casts. - Changed most Field functions to use uchar* instead of char* (reduced a lot of casts). - Changed field->val_xxx(xxx, new_ptr) to take const pointers. Other changes: - Removed a lot of not needed casts - Added a few new cast required by other changes - Added some cast to my_multi_malloc() arguments for safety (as string lengths needs to be uint, not size_t). - Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done explicitely as this conflict was often hided by casting the function to hash_get_key). - Changed some buffers to memory regions to uchar* to avoid casts. - Changed some string lengths from uint to size_t. - Changed field->ptr to be uchar* instead of char*. This allowed us to get rid of a lot of casts. - Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar - Include zlib.h in some files as we needed declaration of crc32() - Changed MY_FILE_ERROR to be (size_t) -1. - Changed many variables to hold the result of my_read() / my_write() to be size_t. This was needed to properly detect errors (which are returned as (size_t) -1). - Removed some very old VMS code - Changed packfrm()/unpackfrm() to not be depending on uint size (portability fix) - Removed windows specific code to restore cursor position as this causes slowdown on windows and we should not mix read() and pread() calls anyway as this is not thread safe. Updated function comment to reflect this. Changed function that depended on original behavior of my_pwrite() to itself restore the cursor position (one such case). - Added some missing checking of return value of malloc(). - Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow. - Changed type of table_def::m_size from my_size_t to ulong to reflect that m_size is the number of elements in the array, not a string/memory length. - Moved THD::max_row_length() to table.cc (as it's not depending on THD). Inlined max_row_length_blob() into this function. - More function comments - Fixed some compiler warnings when compiled without partitions. - Removed setting of LEX_STRING() arguments in declaration (portability fix). - Some trivial indentation/variable name changes. - Some trivial code simplifications: - Replaced some calls to alloc_root + memcpy to use strmake_root()/strdup_root(). - Changed some calls from memdup() to strmake() (Safety fix) - Simpler loops in client-simple.c
19 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
  1. /* Copyright (C) 2000 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. /* Return error-text for system error messages and handler messages */
  13. #define PERROR_VERSION "2.11"
  14. #include <my_global.h>
  15. #include <my_sys.h>
  16. #include <m_string.h>
  17. #include <errno.h>
  18. #include <my_getopt.h>
  19. #ifdef WITH_NDBCLUSTER_STORAGE_ENGINE
  20. #include "../storage/ndb/src/ndbapi/ndberror.c"
  21. #include "../storage/ndb/src/kernel/error/ndbd_exit_codes.c"
  22. #include "../storage/ndb/include/mgmapi/mgmapi_error.h"
  23. #endif
  24. static my_bool verbose, print_all_codes;
  25. #include "../include/my_base.h"
  26. #include "../mysys/my_handler_errors.h"
  27. #include "../include/my_handler.h"
  28. #ifdef WITH_NDBCLUSTER_STORAGE_ENGINE
  29. static my_bool ndb_code;
  30. static char ndb_string[1024];
  31. int mgmapi_error_string(int err_no, char *str, int size)
  32. {
  33. int i;
  34. for (i= 0; i < ndb_mgm_noOfErrorMsgs; i++)
  35. {
  36. if ((int)ndb_mgm_error_msgs[i].code == err_no)
  37. {
  38. my_snprintf(str, size-1, "%s", ndb_mgm_error_msgs[i].msg);
  39. str[size-1]= '\0';
  40. return 0;
  41. }
  42. }
  43. return -1;
  44. }
  45. #endif
  46. static struct my_option my_long_options[] =
  47. {
  48. {"help", '?', "Displays this help and exits.", 0, 0, 0, GET_NO_ARG,
  49. NO_ARG, 0, 0, 0, 0, 0, 0},
  50. {"info", 'I', "Synonym for --help.", 0, 0, 0, GET_NO_ARG,
  51. NO_ARG, 0, 0, 0, 0, 0, 0},
  52. #ifdef WITH_NDBCLUSTER_STORAGE_ENGINE
  53. {"ndb", 257, "Ndbcluster storage engine specific error codes.", (uchar**) &ndb_code,
  54. (uchar**) &ndb_code, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
  55. #endif
  56. #ifdef HAVE_SYS_ERRLIST
  57. {"all", 'a', "Print all the error messages and the number.",
  58. (uchar**) &print_all_codes, (uchar**) &print_all_codes, 0, GET_BOOL, NO_ARG,
  59. 0, 0, 0, 0, 0, 0},
  60. #endif
  61. {"silent", 's', "Only print the error message.", 0, 0, 0, GET_NO_ARG, NO_ARG,
  62. 0, 0, 0, 0, 0, 0},
  63. {"verbose", 'v', "Print error code and message (default).", (uchar**) &verbose,
  64. (uchar**) &verbose, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
  65. {"version", 'V', "Displays version information and exits.",
  66. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  67. {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
  68. };
  69. typedef struct ha_errors {
  70. int errcode;
  71. const char *msg;
  72. } HA_ERRORS;
  73. static HA_ERRORS ha_errlist[]=
  74. {
  75. { -30999, "DB_INCOMPLETE: Sync didn't finish"},
  76. { -30998, "DB_KEYEMPTY: Key/data deleted or never created"},
  77. { -30997, "DB_KEYEXIST: The key/data pair already exists"},
  78. { -30996, "DB_LOCK_DEADLOCK: Deadlock"},
  79. { -30995, "DB_LOCK_NOTGRANTED: Lock unavailable"},
  80. { -30994, "DB_NOSERVER: Server panic return"},
  81. { -30993, "DB_NOSERVER_HOME: Bad home sent to server"},
  82. { -30992, "DB_NOSERVER_ID: Bad ID sent to server"},
  83. { -30991, "DB_NOTFOUND: Key/data pair not found (EOF)"},
  84. { -30990, "DB_OLD_VERSION: Out-of-date version"},
  85. { -30989, "DB_RUNRECOVERY: Panic return"},
  86. { -30988, "DB_VERIFY_BAD: Verify failed; bad format"},
  87. { 0,NullS },
  88. };
  89. #include <help_start.h>
  90. static void print_version(void)
  91. {
  92. printf("%s Ver %s, for %s (%s)\n",my_progname,PERROR_VERSION,
  93. SYSTEM_TYPE,MACHINE_TYPE);
  94. }
  95. static void usage(void)
  96. {
  97. print_version();
  98. puts("This software comes with ABSOLUTELY NO WARRANTY. This is free software,\nand you are welcome to modify and redistribute it under the GPL license\n");
  99. printf("Print a description for a system error code or a MySQL error code.\n");
  100. printf("If you want to get the error for a negative error code, you should use\n-- before the first error code to tell perror that there was no more options.\n\n");
  101. printf("Usage: %s [OPTIONS] [ERRORCODE [ERRORCODE...]]\n",my_progname);
  102. my_print_help(my_long_options);
  103. my_print_variables(my_long_options);
  104. }
  105. #include <help_end.h>
  106. static my_bool
  107. get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
  108. char *argument __attribute__((unused)))
  109. {
  110. switch (optid) {
  111. case 's':
  112. verbose=0;
  113. break;
  114. case 'V':
  115. print_version();
  116. exit(0);
  117. break;
  118. case 'I':
  119. case '?':
  120. usage();
  121. exit(0);
  122. break;
  123. }
  124. return 0;
  125. }
  126. static int get_options(int *argc,char ***argv)
  127. {
  128. int ho_error;
  129. if ((ho_error=handle_options(argc, argv, my_long_options, get_one_option)))
  130. exit(ho_error);
  131. if (!*argc && !print_all_codes)
  132. {
  133. usage();
  134. return 1;
  135. }
  136. return 0;
  137. } /* get_options */
  138. static const char *get_ha_error_msg(int code)
  139. {
  140. HA_ERRORS *ha_err_ptr;
  141. /*
  142. If you got compilation error here about compile_time_assert array, check
  143. that every HA_ERR_xxx constant has a corresponding error message in
  144. handler_error_messages[] list (check mysys/ma_handler_errors.h and
  145. include/my_base.h).
  146. */
  147. compile_time_assert(HA_ERR_FIRST + array_elements(handler_error_messages) ==
  148. HA_ERR_LAST + 1);
  149. if (code >= HA_ERR_FIRST && code <= HA_ERR_LAST)
  150. return handler_error_messages[code - HA_ERR_FIRST];
  151. for (ha_err_ptr=ha_errlist ; ha_err_ptr->errcode ;ha_err_ptr++)
  152. if (ha_err_ptr->errcode == code)
  153. return ha_err_ptr->msg;
  154. return NullS;
  155. }
  156. typedef struct
  157. {
  158. const char *name;
  159. uint code;
  160. const char *text;
  161. } st_error;
  162. static st_error global_error_names[] =
  163. {
  164. #include <mysqld_ername.h>
  165. { 0, 0, 0 }
  166. };
  167. /**
  168. Lookup an error by code in the global_error_names array.
  169. @param code the code to lookup
  170. @param [out] name_ptr the error name, when found
  171. @param [out] msg_ptr the error text, when found
  172. @return 1 when found, otherwise 0
  173. */
  174. int get_ER_error_msg(uint code, const char **name_ptr, const char **msg_ptr)
  175. {
  176. st_error *tmp_error;
  177. tmp_error= & global_error_names[0];
  178. while (tmp_error->name != NULL)
  179. {
  180. if (tmp_error->code == code)
  181. {
  182. *name_ptr= tmp_error->name;
  183. *msg_ptr= tmp_error->text;
  184. return 1;
  185. }
  186. tmp_error++;
  187. }
  188. return 0;
  189. }
  190. #if defined(__WIN__)
  191. static my_bool print_win_error_msg(DWORD error, my_bool verbose)
  192. {
  193. LPTSTR s;
  194. if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  195. FORMAT_MESSAGE_FROM_SYSTEM,
  196. NULL, error, 0, (LPTSTR)&s, 0,
  197. NULL))
  198. {
  199. if (verbose)
  200. printf("Win32 error code %d: %s", error, s);
  201. else
  202. puts(s);
  203. LocalFree(s);
  204. return 0;
  205. }
  206. return 1;
  207. }
  208. #endif
  209. int main(int argc,char *argv[])
  210. {
  211. int error,code,found;
  212. const char *msg;
  213. const char *name;
  214. char *unknown_error = 0;
  215. #if defined(__WIN__)
  216. my_bool skip_win_message= 0;
  217. #endif
  218. MY_INIT(argv[0]);
  219. if (get_options(&argc,&argv))
  220. exit(1);
  221. my_handler_error_register();
  222. error=0;
  223. #ifdef HAVE_SYS_ERRLIST
  224. if (print_all_codes)
  225. {
  226. HA_ERRORS *ha_err_ptr;
  227. for (code=1 ; code < sys_nerr ; code++)
  228. {
  229. if (sys_errlist[code][0])
  230. { /* Skip if no error-text */
  231. printf("%3d = %s\n",code,sys_errlist[code]);
  232. }
  233. }
  234. for (ha_err_ptr=ha_errlist ; ha_err_ptr->errcode ;ha_err_ptr++)
  235. printf("%3d = %s\n",ha_err_ptr->errcode,ha_err_ptr->msg);
  236. }
  237. else
  238. #endif
  239. {
  240. /*
  241. On some system, like NETWARE, strerror(unknown_error) returns a
  242. string 'Unknown Error'. To avoid printing it we try to find the
  243. error string by asking for an impossible big error message.
  244. On Solaris 2.8 it might return NULL
  245. */
  246. if ((msg= strerror(10000)) == NULL)
  247. msg= "Unknown Error";
  248. /*
  249. Allocate a buffer for unknown_error since strerror always returns
  250. the same pointer on some platforms such as Windows
  251. */
  252. unknown_error= malloc(strlen(msg)+1);
  253. strmov(unknown_error, msg);
  254. for ( ; argc-- > 0 ; argv++)
  255. {
  256. found=0;
  257. code=atoi(*argv);
  258. #ifdef WITH_NDBCLUSTER_STORAGE_ENGINE
  259. if (ndb_code)
  260. {
  261. if ((ndb_error_string(code, ndb_string, sizeof(ndb_string)) < 0) &&
  262. (ndbd_exit_string(code, ndb_string, sizeof(ndb_string)) < 0) &&
  263. (mgmapi_error_string(code, ndb_string, sizeof(ndb_string)) < 0))
  264. {
  265. msg= 0;
  266. }
  267. else
  268. msg= ndb_string;
  269. if (msg)
  270. {
  271. if (verbose)
  272. printf("NDB error code %3d: %s\n",code,msg);
  273. else
  274. puts(msg);
  275. }
  276. else
  277. {
  278. fprintf(stderr,"Illegal ndb error code: %d\n",code);
  279. error= 1;
  280. }
  281. found= 1;
  282. msg= 0;
  283. }
  284. else
  285. #endif
  286. msg = strerror(code);
  287. /*
  288. We don't print the OS error message if it is the same as the
  289. unknown_error message we retrieved above, or it starts with
  290. 'Unknown Error' (without regard to case).
  291. */
  292. if (msg &&
  293. my_strnncoll(&my_charset_latin1, (const uchar*) msg, 13,
  294. (const uchar*) "Unknown Error", 13) &&
  295. (!unknown_error || strcmp(msg, unknown_error)))
  296. {
  297. found= 1;
  298. if (verbose)
  299. printf("OS error code %3d: %s\n", code, msg);
  300. else
  301. puts(msg);
  302. }
  303. if ((msg= get_ha_error_msg(code)))
  304. {
  305. found= 1;
  306. if (verbose)
  307. printf("MySQL error code %3d: %s\n", code, msg);
  308. else
  309. puts(msg);
  310. }
  311. if (get_ER_error_msg(code, & name, & msg))
  312. {
  313. found= 1;
  314. if (verbose)
  315. printf("MySQL error code %3d (%s): %s\n", code, name, msg);
  316. else
  317. puts(msg);
  318. }
  319. if (!found)
  320. {
  321. #if defined(__WIN__)
  322. if (!(skip_win_message= !print_win_error_msg((DWORD)code, verbose)))
  323. {
  324. #endif
  325. fprintf(stderr,"Illegal error code: %d\n",code);
  326. error=1;
  327. #if defined(__WIN__)
  328. }
  329. #endif
  330. }
  331. #if defined(__WIN__)
  332. if (!skip_win_message)
  333. print_win_error_msg((DWORD)code, verbose);
  334. #endif
  335. }
  336. }
  337. /* if we allocated a buffer for unknown_error, free it now */
  338. if (unknown_error)
  339. free(unknown_error);
  340. exit(error);
  341. return error;
  342. }