On Mon, May 28, 2012 at 07:34:28PM +0200, Michal Kubecek wrote:
> 
> I did experiment a bit and it seems that locale (most likely LC_CTYPE,
> unfortunately the one that needs to be set for libedit to work), affects
> only output with "set list off", with "set list on", the output seems to
> be OK. So the interference might be with the code responsible for column
> formatting - and the output looks as if all non-printable characters
> (according to locale setting) were omitted.

The problem seems to be caused by sprintf() used for column alignment of
strings. Attached patch (for B2_5_Release) replaces it with simple
combination of memcpy() and memset(). This is not going to work properly
with non-8-bit encodings (UTF-16, CJK?) but neither did sprintf() with
"C" locale.

I'll post a patch for HEAD later (after some basic tests).

                                                          Michal Kubecek

From: Michal Kubecek <[email protected]>
Date: Mon, 28 May 2012 22:41:06 +0200
Subject: isql: fix output if charset is different from locale

Align strings manually instead of using sprintf() so that column
alignment works as intended even if LC_CTYPE is set to something
else than "C".

Also set only LC_CTYPE according to environment variables rather
than LC_ALL to prevent number formatting to be affected by
locale settings.
---
 src/isql/isql.epp |   24 +++++++++++++++---------
 1 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/src/isql/isql.epp b/src/isql/isql.epp
index ba0af30..d764102 100644
--- a/src/isql/isql.epp
+++ b/src/isql/isql.epp
@@ -578,7 +578,7 @@ int ISQL_main(int argc, char* argv[])
 	}
 #endif	// MU_ISQL
 
-	setlocale(LC_ALL, "");
+	setlocale(LC_CTYPE, "");
 
 	TEXT tabname[WORDLENGTH];
 	tabname[0] = '\0';
@@ -7358,6 +7358,18 @@ static bool checkSpecial(TEXT* const p, const int length, const double value)
 }
 
 
+static void align_string(char* out, unsigned clen, unsigned slen, const char* s)
+{
+	if (slen > clen)
+		slen = clen;
+	memcpy(out, s, slen);
+	if (clen > slen)
+		memset(out + slen, ' ', clen - slen);
+	out[clen] = ' ';
+	out[clen + 1] = '\0';
+}
+
+
 static SSHORT print_item(TEXT** s, XSQLVAR* var, const int length)
 {
 /**************************************
@@ -7650,10 +7662,7 @@ static SSHORT print_item(TEXT** s, XSQLVAR* var, const int length)
 			}
 			else
 			{
-				// Truncate if necessary
-				if (length < var->sqllen)
-					string[length] = '\0';
-				sprintf(p, "%-*s ", length, var->sqldata);
+				align_string(p, length, var->sqllen, var->sqldata);
 			}
 			break;
 
@@ -7746,10 +7755,7 @@ static SSHORT print_item(TEXT** s, XSQLVAR* var, const int length)
 				}
 				else
 				{
-					// Truncate if necessary
-					if (length < avary->vary_length)
-						avary->vary_length = length;
-					sprintf(p, "%-*.*s ", length, (int) avary->vary_length, avary->vary_string);
+					align_string(p, length, avary->vary_length, avary->vary_string);
 				}
 				break;
 			}
-- 
1.7.7

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
Firebird-Devel mailing list, web interface at 
https://lists.sourceforge.net/lists/listinfo/firebird-devel

Reply via email to