void script_process(char* script) { // Split script in independent pipelines char* array[10]; int numPipelines = gettokens(script, array, 10, ";"); // run script commands int i; for(i = 0; i < numPipelines; ++i) { // expand environment variables and substitute command line char pointer // with the one with expanded environment variables (if necesary) // char* expanded = expand_envVars(array[i]); // if(expanded) array[i] = expanded; array[i] = environment_expand(array[i]); // move commands to background (if necesary) background(array[i]); // Exec `cd` (it's a built-in, but must change shell environment itself, // so we check and exec for it directly here) if(builtin_cd(array[i])) { free(array[i]); continue; } // Set environment variable if(environment_set(array[i])) { free(array[i]); continue; } // run foreground command pipeline_process(array[i]); // free line created by environment variables expansion free(array[i]); } }
static void environment_override() { const gchar *path; gchar *env_file; GKeyFile *keyfile=g_key_file_new(); GError *error; gchar **env_vars; gsize param_count; gint i; gboolean got_keyfile; if ((path = g_getenv("GNC_CONFIG_PATH"))) config_path = g_strdup(path); if ((path = g_getenv("GNC_SHARE_PATH"))) share_path = g_strdup(path); if ((path = g_getenv("GNC_DOC_PATH"))) help_path = g_strdup(path); if ((path = g_getenv("GNC_GCONF_PATH"))) gconf_path = g_strdup(path); #ifdef G_OS_WIN32 { /* unhide files without extension */ gchar *pathext = g_build_path(";", ".", g_getenv("PATHEXT"), (gchar*) NULL); g_setenv("PATHEXT", pathext, TRUE); g_free(pathext); } #endif env_file = g_strjoin(G_DIR_SEPARATOR_S, config_path, "environment", NULL); got_keyfile = g_key_file_load_from_file (keyfile, env_file, G_KEY_FILE_NONE, &error); g_free (env_file); if ( !got_keyfile ) return; /* Read the environment overrides and apply them */ env_vars = g_key_file_get_keys(keyfile, "Variables", ¶m_count, &error); for ( i = 0; i < param_count; i++ ) { gchar **val_list; gsize val_count; gint j; gchar *new_val = NULL, *tmp_val; /* For each variable, read its new value, optionally expand it and (un)set it */ val_list = g_key_file_get_string_list (keyfile, "Variables", env_vars[i], &val_count, &error ); if (!val_list) g_unsetenv (env_vars[i]); else { /* Set an initial return value, so we can always use g_build_path below) */ tmp_val = g_strdup ("x"); for ( j = 0; j < val_count; j++ ) { gchar *expanded = environment_expand (val_list[j]); new_val = g_build_path (G_SEARCHPATH_SEPARATOR_S, tmp_val, expanded, NULL); g_free (tmp_val); tmp_val = new_val; } g_strfreev (val_list); /* Remove the "x" from our result */ if (g_strcmp0 (tmp_val, "x")) new_val = g_strdup (tmp_val + sizeof (G_SEARCHPATH_SEPARATOR_S)); g_free (tmp_val); if (!g_setenv (env_vars[i], new_val, TRUE)) g_warning ("Couldn't properly override environment variable \"%s\". " "This may lead to unexpected results", env_vars[i]); } } g_strfreev(env_vars); }
static void gnc_environment_parse_one (const gchar *env_path) { GKeyFile *keyfile = g_key_file_new(); GError *error = NULL; gchar **env_vars; gsize param_count; gint i; gboolean got_keyfile; got_keyfile = g_key_file_load_from_file (keyfile, env_path, G_KEY_FILE_NONE, &error); if ( !got_keyfile ) { g_key_file_free(keyfile); return; } /* Read the environment overrides and apply them */ env_vars = g_key_file_get_keys(keyfile, "Variables", ¶m_count, &error); for ( i = 0; i < param_count; i++ ) { gchar **val_list; gsize val_count; gint j; gchar *new_val = NULL, *tmp_val; /* For each variable, read its new value, optionally expand it and set/unset it */ val_list = g_key_file_get_string_list (keyfile, "Variables", env_vars[i], &val_count, &error ); if ( val_count == 0 ) g_unsetenv (env_vars[i]); else { /* Set an initial return value, so we can always use g_build_path below) */ tmp_val = g_strdup ("x"); for ( j = 0; j < val_count; j++ ) { gchar *expanded = environment_expand (val_list[j]); if (expanded && strlen(expanded)) { new_val = g_build_path (G_SEARCHPATH_SEPARATOR_S, tmp_val, expanded, NULL); g_free (tmp_val); g_free(expanded); tmp_val = new_val; } } g_strfreev (val_list); /* Remove the "x" from our result */ if (g_strcmp0 (tmp_val, "x")) new_val = g_strdup (tmp_val + sizeof (G_SEARCHPATH_SEPARATOR_S)); g_free (tmp_val); if (!g_setenv (env_vars[i], new_val, TRUE)) g_warning ("Couldn't properly override environment variable \"%s\". " "This may lead to unexpected results", env_vars[i]); g_free(new_val); } } g_strfreev(env_vars); g_key_file_free(keyfile); }