29 static gchar *environment_expand(gchar *param)
36 gchar *expanded = NULL;
42 result = g_strdup (
"x");
46 opening_brace = g_strstr_len (search_start, -1,
"{");
47 closing_brace = g_strstr_len (search_start, -1,
"}");
55 while ( opening_brace && closing_brace && (closing_brace > opening_brace) )
62 if (opening_brace > search_start)
64 gchar *prefix = g_strndup (search_start, opening_brace - search_start);
66 tmp = g_strconcat (result, prefix, NULL);
73 to_expand = g_strndup (opening_brace + 1, closing_brace - opening_brace - 1);
74 env_val = g_getenv (to_expand);
75 tmp = g_strconcat (result, env_val, NULL);
81 search_start = closing_brace + 1;
82 opening_brace = g_strstr_len (search_start, -1,
"{");
83 closing_brace = g_strstr_len (search_start, -1,
"}");
87 tmp = g_strconcat (result, search_start, NULL);
92 if (g_strcmp0 (result,
"x"))
93 expanded = g_strdup (result + 1);
100 gnc_environment_parse_one (
const gchar *env_path)
102 GKeyFile *keyfile = g_key_file_new();
106 gboolean got_keyfile;
108 got_keyfile = g_key_file_load_from_file (keyfile, env_path, G_KEY_FILE_NONE, NULL);
111 g_key_file_free(keyfile);
116 env_vars = g_key_file_get_keys(keyfile,
"Variables", ¶m_count, NULL);
117 for ( i = 0; i < param_count; i++ )
122 gchar *new_val = NULL, *tmp_val;
125 val_list = g_key_file_get_string_list (keyfile,
"Variables",
126 env_vars[i], &val_count,
128 if ( val_count == 0 )
129 g_unsetenv (env_vars[i]);
133 tmp_val = g_strdup (
"x");
134 for ( j = 0; j < val_count; j++ )
136 gchar *expanded = environment_expand (val_list[j]);
137 if (expanded && strlen(expanded))
139 new_val = g_build_path (G_SEARCHPATH_SEPARATOR_S, tmp_val, expanded, NULL);
145 g_strfreev (val_list);
148 if (g_strcmp0 (tmp_val,
"x"))
150 new_val = g_strdup (tmp_val +
sizeof (G_SEARCHPATH_SEPARATOR_S));
153 if (!g_setenv (env_vars[i], new_val, TRUE))
154 g_warning (
"Couldn't properly override environment variable \"%s\". " 155 "This may lead to unexpected results", env_vars[i]);
160 g_strfreev(env_vars);
161 g_key_file_free(keyfile);
172 env_parm = gnc_path_get_prefix();
173 if (!g_setenv(
"GNC_HOME", env_parm, FALSE))
174 g_warning (
"Couldn't set/override environment variable GNC_HOME.");
176 env_parm = gnc_path_get_bindir();
177 if (!g_setenv(
"GNC_BIN", env_parm, FALSE))
178 g_warning (
"Couldn't set/override environment variable GNC_BIN.");
180 env_parm = gnc_path_get_pkglibdir();
181 if (!g_setenv(
"GNC_LIB", env_parm, FALSE))
182 g_warning (
"Couldn't set/override environment variable GNC_LIB.");
184 env_parm = gnc_path_get_pkgdatadir();
185 if (!g_setenv(
"GNC_DATA", env_parm, FALSE))
186 g_warning (
"Couldn't set/override environment variable GNC_DATA.");
188 env_parm = gnc_path_get_pkgsysconfdir();
189 if (!g_setenv(
"GNC_CONF", env_parm, FALSE))
190 g_warning (
"Couldn't set/override environment variable GNC_CONF.");
192 env_parm = gnc_path_get_libdir();
193 if (!g_setenv(
"SYS_LIB", env_parm, FALSE))
194 g_warning (
"Couldn't set/override environment variable SYS_LIB.");
197 config_path = gnc_path_get_pkgsysconfdir();
201 gchar *pathext = g_build_path(
";",
".", g_getenv(
"PATHEXT"),
203 g_setenv(
"PATHEXT", pathext, TRUE);
209 env_path = g_build_filename (config_path,
"environment", NULL);
210 gnc_environment_parse_one(env_path);
214 env_path = g_build_filename (config_path,
"environment.local", NULL);
215 gnc_environment_parse_one(env_path);
217 g_free (config_path);
code to set up the environment for proper gnucash functioning.
void gnc_environment_setup(void)
Parse <prefix>/etc/gnucash/environment and set environment variables based on the contents of that fi...