From 4578a490ce5a5a6325b4ff2b8f44468464de2d94 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 22 Sep 2005 15:43:22 +0000 Subject: [PATCH] * Parse multi-valued options. --- nix.conf.example | 2 +- src/libstore/globals.cc | 47 +++++++++++++++++++++++++++++------------ src/libstore/globals.hh | 2 +- src/libutil/util.cc | 15 +++++++++++++ src/libutil/util.hh | 4 ++++ 5 files changed, 54 insertions(+), 16 deletions(-) diff --git a/nix.conf.example b/nix.conf.example index 1c39dff6f2..e2735d1807 100644 --- a/nix.conf.example +++ b/nix.conf.example @@ -77,4 +77,4 @@ build-allow-root = true # # Example: # build-users = nix-builder-1 nix-builder-2 nix-builder-3 -#build-users = +build-users = diff --git a/src/libstore/globals.cc b/src/libstore/globals.cc index 8cbae54e20..9a3ac69813 100644 --- a/src/libstore/globals.cc +++ b/src/libstore/globals.cc @@ -1,6 +1,7 @@ #include "globals.hh" #include +#include string nixStore = "/UNINIT"; @@ -22,7 +23,15 @@ list buildUsers; static bool settingsRead = false; -static map settings; +static map settings; + + +template A & genericAt(T & container, unsigned int n) +{ + class T::iterator i = container.begin(); + advance(i, n); + return *i; +} static void readSettings() @@ -43,34 +52,44 @@ static void readSettings() if (hash != string::npos) line = string(line, 0, hash); - if (line.find_first_not_of(" ") == string::npos) continue; + Strings tokens = tokenizeString(line); + if (tokens.empty()) continue; - istringstream is(line); - string name, sep, value; - is >> name >> sep >> value; - if (sep != "=" || !is) + if (tokens.size() < 2 || genericAt(tokens, 1) != "=") throw Error(format("illegal configuration line `%1%' in `%2%'") % line % settingsFile); - - settings[name] = value; + + string name = genericAt(tokens, 0); + + Strings::iterator i = tokens.begin(); + advance(i, 2); + settings[name] = Strings(i, tokens.end()); }; settingsRead = true; } -string querySetting(const string & name, const string & def) +Strings querySetting(const string & name, const Strings & def) { if (!settingsRead) readSettings(); - map::iterator i = settings.find(name); + map::iterator i = settings.find(name); return i == settings.end() ? def : i->second; } bool queryBoolSetting(const string & name, bool def) { - string value = querySetting(name, def ? "true" : "false"); - if (value == "true") return true; - else if (value == "false") return false; + Strings defs; + if (def) defs.push_back("true"); else defs.push_back("false"); + + Strings value = querySetting(name, defs); + if (value.size() != 1) + throw Error(format("configuration option `%1%' should be either `true' or `false', not a list") + % name); + + string v = value.front(); + if (v == "true") return true; + else if (v == "false") return false; else throw Error(format("configuration option `%1%' should be either `true' or `false', not `%2%'") - % name % value); + % name % v); } diff --git a/src/libstore/globals.hh b/src/libstore/globals.hh index 327b1bbc3d..8ba0a03004 100644 --- a/src/libstore/globals.hh +++ b/src/libstore/globals.hh @@ -63,7 +63,7 @@ extern bool buildAllowRoot; extern list buildUsers; -string querySetting(const string & name, const string & def); +Strings querySetting(const string & name, const Strings & def); bool queryBoolSetting(const string & name, bool def); diff --git a/src/libutil/util.cc b/src/libutil/util.cc index 574c2566b7..2e684e9c14 100644 --- a/src/libutil/util.cc +++ b/src/libutil/util.cc @@ -665,6 +665,21 @@ Strings unpackStrings(const string & s) } +Strings tokenizeString(const string & s, const string & separators) +{ + Strings result; + string::size_type pos = s.find_first_not_of(separators, 0); + while (pos != string::npos) { + string::size_type end = s.find_first_of(separators, pos + 1); + if (end == string::npos) end = s.size(); + string token(s, pos, end - pos); + result.push_back(token); + pos = s.find_first_not_of(separators, end); + } + return result; +} + + string statusToString(int status) { if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { diff --git a/src/libutil/util.hh b/src/libutil/util.hh index beb98fe7c6..9e7eb11bd1 100644 --- a/src/libutil/util.hh +++ b/src/libutil/util.hh @@ -261,6 +261,10 @@ string packStrings(const Strings & strings); Strings unpackStrings(const string & s); +/* String tokenizer. */ +Strings tokenizeString(const string & s, const string & separators = " \t\n\r"); + + /* Convert the exit status of a child as returned by wait() into an error string. */ string statusToString(int status);