GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Files | Functions

Files

file  gnc-environment.h
 code to set up the environment for proper gnucash functioning.
 

Functions

void gnc_environment_setup (void)
 

Detailed Description

The API in this file is used to read the environment configuration file and set up a number of environment variables based on the values found in it. These parameters can be used to configure certain aspects of gnucash or components it depends on.

For example if not installed in the standard prefix "/usr", environment variable XDG_DATA_DIRS should be set such that glib can find the gsettings schemas installed by GnuCash and yelp can find the help file and guide (if these are installed).

Function Documentation

void gnc_environment_setup ( void  )

Parse <prefix>/etc/gnucash/environment and set environment variables based on the contents of that file. Read the comments in <prefix>/etc/gnucash/environment for more details.

Definition at line 100 of file gnc-environment.c.

101 {
102  gchar *config_path;
103  gchar *env_file;
104  GKeyFile *keyfile = g_key_file_new();
105  GError *error;
106  gchar **env_vars;
107  gsize param_count;
108  gint i;
109  gboolean got_keyfile;
110  gchar *env_parm;
111 
112  /* Export default parameters to the environment */
113  env_parm = gnc_path_get_prefix();
114  if (!g_setenv("GNC_HOME", env_parm, FALSE))
115  g_warning ("Couldn't set/override environment variable GNC_HOME.");
116  g_free (env_parm);
117  env_parm = gnc_path_get_bindir();
118  if (!g_setenv("GNC_BIN", env_parm, FALSE))
119  g_warning ("Couldn't set/override environment variable GNC_BIN.");
120  g_free (env_parm);
121  env_parm = gnc_path_get_pkglibdir();
122  if (!g_setenv("GNC_LIB", env_parm, FALSE))
123  g_warning ("Couldn't set/override environment variable GNC_LIB.");
124  g_free (env_parm);
125  env_parm = gnc_path_get_pkgdatadir();
126  if (!g_setenv("GNC_DATA", env_parm, FALSE))
127  g_warning ("Couldn't set/override environment variable GNC_DATA.");
128  g_free (env_parm);
129  env_parm = gnc_path_get_pkgsysconfdir();
130  if (!g_setenv("GNC_CONF", env_parm, FALSE))
131  g_warning ("Couldn't set/override environment variable GNC_CONF.");
132  g_free (env_parm);
133  env_parm = gnc_path_get_libdir();
134  if (!g_setenv("SYS_LIB", env_parm, FALSE))
135  g_warning ("Couldn't set/override environment variable SYS_LIB.");
136  g_free (env_parm);
137 
138  config_path = gnc_path_get_pkgsysconfdir();
139 #ifdef G_OS_WIN32
140  {
141  /* unhide files without extension */
142  gchar *pathext = g_build_path(";", ".", g_getenv("PATHEXT"),
143  (gchar*) NULL);
144  g_setenv("PATHEXT", pathext, TRUE);
145  g_free(pathext);
146  }
147 #endif
148 
149  env_file = g_build_filename (config_path, "environment", NULL);
150  got_keyfile = g_key_file_load_from_file (keyfile, env_file, G_KEY_FILE_NONE, &error);
151  g_free (config_path);
152  g_free (env_file);
153  if ( !got_keyfile )
154  {
155  g_key_file_free(keyfile);
156  return;
157  }
158 
159  /* Read the environment overrides and apply them */
160  env_vars = g_key_file_get_keys(keyfile, "Variables", &param_count, &error);
161  for ( i = 0; i < param_count; i++ )
162  {
163  gchar **val_list;
164  gsize val_count;
165  gint j;
166  gchar *new_val = NULL, *tmp_val;
167 
168  /* For each variable, read its new value, optionally expand it and set/unset it */
169  val_list = g_key_file_get_string_list (keyfile, "Variables",
170  env_vars[i], &val_count,
171  &error );
172  if ( val_count == 0 )
173  g_unsetenv (env_vars[i]);
174  else
175  {
176  /* Set an initial return value, so we can always use g_build_path below) */
177  tmp_val = g_strdup ("x");
178  for ( j = 0; j < val_count; j++ )
179  {
180  gchar *expanded = environment_expand (val_list[j]);
181  if (expanded && strlen(expanded))
182  {
183  new_val = g_build_path (G_SEARCHPATH_SEPARATOR_S, tmp_val, expanded, NULL);
184  g_free (tmp_val);
185  g_free(expanded);
186  tmp_val = new_val;
187  }
188  }
189  g_strfreev (val_list);
190 
191  /* Remove the "x" from our result */
192  if (g_strcmp0 (tmp_val, "x"))
193  new_val = g_strdup (tmp_val + sizeof (G_SEARCHPATH_SEPARATOR_S));
194  g_free (tmp_val);
195 
196  if (!g_setenv (env_vars[i], new_val, TRUE))
197  g_warning ("Couldn't properly override environment variable \"%s\". "
198  "This may lead to unexpected results", env_vars[i]);
199  g_free(new_val);
200  }
201  }
202 
203  g_strfreev(env_vars);
204  g_key_file_free(keyfile);
205 }