1 /*-------------------------------------------------------------------------
4 * Functions for handling locale-related info
7 * Copyright (c) 1996-2009, PostgreSQL Global Development Group
11 * $PostgreSQL: pgsql/src/port/chklocale.c,v 1.12 2009/11/12 02:46:16 tgl Exp $
13 *-------------------------------------------------------------------------
19 #include "postgres_fe.h"
23 #ifdef HAVE_LANGINFO_H
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
37 * Note that we search the table with pg_strcasecmp(), so variant
38 * capitalizations don't need their own entries.
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"},
49 {PG_EUC_JP, "IBM-eucJP"},
50 {PG_EUC_JP, "sdeckanji"},
51 {PG_EUC_JP, "CP20932"},
53 {PG_EUC_CN, "EUC-CN"},
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"},
62 {PG_EUC_KR, "IBM-eucKR"},
63 {PG_EUC_KR, "deckorean"},
65 {PG_EUC_KR, "CP51949"}, /* or 20949 ? */
67 {PG_EUC_TW, "EUC-TW"},
69 {PG_EUC_TW, "IBM-eucTW"},
70 {PG_EUC_TW, "cns11643"},
71 /* No codepage for EUC-TW ? */
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_KOI8U, "KOI8-U"},
127 {PG_KOI8U, "CP21866"},
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"},
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"},
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"},
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"},
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"},
167 {PG_BIG5, "BIG5HKSCS"},
168 {PG_BIG5, "Big5-HKSCS"},
177 {PG_JOHAB, "CP1361"},
179 {PG_GB18030, "GB18030"},
180 {PG_GB18030, "CP54936"},
182 {PG_SHIFT_JIS_2004, "SJIS_2004"},
184 {PG_SQL_ASCII, "US-ASCII"},
186 {PG_SQL_ASCII, NULL} /* end marker */
191 * On Windows, use CP<codepage number> instead of the nl_langinfo() result
194 win32_langinfo(const char *ctype)
201 * Locale format on Win32 is <Language>_<Country>.<CodePage> . For
202 * example, English_USA.1252.
204 codepage = strrchr(ctype, '.');
208 ln = strlen(codepage);
210 sprintf(r, "CP%s", codepage);
216 #if (defined(HAVE_LANGINFO_H) && defined(CODESET)) || defined(WIN32)
219 * Given a setting for LC_CTYPE, return the Postgres ID of the associated
220 * encoding, if we can determine it. Return -1 if we can't determine it.
222 * Pass in NULL to get the encoding for the current locale setting.
223 * Pass "" to get the encoding selected by the server's environment.
225 * If the result is PG_SQL_ASCII, callers should treat it as being compatible
226 * with any desired encoding.
229 pg_get_encoding_from_locale(const char *ctype)
234 /* Get the CODESET property, and also LC_CTYPE if not passed in */
240 save = setlocale(LC_CTYPE, NULL);
242 return -1; /* setlocale() broken? */
243 /* must copy result, or it might change after setlocale */
246 return -1; /* out of memory; unlikely */
248 name = setlocale(LC_CTYPE, ctype);
252 return -1; /* bogus ctype passed in? */
256 sys = nl_langinfo(CODESET);
260 sys = win32_langinfo(name);
263 setlocale(LC_CTYPE, save);
269 ctype = setlocale(LC_CTYPE, NULL);
271 return -1; /* setlocale() broken? */
273 sys = nl_langinfo(CODESET);
277 sys = win32_langinfo(ctype);
282 return -1; /* out of memory; unlikely */
284 /* If locale is C or POSIX, we can allow all encodings */
285 if (pg_strcasecmp(ctype, "C") == 0 || pg_strcasecmp(ctype, "POSIX") == 0)
291 /* Check the table */
292 for (i = 0; encoding_match_list[i].system_enc_name; i++)
294 if (pg_strcasecmp(sys, encoding_match_list[i].system_enc_name) == 0)
297 return encoding_match_list[i].pg_enc_code;
301 /* Special-case kluges for particular platforms go here */
306 * Current OS X has many locales that report an empty string for CODESET,
307 * but they all seem to actually use UTF-8.
309 if (strlen(sys) == 0)
317 * We print a warning if we got a CODESET string but couldn't recognize
318 * it. This means we need another entry in the table.
321 fprintf(stderr, _("could not determine encoding for locale \"%s\": codeset is \"%s\""),
323 /* keep newline separate so there's only one translatable string */
327 (errmsg("could not determine encoding for locale \"%s\": codeset is \"%s\"",
329 errdetail("Please report this to <pgsql-bugs@postgresql.org>.")));
335 #else /* (HAVE_LANGINFO_H && CODESET) || WIN32 */
338 * stub if no platform support
340 * Note: we could return -1 here, but that would have the effect of
341 * forcing users to specify an encoding to initdb on such platforms.
342 * It seems better to silently default to SQL_ASCII.
345 pg_get_encoding_from_locale(const char *ctype)
350 #endif /* (HAVE_LANGINFO_H && CODESET) || WIN32 */