2005-02-15 13:45:41 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file environment.C
|
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
|
*
|
|
|
|
|
* \author Angus Leeming
|
2005-02-21 14:11:58 +00:00
|
|
|
|
* \author Jo<EFBFBD>o Luis M. Assirati
|
|
|
|
|
* \author Lars Gullik Bj<EFBFBD>nnes
|
2005-02-15 13:45:41 +00:00
|
|
|
|
*
|
|
|
|
|
* 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>
|
2005-02-15 18:43:41 +00:00
|
|
|
|
#include <sstream>
|
2005-02-15 13:45:41 +00:00
|
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
using std::vector;
|
|
|
|
|
|
2005-02-15 18:43:41 +00:00
|
|
|
|
|
2005-02-15 13:45:41 +00:00
|
|
|
|
namespace lyx {
|
|
|
|
|
namespace support {
|
|
|
|
|
|
|
|
|
|
string const getEnv(string const & envname)
|
|
|
|
|
{
|
|
|
|
|
// f.ex. what about error checking?
|
|
|
|
|
char const * const ch = getenv(envname.c_str());
|
2007-01-18 20:47:27 +00:00
|
|
|
|
return ch ? to_utf8(from_local8bit(ch)) : string();
|
2005-02-15 13:45:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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?
|
|
|
|
|
|
2007-01-18 20:47:27 +00:00
|
|
|
|
string const encoded(to_local8bit(from_utf8(value)));
|
2005-02-15 13:45:41 +00:00
|
|
|
|
#if defined (HAVE_SETENV)
|
2007-01-18 20:47:27 +00:00
|
|
|
|
int const retval = ::setenv(name.c_str(), encoded.c_str(), true);
|
2005-02-15 13:45:41 +00:00
|
|
|
|
|
|
|
|
|
#elif defined (HAVE_PUTENV)
|
|
|
|
|
static std::map<string, char *> varmap;
|
|
|
|
|
|
2007-01-18 20:47:27 +00:00
|
|
|
|
string envstr = name + '=' + encoded;
|
2005-02-15 13:45:41 +00:00
|
|
|
|
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;
|
2005-02-15 18:56:07 +00:00
|
|
|
|
vector<string>::const_iterator const begin = env.begin();
|
2005-02-15 13:45:41 +00:00
|
|
|
|
vector<string>::const_iterator const end = env.end();
|
2005-02-15 18:56:07 +00:00
|
|
|
|
vector<string>::const_iterator it = begin;
|
2005-02-15 13:45:41 +00:00
|
|
|
|
for (; it != end; ++it) {
|
2005-02-15 18:56:07 +00:00
|
|
|
|
if (it != begin)
|
2005-02-15 13:45:41 +00:00
|
|
|
|
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
|