From 7a6c19e71a61c6645c812d8e0226ec15d5a89580 Mon Sep 17 00:00:00 2001 From: =?utf-8?q?P=C3=A1draig=20Brady?=
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
--- misc-utils/cal.c | 138 +++++++++++++++++++++++++++-------------------- tests/expected/ts-cal-1 | 4 +- tests/expected/ts-cal-3 | 8 ++-- tests/expected/ts-cal-y | 48 ++++++++-------- 4 files changed, 110 insertions(+), 88 deletions(-) diff --git a/misc-utils/cal.c b/misc-utils/cal.c index fd2700f..76313a7 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); @@ -556,7 +537,7 @@ j_yearly(int day, int year) { char *p, lineout[80]; sprintf(lineout, "%d", year); - center(lineout, J_WEEK_LEN * 2 + J_HEAD_SEP, 0); + center(lineout, J_WEEK_LEN*2 + J_HEAD_SEP - 1, 0); printf("\n\n"); for (i = 0; i < 12; i++) @@ -564,8 +545,8 @@ j_yearly(int day, int year) { memset(lineout, ' ', sizeof(lineout) - 1); lineout[sizeof(lineout) - 1] = '\0'; for (month = 0; month < 12; month += 2) { - center(full_month[month], J_WEEK_LEN, J_HEAD_SEP); - center(full_month[month + 1], J_WEEK_LEN, 0); + center(full_month[month], J_WEEK_LEN-1, J_HEAD_SEP+1); + center(full_month[month + 1], J_WEEK_LEN-1, 0); printf("\n%s%*s %s\n", j_day_headings, J_HEAD_SEP, "", j_day_headings); for (row = 0; row < 6; row++) { @@ -592,7 +573,7 @@ yearly(int day, int year) { char *p, lineout[100]; sprintf(lineout, "%d", year); - center(lineout, WEEK_LEN * 3 + HEAD_SEP * 2, 0); + center(lineout, WEEK_LEN*3 + HEAD_SEP*2 - 1, 0); printf("\n\n"); for (i = 0; i < 12; i++) @@ -600,9 +581,9 @@ yearly(int day, int year) { memset(lineout, ' ', sizeof(lineout) - 1); lineout[sizeof(lineout) - 1] = '\0'; for (month = 0; month < 12; month += 3) { - center(full_month[month], WEEK_LEN, HEAD_SEP); - center(full_month[month + 1], WEEK_LEN, HEAD_SEP); - center(full_month[month + 2], WEEK_LEN, 0); + center(full_month[month], WEEK_LEN-1, HEAD_SEP+1); + center(full_month[month + 1], WEEK_LEN-1, HEAD_SEP+1); + center(full_month[month + 2], WEEK_LEN-1, 0); printf("\n%s%*s %s%*s %s\n", day_headings, HEAD_SEP, "", day_headings, HEAD_SEP, "", day_headings); for (row = 0; row < 6; row++) { @@ -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..616d61a 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 @@ -72,9 +72,9 @@ Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 31 Julian - Monday-based week - 2006 + 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 @@ -98,7 +98,7 @@ Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun 142 143 144 145 146 147 148 170 171 172 173 174 175 176 149 150 151 177 178 179 180 181 - July August + July August Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun 182 183 213 214 215 216 217 218 184 185 186 187 188 189 190 219 220 221 222 223 224 225 @@ -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 @@ -114,7 +114,7 @@ Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun 261 262 263 264 265 266 267 289 290 291 292 293 294 295 268 269 270 271 272 273 296 297 298 299 300 301 302 303 304 - November December + November December Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun 305 306 307 308 309 335 336 337 310 311 312 313 314 315 316 338 339 340 341 342 343 344 @@ -124,9 +124,9 @@ Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun Julian - Sunday-based week - 2006 + 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 @@ -150,7 +150,7 @@ Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat 141 142 143 144 145 146 147 169 170 171 172 173 174 175 148 149 150 151 176 177 178 179 180 181 - July August + July August Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat 182 213 214 215 216 217 183 184 185 186 187 188 189 218 219 220 221 222 223 224 @@ -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 @@ -166,7 +166,7 @@ Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat 260 261 262 263 264 265 266 295 296 297 298 299 300 301 267 268 269 270 271 272 273 302 303 304 - November December + November December Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat 305 306 307 308 335 336 309 310 311 312 313 314 315 337 338 339 340 341 342 343 -- 1.5.3.6