From b58a964bfac6d6af19a5e4d8b74508d06705909f Mon Sep 17 00:00:00 2001 From: Ray Strode Date: Thu, 2 Jul 2015 13:08:05 -0400 Subject: [PATCH] session: load locale.conf from gdm-session.c Right now we're not picking up changes to locale.conf completely at runtime. This commit moves reading locale.conf to a different place in the code, so that it's more effectively read and used. --- daemon/gdm-launch-environment.c | 106 ---------------------------------------- daemon/gdm-session.c | 102 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 99 insertions(+), 109 deletions(-) diff --git a/daemon/gdm-launch-environment.c b/daemon/gdm-launch-environment.c index e6b16dd..4aee187 100644 --- a/daemon/gdm-launch-environment.c +++ b/daemon/gdm-launch-environment.c @@ -107,109 +107,6 @@ static void gdm_launch_environment_finalize (GObject G_DEFINE_TYPE (GdmLaunchEnvironment, gdm_launch_environment, G_TYPE_OBJECT) -static void -load_lang_config_file (const char *config_file, - const char **str_array) -{ - gchar *contents = NULL; - gchar *p; - gchar *str_joinv; - gchar *pattern; - gchar *key; - gchar *value; - gsize length; - GError *error; - GString *line; - GRegex *re; - - g_return_if_fail (config_file != NULL); - g_return_if_fail (str_array != NULL); - - if (!g_file_test (config_file, G_FILE_TEST_EXISTS)) { - g_debug ("Cannot access '%s'", config_file); - return; - } - - error = NULL; - if (!g_file_get_contents (config_file, &contents, &length, &error)) { - g_debug ("Failed to parse '%s': %s", - config_file, - (error && error->message) ? error->message : "(null)"); - g_error_free (error); - return; - } - - if (!g_utf8_validate (contents, length, NULL)) { - g_warning ("Invalid UTF-8 in '%s'", config_file); - g_free (contents); - return; - } - - str_joinv = g_strjoinv ("|", (char **) str_array); - if (str_joinv == NULL) { - g_warning ("Error in joined"); - g_free (contents); - return; - } - - pattern = g_strdup_printf ("(?P(%s))=(\")?(?P[^\"]*)?(\")?", - str_joinv); - error = NULL; - re = g_regex_new (pattern, 0, 0, &error); - g_free (pattern); - g_free (str_joinv); - if (re == NULL) { - g_warning ("Failed to regex: %s", - (error && error->message) ? error->message : "(null)"); - g_error_free (error); - g_free (contents); - return; - } - - line = g_string_new (""); - for (p = contents; p && *p; p = g_utf8_find_next_char (p, NULL)) { - gunichar ch; - GMatchInfo *match_info = NULL; - - ch = g_utf8_get_char (p); - if ((ch != '\n') && (ch != '\0')) { - g_string_append_unichar (line, ch); - continue; - } - - if (line->str && g_utf8_get_char (line->str) == '#') { - goto next_line; - } - - if (!g_regex_match (re, line->str, 0, &match_info)) { - goto next_line; - } - - if (!g_match_info_matches (match_info)) { - goto next_line; - } - - key = g_match_info_fetch_named (match_info, "key"); - value = g_match_info_fetch_named (match_info, "value"); - - if (key && *key && value && *value) { - g_setenv (key, value, TRUE); - } else if (key && *key) { - g_unsetenv (key); - } - - g_free (key); - g_free (value); -next_line: - g_match_info_free (match_info); - g_string_set_size (line, 0); - } - - g_string_free (line, TRUE); - g_regex_unref (re); - g_free (contents); -} - static GHashTable * build_launch_environment (GdmLaunchEnvironment *launch_environment, gboolean start_session) @@ -226,9 +123,6 @@ build_launch_environment (GdmLaunchEnvironment *launch_environment, char *system_data_dirs; int i; - load_lang_config_file (LANG_CONFIG_FILE, - (const char **) optional_environment); - /* create a hash table of current environment, then update keys has necessary */ hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); diff --git a/daemon/gdm-session.c b/daemon/gdm-session.c index 1674bb6..d3643c2 100644 --- a/daemon/gdm-session.c +++ b/daemon/gdm-session.c @@ -523,8 +523,17 @@ get_session_command_for_name (const char *name, static const char * get_default_language_name (GdmSession *self) { + const char *default_language; + if (self->priv->saved_language != NULL) { - return self->priv->saved_language; + return self->priv->saved_language; + } + + default_language = g_hash_table_lookup (self->priv->environment, + "LANG"); + + if (default_language != NULL) { + return default_language; } return setlocale (LC_MESSAGES, NULL); @@ -1705,6 +1714,90 @@ free_conversation (GdmSessionConversation *conversation) } static void +load_lang_config_file (GdmSession *self) +{ + static const char *config_file = LANG_CONFIG_FILE; + gchar *contents = NULL; + gchar *p; + gchar *key; + gchar *value; + gsize length; + GError *error; + GString *line; + GRegex *re; + + if (!g_file_test (config_file, G_FILE_TEST_EXISTS)) { + g_debug ("Cannot access '%s'", config_file); + return; + } + + error = NULL; + if (!g_file_get_contents (config_file, &contents, &length, &error)) { + g_debug ("Failed to parse '%s': %s", + LANG_CONFIG_FILE, + (error && error->message) ? error->message : "(null)"); + g_error_free (error); + return; + } + + if (!g_utf8_validate (contents, length, NULL)) { + g_warning ("Invalid UTF-8 in '%s'", config_file); + g_free (contents); + return; + } + + re = g_regex_new ("(?P(LANG|LANGUAGE|LC_CTYPE|LC_NUMERIC|LC_TIME|LC_COLLATE|LC_MONETARY|LC_MESSAGES|LC_PAPER|LC_NAME|LC_ADDRESS|LC_TELEPHONE|LC_MEASUREMENT|LC_IDENTIFICATION|LC_ALL))=(\")?(?P[^\"]*)?(\")?", 0, 0, &error); + if (re == NULL) { + g_warning ("Failed to regex: %s", + (error && error->message) ? error->message : "(null)"); + g_error_free (error); + g_free (contents); + return; + } + + line = g_string_new (""); + for (p = contents; p && *p; p = g_utf8_find_next_char (p, NULL)) { + gunichar ch; + GMatchInfo *match_info = NULL; + + ch = g_utf8_get_char (p); + if ((ch != '\n') && (ch != '\0')) { + g_string_append_unichar (line, ch); + continue; + } + + if (line->str && g_utf8_get_char (line->str) == '#') { + goto next_line; + } + + if (!g_regex_match (re, line->str, 0, &match_info)) { + goto next_line; + } + + if (!g_match_info_matches (match_info)) { + goto next_line; + } + + key = g_match_info_fetch_named (match_info, "key"); + value = g_match_info_fetch_named (match_info, "value"); + + if (key && *key && value && *value) { + g_setenv (key, value, TRUE); + } + + g_free (key); + g_free (value); +next_line: + g_match_info_free (match_info); + g_string_set_size (line, 0); + } + + g_string_free (line, TRUE); + g_regex_unref (re); + g_free (contents); +} + +static void gdm_session_init (GdmSession *self) { self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, @@ -1734,6 +1827,7 @@ gdm_session_init (GdmSession *self) (GDestroyNotify) g_free, (GDestroyNotify) g_free); + load_lang_config_file (self); setup_worker_server (self); setup_outside_server (self); } @@ -2396,7 +2490,7 @@ set_up_session_environment (GdmSession *self) { GdmSessionDisplayMode display_mode; gchar *desktop_names; - const char *locale; + char *locale; gdm_session_set_environment_variable (self, "GDMSESSION", @@ -2415,7 +2509,7 @@ set_up_session_environment (GdmSession *self) set_up_session_language (self); - locale = get_default_language_name (self); + locale = g_strdup (get_default_language_name (self)); if (locale != NULL && locale[0] != '\0') { gdm_session_set_environment_variable (self, @@ -2426,6 +2520,8 @@ set_up_session_environment (GdmSession *self) locale); } + g_free (locale); + display_mode = gdm_session_get_display_mode (self); if (display_mode == GDM_SESSION_DISPLAY_MODE_REUSE_VT) { gdm_session_set_environment_variable (self, -- 1.8.3.1