2
* Copyright (C) 2003-2013 The Music Player Daemon Project
3
* http://www.musicpd.org
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License along
16
* with this program; if not, write to the Free Software Foundation, Inc.,
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
#include "ConfigFile.hxx"
22
#include "ConfigError.hxx"
23
#include "ConfigData.hxx"
24
#include "ConfigTemplates.hxx"
25
#include "util/Tokenizer.hxx"
26
#include "util/StringUtil.hxx"
27
#include "util/Error.hxx"
28
#include "util/Domain.hxx"
29
#include "fs/Limits.hxx"
30
#include "fs/Path.hxx"
31
#include "fs/FileSystem.hxx"
39
#define MAX_STRING_SIZE MPD_PATH_MAX+80
41
#define CONF_COMMENT '#'
43
static constexpr Domain config_file_domain("config_file");
46
config_read_name_value(struct config_param *param, char *input, unsigned line,
49
Tokenizer tokenizer(input);
51
const char *name = tokenizer.NextWord(error);
52
if (name == nullptr) {
53
assert(!tokenizer.IsEnd());
57
const char *value = tokenizer.NextString(error);
58
if (value == nullptr) {
59
if (tokenizer.IsEnd()) {
60
error.Set(config_file_domain, "Value missing");
62
assert(error.IsDefined());
68
if (!tokenizer.IsEnd() && tokenizer.CurrentChar() != CONF_COMMENT) {
69
error.Set(config_file_domain, "Unknown tokens after value");
73
const struct block_param *bp = param->GetBlockParam(name);
75
error.Format(config_file_domain,
76
"\"%s\" is duplicate, first defined on line %i",
81
param->AddBlockParam(name, value, line);
85
static struct config_param *
86
config_read_block(FILE *fp, int *count, char *string, Error &error)
88
struct config_param *ret = new config_param(*count);
93
line = fgets(string, MAX_STRING_SIZE, fp);
94
if (line == nullptr) {
96
error.Set(config_file_domain,
97
"Expected '}' before end-of-file");
102
line = strchug_fast(line);
103
if (*line == 0 || *line == CONF_COMMENT)
107
/* end of this block; return from the function
108
(and from this "while" loop) */
110
line = strchug_fast(line + 1);
111
if (*line != 0 && *line != CONF_COMMENT) {
113
error.Format(config_file_domain,
114
"line %i: Unknown tokens after '}'",
122
/* parse name and value */
124
if (!config_read_name_value(ret, line, *count, error)) {
127
error.FormatPrefix("line %i: ", *count);
135
Append(config_param *&head, config_param *p)
137
assert(p->next == nullptr);
139
config_param **i = &head;
140
while (*i != nullptr)
147
ReadConfigFile(ConfigData &config_data, FILE *fp, Error &error)
149
assert(fp != nullptr);
151
char string[MAX_STRING_SIZE + 1];
153
struct config_param *param;
155
while (fgets(string, MAX_STRING_SIZE, fp)) {
157
const char *name, *value;
161
line = strchug_fast(string);
162
if (*line == 0 || *line == CONF_COMMENT)
165
/* the first token in each line is the name, followed
166
by either the value or '{' */
168
Tokenizer tokenizer(line);
169
name = tokenizer.NextWord(error);
170
if (name == nullptr) {
171
assert(!tokenizer.IsEnd());
172
error.FormatPrefix("line %i: ", count);
176
/* get the definition of that option, and check the
179
const ConfigOption o = ParseConfigOptionName(name);
181
error.Format(config_file_domain,
182
"unrecognized parameter in config file at "
183
"line %i: %s\n", count, name);
187
const unsigned i = unsigned(o);
188
const ConfigTemplate &option = config_templates[i];
189
config_param *&head = config_data.params[i];
191
if (head != nullptr && !option.repeatable) {
193
error.Format(config_file_domain,
194
"config parameter \"%s\" is first defined "
195
"on line %i and redefined on line %i\n",
196
name, param->line, count);
200
/* now parse the block or the value */
203
/* it's a block, call config_read_block() */
205
if (tokenizer.CurrentChar() != '{') {
206
error.Format(config_file_domain,
207
"line %i: '{' expected", count);
211
line = strchug_fast(tokenizer.Rest() + 1);
212
if (*line != 0 && *line != CONF_COMMENT) {
213
error.Format(config_file_domain,
214
"line %i: Unknown tokens after '{'",
219
param = config_read_block(fp, &count, string, error);
220
if (param == nullptr) {
226
value = tokenizer.NextString(error);
227
if (value == nullptr) {
228
if (tokenizer.IsEnd())
229
error.Format(config_file_domain,
230
"line %i: Value missing",
233
error.FormatPrefix("line %i: ", count);
238
if (!tokenizer.IsEnd() &&
239
tokenizer.CurrentChar() != CONF_COMMENT) {
240
error.Format(config_file_domain,
241
"line %i: Unknown tokens after value",
246
param = new config_param(value, count);
256
ReadConfigFile(ConfigData &config_data, Path path, Error &error)
258
assert(!path.IsNull());
259
const std::string path_utf8 = path.ToUTF8();
261
FormatDebug(config_file_domain, "loading file %s", path_utf8.c_str());
263
FILE *fp = FOpen(path, FOpenMode::ReadText);
265
error.FormatErrno("Failed to open %s", path_utf8.c_str());
269
bool result = ReadConfigFile(config_data, fp, error);