Use Unicode output on MS-Windows consoles 1.51
authorali <ali@juiblex.co.uk>
Mon Jan 30 23:32:47 2012 +0000 (2012-01-30)
changeset 114a80c6053a66
parent 10 f28ad4577863
child 12 84459e0f099b
Use Unicode output on MS-Windows consoles
bl/Makefile.am
bl/bl.h
bl/print.c
bl/print.h
bl/spawn.c
test/harness/loupe-test.c
test/harness/testcase.c
test/harness/testcaseio.c
test/harness/testcaseparser.c
     1.1 --- a/bl/Makefile.am	Mon Jan 30 09:11:07 2012 +0000
     1.2 +++ b/bl/Makefile.am	Mon Jan 30 23:32:47 2012 +0000
     1.3 @@ -4,4 +4,4 @@
     1.4  
     1.5  noinst_LTLIBRARIES=libbl.la
     1.6  libbl_la_SOURCES=bl.h textfileutils.c textfileutils.h spawn.c spawn.h \
     1.7 -	path.c path.h mkdtemp.c mkdtemp.h
     1.8 +	path.c path.h mkdtemp.c mkdtemp.h print.c print.h
     2.1 --- a/bl/bl.h	Mon Jan 30 09:11:07 2012 +0000
     2.2 +++ b/bl/bl.h	Mon Jan 30 23:32:47 2012 +0000
     2.3 @@ -2,3 +2,4 @@
     2.4  #include <bl/spawn.h>
     2.5  #include <bl/path.h>
     2.6  #include <bl/mkdtemp.h>
     2.7 +#include <bl/print.h>
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/bl/print.c	Mon Jan 30 23:32:47 2012 +0000
     3.3 @@ -0,0 +1,64 @@
     3.4 +#ifdef WIN32
     3.5 +#include <windows.h>
     3.6 +#endif
     3.7 +#include <stdlib.h>
     3.8 +#include <stdio.h>
     3.9 +#include <glib.h>
    3.10 +
    3.11 +/*
    3.12 + * Handlers for g_print() and g_printerr() which will output via
    3.13 + * WriteConsoleW when run under MS-Windows and the corresponding
    3.14 + * stream has not been re-directed. In all other cases, output
    3.15 + * via stdout and stderr respectively.
    3.16 + */
    3.17 +
    3.18 +#ifdef WIN32
    3.19 +static HANDLE bl_console=0;
    3.20 +
    3.21 +static void bl_print_handler_console(const char *string)
    3.22 +{
    3.23 +    long len;
    3.24 +    DWORD dummy;
    3.25 +    gunichar2 *string2;
    3.26 +    string2=g_utf8_to_utf16(string,-1,NULL,&len,NULL);
    3.27 +    if (string2)
    3.28 +    {
    3.29 +	WriteConsoleW(bl_console,string2,len,&dummy,NULL);
    3.30 +	g_free(string2);
    3.31 +    }
    3.32 +}
    3.33 +#endif
    3.34 +
    3.35 +static void bl_print_handler_stdout(const char *string)
    3.36 +{
    3.37 +    fputs(string,stdout);
    3.38 +}
    3.39 +
    3.40 +static void bl_print_handler_stderr(const char *string)
    3.41 +{
    3.42 +    fputs(string,stderr);
    3.43 +}
    3.44 +
    3.45 +void bl_set_print_handlers(void)
    3.46 +{
    3.47 +#ifdef WIN32
    3.48 +    DWORD dummy;
    3.49 +    if (GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE),&dummy))
    3.50 +    {
    3.51 +	bl_console=GetStdHandle(STD_OUTPUT_HANDLE);
    3.52 +	g_set_print_handler(bl_print_handler_console);
    3.53 +    }
    3.54 +    else
    3.55 +#endif
    3.56 +	g_set_print_handler(bl_print_handler_stdout);
    3.57 +#ifdef WIN32
    3.58 +    if (GetConsoleMode(GetStdHandle(STD_ERROR_HANDLE),&dummy))
    3.59 +    {
    3.60 +	if (!bl_console)
    3.61 +	    bl_console=GetStdHandle(STD_ERROR_HANDLE);
    3.62 +	g_set_printerr_handler(bl_print_handler_console);
    3.63 +    }
    3.64 +    else
    3.65 +#endif
    3.66 +	g_set_printerr_handler(bl_print_handler_stderr);
    3.67 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/bl/print.h	Mon Jan 30 23:32:47 2012 +0000
     4.3 @@ -0,0 +1,6 @@
     4.4 +#ifndef BL_PRINT_H
     4.5 +#define BL_PRINT_H
     4.6 +
     4.7 +void bl_set_print_handlers(void);
     4.8 +
     4.9 +#endif	/* BL_PRINT_H */
     5.1 --- a/bl/spawn.c	Mon Jan 30 09:11:07 2012 +0000
     5.2 +++ b/bl/spawn.c	Mon Jan 30 23:32:47 2012 +0000
     5.3 @@ -20,7 +20,7 @@
     5.4      retval=g_spawn_sync(working_directory,argv,NULL,flags,NULL,NULL,
     5.5        standard_output,&standard_error,exit_status,error);
     5.6      if (standard_error)
     5.7 -	fputs(standard_error,stderr);
     5.8 +	g_printerr("%s",standard_error);
     5.9      g_free(standard_error);
    5.10      if (retval && exit_status)
    5.11  	*exit_status=WEXITSTATUS(*exit_status);
     6.1 --- a/test/harness/loupe-test.c	Mon Jan 30 09:11:07 2012 +0000
     6.2 +++ b/test/harness/loupe-test.c	Mon Jan 30 23:32:47 2012 +0000
     6.3 @@ -26,6 +26,7 @@
     6.4  {
     6.5      int i;
     6.6      gboolean pass=TRUE;
     6.7 +    bl_set_print_handlers();
     6.8      for(i=1;i<argc;i++)
     6.9  	pass&=run_test(argv[i]);
    6.10      return pass?0:1;
     7.1 --- a/test/harness/testcase.c	Mon Jan 30 09:11:07 2012 +0000
     7.2 +++ b/test/harness/testcase.c	Mon Jan 30 23:32:47 2012 +0000
     7.3 @@ -62,7 +62,7 @@
     7.4  	else if (!g_unichar_iszerowidth(c))
     7.5  	    col++;
     7.6      }
     7.7 -    fprintf(stderr,"%s\n%*s^\n",string->str,col,"");
     7.8 +    g_print("%s\n%*s^\n",string->str,col,"");
     7.9      g_string_free(string,TRUE);
    7.10  }
    7.11  
    7.12 @@ -252,8 +252,8 @@
    7.13      GError *error=NULL;
    7.14      if (!testcase_create_input_files(testcase,&error))
    7.15      {
    7.16 -	fprintf(stderr,"%s: FAIL\n",testcase->basename);
    7.17 -	fprintf(stderr,"%s\n",error->message);
    7.18 +	g_print("%s: FAIL\n",testcase->basename);
    7.19 +	g_print("%s\n",error->message);
    7.20  	g_error_free(error);
    7.21  	return FALSE;
    7.22      }
    7.23 @@ -266,8 +266,8 @@
    7.24      }
    7.25      if (!r)
    7.26      {
    7.27 -	fprintf(stderr,"%s: FAIL\n",testcase->basename);
    7.28 -	fprintf(stderr,"%s\n",error->message);
    7.29 +	g_print("%s: FAIL\n",testcase->basename);
    7.30 +	g_print("%s\n",error->message);
    7.31  	g_error_free(error);
    7.32  	(void)testcase_remove_input_files(testcase,NULL);
    7.33  	return FALSE;
    7.34 @@ -275,8 +275,8 @@
    7.35      filename=testcase_resolve_input_files(testcase,"TEST-XXXXXX");
    7.36      if (!testcase_remove_input_files(testcase,&error))
    7.37      {
    7.38 -	fprintf(stderr,"%s: FAIL\n",testcase->basename);
    7.39 -	fprintf(stderr,"%s\n",error->message);
    7.40 +	g_print("%s: FAIL\n",testcase->basename);
    7.41 +	g_print("%s\n",error->message);
    7.42  	g_error_free(error);
    7.43  	return FALSE;
    7.44      }
    7.45 @@ -288,9 +288,9 @@
    7.46  	expected=g_string_new(testcase->expected);
    7.47  	if (!g_str_has_prefix(output,header->str))
    7.48  	{
    7.49 -	    fprintf(stderr,"%s: FAIL\n",testcase->basename);
    7.50 +	    g_print("%s: FAIL\n",testcase->basename);
    7.51  	    offset=common_prefix_length(output,header->str);
    7.52 -	    fprintf(stderr,"Unexpected header from bookloupe:\n");
    7.53 +	    g_print("Unexpected header from bookloupe:\n");
    7.54  	    print_unexpected(output,offset);
    7.55  	    r=FALSE;
    7.56  	}
    7.57 @@ -303,22 +303,22 @@
    7.58  		pos=s-output+2;
    7.59  	    else
    7.60  	    {
    7.61 -		fprintf(stderr,"%s: FAIL\n",testcase->basename);
    7.62 +		g_print("%s: FAIL\n",testcase->basename);
    7.63  		offset=common_prefix_length(output,header->str);
    7.64 -		fprintf(stderr,"Unterminated summary from bookloupe:\n%s\n",
    7.65 +		g_print("Unterminated summary from bookloupe:\n%s\n",
    7.66  		  output+pos);
    7.67  		r=FALSE;
    7.68  	    }
    7.69  	}
    7.70  	if (r && strcmp(output+pos,expected->str))
    7.71  	{
    7.72 -	    fprintf(stderr,"%s: FAIL\n",testcase->basename);
    7.73 +	    g_print("%s: FAIL\n",testcase->basename);
    7.74  	    offset=common_prefix_length(output+pos,expected->str);
    7.75  	    if (!offset && !output[pos+offset])
    7.76 -		fprintf(stderr,"Unexpected zero warnings from bookloupe.\n");
    7.77 +		g_print("Unexpected zero warnings from bookloupe.\n");
    7.78  	    else
    7.79  	    {
    7.80 -		fprintf(stderr,"Unexpected output from bookloupe:\n");
    7.81 +		g_print("Unexpected output from bookloupe:\n");
    7.82  		print_unexpected(output+pos,offset);
    7.83  	    }
    7.84  	    r=FALSE;
    7.85 @@ -329,7 +329,7 @@
    7.86      g_free(filename);
    7.87      g_free(output);
    7.88      if (r)
    7.89 -	fprintf(stderr,"%s: PASS\n",testcase->basename);
    7.90 +	g_print("%s: PASS\n",testcase->basename);
    7.91      return r;
    7.92  }
    7.93  
     8.1 --- a/test/harness/testcaseio.c	Mon Jan 30 09:11:07 2012 +0000
     8.2 +++ b/test/harness/testcaseio.c	Mon Jan 30 23:32:47 2012 +0000
     8.3 @@ -9,7 +9,7 @@
     8.4  
     8.5  /*
     8.6   * Read a testcase in from a file.
     8.7 - * On error, print a suitable message on stderr and return NULL.
     8.8 + * On error, print a suitable message using g_printerr and return NULL.
     8.9   * The returned testcase should be freed with testcase_free().
    8.10   */
    8.11  Testcase *testcase_parse_file(const char *filename)
    8.12 @@ -25,7 +25,7 @@
    8.13  	return NULL;
    8.14      if (!*testcase_parser_get_flag(parser))
    8.15      {
    8.16 -	fprintf(stderr,"%s: Not a valid testcase (flag)\n",filename);
    8.17 +	g_printerr("%s: Not a valid testcase (flag)\n",filename);
    8.18  	testcase_parser_free(parser);
    8.19  	return NULL;
    8.20      }
    8.21 @@ -49,7 +49,7 @@
    8.22  		 * file on somebody's computer by getting them to run a
    8.23  		 * testcase!
    8.24  		 */
    8.25 -		fprintf(stderr,
    8.26 +		g_printerr(
    8.27  		  "%s: Input files may not have a directory component\n",arg);
    8.28  		g_free(s);
    8.29  		g_free(arg);
    8.30 @@ -76,7 +76,7 @@
    8.31  	}
    8.32  	else
    8.33  	{
    8.34 -	    fprintf(stderr,"%s: Not a valid testcase (%s)\n",filename,tag);
    8.35 +	    g_printerr("%s: Not a valid testcase (%s)\n",filename,tag);
    8.36  	    testcase_free(testcase);
    8.37  	    testcase_parser_free(parser);
    8.38  	    return NULL;
    8.39 @@ -86,10 +86,10 @@
    8.40      if (!testcase_parser_at_eof(parser))
    8.41      {
    8.42  	if (found_tag)
    8.43 -	    fprintf(stderr,"%s: Not a valid testcase (garbage at end)\n",
    8.44 +	    g_printerr("%s: Not a valid testcase (garbage at end)\n",
    8.45  	      filename);
    8.46  	else
    8.47 -	    fprintf(stderr,"%s: Not a valid testcase (no valid tags)\n",
    8.48 +	    g_printerr("%s: Not a valid testcase (no valid tags)\n",
    8.49  	      filename);
    8.50  	testcase_free(testcase);
    8.51  	testcase_parser_free(parser);
     9.1 --- a/test/harness/testcaseparser.c	Mon Jan 30 09:11:07 2012 +0000
     9.2 +++ b/test/harness/testcaseparser.c	Mon Jan 30 23:32:47 2012 +0000
     9.3 @@ -99,7 +99,7 @@
     9.4      }
     9.5      if (!g_utf8_validate(parser->contents,len,NULL))
     9.6      {
     9.7 -	fprintf(stderr,"%s: Does not contain valid UTF-8\n",filename);
     9.8 +	g_printerr("%s: Does not contain valid UTF-8\n",filename);
     9.9  	g_free(parser->contents);
    9.10  	g_free(parser);
    9.11  	return NULL;