]> git.lyx.org Git - features.git/commitdiff
The setEnvPath stuff.
authorAngus Leeming <leeming@lyx.org>
Thu, 13 Jan 2005 10:10:16 +0000 (10:10 +0000)
committerAngus Leeming <leeming@lyx.org>
Thu, 13 Jan 2005 10:10:16 +0000 (10:10 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@9473 a592a061-630c-0410-9148-cb99ea01b6c8

src/ChangeLog
src/lyx_main.C
src/support/ChangeLog
src/support/filetools.C
src/support/filetools.h
src/support/os.h
src/support/os_os2.C
src/support/os_unix.C
src/support/os_win32.C

index fe2acaeca1af4e0fa53ff84645cf892ae6dc1431..62389552911fd950828e0100acb72eb724b0744c 100644 (file)
@@ -1,3 +1,13 @@
+2005-01-12  Angus Leeming  <leeming@lyx.org>
+
+       * 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  <leeming@lyx.org>
+
+       * 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  <leeming@lyx.org>
 
        * Makefile.am: remove the lyx_main.C special casing.
index 5e1380ae1ffc91a4cbb4ce9f6f695e6343f9fb04..cca4ee899f624aca4dcf3c501059e2678dd645b9 100644 (file)
@@ -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<string> 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<string> 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) {
index 84c8dbce370b0da67829341d537021f497943901..63617dfadf2029a38ae422db30cbc0f1e2a3ad87 100644 (file)
@@ -1,15 +1,22 @@
 2005-01-12  Angus Leeming  <leeming@lyx.org>
 
+       * 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  <leeming@lyx.org>
-
-       * os_win32.C: add #include "lstring.h" back in.
-
 2005-01-10  Angus Leeming  <leeming@lyx.org>
 
        * os.h:
index 5a354a6096141c770ca6f9150ba1f777acd24d06..d5a09c4798c0d0455932d73fa59c9f7de6d0753b 100644 (file)
@@ -399,13 +399,8 @@ vector<string> const getEnvPath(string const & name)
        typedef boost::char_separator<char> Separator;
        typedef boost::tokenizer<Separator> 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<string> const getEnvPath(string const & name)
 }
 
 
-string const GetEnvPath(string const & name)
+void setEnvPath(string const & name, vector<string> const & env)
 {
-#ifndef __EMX__
-       string const pathlist = subst(GetEnv(name), ':', ';');
+       char const separator(os::path_separator());
+       std::ostringstream ss;
+       vector<string>::const_iterator it = env.begin();
+       vector<string>::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;
 }
 
 
index 29069f88b89285fac3299e19d6cf69d23afc5c47..eeb45188a299926f6c688f216f65208f677a86cc 100644 (file)
@@ -123,6 +123,15 @@ std::string const GetEnv(std::string const & envname);
  */
 std::vector<std::string> 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<std::string> 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);
 
index 2aae334d340846e19ce60e39e0e516ff29fb81d8..ca4bd675ec9b3031ab644365bfe2e48e15f6780c 100644 (file)
@@ -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
index 9a42ebeecb5fac64b080d5f93ee84ff60505243c..aba74d9168a4c8b6ebc72188ef56b79d68a35694 100644 (file)
@@ -202,6 +202,12 @@ shell_type shell()
        return shell_;
 }
 
+
+char path_separator()
+{
+       return ';';
+}
+
 } // namespace os
 } // namespace support
 } // namespace lyx
index b5d67f34e3f220b7f344ecac71a8f2881ed554f4..d2a3e07c36d4dd1c0a19fec5dc90ab9c3247155f 100644 (file)
@@ -87,6 +87,11 @@ shell_type shell()
        return UNIX;
 }
 
+char path_separator()
+{
+       return ':';
+}
+
 } // namespace os
 } // namespace support
 } // namespace lyx
index 9feb6062e0cee5cfb281859e27ad9954bcc210b6..28b0ca104ee36d622faf213bdcce55960eadaa9b 100644 (file)
@@ -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