gnucash maint: Multiple changes pushed

John Ralls jralls at code.gnucash.org
Sun Apr 28 20:00:03 EDT 2019


Updated	 via  https://github.com/Gnucash/gnucash/commit/3feb8646 (commit)
	 via  https://github.com/Gnucash/gnucash/commit/6c7ccbd9 (commit)
	from  https://github.com/Gnucash/gnucash/commit/94bb28d9 (commit)



commit 3feb864691cf855f0fcd0240731eb05206ec2254
Author: John Ralls <jralls at ceridwen.us>
Date:   Sun Apr 28 16:59:51 2019 -0700

    Add an optionally-compiled diagnostic console on Windows.

diff --git a/gnucash/gnucash-bin.c b/gnucash/gnucash-bin.c
index 3278d3ae6..659f17381 100644
--- a/gnucash/gnucash-bin.c
+++ b/gnucash/gnucash-bin.c
@@ -58,11 +58,17 @@
 #include "guile-mappings.h"
 #ifdef __MINGW32__
 #include <Windows.h>
+#include <fcntl.h>
 #endif
 
 /* This static indicates the debugging module that this .o belongs to.  */
 static QofLogModule log_module = GNC_MOD_GUI;
 
+/* Change the following to have a console window attached to GnuCash
+ * for displaying stdout and stderr on Windows.
+ */
+#define __MSWIN_CONSOLE__ 0
+
 #ifdef HAVE_GETTEXT
 #  include <libintl.h>
 #  include <locale.h>
@@ -809,6 +815,53 @@ set_win32_thread_locale()
 }
 #endif
 
+/* Creates a console window on MSWindows to display stdout and stderr
+ * when __MSWIN_CONSOLE__ is defined at the top of the file.
+ *
+ * Useful for displaying the diagnostics printed before logging is
+ * started and if logging is redirected with --logto=stderr.
+ */
+static void
+redirect_stdout (void)
+{
+#ifdef __MINGW32__ && __MSWIN_CONSOLE__
+    static const WORD MAX_CONSOLE_LINES = 500;
+   int hConHandle;
+    long lStdHandle;
+    CONSOLE_SCREEN_BUFFER_INFO coninfo;
+    FILE *fp;
+
+    // allocate a console for this app
+    AllocConsole();
+
+    // set the screen buffer to be big enough to let us scroll text
+    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
+    coninfo.dwSize.Y = MAX_CONSOLE_LINES;
+    SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
+
+    // redirect unbuffered STDOUT to the console
+    lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
+    hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
+    fp = _fdopen( hConHandle, "w" );
+    *stdout = *fp;
+    setvbuf( stdout, NULL, _IONBF, 0 );
+
+    // redirect unbuffered STDIN to the console
+    lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
+    hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
+    fp = _fdopen( hConHandle, "r" );
+    *stdin = *fp;
+    setvbuf( stdin, NULL, _IONBF, 0 );
+
+    // redirect unbuffered STDERR to the console
+    lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
+    hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
+    fp = _fdopen( hConHandle, "w" );
+    *stderr = *fp;
+    setvbuf( stderr, NULL, _IONBF, 0 );
+#endif
+}
+
 int
 main(int argc, char ** argv)
 {
@@ -825,6 +878,7 @@ main(int argc, char ** argv)
         }
     }
 #endif
+    redirect_stdout ();
 
     /* This should be called before gettext is initialized
      * The user may have configured a different language via
@@ -846,7 +900,8 @@ main(int argc, char ** argv)
         g_setenv ("LC_ALL", "C", TRUE);
         setlocale (LC_ALL, "C");
       }
-#endif
+    else
+      printf ("Locale set to %s\n", sys_locale);
 #ifdef HAVE_GETTEXT
     {
         gchar *localedir = gnc_path_get_localedir();

commit 6c7ccbd9e77fd377c8eae97323fbdadd77d3192f
Author: John Ralls <jralls at ceridwen.us>
Date:   Sun Apr 28 16:34:58 2019 -0700

    Bug 797134 - Decimal separator: Windows configured as dot,...
    
    Gnucash shows comma.
    
    This was introduced by 8f88b7f2b. It turns out that Windows
    implementation of setlocale works only if the POSIX locale environment
    variables (i.e. LC_FOO or LANG) are set, otherwise it returns NULL. We
    were unknowingly relying on Guile to retrieve the Windows environment
    for us and that would do the wrong thing if the user had set the locale
    in the environment file, so 8f88b7f2b restored it... unfortunately to
    the C locale.
    
    This commit changes the set_win32_thread_locale function to work in
    both directions, setting both locales from the environment variables
    if any are set and calling setlocale with the result of
    GetUserDefaultLocaleName if not.

diff --git a/gnucash/gnucash-bin.c b/gnucash/gnucash-bin.c
index 12866092f..3278d3ae6 100644
--- a/gnucash/gnucash-bin.c
+++ b/gnucash/gnucash-bin.c
@@ -763,31 +763,49 @@ gnc_log_init()
 }
 
 #ifdef __MINGW32__
-/* Set the Win32 internal localization for the main thread if the
- * locale has been overridden in the environment. Note that this is
- * separate from what setlocale() does; that only affects POSIX
- * functions and does *not* set the Windows locale.
+/* If one of the Unix locale variables LC_ALL, LC_MESSAGES, or LANG is
+ * set in the environment check to see if it's a valid locale and if
+ * it is set both the Windows and POSIX locales to that. If not
+ * retrieve the Windows locale and set POSIX to match.
  */
 static void
 set_win32_thread_locale()
 {
-    gunichar2* wlocale = NULL;
+    WCHAR lpLocaleName[LOCALE_NAME_MAX_LENGTH];
     char *locale = NULL;
-    int len = 0;
-    if (((locale = getenv ("LC_ALL")) == NULL || locale[0] == '\0')
-      && ((locale = getenv ("LC_MESSAGES")) == NULL || locale[0] == '\0')
-      && ((locale = getenv ("LANG")) == NULL || locale[0] == '\0'))
-	return;
-    len = strchr(locale, '.') - locale;
-    locale[2] = '-';
-    wlocale = g_utf8_to_utf16 (locale, len, NULL, NULL, NULL);
-    if (IsValidLocaleName(wlocale))
+
+    if (((locale = getenv ("LC_ALL")) != NULL && locale[0] != '\0') ||
+      ((locale = getenv ("LC_MESSAGES")) != NULL && locale[0] != '\0') ||
+      ((locale = getenv ("LANG")) != NULL && locale[0] != '\0'))
+    {
+	gunichar2* wlocale = NULL;
+	int len = 0;
+	len = strchr(locale, '.') - locale;
+	locale[2] = '-';
+	wlocale = g_utf8_to_utf16 (locale, len, NULL, NULL, NULL);
+	if (IsValidLocaleName(wlocale))
+	{
+	    LCID lcid = LocaleNameToLCID(wlocale, LOCALE_ALLOW_NEUTRAL_NAMES);
+	    printf ("win32_thread_locale setting %s from unix environment.\n",
+		    locale);
+	    SetThreadLocale(lcid);
+	    setlocale (LC_ALL, locale);
+	    sys_locale = locale;
+	    g_free(locale);
+	    g_free(wlocale);
+	    return;
+	}
+	g_free(locale);
+	g_free(wlocale);
+    }
+    if (GetUserDefaultLocaleName(lpLocaleName, LOCALE_NAME_MAX_LENGTH))
     {
-	LCID lcid = LocaleNameToLCID(wlocale, LOCALE_ALLOW_NEUTRAL_NAMES);
-	SetThreadLocale(lcid);
+	sys_locale = g_utf16_to_utf8((gunichar2*)lpLocaleName,
+				     LOCALE_NAME_MAX_LENGTH,
+				     NULL, NULL, NULL);
+	setlocale (LC_ALL, sys_locale);
+	return;
     }
-    g_free(locale);
-    g_free(wlocale);
 }
 #endif
 
@@ -814,12 +832,12 @@ main(int argc, char ** argv)
      */
 #ifdef MAC_INTEGRATION
     set_mac_locale();
+#elif defined __MINGW32__
+    set_win32_thread_locale();
 #endif
     gnc_environment_setup();
-#ifndef MAC_INTEGRATION /* setlocale already done */
+#if ! defined MAC_INTEGRATION && ! defined __MINGW32__/* setlocale already done */
     sys_locale = g_strdup (setlocale (LC_ALL, ""));
-#ifdef __MINGW32__
-    set_win32_thread_locale();
 #endif
     if (!sys_locale)
       {



Summary of changes:
 gnucash/gnucash-bin.c | 117 ++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 95 insertions(+), 22 deletions(-)



More information about the gnucash-changes mailing list