From 099030e50aedeb3a16c256afd61ef113e031e9f6 Mon Sep 17 00:00:00 2001 From: Angus Leeming Date: Tue, 15 Feb 2005 13:45:41 +0000 Subject: [PATCH] Rearrange and rename the environment variable setter/getter functions. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@9634 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/ChangeLog | 10 +++ src/lyx_main.C | 7 +- src/lyxfunc.C | 1 + src/lyxrc.C | 5 +- src/lyxsocket.C | 5 +- src/support/ChangeLog | 25 +++++++- src/support/Makefile.am | 3 +- src/support/environment.C | 130 ++++++++++++++++++++++++++++++++++++++ src/support/environment.h | 58 +++++++++++++++++ src/support/filetools.C | 130 +++----------------------------------- src/support/filetools.h | 29 --------- src/support/lyxlib.h | 2 - src/support/os_os2.C | 4 +- src/support/package.C.in | 7 +- src/support/putenv.C | 38 ----------- src/support/userinfo.C | 6 +- 16 files changed, 254 insertions(+), 206 deletions(-) create mode 100644 src/support/environment.C create mode 100644 src/support/environment.h delete mode 100644 src/support/putenv.C diff --git a/src/ChangeLog b/src/ChangeLog index b26aa048f0..f5d4251677 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,13 @@ +2005-02-15 Angus Leeming + + * lyx_main.C (error_handler): + * lyxfunc.C: + * lyxrc.C (setDefaults): + s/GetEnv/getEnv/. + #include "environment.h". + + * lyxsocket.C (LyXServerSocket): s/putenv/setEnv/. + 2005-02-15 Angus Leeming * lyxserver.C (startPipe): squash MSVC warning "local variable diff --git a/src/lyx_main.C b/src/lyx_main.C index d5074964f9..67deea775e 100644 --- a/src/lyx_main.C +++ b/src/lyx_main.C @@ -45,6 +45,7 @@ #include "frontends/lyx_gui.h" #include "frontends/LyXView.h" +#include "support/environment.h" #include "support/filetools.h" #include "support/lyxlib.h" #include "support/os.h" @@ -63,7 +64,7 @@ using lyx::support::bformat; using lyx::support::createDirectory; using lyx::support::createLyXTmpDir; using lyx::support::FileSearch; -using lyx::support::GetEnv; +using lyx::support::getEnv; using lyx::support::i18nLibFileSearch; using lyx::support::LibFileSearch; using lyx::support::package; @@ -369,9 +370,9 @@ static void error_handler(int err_sig) #ifdef SIGHUP if (err_sig == SIGSEGV || - (err_sig != SIGHUP && !GetEnv("LYXDEBUG").empty())) + (err_sig != SIGHUP && !getEnv("LYXDEBUG").empty())) #else - if (err_sig == SIGSEGV || !GetEnv("LYXDEBUG").empty()) + if (err_sig == SIGSEGV || !getEnv("LYXDEBUG").empty()) #endif lyx::support::abort(); exit(0); diff --git a/src/lyxfunc.C b/src/lyxfunc.C index 6085a5bbe6..ef7ccd59c7 100644 --- a/src/lyxfunc.C +++ b/src/lyxfunc.C @@ -78,6 +78,7 @@ #include "frontends/Menubar.h" #include "frontends/Toolbars.h" +#include "support/environment.h" #include "support/filefilterlist.h" #include "support/filetools.h" #include "support/forkedcontr.h" diff --git a/src/lyxrc.C b/src/lyxrc.C index 96350ce760..13497997c4 100644 --- a/src/lyxrc.C +++ b/src/lyxrc.C @@ -34,6 +34,7 @@ #include "graphics/GraphicsTypes.h" #include "support/convert.h" +#include "support/environment.h" #include "support/filetools.h" #include "support/lstrings.h" #include "support/userinfo.h" @@ -41,7 +42,7 @@ using lyx::support::ascii_lowercase; using lyx::support::bformat; using lyx::support::ExpandPath; -using lyx::support::GetEnv; +using lyx::support::getEnv; using lyx::support::LibFileSearch; using lyx::support::token; @@ -188,7 +189,7 @@ void LyXRC::setDefaults() { ui_file = "default"; // Get printer from the environment. If fail, use default "", // assuming that everything is set up correctly. - printer = GetEnv("PRINTER"); + printer = getEnv("PRINTER"); print_adapt_output = false; print_command = "dvips"; print_evenpage_flag = "-B"; diff --git a/src/lyxsocket.C b/src/lyxsocket.C index 17c3316524..29a2480aa0 100644 --- a/src/lyxsocket.C +++ b/src/lyxsocket.C @@ -21,6 +21,7 @@ #include "frontends/lyx_gui.h" +#include "support/environment.h" #include "support/lyxlib.h" #include "support/socktools.h" @@ -50,9 +51,9 @@ LyXServerSocket::LyXServerSocket(LyXFunc * f, string const & addr) // These env vars are used by DVI inverse search // Needed by xdvi - lyx::support::putenv("XEDITOR", "lyxclient -g %f %l"); + lyx::support::setEnv("XEDITOR", "lyxclient -g %f %l"); // Needed by lyxclient - lyx::support::putenv("LYXSOCKET", address_); + lyx::support::setEnv("LYXSOCKET", address_); lyx_gui::register_socket_callback( fd_, diff --git a/src/support/ChangeLog b/src/support/ChangeLog index ff0ae18346..f451fee9f7 100644 --- a/src/support/ChangeLog +++ b/src/support/ChangeLog @@ -1,3 +1,26 @@ +2005-02-15 Angus Leeming + + * environment.[Ch]: new files. Move the environment setter/getter + functions here from filetools.[Ch]. In the process: + rename GetEnv as getEnv. + rename putEnv as setEnv. Change the signature of the function + to take a pair of arguments (a name and a value) rather than the + existing coded "name=value" single argument. Merge the putenv.C + code into setEnv. + + * lyxlib.h (putenv): remove declaration. + * putenv.C: removed. + + * Makefile.am: add environment.[Ch]. Remove putenv.C. + + * filetools.[Ch]: remove environment setter/getter functions. + + * os_os2.C (init): s/GetEnvPath/getEnvPath/. Left over from an + earlier change. + + * package.C.in (get_home_dir, extract_env_var_dir): + * userinfo.C (user_email): s/GetEnv/getEnv/. + 2005-02-03 Angus Leeming * forkedcall.C (running): call the lyx::kill wrapper function @@ -21,7 +44,7 @@ 2005-01-31 Lars Gullik Bjonnes - * fs_extras.C: add changes from Asgers Win32 patch. + * fs_extras.C: add changes from Asger's Win32 patch. 2005-01-31 Asger Ottar Alstrup diff --git a/src/support/Makefile.am b/src/support/Makefile.am index a039c82768..e066fe3b8d 100644 --- a/src/support/Makefile.am +++ b/src/support/Makefile.am @@ -27,6 +27,8 @@ libsupport_la_SOURCES = \ copied_ptr.h \ cow_ptr.h \ debugstream.h \ + environment.h \ + environment.C \ filefilterlist.C \ filefilterlist.h \ filename.C \ @@ -63,7 +65,6 @@ libsupport_la_SOURCES = \ path.h \ package.C \ package.h \ - putenv.C \ rename.C \ socktools.C \ socktools.h \ diff --git a/src/support/environment.C b/src/support/environment.C new file mode 100644 index 0000000000..d06858216f --- /dev/null +++ b/src/support/environment.C @@ -0,0 +1,130 @@ +/** + * \file environment.C + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author Angus Leeming + * \author João Luis M. Assirati + * \author Lars Gullik Bjønnes + * + * Full author contact details are available in file CREDITS. + */ + +#include + +#include "support/environment.h" +#include "support/os.h" + +#include + +#include +#include + + +using std::string; +using std::vector; + +namespace lyx { +namespace support { + +string const getEnv(string const & envname) +{ + // f.ex. what about error checking? + char const * const ch = getenv(envname.c_str()); + string const envstr = !ch ? "" : ch; + return envstr; +} + + +vector const getEnvPath(string const & name) +{ + typedef boost::char_separator Separator; + typedef boost::tokenizer Tokenizer; + + 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(); + + std::vector vars; + for (; it != end; ++it) + vars.push_back(os::internal_path(*it)); + + return vars; +} + + +bool setEnv(string const & name, string const & value) +{ + // CHECK Look at and fix this. + // f.ex. what about error checking? + +#if defined (HAVE_SETENV) + int const retval = ::setenv(name.c_str(), value.c_str(), true); + +#elif defined (HAVE_PUTENV) + static std::map varmap; + + string envstr = name + '=' + value; + char * newptr = new char[envstr.size() + 1]; + envstr.copy(newptr, envstr.length()); + newptr[envstr.length()] = '\0'; + int const retval = ::putenv(newptr); + + char * oldptr = varmap[name]; + if (oldptr) + delete oldptr; + varmap[name] = newptr; + +#else +#error No environment-setting function has been defined. +#endif + return retval == 0; +} + + +void setEnvPath(string const & name, vector const & env) +{ + 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); + } + setEnv(name, ss.str()); +} + + +void prependEnvPath(string const & name, string const & prefix) +{ + vector env_var = getEnvPath(name); + + typedef boost::char_separator Separator; + typedef boost::tokenizer Tokenizer; + + Separator const separator(string(1, os::path_separator()).c_str()); + + // Prepend each new element to the list, removing identical elements + // that occur later in the list. + Tokenizer const tokens(prefix, separator); + vector reversed_tokens(tokens.begin(), tokens.end()); + + typedef vector::const_reverse_iterator token_iterator; + token_iterator it = reversed_tokens.rbegin(); + token_iterator const end = reversed_tokens.rend(); + for (; it != end; ++it) { + vector::iterator remove_it = + std::remove(env_var.begin(), env_var.end(), *it); + env_var.erase(remove_it, env_var.end()); + env_var.insert(env_var.begin(), *it); + } + + setEnvPath(name, env_var); +} + +} // namespace support +} // namespace lyx diff --git a/src/support/environment.h b/src/support/environment.h new file mode 100644 index 0000000000..6893762f90 --- /dev/null +++ b/src/support/environment.h @@ -0,0 +1,58 @@ +// -*- C++ -*- +/** + * \file environment.h + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author Angus Leeming + * + * Full author contact details are available in file CREDITS. + */ + +#ifndef LYX_ENVIRONMENT_H +#define LYX_ENVIRONMENT_H + +#include +#include + +namespace lyx { +namespace support { + +/// @returns the contents of the environment variable @c name. +std::string const getEnv(std::string const & envname); + +/** @returns the contents of the environment variable @c name, + * split into path elements using the OS-dependent separator token. + * Each element is then passed through os::internal_path() to + * guarantee that it is in the form of a unix-style path. + * If the environment variable is not set, then the function returns + * an empty vector. + */ +std::vector const getEnvPath(std::string const & name); + +/** Set the contents of the environment variable @c name to @c value. + * @returns true if the variable was set successfully. + */ +bool setEnv(std::string const & name, std::string const & value); + +/** 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(). + * Multiple elements are concatenated into a single string using + * os::path_separator(). + */ +void setEnvPath(std::string const & name, std::vector const & env); + +/** Prepend a list of paths to that returned by the environment variable. + * Identical paths occurring later in the list are removed. + * @param name the name of the environment variable. + * @prefix the list of paths in OS-native syntax. + * Eg "/foo/bar:/usr/bin:/usr/local/bin" on *nix, + * "C:\foo\bar;C:\windows" on Windows. + */ +void prependEnvPath(std::string const & name, std::string const & prefix); + +} // namespace support +} // namespace lyx + +#endif // LYX_ENVIRONMENT_H diff --git a/src/support/filetools.C b/src/support/filetools.C index 45cce5bb3a..4671f28960 100644 --- a/src/support/filetools.C +++ b/src/support/filetools.C @@ -22,15 +22,16 @@ #include #include "support/convert.h" -#include "support/systemcall.h" +#include "support/environment.h" #include "support/filetools.h" -#include "support/lstrings.h" #include "support/forkedcontr.h" #include "support/fs_extras.h" -#include "support/package.h" -#include "support/path.h" +#include "support/lstrings.h" #include "support/lyxlib.h" #include "support/os.h" +#include "support/package.h" +#include "support/path.h" +#include "support/systemcall.h" // FIXME Interface violation #include "gettext.h" @@ -39,7 +40,6 @@ #include #include #include -#include #include @@ -268,17 +268,17 @@ i18nLibFileSearch(string const & dir, string const & name, /* [Otherwise] We have to proceed with the POSIX methods of looking to `LC_ALL', `LC_xxx', and `LANG'. */ - string lang = GetEnv("LC_ALL"); + string lang = getEnv("LC_ALL"); if (lang.empty()) { - lang = GetEnv("LC_MESSAGES"); + lang = getEnv("LC_MESSAGES"); if (lang.empty()) { - lang = GetEnv("LANG"); + lang = getEnv("LANG"); if (lang.empty()) lang = "C"; } } - string const language = GetEnv("LANGUAGE"); + string const language = getEnv("LANGUAGE"); if (lang != "C" && lang != "POSIX" && !language.empty()) lang = language; @@ -330,116 +330,6 @@ string const LibScriptSearch(string const & command_in) } -string const GetEnv(string const & envname) -{ - // f.ex. what about error checking? - char const * const ch = getenv(envname.c_str()); - string const envstr = !ch ? "" : ch; - return envstr; -} - - -vector const getEnvPath(string const & name) -{ - typedef boost::char_separator Separator; - typedef boost::tokenizer Tokenizer; - - 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(); - - std::vector vars; - for (; it != end; ++it) - vars.push_back(os::internal_path(*it)); - - return vars; -} - - -void setEnvPath(string const & name, vector const & env) -{ - 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()); -} - - -void prependEnvPath(string const & name, string const & prefix) -{ - vector env_var = getEnvPath(name); - - typedef boost::char_separator Separator; - typedef boost::tokenizer Tokenizer; - - Separator const separator(string(1, os::path_separator()).c_str()); - - // Prepend each new element to the list, removing identical elements - // that occur later in the list. - Tokenizer const tokens(prefix, separator); - vector reversed_tokens(tokens.begin(), tokens.end()); - - typedef vector::const_reverse_iterator token_iterator; - token_iterator it = reversed_tokens.rbegin(); - token_iterator const end = reversed_tokens.rend(); - for (; it != end; ++it) { - vector::iterator remove_it = - std::remove(env_var.begin(), env_var.end(), *it); - env_var.erase(remove_it, env_var.end()); - env_var.insert(env_var.begin(), *it); - } - - setEnvPath(name, env_var); -} - - -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 - // No environment setting function. Can this happen? - int const retval = 1; //return an error condition. -#endif - return retval == 0; -} - - namespace { string const createTmpDir(string const & tempdir, string const & mask) @@ -771,7 +661,7 @@ string const ReplaceEnvironmentPath(string const & path) if (!what[0].matched) break; } - result = what.str(1) + GetEnv(what.str(2)) + what.str(3); + result = what.str(1) + getEnv(what.str(2)) + what.str(3); } return result; } diff --git a/src/support/filetools.h b/src/support/filetools.h index a34e981ff3..bdba4bca59 100644 --- a/src/support/filetools.h +++ b/src/support/filetools.h @@ -104,35 +104,6 @@ i18nLibFileSearch(std::string const & dir, std::string const & name, */ std::string const LibScriptSearch(std::string const & command); -/// -std::string const GetEnv(std::string const & envname); - -/** Return the contents of the environment variable \c name, - * split using the OS-dependent token separating elements. - * Each element is then passed through os::internal_path to - * guarantee that it is in the form of a unix-stype path. - * If the environment variable is not set, then returns an empty vector. - */ -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); - -/** Prepend a list of paths to that returned by the environment variable. - * Identical paths occurring later in the list are removed. - * @param name the name of the environment variable. - * @prefix the list of paths in OS-native syntax. - * Eg "/foo/bar:/usr/bin:/usr/local/bin" on *nix, - * "C:\foo\bar;C:\windows" on Windows. - */ -void prependEnvPath(std::string const & name, std::string const & prefix); - -/// 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/lyxlib.h b/src/support/lyxlib.h index 235a113744..fa06e1de11 100644 --- a/src/support/lyxlib.h +++ b/src/support/lyxlib.h @@ -40,8 +40,6 @@ int kill(int pid, int sig); void abort(); /// create the given directory with the given mode int mkdir(std::string const & pathname, unsigned long int mode); -/// put variable=value as a C std::string into the environment -bool putenv(std::string const & varname, std::string const & value); /// unlink the given file int unlink(std::string const & file); /// (securely) create a temporary file in the given dir with the given prefix diff --git a/src/support/os_os2.C b/src/support/os_os2.C index e49b3317f6..86713af6ee 100644 --- a/src/support/os_os2.C +++ b/src/support/os_os2.C @@ -44,10 +44,10 @@ void init(int argc, char * argv[]) exit(rc); // OS/2 cmd.exe has another use for '&' - string sh = OnlyFilename(GetEnvPath("EMXSHELL")); + string sh = OnlyFilename(getEnvPath("EMXSHELL")); if (sh.empty()) { // COMSPEC is set, unless user unsets - sh = OnlyFilename(GetEnvPath("COMSPEC")); + sh = OnlyFilename(getEnvPath("COMSPEC")); if (sh.empty()) sh = "cmd.exe"; } diff --git a/src/support/package.C.in b/src/support/package.C.in index f7db4a77fa..9a9a2afc7b 100644 --- a/src/support/package.C.in +++ b/src/support/package.C.in @@ -19,6 +19,7 @@ #include "debug.h" #include "gettext.h" +#include "support/environment.h" #include "support/filetools.h" #include "support/lstrings.h" #include "support/os.h" @@ -319,9 +320,9 @@ string const get_document_dir(string const & home_dir) string const get_home_dir() { #if defined (USE_WINDOWS_PACKAGING) - string const home_dir = GetEnv("USERPROFILE"); + string const home_dir = getEnv("USERPROFILE"); #else // Posix-like. - string const home_dir = GetEnv("HOME"); + string const home_dir = getEnv("HOME"); #endif return os::internal_path(home_dir); @@ -650,7 +651,7 @@ bool check_command_line_dir(string const & dir, // The environment variable @c env_var expands to a (single) file path. string const extract_env_var_dir(string const & env_var) { - string const dir = os::internal_path(GetEnv(env_var)); + string const dir = os::internal_path(getEnv(env_var)); return dir.empty() ? dir : MakeAbsPath(dir); } diff --git a/src/support/putenv.C b/src/support/putenv.C deleted file mode 100644 index efb26fa9e5..0000000000 --- a/src/support/putenv.C +++ /dev/null @@ -1,38 +0,0 @@ -/** - * \file putenv.C - * This file is part of LyX, the document processor. - * Licence details can be found in the file COPYING. - * - * \author Lars Gullik Bjønnes - * \author João Luis M. Assirati - * - * Full author contact details are available in file CREDITS. - */ - -#include - -#include "support/lyxlib.h" - -#include -#include -#include - -using std::string; -using std::map; - -bool lyx::support::putenv(string const & varname, string const & value) -{ - static map varmap; - - string str = varname + '=' + value; - char * newptr = new char[str.size() + 1]; - newptr[str.copy(newptr, string::npos)] = '\0'; - bool status = (::putenv(newptr) == 0); - - char * oldptr = varmap[varname]; - if (oldptr) - delete oldptr; - varmap[varname] = newptr; - - return status; -} diff --git a/src/support/userinfo.C b/src/support/userinfo.C index e10cd6a17d..758ce34572 100644 --- a/src/support/userinfo.C +++ b/src/support/userinfo.C @@ -11,7 +11,7 @@ #include #include "support/userinfo.h" -#include "support/filetools.h" +#include "support/environment.h" #include @@ -40,9 +40,9 @@ string const user_name() string const user_email() { - string email = GetEnv("EMAIL_ADDRESS"); + string email = getEnv("EMAIL_ADDRESS"); if (email.empty()) - email = GetEnv("EMAIL"); + email = getEnv("EMAIL"); return email; }