2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2013-01-10 18:11:12 +01:00
|
|
|
* Copyright (C) 2003-2013 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"
|
2013-01-30 19:44:59 +01:00
|
|
|
#include "ConfigFile.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "ConfigError.hxx"
|
2013-01-30 19:44:59 +01:00
|
|
|
#include "ConfigData.hxx"
|
2013-01-30 17:53:13 +01:00
|
|
|
#include "ConfigTemplates.hxx"
|
2013-04-08 23:51:39 +02:00
|
|
|
#include "util/Tokenizer.hxx"
|
2013-04-09 01:08:20 +02:00
|
|
|
#include "util/StringUtil.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
#include "util/Domain.hxx"
|
2013-10-17 22:00:01 +02:00
|
|
|
#include "fs/Limits.hxx"
|
2013-01-21 19:14:37 +01:00
|
|
|
#include "fs/Path.hxx"
|
2013-01-29 17:23:35 +01:00
|
|
|
#include "fs/FileSystem.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>
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <string.h>
|
2009-01-03 14:53:34 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
2008-12-29 17:28:32 +01:00
|
|
|
|
2007-12-28 03:56:25 +01:00
|
|
|
#define MAX_STRING_SIZE MPD_PATH_MAX+80
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-10-28 07:14:55 +02:00
|
|
|
#define CONF_COMMENT '#'
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
static constexpr Domain config_file_domain("config_file");
|
|
|
|
|
2011-09-09 21:06:23 +02:00
|
|
|
static bool
|
|
|
|
config_read_name_value(struct config_param *param, char *input, unsigned line,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2011-09-09 21:06:23 +02:00
|
|
|
{
|
2013-04-08 23:51:39 +02:00
|
|
|
Tokenizer tokenizer(input);
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
const char *name = tokenizer.NextWord(error);
|
2013-10-19 18:19:03 +02:00
|
|
|
if (name == nullptr) {
|
2013-04-08 23:51:39 +02:00
|
|
|
assert(!tokenizer.IsEnd());
|
2011-09-09 21:06:23 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
const char *value = tokenizer.NextString(error);
|
2013-10-19 18:19:03 +02:00
|
|
|
if (value == nullptr) {
|
2013-04-08 23:51:39 +02:00
|
|
|
if (tokenizer.IsEnd()) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(config_file_domain, "Value missing");
|
2011-09-09 21:06:23 +02:00
|
|
|
} else {
|
2013-08-10 18:02:44 +02:00
|
|
|
assert(error.IsDefined());
|
2011-09-09 21:06:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-08 23:51:39 +02:00
|
|
|
if (!tokenizer.IsEnd() && tokenizer.CurrentChar() != CONF_COMMENT) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(config_file_domain, "Unknown tokens after value");
|
2011-09-09 21:06:23 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-30 22:25:17 +01:00
|
|
|
const struct block_param *bp = param->GetBlockParam(name);
|
2013-10-19 18:19:03 +02:00
|
|
|
if (bp != nullptr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(config_file_domain,
|
|
|
|
"\"%s\" is duplicate, first defined on line %i",
|
|
|
|
name, bp->line);
|
2011-09-09 21:16:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-30 22:25:17 +01:00
|
|
|
param->AddBlockParam(name, value, line);
|
2011-09-09 21:16:46 +02:00
|
|
|
return true;
|
2011-09-09 21:06:23 +02:00
|
|
|
}
|
|
|
|
|
2009-01-17 20:23:27 +01:00
|
|
|
static struct config_param *
|
2013-08-10 18:02:44 +02:00
|
|
|
config_read_block(FILE *fp, int *count, char *string, Error &error)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-01-30 22:00:32 +01:00
|
|
|
struct config_param *ret = new config_param(*count);
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2009-07-19 15:11:37 +02:00
|
|
|
while (true) {
|
|
|
|
char *line;
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2009-07-19 15:11:37 +02:00
|
|
|
line = fgets(string, MAX_STRING_SIZE, fp);
|
2013-10-19 18:19:03 +02:00
|
|
|
if (line == nullptr) {
|
2013-01-30 22:00:32 +01:00
|
|
|
delete ret;
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(config_file_domain,
|
|
|
|
"Expected '}' before end-of-file");
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2009-09-24 21:40:07 +02:00
|
|
|
}
|
2006-10-06 12:33:27 +02:00
|
|
|
|
2004-10-28 07:14:55 +02:00
|
|
|
(*count)++;
|
2010-12-23 16:16:01 +01:00
|
|
|
line = strchug_fast(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
|
|
|
|
2010-12-23 16:16:01 +01:00
|
|
|
line = strchug_fast(line + 1);
|
2009-09-24 21:40:07 +02:00
|
|
|
if (*line != 0 && *line != CONF_COMMENT) {
|
2013-01-30 22:00:32 +01:00
|
|
|
delete ret;
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(config_file_domain,
|
|
|
|
"line %i: Unknown tokens after '}'",
|
|
|
|
*count);
|
2013-01-10 18:11:12 +01:00
|
|
|
return nullptr;
|
2009-09-24 21:40:07 +02:00
|
|
|
}
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2009-07-19 15:11:37 +02:00
|
|
|
return ret;
|
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
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!config_read_name_value(ret, line, *count, error)) {
|
2009-07-19 15:11:37 +02:00
|
|
|
assert(*line != 0);
|
2013-01-30 22:00:32 +01:00
|
|
|
delete ret;
|
2013-08-10 18:02:44 +02:00
|
|
|
error.FormatPrefix("line %i: ", *count);
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
2009-07-19 15:11:37 +02:00
|
|
|
}
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
|
|
|
|
2013-01-30 22:37:17 +01:00
|
|
|
gcc_nonnull_all
|
|
|
|
static void
|
|
|
|
Append(config_param *&head, config_param *p)
|
|
|
|
{
|
|
|
|
assert(p->next == nullptr);
|
|
|
|
|
|
|
|
config_param **i = &head;
|
|
|
|
while (*i != nullptr)
|
|
|
|
i = &(*i)->next;
|
|
|
|
|
|
|
|
*i = p;
|
|
|
|
}
|
|
|
|
|
2013-01-30 19:59:49 +01:00
|
|
|
static bool
|
2013-08-10 18:02:44 +02:00
|
|
|
ReadConfigFile(ConfigData &config_data, FILE *fp, Error &error)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-01-30 19:59:49 +01:00
|
|
|
assert(fp != nullptr);
|
2013-01-29 17:23:35 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
char string[MAX_STRING_SIZE + 1];
|
2004-08-10 18:03:03 +02:00
|
|
|
int count = 0;
|
2009-01-17 20:23:27 +01:00
|
|
|
struct config_param *param;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-12-28 19:54:49 +01:00
|
|
|
while (fgets(string, MAX_STRING_SIZE, fp)) {
|
2009-07-19 15:11:37 +02:00
|
|
|
char *line;
|
|
|
|
const char *name, *value;
|
2008-12-28 19:54:49 +01:00
|
|
|
|
2004-08-10 18:03:03 +02:00
|
|
|
count++;
|
|
|
|
|
2010-12-23 16:16:01 +01:00
|
|
|
line = strchug_fast(string);
|
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);
|
2013-08-10 18:02:44 +02:00
|
|
|
name = tokenizer.NextWord(error);
|
2013-10-19 18:19:03 +02:00
|
|
|
if (name == nullptr) {
|
2013-04-08 23:51:39 +02:00
|
|
|
assert(!tokenizer.IsEnd());
|
2013-08-10 18:02:44 +02:00
|
|
|
error.FormatPrefix("line %i: ", count);
|
2009-09-24 21:40:07 +02:00
|
|
|
return false;
|
2004-11-11 04:20:49 +01:00
|
|
|
}
|
2004-10-28 07:14:55 +02:00
|
|
|
|
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);
|
|
|
|
if (o == CONF_MAX) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(config_file_domain,
|
|
|
|
"unrecognized parameter in config file at "
|
|
|
|
"line %i: %s\n", count, name);
|
2009-09-24 21:40:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2013-01-30 22:37:17 +01:00
|
|
|
const unsigned i = unsigned(o);
|
2013-01-30 17:53:13 +01:00
|
|
|
const ConfigTemplate &option = config_templates[i];
|
2013-01-30 22:37:17 +01:00
|
|
|
config_param *&head = config_data.params[i];
|
2013-01-30 17:35:33 +01:00
|
|
|
|
2013-01-30 22:37:17 +01:00
|
|
|
if (head != nullptr && !option.repeatable) {
|
|
|
|
param = head;
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(config_file_domain,
|
|
|
|
"config parameter \"%s\" is first defined "
|
|
|
|
"on line %i and redefined on line %i\n",
|
|
|
|
name, param->line, count);
|
2009-09-24 21:40:07 +02:00
|
|
|
return false;
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
|
|
|
|
2009-07-19 15:11:37 +02:00
|
|
|
/* now parse the block or the value */
|
|
|
|
|
2013-01-30 17:35:33 +01:00
|
|
|
if (option.block) {
|
2009-07-19 15:11:37 +02:00
|
|
|
/* it's a block, call config_read_block() */
|
|
|
|
|
2013-04-08 23:51:39 +02:00
|
|
|
if (tokenizer.CurrentChar() != '{') {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(config_file_domain,
|
|
|
|
"line %i: '{' expected", count);
|
2009-09-24 21:40:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
2009-07-19 15:11:37 +02:00
|
|
|
|
2013-04-08 23:51:39 +02:00
|
|
|
line = strchug_fast(tokenizer.Rest() + 1);
|
2009-09-24 21:40:07 +02:00
|
|
|
if (*line != 0 && *line != CONF_COMMENT) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(config_file_domain,
|
|
|
|
"line %i: Unknown tokens after '{'",
|
|
|
|
count);
|
2009-09-24 21:40:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
2009-07-19 15:11:37 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
param = config_read_block(fp, &count, string, error);
|
2013-10-19 18:19:03 +02:00
|
|
|
if (param == nullptr) {
|
2009-09-24 21:40:07 +02:00
|
|
|
return false;
|
2011-07-18 12:38:43 +02:00
|
|
|
}
|
2009-07-19 15:11:37 +02:00
|
|
|
} else {
|
|
|
|
/* a string value */
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
value = tokenizer.NextString(error);
|
2013-10-19 18:19:03 +02:00
|
|
|
if (value == nullptr) {
|
2013-04-08 23:51:39 +02:00
|
|
|
if (tokenizer.IsEnd())
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(config_file_domain,
|
|
|
|
"line %i: Value missing",
|
|
|
|
count);
|
|
|
|
else
|
|
|
|
error.FormatPrefix("line %i: ", count);
|
2009-09-24 21:40:07 +02:00
|
|
|
|
|
|
|
return false;
|
2009-07-19 15:11:37 +02:00
|
|
|
}
|
|
|
|
|
2013-04-08 23:51:39 +02:00
|
|
|
if (!tokenizer.IsEnd() &&
|
|
|
|
tokenizer.CurrentChar() != CONF_COMMENT) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(config_file_domain,
|
|
|
|
"line %i: Unknown tokens after value",
|
|
|
|
count);
|
2009-09-24 21:40:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
2009-07-19 15:11:37 +02:00
|
|
|
|
2013-01-30 22:00:32 +01:00
|
|
|
param = new config_param(value, count);
|
2009-07-19 15:11:37 +02:00
|
|
|
}
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2013-01-30 22:37:17 +01:00
|
|
|
Append(head, param);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2009-09-24 21:40:07 +02:00
|
|
|
|
|
|
|
return true;
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
2013-01-30 19:59:49 +01:00
|
|
|
|
|
|
|
bool
|
2013-10-17 21:59:35 +02:00
|
|
|
ReadConfigFile(ConfigData &config_data, Path path, Error &error)
|
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
|
|
|
|
2013-02-02 15:19:25 +01:00
|
|
|
FILE *fp = FOpen(path, FOpenMode::ReadText);
|
2013-01-30 19:59:49 +01:00
|
|
|
if (fp == nullptr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.FormatErrno("Failed to open %s", path_utf8.c_str());
|
2013-01-30 19:59:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
bool result = ReadConfigFile(config_data, fp, error);
|
2013-01-30 19:59:49 +01:00
|
|
|
fclose(fp);
|
|
|
|
return result;
|
|
|
|
}
|