2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2018-07-16 19:49:30 +02:00
|
|
|
* Copyright 2003-2018 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-02-24 00:41:20 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2004-02-24 00:41:20 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2018-07-16 19:50:07 +02:00
|
|
|
#include "File.hxx"
|
2015-01-21 21:49:09 +01:00
|
|
|
#include "Data.hxx"
|
2015-01-21 21:14:25 +01:00
|
|
|
#include "Param.hxx"
|
2015-01-21 22:13:44 +01:00
|
|
|
#include "Block.hxx"
|
2018-07-16 19:50:07 +02:00
|
|
|
#include "Templates.hxx"
|
2013-04-08 23:51:39 +02:00
|
|
|
#include "util/Tokenizer.hxx"
|
2017-07-05 17:20:02 +02:00
|
|
|
#include "util/StringStrip.hxx"
|
2018-07-17 20:12:42 +02:00
|
|
|
#include "util/StringAPI.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Domain.hxx"
|
2015-12-16 11:12:30 +01:00
|
|
|
#include "util/RuntimeError.hxx"
|
2013-01-21 19:14:37 +01:00
|
|
|
#include "fs/Path.hxx"
|
2015-01-21 23:36:57 +01:00
|
|
|
#include "fs/io/FileReader.hxx"
|
|
|
|
#include "fs/io/BufferedReader.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2008-12-28 19:54:49 +01:00
|
|
|
|
2009-01-25 17:37:45 +01:00
|
|
|
#include <assert.h>
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2015-01-21 22:12:16 +01:00
|
|
|
static constexpr char CONF_COMMENT = '#';
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
static constexpr Domain config_file_domain("config_file");
|
|
|
|
|
2018-07-17 20:14:43 +02:00
|
|
|
/**
|
|
|
|
* Read a string value as the last token of a line. Throws on error.
|
|
|
|
*/
|
|
|
|
static auto
|
|
|
|
ExpectValueAndEnd(Tokenizer &tokenizer)
|
|
|
|
{
|
|
|
|
auto value = tokenizer.NextString();
|
|
|
|
if (!value)
|
|
|
|
throw std::runtime_error("Value missing");
|
|
|
|
|
|
|
|
if (!tokenizer.IsEnd() && tokenizer.CurrentChar() != CONF_COMMENT)
|
|
|
|
throw std::runtime_error("Unknown tokens after value");
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2015-12-16 11:34:26 +01:00
|
|
|
static void
|
|
|
|
config_read_name_value(ConfigBlock &block, char *input, unsigned line)
|
2011-09-09 21:06:23 +02:00
|
|
|
{
|
2013-04-08 23:51:39 +02:00
|
|
|
Tokenizer tokenizer(input);
|
|
|
|
|
2015-12-16 11:34:26 +01:00
|
|
|
const char *name = tokenizer.NextWord();
|
|
|
|
assert(name != nullptr);
|
2011-09-09 21:06:23 +02:00
|
|
|
|
2018-07-17 20:14:43 +02:00
|
|
|
auto value = ExpectValueAndEnd(tokenizer);
|
2011-09-09 21:06:23 +02:00
|
|
|
|
2015-01-21 22:13:44 +01:00
|
|
|
const BlockParam *bp = block.GetBlockParam(name);
|
2015-12-16 11:12:30 +01:00
|
|
|
if (bp != nullptr)
|
|
|
|
throw FormatRuntimeError("\"%s\" is duplicate, first defined on line %i",
|
|
|
|
name, bp->line);
|
2011-09-09 21:16:46 +02:00
|
|
|
|
2018-07-17 20:14:43 +02:00
|
|
|
block.AddBlockParam(name, std::move(value), line);
|
2011-09-09 21:06:23 +02:00
|
|
|
}
|
|
|
|
|
2018-07-18 11:21:59 +02:00
|
|
|
static ConfigBlock
|
2015-12-16 11:05:33 +01:00
|
|
|
config_read_block(BufferedReader &reader)
|
2018-07-16 19:49:30 +02:00
|
|
|
{
|
2018-07-18 11:21:59 +02:00
|
|
|
ConfigBlock block(reader.GetLineNumber());
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2009-07-19 15:11:37 +02:00
|
|
|
while (true) {
|
2015-01-21 23:36:57 +01:00
|
|
|
char *line = reader.ReadLine();
|
2015-12-16 11:05:33 +01:00
|
|
|
if (line == nullptr)
|
|
|
|
throw std::runtime_error("Expected '}' before end-of-file");
|
2006-10-06 12:33:27 +02:00
|
|
|
|
2014-08-07 14:53:07 +02:00
|
|
|
line = StripLeft(line);
|
2009-07-19 15:11:37 +02:00
|
|
|
if (*line == 0 || *line == CONF_COMMENT)
|
|
|
|
continue;
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2009-07-19 15:11:37 +02:00
|
|
|
if (*line == '}') {
|
|
|
|
/* end of this block; return from the function
|
|
|
|
(and from this "while" loop) */
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2014-08-07 14:53:07 +02:00
|
|
|
line = StripLeft(line + 1);
|
2015-12-16 11:12:30 +01:00
|
|
|
if (*line != 0 && *line != CONF_COMMENT)
|
|
|
|
throw std::runtime_error("Unknown tokens after '}'");
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2018-07-18 10:17:45 +02:00
|
|
|
return block;
|
2004-11-11 04:20:49 +01:00
|
|
|
}
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2009-07-19 15:11:37 +02:00
|
|
|
/* parse name and value */
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2018-07-18 11:21:59 +02:00
|
|
|
config_read_name_value(block, line,
|
2015-12-16 11:34:26 +01:00
|
|
|
reader.GetLineNumber());
|
2009-07-19 15:11:37 +02:00
|
|
|
}
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
|
|
|
|
2015-12-16 11:05:33 +01:00
|
|
|
static void
|
2015-01-21 22:13:44 +01:00
|
|
|
ReadConfigBlock(ConfigData &config_data, BufferedReader &reader,
|
2015-01-22 19:08:09 +01:00
|
|
|
const char *name, ConfigBlockOption o,
|
2015-12-16 11:05:33 +01:00
|
|
|
Tokenizer &tokenizer)
|
2015-01-21 22:13:44 +01:00
|
|
|
{
|
|
|
|
const unsigned i = unsigned(o);
|
|
|
|
const ConfigTemplate &option = config_block_templates[i];
|
2018-07-18 10:17:45 +02:00
|
|
|
|
|
|
|
if (!option.repeatable)
|
|
|
|
if (const auto *block = config_data.GetBlock(o))
|
|
|
|
throw FormatRuntimeError("config parameter \"%s\" is first defined "
|
|
|
|
"on line %d and redefined on line %u\n",
|
|
|
|
name, block->line,
|
|
|
|
reader.GetLineNumber());
|
2015-01-21 22:13:44 +01:00
|
|
|
|
|
|
|
/* now parse the block or the value */
|
|
|
|
|
2015-12-16 11:12:30 +01:00
|
|
|
if (tokenizer.CurrentChar() != '{')
|
2018-07-16 19:49:30 +02:00
|
|
|
throw std::runtime_error("'{' expected");
|
2015-01-21 22:13:44 +01:00
|
|
|
|
|
|
|
char *line = StripLeft(tokenizer.Rest() + 1);
|
2015-12-16 11:12:30 +01:00
|
|
|
if (*line != 0 && *line != CONF_COMMENT)
|
2018-07-16 19:49:30 +02:00
|
|
|
throw std::runtime_error("Unknown tokens after '{'");
|
2015-01-21 22:13:44 +01:00
|
|
|
|
2018-07-18 10:17:45 +02:00
|
|
|
config_data.AddBlock(o, config_read_block(reader));
|
2013-01-30 22:37:17 +01:00
|
|
|
}
|
|
|
|
|
2015-12-16 11:34:26 +01:00
|
|
|
static void
|
2015-01-21 23:23:56 +01:00
|
|
|
ReadConfigParam(ConfigData &config_data, BufferedReader &reader,
|
|
|
|
const char *name, ConfigOption o,
|
2015-12-16 11:34:26 +01:00
|
|
|
Tokenizer &tokenizer)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2015-01-21 23:23:56 +01:00
|
|
|
const unsigned i = unsigned(o);
|
2015-01-21 22:13:44 +01:00
|
|
|
const ConfigTemplate &option = config_param_templates[i];
|
2018-07-18 10:17:45 +02:00
|
|
|
|
|
|
|
if (!option.repeatable)
|
|
|
|
if (const auto *param = config_data.GetParam(o))
|
|
|
|
throw FormatRuntimeError("config parameter \"%s\" is first defined "
|
|
|
|
"on line %d and redefined on line %u\n",
|
|
|
|
name, param->line,
|
|
|
|
reader.GetLineNumber());
|
2015-01-21 23:23:56 +01:00
|
|
|
|
|
|
|
/* now parse the block or the value */
|
|
|
|
|
2018-07-18 11:21:59 +02:00
|
|
|
config_data.AddParam(o, ConfigParam(ExpectValueAndEnd(tokenizer),
|
|
|
|
reader.GetLineNumber()));
|
2015-01-21 23:23:56 +01:00
|
|
|
}
|
|
|
|
|
2018-07-17 20:12:42 +02:00
|
|
|
/**
|
|
|
|
* @param directory the directory used to resolve relative paths
|
|
|
|
*/
|
2015-12-16 11:05:33 +01:00
|
|
|
static void
|
2018-07-17 20:12:42 +02:00
|
|
|
ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Path directory)
|
2015-01-21 23:23:56 +01:00
|
|
|
{
|
2015-01-21 23:36:57 +01:00
|
|
|
while (true) {
|
|
|
|
char *line = reader.ReadLine();
|
|
|
|
if (line == nullptr)
|
2015-12-16 11:05:33 +01:00
|
|
|
return;
|
2015-01-21 23:36:57 +01:00
|
|
|
|
|
|
|
line = StripLeft(line);
|
2009-07-19 15:11:37 +02:00
|
|
|
if (*line == 0 || *line == CONF_COMMENT)
|
|
|
|
continue;
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2009-07-19 15:11:37 +02:00
|
|
|
/* the first token in each line is the name, followed
|
|
|
|
by either the value or '{' */
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2013-04-08 23:51:39 +02:00
|
|
|
Tokenizer tokenizer(line);
|
2015-12-16 11:34:26 +01:00
|
|
|
const char *name = tokenizer.NextWord();
|
|
|
|
assert(name != nullptr);
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2018-07-17 20:12:42 +02:00
|
|
|
if (StringIsEqual(name, "include")) {
|
2018-07-18 12:39:39 +02:00
|
|
|
// TODO: detect recursion
|
2018-07-17 20:12:42 +02:00
|
|
|
// TODO: Config{Block,Param} have only line number but no file name
|
|
|
|
// TODO: support wildcards (include "conf.d/*.conf")
|
|
|
|
// TODO: add "include_optional"
|
2018-07-18 13:09:04 +02:00
|
|
|
const auto path = AllocatedPath::Apply(directory,
|
|
|
|
AllocatedPath::FromUTF8Throw(ExpectValueAndEnd(tokenizer)));
|
|
|
|
ReadConfigFile(config_data, path);
|
2018-07-17 20:12:42 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-07-19 15:11:37 +02:00
|
|
|
/* get the definition of that option, and check the
|
|
|
|
"repeatable" flag */
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2013-01-30 17:52:51 +01:00
|
|
|
const ConfigOption o = ParseConfigOptionName(name);
|
2015-01-21 22:13:44 +01:00
|
|
|
ConfigBlockOption bo;
|
2015-01-21 23:23:56 +01:00
|
|
|
if (o != ConfigOption::MAX) {
|
2015-12-16 11:34:26 +01:00
|
|
|
ReadConfigParam(config_data, reader, name, o,
|
|
|
|
tokenizer);
|
2015-01-21 22:13:44 +01:00
|
|
|
} else if ((bo = ParseConfigBlockOptionName(name)) != ConfigBlockOption::MAX) {
|
2015-12-16 11:05:33 +01:00
|
|
|
ReadConfigBlock(config_data, reader, name, bo,
|
|
|
|
tokenizer);
|
2015-01-21 23:23:56 +01:00
|
|
|
} else {
|
2018-07-16 19:49:30 +02:00
|
|
|
throw FormatRuntimeError("unrecognized parameter: %s\n",
|
|
|
|
name);
|
2009-09-24 21:40:07 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
2013-01-30 19:59:49 +01:00
|
|
|
|
2015-12-16 11:05:33 +01:00
|
|
|
void
|
|
|
|
ReadConfigFile(ConfigData &config_data, Path path)
|
2013-01-30 19:59:49 +01:00
|
|
|
{
|
|
|
|
assert(!path.IsNull());
|
|
|
|
const std::string path_utf8 = path.ToUTF8();
|
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(config_file_domain, "loading file %s", path_utf8.c_str());
|
2013-01-30 19:59:49 +01:00
|
|
|
|
2015-12-16 11:05:33 +01:00
|
|
|
FileReader file(path);
|
2013-01-30 19:59:49 +01:00
|
|
|
|
2015-01-21 23:36:57 +01:00
|
|
|
BufferedReader reader(file);
|
2018-07-16 19:49:30 +02:00
|
|
|
|
|
|
|
try {
|
2018-07-17 20:12:42 +02:00
|
|
|
ReadConfigFile(config_data, reader, path.GetDirectoryName());
|
2018-07-16 19:49:30 +02:00
|
|
|
} catch (...) {
|
|
|
|
std::throw_with_nested(FormatRuntimeError("Error in %s line %u",
|
|
|
|
path_utf8.c_str(),
|
|
|
|
reader.GetLineNumber()));
|
|
|
|
}
|
2013-01-30 19:59:49 +01:00
|
|
|
}
|