Fossil SCM
Fix related to previous one: prevent to convert from UTF-8 to Unicode before writing to the (non-redirected) windows console. Strange effects could happen.
Commit
156ef9ec06a069f3791c4fba8e3972a1099bad7b
Parent
f2fc37c063af9fa…
1 file changed
+11
-2
+11
-2
| --- src/utf8.c | ||
| +++ src/utf8.c | ||
| @@ -309,10 +309,11 @@ | ||
| 309 | 309 | int fossil_utf8_to_console(const char *zUtf8, int nByte, int toStdErr){ |
| 310 | 310 | #ifdef _WIN32 |
| 311 | 311 | int nChar, written = 0; |
| 312 | 312 | wchar_t *zUnicode; /* Unicode version of zUtf8 */ |
| 313 | 313 | DWORD dummy; |
| 314 | + Blob blob; | |
| 314 | 315 | |
| 315 | 316 | static int istty[2] = { -1, -1 }; |
| 316 | 317 | if( istty[toStdErr] == -1 ){ |
| 317 | 318 | istty[toStdErr] = _isatty(toStdErr + 1) != 0; |
| 318 | 319 | } |
| @@ -319,16 +320,24 @@ | ||
| 319 | 320 | if( !istty[toStdErr] ){ |
| 320 | 321 | /* stdout/stderr is not a console. */ |
| 321 | 322 | return -1; |
| 322 | 323 | } |
| 323 | 324 | |
| 324 | - nChar = MultiByteToWideChar(CP_UTF8, 0, zUtf8, nByte, NULL, 0); | |
| 325 | + /* If blob to be written to the Windows console is not | |
| 326 | + * UTF-8, convert it to UTF-8 first. | |
| 327 | + */ | |
| 328 | + blob_init(&blob, zUtf8, nByte); | |
| 329 | + blob_to_utf8_no_bom(&blob, 1); | |
| 330 | + nChar = MultiByteToWideChar(CP_UTF8, 0, blob_buffer(&blob), | |
| 331 | + blob_size(&blob), NULL, 0); | |
| 325 | 332 | zUnicode = malloc( (nChar + 1) *sizeof(zUnicode[0]) ); |
| 326 | 333 | if( zUnicode==0 ){ |
| 327 | 334 | return 0; |
| 328 | 335 | } |
| 329 | - nChar = MultiByteToWideChar(CP_UTF8, 0, zUtf8, nByte, zUnicode, nChar); | |
| 336 | + nChar = MultiByteToWideChar(CP_UTF8, 0, blob_buffer(&blob), | |
| 337 | + blob_size(&blob), zUnicode, nChar); | |
| 338 | + blob_reset(&blob); | |
| 330 | 339 | /* Split WriteConsoleW call into multiple chunks, if necessary. See: |
| 331 | 340 | * <https://connect.microsoft.com/VisualStudio/feedback/details/635230> */ |
| 332 | 341 | while( written < nChar ){ |
| 333 | 342 | int size = nChar-written; |
| 334 | 343 | if( size > 26000 ) size = 26000; |
| 335 | 344 |
| --- src/utf8.c | |
| +++ src/utf8.c | |
| @@ -309,10 +309,11 @@ | |
| 309 | int fossil_utf8_to_console(const char *zUtf8, int nByte, int toStdErr){ |
| 310 | #ifdef _WIN32 |
| 311 | int nChar, written = 0; |
| 312 | wchar_t *zUnicode; /* Unicode version of zUtf8 */ |
| 313 | DWORD dummy; |
| 314 | |
| 315 | static int istty[2] = { -1, -1 }; |
| 316 | if( istty[toStdErr] == -1 ){ |
| 317 | istty[toStdErr] = _isatty(toStdErr + 1) != 0; |
| 318 | } |
| @@ -319,16 +320,24 @@ | |
| 319 | if( !istty[toStdErr] ){ |
| 320 | /* stdout/stderr is not a console. */ |
| 321 | return -1; |
| 322 | } |
| 323 | |
| 324 | nChar = MultiByteToWideChar(CP_UTF8, 0, zUtf8, nByte, NULL, 0); |
| 325 | zUnicode = malloc( (nChar + 1) *sizeof(zUnicode[0]) ); |
| 326 | if( zUnicode==0 ){ |
| 327 | return 0; |
| 328 | } |
| 329 | nChar = MultiByteToWideChar(CP_UTF8, 0, zUtf8, nByte, zUnicode, nChar); |
| 330 | /* Split WriteConsoleW call into multiple chunks, if necessary. See: |
| 331 | * <https://connect.microsoft.com/VisualStudio/feedback/details/635230> */ |
| 332 | while( written < nChar ){ |
| 333 | int size = nChar-written; |
| 334 | if( size > 26000 ) size = 26000; |
| 335 |
| --- src/utf8.c | |
| +++ src/utf8.c | |
| @@ -309,10 +309,11 @@ | |
| 309 | int fossil_utf8_to_console(const char *zUtf8, int nByte, int toStdErr){ |
| 310 | #ifdef _WIN32 |
| 311 | int nChar, written = 0; |
| 312 | wchar_t *zUnicode; /* Unicode version of zUtf8 */ |
| 313 | DWORD dummy; |
| 314 | Blob blob; |
| 315 | |
| 316 | static int istty[2] = { -1, -1 }; |
| 317 | if( istty[toStdErr] == -1 ){ |
| 318 | istty[toStdErr] = _isatty(toStdErr + 1) != 0; |
| 319 | } |
| @@ -319,16 +320,24 @@ | |
| 320 | if( !istty[toStdErr] ){ |
| 321 | /* stdout/stderr is not a console. */ |
| 322 | return -1; |
| 323 | } |
| 324 | |
| 325 | /* If blob to be written to the Windows console is not |
| 326 | * UTF-8, convert it to UTF-8 first. |
| 327 | */ |
| 328 | blob_init(&blob, zUtf8, nByte); |
| 329 | blob_to_utf8_no_bom(&blob, 1); |
| 330 | nChar = MultiByteToWideChar(CP_UTF8, 0, blob_buffer(&blob), |
| 331 | blob_size(&blob), NULL, 0); |
| 332 | zUnicode = malloc( (nChar + 1) *sizeof(zUnicode[0]) ); |
| 333 | if( zUnicode==0 ){ |
| 334 | return 0; |
| 335 | } |
| 336 | nChar = MultiByteToWideChar(CP_UTF8, 0, blob_buffer(&blob), |
| 337 | blob_size(&blob), zUnicode, nChar); |
| 338 | blob_reset(&blob); |
| 339 | /* Split WriteConsoleW call into multiple chunks, if necessary. See: |
| 340 | * <https://connect.microsoft.com/VisualStudio/feedback/details/635230> */ |
| 341 | while( written < nChar ){ |
| 342 | int size = nChar-written; |
| 343 | if( size > 26000 ) size = 26000; |
| 344 |