~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to sql/derror.cc

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000-2005 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
 
 
17
/**
 
18
  @file
 
19
 
 
20
  @brief
 
21
  Read language depeneded messagefile
 
22
*/
 
23
 
 
24
#include "mysql_priv.h"
 
25
#include "mysys_err.h"
 
26
 
 
27
static bool read_texts(const char *file_name,const char ***point,
 
28
                       uint error_messages);
 
29
static void init_myfunc_errs(void);
 
30
 
 
31
/**
 
32
  Read messages from errorfile.
 
33
 
 
34
  This function can be called multiple times to reload the messages.
 
35
  If it fails to load the messages, it will fail softly by initializing
 
36
  the errmesg pointer to an array of empty strings or by keeping the
 
37
  old array if it exists.
 
38
 
 
39
  @retval
 
40
    FALSE       OK
 
41
  @retval
 
42
    TRUE        Error
 
43
*/
 
44
 
 
45
bool init_errmessage(void)
 
46
{
 
47
  const char **errmsgs, **ptr;
 
48
  DBUG_ENTER("init_errmessage");
 
49
 
 
50
  /*
 
51
    Get a pointer to the old error messages pointer array.
 
52
    read_texts() tries to free it.
 
53
  */
 
54
  errmsgs= my_error_unregister(ER_ERROR_FIRST, ER_ERROR_LAST);
 
55
 
 
56
  /* Read messages from file. */
 
57
  if (read_texts(ERRMSG_FILE, &errmsgs, ER_ERROR_LAST - ER_ERROR_FIRST + 1) &&
 
58
      !errmsgs)
 
59
  {
 
60
    if (!(errmsgs= (const char**) my_malloc((ER_ERROR_LAST-ER_ERROR_FIRST+1)*
 
61
                                            sizeof(char*), MYF(0))))
 
62
      DBUG_RETURN(TRUE);
 
63
    for (ptr= errmsgs; ptr < errmsgs + ER_ERROR_LAST - ER_ERROR_FIRST; ptr++)
 
64
          *ptr= "";
 
65
  }
 
66
 
 
67
  /* Register messages for use with my_error(). */
 
68
  if (my_error_register(errmsgs, ER_ERROR_FIRST, ER_ERROR_LAST))
 
69
  {
 
70
    x_free((uchar*) errmsgs);
 
71
    DBUG_RETURN(TRUE);
 
72
  }
 
73
 
 
74
  errmesg= errmsgs;                     /* Init global variabel */
 
75
  init_myfunc_errs();                   /* Init myfunc messages */
 
76
  DBUG_RETURN(FALSE);
 
77
}
 
78
 
 
79
 
 
80
/**
 
81
  Read text from packed textfile in language-directory.
 
82
 
 
83
  If we can't read messagefile then it's panic- we can't continue.
 
84
 
 
85
  @todo
 
86
    Convert the character set to server system character set
 
87
*/
 
88
 
 
89
static bool read_texts(const char *file_name,const char ***point,
 
90
                       uint error_messages)
 
91
{
 
92
  register uint i;
 
93
  uint count,funktpos,textcount;
 
94
  size_t length;
 
95
  File file;
 
96
  char name[FN_REFLEN];
 
97
  uchar *buff;
 
98
  uchar head[32],*pos;
 
99
  const char *errmsg;
 
100
  DBUG_ENTER("read_texts");
 
101
 
 
102
  LINT_INIT(buff);
 
103
  funktpos=0;
 
104
  if ((file=my_open(fn_format(name,file_name,language,"",4),
 
105
                    O_RDONLY | O_SHARE | O_BINARY,
 
106
                    MYF(0))) < 0)
 
107
    goto err; /* purecov: inspected */
 
108
 
 
109
  funktpos=1;
 
110
  if (my_read(file,(uchar*) head,32,MYF(MY_NABP))) goto err;
 
111
  if (head[0] != (uchar) 254 || head[1] != (uchar) 254 ||
 
112
      head[2] != 2 || head[3] != 1)
 
113
    goto err; /* purecov: inspected */
 
114
  textcount=head[4];
 
115
 
 
116
  if (!head[30])
 
117
  {
 
118
    sql_print_error("Character set information not found in '%s'. \
 
119
Please install the latest version of this file.",name);
 
120
    goto err1;
 
121
  }
 
122
  
 
123
  /* TODO: Convert the character set to server system character set */
 
124
  if (!get_charset(head[30],MYF(MY_WME)))
 
125
  {
 
126
    sql_print_error("Character set #%d is not supported for messagefile '%s'",
 
127
                    (int)head[30],name);
 
128
    goto err1;
 
129
  }
 
130
  
 
131
  length=uint2korr(head+6); count=uint2korr(head+8);
 
132
 
 
133
  if (count < error_messages)
 
134
  {
 
135
    sql_print_error("\
 
136
Error message file '%s' had only %d error messages,\n\
 
137
but it should contain at least %d error messages.\n\
 
138
Check that the above file is the right version for this program!",
 
139
                    name,count,error_messages);
 
140
    VOID(my_close(file,MYF(MY_WME)));
 
141
    DBUG_RETURN(1);
 
142
  }
 
143
 
 
144
  x_free((uchar*) *point);              /* Free old language */
 
145
  if (!(*point= (const char**)
 
146
        my_malloc((size_t) (length+count*sizeof(char*)),MYF(0))))
 
147
  {
 
148
    funktpos=2;                                 /* purecov: inspected */
 
149
    goto err;                                   /* purecov: inspected */
 
150
  }
 
151
  buff= (uchar*) (*point + count);
 
152
 
 
153
  if (my_read(file, buff, (size_t) count*2,MYF(MY_NABP)))
 
154
    goto err;
 
155
  for (i=0, pos= buff ; i< count ; i++)
 
156
  {
 
157
    (*point)[i]= (char*) buff+uint2korr(pos);
 
158
    pos+=2;
 
159
  }
 
160
  if (my_read(file, buff, length, MYF(MY_NABP)))
 
161
    goto err;
 
162
 
 
163
  for (i=1 ; i < textcount ; i++)
 
164
  {
 
165
    point[i]= *point +uint2korr(head+10+i+i);
 
166
  }
 
167
  VOID(my_close(file,MYF(0)));
 
168
  DBUG_RETURN(0);
 
169
 
 
170
err:
 
171
  switch (funktpos) {
 
172
  case 2:
 
173
    errmsg= "Not enough memory for messagefile '%s'";
 
174
    break;
 
175
  case 1:
 
176
    errmsg= "Can't read from messagefile '%s'";
 
177
    break;
 
178
  default:
 
179
    errmsg= "Can't find messagefile '%s'";
 
180
    break;
 
181
  }
 
182
  sql_print_error(errmsg, name);
 
183
err1:
 
184
  if (file != FERR)
 
185
    VOID(my_close(file,MYF(MY_WME)));
 
186
  DBUG_RETURN(1);
 
187
} /* read_texts */
 
188
 
 
189
 
 
190
/**
 
191
  Initiates error-messages used by my_func-library.
 
192
*/
 
193
 
 
194
static void init_myfunc_errs()
 
195
{
 
196
  init_glob_errs();                     /* Initiate english errors */
 
197
  if (!(specialflag & SPECIAL_ENGLISH))
 
198
  {
 
199
    EE(EE_FILENOTFOUND)   = ER(ER_FILE_NOT_FOUND);
 
200
    EE(EE_CANTCREATEFILE) = ER(ER_CANT_CREATE_FILE);
 
201
    EE(EE_READ)           = ER(ER_ERROR_ON_READ);
 
202
    EE(EE_WRITE)          = ER(ER_ERROR_ON_WRITE);
 
203
    EE(EE_BADCLOSE)       = ER(ER_ERROR_ON_CLOSE);
 
204
    EE(EE_OUTOFMEMORY)    = ER(ER_OUTOFMEMORY);
 
205
    EE(EE_DELETE)         = ER(ER_CANT_DELETE_FILE);
 
206
    EE(EE_LINK)           = ER(ER_ERROR_ON_RENAME);
 
207
    EE(EE_EOFERR)         = ER(ER_UNEXPECTED_EOF);
 
208
    EE(EE_CANTLOCK)       = ER(ER_CANT_LOCK);
 
209
    EE(EE_DIR)            = ER(ER_CANT_READ_DIR);
 
210
    EE(EE_STAT)           = ER(ER_CANT_GET_STAT);
 
211
    EE(EE_GETWD)          = ER(ER_CANT_GET_WD);
 
212
    EE(EE_SETWD)          = ER(ER_CANT_SET_WD);
 
213
    EE(EE_DISK_FULL)      = ER(ER_DISK_FULL);
 
214
  }
 
215
}