From 227767112c3d3a6562b27e0a0da064d8b8ca1ebf Mon Sep 17 00:00:00 2001
From: Tatsuo Ishii <ishii@postgresql.org>
Date: Thu, 6 Sep 2001 04:57:30 +0000
Subject: [PATCH] Commit Karel's patch.

-------------------------------------------------------------------
Subject: Re: [PATCHES] encoding names
From: Karel Zak <zakkr@zf.jcu.cz>
To: Peter Eisentraut <peter_e@gmx.net>
Cc: pgsql-patches <pgsql-patches@postgresql.org>
Date: Fri, 31 Aug 2001 17:24:38 +0200

On Thu, Aug 30, 2001 at 01:30:40AM +0200, Peter Eisentraut wrote:
> > 		- convert encoding 'name' to 'id'
>
> I thought we decided not to add functions returning "new" names until we
> know exactly what the new names should be, and pending schema

 Ok, the patch not to add functions.

> better
>
>     ...(): encoding name too long

 Fixed.

 I found new bug in command/variable.c in parse_client_encoding(), nobody
probably never see this error:

if (pg_set_client_encoding(encoding))
{
	elog(ERROR, "Conversion between %s and %s is not supported",
                     value, GetDatabaseEncodingName());
}

because pg_set_client_encoding() returns -1 for error and 0 as true.
It's fixed too.

 IMHO it can be apply.

		Karel
PS:

    * following files are renamed:

src/utils/mb/Unicode/KOI8_to_utf8.map  -->
        src/utils/mb/Unicode/koi8r_to_utf8.map

src/utils/mb/Unicode/WIN_to_utf8.map  -->
        src/utils/mb/Unicode/win1251_to_utf8.map

src/utils/mb/Unicode/utf8_to_KOI8.map -->
        src/utils/mb/Unicode/utf8_to_koi8r.map

src/utils/mb/Unicode/utf8_to_WIN.map -->
        src/utils/mb/Unicode/utf8_to_win1251.map

   * new file:

src/utils/mb/encname.c

   * removed file:

src/utils/mb/common.c

--
 Karel Zak  <zakkr@zf.jcu.cz>
 http://home.zf.jcu.cz/~zakkr/

 C, PostgreSQL, PHP, WWW, http://docs.linux.cz, http://mape.jcu.cz
---
 src/backend/commands/dbcommands.c             |  13 +-
 src/backend/commands/variable.c               |  17 +-
 src/backend/parser/gram.y                     |   6 +-
 src/backend/utils/adt/ascii.c                 |   8 +-
 src/backend/utils/init/postinit.c             |   4 +-
 src/backend/utils/mb/Makefile                 |   8 +-
 src/backend/utils/mb/Unicode/Makefile         |   6 +-
 .../utils/mb/Unicode/UCS_to_cyrillic.pl       |   8 +-
 src/backend/utils/mb/conv.c                   | 200 +++++++---------
 src/backend/utils/mb/mbutils.c                | 136 +++++++----
 src/backend/utils/mb/wchar.c                  |  70 +++---
 src/bin/initdb/initdb.sh                      |  10 +-
 src/bin/pg_encoding/pg_encoding.c             |  66 ++++--
 src/bin/scripts/createdb                      |   6 +-
 src/include/catalog/pg_proc.h                 |   4 +-
 src/include/mb/pg_wchar.h                     | 215 ++++++++++++------
 src/include/utils/builtins.h                  |   5 +-
 src/interfaces/libpq/Makefile                 |   8 +-
 src/interfaces/libpq/fe-connect.c             |  77 +------
 src/interfaces/libpq/fe-misc.c                |   4 +-
 src/interfaces/odbc/multibyte.c               |  10 +-
 21 files changed, 479 insertions(+), 402 deletions(-)

diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c
index f7f765d9d7a..88b01fd91e9 100644
--- a/src/backend/commands/dbcommands.c
+++ b/src/backend/commands/dbcommands.c
@@ -9,7 +9,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.79 2001/08/26 16:55:59 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.80 2001/09/06 04:57:28 ishii Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -36,6 +36,10 @@
 #include "utils/fmgroids.h"
 #include "utils/syscache.h"
 
+#ifdef MULTIBYTE
+#include "mb/pg_wchar.h"	/* encoding check */
+#endif
+
 
 /* non-export function prototypes */
 static bool get_db_info(const char *name, Oid *dbIdP, int4 *ownerIdP,
@@ -142,6 +146,13 @@ createdb(const char *dbname, const char *dbpath,
 	if (encoding < 0)
 		encoding = src_encoding;
 
+#ifdef MULTIBYTE
+	/* Some encodings are client only */
+	if (!PG_VALID_BE_ENCODING( encoding ))
+		elog(ERROR, "CREATE DATABASE: invalid backend encoding");
+#else
+	Assert(encoding == 0);		/* zero is PG_SQL_ASCII */
+#endif
 	/*
 	 * Preassign OID for pg_database tuple, so that we can compute db
 	 * path.
diff --git a/src/backend/commands/variable.c b/src/backend/commands/variable.c
index 3d96ba18390..cc7ab385aa8 100644
--- a/src/backend/commands/variable.c
+++ b/src/backend/commands/variable.c
@@ -9,7 +9,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/commands/variable.c,v 1.51 2001/06/30 22:03:25 petere Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/commands/variable.c,v 1.52 2001/09/06 04:57:28 ishii Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -34,8 +34,8 @@
 #include "mb/pg_wchar.h"
 #else
 /* Grand unified hard-coded badness */
-#define pg_encoding_to_char(x) "SQL_ASCII"
-#define pg_get_client_encoding()  0
+#define pg_get_client_encoding_name()  "SQL_ASCII"
+#define GetDatabaseEncodingName() "SQL_ASCII"
 #endif
 
 
@@ -559,15 +559,15 @@ parse_client_encoding(char *value)
 	}
 	else
 	{
-		if (pg_set_client_encoding(encoding))
+		if (pg_set_client_encoding(encoding) < 0)
 		{
 			elog(ERROR, "Conversion between %s and %s is not supported",
-				 value, pg_encoding_to_char(GetDatabaseEncoding()));
+				 value, GetDatabaseEncodingName());
 		}
 	}
 #else
 	if (value &&
-	strcasecmp(value, pg_encoding_to_char(pg_get_client_encoding())) != 0)
+	strcasecmp(value, pg_get_client_encoding_name()) != 0)
 		elog(ERROR, "Client encoding %s is not supported", value);
 #endif
 	return TRUE;
@@ -577,7 +577,7 @@ static bool
 show_client_encoding(void)
 {
 	elog(NOTICE, "Current client encoding is %s",
-		 pg_encoding_to_char(pg_get_client_encoding()));
+		pg_get_client_encoding_name());
 	return TRUE;
 }
 
@@ -619,8 +619,7 @@ parse_server_encoding(char *value)
 static bool
 show_server_encoding(void)
 {
-	elog(NOTICE, "Current server encoding is %s",
-		 pg_encoding_to_char(GetDatabaseEncoding()));
+	elog(NOTICE, "Current server encoding is %s", GetDatabaseEncodingName());
 	return TRUE;
 }
 
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index 8d5e9abb07d..f440e239a86 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -11,7 +11,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.249 2001/08/26 16:55:59 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.250 2001/09/06 04:57:28 ishii Exp $
  *
  * HISTORY
  *	  AUTHOR			DATE			MAJOR EVENT
@@ -62,7 +62,7 @@
 #ifdef MULTIBYTE
 #include "mb/pg_wchar.h"
 #else
-#define GetStandardEncoding()	0		/* SQL_ASCII */
+#define GetStandardEncoding()	0		/* PG_SQL_ASCII */
 #define GetStandardEncodingName()	"SQL_ASCII"
 #endif
 
@@ -3027,7 +3027,7 @@ createdb_opt_item:  LOCATION '=' Sconst
 		| ENCODING '=' Iconst
 				{
 #ifdef MULTIBYTE
-					if (!pg_get_encent_by_encoding($3))
+					if (!pg_get_enconv_by_encoding($3))
 						elog(ERROR, "%d is not a valid encoding code", $3);
 #else
 					if ($3 != GetStandardEncoding())
diff --git a/src/backend/utils/adt/ascii.c b/src/backend/utils/adt/ascii.c
index 234651aebab..99bafca1baa 100644
--- a/src/backend/utils/adt/ascii.c
+++ b/src/backend/utils/adt/ascii.c
@@ -1,7 +1,7 @@
 /* -----------------------------------------------------------------------
  * ascii.c
  *
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/ascii.c,v 1.8 2001/03/22 06:16:17 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/ascii.c,v 1.9 2001/09/06 04:57:29 ishii Exp $
  *
  *	 Portions Copyright (c) 1999-2000, PostgreSQL Global Development Group
  *
@@ -78,7 +78,7 @@ pg_to_ascii(unsigned char *src, unsigned char *src_end, unsigned char *desc, int
 #define RANGE_160	160
 
 
-	if (enc == LATIN1)
+	if (enc == PG_LATIN1)
 	{
 
 		/*
@@ -87,7 +87,7 @@ pg_to_ascii(unsigned char *src, unsigned char *src_end, unsigned char *desc, int
 		ascii = "  cL Y  \"Ca  -R     'u .,      ?AAAAAAACEEEEIIII NOOOOOxOUUUUYTBaaaaaaaceeeeiiii nooooo/ouuuuyty";
 		range = RANGE_160;
 	}
-	else if (enc == LATIN2)
+	else if (enc == PG_LATIN2)
 	{
 
 		/*
@@ -96,7 +96,7 @@ pg_to_ascii(unsigned char *src, unsigned char *src_end, unsigned char *desc, int
 		ascii = " A L LS \"SSTZ-ZZ a,l'ls ,sstz\"zzRAAAALCCCEEEEIIDDNNOOOOxRUUUUYTBraaaalccceeeeiiddnnoooo/ruuuuyt.";
 		range = RANGE_160;
 	}
-	else if (enc == WIN1250)
+	else if (enc == PG_WIN1250)
 	{
 
 		/*
diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c
index 51bd00bec30..edaf3893e9e 100644
--- a/src/backend/utils/init/postinit.c
+++ b/src/backend/utils/init/postinit.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/utils/init/postinit.c,v 1.88 2001/08/25 18:52:42 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/utils/init/postinit.c,v 1.89 2001/09/06 04:57:29 ishii Exp $
  *
  *
  *-------------------------------------------------------------------------
@@ -127,7 +127,7 @@ ReverifyMyDatabase(const char *name)
 #ifdef MULTIBYTE
 	SetDatabaseEncoding(dbform->encoding);
 #else
-	if (dbform->encoding != SQL_ASCII)
+	if (dbform->encoding != PG_SQL_ASCII)
 		elog(FATAL, "database was initialized with MULTIBYTE encoding %d,\n\tbut the backend was compiled without multibyte support.\n\tlooks like you need to initdb or recompile.",
 			 dbform->encoding);
 #endif
diff --git a/src/backend/utils/mb/Makefile b/src/backend/utils/mb/Makefile
index 952ba8789d8..2f29a33b2bf 100644
--- a/src/backend/utils/mb/Makefile
+++ b/src/backend/utils/mb/Makefile
@@ -4,7 +4,7 @@
 #    Makefile for utils/mb
 #
 # IDENTIFICATION
-#    $Header: /cvsroot/pgsql/src/backend/utils/mb/Makefile,v 1.15 2000/11/30 20:36:11 petere Exp $
+#    $Header: /cvsroot/pgsql/src/backend/utils/mb/Makefile,v 1.16 2001/09/06 04:57:29 ishii Exp $
 #
 #-------------------------------------------------------------------------
 
@@ -12,7 +12,7 @@ subdir = src/backend/utils/mb
 top_builddir = ../../../..
 include $(top_builddir)/src/Makefile.global
 
-OBJS = common.o conv.o mbutils.o wchar.o wstrcmp.o wstrncmp.o big5.o
+OBJS = encnames.o conv.o mbutils.o wchar.o wstrcmp.o wstrncmp.o big5.o
 
 all: SUBSYS.o
 
@@ -21,13 +21,13 @@ SUBSYS.o: $(OBJS)
 
 utftest.o: utftest.c conv.c wchar.c mbutils.c
 
-sjistest: sjistest.o palloc.o common.o mbutils.o wchar.o wstrcmp.o wstrncmp.o big5.o
+sjistest: sjistest.o palloc.o encnames.o mbutils.o wchar.o wstrcmp.o wstrncmp.o big5.o
 	$(CC) $(CFLAGS) $(LDFLAGS) $^ $(LIBS) -o $@
 
 liketest: liketest.o palloc.o $(OBJS)
 	$(CC) $(CFLAGS) $(LDFLAGS) $^ $(LIBS) -o $@
 
-utftest: utftest.o palloc.o common.o wstrcmp.o wstrncmp.o big5.o
+utftest: utftest.o palloc.o encnames.o wstrcmp.o wstrncmp.o big5.o
 	$(CC) $(CFLAGS) $(LDFLAGS) $^ $(LIBS) -o $@
 
 depend dep:
diff --git a/src/backend/utils/mb/Unicode/Makefile b/src/backend/utils/mb/Unicode/Makefile
index 68a7750d1f0..6d9185b0381 100644
--- a/src/backend/utils/mb/Unicode/Makefile
+++ b/src/backend/utils/mb/Unicode/Makefile
@@ -4,7 +4,7 @@
 #
 # Copyright 2001 by PostgreSQL Global Development Group
 #
-# $Header: /cvsroot/pgsql/src/backend/utils/mb/Unicode/Makefile,v 1.2 2001/04/29 07:27:38 ishii Exp $
+# $Header: /cvsroot/pgsql/src/backend/utils/mb/Unicode/Makefile,v 1.3 2001/09/06 04:57:29 ishii Exp $
 #
 #-------------------------------------------------------------------------
 
@@ -18,8 +18,8 @@ ISO8859MAPS=iso8859_2_to_utf8.map iso8859_3_to_utf8.map \
 	utf8_to_iso8859_4.map utf8_to_iso8859_5.map 
 
 
-CYRILLICMAPS=KOI8_to_utf8.map WIN_to_utf8.map ALT_to_utf8.map\
-             utf8_to_KOI8.map  utf8_to_WIN.map utf8_to_ALT.map
+CYRILLICMAPS=koi8r_to_utf8.map win1251_to_utf8.map alt_to_utf8.map\
+             utf8_to_koi8r.map  utf8_to_win1251.map utf8_to_alt.map
 
 MAPS= $(ISO8859MAPS) $(CYRILLICMAPS)\
 	big5_to_utf8.map euc_cn_to_utf8.map euc_jp_to_utf8.map \
diff --git a/src/backend/utils/mb/Unicode/UCS_to_cyrillic.pl b/src/backend/utils/mb/Unicode/UCS_to_cyrillic.pl
index cc4fb90ac06..3557ed40fdf 100644
--- a/src/backend/utils/mb/Unicode/UCS_to_cyrillic.pl
+++ b/src/backend/utils/mb/Unicode/UCS_to_cyrillic.pl
@@ -2,7 +2,7 @@
 #
 # Copyright 2001 by PostgreSQL Global Development Group
 #
-# $Id: UCS_to_cyrillic.pl,v 1.1 2001/04/29 07:27:38 ishii Exp $
+# $Id: UCS_to_cyrillic.pl,v 1.2 2001/09/06 04:57:29 ishii Exp $
 #
 # Generate UTF-8 <--> ISO8859 code conversion tables from
 # map files provided by Unicode organization.
@@ -15,10 +15,10 @@
 #		 # and Unicode name (not used in this script)
 
 require "ucs2utf.pl";
-%filename = ('KOI8'=>'koi8-r.txt',
-             'WIN'=>'cp1251.txt',
+%filename = ('KOI8R'=>'koi8-r.txt',
+             'WIN1251'=>'cp1251.txt',
              'ALT'=>'cp866.txt');
-@charsets = ('KOI8','ALT','WIN');
+@charsets = ('KOI8R','ALT','WIN1251');
 foreach $charset (@charsets) {
 
 #
diff --git a/src/backend/utils/mb/conv.c b/src/backend/utils/mb/conv.c
index 91f781d21b6..6dbb9c6649a 100644
--- a/src/backend/utils/mb/conv.c
+++ b/src/backend/utils/mb/conv.c
@@ -6,7 +6,7 @@
  * WIN1250 client encoding support contributed by Pavel Behal
  * SJIS UDC (NEC selection IBM kanji) support contributed by Eiji Tokuya
  *
- * $Id: conv.c,v 1.26 2001/05/28 01:00:25 ishii Exp $
+ * $Id: conv.c,v 1.27 2001/09/06 04:57:29 ishii Exp $
  *
  *
  */
@@ -39,12 +39,12 @@
 #include "Unicode/utf8_to_big5.map"
 #include "Unicode/big5_to_utf8.map"
 /* Cyrillic charset conversion */
-#include "Unicode/ALT_to_utf8.map"
-#include "Unicode/KOI8_to_utf8.map"
-#include "Unicode/WIN_to_utf8.map"
-#include "Unicode/utf8_to_ALT.map"
-#include "Unicode/utf8_to_KOI8.map"
-#include "Unicode/utf8_to_WIN.map"
+#include "Unicode/alt_to_utf8.map"
+#include "Unicode/koi8r_to_utf8.map"
+#include "Unicode/win1251_to_utf8.map"
+#include "Unicode/utf8_to_alt.map"
+#include "Unicode/utf8_to_koi8r.map"
+#include "Unicode/utf8_to_win1251.map"
 
 
 #endif	 /* UNICODE_CONVERSION */
@@ -824,16 +824,16 @@ mic2ascii(unsigned char *mic, unsigned char *p, int len)
  * Alternativny Variant (MS-DOS CP866)
  */
 
-/* koi2mic: KOI8-R to Mule internal code */
+/* koi8r2mic: KOI8-R to Mule internal code */
 static void
-koi2mic(unsigned char *l, unsigned char *p, int len)
+koi8r2mic(unsigned char *l, unsigned char *p, int len)
 {
 	latin2mic(l, p, len, LC_KOI8_R);
 }
 
-/* mic2koi: Mule internal code to KOI8-R */
+/* mic2koi8r: Mule internal code to KOI8-R */
 static void
-mic2koi(unsigned char *mic, unsigned char *p, int len)
+mic2koi8r(unsigned char *mic, unsigned char *p, int len)
 {
 	mic2latin(mic, p, len, LC_KOI8_R);
 }
@@ -981,7 +981,7 @@ mic2iso(unsigned char *mic, unsigned char *p, int len)
 
 /* win2mic: CP1251 to Mule internal code */
 static void
-win2mic(unsigned char *l, unsigned char *p, int len)
+win12512mic(unsigned char *l, unsigned char *p, int len)
 {
 	static unsigned char win2koi[] = {
 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -1007,7 +1007,7 @@ win2mic(unsigned char *l, unsigned char *p, int len)
 
 /* mic2win: Mule internal code to CP1251 */
 static void
-mic2win(unsigned char *mic, unsigned char *p, int len)
+mic2win1251(unsigned char *mic, unsigned char *p, int len)
 {
 	static unsigned char koi2win[] = {
 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -1343,23 +1343,23 @@ utf_to_latin5(unsigned char *utf, unsigned char *iso, int len)
  */
 
 /*
- * UTF-8 --->KOI8 
+ * UTF-8 --->KOI8-R 
  */
 static void
-utf_to_KOI8(unsigned char *utf, unsigned char *iso, int len)
+utf_to_KOI8R(unsigned char *utf, unsigned char *iso, int len)
 
 {
-	utf_to_local(utf, iso, ULmap_KOI8, sizeof(ULmap_KOI8) / sizeof(pg_utf_to_local), len);
+	utf_to_local(utf, iso, ULmap_KOI8R, sizeof(ULmap_KOI8R) / sizeof(pg_utf_to_local), len);
 }
 
 /*
- * UTF-8 --->WIN 
+ * UTF-8 --->WIN1251 
  */
 static void
-utf_to_WIN(unsigned char *utf, unsigned char *iso, int len)
+utf_to_WIN1251(unsigned char *utf, unsigned char *iso, int len)
 
 {
-	utf_to_local(utf, iso, ULmap_WIN, sizeof(ULmap_WIN) / sizeof(pg_utf_to_local), len);
+	utf_to_local(utf, iso, ULmap_WIN1251, sizeof(ULmap_WIN1251) / sizeof(pg_utf_to_local), len);
 }
 
 /*
@@ -1382,10 +1382,8 @@ local_to_utf(unsigned char *iso, unsigned char *utf,
 	unsigned int iiso;
 	int			l;
 	pg_local_to_utf *p;
-	pg_encoding_conv_tbl *e;
 
-	e = pg_get_enc_ent(encoding);
-	if (e == 0)
+	if (!PG_VALID_ENCODING(encoding))
 		elog(ERROR, "Invalid encoding number %d", encoding);
 
 	for (; len > 0 && *iso; len -= l)
@@ -1424,7 +1422,7 @@ local_to_utf(unsigned char *iso, unsigned char *utf,
 		if (p == NULL)
 		{
 			elog(NOTICE, "local_to_utf: could not convert (0x%04x) %s to UTF-8. Ignored",
-				 iiso, e->name);
+				 iiso, (&pg_enc2name_tbl[ encoding ])->name);
 			continue;
 		}
 		if (p->utf & 0xff000000)
@@ -1445,7 +1443,7 @@ local_to_utf(unsigned char *iso, unsigned char *utf,
 static void
 latin2_to_utf(unsigned char *iso, unsigned char *utf, int len)
 {
-	local_to_utf(iso, utf, LUmapISO8859_2, sizeof(LUmapISO8859_2) / sizeof(pg_local_to_utf), LATIN2, len);
+	local_to_utf(iso, utf, LUmapISO8859_2, sizeof(LUmapISO8859_2) / sizeof(pg_local_to_utf), PG_LATIN2, len);
 }
 
 /*
@@ -1454,7 +1452,7 @@ latin2_to_utf(unsigned char *iso, unsigned char *utf, int len)
 static void
 latin3_to_utf(unsigned char *iso, unsigned char *utf, int len)
 {
-	local_to_utf(iso, utf, LUmapISO8859_3, sizeof(LUmapISO8859_3) / sizeof(pg_local_to_utf), LATIN3, len);
+	local_to_utf(iso, utf, LUmapISO8859_3, sizeof(LUmapISO8859_3) / sizeof(pg_local_to_utf), PG_LATIN3, len);
 }
 
 /*
@@ -1463,7 +1461,7 @@ latin3_to_utf(unsigned char *iso, unsigned char *utf, int len)
 static void
 latin4_to_utf(unsigned char *iso, unsigned char *utf, int len)
 {
-	local_to_utf(iso, utf, LUmapISO8859_4, sizeof(LUmapISO8859_4) / sizeof(pg_local_to_utf), LATIN4, len);
+	local_to_utf(iso, utf, LUmapISO8859_4, sizeof(LUmapISO8859_4) / sizeof(pg_local_to_utf), PG_LATIN4, len);
 }
 
 /*
@@ -1472,25 +1470,25 @@ latin4_to_utf(unsigned char *iso, unsigned char *utf, int len)
 static void
 latin5_to_utf(unsigned char *iso, unsigned char *utf, int len)
 {
-	local_to_utf(iso, utf, LUmapISO8859_5, sizeof(LUmapISO8859_5) / sizeof(pg_local_to_utf), LATIN5, len);
+	local_to_utf(iso, utf, LUmapISO8859_5, sizeof(LUmapISO8859_5) / sizeof(pg_local_to_utf), PG_LATIN5, len);
 }
 
 /*
- * KOI8 ---> UTF-8
+ * KOI8-R ---> UTF-8
  */
 static void
-KOI8_to_utf(unsigned char *iso, unsigned char *utf, int len)
+KOI8R_to_utf(unsigned char *iso, unsigned char *utf, int len)
 {
-	local_to_utf(iso, utf, LUmapKOI8, sizeof(LUmapKOI8) / sizeof(pg_local_to_utf), KOI8, len);
+	local_to_utf(iso, utf, LUmapKOI8R, sizeof(LUmapKOI8R) / sizeof(pg_local_to_utf), PG_KOI8R, len);
 }
 
 /*
- * WIN ---> UTF-8
+ * WIN1251 ---> UTF-8
  */
 static void
-WIN_to_utf(unsigned char *iso, unsigned char *utf, int len)
+WIN1251_to_utf(unsigned char *iso, unsigned char *utf, int len)
 {
-	local_to_utf(iso, utf, LUmapWIN, sizeof(LUmapWIN) / sizeof(pg_local_to_utf), WIN, len);
+	local_to_utf(iso, utf, LUmapWIN1251, sizeof(LUmapWIN1251) / sizeof(pg_local_to_utf), PG_WIN1251, len);
 }
 
 /*
@@ -1499,7 +1497,7 @@ WIN_to_utf(unsigned char *iso, unsigned char *utf, int len)
 static void
 ALT_to_utf(unsigned char *iso, unsigned char *utf, int len)
 {
-	local_to_utf(iso, utf, LUmapALT, sizeof(LUmapALT) / sizeof(pg_local_to_utf), ALT, len);
+	local_to_utf(iso, utf, LUmapALT, sizeof(LUmapALT) / sizeof(pg_local_to_utf), PG_ALT, len);
 }
 /*
  * UTF-8 ---> EUC_JP
@@ -1519,7 +1517,7 @@ static void
 euc_jp_to_utf(unsigned char *euc, unsigned char *utf, int len)
 {
 	local_to_utf(euc, utf, LUmapEUC_JP,
-			 sizeof(LUmapEUC_JP) / sizeof(pg_local_to_utf), EUC_JP, len);
+			 sizeof(LUmapEUC_JP) / sizeof(pg_local_to_utf), PG_EUC_JP, len);
 }
 
 /*
@@ -1540,7 +1538,7 @@ static void
 euc_cn_to_utf(unsigned char *euc, unsigned char *utf, int len)
 {
 	local_to_utf(euc, utf, LUmapEUC_CN,
-			 sizeof(LUmapEUC_CN) / sizeof(pg_local_to_utf), EUC_CN, len);
+			 sizeof(LUmapEUC_CN) / sizeof(pg_local_to_utf), PG_EUC_CN, len);
 }
 
 /*
@@ -1561,7 +1559,7 @@ static void
 euc_kr_to_utf(unsigned char *euc, unsigned char *utf, int len)
 {
 	local_to_utf(euc, utf, LUmapEUC_KR,
-			 sizeof(LUmapEUC_KR) / sizeof(pg_local_to_utf), EUC_KR, len);
+			 sizeof(LUmapEUC_KR) / sizeof(pg_local_to_utf), PG_EUC_KR, len);
 }
 
 /*
@@ -1582,7 +1580,7 @@ static void
 euc_tw_to_utf(unsigned char *euc, unsigned char *utf, int len)
 {
 	local_to_utf(euc, utf, LUmapEUC_TW,
-			 sizeof(LUmapEUC_TW) / sizeof(pg_local_to_utf), EUC_TW, len);
+			 sizeof(LUmapEUC_TW) / sizeof(pg_local_to_utf), PG_EUC_TW, len);
 }
 
 /*
@@ -1603,7 +1601,7 @@ static void
 sjis_to_utf(unsigned char *euc, unsigned char *utf, int len)
 {
 	local_to_utf(euc, utf, LUmapSJIS,
-				 sizeof(LUmapSJIS) / sizeof(pg_local_to_utf), SJIS, len);
+				 sizeof(LUmapSJIS) / sizeof(pg_local_to_utf), PG_SJIS, len);
 }
 
 /*
@@ -1624,85 +1622,59 @@ static void
 big5_to_utf(unsigned char *euc, unsigned char *utf, int len)
 {
 	local_to_utf(euc, utf, LUmapBIG5,
-				 sizeof(LUmapBIG5) / sizeof(pg_local_to_utf), BIG5, len);
-}
-
-/*-----------------------------------------------------------------*/
-
-pg_encoding_conv_tbl pg_conv_tbl[] = {
-	{SQL_ASCII, "SQL_ASCII", 0, ascii2mic, mic2ascii,
-	ascii2utf, utf2ascii},		/* SQL/ASCII */
-	{EUC_JP, "EUC_JP", 0, euc_jp2mic, mic2euc_jp,
-	euc_jp_to_utf, utf_to_euc_jp},		/* EUC_JP */
-	{EUC_CN, "EUC_CN", 0, euc_cn2mic, mic2euc_cn,
-	euc_cn_to_utf, utf_to_euc_cn},		/* EUC_CN */
-	{EUC_KR, "EUC_KR", 0, euc_kr2mic, mic2euc_kr,
-	euc_kr_to_utf, utf_to_euc_kr},		/* EUC_KR */
-	{EUC_TW, "EUC_TW", 0, euc_tw2mic, mic2euc_tw,
-	euc_tw_to_utf, utf_to_euc_tw},		/* EUC_TW */
-	{UNICODE, "UNICODE", 0, 0, 0},		/* UNICODE */
-	{MULE_INTERNAL, "MULE_INTERNAL", 0, 0, 0},	/* MULE_INTERNAL */
-	{LATIN1, "LATIN1", 0, latin12mic, mic2latin1,
-	latin1_to_utf, utf_to_latin1},		/* ISO 8859 Latin 1 */
-	{LATIN2, "LATIN2", 0, latin22mic, mic2latin2,
-	latin2_to_utf, utf_to_latin2},		/* ISO 8859 Latin 2 */
-	{LATIN3, "LATIN3", 0, latin32mic, mic2latin3,
-	latin3_to_utf, utf_to_latin3},		/* ISO 8859 Latin 3 */
-	{LATIN4, "LATIN4", 0, latin42mic, mic2latin4,
-	latin4_to_utf, utf_to_latin4},		/* ISO 8859 Latin 4 */
-	{LATIN5, "LATIN5", 0, iso2mic, mic2iso,
-	latin5_to_utf, utf_to_latin5},		/* ISO 8859 Latin 5 */
-	{KOI8, "KOI8", 0, koi2mic, mic2koi,
-	KOI8_to_utf, utf_to_KOI8},						/* KOI8-R */
-	{WIN, "WIN", 0, win2mic, mic2win,
-	WIN_to_utf , utf_to_WIN},						/* CP1251 */
-	{ALT, "ALT", 0, alt2mic, mic2alt,
-	ALT_to_utf, utf_to_ALT},						/* CP866 */
-	{SJIS, "SJIS", 1, sjis2mic, mic2sjis,
-	sjis_to_utf, utf_to_sjis},	/* SJIS */
-	{BIG5, "BIG5", 1, big52mic, mic2big5,
-	big5_to_utf, utf_to_big5},	/* Big5 */
-	{WIN1250, "WIN1250", 1, win12502mic, mic2win1250,
-	0, 0},						/* WIN 1250 */
-	{-1, "", 0, 0, 0, 0}		/* end mark */
+				 sizeof(LUmapBIG5) / sizeof(pg_local_to_utf), PG_BIG5, len);
+}
+
+/* ----------
+ * Encoding conversion routines
+ *
+ * WARINIG: must by same order as pg_enc in include/mb/pg_wchar.h!
+ * ----------
+ */
+pg_enconv pg_enconv_tbl[] = 
+{
+	{ PG_SQL_ASCII,	ascii2mic, mic2ascii, ascii2utf, utf2ascii },
+	{ PG_EUC_JP,	euc_jp2mic, mic2euc_jp, euc_jp_to_utf, utf_to_euc_jp },
+	{ PG_EUC_CN,	euc_cn2mic, mic2euc_cn, euc_cn_to_utf, utf_to_euc_cn },
+	{ PG_EUC_KR,	euc_kr2mic, mic2euc_kr, euc_kr_to_utf, utf_to_euc_kr },
+	{ PG_EUC_TW,	euc_tw2mic, mic2euc_tw, euc_tw_to_utf, utf_to_euc_tw },
+	{ PG_UTF8, 	0, 0, 0, 0 },
+	{ PG_MULE_INTERNAL, 0, 0, 0, 0 },
+	{ PG_LATIN1,	latin12mic, mic2latin1, latin1_to_utf, utf_to_latin1 },
+	{ PG_LATIN2,	latin22mic, mic2latin2, latin2_to_utf, utf_to_latin2 },
+	{ PG_LATIN3,	latin32mic, mic2latin3, latin3_to_utf, utf_to_latin3 },
+	{ PG_LATIN4,	latin42mic, mic2latin4, latin4_to_utf, utf_to_latin4 },
+	{ PG_LATIN5,	iso2mic, mic2iso, latin5_to_utf, utf_to_latin5 },
+	{ PG_KOI8R,	koi8r2mic, mic2koi8r, KOI8R_to_utf, utf_to_KOI8R },
+	{ PG_WIN1251,	win12512mic, mic2win1251, WIN1251_to_utf, utf_to_WIN1251 },
+	{ PG_ALT,	alt2mic, mic2alt, ALT_to_utf, utf_to_ALT },
+	{ PG_SJIS,	sjis2mic, mic2sjis, sjis_to_utf, utf_to_sjis },
+	{ PG_BIG5,	big52mic, mic2big5, big5_to_utf, utf_to_big5},
+	{ PG_WIN1250,	win12502mic, mic2win1250, 0, 0 },
 };
 
 #else
 
-pg_encoding_conv_tbl pg_conv_tbl[] = {
-	{SQL_ASCII, "SQL_ASCII", 0, ascii2mic, mic2ascii,
-	0, 0},						/* SQL/ASCII */
-	{EUC_JP, "EUC_JP", 0, euc_jp2mic, mic2euc_jp,
-	0, 0},						/* EUC_JP */
-	{EUC_CN, "EUC_CN", 0, euc_cn2mic, mic2euc_cn,
-	0, 0},						/* EUC_CN */
-	{EUC_KR, "EUC_KR", 0, euc_kr2mic, mic2euc_kr},		/* EUC_KR */
-	{EUC_TW, "EUC_TW", 0, euc_tw2mic, mic2euc_tw},		/* EUC_TW */
-	{UNICODE, "UNICODE", 0, 0, 0},		/* UNICODE */
-	{MULE_INTERNAL, "MULE_INTERNAL", 0, 0, 0},	/* MULE_INTERNAL */
-	{LATIN1, "LATIN1", 0, latin12mic, mic2latin1,
-	0, 0},						/* ISO 8859 Latin 1 */
-	{LATIN2, "LATIN2", 0, latin22mic, mic2latin2,
-	0, 0},						/* ISO 8859 Latin 2 */
-	{LATIN3, "LATIN3", 0, latin32mic, mic2latin3,
-	0, 0},						/* ISO 8859 Latin 3 */
-	{LATIN4, "LATIN4", 0, latin42mic, mic2latin4,
-	0, 0},						/* ISO 8859 Latin 4 */
-	{LATIN5, "LATIN5", 0, iso2mic, mic2iso,
-	0, 0},						/* ISO 8859 Latin 5 */
-	{KOI8, "KOI8", 0, koi2mic, mic2koi,
-	0, 0},						/* KOI8-R */
-	{WIN, "WIN", 0, win2mic, mic2win,
-	0, 0},						/* CP1251 */
-	{ALT, "ALT", 0, alt2mic, mic2alt,
-	0, 0},						/* CP866 */
-	{SJIS, "SJIS", 1, sjis2mic, mic2sjis,
-	0, 0},						/* SJIS */
-	{BIG5, "BIG5", 1, big52mic, mic2big5,
-	0, 0},						/* Big5 */
-	{WIN1250, "WIN1250", 1, win12502mic, mic2win1250,
-	0, 0},						/* WIN 1250 */
-	{-1, "", 0, 0, 0, 0}		/* end mark */
+pg_enconv pg_enconv_tbl[] = 
+{
+	{ PG_SQL_ASCII,	ascii2mic, mic2ascii, 0, 0 },
+	{ PG_EUC_JP,	euc_jp2mic, mic2euc_jp, 0, 0 },
+	{ PG_EUC_CN,	euc_cn2mic, mic2euc_cn, 0, 0 },
+	{ PG_EUC_KR,	euc_kr2mic, mic2euc_kr, 0, 0 },
+	{ PG_EUC_TW,	euc_tw2mic, mic2euc_tw, 0, 0 },
+	{ PG_UTF8, 	0, 0, 0, 0 },
+	{ PG_MULE_INTERNAL, 0, 0, 0, 0 },
+	{ PG_LATIN1,	latin12mic, mic2latin1, 0, 0 },
+	{ PG_LATIN2,	latin22mic, mic2latin2, 0, 0 },
+	{ PG_LATIN3,	latin32mic, mic2latin3, 0, 0 },
+	{ PG_LATIN4,	latin42mic, mic2latin4, 0, 0 },
+	{ PG_LATIN5,	iso2mic, mic2iso, 0, 0 },
+	{ PG_KOI8R,	koi8r2mic, mic2koi8r, 0, 0 },
+	{ PG_WIN1251,	win12512mic, mic2win1251, 0, 0 },
+	{ PG_ALT,	alt2mic, mic2alt, 0, 0 },
+	{ PG_SJIS,	sjis2mic, mic2sjis, 0, 0 },
+	{ PG_BIG5,	big52mic, mic2big5, 0, 0 },
+	{ PG_WIN1250,	win12502mic, mic2win1250, 0, 0 },
 };
 
 #endif	 /* UNICODE_CONVERSION */
diff --git a/src/backend/utils/mb/mbutils.c b/src/backend/utils/mb/mbutils.c
index 8e4fc56ef09..39d59cdb140 100644
--- a/src/backend/utils/mb/mbutils.c
+++ b/src/backend/utils/mb/mbutils.c
@@ -3,7 +3,7 @@
  * client encoding and server internal encoding.
  * (currently mule internal code (mic) is used)
  * Tatsuo Ishii
- * $Id: mbutils.c,v 1.19 2001/08/15 07:07:40 ishii Exp $
+ * $Id: mbutils.c,v 1.20 2001/09/06 04:57:29 ishii Exp $
  */
 #include "postgres.h"
 
@@ -11,26 +11,36 @@
 #include "mb/pg_wchar.h"
 #include "utils/builtins.h"
 
-static int	client_encoding = -1;
-static void (*client_to_mic) ();/* something to MIC */
-static void (*client_from_mic) ();		/* MIC to something */
-static void (*server_to_mic) ();/* something to MIC */
-static void (*server_from_mic) ();		/* MIC to something */
+/*
+ * We handle for actual FE and BE encoding setting encoding-identificator 
+ * and encoding-name too. It prevent searching and conversion from encoding
+ * to encoding name in getdatabaseencoding() and other routines.
+ *
+ * Default is PG_SQL_ASCII encoding (but this is never used, because
+ * backend during startup init it by SetDatabaseEncoding()).
+ *
+ * Karel Zak (Aug 2001)
+ */
+static pg_enc2name	*ClientEncoding = NULL;
+static pg_enc2name	*DatabaseEncoding = &pg_enc2name_tbl[ PG_SQL_ASCII ];
+
+static void	(*client_to_mic) ();	/* something to MIC */
+static void	(*client_from_mic) ();	/* MIC to something */
+static void	(*server_to_mic) ();	/* something to MIC */
+static void	(*server_from_mic) ();	/* MIC to something */
 
 /*
  * find encoding table entry by encoding
  */
-pg_encoding_conv_tbl *
-pg_get_enc_ent(int encoding)
+pg_enconv *
+pg_get_enconv_by_encoding(int encoding)
 {
-	pg_encoding_conv_tbl *p = pg_conv_tbl;
-
-	for (; p->encoding >= 0; p++)
+	if (PG_VALID_ENCODING(encoding))
 	{
-		if (p->encoding == encoding)
-			return (p);
+		Assert((&pg_enconv_tbl[ encoding ])->encoding == encoding);
+		return &pg_enconv_tbl[ encoding ];
 	}
-	return (0);
+	return 0;
 }
 
 /*
@@ -56,38 +66,38 @@ pg_find_encoding_converters(int src, int dest, void (**src_to_mic)(), void (**de
 	{							/* src == dest? */
 		*src_to_mic = *dest_from_mic = 0;
 	}
-	else if (src == MULE_INTERNAL)
+	else if (src == PG_MULE_INTERNAL)
 	{							/* src == MULE_INETRNAL? */
-		*dest_from_mic = pg_get_enc_ent(dest)->from_mic;
+		*dest_from_mic = pg_get_enconv_by_encoding(dest)->from_mic;
 		if (*dest_from_mic == 0)
 			return (-1);
 		*src_to_mic = 0;
 	}
-	else if (dest == MULE_INTERNAL)
+	else if (dest == PG_MULE_INTERNAL)
 	{							/* dest == MULE_INETRNAL? */
-		*src_to_mic = pg_get_enc_ent(src)->to_mic;
+		*src_to_mic = pg_get_enconv_by_encoding(src)->to_mic;
 		if (*src_to_mic == 0)
 			return (-1);
 		*dest_from_mic = 0;
 	}
-	else if (src == UNICODE)
+	else if (src == PG_UTF8)
 	{							/* src == UNICODE? */
-		*dest_from_mic = pg_get_enc_ent(dest)->from_unicode;
+		*dest_from_mic = pg_get_enconv_by_encoding(dest)->from_unicode;
 		if (*dest_from_mic == 0)
 			return (-1);
 		*src_to_mic = 0;
 	}
-	else if (dest == UNICODE)
+	else if (dest == PG_UTF8)
 	{							/* dest == UNICODE? */
-		*src_to_mic = pg_get_enc_ent(src)->to_unicode;
+		*src_to_mic = pg_get_enconv_by_encoding(src)->to_unicode;
 		if (*src_to_mic == 0)
 			return (-1);
 		*dest_from_mic = 0;
 	}
 	else
 	{
-		*src_to_mic = pg_get_enc_ent(src)->to_mic;
-		*dest_from_mic = pg_get_enc_ent(dest)->from_mic;
+		*src_to_mic = pg_get_enconv_by_encoding(src)->to_mic;
+		*dest_from_mic = pg_get_enconv_by_encoding(dest)->from_mic;
 		if (*src_to_mic == 0 || *dest_from_mic == 0)
 			return (-1);
 	}
@@ -101,11 +111,17 @@ pg_find_encoding_converters(int src, int dest, void (**src_to_mic)(), void (**de
 int
 pg_set_client_encoding(int encoding)
 {
-	int current_server_encoding = GetDatabaseEncoding();
+	int current_server_encoding = DatabaseEncoding->encoding;
+
+	if (!PG_VALID_FE_ENCODING(encoding))
+		return (-1);
 
 	if (pg_find_encoding_converters(encoding, current_server_encoding, &client_to_mic, &server_from_mic) < 0)
 		return (-1);
-	client_encoding = encoding;
+
+	ClientEncoding = &pg_enc2name_tbl[ encoding ];
+
+	Assert(ClientEncoding->encoding == encoding);
 
 	if (pg_find_encoding_converters(current_server_encoding, encoding, &server_to_mic, &client_from_mic) < 0)
 		return (-1);
@@ -118,12 +134,30 @@ pg_set_client_encoding(int encoding)
 int
 pg_get_client_encoding()
 {
-	if (client_encoding == -1)
+	Assert(DatabaseEncoding);
+
+	if (ClientEncoding == NULL)
 	{
 		/* this is the first time */
-		client_encoding = GetDatabaseEncoding();
+		ClientEncoding = DatabaseEncoding;
 	}
-	return (client_encoding);
+	return (ClientEncoding->encoding);
+}
+
+/*
+ * returns the current client encoding name
+ */
+const char *
+pg_get_client_encoding_name()
+{
+	Assert(DatabaseEncoding);
+
+	if (ClientEncoding == NULL)
+	{
+		/* this is the first time */
+		ClientEncoding = DatabaseEncoding;
+	}
+	return (ClientEncoding->name);
 }
 
 /*
@@ -189,7 +223,7 @@ pg_convert(PG_FUNCTION_ARGS)
 	text	*string = PG_GETARG_TEXT_P(0);
 	Name	s = PG_GETARG_NAME(1);
 	int encoding = pg_char_to_encoding(NameStr(*s));
-	int db_encoding = GetDatabaseEncoding();
+	int db_encoding = DatabaseEncoding->encoding;
 	void (*src)(), (*dest)();
 	unsigned char	*result;
 	text	*retval;
@@ -277,7 +311,10 @@ pg_convert2(PG_FUNCTION_ARGS)
 unsigned char *
 pg_client_to_server(unsigned char *s, int len)
 {
-	if (client_encoding == GetDatabaseEncoding())
+	Assert(ClientEncoding);
+	Assert(DatabaseEncoding);
+
+	if (ClientEncoding->encoding == DatabaseEncoding->encoding)
 	    return s;
 
 	return pg_do_encoding_conversion(s, len, client_to_mic, server_from_mic);
@@ -299,7 +336,10 @@ pg_client_to_server(unsigned char *s, int len)
 unsigned char *
 pg_server_to_client(unsigned char *s, int len)
 {
-	if (client_encoding == GetDatabaseEncoding())
+	Assert(ClientEncoding);
+	Assert(DatabaseEncoding);
+
+	if (ClientEncoding->encoding == DatabaseEncoding->encoding)
 		return s;
 
 	return pg_do_encoding_conversion(s, len, server_to_mic, client_from_mic);
@@ -309,21 +349,21 @@ pg_server_to_client(unsigned char *s, int len)
 int
 pg_mb2wchar(const unsigned char *from, pg_wchar * to)
 {
-	return (*pg_wchar_table[GetDatabaseEncoding()].mb2wchar_with_len) (from, to, strlen(from));
+	return (*pg_wchar_table[ DatabaseEncoding->encoding ].mb2wchar_with_len) (from, to, strlen(from));
 }
 
 /* convert a multi-byte string to a wchar with a limited length */
 int
 pg_mb2wchar_with_len(const unsigned char *from, pg_wchar * to, int len)
 {
-	return (*pg_wchar_table[GetDatabaseEncoding()].mb2wchar_with_len) (from, to, len);
+	return (*pg_wchar_table[ DatabaseEncoding->encoding ].mb2wchar_with_len) (from, to, len);
 }
 
 /* returns the byte length of a multi-byte word */
 int
 pg_mblen(const unsigned char *mbstr)
 {
-	return ((*pg_wchar_table[GetDatabaseEncoding()].mblen) (mbstr));
+	return ((*pg_wchar_table[ DatabaseEncoding->encoding ].mblen) (mbstr));
 }
 
 /* returns the length (counted as a wchar) of a multi-byte string */
@@ -407,27 +447,33 @@ pg_mbcharcliplen(const unsigned char *mbstr, int len, int limit)
 	return (clen);
 }
 
-/*
- * functions for utils/init */
-static int	DatabaseEncoding = MULTIBYTE;
-
 void
 SetDatabaseEncoding(int encoding)
 {
-	DatabaseEncoding = encoding;
+	if (!PG_VALID_BE_ENCODING(encoding))
+		elog(ERROR, "SetDatabaseEncoding(): invalid database encoding");
+
+	DatabaseEncoding = &pg_enc2name_tbl[ encoding ];
+	Assert(DatabaseEncoding->encoding == encoding);
 }
 
 int
 GetDatabaseEncoding()
 {
-	return (DatabaseEncoding);
+	Assert(DatabaseEncoding);
+	return (DatabaseEncoding->encoding);
+}
+
+const char *
+GetDatabaseEncodingName()
+{
+	Assert(DatabaseEncoding);
+	return (DatabaseEncoding->name);
 }
 
-/* for builtin-function */
 Datum
 getdatabaseencoding(PG_FUNCTION_ARGS)
 {
-	const char *encoding_name = pg_encoding_to_char(DatabaseEncoding);
-
-	return DirectFunctionCall1(namein, CStringGetDatum(encoding_name));
+	Assert(DatabaseEncoding);
+	return DirectFunctionCall1(namein, CStringGetDatum(DatabaseEncoding->name));
 }
diff --git a/src/backend/utils/mb/wchar.c b/src/backend/utils/mb/wchar.c
index 43371f9477a..a7e97cc1866 100644
--- a/src/backend/utils/mb/wchar.c
+++ b/src/backend/utils/mb/wchar.c
@@ -1,16 +1,22 @@
 /*
  * conversion functions between pg_wchar and multi-byte streams.
  * Tatsuo Ishii
- * $Id: wchar.c,v 1.18 2001/04/19 02:34:35 ishii Exp $
+ * $Id: wchar.c,v 1.19 2001/09/06 04:57:29 ishii Exp $
  *
  * WIN1250 client encoding updated by Pavel Behal
  *
  */
 /* can be used in either frontend or backend */
 #include "postgres_fe.h"
-
 #include "mb/pg_wchar.h"
 
+#ifdef FRONTEND
+	#define Assert(condition)
+#else
+	#include "postgres.h"
+#endif
+
+
 /*
  * conversion to pg_wchar is done by "table driven."
  * to add an encoding support, define mb2wchar_with_len(), mblen()
@@ -452,41 +458,24 @@ pg_big5_mblen(const unsigned char *s)
 }
 
 pg_wchar_tbl pg_wchar_table[] = {
-	{pg_ascii2wchar_with_len, pg_ascii_mblen},	/* 0 */
-	{pg_eucjp2wchar_with_len, pg_eucjp_mblen},	/* 1 */
-	{pg_euccn2wchar_with_len, pg_euccn_mblen},	/* 2 */
-	{pg_euckr2wchar_with_len, pg_euckr_mblen},	/* 3 */
-	{pg_euctw2wchar_with_len, pg_euctw_mblen},	/* 4 */
-	{pg_utf2wchar_with_len, pg_utf_mblen},		/* 5 */
-	{pg_mule2wchar_with_len, pg_mule_mblen},	/* 6 */
-	{pg_latin12wchar_with_len, pg_latin1_mblen},		/* 7 */
-	{pg_latin12wchar_with_len, pg_latin1_mblen},		/* 8 */
-	{pg_latin12wchar_with_len, pg_latin1_mblen},		/* 9 */
-	{pg_latin12wchar_with_len, pg_latin1_mblen},		/* 10 */
-	{pg_latin12wchar_with_len, pg_latin1_mblen},		/* 11 */
-	{pg_latin12wchar_with_len, pg_latin1_mblen},		/* 12 */
-	{pg_latin12wchar_with_len, pg_latin1_mblen},		/* 13 */
-	{pg_latin12wchar_with_len, pg_latin1_mblen},		/* 14 */
-	{pg_latin12wchar_with_len, pg_latin1_mblen},		/* 15 */
-	{pg_latin12wchar_with_len, pg_latin1_mblen},		/* 16 */
-	{pg_latin12wchar_with_len, pg_latin1_mblen},		/* 17 */
-	{pg_latin12wchar_with_len, pg_latin1_mblen},		/* 18 */
-	{pg_latin12wchar_with_len, pg_latin1_mblen},		/* 19 */
-	{pg_latin12wchar_with_len, pg_latin1_mblen},		/* 20 */
-	{pg_latin12wchar_with_len, pg_latin1_mblen},		/* 21 */
-	{pg_latin12wchar_with_len, pg_latin1_mblen},		/* 22 */
-	{pg_latin12wchar_with_len, pg_latin1_mblen},		/* 23 */
-	{pg_latin12wchar_with_len, pg_latin1_mblen},		/* 24 */
-	{pg_latin12wchar_with_len, pg_latin1_mblen},		/* 25 */
-	{pg_latin12wchar_with_len, pg_latin1_mblen},		/* 26 */
-	{pg_latin12wchar_with_len, pg_latin1_mblen},		/* 27 */
-	{pg_latin12wchar_with_len, pg_latin1_mblen},		/* 28 */
-	{pg_latin12wchar_with_len, pg_latin1_mblen},		/* 29 */
-	{pg_latin12wchar_with_len, pg_latin1_mblen},		/* 30 */
-	{pg_latin12wchar_with_len, pg_latin1_mblen},		/* 31 */
-	{0, pg_sjis_mblen},			/* 32 */
-	{0, pg_big5_mblen},			/* 33 */
-	{pg_latin12wchar_with_len, pg_latin1_mblen} /* 34 */
+	{pg_ascii2wchar_with_len, pg_ascii_mblen},	/* 0; PG_SQL_ASCII  */
+	{pg_eucjp2wchar_with_len, pg_eucjp_mblen},	/* 1; PG_EUC_JP */
+	{pg_euccn2wchar_with_len, pg_euccn_mblen},	/* 2; PG_EUC_CN */
+	{pg_euckr2wchar_with_len, pg_euckr_mblen},	/* 3; PG_EUC_KR */
+	{pg_euctw2wchar_with_len, pg_euctw_mblen},	/* 4; PG_EUC_TW */
+	{pg_utf2wchar_with_len, pg_utf_mblen},		/* 5; PG_UNICODE */
+	{pg_mule2wchar_with_len, pg_mule_mblen},	/* 6; PG_MULE_INTERNAL */
+	{pg_latin12wchar_with_len, pg_latin1_mblen},	/* 7; PG_LATIN1 */
+	{pg_latin12wchar_with_len, pg_latin1_mblen},	/* 8; PG_LATIN2 */
+	{pg_latin12wchar_with_len, pg_latin1_mblen},	/* 9; PG_LATIN3 */
+	{pg_latin12wchar_with_len, pg_latin1_mblen},	/* 10; PG_LATIN4 */
+	{pg_latin12wchar_with_len, pg_latin1_mblen},	/* 11; PG_LATIN5 */
+	{pg_latin12wchar_with_len, pg_latin1_mblen},	/* 12; PG_KOI8 */
+	{pg_latin12wchar_with_len, pg_latin1_mblen},	/* 13; PG_WIN1251 */
+	{pg_latin12wchar_with_len, pg_latin1_mblen},	/* 14; PG_ALT */
+	{0, pg_sjis_mblen},				/* 15; PG_SJIS */
+	{0, pg_big5_mblen},				/* 17; PG_BIG5 */
+	{pg_latin12wchar_with_len, pg_latin1_mblen} 	/* 18; PG_WIN1250 */
 };
 
 /* returns the byte length of a word for mule internal code */
@@ -502,5 +491,10 @@ pg_mic_mblen(const unsigned char *mbstr)
 int
 pg_encoding_mblen(int encoding, const unsigned char *mbstr)
 {
-	return( (encoding >= 0 && encoding < sizeof(pg_wchar_table)/sizeof(pg_wchar_tbl))? ((*pg_wchar_table[encoding].mblen) (mbstr)) : ((*pg_wchar_table[SQL_ASCII].mblen) (mbstr)));
+	Assert(PG_VALID_ENCODING(encoding));
+
+	return( (encoding >= 0 && 
+	         encoding < sizeof(pg_wchar_table)/sizeof(pg_wchar_tbl)) ? 
+			((*pg_wchar_table[encoding].mblen) (mbstr)) : 
+			((*pg_wchar_table[PG_SQL_ASCII].mblen) (mbstr)));
 }
diff --git a/src/bin/initdb/initdb.sh b/src/bin/initdb/initdb.sh
index 11799e70490..16f7cb95d3c 100644
--- a/src/bin/initdb/initdb.sh
+++ b/src/bin/initdb/initdb.sh
@@ -27,7 +27,7 @@
 # Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
 # Portions Copyright (c) 1994, Regents of the University of California
 #
-# $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.135 2001/08/26 16:56:00 tgl Exp $
+# $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.136 2001/09/06 04:57:29 ishii Exp $
 #
 #-------------------------------------------------------------------------
 
@@ -292,7 +292,7 @@ fi
 
 if [ "$MULTIBYTE" ]
 then
-	MULTIBYTEID=`$PGPATH/pg_encoding $MULTIBYTE`
+	MULTIBYTEID=`$PGPATH/pg_encoding -b $MULTIBYTE`
         if [ "$?" -ne 0 ]
 	then
               (
@@ -305,11 +305,7 @@ then
         fi
 	if [ -z "$MULTIBYTEID" ]
 	then
-		echo "$CMDNAME: $MULTIBYTE is not a valid encoding name" 1>&2
-		exit 1
-	elif [ $MULTIBYTEID -gt 31 ]
-	then
-		echo "$CMDNAME: $MULTIBYTE cannot be used as a database encoding" 1>&2
+		echo "$CMDNAME: $MULTIBYTE is not a valid backend encoding name" 1>&2
 		exit 1
 	fi
 fi
diff --git a/src/bin/pg_encoding/pg_encoding.c b/src/bin/pg_encoding/pg_encoding.c
index fc0714641e1..3e2b84e0bd6 100644
--- a/src/bin/pg_encoding/pg_encoding.c
+++ b/src/bin/pg_encoding/pg_encoding.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/bin/pg_encoding/Attic/pg_encoding.c,v 1.8 2000/02/19 04:59:30 ishii Exp $
+ *	  $Header: /cvsroot/pgsql/src/bin/pg_encoding/Attic/pg_encoding.c,v 1.9 2001/09/06 04:57:29 ishii Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -15,37 +15,75 @@
 #include "miscadmin.h"
 #include "mb/pg_wchar.h"
 
+#include <ctype.h>
+
 static void usage(void);
 
 int
 main(int argc, char **argv)
 {
-	char		c;
-	char	   *p;
-	int			rtn;
+	char	*p;
+	int	enc;
+	bool	be_only = FALSE;
 
 	if (argc < 2)
 	{
 		usage();
 		exit(1);
 	}
-	p = argv[1];
-	while ((c = *p++))
+	
+	if (strcmp(argv[1], "-b")==0)
 	{
-		if (c < '0' || c > '9')
+		if (argc < 3)
 		{
-			rtn = pg_char_to_encoding(argv[1]);
-			if (rtn >= 0)
-				printf("%d\n", rtn);
-			exit(0);
+			usage();
+			exit(1);
 		}
+		be_only = TRUE;
+		p = argv[2];
 	}
-	printf("%s\n", pg_encoding_to_char(atoi(argv[1])));
-	exit(0);
+	else
+		p = argv[1];
+	
+	if (p && *p && isdigit((unsigned char) *p))
+	{
+		/*
+		 * Encoding number to name
+		 */
+		char	*name;
+		
+		enc = atoi(p);
+
+		if ((name = (char *) pg_encoding_to_char(enc)))
+		{
+			if (be_only && pg_valid_server_encoding(name) < 0)
+				exit(0);
+			printf("%s\n", name);
+		}
+		exit(0);
+	}
+	else if (p && *p)
+	{
+		/*
+		 * Encoding name to encoding number
+		 */
+		if ((enc = pg_char_to_encoding(p)) >= 0)
+		{
+			if (be_only && pg_valid_server_encoding(p) < 0)
+				exit(0);
+			printf("%d\n", enc);
+		}
+		exit(0);
+	}	
+	exit(1);
 }
 
 static void
 usage()
 {
-	fprintf(stderr, "\nUsage: pg_encoding encoding_name | encoding_number\n\n");
+	fprintf(stderr, 
+	    "\nUsage: pg_encoding [options] encoding_name | encoding_number\n\n"
+	    "options:"
+	    "         -b        check if encoding is valid for backend\n\n"
+	);
 }
diff --git a/src/bin/scripts/createdb b/src/bin/scripts/createdb
index fbb39e00666..2c13d963249 100644
--- a/src/bin/scripts/createdb
+++ b/src/bin/scripts/createdb
@@ -12,7 +12,7 @@
 #
 #
 # IDENTIFICATION
-#    $Header: /cvsroot/pgsql/src/bin/scripts/Attic/createdb,v 1.15 2001/03/27 05:47:13 ishii Exp $
+#    $Header: /cvsroot/pgsql/src/bin/scripts/Attic/createdb,v 1.16 2001/09/06 04:57:29 ishii Exp $
 #
 #-------------------------------------------------------------------------
 
@@ -145,10 +145,6 @@ then
 	then
 		echo "$CMDNAME: \"$MB\" is not a valid encoding name" 1>&2
 		exit 1
-	elif [ $mbcode -gt 31 ]
-	then
-		echo "$CMDNAME: $MB cannot be used as a database encoding" 1>&2
-		exit 1
 	fi
 fi
 
diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h
index fbea1d09a5c..fc1a5a73b67 100644
--- a/src/include/catalog/pg_proc.h
+++ b/src/include/catalog/pg_proc.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: pg_proc.h,v 1.208 2001/09/06 03:22:42 momjian Exp $
+ * $Id: pg_proc.h,v 1.209 2001/09/06 04:57:29 ishii Exp $
  *
  * NOTES
  *	  The script catalog/genbki.sh reads this file and generates .bki
@@ -2136,6 +2136,8 @@ DATA(insert OID =  937 (  substring    PGUID 14 f t t t 2 f 25 "25 23" 100 0 0 1
 DESCR("return portion of string");
 
 /* for multi-byte support */
+
+/* old encoding names - back compatibility only */
 DATA(insert OID = 1039 (  getdatabaseencoding	   PGUID 12 f t f t 0 f 19 "0" 100 0 0 100	getdatabaseencoding - ));
 DESCR("encoding name of current database");
 
diff --git a/src/include/mb/pg_wchar.h b/src/include/mb/pg_wchar.h
index 6df58708f67..ed1a6e19093 100644
--- a/src/include/mb/pg_wchar.h
+++ b/src/include/mb/pg_wchar.h
@@ -1,4 +1,4 @@
-/* $Id: pg_wchar.h,v 1.28 2001/08/15 07:07:40 ishii Exp $ */
+/* $Id: pg_wchar.h,v 1.29 2001/09/06 04:57:29 ishii Exp $ */
 
 #ifndef PG_WCHAR_H
 #define PG_WCHAR_H
@@ -12,30 +12,9 @@
 #define pfree free
 #endif
 
-#define SQL_ASCII 0				/* SQL/ASCII */
-#define EUC_JP 1				/* EUC for Japanese */
-#define EUC_CN 2				/* EUC for Chinese */
-#define EUC_KR 3				/* EUC for Korean */
-#define EUC_TW 4				/* EUC for Taiwan */
-#define UNICODE 5				/* Unicode UTF-8 */
-#define MULE_INTERNAL 6			/* Mule internal code */
-#define LATIN1 7				/* ISO-8859 Latin 1 */
-#define LATIN2 8				/* ISO-8859 Latin 2 */
-#define LATIN3 9				/* ISO-8859 Latin 3 */
-#define LATIN4 10				/* ISO-8859 Latin 4 */
-#define LATIN5 11				/* ISO-8859 Latin 5 */
-#define LATIN6 12				/* ISO-8859 Latin 6 */
-#define LATIN7 13				/* ISO-8859 Latin 7 */
-#define LATIN8 14				/* ISO-8859 Latin 8 */
-#define LATIN9 15				/* ISO-8859 Latin 9 */
-#define KOI8   16				/* KOI8-R/U */
-#define WIN    17				/* windows-1251 */
-#define ALT    18				/* Alternativny Variant (MS-DOS CP866) */
-/* followings are for client encoding only */
-#define SJIS 32					/* Shift JIS */
-#define BIG5 33					/* Big5 */
-#define WIN1250  34				/* windows-1250 */
-
+/*
+ * The pg_wchar 
+ */
 #ifdef MULTIBYTE
 typedef unsigned int pg_wchar;
 
@@ -67,12 +46,12 @@ typedef unsigned int pg_wchar;
 #define LC_ISO8859_5	0x8d	/* ISO8859 Latin 5 */
 #define LC_JISX0201K	0x89	/* Japanese 1 byte kana */
 #define LC_JISX0201R	0x8a	/* Japanese 1 byte Roman */
-#define LC_KOI8_R	0x8c		/* Cyrillic KOI8-R */
-#define LC_KOI8_U	0x8c		/* Cyrillic KOI8-U */
+#define LC_KOI8_R	0x8c	/* Cyrillic KOI8-R */
+#define LC_KOI8_U	0x8c	/* Cyrillic KOI8-U */
 #define LC_GB2312_80	0x91	/* Chinese */
-#define LC_JISX0208 0x92		/* Japanese Kanji */
-#define LC_KS5601	0x93		/* Korean */
-#define LC_JISX0212 0x94		/* Japanese Kanji (JISX0212) */
+#define LC_JISX0208	0x92	/* Japanese Kanji */
+#define LC_KS5601	0x93	/* Korean */
+#define LC_JISX0212	0x94	/* Japanese Kanji (JISX0212) */
 #define LC_CNS11643_1	0x95	/* CNS 11643-1992 Plane 1 */
 #define LC_CNS11643_2	0x96	/* CNS 11643-1992 Plane 2 */
 #define LC_CNS11643_3	0xf6	/* CNS 11643-1992 Plane 3 */
@@ -81,27 +60,129 @@ typedef unsigned int pg_wchar;
 #define LC_CNS11643_6	0xf9	/* CNS 11643-1992 Plane 6 */
 #define LC_CNS11643_7	0xfa	/* CNS 11643-1992 Plane 7 */
 
+/*
+ * Encoding numeral identificators
+ *
+ * WARNING: the order of this table must be same as order 
+ *          in the pg_enconv[] (mb/conv.c) and pg_enc2name[] (mb/names.c) array!
+ *
+ *          If you add some encoding don'y forget check  
+ *          PG_ENCODING_[BE|FE]_LAST macros.
+ *
+ *	    The PG_SQL_ASCII is default encoding and must be = 0.
+ */
+typedef enum pg_enc
+{
+	PG_SQL_ASCII = 0,			/* SQL/ASCII */
+	PG_EUC_JP,				/* EUC for Japanese */
+	PG_EUC_CN,				/* EUC for Chinese */
+	PG_EUC_KR,				/* EUC for Korean */
+	PG_EUC_TW,				/* EUC for Taiwan */
+	PG_UTF8,				/* Unicode UTF-8 */
+	PG_MULE_INTERNAL,			/* Mule internal code */
+	PG_LATIN1,				/* ISO-8859 Latin 1 */
+	PG_LATIN2,				/* ISO-8859 Latin 2 */
+	PG_LATIN3,				/* ISO-8859 Latin 3 */
+	PG_LATIN4,				/* ISO-8859 Latin 4 */
+	PG_LATIN5,				/* ISO-8859 Latin 5 */
+	PG_KOI8R,				/* KOI8-R */
+	PG_WIN1251,				/* windows-1251 (was: WIN) */
+	PG_ALT,					/* (MS-DOS CP866) */
+	
+	/* followings are for client encoding only */
+	PG_SJIS,				/* Shift JIS */
+	PG_BIG5,				/* Big5 */
+	PG_WIN1250,				/* windows-1250 */
+
+	_PG_LAST_ENCODING_			/* mark only */
+
+} pg_enc;
+
+#define PG_ENCODING_BE_LAST	PG_ALT
+#define PG_ENCODING_FE_LAST	PG_WIN1250
+
+
 #ifdef MULTIBYTE
-typedef struct
+
+/*
+ * Please use these tests before access to pg_encconv_tbl[] 
+ * or to other places...
+ */
+#define PG_VALID_BE_ENCODING(_enc) \
+		((_enc) >= 0 && (_enc) <= PG_ENCODING_BE_LAST) 
+ 
+#define PG_ENCODING_IS_CLIEN_ONLY(_enc) \
+		(((_enc) > PG_ENCODING_BE_LAST && (_enc) <= PG_ENCODING_FE_LAST)
+
+#define PG_VALID_ENCODING(_enc) \
+		((_enc) >= 0 && (_enc) < _PG_LAST_ENCODING_)
+
+/* On FE are possible all encodings 
+ */
+#define PG_VALID_FE_ENCODING(_enc) 	PG_VALID_ENCODING(_enc)
+
+/* 
+ * Encoding names with all aliases
+ */
+typedef struct pg_encname
+{
+	char	*name;
+	pg_enc	encoding;
+} pg_encname;
+
+extern pg_encname	pg_encname_tbl[];
+extern unsigned	int	pg_encname_tbl_sz;
+
+/*
+ * Careful:
+ *
+ * if (PG_VALID_ENCODING(encoding))
+ *		pg_enc2name_tbl[ encoding ];
+ */
+typedef struct pg_enc2name
 {
-	int			encoding;		/* encoding symbol value */
-	char	   *name;			/* encoding name */
-	int			is_client_only; /* 0: server/client both supported 1:
-								 * client only */
-	void		(*to_mic) ();	/* client encoding to MIC */
-	void		(*from_mic) (); /* MIC to client encoding */
-	void		(*to_unicode) ();		/* client encoding to UTF-8 */
-	void		(*from_unicode) ();		/* UTF-8 to client encoding */
-}			pg_encoding_conv_tbl;
-
-extern pg_encoding_conv_tbl pg_conv_tbl[];
+	char	*name;
+	pg_enc	encoding;
+} pg_enc2name;
+
+extern pg_enc2name	pg_enc2name_tbl[];
+
+extern pg_encname	*pg_char_to_encname_struct(const char *name);
 
+extern int		pg_char_to_encoding(const char *s);
+extern const char	*pg_encoding_to_char(int encoding);
+
+/*
+ * The backend encoding conversion routines
+ * Careful:
+ *
+ *	if (PG_VALID_ENCODING(enc))
+ *		pg_encconv_tbl[ enc ]->foo
+ */
+#ifndef FRONTEND
+typedef struct pg_enconv
+{
+	pg_enc		encoding;		/* encoding identificator */
+	void		(*to_mic) ();		/* client encoding to MIC */
+	void		(*from_mic) (); 	/* MIC to client encoding */
+	void		(*to_unicode) ();	/* client encoding to UTF-8 */
+	void		(*from_unicode) ();	/* UTF-8 to client encoding */
+} pg_enconv;
+
+extern pg_enconv pg_enconv_tbl[];
+extern pg_enconv *pg_get_enconv_by_encoding(int encoding);
+
+#endif	/* FRONTEND */
+
+/*
+ * pg_wchar stuff
+ */
 typedef struct
 {
-	int			(*mb2wchar_with_len) ();		/* convert a multi-byte
-												 * string to a wchar */
-	int			(*mblen) ();	/* returns the length of a multi-byte word */
-}			pg_wchar_tbl;
+	int		(*mb2wchar_with_len) ();	/* convert a multi-byte	
+							 * string to a wchar */
+	int		(*mblen) ();			/* returns the length of a multi-byte word */
+} pg_wchar_tbl;
 
 extern pg_wchar_tbl pg_wchar_table[];
 
@@ -110,25 +191,25 @@ extern pg_wchar_tbl pg_wchar_table[];
  */
 typedef struct
 {
-	unsigned int utf;			/* UTF-8 */
-	unsigned int code;			/* local code */
-}			pg_utf_to_local;
+	unsigned int	utf;			/* UTF-8 */
+	unsigned int	code;			/* local code */
+} pg_utf_to_local;
 
 /*
  * local code to UTF-8 conversion map
  */
 typedef struct
 {
-	unsigned int code;			/* local code */
-	unsigned int utf;			/* UTF-8 */
-}			pg_local_to_utf;
+	unsigned int	code;			/* local code */
+	unsigned int	utf;			/* UTF-8 */
+} pg_local_to_utf;
 
 extern int	pg_mb2wchar(const unsigned char *, pg_wchar *);
 extern int	pg_mb2wchar_with_len(const unsigned char *, pg_wchar *, int);
 extern int	pg_char_and_wchar_strcmp(const char *, const pg_wchar *);
 extern int	pg_wchar_strncmp(const pg_wchar *, const pg_wchar *, size_t);
 extern int	pg_char_and_wchar_strncmp(const char *, const pg_wchar *, size_t);
-extern size_t pg_wchar_strlen(const pg_wchar *);
+extern size_t	pg_wchar_strlen(const pg_wchar *);
 extern int	pg_mblen(const unsigned char *);
 extern int	pg_encoding_mblen(int, const unsigned char *);
 extern int	pg_mule_mblen(const unsigned char *);
@@ -137,25 +218,25 @@ extern int	pg_mbstrlen(const unsigned char *);
 extern int	pg_mbstrlen_with_len(const unsigned char *, int);
 extern int	pg_mbcliplen(const unsigned char *, int, int);
 extern int	pg_mbcharcliplen(const unsigned char *, int, int);
-extern pg_encoding_conv_tbl *pg_get_encent_by_encoding(int);
-extern int	pg_set_client_encoding(int);
-extern int	pg_get_client_encoding(void);
-extern unsigned char *pg_client_to_server(unsigned char *, int);
-extern unsigned char *pg_server_to_client(unsigned char *, int);
-extern int	pg_valid_client_encoding(const char *);
-extern pg_encoding_conv_tbl *pg_get_enc_ent(int);
+
+extern int		pg_set_client_encoding(int);
+extern int		pg_get_client_encoding(void);
+extern const char	*pg_get_client_encoding_name(void);
+
+extern void		SetDatabaseEncoding(int);
+extern int		GetDatabaseEncoding(void);
+extern const char	*GetDatabaseEncodingName(void);
+
+extern int	pg_valid_client_encoding(const char *name);
+extern int	pg_valid_server_encoding(const char *name);
+
 extern int	pg_utf_mblen(const unsigned char *);
 extern int	pg_find_encoding_converters(int, int, void (**)(), void (**)());
 extern unsigned char *pg_do_encoding_conversion(unsigned char *, int, void (*)(), void (*)());
 
-/* internally-used versions of functions.  The PG_xxx forms of these
- * functions have fmgr-compatible interfaves.
- */
-extern const char *pg_encoding_to_char(int);
-extern int	pg_char_to_encoding(const char *);
+extern unsigned char *pg_client_to_server(unsigned char *, int);
+extern unsigned char *pg_server_to_client(unsigned char *, int);
 
-extern int	GetDatabaseEncoding(void);
-extern void SetDatabaseEncoding(int);
 extern unsigned short BIG5toCNS(unsigned short, unsigned char *);
 extern unsigned short CNStoBIG5(unsigned short, unsigned char);
 
diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h
index bb91e1166e1..cb3a0a8913b 100644
--- a/src/include/utils/builtins.h
+++ b/src/include/utils/builtins.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: builtins.h,v 1.162 2001/08/15 07:07:40 ishii Exp $
+ * $Id: builtins.h,v 1.163 2001/09/06 04:57:29 ishii Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -579,8 +579,11 @@ extern Datum RI_FKey_setdefault_upd(PG_FUNCTION_ARGS);
  * since pg_proc.h has references to them.
  */
 extern Datum getdatabaseencoding(PG_FUNCTION_ARGS);
+extern Datum database_character_set(PG_FUNCTION_ARGS);
 extern Datum PG_encoding_to_char(PG_FUNCTION_ARGS);
 extern Datum PG_char_to_encoding(PG_FUNCTION_ARGS);
+extern Datum PG_character_set_name(PG_FUNCTION_ARGS);
+extern Datum PG_character_set_id(PG_FUNCTION_ARGS);
 extern Datum pg_convert(PG_FUNCTION_ARGS);
 extern Datum pg_convert2(PG_FUNCTION_ARGS);
 
diff --git a/src/interfaces/libpq/Makefile b/src/interfaces/libpq/Makefile
index ce2bd7058f0..a13292c70f3 100644
--- a/src/interfaces/libpq/Makefile
+++ b/src/interfaces/libpq/Makefile
@@ -4,7 +4,7 @@
 #
 # Copyright (c) 1994, Regents of the University of California
 #
-# $Header: /cvsroot/pgsql/src/interfaces/libpq/Makefile,v 1.56 2001/08/28 14:20:28 petere Exp $
+# $Header: /cvsroot/pgsql/src/interfaces/libpq/Makefile,v 1.57 2001/09/06 04:57:30 ishii Exp $
 #
 #-------------------------------------------------------------------------
 
@@ -24,7 +24,7 @@ OBJS= fe-auth.o fe-connect.o fe-exec.o fe-misc.o fe-print.o fe-lobj.o \
       $(INET_ATON) $(SNPRINTF) $(STRERROR)
 
 ifdef MULTIBYTE
-OBJS+= wchar.o
+OBJS+= wchar.o encnames.o
 endif
 
 # Add libraries that libpq depends (or might depend) on into the
@@ -67,6 +67,8 @@ strerror.c: $(backend_src)/port/strerror.c
 ifdef MULTIBYTE
 wchar.c : % : $(backend_src)/utils/mb/%
 	rm -f $@ && $(LN_S) $< .
+encnames.c : % : $(backend_src)/utils/mb/%
+	rm -f $@ && $(LN_S) $< .
 endif
 
 
@@ -82,5 +84,5 @@ uninstall: uninstall-lib
 	rm -f $(DESTDIR)$(includedir)/libpq-fe.h $(DESTDIR)$(includedir_internal)/libpq-int.h $(includedir_internal)/pqexpbuffer.h
 
 clean distclean maintainer-clean: clean-lib
-	rm -f $(OBJS) dllist.c md5.c md5.h wchar.c
+	rm -f $(OBJS) dllist.c md5.c md5.h wchar.c encnames.c
 	rm -f $(OBJS) inet_aton.c snprintf.c strerror.c
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 9301d1b1cfb..8829c69e32b 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.176 2001/08/21 20:39:52 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.177 2001/09/06 04:57:30 ishii Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1659,7 +1659,7 @@ keep_going:						/* We will come back to here until there
 					/* set client encoding in pg_conn struct */
 					encoding = PQgetvalue(res, 0, 0);
 					if (!encoding)		/* this should not happen */
-						conn->client_encoding = SQL_ASCII;
+						conn->client_encoding = PG_SQL_ASCII;
 					else
 						conn->client_encoding = pg_char_to_encoding(encoding);
 					PQclear(res);
@@ -2758,7 +2758,8 @@ PQsetClientEncoding(PGconn *conn, const char *encoding)
 	return (status);
 }
 
-#else
+#else	/* without multibytle support */
+
 int
 PQsetClientEncoding(PGconn *conn, const char *encoding)
 {
@@ -2832,73 +2833,3 @@ defaultNoticeProcessor(void *arg, const char *message)
 	/* Note: we expect the supplied string to end with a newline already. */
 	fprintf(stderr, "%s", message);
 }
-
-#ifdef MULTIBYTE
-/*
- * convert an encoding string to encoding symbol value.
- * case is ignored.
- * if there's no valid encoding, returns -1
- */
-
-typedef struct
-{
-	int			encoding;		/* encoding symbol value */
-	char	   *name;			/* encoding string */
-}			PQ_encoding_conv_tbl;
-
-static PQ_encoding_conv_tbl pq_conv_tbl[] = {
-	{SQL_ASCII, "SQL_ASCII"},
-	{EUC_JP, "EUC_JP"},
-	{EUC_CN, "EUC_CN"},
-	{EUC_KR, "EUC_KR"},
-	{EUC_TW, "EUC_TW"},
-	{UNICODE, "UNICODE"},
-	{MULE_INTERNAL, "MULE_INTERNAL"},
-	{LATIN1, "LATIN1"},
-	{LATIN2, "LATIN2"},
-	{LATIN3, "LATIN3"},
-	{LATIN4, "LATIN4"},
-	{LATIN5, "LATIN5"},
-	{KOI8, "KOI8"},
-	{WIN, "WIN"},
-	{ALT, "ALT"},
-	{SJIS, "SJIS"},
-	{BIG5, "BIG5"},
-	{WIN1250, "WIN1250"},
-	{-1, ""}
-};
-
-int
-pg_char_to_encoding(const char *s)
-{
-	PQ_encoding_conv_tbl *p = pq_conv_tbl;
-
-	if (!s)
-		return (-1);
-
-	for (; p->encoding >= 0; p++)
-	{
-		if (!strcasecmp(s, p->name))
-			break;
-	}
-	return (p->encoding);
-}
-
-/*
- * convert encoding symbol to encoding char.
- * if there's no valid encoding symbol, returns ""
- */
-const char *
-pg_encoding_to_char(int encoding)
-{
-	PQ_encoding_conv_tbl *p = pq_conv_tbl;
-
-	for (; p->encoding >= 0; p++)
-	{
-		if (p->encoding == encoding)
-			return (p->name);
-	}
-	return ("");
-}
-
-#endif
diff --git a/src/interfaces/libpq/fe-misc.c b/src/interfaces/libpq/fe-misc.c
index 411b839be47..a98b4c745cf 100644
--- a/src/interfaces/libpq/fe-misc.c
+++ b/src/interfaces/libpq/fe-misc.c
@@ -25,7 +25,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.55 2001/09/06 02:52:00 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.56 2001/09/06 04:57:30 ishii Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -809,7 +809,7 @@ int
 PQenv2encoding(void)
 {
 	char	   *str;
-	int			encoding = SQL_ASCII;
+	int			encoding = PG_SQL_ASCII;
 
 	str = getenv("PGCLIENTENCODING");
 	if (str && *str != '\0')
diff --git a/src/interfaces/odbc/multibyte.c b/src/interfaces/odbc/multibyte.c
index a9c9c2f83bb..dec9a33a2e8 100644
--- a/src/interfaces/odbc/multibyte.c
+++ b/src/interfaces/odbc/multibyte.c
@@ -70,12 +70,18 @@ multibyte_init(void)
 unsigned char *
 check_client_encoding(unsigned char *str)
 {
-	if (strstr(str, "%27SJIS%27") || strstr(str, "'SJIS'") || strstr(str, "'sjis'"))
+	if (strstr(str, "%27SJIS%27") || 
+	    strstr(str, "'SJIS'") || 
+	    strstr(str, "'sjis'") ||
+	    strstr(str, "'Shift_JIS'"))
 	{
 		multibyte_client_encoding = SJIS;
 		return ("SJIS");
 	}
-	if (strstr(str, "%27BIG5%27") || strstr(str, "'BIG5'") || strstr(str, "'big5'"))
+	if (strstr(str, "%27BIG5%27") || 
+	    strstr(str, "'BIG5'") || 
+	    strstr(str, "'big5'")
+	    strstr(str, "'Big5'"))
 	{
 		multibyte_client_encoding = BIG5;
 		return ("BIG5");
-- 
GitLab