2017-04-13 16:55:38 +03:00
|
|
|
|
#include "config.hh"
|
|
|
|
|
#include "args.hh"
|
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
void Config::set(const std::string & name, const std::string & value)
|
|
|
|
|
{
|
|
|
|
|
auto i = _settings.find(name);
|
|
|
|
|
if (i == _settings.end())
|
|
|
|
|
throw UsageError("unknown setting '%s'", name);
|
|
|
|
|
i->second.setting->set(value);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 21:53:23 +03:00
|
|
|
|
void Config::addSetting(AbstractSetting * setting)
|
2017-04-13 16:55:38 +03:00
|
|
|
|
{
|
|
|
|
|
_settings.emplace(setting->name, Config::SettingData{false, setting});
|
|
|
|
|
for (auto & alias : setting->aliases)
|
|
|
|
|
_settings.emplace(alias, Config::SettingData{true, setting});
|
|
|
|
|
|
|
|
|
|
bool set = false;
|
|
|
|
|
|
|
|
|
|
auto i = initials.find(setting->name);
|
|
|
|
|
if (i != initials.end()) {
|
|
|
|
|
setting->set(i->second);
|
|
|
|
|
initials.erase(i);
|
|
|
|
|
set = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto & alias : setting->aliases) {
|
|
|
|
|
auto i = initials.find(alias);
|
|
|
|
|
if (i != initials.end()) {
|
|
|
|
|
if (set)
|
|
|
|
|
warn("setting '%s' is set, but it's an alias of '%s' which is also set",
|
|
|
|
|
alias, setting->name);
|
|
|
|
|
else {
|
|
|
|
|
setting->set(i->second);
|
|
|
|
|
initials.erase(i);
|
|
|
|
|
set = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 21:53:23 +03:00
|
|
|
|
void Config::warnUnknownSettings()
|
2017-04-13 16:55:38 +03:00
|
|
|
|
{
|
|
|
|
|
for (auto & i : initials)
|
|
|
|
|
warn("unknown setting '%s'", i.first);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 21:53:23 +03:00
|
|
|
|
StringMap Config::getSettings()
|
2017-04-13 16:55:38 +03:00
|
|
|
|
{
|
2017-04-13 21:53:23 +03:00
|
|
|
|
StringMap res;
|
2017-04-13 16:55:38 +03:00
|
|
|
|
for (auto & opt : _settings)
|
|
|
|
|
if (!opt.second.isAlias)
|
2017-04-13 21:53:23 +03:00
|
|
|
|
res.emplace(opt.first, opt.second.setting->to_string());
|
2017-04-13 16:55:38 +03:00
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 21:53:23 +03:00
|
|
|
|
void Config::applyConfigFile(const Path & path, bool fatal)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
string contents = readFile(path);
|
|
|
|
|
|
|
|
|
|
unsigned int pos = 0;
|
|
|
|
|
|
|
|
|
|
while (pos < contents.size()) {
|
|
|
|
|
string line;
|
|
|
|
|
while (pos < contents.size() && contents[pos] != '\n')
|
|
|
|
|
line += contents[pos++];
|
|
|
|
|
pos++;
|
|
|
|
|
|
|
|
|
|
string::size_type hash = line.find('#');
|
|
|
|
|
if (hash != string::npos)
|
|
|
|
|
line = string(line, 0, hash);
|
|
|
|
|
|
|
|
|
|
vector<string> tokens = tokenizeString<vector<string> >(line);
|
|
|
|
|
if (tokens.empty()) continue;
|
|
|
|
|
|
|
|
|
|
if (tokens.size() < 2 || tokens[1] != "=")
|
|
|
|
|
throw UsageError("illegal configuration line ‘%1%’ in ‘%2%’", line, path);
|
|
|
|
|
|
|
|
|
|
string name = tokens[0];
|
|
|
|
|
|
|
|
|
|
vector<string>::iterator i = tokens.begin();
|
|
|
|
|
advance(i, 2);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
set(name, concatStringsSep(" ", Strings(i, tokens.end()))); // FIXME: slow
|
|
|
|
|
} catch (UsageError & e) {
|
|
|
|
|
if (fatal) throw;
|
|
|
|
|
warn("in configuration file '%s': %s", path, e.what());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
} catch (SysError &) { }
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 16:55:38 +03:00
|
|
|
|
AbstractSetting::AbstractSetting(
|
|
|
|
|
const std::string & name,
|
|
|
|
|
const std::string & description,
|
|
|
|
|
const std::set<std::string> & aliases)
|
|
|
|
|
: name(name), description(description), aliases(aliases)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<> void Setting<std::string>::set(const std::string & str)
|
|
|
|
|
{
|
|
|
|
|
value = str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<> std::string Setting<std::string>::to_string()
|
|
|
|
|
{
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 21:53:23 +03:00
|
|
|
|
template<typename T, typename Tag>
|
|
|
|
|
void Setting<T, Tag>::set(const std::string & str)
|
2017-04-13 16:55:38 +03:00
|
|
|
|
{
|
2017-04-13 18:54:05 +03:00
|
|
|
|
static_assert(std::is_integral<T>::value, "Integer required.");
|
2017-04-13 21:53:23 +03:00
|
|
|
|
if (!string2Int(str, value))
|
2017-04-13 16:55:38 +03:00
|
|
|
|
throw UsageError("setting '%s' has invalid value '%s'", name, str);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 21:53:23 +03:00
|
|
|
|
template<typename T, typename Tag>
|
|
|
|
|
std::string Setting<T, Tag>::to_string()
|
2017-04-13 16:55:38 +03:00
|
|
|
|
{
|
2017-04-13 18:54:05 +03:00
|
|
|
|
static_assert(std::is_integral<T>::value, "Integer required.");
|
2017-04-13 16:55:38 +03:00
|
|
|
|
return std::to_string(value);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 21:53:23 +03:00
|
|
|
|
bool AbstractSetting::parseBool(const std::string & str)
|
2017-04-13 16:55:38 +03:00
|
|
|
|
{
|
2017-04-13 17:31:28 +03:00
|
|
|
|
if (str == "true" || str == "yes" || str == "1")
|
2017-04-13 21:53:23 +03:00
|
|
|
|
return true;
|
2017-04-13 17:31:28 +03:00
|
|
|
|
else if (str == "false" || str == "no" || str == "0")
|
2017-04-13 21:53:23 +03:00
|
|
|
|
return false;
|
2017-04-13 17:31:28 +03:00
|
|
|
|
else
|
|
|
|
|
throw UsageError("Boolean setting '%s' has invalid value '%s'", name, str);
|
2017-04-13 16:55:38 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 21:53:23 +03:00
|
|
|
|
template<> void Setting<bool>::set(const std::string & str)
|
|
|
|
|
{
|
|
|
|
|
value = parseBool(str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string AbstractSetting::printBool(bool b)
|
|
|
|
|
{
|
|
|
|
|
return b ? "true" : "false";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-04-13 16:55:38 +03:00
|
|
|
|
template<> std::string Setting<bool>::to_string()
|
|
|
|
|
{
|
2017-04-13 21:53:23 +03:00
|
|
|
|
return printBool(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<> void Setting<Strings>::set(const std::string & str)
|
|
|
|
|
{
|
|
|
|
|
value = tokenizeString<Strings>(str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<> std::string Setting<Strings>::to_string()
|
|
|
|
|
{
|
|
|
|
|
return concatStringsSep(" ", value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<> void Setting<StringSet>::set(const std::string & str)
|
|
|
|
|
{
|
|
|
|
|
value = tokenizeString<StringSet>(str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<> std::string Setting<StringSet>::to_string()
|
|
|
|
|
{
|
|
|
|
|
return concatStringsSep(" ", value);
|
2017-04-13 16:55:38 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 18:54:05 +03:00
|
|
|
|
template class Setting<int>;
|
|
|
|
|
template class Setting<unsigned int>;
|
|
|
|
|
template class Setting<long>;
|
2017-04-14 14:59:39 +03:00
|
|
|
|
template class Setting<unsigned long>;
|
|
|
|
|
template class Setting<long long>;
|
|
|
|
|
template class Setting<unsigned long long>;
|
2017-04-13 18:54:05 +03:00
|
|
|
|
|
2017-04-13 16:55:38 +03:00
|
|
|
|
void PathSetting::set(const std::string & str)
|
|
|
|
|
{
|
|
|
|
|
if (str == "") {
|
|
|
|
|
if (allowEmpty)
|
|
|
|
|
value = "";
|
|
|
|
|
else
|
|
|
|
|
throw UsageError("setting '%s' cannot be empty", name);
|
|
|
|
|
} else
|
|
|
|
|
value = canonPath(str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|