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
This commit is contained in:
Angus Leeming 2005-02-15 13:45:41 +00:00
parent 46cacd9dc5
commit 099030e50a
16 changed files with 254 additions and 206 deletions

View File

@ -1,3 +1,13 @@
2005-02-15 Angus Leeming <leeming@lyx.org>
* 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 <leeming@lyx.org>
* lyxserver.C (startPipe): squash MSVC warning "local variable

View File

@ -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);

View File

@ -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"

View File

@ -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";

View File

@ -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_,

View File

@ -1,3 +1,26 @@
2005-02-15 Angus Leeming <leeming@lyx.org>
* 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 <leeming@lyx.org>
* forkedcall.C (running): call the lyx::kill wrapper function
@ -21,7 +44,7 @@
2005-01-31 Lars Gullik Bjonnes <larsbj@gullik.net>
* 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 <aalstrup@laerdal.dk>

View File

@ -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 \

130
src/support/environment.C Normal file
View File

@ -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 <config.h>
#include "support/environment.h"
#include "support/os.h"
#include <boost/tokenizer.hpp>
#include <cstdlib>
#include <map>
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<string> const getEnvPath(string const & name)
{
typedef boost::char_separator<char> Separator;
typedef boost::tokenizer<Separator> 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<string> 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<string, char *> 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<string> const & env)
{
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);
}
setEnv(name, ss.str());
}
void prependEnvPath(string const & name, string const & prefix)
{
vector<string> env_var = getEnvPath(name);
typedef boost::char_separator<char> Separator;
typedef boost::tokenizer<Separator> 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<string> reversed_tokens(tokens.begin(), tokens.end());
typedef vector<string>::const_reverse_iterator token_iterator;
token_iterator it = reversed_tokens.rbegin();
token_iterator const end = reversed_tokens.rend();
for (; it != end; ++it) {
vector<string>::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

58
src/support/environment.h Normal file
View File

@ -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 <string>
#include <vector>
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<std::string> 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<std::string> 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

View File

@ -22,15 +22,16 @@
#include <config.h>
#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 <boost/assert.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/regex.hpp>
#include <boost/tokenizer.hpp>
#include <fcntl.h>
@ -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<string> const getEnvPath(string const & name)
{
typedef boost::char_separator<char> Separator;
typedef boost::tokenizer<Separator> 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<string> vars;
for (; it != end; ++it)
vars.push_back(os::internal_path(*it));
return vars;
}
void setEnvPath(string const & name, vector<string> const & env)
{
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());
}
void prependEnvPath(string const & name, string const & prefix)
{
vector<string> env_var = getEnvPath(name);
typedef boost::char_separator<char> Separator;
typedef boost::tokenizer<Separator> 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<string> reversed_tokens(tokens.begin(), tokens.end());
typedef vector<string>::const_reverse_iterator token_iterator;
token_iterator it = reversed_tokens.rbegin();
token_iterator const end = reversed_tokens.rend();
for (; it != end; ++it) {
vector<string>::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;
}

View File

@ -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<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);
/** 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);

View File

@ -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

View File

@ -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";
}

View File

@ -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);
}

View File

@ -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 <config.h>
#include "support/lyxlib.h"
#include <cstdlib>
#include <string>
#include <map>
using std::string;
using std::map;
bool lyx::support::putenv(string const & varname, string const & value)
{
static map<string, char *> 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;
}

View File

@ -11,7 +11,7 @@
#include <config.h>
#include "support/userinfo.h"
#include "support/filetools.h"
#include "support/environment.h"
#include <boost/assert.hpp>
@ -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;
}