derror.cc 5.78 KB
Newer Older
1
/* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3 4
   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
kent@mysql.com/kent-amd64.(none)'s avatar
kent@mysql.com/kent-amd64.(none) committed
5
   the Free Software Foundation; version 2 of the License.
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7 8 9 10
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
11

bk@work.mysql.com's avatar
bk@work.mysql.com committed
12 13
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
15

16 17 18 19 20 21
/**
  @file

  @brief
  Read language depeneded messagefile
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
22 23 24 25

#include "mysql_priv.h"
#include "mysys_err.h"

26
static bool read_texts(const char *file_name,const char ***point,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
27 28 29
		       uint error_messages);
static void init_myfunc_errs(void);

30
/**
31 32
  Read messages from errorfile.

33 34 35 36
  This function can be called multiple times to reload the messages.
  If it fails to load the messages, it will fail softly by initializing
  the errmesg pointer to an array of empty strings or by keeping the
  old array if it exists.
37

38
  @retval
39
    FALSE       OK
40
  @retval
41 42
    TRUE        Error
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
43

44
bool init_errmessage(void)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
45
{
46
  const char **errmsgs, **ptr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
47 48
  DBUG_ENTER("init_errmessage");

49 50 51 52 53 54 55
  /*
    Get a pointer to the old error messages pointer array.
    read_texts() tries to free it.
  */
  errmsgs= my_error_unregister(ER_ERROR_FIRST, ER_ERROR_LAST);

  /* Read messages from file. */
56 57 58 59 60 61 62 63 64
  if (read_texts(ERRMSG_FILE, &errmsgs, ER_ERROR_LAST - ER_ERROR_FIRST + 1) &&
      !errmsgs)
  {
    if (!(errmsgs= (const char**) my_malloc((ER_ERROR_LAST-ER_ERROR_FIRST+1)*
                                            sizeof(char*), MYF(0))))
      DBUG_RETURN(TRUE);
    for (ptr= errmsgs; ptr < errmsgs + ER_ERROR_LAST - ER_ERROR_FIRST; ptr++)
	  *ptr= "";
  }
65 66 67 68

  /* Register messages for use with my_error(). */
  if (my_error_register(errmsgs, ER_ERROR_FIRST, ER_ERROR_LAST))
  {
69
    x_free((uchar*) errmsgs);
70 71 72 73
    DBUG_RETURN(TRUE);
  }

  errmesg= errmsgs;		        /* Init global variabel */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
74
  init_myfunc_errs();			/* Init myfunc messages */
75
  DBUG_RETURN(FALSE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
76 77 78
}


79 80 81 82 83 84 85 86
/**
  Read text from packed textfile in language-directory.

  If we can't read messagefile then it's panic- we can't continue.

  @todo
    Convert the character set to server system character set
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
87

88
static bool read_texts(const char *file_name,const char ***point,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
89 90 91
		       uint error_messages)
{
  register uint i;
92 93
  uint count,funktpos,textcount;
  size_t length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
94 95
  File file;
  char name[FN_REFLEN];
96
  uchar *buff;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
97 98 99 100 101 102 103 104 105 106 107
  uchar head[32],*pos;
  DBUG_ENTER("read_texts");

  LINT_INIT(buff);
  funktpos=0;
  if ((file=my_open(fn_format(name,file_name,language,"",4),
		    O_RDONLY | O_SHARE | O_BINARY,
		    MYF(0))) < 0)
    goto err; /* purecov: inspected */

  funktpos=1;
108
  if (my_read(file,(uchar*) head,32,MYF(MY_NABP))) goto err;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
109 110 111
  if (head[0] != (uchar) 254 || head[1] != (uchar) 254 ||
      head[2] != 2 || head[3] != 1)
    goto err; /* purecov: inspected */
112
  textcount=head[4];
113

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
114 115
  if (!head[30])
  {
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
116
    sql_print_error("Character set information not found in '%s'. \
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
117
Please install the latest version of this file.",name);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
118 119 120
    goto err1;
  }
  
121
  /* TODO: Convert the character set to server system character set */
122
  if (!get_charset(head[30],MYF(MY_WME)))
123
  {
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
124 125 126
    sql_print_error("Character set #%d is not supported for messagefile '%s'",
                    (int)head[30],name);
    goto err1;
127 128
  }
  
129
  length=uint2korr(head+6); count=uint2korr(head+8);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
130

131
  if (count < error_messages)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
132
  {
133 134 135 136 137
    sql_print_error("\
Error message file '%s' had only %d error messages,\n\
but it should contain at least %d error messages.\n\
Check that the above file is the right version for this program!",
		    name,count,error_messages);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
138
    VOID(my_close(file,MYF(MY_WME)));
139
    DBUG_RETURN(1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
140 141
  }

142
  x_free((uchar*) *point);		/* Free old language */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
143
  if (!(*point= (const char**)
144
	my_malloc((size_t) (length+count*sizeof(char*)),MYF(0))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
145 146 147 148
  {
    funktpos=2;					/* purecov: inspected */
    goto err;					/* purecov: inspected */
  }
149
  buff= (uchar*) (*point + count);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
150

151 152 153
  if (my_read(file, buff, (size_t) count*2,MYF(MY_NABP)))
    goto err;
  for (i=0, pos= buff ; i< count ; i++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
154
  {
155
    (*point)[i]= (char*) buff+uint2korr(pos);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
156 157
    pos+=2;
  }
158 159
  if (my_read(file, buff, length, MYF(MY_NABP)))
    goto err;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
160

161
  for (i=1 ; i < textcount ; i++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
162 163 164 165
  {
    point[i]= *point +uint2korr(head+10+i+i);
  }
  VOID(my_close(file,MYF(0)));
monty@narttu.mysql.fi's avatar
Merge  
monty@narttu.mysql.fi committed
166
  DBUG_RETURN(0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
167 168

err:
169 170 171
  sql_print_error((funktpos == 2) ? "Not enough memory for messagefile '%s'" :
                  ((funktpos == 1) ? "Can't read from messagefile '%s'" :
                   "Can't find messagefile '%s'"), name);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
172
err1:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
173 174
  if (file != FERR)
    VOID(my_close(file,MYF(MY_WME)));
175
  DBUG_RETURN(1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
176 177 178
} /* read_texts */


179 180 181
/**
  Initiates error-messages used by my_func-library.
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
182 183 184 185 186 187

static void init_myfunc_errs()
{
  init_glob_errs();			/* Initiate english errors */
  if (!(specialflag & SPECIAL_ENGLISH))
  {
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    EE(EE_FILENOTFOUND)   = ER(ER_FILE_NOT_FOUND);
    EE(EE_CANTCREATEFILE) = ER(ER_CANT_CREATE_FILE);
    EE(EE_READ)           = ER(ER_ERROR_ON_READ);
    EE(EE_WRITE)          = ER(ER_ERROR_ON_WRITE);
    EE(EE_BADCLOSE)       = ER(ER_ERROR_ON_CLOSE);
    EE(EE_OUTOFMEMORY)    = ER(ER_OUTOFMEMORY);
    EE(EE_DELETE)         = ER(ER_CANT_DELETE_FILE);
    EE(EE_LINK)           = ER(ER_ERROR_ON_RENAME);
    EE(EE_EOFERR)         = ER(ER_UNEXPECTED_EOF);
    EE(EE_CANTLOCK)       = ER(ER_CANT_LOCK);
    EE(EE_DIR)            = ER(ER_CANT_READ_DIR);
    EE(EE_STAT)           = ER(ER_CANT_GET_STAT);
    EE(EE_GETWD)          = ER(ER_CANT_GET_WD);
    EE(EE_SETWD)          = ER(ER_CANT_SET_WD);
    EE(EE_DISK_FULL)      = ER(ER_DISK_FULL);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
203 204
  }
}