33 *
44 * Copyright (c) 2000-2008, PostgreSQL Global Development Group
55 *
6- * $PostgreSQL: pgsql/src/bin/psql/mbprint.c,v 1.32 2008/05/08 19:11:36 momjian Exp $
6+ * $PostgreSQL: pgsql/src/bin/psql/mbprint.c,v 1.33 2008/05/09 05:25:04 tgl Exp $
77 *
88 * XXX this file does not really belong in psql/. Perhaps move to libpq?
99 * It also seems that the mbvalidate function is redundant with existing
@@ -205,12 +205,15 @@ pg_wcswidth(const unsigned char *pwcs, size_t len, int encoding)
205205 * pg_wcssize takes the given string in the given encoding and returns three
206206 * values:
207207 * result_width: Width in display characters of the longest line in string
208- * result_height: Number of newlines in display output
209- * result_format_size: Number of bytes required to store formatted representation of string
208+ * result_height: Number of lines in display output
209+ * result_format_size: Number of bytes required to store formatted
210+ *representation of string
211+ *
212+ * This MUST be kept in sync with pg_wcsformat!
210213 */
211- int
212- pg_wcssize (unsignedchar * pwcs ,size_t len ,int encoding ,int * result_width ,
213- int * result_height ,int * result_format_size )
214+ void
215+ pg_wcssize (unsignedchar * pwcs ,size_t len ,int encoding ,
216+ int * result_width , int * result_height ,int * result_format_size )
214217{
215218int w ,
216219chlen = 0 ,
@@ -241,6 +244,14 @@ pg_wcssize(unsigned char *pwcs, size_t len, int encoding, int *result_width,
241244linewidth += 2 ;
242245format_size += 2 ;
243246}
247+ else if (* pwcs == '\t' )/* Tab */
248+ {
249+ do
250+ {
251+ linewidth ++ ;
252+ format_size ++ ;
253+ }while (linewidth %8 != 0 );
254+ }
244255else if (w < 0 )/* Other control char */
245256{
246257linewidth += 4 ;
@@ -266,7 +277,7 @@ pg_wcssize(unsigned char *pwcs, size_t len, int encoding, int *result_width,
266277}
267278if (linewidth > width )
268279width = linewidth ;
269- format_size += 1 ;
280+ format_size += 1 ;/* For NUL char */
270281
271282/* Set results */
272283if (result_width )
@@ -275,14 +286,13 @@ pg_wcssize(unsigned char *pwcs, size_t len, int encoding, int *result_width,
275286* result_height = height ;
276287if (result_format_size )
277288* result_format_size = format_size ;
278-
279- return width ;
280289}
281290
282291/*
283- * Filter out unprintable characters, companion to wcs_size.
284- * Break input into lines based on \n. lineptr[i].ptr == NULL
285- *indicates the end of the array.
292+ * Format a string into one or more "struct lineptr" lines.
293+ * lines[i].ptr == NULL indicates the end of the array.
294+ *
295+ * This MUST be kept in sync with pg_wcssize!
286296 */
287297void
288298pg_wcsformat (unsignedchar * pwcs ,size_t len ,int encoding ,
@@ -309,7 +319,7 @@ pg_wcsformat(unsigned char *pwcs, size_t len, int encoding,
309319linewidth = 0 ;
310320lines ++ ;
311321count -- ;
312- if (count = =0 )
322+ if (count < =0 )
313323exit (1 );/* Screwup */
314324
315325/* make next line point to remaining memory */
@@ -346,14 +356,14 @@ pg_wcsformat(unsigned char *pwcs, size_t len, int encoding,
346356if (encoding == PG_UTF8 )
347357sprintf ((char * )ptr ,"\\u%04X" ,utf2ucs (pwcs ));
348358else
349-
359+ {
350360/*
351361 * This case cannot happen in the current code because only
352362 * UTF-8 signals multibyte control characters. But we may need
353363 * to support it at some stage
354364 */
355365sprintf ((char * )ptr ,"\\u????" );
356-
366+ }
357367ptr += 6 ;
358368linewidth += 6 ;
359369}
@@ -370,7 +380,7 @@ pg_wcsformat(unsigned char *pwcs, size_t len, int encoding,
370380lines -> width = linewidth ;
371381* ptr ++ = '\0' ;/* Terminate formatted string */
372382
373- if (count = =0 )
383+ if (count < =0 )
374384exit (1 );/* Screwup */
375385
376386(lines + 1 )-> ptr = NULL ;/* terminate line array */