From 6447b191eef8bf8f4942569e55c9a0f297c67a8e Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Thu, 10 Mar 2022 21:22:36 +0100 Subject: Windows: redirect standard I/O and convert text to UTF-16 Preprocessor magic is used to redirect putc/fputc/fputs/printf/fprintf to custom implementations. The custom implementations try to figure out if we are printing to the console and, if so, convert the resulting strings to UTF-16 and print them through ConsoleWriteW. If the output is redirected to a file or a pipe, the original (presummed) UTF-8 is kept. Simply setting the console output codepage to UTF-8 does not work, because the standard I/O facilities of MSVCRT either does not support unicode (in non-wchar mode), or has half-broken support through fputs, which can still break up multi-byte sequences through its internal buffering. Likewise, changing the codepage and using ConsoleWriteA, or trying to use fputws did not work in a test VM either. This approach is the one that worked most consistently among the ones tried, but also has problems. E.g. it breaks when setting the codepage to UTF-8 manually (using `chcp 65001`). Signed-off-by: David Oberhollenzer --- include/compat.h | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'include/compat.h') diff --git a/include/compat.h b/include/compat.h index 59133c9..65872ec 100644 --- a/include/compat.h +++ b/include/compat.h @@ -8,9 +8,11 @@ #define COMPAT_H #include "sqfs/predef.h" +#include "fstream.h" #include "config.h" #include +#include #if defined(__GNUC__) && __GNUC__ >= 5 # define SZ_ADD_OV __builtin_add_overflow @@ -228,9 +230,19 @@ int fnmatch(const char *, const char *, int); #endif #if defined(_WIN32) || defined(__WINDOWS__) -#define main sqfs_tools_main - extern int sqfs_tools_main(int argc, char **argv); + +int stfs_tools_fputc(int c, FILE *strm); +int stfs_tools_fputs(const char *str, FILE *strm); +int stfs_tools_printf(const char *fmt, ...) PRINTF_ATTRIB(1, 2); +int stfs_tools_fprintf(FILE *strm, const char *fmt, ...) PRINTF_ATTRIB(2, 3); + +#define main sqfs_tools_main +#define printf stfs_tools_printf +#define fprintf stfs_tools_fprintf +#define fputs stfs_tools_fputs +#define fputc stfs_tools_fputc +#define putc stfs_tools_fputc #endif #endif /* COMPAT_H */ -- cgit v1.2.3