diff --git a/env.cpp b/env.cpp index b17df3865..0318cd85d 100644 --- a/env.cpp +++ b/env.cpp @@ -70,6 +70,12 @@ */ #define ENV_NULL L"\x1d" +/** Some configuration path environment variables */ +#define FISH_DATADIR_VAR L"__fish_datadir" +#define FISH_SYSCONFDIR_VAR L"__fish_sysconfdir" +#define FISH_HELPDIR_VAR L"__fish_help_dir" +#define FISH_BIN_DIR L"__fish_bin_dir" + /** At init, we read all the environment variables from this array. */ @@ -235,8 +241,21 @@ static void start_fishd() debug( 0, _( L"Could not get user information" ) ); return; } - - wcstring cmd = format_string(FISHD_CMD, pw->pw_name); + + wcstring cmd = format_string(FISHD_CMD, pw->pw_name); + + /* Prefer the fishd in __fish_bin_dir, if exists */ + const env_var_t bin_dir = env_get_string(L"__fish_bin_dir"); + if (! bin_dir.missing_or_empty()) + { + wcstring path = bin_dir + L"/fishd"; + if (waccess(path, X_OK) == 0) + { + /* The path command just looks like 'fishd', so insert the bin path to make it absolute */ + cmd.insert(0, bin_dir + L"/"); + } + } + parser_t &parser = parser_t::principal_parser(); parser.eval( cmd, 0, TOP ); } @@ -510,7 +529,7 @@ static bool variable_can_be_array(const wchar_t *key) { } } -void env_init() +void env_init(const struct config_paths_t *paths /* or NULL */) { char **p; struct passwd *pw; @@ -560,7 +579,7 @@ void env_init() Now the environemnt variable handling is set up, the next step is to insert valid data */ - + /* Import environment variables */ @@ -600,6 +619,15 @@ void env_init() free(key); } + /* Set the given paths in the environment, if we have any */ + if (paths != NULL) + { + env_set(FISH_DATADIR_VAR, paths->data.c_str(), ENV_GLOBAL | ENV_EXPORT); + env_set(FISH_SYSCONFDIR_VAR, paths->sysconf.c_str(), ENV_GLOBAL | ENV_EXPORT); + env_set(FISH_HELPDIR_VAR, paths->doc.c_str(), ENV_GLOBAL | ENV_EXPORT); + env_set(FISH_BIN_DIR, paths->bin.c_str(), ENV_GLOBAL | ENV_EXPORT); + } + /* Set up the PATH variable */ diff --git a/env.h b/env.h index 63e834dac..a687a1aa3 100644 --- a/env.h +++ b/env.h @@ -52,11 +52,20 @@ enum{ } ; +/* A struct of configuration directories, determined in main() that fish will optionally pass to env_init. + */ +struct config_paths_t +{ + wcstring data; // e.g. /usr/local/share + wcstring sysconf; // e.g. /usr/local/etc + wcstring doc; // e.g. /usr/local/share/doc/fish + wcstring bin; // e.g. /usr/local/bin +}; /** Initialize environment variable data */ -void env_init(); +void env_init(const struct config_paths_t *paths = NULL); /** Destroy environment variable data @@ -164,6 +173,8 @@ void env_export_arr(bool recalc, null_terminated_array_t &result); */ wcstring_list_t env_get_names( int flags ); + + /** Update the PWD variable directory diff --git a/fish.cpp b/fish.cpp index b46317efd..3924f96ea 100644 --- a/fish.cpp +++ b/fish.cpp @@ -130,15 +130,6 @@ static std::string get_executable_path(const char *argv0) return std::string(argv0 ? argv0 : ""); } -/* A struct of configuration directories. - */ -struct config_paths_t -{ - wcstring data; // e.g. /usr/local/share - wcstring sysconf; // e.g. /usr/local/etc - wcstring doc; // e.g. /usr/local/share/doc/fish - wcstring bin; // e.g. /usr/local/bin -}; static struct config_paths_t determine_config_directory_paths(const char *argv0) { @@ -216,12 +207,6 @@ static struct config_paths_t determine_config_directory_paths(const char *argv0) done = true; } - /* Set the results in the environment */ - env_set(L"__fish_datadir", paths.data.c_str(), ENV_GLOBAL | ENV_EXPORT); - env_set(L"__fish_sysconfdir", paths.sysconf.c_str(), ENV_GLOBAL | ENV_EXPORT); - env_set(L"__fish_help_dir", paths.doc.c_str(), ENV_GLOBAL | ENV_EXPORT); - env_set(L"__fish_bin_dir", paths.bin.c_str(), ENV_GLOBAL | ENV_EXPORT); - return paths; } @@ -450,6 +435,8 @@ int main( int argc, char **argv ) debug( 1, _(L"Can not use the no-execute mode when running an interactive session") ); no_exec = 0; } + + const struct config_paths_t paths = determine_config_directory_paths(argv[0]); proc_init(); event_init(); @@ -457,7 +444,7 @@ int main( int argc, char **argv ) //parser_init(); builtin_init(); function_init(); - env_init(); + env_init(&paths); reader_init(); history_init(); @@ -466,8 +453,6 @@ int main( int argc, char **argv ) if (g_log_forks) printf("%d: g_fork_count: %d\n", __LINE__, g_fork_count); - /* Determine config paths */ - const struct config_paths_t paths = determine_config_directory_paths(argv[0]); if( read_init(paths) ) { if( cmd != 0 )