mpd/src/ConfigFile.cxx

286 lines
6.7 KiB
C++
Raw Normal View History

/*
2013-01-10 18:11:12 +01:00
* Copyright (C) 2003-2013 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 "ConfigFile.hxx"
#include "ConfigQuark.hxx"
#include "ConfigData.hxx"
2013-01-30 17:53:13 +01:00
#include "ConfigTemplates.hxx"
#include "conf.h"
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-01-21 19:14:37 +01:00
#include "fs/Path.hxx"
#include "fs/FileSystem.hxx"
#include <glib.h>
#include <assert.h>
#include <string.h>
2009-01-03 14:53:34 +01:00
#include <stdio.h>
#include <errno.h>
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "config"
Merge branches/ew r7104 thread-safety work in preparation for rewrite to use pthreads Expect no regressions against trunk (r7078), possibly minor performance improvements in update (due to fewer heap allocations), but increased stack usage. Applied the following patches: * maxpath_str for reentrancy (temporary fix, reverted) * path: start working on thread-safe variants of these methods * Re-entrancy work on path/character-set conversions * directory.c: exploreDirectory() use reentrant functions here * directory/update: more use of reentrant functions + cleanups * string_toupper: a strdup-less version of strDupToUpper * get_song_url: a static-variable-free version of getSongUrl() * Use reentrant/thread-safe get_song_url everywhere * replace rmp2amp with the reentrant version, rmp2amp_r * Get rid of the non-reentrant/non-thread-safe rpp2app, too. * buffer2array: assert strdup() returns a usable value in unit tests * replace utf8ToFsCharset and fsCharsetToUtf8 with thread-safe variants * fix storing playlists w/o absolute paths * parent_path(), a reentrant version of parentPath() * parentPath => parent_path for reentrancy and thread-safety * allow "make test" to automatically run embedded unit tests * remove convStrDup() and maxpath_str() * use MPD_PATH_MAX everywhere instead of MAXPATHLEN * path: get rid of appendSlash, pfx_path and just use pfx_dir * get_song_url: fix the ability to play songs in the top-level music_directory git-svn-id: https://svn.musicpd.org/mpd/trunk@7106 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2007-12-28 03:56:25 +01:00
#define MAX_STRING_SIZE MPD_PATH_MAX+80
#define CONF_COMMENT '#'
static bool
config_read_name_value(struct config_param *param, char *input, unsigned line,
GError **error_r)
{
2013-04-08 23:51:39 +02:00
Tokenizer tokenizer(input);
const char *name = tokenizer.NextWord(error_r);
if (name == NULL) {
2013-04-08 23:51:39 +02:00
assert(!tokenizer.IsEnd());
return false;
}
2013-04-08 23:51:39 +02:00
const char *value = tokenizer.NextString(error_r);
if (value == NULL) {
2013-04-08 23:51:39 +02:00
if (tokenizer.IsEnd()) {
assert(error_r == NULL || *error_r == NULL);
g_set_error(error_r, config_quark(), 0,
"Value missing");
} else {
assert(error_r == NULL || *error_r != NULL);
}
return false;
}
2013-04-08 23:51:39 +02:00
if (!tokenizer.IsEnd() && tokenizer.CurrentChar() != CONF_COMMENT) {
g_set_error(error_r, config_quark(), 0,
"Unknown tokens after value");
return false;
}
const struct block_param *bp = param->GetBlockParam(name);
if (bp != NULL) {
g_set_error(error_r, config_quark(), 0,
"\"%s\" is duplicate, first defined on line %i",
name, bp->line);
return false;
}
param->AddBlockParam(name, value, line);
return true;
}
static struct config_param *
config_read_block(FILE *fp, int *count, char *string, GError **error_r)
{
struct config_param *ret = new config_param(*count);
GError *error = NULL;
2009-07-19 15:11:37 +02:00
while (true) {
char *line;
2009-07-19 15:11:37 +02:00
line = fgets(string, MAX_STRING_SIZE, fp);
if (line == NULL) {
delete ret;
g_set_error(error_r, config_quark(), 0,
"Expected '}' before end-of-file");
return NULL;
}
(*count)++;
line = strchug_fast(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 = strchug_fast(line + 1);
if (*line != 0 && *line != CONF_COMMENT) {
delete ret;
g_set_error(error_r, config_quark(), 0,
"line %i: Unknown tokens after '}'",
*count);
2013-01-10 18:11:12 +01:00
return nullptr;
}
2009-07-19 15:11:37 +02:00
return ret;
}
2009-07-19 15:11:37 +02:00
/* parse name and value */
if (!config_read_name_value(ret, line, *count, &error)) {
2009-07-19 15:11:37 +02:00
assert(*line != 0);
delete ret;
g_propagate_prefixed_error(error_r, error,
"line %i: ", *count);
return NULL;
}
2009-07-19 15:11:37 +02: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
ReadConfigFile(ConfigData &config_data, FILE *fp, GError **error_r)
{
2013-01-30 19:59:49 +01:00
assert(fp != nullptr);
char string[MAX_STRING_SIZE + 1];
int count = 0;
struct config_param *param;
while (fgets(string, MAX_STRING_SIZE, fp)) {
2009-07-19 15:11:37 +02:00
char *line;
const char *name, *value;
GError *error = NULL;
count++;
line = strchug_fast(string);
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);
name = tokenizer.NextWord(&error);
2009-07-19 15:11:37 +02:00
if (name == NULL) {
2013-04-08 23:51:39 +02:00
assert(!tokenizer.IsEnd());
g_propagate_prefixed_error(error_r, error,
"line %i: ", count);
return false;
}
2009-07-19 15:11:37 +02:00
/* get the definition of that option, and check the
"repeatable" flag */
const ConfigOption o = ParseConfigOptionName(name);
if (o == CONF_MAX) {
g_set_error(error_r, config_quark(), 0,
"unrecognized parameter in config file at "
"line %i: %s\n", count, name);
return false;
}
const unsigned i = unsigned(o);
2013-01-30 17:53:13 +01:00
const ConfigTemplate &option = config_templates[i];
config_param *&head = config_data.params[i];
if (head != nullptr && !option.repeatable) {
param = head;
g_set_error(error_r, config_quark(), 0,
"config parameter \"%s\" is first defined "
"on line %i and redefined on line %i\n",
name, param->line, count);
return false;
}
2009-07-19 15:11:37 +02:00
/* now parse the block or the value */
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() != '{') {
g_set_error(error_r, config_quark(), 0,
"line %i: '{' expected", count);
return false;
}
2009-07-19 15:11:37 +02:00
2013-04-08 23:51:39 +02:00
line = strchug_fast(tokenizer.Rest() + 1);
if (*line != 0 && *line != CONF_COMMENT) {
g_set_error(error_r, config_quark(), 0,
"line %i: Unknown tokens after '{'",
count);
return false;
}
2009-07-19 15:11:37 +02:00
param = config_read_block(fp, &count, string, error_r);
if (param == NULL) {
return false;
}
2009-07-19 15:11:37 +02:00
} else {
/* a string value */
2013-04-08 23:51:39 +02:00
value = tokenizer.NextString(&error);
2009-07-19 15:11:37 +02:00
if (value == NULL) {
2013-04-08 23:51:39 +02:00
if (tokenizer.IsEnd())
g_set_error(error_r, config_quark(), 0,
"line %i: Value missing",
count);
else {
g_set_error(error_r, config_quark(), 0,
"line %i: %s", count,
error->message);
g_error_free(error);
}
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) {
g_set_error(error_r, config_quark(), 0,
"line %i: Unknown tokens after value",
count);
return false;
}
2009-07-19 15:11:37 +02:00
param = new config_param(value, count);
2009-07-19 15:11:37 +02:00
}
Append(head, param);
}
return true;
}
2013-01-30 19:59:49 +01:00
bool
ReadConfigFile(ConfigData &config_data, const Path &path, GError **error_r)
{
assert(!path.IsNull());
const std::string path_utf8 = path.ToUTF8();
g_debug("loading file %s", path_utf8.c_str());
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) {
g_set_error(error_r, config_quark(), errno,
"Failed to open %s: %s",
path_utf8.c_str(), g_strerror(errno));
return false;
}
bool result = ReadConfigFile(config_data, fp, error_r);
fclose(fp);
return result;
}