]> granicus.if.org Git - postgresql/blob - src/port/chklocale.c
Add "SHIFT_JIS" as an accepted encoding name for locale checking.
[postgresql] / src / port / chklocale.c
1 /*-------------------------------------------------------------------------
2  *
3  * chklocale.c
4  *              Functions for handling locale-related info
5  *
6  *
7  * Copyright (c) 1996-2013, PostgreSQL Global Development Group
8  *
9  *
10  * IDENTIFICATION
11  *        src/port/chklocale.c
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 #ifndef FRONTEND
17 #include "postgres.h"
18 #else
19 #include "postgres_fe.h"
20 #endif
21
22 #include <locale.h>
23 #ifdef HAVE_LANGINFO_H
24 #include <langinfo.h>
25 #endif
26
27 #include "mb/pg_wchar.h"
28
29
30 /*
31  * This table needs to recognize all the CODESET spellings for supported
32  * backend encodings, as well as frontend-only encodings where possible
33  * (the latter case is currently only needed for initdb to recognize
34  * error situations).  On Windows, we rely on entries for codepage
35  * numbers (CPnnn).
36  *
37  * Note that we search the table with pg_strcasecmp(), so variant
38  * capitalizations don't need their own entries.
39  */
40 struct encoding_match
41 {
42         enum pg_enc pg_enc_code;
43         const char *system_enc_name;
44 };
45
46 static const struct encoding_match encoding_match_list[] = {
47         {PG_EUC_JP, "EUC-JP"},
48         {PG_EUC_JP, "eucJP"},
49         {PG_EUC_JP, "IBM-eucJP"},
50         {PG_EUC_JP, "sdeckanji"},
51         {PG_EUC_JP, "CP20932"},
52
53         {PG_EUC_CN, "EUC-CN"},
54         {PG_EUC_CN, "eucCN"},
55         {PG_EUC_CN, "IBM-eucCN"},
56         {PG_EUC_CN, "GB2312"},
57         {PG_EUC_CN, "dechanzi"},
58         {PG_EUC_CN, "CP20936"},
59
60         {PG_EUC_KR, "EUC-KR"},
61         {PG_EUC_KR, "eucKR"},
62         {PG_EUC_KR, "IBM-eucKR"},
63         {PG_EUC_KR, "deckorean"},
64         {PG_EUC_KR, "5601"},
65         {PG_EUC_KR, "CP51949"},
66
67         {PG_EUC_TW, "EUC-TW"},
68         {PG_EUC_TW, "eucTW"},
69         {PG_EUC_TW, "IBM-eucTW"},
70         {PG_EUC_TW, "cns11643"},
71         /* No codepage for EUC-TW ? */
72
73         {PG_UTF8, "UTF-8"},
74         {PG_UTF8, "utf8"},
75         {PG_UTF8, "CP65001"},
76
77         {PG_LATIN1, "ISO-8859-1"},
78         {PG_LATIN1, "ISO8859-1"},
79         {PG_LATIN1, "iso88591"},
80         {PG_LATIN1, "CP28591"},
81
82         {PG_LATIN2, "ISO-8859-2"},
83         {PG_LATIN2, "ISO8859-2"},
84         {PG_LATIN2, "iso88592"},
85         {PG_LATIN2, "CP28592"},
86
87         {PG_LATIN3, "ISO-8859-3"},
88         {PG_LATIN3, "ISO8859-3"},
89         {PG_LATIN3, "iso88593"},
90         {PG_LATIN3, "CP28593"},
91
92         {PG_LATIN4, "ISO-8859-4"},
93         {PG_LATIN4, "ISO8859-4"},
94         {PG_LATIN4, "iso88594"},
95         {PG_LATIN4, "CP28594"},
96
97         {PG_LATIN5, "ISO-8859-9"},
98         {PG_LATIN5, "ISO8859-9"},
99         {PG_LATIN5, "iso88599"},
100         {PG_LATIN5, "CP28599"},
101
102         {PG_LATIN6, "ISO-8859-10"},
103         {PG_LATIN6, "ISO8859-10"},
104         {PG_LATIN6, "iso885910"},
105
106         {PG_LATIN7, "ISO-8859-13"},
107         {PG_LATIN7, "ISO8859-13"},
108         {PG_LATIN7, "iso885913"},
109
110         {PG_LATIN8, "ISO-8859-14"},
111         {PG_LATIN8, "ISO8859-14"},
112         {PG_LATIN8, "iso885914"},
113
114         {PG_LATIN9, "ISO-8859-15"},
115         {PG_LATIN9, "ISO8859-15"},
116         {PG_LATIN9, "iso885915"},
117         {PG_LATIN9, "CP28605"},
118
119         {PG_LATIN10, "ISO-8859-16"},
120         {PG_LATIN10, "ISO8859-16"},
121         {PG_LATIN10, "iso885916"},
122
123         {PG_KOI8R, "KOI8-R"},
124         {PG_KOI8R, "CP20866"},
125
126         {PG_KOI8U, "KOI8-U"},
127         {PG_KOI8U, "CP21866"},
128
129         {PG_WIN866, "CP866"},
130         {PG_WIN874, "CP874"},
131         {PG_WIN1250, "CP1250"},
132         {PG_WIN1251, "CP1251"},
133         {PG_WIN1251, "ansi-1251"},
134         {PG_WIN1252, "CP1252"},
135         {PG_WIN1253, "CP1253"},
136         {PG_WIN1254, "CP1254"},
137         {PG_WIN1255, "CP1255"},
138         {PG_WIN1256, "CP1256"},
139         {PG_WIN1257, "CP1257"},
140         {PG_WIN1258, "CP1258"},
141
142         {PG_ISO_8859_5, "ISO-8859-5"},
143         {PG_ISO_8859_5, "ISO8859-5"},
144         {PG_ISO_8859_5, "iso88595"},
145         {PG_ISO_8859_5, "CP28595"},
146
147         {PG_ISO_8859_6, "ISO-8859-6"},
148         {PG_ISO_8859_6, "ISO8859-6"},
149         {PG_ISO_8859_6, "iso88596"},
150         {PG_ISO_8859_6, "CP28596"},
151
152         {PG_ISO_8859_7, "ISO-8859-7"},
153         {PG_ISO_8859_7, "ISO8859-7"},
154         {PG_ISO_8859_7, "iso88597"},
155         {PG_ISO_8859_7, "CP28597"},
156
157         {PG_ISO_8859_8, "ISO-8859-8"},
158         {PG_ISO_8859_8, "ISO8859-8"},
159         {PG_ISO_8859_8, "iso88598"},
160         {PG_ISO_8859_8, "CP28598"},
161
162         {PG_SJIS, "SJIS"},
163         {PG_SJIS, "PCK"},
164         {PG_SJIS, "CP932"},
165         {PG_SJIS, "SHIFT_JIS"},
166
167         {PG_BIG5, "BIG5"},
168         {PG_BIG5, "BIG5HKSCS"},
169         {PG_BIG5, "Big5-HKSCS"},
170         {PG_BIG5, "CP950"},
171
172         {PG_GBK, "GBK"},
173         {PG_GBK, "CP936"},
174
175         {PG_UHC, "UHC"},
176         {PG_UHC, "CP949"},
177
178         {PG_JOHAB, "JOHAB"},
179         {PG_JOHAB, "CP1361"},
180
181         {PG_GB18030, "GB18030"},
182         {PG_GB18030, "CP54936"},
183
184         {PG_SHIFT_JIS_2004, "SJIS_2004"},
185
186         {PG_SQL_ASCII, "US-ASCII"},
187
188         {PG_SQL_ASCII, NULL}            /* end marker */
189 };
190
191 #ifdef WIN32
192 /*
193  * On Windows, use CP<code page number> instead of the nl_langinfo() result
194  *
195  * Visual Studio 2012 expanded the set of valid LC_CTYPE values, so have its
196  * locale machinery determine the code page.  See comments at IsoLocaleName().
197  * For other compilers, follow the locale's predictable format.
198  *
199  * Returns a malloc()'d string for the caller to free.
200  */
201 static char *
202 win32_langinfo(const char *ctype)
203 {
204         char       *r = NULL;
205
206 #if (_MSC_VER >= 1700)
207         _locale_t       loct = NULL;
208
209         loct = _create_locale(LC_CTYPE, ctype);
210         if (loct != NULL)
211         {
212                 r = malloc(16);                 /* excess */
213                 if (r != NULL)
214                         sprintf(r, "CP%u", loct->locinfo->lc_codepage);
215                 _free_locale(loct);
216         }
217 #else
218         char       *codepage;
219
220         /*
221          * Locale format on Win32 is <Language>_<Country>.<CodePage> . For
222          * example, English_United States.1252.
223          */
224         codepage = strrchr(ctype, '.');
225         if (codepage != NULL)
226         {
227                 int                     ln;
228
229                 codepage++;
230                 ln = strlen(codepage);
231                 r = malloc(ln + 3);
232                 if (r != NULL)
233                         sprintf(r, "CP%s", codepage);
234         }
235 #endif
236
237         return r;
238 }
239
240 #ifndef FRONTEND
241 /*
242  * Given a Windows code page identifier, find the corresponding PostgreSQL
243  * encoding.  Issue a warning and return -1 if none found.
244  */
245 int
246 pg_codepage_to_encoding(UINT cp)
247 {
248         char            sys[16];
249         int                     i;
250
251         sprintf(sys, "CP%u", cp);
252
253         /* Check the table */
254         for (i = 0; encoding_match_list[i].system_enc_name; i++)
255                 if (pg_strcasecmp(sys, encoding_match_list[i].system_enc_name) == 0)
256                         return encoding_match_list[i].pg_enc_code;
257
258         ereport(WARNING,
259                         (errmsg("could not determine encoding for codeset \"%s\"", sys),
260                    errdetail("Please report this to <pgsql-bugs@postgresql.org>.")));
261
262         return -1;
263 }
264 #endif
265 #endif   /* WIN32 */
266
267 #if (defined(HAVE_LANGINFO_H) && defined(CODESET)) || defined(WIN32)
268
269 /*
270  * Given a setting for LC_CTYPE, return the Postgres ID of the associated
271  * encoding, if we can determine it.  Return -1 if we can't determine it.
272  *
273  * Pass in NULL to get the encoding for the current locale setting.
274  * Pass "" to get the encoding selected by the server's environment.
275  *
276  * If the result is PG_SQL_ASCII, callers should treat it as being compatible
277  * with any desired encoding.
278  *
279  * If running in the backend and write_message is false, this function must
280  * cope with the possibility that elog() and palloc() are not yet usable.
281  */
282 int
283 pg_get_encoding_from_locale(const char *ctype, bool write_message)
284 {
285         char       *sys;
286         int                     i;
287
288         /* Get the CODESET property, and also LC_CTYPE if not passed in */
289         if (ctype)
290         {
291                 char       *save;
292                 char       *name;
293
294                 /* If locale is C or POSIX, we can allow all encodings */
295                 if (pg_strcasecmp(ctype, "C") == 0 ||
296                         pg_strcasecmp(ctype, "POSIX") == 0)
297                         return PG_SQL_ASCII;
298
299                 save = setlocale(LC_CTYPE, NULL);
300                 if (!save)
301                         return -1;                      /* setlocale() broken? */
302                 /* must copy result, or it might change after setlocale */
303                 save = strdup(save);
304                 if (!save)
305                         return -1;                      /* out of memory; unlikely */
306
307                 name = setlocale(LC_CTYPE, ctype);
308                 if (!name)
309                 {
310                         free(save);
311                         return -1;                      /* bogus ctype passed in? */
312                 }
313
314 #ifndef WIN32
315                 sys = nl_langinfo(CODESET);
316                 if (sys)
317                         sys = strdup(sys);
318 #else
319                 sys = win32_langinfo(name);
320 #endif
321
322                 setlocale(LC_CTYPE, save);
323                 free(save);
324         }
325         else
326         {
327                 /* much easier... */
328                 ctype = setlocale(LC_CTYPE, NULL);
329                 if (!ctype)
330                         return -1;                      /* setlocale() broken? */
331
332                 /* If locale is C or POSIX, we can allow all encodings */
333                 if (pg_strcasecmp(ctype, "C") == 0 ||
334                         pg_strcasecmp(ctype, "POSIX") == 0)
335                         return PG_SQL_ASCII;
336
337 #ifndef WIN32
338                 sys = nl_langinfo(CODESET);
339                 if (sys)
340                         sys = strdup(sys);
341 #else
342                 sys = win32_langinfo(ctype);
343 #endif
344         }
345
346         if (!sys)
347                 return -1;                              /* out of memory; unlikely */
348
349         /* Check the table */
350         for (i = 0; encoding_match_list[i].system_enc_name; i++)
351         {
352                 if (pg_strcasecmp(sys, encoding_match_list[i].system_enc_name) == 0)
353                 {
354                         free(sys);
355                         return encoding_match_list[i].pg_enc_code;
356                 }
357         }
358
359         /* Special-case kluges for particular platforms go here */
360
361 #ifdef __darwin__
362
363         /*
364          * Current OS X has many locales that report an empty string for CODESET,
365          * but they all seem to actually use UTF-8.
366          */
367         if (strlen(sys) == 0)
368         {
369                 free(sys);
370                 return PG_UTF8;
371         }
372 #endif
373
374         /*
375          * We print a warning if we got a CODESET string but couldn't recognize
376          * it.  This means we need another entry in the table.
377          */
378         if (write_message)
379         {
380 #ifdef FRONTEND
381                 fprintf(stderr, _("could not determine encoding for locale \"%s\": codeset is \"%s\""),
382                                 ctype, sys);
383                 /* keep newline separate so there's only one translatable string */
384                 fputc('\n', stderr);
385 #else
386                 ereport(WARNING,
387                                 (errmsg("could not determine encoding for locale \"%s\": codeset is \"%s\"",
388                                                 ctype, sys),
389                    errdetail("Please report this to <pgsql-bugs@postgresql.org>.")));
390 #endif
391         }
392
393         free(sys);
394         return -1;
395 }
396 #else                                                   /* (HAVE_LANGINFO_H && CODESET) || WIN32 */
397
398 /*
399  * stub if no multi-language platform support
400  *
401  * Note: we could return -1 here, but that would have the effect of
402  * forcing users to specify an encoding to initdb on such platforms.
403  * It seems better to silently default to SQL_ASCII.
404  */
405 int
406 pg_get_encoding_from_locale(const char *ctype, bool write_message)
407 {
408         return PG_SQL_ASCII;
409 }
410
411 #endif   /* (HAVE_LANGINFO_H && CODESET) || WIN32 */