Skip to content

Instantly share code, notes, and snippets.

@h2oota
Created May 18, 2013 03:46
Show Gist options
  • Save h2oota/5603161 to your computer and use it in GitHub Desktop.
Save h2oota/5603161 to your computer and use it in GitHub Desktop.
a patch for output utf-8 strings in windows console.
--- shell.c.orig Fri Apr 12 21:21:39 2013
+++ shell.c Sat May 18 10:16:01 2013
@@ -36,6 +36,81 @@
#include <ctype.h>
#include <stdarg.h>
+#if defined(_WIN32)
+#include <windows.h>
+#include <wincon.h>
+static int stdout_is_interactive = 0;
+int w32cons_vfprintf(FILE *fp, const char *fmt, va_list arg)
+{
+ if (stdout_is_interactive) {
+ int r;
+ UINT prev_cp = GetConsoleCP();
+ SetConsoleOutputCP(CP_UTF8);
+ r = vfprintf(fp, fmt, arg);
+ fflush(fp);
+ SetConsoleOutputCP(prev_cp);
+ return r;
+ } else
+ return vfprintf(fp, fmt, arg);
+}
+int w32cons_fprintf(FILE *fp, const char *fmt, ...)
+{
+ int r;
+ va_list args;
+ va_start(args, fmt);
+ r = w32cons_vfprintf(fp, fmt, args);
+ va_end(args);
+ return r;
+}
+int w32cons_printf(const char *fmt, ...)
+{
+ int r;
+ va_list args;
+ va_start(args, fmt);
+ r = w32cons_vfprintf(stdout, fmt, args);
+ va_end(args);
+ return r;
+}
+void w32cons_cls( HANDLE hConsole )
+{
+ COORD coordScreen = { 0, 0 }; /* here's where we'll home the
+ cursor */
+ BOOL bSuccess;
+ DWORD cCharsWritten;
+ CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
+ DWORD dwConSize; /* number of character cells in
+ the current buffer */
+
+ /* get the number of character cells in the current buffer */
+
+ bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi );
+ dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
+
+ /* fill the entire screen with blanks */
+
+ bSuccess = FillConsoleOutputCharacter( hConsole, (TCHAR) ' ',
+ dwConSize, coordScreen, &cCharsWritten );
+
+ /* get the current text attribute */
+
+ bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi );
+
+ /* now set the buffer's attributes accordingly */
+
+ bSuccess = FillConsoleOutputAttribute( hConsole, csbi.wAttributes,
+ dwConSize, coordScreen, &cCharsWritten );
+
+ /* put the cursor at (0, 0) */
+
+ bSuccess = SetConsoleCursorPosition( hConsole, coordScreen );
+ return;
+}
+
+#define vfprintf(fp, fmt, args) w32cons_vfprintf(fp, fmt, args)
+#define fprintf(fp, fmt, ...) w32cons_fprintf(fp, fmt, __VA_ARGS__)
+#define printf(fmt, ...) w32cons_printf(fmt, __VA_ARGS__)
+#endif
+
#if !defined(_WIN32) && !defined(WIN32)
# include <signal.h>
# if !defined(__RTP__) && !defined(_WRS_KERNEL)
@@ -3140,6 +3215,12 @@
if( bail_on_error ) return rc;
}
}
+ } else if (strcmp(z, "-utf8-console") == 0) {
+ stdout_is_interactive = isatty(1);
+ } else if (strcmp(z, "-utf8-console-cls") == 0) {
+ stdout_is_interactive = isatty(1);
+ if (stdout_is_interactive)
+ w32cons_cls(GetStdHandle(STD_OUTPUT_HANDLE));
}else{
fprintf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
fprintf(stderr,"Use -help for a list of options.\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment