aboutsummaryrefslogtreecommitdiff
path: root/src/port/chklocale.c
diff options
context:
space:
mode:
authorNoah Misch <noah@leadboat.com>2013-06-26 11:17:33 -0400
committerNoah Misch <noah@leadboat.com>2013-06-26 11:17:33 -0400
commit5f538ad004aa00cf0881f179f0cde789aad4f47e (patch)
tree4a84500c39ff82734078f1c9169879decd163bf7 /src/port/chklocale.c
parent2c1031bd8602f749a81672015811f365a129acff (diff)
downloadpostgresql-5f538ad004aa00cf0881f179f0cde789aad4f47e.tar.gz
postgresql-5f538ad004aa00cf0881f179f0cde789aad4f47e.zip
Renovate display of non-ASCII messages on Windows.
GNU gettext selects a default encoding for the messages it emits in a platform-specific manner; it uses the Windows ANSI code page on Windows and follows LC_CTYPE on other platforms. This is inconvenient for PostgreSQL server processes, so realize consistent cross-platform behavior by calling bind_textdomain_codeset() on Windows each time we permanently change LC_CTYPE. This primarily affects SQL_ASCII databases and processes like the postmaster that do not attach to a database, making their behavior consistent with PostgreSQL on non-Windows platforms. Messages from SQL_ASCII databases use the encoding implied by the database LC_CTYPE, and messages from non-database processes use LC_CTYPE from the postmaster system environment. PlatformEncoding becomes unused, so remove it. Make write_console() prefer WriteConsoleW() to write() regardless of the encodings in use. In this situation, write() will invariably mishandle non-ASCII characters. elog.c has assumed that messages conform to the database encoding. While usually true, this does not hold for SQL_ASCII and MULE_INTERNAL. Introduce MessageEncoding to track the actual encoding of message text. The present consumers are Windows-specific code for converting messages to UTF16 for use in system interfaces. This fixes the appearance in Windows event logs and consoles of translated messages from SQL_ASCII processes like the postmaster. Note that SQL_ASCII inherently disclaims a strong notion of encoding, so non-ASCII byte sequences interpolated into messages by %s may yet yield a nonsensical message. MULE_INTERNAL has similar problems at present, albeit for a different reason: its lack of libiconv support or a conversion to UTF8. Consequently, one need no longer restart Windows with a different Windows ANSI code page to broadly test backend logging under a given language. Changing the user's locale ("Format") is enough. Several accounts can simultaneously run postmasters under different locales, all correctly logging localized messages to Windows event logs and consoles. Alexander Law and Noah Misch
Diffstat (limited to 'src/port/chklocale.c')
-rw-r--r--src/port/chklocale.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/port/chklocale.c b/src/port/chklocale.c
index 9e889383f26..8b8862ffb29 100644
--- a/src/port/chklocale.c
+++ b/src/port/chklocale.c
@@ -235,6 +235,32 @@ win32_langinfo(const char *ctype)
return r;
}
+
+#ifndef FRONTEND
+/*
+ * Given a Windows code page identifier, find the corresponding PostgreSQL
+ * encoding. Issue a warning and return -1 if none found.
+ */
+int
+pg_codepage_to_encoding(UINT cp)
+{
+ char sys[16];
+ int i;
+
+ sprintf(sys, "CP%u", cp);
+
+ /* Check the table */
+ for (i = 0; encoding_match_list[i].system_enc_name; i++)
+ if (pg_strcasecmp(sys, encoding_match_list[i].system_enc_name) == 0)
+ return encoding_match_list[i].pg_enc_code;
+
+ ereport(WARNING,
+ (errmsg("could not determine encoding for codeset \"%s\"", sys),
+ errdetail("Please report this to <pgsql-bugs@postgresql.org>.")));
+
+ return -1;
+}
+#endif
#endif /* WIN32 */
#if (defined(HAVE_LANGINFO_H) && defined(CODESET)) || defined(WIN32)
@@ -248,6 +274,9 @@ win32_langinfo(const char *ctype)
*
* If the result is PG_SQL_ASCII, callers should treat it as being compatible
* with any desired encoding.
+ *
+ * If running in the backend and write_message is false, this function must
+ * cope with the possibility that elog() and palloc() are not yet usable.
*/
int
pg_get_encoding_from_locale(const char *ctype, bool write_message)