From 211c2f31361d2d6417dee98fd2c92007de7ef3f8 Mon Sep 17 00:00:00 2001 From: Angus Leeming Date: Thu, 13 Jan 2005 10:10:16 +0000 Subject: [PATCH] The setEnvPath stuff. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@9473 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/ChangeLog | 10 +++++++ src/lyx_main.C | 37 +++++++++++-------------- src/support/ChangeLog | 15 ++++++++--- src/support/filetools.C | 60 +++++++++++++++++++++++++++++++++-------- src/support/filetools.h | 9 +++++++ src/support/os.h | 36 ++++++++++++++++--------- src/support/os_os2.C | 6 +++++ src/support/os_unix.C | 5 ++++ src/support/os_win32.C | 9 +++++++ 9 files changed, 138 insertions(+), 49 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index fe2acaeca1..6238955291 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,13 @@ +2005-01-12 Angus Leeming + + * lyx_main.C (init): set the PATH variable to include the + directory containing the LyX binary when running on Mac or Windows. + +2005-01-12 Angus Leeming + + * lyx_main.C (init): remove cruft that purports to set the locale + dir. It doesn't and is not needed anyway. + 2005-01-10 Angus Leeming * Makefile.am: remove the lyx_main.C special casing. diff --git a/src/lyx_main.C b/src/lyx_main.C index 5e1380ae1f..cca4ee899f 100644 --- a/src/lyx_main.C +++ b/src/lyx_main.C @@ -343,32 +343,25 @@ void LyX::init(bool gui) signal(SIGTERM, error_handler); // SIGPIPE can be safely ignored. +#if !defined (USE_POSIX_PACKAGING) + // Add the directory containing the LyX executable to the path + // so that LyX can find things like reLyX. + if (package.build_support().empty()) { + vector path = getEnvPath("PATH"); + path.insert(path.begin(), package.binary_dir()); + setEnvPath("PATH", path); + } +#endif #if defined (USE_MACOSX_PACKAGING) - // Set PATH for LyX/Mac - // - // LyX/Mac is a relocatable application bundle; here we add to - // the PATH so it can find binaries like reLyX inside its own - // application bundle, and also append PATH elements that it - // needs to run latex, previewers, etc. - string oldpath = GetEnv("PATH"); - string newpath = "PATH=" + oldpath + ":" + package().binary_dir() + ":"; - newpath += "/sw/bin:/usr/local/bin:" - "/usr/local/teTeX/bin/powerpc-apple-darwin-current"; - PutEnv(newpath); - lyxerr[Debug::INIT] << "Running from LyX/Mac bundle. " + // This hard-coded nastiness should be moved into a LyXRC variable. + vector path = getEnvPath("PATH"); + path.insert(path.begin(), "/usr/local/teTeX/bin/powerpc-apple-darwin-current"); + path.insert(path.begin(), "/usr/local/bin"); + path.insert(path.begin(), "/sw/bin"); + lyxerr[Debug::INIT] << "Running from LyX/Mac bundle. " "Setting PATH to: " << GetEnv("PATH") << endl; #endif - // Set the locale_dir. - string const & locale_dir = package().locale_dir(); - FileInfo fi(locale_dir); - if (fi.isOK() && fi.isDir()) { - lyxerr[Debug::INIT] - << "Setting locale directory to " - << locale_dir << endl; - //gettext_init(locale_dir); - } - // Check that user LyX directory is ok. We don't do that if // running in batch mode. if (gui) { diff --git a/src/support/ChangeLog b/src/support/ChangeLog index 84c8dbce37..63617dfadf 100644 --- a/src/support/ChangeLog +++ b/src/support/ChangeLog @@ -1,15 +1,22 @@ 2005-01-12 Angus Leeming + * filetools.[Ch] (setEnvPath): new function to create a PATH-style + string from a vector of paths and to use it to set an environment + variable. + (putEnv): resurrect this from the grave. + + * os.h, os_os2.C, os_unix.C, os_win32.C (path_separator): new + function returning the character used to separate paths returned + by the PATH environment variable. + + * os_win32.C: add #include "lstring.h" back in. + * package.C.in (package): comment out the ASSERT for now. (check_env_var_dir): write one of the strings to be translated (any one, doesn't matter) on a single line so that the gettext search mechanism in po/Makefile.in.in will register package.C.in as a file containing strings that need translation. -2005-01-12 Angus Leeming - - * os_win32.C: add #include "lstring.h" back in. - 2005-01-10 Angus Leeming * os.h: diff --git a/src/support/filetools.C b/src/support/filetools.C index 5a354a6096..d5a09c4798 100644 --- a/src/support/filetools.C +++ b/src/support/filetools.C @@ -399,13 +399,8 @@ vector const getEnvPath(string const & name) typedef boost::char_separator Separator; typedef boost::tokenizer Tokenizer; -#if defined (__EMX__) || defined (_WIN32) - Separator const separator(";"); -#else - Separator const separator(":"); -#endif - string const env_var = GetEnv(name); + Separator const separator(string(1, os::path_separator()).c_str()); Tokenizer const tokens(env_var, separator); Tokenizer::const_iterator it = tokens.begin(); Tokenizer::const_iterator const end = tokens.end(); @@ -418,14 +413,57 @@ vector const getEnvPath(string const & name) } -string const GetEnvPath(string const & name) +void setEnvPath(string const & name, vector const & env) { -#ifndef __EMX__ - string const pathlist = subst(GetEnv(name), ':', ';'); + char const separator(os::path_separator()); + std::ostringstream ss; + vector::const_iterator it = env.begin(); + vector::const_iterator const end = env.end(); + for (; it != end; ++it) { + if (ss.tellp() > 0) + ss << separator; + ss << os::external_path(*it); + } + putEnv(name + "=" + ss.str()); +} + + +bool putEnv(string const & envstr) +{ + // CHECK Look at and fix this. + // f.ex. what about error checking? + +#if defined (HAVE_SETENV) + string name; + string const value = split(envstr, name, '='); + int const retval = ::setenv(name.c_str(), value.c_str(), true); +#elif defined (HAVE_PUTENV) + // this leaks, but what can we do about it? + // Is doing a getenv() and a free() of the older value + // a good idea? (JMarc) + // Actually we don't have to leak...calling putenv like this + // should be enough: ... and this is obviously not enough if putenv + // does not make a copy of the string. It is also not very wise to + // put a string on the free store. If we have to leak we should do it + // like this: + char * leaker = new char[envstr.length() + 1]; + envstr.copy(leaker, envstr.length()); + leaker[envstr.length()] = '\0'; + int const retval = ::putenv(leaker); + + // If putenv does not make a copy of the char const * this + // is very dangerous. OTOH if it does take a copy this is the + // best solution. + // The only implementation of putenv that I have seen does not + // allocate memory. _And_ after testing the putenv in glibc it + // seems that we need to make a copy of the string contents. + // I will enable the above. + //int retval = lyx::putenv(envstr.c_str()); #else - string const pathlist = os::internal_path(GetEnv(name)); + // No environment setting function. Can this happen? + int const retval = 1; //return an error condition. #endif - return rtrim(pathlist, ";"); + return retval == 0; } diff --git a/src/support/filetools.h b/src/support/filetools.h index 29069f88b8..eeb45188a2 100644 --- a/src/support/filetools.h +++ b/src/support/filetools.h @@ -123,6 +123,15 @@ std::string const GetEnv(std::string const & envname);  */ std::vector const getEnvPath(std::string const & name); +/** Set the contents of the environment variable \c name + * using the paths stored in the \c env vector. + * Each element is passed through os::external_path. + */ +void setEnvPath(std::string const & name, std::vector const & env); + +/// Set an environment variable using a string of the form "name=FOO". +bool putEnv(std::string const & envstr); + /// Substitutes active latex characters with underscores in filename std::string const MakeLatexName(std::string const & file); diff --git a/src/support/os.h b/src/support/os.h index 2aae334d34..ca4bd675ec 100644 --- a/src/support/os.h +++ b/src/support/os.h @@ -21,32 +21,44 @@ namespace lyx { namespace support { namespace os { -// enum shell_type { UNIX, // Do we have to distinguish sh and csh? CMD_EXE }; -// do some work just once +/// Do some work just once. void init(int argc, char * argv[]); -// Returns the name of the NULL device (/dev/null, null). +/// Returns the name of the NULL device (/dev/null, null). std::string const & nulldev(); -// + +/// Returns "/" on *nix, "C:/", etc on Windows. std::string current_root(); -// + +/// shell_type shell(); -// DBCS aware! + +/// Extract the path common to both @c p1 and @c p2. DBCS aware! std::string::size_type common_path(std::string const & p1, std::string const & p2); -// Converts a unix style path to host OS style. + +/// Converts a unix style path to host OS style. std::string external_path(std::string const & p); -// Converts a host OS style path to unix style. + +/// Converts a host OS style path to unix style. std::string internal_path(std::string const & p); -// is path absolute? + +/// Is the path absolute? bool is_absolute_path(std::string const & p); -// returns a string suitable to be passed to popen when -// same for popen(). - char const * popen_read_mode(); + +/** Returns a string suitable to be passed to popen when + * reading a file. + */ +char const * popen_read_mode(); + +/** The character used to separate paths returned by the + * PATH environment variable. + */ +char path_separator(); } // namespace os } // namespace support diff --git a/src/support/os_os2.C b/src/support/os_os2.C index 9a42ebeecb..aba74d9168 100644 --- a/src/support/os_os2.C +++ b/src/support/os_os2.C @@ -202,6 +202,12 @@ shell_type shell() return shell_; } + +char path_separator() +{ + return ';'; +} + } // namespace os } // namespace support } // namespace lyx diff --git a/src/support/os_unix.C b/src/support/os_unix.C index b5d67f34e3..d2a3e07c36 100644 --- a/src/support/os_unix.C +++ b/src/support/os_unix.C @@ -87,6 +87,11 @@ shell_type shell() return UNIX; } +char path_separator() +{ + return ':'; +} + } // namespace os } // namespace support } // namespace lyx diff --git a/src/support/os_win32.C b/src/support/os_win32.C index 9feb6062e0..28b0ca104e 100644 --- a/src/support/os_win32.C +++ b/src/support/os_win32.C @@ -163,6 +163,15 @@ shell_type shell() #endif } +char path_separator() +{ +#if defined (_WIN32) + return ';'; +#else // Cygwin + return ':'; +#endif +} + } // namespace os } // namespace support } // namespace lyx -- 2.39.2