ConfigGlobal.cxx 4.62 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * 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 "ConfigGlobal.hxx"
#include "ConfigParser.hxx"
23
#include "Data.hxx"
24
#include "Param.hxx"
25
#include "Block.hxx"
26
#include "ConfigFile.hxx"
Max Kellermann's avatar
Max Kellermann committed
27
#include "ConfigPath.hxx"
28
#include "ConfigError.hxx"
29
#include "fs/Path.hxx"
30
#include "fs/AllocatedPath.hxx"
31
#include "util/Error.hxx"
32
#include "system/FatalError.hxx"
33
#include "Log.hxx"
34

35
#include <stdlib.h>
36 37 38 39 40

static ConfigData config_data;

void config_global_finish(void)
{
41
	config_data.Clear();
42 43 44 45 46 47
}

void config_global_init(void)
{
}

48 49
void
ReadConfigFile(Path path)
50
{
51
	return ReadConfigFile(config_data, path);
52 53 54
}

static void
55
Check(const ConfigBlock &block)
56
{
57 58
	if (!block.used)
		/* this whole block was not queried at all -
59 60 61 62
		   the feature might be disabled at compile time?
		   Silently ignore it here. */
		return;

63
	for (const auto &i : block.block_params) {
64
		if (!i.used)
65 66 67
			FormatWarning(config_domain,
				      "option '%s' on line %i was not recognized",
				      i.name.c_str(), i.line);
68 69 70 71 72
	}
}

void config_global_check(void)
{
73 74 75
	for (auto i : config_data.blocks)
		for (const auto *p = i; p != nullptr; p = p->next)
			Check(*p);
76 77
}

78 79
const config_param *
config_get_param(ConfigOption option)
80
{
81
	config_param *param = config_data.params[unsigned(option)];
82 83
	if (param != nullptr)
		param->used = true;
84 85 86
	return param;
}

87 88 89 90 91 92 93 94 95 96 97
const ConfigBlock *
config_get_block(ConfigBlockOption option)
{
	ConfigBlock *block = config_data.blocks[unsigned(option)];
	if (block != nullptr)
		block->used = true;
	return block;
}

const ConfigBlock *
config_find_block(ConfigBlockOption option, const char *key, const char *value)
98
{
99 100 101
	for (const auto *block = config_get_block(option);
	     block != nullptr; block = block->next) {
		const char *value2 = block->GetBlockValue(key);
102 103
		if (value2 == nullptr)
			FormatFatalError("block without '%s' name in line %d",
104
					 key, block->line);
105 106

		if (strcmp(value2, value) == 0)
107
			return block;
108 109 110 111 112
	}

	return nullptr;
}

113 114 115 116 117
const char *
config_get_string(ConfigOption option, const char *default_value)
{
	const struct config_param *param = config_get_param(option);

118
	if (param == nullptr)
119 120
		return default_value;

121
	return param->value.c_str();
122 123
}

124
AllocatedPath
125
config_get_path(ConfigOption option, Error &error)
126 127
{
	const struct config_param *param = config_get_param(option);
128
	if (param == nullptr)
129
		return AllocatedPath::Null();
130

131 132 133
	return config_parse_path(param, error);
}

134
AllocatedPath
135 136
config_parse_path(const struct config_param *param, Error & error)
{
137
	AllocatedPath path = ParsePath(param->value.c_str(), error);
138
	if (gcc_unlikely(path.IsNull()))
139 140
		error.FormatPrefix("Invalid path at line %i: ",
				   param->line);
141 142 143 144 145 146 147 148 149 150 151

	return path;
}

unsigned
config_get_unsigned(ConfigOption option, unsigned default_value)
{
	const struct config_param *param = config_get_param(option);
	long value;
	char *endptr;

152
	if (param == nullptr)
153 154
		return default_value;

155
	value = strtol(param->value.c_str(), &endptr, 0);
156
	if (*endptr != 0 || value < 0)
157 158
		FormatFatalError("Not a valid non-negative number in line %i",
				 param->line);
159 160 161 162 163 164 165 166 167 168 169

	return (unsigned)value;
}

unsigned
config_get_positive(ConfigOption option, unsigned default_value)
{
	const struct config_param *param = config_get_param(option);
	long value;
	char *endptr;

170
	if (param == nullptr)
171 172
		return default_value;

173
	value = strtol(param->value.c_str(), &endptr, 0);
174
	if (*endptr != 0)
175
		FormatFatalError("Not a valid number in line %i", param->line);
176 177

	if (value <= 0)
178 179
		FormatFatalError("Not a positive number in line %i",
				 param->line);
180 181 182 183 184 185 186 187 188 189

	return (unsigned)value;
}

bool
config_get_bool(ConfigOption option, bool default_value)
{
	const struct config_param *param = config_get_param(option);
	bool success, value;

190
	if (param == nullptr)
191 192
		return default_value;

193
	success = get_bool(param->value.c_str(), &value);
194
	if (!success)
195 196 197
		FormatFatalError("Expected boolean value (yes, true, 1) or "
				 "(no, false, 0) on line %i\n",
				 param->line);
198 199 200

	return value;
}