aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2022-03-10 21:22:36 +0100
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2022-03-10 21:53:29 +0100
commit6447b191eef8bf8f4942569e55c9a0f297c67a8e (patch)
tree8d38fa052c522ac4cfd9440c1c5297fce0571f5c /include
parent8b0e45bd2cf2ae97d26e7922f9dbf6c9903bb7c6 (diff)
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 <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'include')
-rw-r--r--include/compat.h16
1 files changed, 14 insertions, 2 deletions
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 <limits.h>
+#include <stdio.h>
#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 */