From 15409bae5ffdfce4ef17c4ccf14c8cd4c1b8f37e Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sun, 14 Jun 2015 19:08:06 +0200 Subject: handle environment variables more consistently * the registers beginning with "$" are exported into sub-process environments. Therefore macros can now modify the environment (variables) of commands executed via EC/EG. A variable can be modified temporarily, e.g.: [[$FOO] ^U[$FOO]bar$ EC...$ ][$FOO] * SciTECO accesses the global environment registers instead of using g_getenv(). Therefore now, tilde-expansion will always use the current value of the "$HOME" register. Previously, both register and environment variable could diverge. * This effectively fully maps the process environment to a subset of Q-Registers beginning with "$". * This hasn't been implemented by mapping those registers to special implementations that updates the process environment directly, since g_setenv() is non-thread-safe on UNIX and we're expected to have threads soon - at least in the GTK+ UI. --- src/main.cpp | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 6966d22..120d73e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -196,9 +196,15 @@ static inline void initialize_environment(const gchar *program) { gchar *default_configpath, *abs_path; - gchar **env; /* + * Initialize some "special" environment variables. + * For ease of use and because there are no threads yet, + * we modify the process environment directly. + * Later it is imported into the global Q-Register table + * and the process environment should no longer be accessed + * directly. + * * Initialize and canonicalize $HOME. * Therefore we can refer to $HOME as the * current user's home directory on any platform @@ -237,20 +243,19 @@ initialize_environment(const gchar *program) g_setenv("SCITECOPATH", abs_path, TRUE); g_free(abs_path); - env = g_listenv(); - - for (gchar **key = env; *key; key++) { - gchar name[1 + strlen(*key) + 1]; - QRegister *reg; - - name[0] = '$'; - strcpy(name + 1, *key); - - reg = QRegisters::globals.insert(name); - reg->set_string(g_getenv(*key)); - } - - g_strfreev(env); + /* + * Import process environment into global Q-Register + * table. While it is safe to use g_setenv() early + * on at startup, it might be problematic later on + * (e.g. it's non-thread-safe). + * Therefore the environment registers in the global + * table should be used from now on to set and get + * environment variables. + * When spawning external processes that should inherit + * the environment variables, the environment should + * be exported via QRegisters::globals.get_environ(). + */ + QRegisters::globals.set_environ(); } /* @@ -388,6 +393,7 @@ main(int argc, char **argv) } if (!mung_file && mung_profile) + /* NOTE: Still safe to use g_getenv() */ mung_file = g_build_filename(g_getenv("SCITECOCONFIG"), INI_FILE, NIL); -- cgit v1.2.3