my_error.c 7.93 KB
Newer Older
unknown's avatar
unknown committed
1 2 3 4
/* Copyright (C) 2000 MySQL 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
unknown's avatar
unknown committed
5
   the Free Software Foundation; version 2 of the License.
unknown's avatar
unknown committed
6 7

   This program is distributed in the hope that it will be useful,
unknown's avatar
unknown committed
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
unknown's avatar
unknown committed
9 10 11 12 13 14
   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 */
unknown's avatar
unknown committed
15 16 17 18 19 20 21

#include "mysys_priv.h"
#include "mysys_err.h"
#include <m_string.h>
#include <stdarg.h>
#include <m_ctype.h>

22 23 24 25
/* Max length of a error message. Should be kept in sync with MYSQL_ERRMSG_SIZE. */
#define ERRMSGSIZE      (512)


unknown's avatar
unknown committed
26 27
/* Define some external variables for error handling */

unknown's avatar
unknown committed
28 29 30 31
/*
  WARNING!
  my_error family functions have to be used according following rules:
  - if message have not parameters use my_message(ER_CODE, ER(ER_CODE), MYF(N))
32 33 34
  - if message registered use my_error(ER_CODE, MYF(N), ...).
  - With some special text of errror message use:
  my_printf_error(ER_CODE, format, MYF(N), ...)
unknown's avatar
unknown committed
35 36
*/

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/*
  Message texts are registered into a linked list of 'my_err_head' structs.
  Each struct contains (1.) an array of pointers to C character strings with
  '\0' termination, (2.) the error number for the first message in the array
  (array index 0) and (3.) the error number for the last message in the array
  (array index (last - first)).
  The array may contain gaps with NULL pointers and pointers to empty strings.
  Both kinds of gaps will be translated to "Unknown error %d.", if my_error()
  is called with a respective error number.
  The list of header structs is sorted in increasing order of error numbers.
  Negative error numbers are allowed. Overlap of error numbers is not allowed.
  Not registered error numbers will be translated to "Unknown error %d.".
*/
static struct my_err_head
{
  struct my_err_head    *meh_next;      /* chain link */
  const char            **meh_errmsgs;  /* error messages array */
  int                   meh_first;      /* error number matching array slot 0 */
  int                   meh_last;       /* error number matching last slot */
} my_errmsgs_globerrs = {NULL, globerrs, EE_ERROR_FIRST, EE_ERROR_LAST};
unknown's avatar
unknown committed
57

58 59
static struct my_err_head *my_errmsgs_list= &my_errmsgs_globerrs;

unknown's avatar
unknown committed
60

unknown's avatar
unknown committed
61 62 63 64 65 66 67 68
/*
   Error message to user

   SYNOPSIS
     my_error()
       nr	Errno
       MyFlags	Flags
       ...	variable list
69

70 71 72
  RETURN
    What (*error_handler_hook)() returns:
    0   OK
unknown's avatar
unknown committed
73
*/
unknown's avatar
unknown committed
74

75
int my_error(int nr, myf MyFlags, ...)
unknown's avatar
unknown committed
76
{
77
  const char *format;
78
  struct my_err_head *meh_p;
79
  va_list args;
80
  char ebuff[ERRMSGSIZE];
unknown's avatar
unknown committed
81 82 83
  DBUG_ENTER("my_error");
  DBUG_PRINT("my", ("nr: %d  MyFlags: %d  errno: %d", nr, MyFlags, errno));

84 85 86 87
  /* Search for the error messages array, which could contain the message. */
  for (meh_p= my_errmsgs_list; meh_p; meh_p= meh_p->meh_next)
    if (nr <= meh_p->meh_last)
      break;
unknown's avatar
unknown committed
88

89 90 91 92 93 94 95 96 97 98
  /* get the error message string. Default, if NULL or empty string (""). */
  if (! (format= (meh_p && (nr >= meh_p->meh_first)) ?
         meh_p->meh_errmsgs[nr - meh_p->meh_first] : NULL) || ! *format)
    (void) my_snprintf (ebuff, sizeof(ebuff), "Unknown error %d", nr);
  else
  {
    va_start(args,MyFlags);
    (void) my_vsnprintf (ebuff, sizeof(ebuff), format, args);
    va_end(args);
  }
unknown's avatar
unknown committed
99 100 101
  DBUG_RETURN((*error_handler_hook)(nr, ebuff, MyFlags));
}

unknown's avatar
unknown committed
102

unknown's avatar
unknown committed
103 104 105 106 107 108 109 110 111 112
/*
  Error as printf

  SYNOPSIS
    my_printf_error()
      error	Errno
      format	Format string
      MyFlags	Flags
      ...	variable list
*/
unknown's avatar
unknown committed
113

114
int my_printf_error(uint error, const char *format, myf MyFlags, ...)
unknown's avatar
unknown committed
115 116
{
  va_list args;
117
  char ebuff[ERRMSGSIZE];
118 119 120
  DBUG_ENTER("my_printf_error");
  DBUG_PRINT("my", ("nr: %d  MyFlags: %d  errno: %d  Format: %s",
		    error, MyFlags, errno, format));
unknown's avatar
unknown committed
121 122

  va_start(args,MyFlags);
123
  (void) my_vsnprintf (ebuff, sizeof(ebuff), format, args);
unknown's avatar
unknown committed
124
  va_end(args);
125
  DBUG_RETURN((*error_handler_hook)(error, ebuff, MyFlags));
unknown's avatar
unknown committed
126 127
}

unknown's avatar
unknown committed
128 129 130 131 132 133 134 135 136
/*
  Give message using error_handler_hook

  SYNOPSIS
    my_message()
      error	Errno
      str	Error message
      MyFlags	Flags
*/
unknown's avatar
unknown committed
137 138 139 140 141

int my_message(uint error, const char *str, register myf MyFlags)
{
  return (*error_handler_hook)(error, str, MyFlags);
}
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189


/*
  Register error messages for use with my_error().

  SYNOPSIS
    my_error_register()
    errmsgs                     array of pointers to error messages
    first                       error number of first message in the array
    last                        error number of last message in the array

  DESCRIPTION
    The pointer array is expected to contain addresses to NUL-terminated
    C character strings. The array contains (last - first + 1) pointers.
    NULL pointers and empty strings ("") are allowed. These will be mapped to
    "Unknown error" when my_error() is called with a matching error number.
    This function registers the error numbers 'first' to 'last'.
    No overlapping with previously registered error numbers is allowed.

  RETURN
    0           OK
    != 0        Error
*/

int my_error_register(const char **errmsgs, int first, int last)
{
  struct my_err_head *meh_p;
  struct my_err_head **search_meh_pp;

  /* Allocate a new header structure. */
  if (! (meh_p= (struct my_err_head*) my_malloc(sizeof(struct my_err_head),
                                                MYF(MY_WME))))
    return 1;
  meh_p->meh_errmsgs= errmsgs;
  meh_p->meh_first= first;
  meh_p->meh_last= last;

  /* Search for the right position in the list. */
  for (search_meh_pp= &my_errmsgs_list;
       *search_meh_pp;
       search_meh_pp= &(*search_meh_pp)->meh_next)
  {
    if ((*search_meh_pp)->meh_last > first)
      break;
  }

  /* Error numbers must be unique. No overlapping is allowed. */
  if (*search_meh_pp && ((*search_meh_pp)->meh_first <= last))
190
  {
191
    my_free((uchar*)meh_p, MYF(0));
192
    return 1;
193
  }
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

  /* Insert header into the chain. */
  meh_p->meh_next= *search_meh_pp;
  *search_meh_pp= meh_p;
  return 0;
}


/*
  Unregister formerly registered error messages.

  SYNOPSIS
    my_error_unregister()
    first                       error number of first message
    last                        error number of last message

  DESCRIPTION
    This function unregisters the error numbers 'first' to 'last'.
    These must have been previously registered by my_error_register().
    'first' and 'last' must exactly match the registration.
    If a matching registration is present, the header is removed from the
    list and the pointer to the error messages pointers array is returned.
    Otherwise, NULL is returned.

  RETURN
    non-NULL    OK, returns address of error messages pointers array.
    NULL        Error, no such number range registered.
*/

const char **my_error_unregister(int first, int last)
{
  struct my_err_head    *meh_p;
  struct my_err_head    **search_meh_pp;
  const char            **errmsgs;

  /* Search for the registration in the list. */
  for (search_meh_pp= &my_errmsgs_list;
       *search_meh_pp;
       search_meh_pp= &(*search_meh_pp)->meh_next)
  {
    if (((*search_meh_pp)->meh_first == first) &&
        ((*search_meh_pp)->meh_last == last))
      break;
  }
  if (! *search_meh_pp)
    return NULL;

  /* Remove header from the chain. */
  meh_p= *search_meh_pp;
  *search_meh_pp= meh_p->meh_next;

  /* Save the return value and free the header. */
  errmsgs= meh_p->meh_errmsgs;
247
  my_free((uchar*) meh_p, MYF(0));
248 249 250
  
  return errmsgs;
}
251 252 253 254


void my_error_unregister_all(void)
{
255 256 257
  struct my_err_head *cursor, *saved_next;

  for (cursor= my_errmsgs_globerrs.meh_next; cursor != NULL; cursor= saved_next)
258
  {
Chad MILLER's avatar
Chad MILLER committed
259
    /* We need this ptr, but we're about to free its container, so save it. */
260 261 262
    saved_next= cursor->meh_next;

    my_free((uchar*) cursor, MYF(0));
263
  }
264 265
  my_errmsgs_globerrs.meh_next= NULL;  /* Freed in first iteration above. */

266 267
  my_errmsgs_list= &my_errmsgs_globerrs;
}