]> granicus.if.org Git - postgresql/blob - src/port/chklocale.c
Teach chklocale.c how to extract encoding info from Windows locale
[postgresql] / src / port / chklocale.c
1 /*-------------------------------------------------------------------------
2  *
3  * chklocale.c
4  *              Functions for handling locale-related info
5  *
6  *
7  * Copyright (c) 1996-2007, PostgreSQL Global Development Group
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/port/chklocale.c,v 1.4 2007/10/03 17:16:39 tgl Exp $
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"}, /* or 20949 ? */
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
76         {PG_LATIN1, "ISO-8859-1"},
77         {PG_LATIN1, "ISO8859-1"},
78         {PG_LATIN1, "iso88591"},
79
80         {PG_LATIN2, "ISO-8859-2"},
81         {PG_LATIN2, "ISO8859-2"},
82         {PG_LATIN2, "iso88592"},
83
84         {PG_LATIN3, "ISO-8859-3"},
85         {PG_LATIN3, "ISO8859-3"},
86         {PG_LATIN3, "iso88593"},
87
88         {PG_LATIN4, "ISO-8859-4"},
89         {PG_LATIN4, "ISO8859-4"},
90         {PG_LATIN4, "iso88594"},
91
92         {PG_LATIN5, "ISO-8859-9"},
93         {PG_LATIN5, "ISO8859-9"},
94         {PG_LATIN5, "iso88599"},
95
96         {PG_LATIN6, "ISO-8859-10"},
97         {PG_LATIN6, "ISO8859-10"},
98         {PG_LATIN6, "iso885910"},
99
100         {PG_LATIN7, "ISO-8859-13"},
101         {PG_LATIN7, "ISO8859-13"},
102         {PG_LATIN7, "iso885913"},
103
104         {PG_LATIN8, "ISO-8859-14"},
105         {PG_LATIN8, "ISO8859-14"},
106         {PG_LATIN8, "iso885914"},
107
108         {PG_LATIN9, "ISO-8859-15"},
109         {PG_LATIN9, "ISO8859-15"},
110         {PG_LATIN9, "iso885915"},
111
112         {PG_LATIN10, "ISO-8859-16"},
113         {PG_LATIN10, "ISO8859-16"},
114         {PG_LATIN10, "iso885916"},
115
116         {PG_KOI8R, "KOI8-R"},
117         {PG_KOI8R, "CP20866"},
118
119         {PG_WIN1252, "CP1252"},
120         {PG_WIN1253, "CP1253"},
121         {PG_WIN1254, "CP1254"},
122         {PG_WIN1255, "CP1255"},
123         {PG_WIN1256, "CP1256"},
124         {PG_WIN1257, "CP1257"},
125         {PG_WIN1258, "CP1258"},
126 #ifdef NOT_VERIFIED
127         {PG_WIN874, "???"},
128 #endif
129         {PG_WIN1251, "CP1251"},
130         {PG_WIN866, "CP866"},
131
132         {PG_ISO_8859_5, "ISO-8859-5"},
133         {PG_ISO_8859_5, "ISO8859-5"},
134         {PG_ISO_8859_5, "iso88595"},
135
136         {PG_ISO_8859_6, "ISO-8859-6"},
137         {PG_ISO_8859_6, "ISO8859-6"},
138         {PG_ISO_8859_6, "iso88596"},
139
140         {PG_ISO_8859_7, "ISO-8859-7"},
141         {PG_ISO_8859_7, "ISO8859-7"},
142         {PG_ISO_8859_7, "iso88597"},
143
144         {PG_ISO_8859_8, "ISO-8859-8"},
145         {PG_ISO_8859_8, "ISO8859-8"},
146         {PG_ISO_8859_8, "iso88598"},
147
148         {PG_SJIS, "SJIS"},
149         {PG_SJIS, "PCK"},
150         {PG_SJIS, "CP932"},
151
152         {PG_BIG5, "BIG5"},
153         {PG_BIG5, "BIG5HKSCS"},
154         {PG_BIG5, "CP950"},
155
156         {PG_GBK, "GBK"},
157         {PG_GBK, "CP936"},
158
159         {PG_UHC, "UHC"},
160
161         {PG_JOHAB, "JOHAB"},
162         {PG_JOHAB, "CP1361"},
163
164         {PG_GB18030, "GB18030"},
165         {PG_GB18030, "CP54936"},
166
167         {PG_SHIFT_JIS_2004, "SJIS_2004"},
168
169         {PG_SQL_ASCII, NULL}            /* end marker */
170 };
171
172 #ifdef WIN32
173 /*
174  * On Windows, use CP<codepage number> instead of the nl_langinfo() result
175  */
176 static char *
177 win32_langinfo(const char *ctype)
178 {
179         char       *r;
180         char       *codepage;
181         int                     ln;
182
183         /*
184          * Locale format on Win32 is <Language>_<Country>.<CodePage> .
185          * For example, English_USA.1252.
186          */
187         codepage = strrchr(ctype, '.');
188         if (!codepage)
189                 return NULL;
190         codepage++;
191         ln = strlen(codepage);
192         r = malloc(ln + 3);
193         sprintf(r, "CP%s", codepage);
194
195         return r;
196 }
197 #endif /* WIN32 */
198
199 #if (defined(HAVE_LANGINFO_H) && defined(CODESET)) || defined(WIN32)
200
201 /*
202  * Given a setting for LC_CTYPE, return the Postgres ID of the associated
203  * encoding, if we can determine it.
204  *
205  * Pass in NULL to get the encoding for the current locale setting.
206  *
207  * If the result is PG_SQL_ASCII, callers should treat it as being compatible
208  * with any desired encoding.  We return this if the locale is C/POSIX or we
209  * can't determine the encoding.
210  */
211 int
212 pg_get_encoding_from_locale(const char *ctype)
213 {
214         char       *sys;
215         int                     i;
216
217         /* Get the CODESET property, and also LC_CTYPE if not passed in */
218         if (ctype)
219         {
220                 char       *save;
221                 char       *name;
222
223                 save = setlocale(LC_CTYPE, NULL);
224                 if (!save)
225                         return PG_SQL_ASCII;            /* setlocale() broken? */
226                 /* must copy result, or it might change after setlocale */
227                 save = strdup(save);
228                 if (!save)
229                         return PG_SQL_ASCII;            /* out of memory; unlikely */
230
231                 name = setlocale(LC_CTYPE, ctype);
232                 if (!name)
233                 {
234                         free(save);
235                         return PG_SQL_ASCII;            /* bogus ctype passed in? */
236                 }
237
238 #ifndef WIN32
239                 sys = nl_langinfo(CODESET);
240                 if (sys)
241                         sys = strdup(sys);
242 #else
243                 sys = win32_langinfo(name);
244 #endif
245
246                 setlocale(LC_CTYPE, save);
247                 free(save);
248         }
249         else
250         {
251                 /* much easier... */
252                 ctype = setlocale(LC_CTYPE, NULL);
253                 if (!ctype)
254                         return PG_SQL_ASCII;            /* setlocale() broken? */
255 #ifndef WIN32
256                 sys = nl_langinfo(CODESET);
257                 if (sys)
258                         sys = strdup(sys);
259 #else
260                 sys = win32_langinfo(ctype);
261 #endif
262         }
263
264         if (!sys)
265                 return PG_SQL_ASCII;            /* out of memory; unlikely */
266
267         /* If locale is C or POSIX, we can allow all encodings */
268         if (pg_strcasecmp(ctype, "C") == 0 || pg_strcasecmp(ctype, "POSIX") == 0)
269         {
270                 free(sys);
271                 return PG_SQL_ASCII;
272         }
273
274         /* Check the table */
275         for (i = 0; encoding_match_list[i].system_enc_name; i++)
276         {
277                 if (pg_strcasecmp(sys, encoding_match_list[i].system_enc_name) == 0)
278                 {
279                         free(sys);
280                         return encoding_match_list[i].pg_enc_code;
281                 }
282         }
283
284         /* Special-case kluges for particular platforms go here */
285
286 #ifdef __darwin__
287         /*
288          * Current OS X has many locales that report an empty string for CODESET,
289          * but they all seem to actually use UTF-8.
290          */
291         if (strlen(sys) == 0)
292         {
293                 free(sys);
294                 return PG_UTF8;
295         }
296 #endif
297
298         /*
299          * We print a warning if we got a CODESET string but couldn't recognize
300          * it.  This means we need another entry in the table.
301          */
302 #ifdef FRONTEND
303         fprintf(stderr, _("could not determine encoding for locale \"%s\": codeset is \"%s\""),
304                         ctype, sys);
305         /* keep newline separate so there's only one translatable string */
306         fputc('\n', stderr);
307 #else
308         ereport(WARNING,
309                         (errmsg("could not determine encoding for locale \"%s\": codeset is \"%s\"",
310                                         ctype, sys),
311                          errdetail("Please report this to <pgsql-bugs@postgresql.org>.")));
312 #endif
313
314         free(sys);
315         return PG_SQL_ASCII;
316 }
317
318 #else /* (HAVE_LANGINFO_H && CODESET) || WIN32 */
319
320 /*
321  * stub if no platform support
322  */
323 int
324 pg_get_encoding_from_locale(const char *ctype)
325 {
326         return PG_SQL_ASCII;
327 }
328
329 #endif /* (HAVE_LANGINFO_H && CODESET) || WIN32 */