Remove the encoding *numbers* from the comments. They are useless, and
[PostgreSQL.git] / src / port / chklocale.c
blobc30b6e91d408a0221492f4a0b4f772b9c9239a11
1 /*-------------------------------------------------------------------------
3 * chklocale.c
4 * Functions for handling locale-related info
7 * Copyright (c) 1996-2009, PostgreSQL Global Development Group
10 * IDENTIFICATION
11 * $PostgreSQL$
13 *-------------------------------------------------------------------------
16 #ifndef FRONTEND
17 #include "postgres.h"
18 #else
19 #include "postgres_fe.h"
20 #endif
22 #include <locale.h>
23 #ifdef HAVE_LANGINFO_H
24 #include <langinfo.h>
25 #endif
27 #include "mb/pg_wchar.h"
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).
37 * Note that we search the table with pg_strcasecmp(), so variant
38 * capitalizations don't need their own entries.
40 struct encoding_match
42 enum pg_enc pg_enc_code;
43 const char *system_enc_name;
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"},
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"},
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 ? */
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 ? */
73 {PG_UTF8, "UTF-8"},
74 {PG_UTF8, "utf8"},
75 {PG_UTF8, "CP65001"},
77 {PG_LATIN1, "ISO-8859-1"},
78 {PG_LATIN1, "ISO8859-1"},
79 {PG_LATIN1, "iso88591"},
80 {PG_LATIN1, "CP28591"},
82 {PG_LATIN2, "ISO-8859-2"},
83 {PG_LATIN2, "ISO8859-2"},
84 {PG_LATIN2, "iso88592"},
85 {PG_LATIN2, "CP28592"},
87 {PG_LATIN3, "ISO-8859-3"},
88 {PG_LATIN3, "ISO8859-3"},
89 {PG_LATIN3, "iso88593"},
90 {PG_LATIN3, "CP28593"},
92 {PG_LATIN4, "ISO-8859-4"},
93 {PG_LATIN4, "ISO8859-4"},
94 {PG_LATIN4, "iso88594"},
95 {PG_LATIN4, "CP28594"},
97 {PG_LATIN5, "ISO-8859-9"},
98 {PG_LATIN5, "ISO8859-9"},
99 {PG_LATIN5, "iso88599"},
100 {PG_LATIN5, "CP28599"},
102 {PG_LATIN6, "ISO-8859-10"},
103 {PG_LATIN6, "ISO8859-10"},
104 {PG_LATIN6, "iso885910"},
106 {PG_LATIN7, "ISO-8859-13"},
107 {PG_LATIN7, "ISO8859-13"},
108 {PG_LATIN7, "iso885913"},
110 {PG_LATIN8, "ISO-8859-14"},
111 {PG_LATIN8, "ISO8859-14"},
112 {PG_LATIN8, "iso885914"},
114 {PG_LATIN9, "ISO-8859-15"},
115 {PG_LATIN9, "ISO8859-15"},
116 {PG_LATIN9, "iso885915"},
117 {PG_LATIN9, "CP28605"},
119 {PG_LATIN10, "ISO-8859-16"},
120 {PG_LATIN10, "ISO8859-16"},
121 {PG_LATIN10, "iso885916"},
123 {PG_KOI8R, "KOI8-R"},
124 {PG_KOI8R, "CP20866"},
126 {PG_WIN866, "CP866"},
127 {PG_WIN874, "CP874"},
128 {PG_WIN1250, "CP1250"},
129 {PG_WIN1251, "CP1251"},
130 {PG_WIN1251, "ansi-1251"},
131 {PG_WIN1252, "CP1252"},
132 {PG_WIN1253, "CP1253"},
133 {PG_WIN1254, "CP1254"},
134 {PG_WIN1255, "CP1255"},
135 {PG_WIN1256, "CP1256"},
136 {PG_WIN1257, "CP1257"},
137 {PG_WIN1258, "CP1258"},
139 {PG_ISO_8859_5, "ISO-8859-5"},
140 {PG_ISO_8859_5, "ISO8859-5"},
141 {PG_ISO_8859_5, "iso88595"},
142 {PG_ISO_8859_5, "CP28595"},
144 {PG_ISO_8859_6, "ISO-8859-6"},
145 {PG_ISO_8859_6, "ISO8859-6"},
146 {PG_ISO_8859_6, "iso88596"},
147 {PG_ISO_8859_6, "CP28596"},
149 {PG_ISO_8859_7, "ISO-8859-7"},
150 {PG_ISO_8859_7, "ISO8859-7"},
151 {PG_ISO_8859_7, "iso88597"},
152 {PG_ISO_8859_7, "CP28597"},
154 {PG_ISO_8859_8, "ISO-8859-8"},
155 {PG_ISO_8859_8, "ISO8859-8"},
156 {PG_ISO_8859_8, "iso88598"},
157 {PG_ISO_8859_8, "CP28598"},
159 {PG_SJIS, "SJIS"},
160 {PG_SJIS, "PCK"},
161 {PG_SJIS, "CP932"},
163 {PG_BIG5, "BIG5"},
164 {PG_BIG5, "BIG5HKSCS"},
165 {PG_BIG5, "Big5-HKSCS"},
166 {PG_BIG5, "CP950"},
168 {PG_GBK, "GBK"},
169 {PG_GBK, "CP936"},
171 {PG_UHC, "UHC"},
173 {PG_JOHAB, "JOHAB"},
174 {PG_JOHAB, "CP1361"},
176 {PG_GB18030, "GB18030"},
177 {PG_GB18030, "CP54936"},
179 {PG_SHIFT_JIS_2004, "SJIS_2004"},
181 {PG_SQL_ASCII, NULL} /* end marker */
184 #ifdef WIN32
186 * On Windows, use CP<codepage number> instead of the nl_langinfo() result
188 static char *
189 win32_langinfo(const char *ctype)
191 char *r;
192 char *codepage;
193 int ln;
196 * Locale format on Win32 is <Language>_<Country>.<CodePage> . For
197 * example, English_USA.1252.
199 codepage = strrchr(ctype, '.');
200 if (!codepage)
201 return NULL;
202 codepage++;
203 ln = strlen(codepage);
204 r = malloc(ln + 3);
205 sprintf(r, "CP%s", codepage);
207 return r;
209 #endif /* WIN32 */
211 #if (defined(HAVE_LANGINFO_H) && defined(CODESET)) || defined(WIN32)
214 * Given a setting for LC_CTYPE, return the Postgres ID of the associated
215 * encoding, if we can determine it.
217 * Pass in NULL to get the encoding for the current locale setting.
219 * If the result is PG_SQL_ASCII, callers should treat it as being compatible
220 * with any desired encoding. We return this if the locale is C/POSIX or we
221 * can't determine the encoding.
224 pg_get_encoding_from_locale(const char *ctype)
226 char *sys;
227 int i;
229 /* Get the CODESET property, and also LC_CTYPE if not passed in */
230 if (ctype)
232 char *save;
233 char *name;
235 save = setlocale(LC_CTYPE, NULL);
236 if (!save)
237 return PG_SQL_ASCII; /* setlocale() broken? */
238 /* must copy result, or it might change after setlocale */
239 save = strdup(save);
240 if (!save)
241 return PG_SQL_ASCII; /* out of memory; unlikely */
243 name = setlocale(LC_CTYPE, ctype);
244 if (!name)
246 free(save);
247 return PG_SQL_ASCII; /* bogus ctype passed in? */
250 #ifndef WIN32
251 sys = nl_langinfo(CODESET);
252 if (sys)
253 sys = strdup(sys);
254 #else
255 sys = win32_langinfo(name);
256 #endif
258 setlocale(LC_CTYPE, save);
259 free(save);
261 else
263 /* much easier... */
264 ctype = setlocale(LC_CTYPE, NULL);
265 if (!ctype)
266 return PG_SQL_ASCII; /* setlocale() broken? */
267 #ifndef WIN32
268 sys = nl_langinfo(CODESET);
269 if (sys)
270 sys = strdup(sys);
271 #else
272 sys = win32_langinfo(ctype);
273 #endif
276 if (!sys)
277 return PG_SQL_ASCII; /* out of memory; unlikely */
279 /* If locale is C or POSIX, we can allow all encodings */
280 if (pg_strcasecmp(ctype, "C") == 0 || pg_strcasecmp(ctype, "POSIX") == 0)
282 free(sys);
283 return PG_SQL_ASCII;
286 /* Check the table */
287 for (i = 0; encoding_match_list[i].system_enc_name; i++)
289 if (pg_strcasecmp(sys, encoding_match_list[i].system_enc_name) == 0)
291 free(sys);
292 return encoding_match_list[i].pg_enc_code;
296 /* Special-case kluges for particular platforms go here */
298 #ifdef __darwin__
301 * Current OS X has many locales that report an empty string for CODESET,
302 * but they all seem to actually use UTF-8.
304 if (strlen(sys) == 0)
306 free(sys);
307 return PG_UTF8;
309 #endif
312 * We print a warning if we got a CODESET string but couldn't recognize
313 * it. This means we need another entry in the table.
315 #ifdef FRONTEND
316 fprintf(stderr, _("could not determine encoding for locale \"%s\": codeset is \"%s\""),
317 ctype, sys);
318 /* keep newline separate so there's only one translatable string */
319 fputc('\n', stderr);
320 #else
321 ereport(WARNING,
322 (errmsg("could not determine encoding for locale \"%s\": codeset is \"%s\"",
323 ctype, sys),
324 errdetail("Please report this to <pgsql-bugs@postgresql.org>.")));
325 #endif
327 free(sys);
328 return PG_SQL_ASCII;
330 #else /* (HAVE_LANGINFO_H && CODESET) || WIN32 */
333 * stub if no platform support
336 pg_get_encoding_from_locale(const char *ctype)
338 return PG_SQL_ASCII;
341 #endif /* (HAVE_LANGINFO_H && CODESET) || WIN32 */