mpd/src/config/File.cxx

248 lines
6.6 KiB
C++
Raw Normal View History

/*
* Copyright 2003-2018 The Music Player Daemon Project
* http://www.musicpd.org
*
* 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.
*
* 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.
*/
#include "config.h"
#include "File.hxx"
2015-01-21 21:49:09 +01:00
#include "Data.hxx"
#include "Param.hxx"
#include "Block.hxx"
#include "Templates.hxx"
#include "system/Error.hxx"
2013-04-08 23:51:39 +02:00
#include "util/Tokenizer.hxx"
#include "util/StringStrip.hxx"
#include "util/StringAPI.hxx"
#include "util/Domain.hxx"
#include "util/RuntimeError.hxx"
#include "fs/FileSystem.hxx"
#include "fs/List.hxx"
2013-01-21 19:14:37 +01:00
#include "fs/Path.hxx"
#include "fs/io/FileReader.hxx"
#include "fs/io/BufferedReader.hxx"
#include "Log.hxx"
#include <assert.h>
static constexpr char CONF_COMMENT = '#';
static constexpr Domain config_file_domain("config_file");
/**
* 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;
}
static void
config_read_name_value(ConfigBlock &block, char *input, unsigned line)
{
2013-04-08 23:51:39 +02:00
Tokenizer tokenizer(input);
const char *name = tokenizer.NextWord();
assert(name != nullptr);
auto value = ExpectValueAndEnd(tokenizer);
const BlockParam *bp = block.GetBlockParam(name);
if (bp != nullptr)
throw FormatRuntimeError("\"%s\" is duplicate, first defined on line %i",
name, bp->line);
block.AddBlockParam(name, std::move(value), line);
}
static ConfigBlock
config_read_block(BufferedReader &reader)
{
ConfigBlock block(reader.GetLineNumber());
2009-07-19 15:11:37 +02:00
while (true) {
char *line = reader.ReadLine();
if (line == nullptr)
throw std::runtime_error("Expected '}' before end-of-file");
line = StripLeft(line);
2009-07-19 15:11:37 +02:00
if (*line == 0 || *line == CONF_COMMENT)
continue;
2009-07-19 15:11:37 +02:00
if (*line == '}') {
/* end of this block; return from the function
(and from this "while" loop) */
line = StripLeft(line + 1);
if (*line != 0 && *line != CONF_COMMENT)
throw std::runtime_error("Unknown tokens after '}'");
return block;
}
2009-07-19 15:11:37 +02:00
/* parse name and value */
config_read_name_value(block, line,
reader.GetLineNumber());
2009-07-19 15:11:37 +02:00
}
}
static void
ReadConfigBlock(ConfigData &config_data, BufferedReader &reader,
2015-01-22 19:08:09 +01:00
const char *name, ConfigBlockOption o,
Tokenizer &tokenizer)
{
const unsigned i = unsigned(o);
const ConfigTemplate &option = config_block_templates[i];
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());
/* now parse the block or the value */
if (tokenizer.CurrentChar() != '{')
throw std::runtime_error("'{' expected");
char *line = StripLeft(tokenizer.Rest() + 1);
if (*line != 0 && *line != CONF_COMMENT)
throw std::runtime_error("Unknown tokens after '{'");
config_data.AddBlock(o, config_read_block(reader));
}
static void
ReadConfigParam(ConfigData &config_data, BufferedReader &reader,
const char *name, ConfigOption o,
Tokenizer &tokenizer)
{
const unsigned i = unsigned(o);
const ConfigTemplate &option = config_param_templates[i];
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());
/* now parse the block or the value */
config_data.AddParam(o, ConfigParam(ExpectValueAndEnd(tokenizer),
reader.GetLineNumber()));
}
/**
* @param directory the directory used to resolve relative paths
*/
static void
ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Path directory)
{
while (true) {
char *line = reader.ReadLine();
if (line == nullptr)
return;
line = StripLeft(line);
2009-07-19 15:11:37 +02:00
if (*line == 0 || *line == CONF_COMMENT)
continue;
2009-07-19 15:11:37 +02:00
/* the first token in each line is the name, followed
by either the value or '{' */
2013-04-08 23:51:39 +02:00
Tokenizer tokenizer(line);
const char *name = tokenizer.NextWord();
assert(name != nullptr);
if (StringIsEqual(name, "include")) {
2018-07-18 12:39:39 +02:00
// TODO: detect recursion
// TODO: Config{Block,Param} have only line number but no file name
const auto pattern = AllocatedPath::Apply(directory,
AllocatedPath::FromUTF8Throw(ExpectValueAndEnd(tokenizer)));
for (const auto &path : ListWildcard(pattern))
ReadConfigFile(config_data, path);
continue;
}
if (StringIsEqual(name, "include_optional")) {
const auto pattern = AllocatedPath::Apply(directory,
AllocatedPath::FromUTF8Throw(ExpectValueAndEnd(tokenizer)));
std::forward_list<AllocatedPath> l;
try {
l = ListWildcard(pattern);
} catch (const std::system_error &e) {
/* ignore "file not found */
if (!IsFileNotFound(e) && !IsPathNotFound(e))
throw;
}
for (const auto &path : l)
if (PathExists(path))
ReadConfigFile(config_data, path);
continue;
}
2009-07-19 15:11:37 +02:00
/* get the definition of that option, and check the
"repeatable" flag */
const ConfigOption o = ParseConfigOptionName(name);
ConfigBlockOption bo;
if (o != ConfigOption::MAX) {
ReadConfigParam(config_data, reader, name, o,
tokenizer);
} else if ((bo = ParseConfigBlockOptionName(name)) != ConfigBlockOption::MAX) {
ReadConfigBlock(config_data, reader, name, bo,
tokenizer);
} else {
throw FormatRuntimeError("unrecognized parameter: %s\n",
name);
}
}
}
2013-01-30 19:59:49 +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();
FormatDebug(config_file_domain, "loading file %s", path_utf8.c_str());
2013-01-30 19:59:49 +01:00
FileReader file(path);
2013-01-30 19:59:49 +01:00
BufferedReader reader(file);
try {
ReadConfigFile(config_data, reader, path.GetDirectoryName());
} 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
}