Re: cal(1): faulty weekday alignment for hungarian locale

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



There were a couple of issues with the previous patch:

 * It not build on platforms not supporting WIDE_CHAR
 * It didn't update the test cases appropriately

This new patches fixes the above and also the following:

 * Truncation of widechar strings was not done correctly
   and could produce too wide or too norrow output.
 * Non printable characters in the locale database would
   both be displayed and break the alignment logic.

cheers,
Pádraig.
>From ef2e062df3b5e1bffacc09b7bec002733b147a75 Mon Sep 17 00:00:00 2001
From: =?utf-8?q?P=C3=A1draig=20Brady?= <P@xxxxxxxxxxxxxx>
Date: Fri, 4 Jan 2008 10:44:53 +0000
Subject: [PATCH] Fix weekday alignment for certain locales

For example this had too much padding: LANG=zh_CN.utf8 cal -j
while this had too little padding:     LANG=hu_HU.utf8 cal
This had invalid chars:                LANG=li_BE.utf8 cal
This had too few chars:                LANG=si_LK.utf8 cal

Note some locales may display with slightly worse alignment
(fa_IR.utf8 for example), but that is only because the terminal
is not merging the combining characters. This happens on
gnome-terminal-2.18.3-1.fc8 at least.

Signed-off-by: Pádraig Brady <P@xxxxxxxxxxxxxx>
---
 misc-utils/cal.c        |  124 +++++++++++++++++++++++++++-------------------
 tests/expected/ts-cal-1 |    4 +-
 tests/expected/ts-cal-3 |    8 ++--
 tests/expected/ts-cal-y |   36 +++++++-------
 4 files changed, 97 insertions(+), 75 deletions(-)

diff --git a/misc-utils/cal.c b/misc-utils/cal.c
index fd2700f..cfd744a 100644
--- a/misc-utils/cal.c
+++ b/misc-utils/cal.c
@@ -241,7 +241,7 @@ struct fmt_st
 };
 
 char * ascii_day(char *, int);
-void center_str(const char* src, char* dest, size_t dest_size, int width);
+int center_str(const char* src, char* dest, size_t dest_size, int width);
 void center(const char *, int, int);
 void day_array(int, int, int, int *);
 int day_in_week(int, int, int);
@@ -387,25 +387,11 @@ main(int argc, char **argv) {
 	exit(0);
 }
 
-#ifndef HAVE_WIDECHAR
-static char *eos(char *s) {
-	while (s && *s)
-		s++;
-	return s;
-}
-#endif
-
 void headers_init(void)
 {
   int i, wd;
-#ifdef HAVE_WIDECHAR
-  wchar_t day_headings_wc[22],j_day_headings_wc[29];
   char *cur_dh = day_headings, *cur_j_dh = j_day_headings;
 
-  wcscpy(day_headings_wc, L"");
-  wcscpy(j_day_headings_wc, L"");
-#endif
-
   strcpy(day_headings,"");
   strcpy(j_day_headings,"");
 
@@ -418,27 +404,22 @@ void headers_init(void)
   for(i = 0 ; i < 7 ; i++ ) {
      ssize_t space_left;
      wd = (i + week1stday) % 7;
-#ifdef HAVE_WIDECHAR
-     swprintf(day_headings_wc, SIZE(day_headings_wc), L"%1.2s ", weekday(wd));
-     swprintf(j_day_headings_wc, SIZE(j_day_headings_wc), L"%3.3s ", weekday(wd));
 
+     if (i)
+        strcat(cur_dh++, " ");
      space_left = sizeof(day_headings) - (cur_dh - day_headings);
-     if(space_left <= 0)
-	     break;
-     cur_dh += wcstombs(cur_dh, day_headings_wc, space_left);
-
-     space_left = sizeof(j_day_headings)-(cur_j_dh-j_day_headings);
-     if(space_left <= 0)
-	     break;
-     cur_j_dh +=  wcstombs(cur_j_dh,j_day_headings_wc, space_left);
-#else
-     sprintf(eos(day_headings), "%2.2s ", weekday(wd));
-     sprintf(eos(j_day_headings), "%3.3s ", weekday(wd));
-#endif
+     if(space_left <= 2)
+        break;
+     cur_dh += center_str(weekday(wd), cur_dh, space_left, 2);
+
+     if (i)
+        strcat(cur_j_dh++, " ");
+     space_left = sizeof(j_day_headings) - (cur_j_dh - j_day_headings);
+     if(space_left <= 3)
+        break;
+     cur_j_dh += center_str(weekday(wd), cur_j_dh, space_left, 3);
   }
 
-  trim_trailing_spaces(day_headings);
-  trim_trailing_spaces(j_day_headings);
 #undef weekday
 
   for (i = 0; i < 12; i++) {
@@ -452,7 +433,7 @@ void headers_init(void)
 
 void
 do_monthly(int day, int month, int year, struct fmt_st *out) {
-	int col, row, len, days[MAXDAYS];
+	int col, row, days[MAXDAYS];
 	char *p, lineout[FMT_ST_CHARS];
 	int width = (julian ? J_WEEK_LEN : WEEK_LEN) - 1;
 
@@ -464,8 +445,8 @@ do_monthly(int day, int month, int year, struct fmt_st *out) {
 	 * Basque the translation should be: "%2$dko %1$s", and
 	 * the Vietnamese should be "%s na(m %d", etc.
 	 */
-	len = sprintf(lineout, _("%s %d"), full_month[month - 1], year);
-	center_str(lineout, out->s[0], SIZE(out->s[0]), width);
+	(void) sprintf(lineout, _("%s %d"), full_month[month - 1], year);
+	(void) center_str(lineout, out->s[0], SIZE(out->s[0]), width);
 
 	sprintf(out->s[1],"%s",
 		julian ? j_day_headings : day_headings);
@@ -752,11 +733,49 @@ trim_trailing_spaces(s)
 	*p = '\0';
 }
 
+#ifdef HAVE_WIDECHAR
+/* replace non printable chars.
+ * return 1 if replacement made, 0 otherwise */
+int wc_ensure_printable(wchar_t* wchars)
+{
+	int replaced=0;
+	wchar_t* wc = wchars;
+	while (*wc) {
+		if (!iswprint((wint_t) *wc)) {
+			*wc=L'\uFFFD';
+			replaced=1;
+		}
+		wc++;
+	}
+	return replaced;
+}
+
+/* truncate wchar string to width cells.
+ * returns number of cells used. */
+size_t wc_truncate(wchar_t* wchars, size_t width, size_t minchars)
+{
+	int wc=0;
+	int cells=0;
+	while (*(wchars+wc)) {
+		cells = wcswidth(wchars, wc+1);
+		if (cells > width) {
+			if (wc >= minchars) {
+				break;
+			}
+		}
+		wc++;
+	}
+	wchars[wc]=L'\0';
+	return cells;
+}
+#endif
+
 /*
  * Center string, handling multibyte characters appropriately.
  * In addition if the string is too large for the width it's truncated.
+ * The number of trailing spaces may be 1 less than the number of leading spaces.
  */
-void
+int
 center_str(const char* src, char* dest, size_t dest_size, int width)
 {
 #ifdef HAVE_WIDECHAR
@@ -764,36 +783,39 @@ center_str(const char* src, char* dest, size_t dest_size, int width)
 #endif
 	char str[FMT_ST_CHARS];
 	const char* str_to_print=src;
-	int len, spaces, wide_char_enabled=0;
+	int used, spaces, wc_conversion=0, wc_enabled=0;
 
-	len = strlen(src);
+	used = strlen(src); /* first approximation */
 
 #ifdef HAVE_WIDECHAR
-	if (mbstowcs(str_wc, src, FMT_ST_CHARS) > 0) {
-		wide_char_enabled = 1;
-		len = wcswidth(str_wc, SIZE(str_wc));
+	if (mbstowcs(str_wc, src, SIZE(str_wc)) > 0) {
+		str_wc[SIZE(str_wc)-1]=L'\0';
+		wc_enabled=1;
+		wc_conversion = wc_ensure_printable(str_wc);
+		used = wcswidth(str_wc, SIZE(str_wc));
 	}
 #endif
-	if (len > width) {
+
+	if (wc_conversion || used > width) {
 		str_to_print=str;
-		if (wide_char_enabled) {
+		if (wc_enabled) {
 #ifdef HAVE_WIDECHAR
-			str_wc[width]=L'\0';
+			used = wc_truncate(str_wc, width, 1);
 			wcstombs(str, str_wc, SIZE(str));
 #endif
 		} else {
-			strncpy(str, src, SIZE(str));
+			memcpy(str, src, width);
 			str[width]='\0';
 		}
 	}
 
-	spaces = width - len;
+	spaces = width - used;
 	spaces = ( spaces < 0 ? 0 : spaces );
 
-	snprintf(dest, dest_size, "%*s%s%*s",
-		spaces / 2, "",
-                str_to_print,
-		spaces / 2 + spaces % 2, "" );
+	return snprintf(dest, dest_size, "%*s%s%*s",
+		spaces / 2 + spaces % 2, "",
+		str_to_print,
+		spaces / 2, "" );
 }
 
 void
@@ -803,7 +825,7 @@ center(str, len, separate)
 	int separate;
 {
 	char lineout[FMT_ST_CHARS];
-	center_str(str, lineout, SIZE(lineout), len);
+	(void) center_str(str, lineout, SIZE(lineout), len);
 	fputs(lineout, stdout);
 	if (separate)
 		(void)printf("%*s", separate, "");
diff --git a/tests/expected/ts-cal-1 b/tests/expected/ts-cal-1
index 882d7e9..f657875 100644
--- a/tests/expected/ts-cal-1
+++ b/tests/expected/ts-cal-1
@@ -18,7 +18,7 @@ Su Mo Tu We Th Fr Sa
 24 25 26 27 28 29 30
 
 Julian - Monday-based week
-      September 2006       
+       September 2006      
 Mon Tue Wed Thu Fri Sat Sun
                 244 245 246
 247 248 249 250 251 252 253
@@ -27,7 +27,7 @@ Mon Tue Wed Thu Fri Sat Sun
 268 269 270 271 272 273
 
 Julian - Sunday-based week
-      September 2006       
+       September 2006      
 Sun Mon Tue Wed Thu Fri Sat
                     244 245
 246 247 248 249 250 251 252
diff --git a/tests/expected/ts-cal-3 b/tests/expected/ts-cal-3
index 0648916..ba9657d 100644
--- a/tests/expected/ts-cal-3
+++ b/tests/expected/ts-cal-3
@@ -1,6 +1,6 @@
 
 Gregorian - Monday-based week
-    August 2006          September 2006         October 2006    
+     August 2006         September 2006         October 2006    
 Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su
     1  2  3  4  5  6               1  2  3                     1
  7  8  9 10 11 12 13   4  5  6  7  8  9 10   2  3  4  5  6  7  8
@@ -9,7 +9,7 @@ Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su
 28 29 30 31           25 26 27 28 29 30     23 24 25 26 27 28 29
                                             30 31               
 Gregorian - Sunday-based week
-    August 2006          September 2006         October 2006    
+     August 2006         September 2006         October 2006    
 Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa
        1  2  3  4  5                  1  2   1  2  3  4  5  6  7
  6  7  8  9 10 11 12   3  4  5  6  7  8  9   8  9 10 11 12 13 14
@@ -18,7 +18,7 @@ Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa
 27 28 29 30 31        24 25 26 27 28 29 30  29 30 31            
                                                                 
 Julian - Monday-based week
-        August 2006                September 2006                October 2006        
+        August 2006                 September 2006                October 2006       
 Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun
     213 214 215 216 217 218                  244 245 246                          274
 219 220 221 222 223 224 225  247 248 249 250 251 252 253  275 276 277 278 279 280 281
@@ -27,7 +27,7 @@ Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sa
 240 241 242 243              268 269 270 271 272 273      296 297 298 299 300 301 302
                                                           303 304                    
 Julian - Sunday-based week
-        August 2006                September 2006                October 2006        
+        August 2006                 September 2006                October 2006       
 Sun Mon Tue Wed Thu Fri Sat  Sun Mon Tue Wed Thu Fri Sat  Sun Mon Tue Wed Thu Fri Sat
         213 214 215 216 217                      244 245  274 275 276 277 278 279 280
 218 219 220 221 222 223 224  246 247 248 249 250 251 252  281 282 283 284 285 286 287
diff --git a/tests/expected/ts-cal-y b/tests/expected/ts-cal-y
index 0bc4a2e..c7b40ff 100644
--- a/tests/expected/ts-cal-y
+++ b/tests/expected/ts-cal-y
@@ -1,8 +1,8 @@
 
 Gregorian - Monday-based week
-                               2006                                
+                                2006                               
 
-       January               February                 March        
+       January                February                March        
 Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
                    1          1  2  3  4  5          1  2  3  4  5
  2  3  4  5  6  7  8    6  7  8  9 10 11 12    6  7  8  9 10 11 12
@@ -10,7 +10,7 @@ Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
 16 17 18 19 20 21 22   20 21 22 23 24 25 26   20 21 22 23 24 25 26
 23 24 25 26 27 28 29   27 28                  27 28 29 30 31
 30 31
-        April                   May                   June         
+        April                   May                    June        
 Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
                 1  2    1  2  3  4  5  6  7             1  2  3  4
  3  4  5  6  7  8  9    8  9 10 11 12 13 14    5  6  7  8  9 10 11
@@ -18,7 +18,7 @@ Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
 17 18 19 20 21 22 23   22 23 24 25 26 27 28   19 20 21 22 23 24 25
 24 25 26 27 28 29 30   29 30 31               26 27 28 29 30
 
-        July                  August                September      
+         July                  August               September      
 Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
                 1  2       1  2  3  4  5  6                1  2  3
  3  4  5  6  7  8  9    7  8  9 10 11 12 13    4  5  6  7  8  9 10
@@ -26,7 +26,7 @@ Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
 17 18 19 20 21 22 23   21 22 23 24 25 26 27   18 19 20 21 22 23 24
 24 25 26 27 28 29 30   28 29 30 31            25 26 27 28 29 30
 31
-       October               November               December       
+       October                November               December      
 Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
                    1          1  2  3  4  5                1  2  3
  2  3  4  5  6  7  8    6  7  8  9 10 11 12    4  5  6  7  8  9 10
@@ -36,9 +36,9 @@ Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
 30 31
 
 Gregorian - Sunday-based week
-                               2006                                
+                                2006                               
 
-       January               February                 March        
+       January                February                March        
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
  1  2  3  4  5  6  7             1  2  3  4             1  2  3  4
  8  9 10 11 12 13 14    5  6  7  8  9 10 11    5  6  7  8  9 10 11
@@ -46,7 +46,7 @@ Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
 22 23 24 25 26 27 28   19 20 21 22 23 24 25   19 20 21 22 23 24 25
 29 30 31               26 27 28               26 27 28 29 30 31
 
-        April                   May                   June         
+        April                   May                    June        
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                    1       1  2  3  4  5  6                1  2  3
  2  3  4  5  6  7  8    7  8  9 10 11 12 13    4  5  6  7  8  9 10
@@ -54,7 +54,7 @@ Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
 16 17 18 19 20 21 22   21 22 23 24 25 26 27   18 19 20 21 22 23 24
 23 24 25 26 27 28 29   28 29 30 31            25 26 27 28 29 30
 30
-        July                  August                September      
+         July                  August               September      
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                    1          1  2  3  4  5                   1  2
  2  3  4  5  6  7  8    6  7  8  9 10 11 12    3  4  5  6  7  8  9
@@ -62,7 +62,7 @@ Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
 16 17 18 19 20 21 22   20 21 22 23 24 25 26   17 18 19 20 21 22 23
 23 24 25 26 27 28 29   27 28 29 30 31         24 25 26 27 28 29 30
 30 31
-       October               November               December       
+       October                November               December      
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
  1  2  3  4  5  6  7             1  2  3  4                   1  2
  8  9 10 11 12 13 14    5  6  7  8  9 10 11    3  4  5  6  7  8  9
@@ -74,7 +74,7 @@ Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
 Julian - Monday-based week
                            2006                           
 
-          January                       February          
+           January                      February          
 Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
                           1            32  33  34  35  36
   2   3   4   5   6   7   8    37  38  39  40  41  42  43
@@ -82,7 +82,7 @@ Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
  16  17  18  19  20  21  22    51  52  53  54  55  56  57
  23  24  25  26  27  28  29    58  59
  30  31
-           March                         April            
+            March                         April           
 Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
          60  61  62  63  64                        91  92
  65  66  67  68  69  70  71    93  94  95  96  97  98  99
@@ -90,7 +90,7 @@ Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
  79  80  81  82  83  84  85   107 108 109 110 111 112 113
  86  87  88  89  90           114 115 116 117 118 119 120
 
-            May                           June            
+             May                          June            
 Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
 121 122 123 124 125 126 127               152 153 154 155
 128 129 130 131 132 133 134   156 157 158 159 160 161 162
@@ -106,7 +106,7 @@ Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
 198 199 200 201 202 203 204   233 234 235 236 237 238 239
 205 206 207 208 209 210 211   240 241 242 243
 212
-         September                      October           
+          September                      October          
 Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
                 244 245 246                           274
 247 248 249 250 251 252 253   275 276 277 278 279 280 281
@@ -126,7 +126,7 @@ Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
 Julian - Sunday-based week
                            2006                           
 
-          January                       February          
+           January                      February          
 Sun Mon Tue Wed Thu Fri Sat   Sun Mon Tue Wed Thu Fri Sat
   1   2   3   4   5   6   7                32  33  34  35
   8   9  10  11  12  13  14    36  37  38  39  40  41  42
@@ -134,7 +134,7 @@ Sun Mon Tue Wed Thu Fri Sat   Sun Mon Tue Wed Thu Fri Sat
  22  23  24  25  26  27  28    50  51  52  53  54  55  56
  29  30  31                    57  58  59
 
-           March                         April            
+            March                         April           
 Sun Mon Tue Wed Thu Fri Sat   Sun Mon Tue Wed Thu Fri Sat
              60  61  62  63                            91
  64  65  66  67  68  69  70    92  93  94  95  96  97  98
@@ -142,7 +142,7 @@ Sun Mon Tue Wed Thu Fri Sat   Sun Mon Tue Wed Thu Fri Sat
  78  79  80  81  82  83  84   106 107 108 109 110 111 112
  85  86  87  88  89  90       113 114 115 116 117 118 119
                               120
-            May                           June            
+             May                          June            
 Sun Mon Tue Wed Thu Fri Sat   Sun Mon Tue Wed Thu Fri Sat
     121 122 123 124 125 126                   152 153 154
 127 128 129 130 131 132 133   155 156 157 158 159 160 161
@@ -158,7 +158,7 @@ Sun Mon Tue Wed Thu Fri Sat   Sun Mon Tue Wed Thu Fri Sat
 197 198 199 200 201 202 203   232 233 234 235 236 237 238
 204 205 206 207 208 209 210   239 240 241 242 243
 211 212
-         September                      October           
+          September                      October          
 Sun Mon Tue Wed Thu Fri Sat   Sun Mon Tue Wed Thu Fri Sat
                     244 245   274 275 276 277 278 279 280
 246 247 248 249 250 251 252   281 282 283 284 285 286 287
-- 
1.5.3.6


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux