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"
|
2004-02-24 00:41:20 +01:00
|
|
|
#include "conf.h"
|
2013-01-30 17:53:13 +01:00
|
|
|
#include "ConfigTemplates.hxx"
|
2013-01-30 18:52:14 +01:00
|
|
|
#include "ConfigParser.hxx"
|
2013-01-10 18:11:12 +01:00
|
|
|
|
|
|
|
extern "C" {
|
2004-02-24 00:41:20 +01:00
|
|
|
#include "utils.h"
|
2010-12-23 16:13:11 +01:00
|
|
|
#include "string_util.h"
|
2009-07-19 15:11:37 +02:00
|
|
|
#include "tokenizer.h"
|
2013-01-10 18:11:12 +01:00
|
|
|
}
|
|
|
|
|
2013-01-21 19:14:37 +01:00
|
|
|
#include "fs/Path.hxx"
|
2013-01-29 17:23:35 +01:00
|
|
|
#include "fs/FileSystem.hxx"
|
2010-09-25 15:00:43 +02:00
|
|
|
#include "mpd_error.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-12-28 19:54:49 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
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>
|
2009-01-18 19:45:51 +01:00
|
|
|
#include <stdlib.h>
|
2009-01-03 14:53:34 +01:00
|
|
|
#include <errno.h>
|
2008-12-29 17:28:32 +01:00
|
|
|
|
2009-02-19 09:24:59 +01:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "config"
|
|
|
|
|
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-01-30 17:52:51 +01:00
|
|
|
static GSList *config_params[unsigned(CONF_MAX)];
|
2013-01-30 17:35:33 +01:00
|
|
|
|
2009-09-24 21:40:05 +02:00
|
|
|
static void
|
|
|
|
config_param_free_callback(gpointer data, G_GNUC_UNUSED gpointer user_data)
|
|
|
|
{
|
2013-01-10 18:11:12 +01:00
|
|
|
struct config_param *param = (struct config_param *)data;
|
2009-09-24 21:40:05 +02:00
|
|
|
|
|
|
|
config_param_free(param);
|
|
|
|
}
|
|
|
|
|
2009-01-17 20:23:27 +01:00
|
|
|
void config_global_finish(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-01-30 17:35:33 +01:00
|
|
|
for (unsigned i = 0; i < G_N_ELEMENTS(config_params); ++i) {
|
|
|
|
g_slist_foreach(config_params[i],
|
2009-09-24 21:40:05 +02:00
|
|
|
config_param_free_callback, NULL);
|
2013-01-30 17:35:33 +01:00
|
|
|
g_slist_free(config_params[i]);
|
2009-06-19 07:02:13 +02:00
|
|
|
}
|
2005-11-18 13:09:05 +01:00
|
|
|
}
|
|
|
|
|
2009-01-17 20:23:27 +01:00
|
|
|
void config_global_init(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
|
|
|
|
2009-06-19 09:02:14 +02:00
|
|
|
static void
|
|
|
|
config_param_check(gpointer data, G_GNUC_UNUSED gpointer user_data)
|
|
|
|
{
|
2013-01-10 18:11:12 +01:00
|
|
|
struct config_param *param = (struct config_param *)data;
|
2009-06-19 09:02:14 +02:00
|
|
|
|
|
|
|
if (!param->used)
|
|
|
|
/* this whole config_param was not queried at all -
|
|
|
|
the feature might be disabled at compile time?
|
|
|
|
Silently ignore it here. */
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < param->num_block_params; i++) {
|
|
|
|
struct block_param *bp = ¶m->block_params[i];
|
|
|
|
|
|
|
|
if (!bp->used)
|
|
|
|
g_warning("option '%s' on line %i was not recognized",
|
|
|
|
bp->name, bp->line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void config_global_check(void)
|
|
|
|
{
|
2013-01-30 17:35:33 +01:00
|
|
|
for (unsigned i = 0; i < G_N_ELEMENTS(config_params); ++i)
|
|
|
|
g_slist_foreach(config_params[i], config_param_check, NULL);
|
2009-06-19 09:02:14 +02:00
|
|
|
}
|
|
|
|
|
2011-09-09 21:06:23 +02:00
|
|
|
static bool
|
|
|
|
config_read_name_value(struct config_param *param, char *input, unsigned line,
|
|
|
|
GError **error_r)
|
|
|
|
{
|
|
|
|
const char *name = tokenizer_next_word(&input, error_r);
|
|
|
|
if (name == NULL) {
|
|
|
|
assert(*input != 0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *value = tokenizer_next_string(&input, error_r);
|
|
|
|
if (value == NULL) {
|
|
|
|
if (*input == 0) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*input != 0 && *input != CONF_COMMENT) {
|
|
|
|
g_set_error(error_r, config_quark(), 0,
|
|
|
|
"Unknown tokens after value");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-09 21:16:46 +02:00
|
|
|
const struct block_param *bp = config_get_block_param(param, 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
config_add_block_param(param, name, value, line);
|
|
|
|
return true;
|
2011-09-09 21:06:23 +02:00
|
|
|
}
|
|
|
|
|
2009-01-17 20:23:27 +01:00
|
|
|
static struct config_param *
|
2009-09-24 21:40:07 +02:00
|
|
|
config_read_block(FILE *fp, int *count, char *string, GError **error_r)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-06-03 06:55:04 +02:00
|
|
|
struct config_param *ret = config_new_param(NULL, *count);
|
2009-09-24 21:40:07 +02:00
|
|
|
GError *error = NULL;
|
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);
|
2009-09-24 21:40:07 +02:00
|
|
|
if (line == NULL) {
|
|
|
|
config_param_free(ret);
|
|
|
|
g_set_error(error_r, config_quark(), 0,
|
|
|
|
"Expected '}' before end-of-file");
|
|
|
|
return NULL;
|
|
|
|
}
|
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) {
|
|
|
|
config_param_free(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-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
|
|
|
|
2011-09-09 21:06:23 +02:00
|
|
|
if (!config_read_name_value(ret, line, *count, &error)) {
|
2009-07-19 15:11:37 +02:00
|
|
|
assert(*line != 0);
|
2009-09-24 21:40:07 +02:00
|
|
|
config_param_free(ret);
|
|
|
|
g_propagate_prefixed_error(error_r, error,
|
|
|
|
"line %i: ", *count);
|
|
|
|
return NULL;
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
2009-07-19 15:11:37 +02:00
|
|
|
}
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
|
|
|
|
2009-09-24 21:40:07 +02:00
|
|
|
bool
|
2013-01-29 17:23:35 +01:00
|
|
|
ReadConfigFile(const Path &path, GError **error_r)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-01-29 17:23:35 +01:00
|
|
|
assert(!path.IsNull());
|
|
|
|
const std::string path_utf8 = path.ToUTF8();
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
FILE *fp;
|
|
|
|
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
|
|
|
|
2013-01-29 17:23:35 +01:00
|
|
|
g_debug("loading file %s", path_utf8.c_str());
|
2009-02-19 08:35:22 +01:00
|
|
|
|
2013-01-29 17:23:35 +01:00
|
|
|
if (!(fp = FOpen(path, "r"))) {
|
2009-09-24 21:40:07 +02:00
|
|
|
g_set_error(error_r, config_quark(), errno,
|
|
|
|
"Failed to open %s: %s",
|
2013-01-29 17:23:35 +01:00
|
|
|
path_utf8.c_str(), g_strerror(errno));
|
2009-09-24 21:40:07 +02:00
|
|
|
return false;
|
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;
|
|
|
|
GError *error = NULL;
|
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
|
|
|
|
2009-07-19 15:11:37 +02:00
|
|
|
name = tokenizer_next_word(&line, &error);
|
|
|
|
if (name == NULL) {
|
|
|
|
assert(*line != 0);
|
2009-09-24 21:40:07 +02:00
|
|
|
g_propagate_prefixed_error(error_r, error,
|
|
|
|
"line %i: ", count);
|
2011-07-18 12:38:43 +02:00
|
|
|
fclose(fp);
|
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) {
|
2009-09-24 21:40:07 +02:00
|
|
|
g_set_error(error_r, config_quark(), 0,
|
|
|
|
"unrecognized parameter in config file at "
|
|
|
|
"line %i: %s\n", count, name);
|
2011-07-18 12:38:43 +02:00
|
|
|
fclose(fp);
|
2009-09-24 21:40:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2013-01-30 17:52:51 +01:00
|
|
|
const unsigned i = ParseConfigOptionName(name);
|
2013-01-30 17:53:13 +01:00
|
|
|
const ConfigTemplate &option = config_templates[i];
|
2013-01-30 17:35:33 +01:00
|
|
|
GSList *¶ms = config_params[i];
|
|
|
|
|
|
|
|
if (params != NULL && !option.repeatable) {
|
|
|
|
param = (struct config_param *)params->data;
|
2009-09-24 21:40:07 +02:00
|
|
|
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);
|
2011-07-18 12:38:43 +02:00
|
|
|
fclose(fp);
|
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() */
|
|
|
|
|
2009-09-24 21:40:07 +02:00
|
|
|
if (*line != '{') {
|
|
|
|
g_set_error(error_r, config_quark(), 0,
|
|
|
|
"line %i: '{' expected", count);
|
2011-07-18 12:38:43 +02:00
|
|
|
fclose(fp);
|
2009-09-24 21:40:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
2009-07-19 15:11:37 +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) {
|
|
|
|
g_set_error(error_r, config_quark(), 0,
|
|
|
|
"line %i: Unknown tokens after '{'",
|
|
|
|
count);
|
2011-07-18 12:38:43 +02:00
|
|
|
fclose(fp);
|
2009-09-24 21:40:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
2009-07-19 15:11:37 +02:00
|
|
|
|
2009-09-24 21:40:07 +02:00
|
|
|
param = config_read_block(fp, &count, string, error_r);
|
2011-07-18 12:38:43 +02:00
|
|
|
if (param == NULL) {
|
|
|
|
fclose(fp);
|
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 */
|
|
|
|
|
|
|
|
value = tokenizer_next_string(&line, &error);
|
|
|
|
if (value == NULL) {
|
|
|
|
if (*line == 0)
|
2009-09-24 21:40:07 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-07-18 12:38:43 +02:00
|
|
|
fclose(fp);
|
2009-09-24 21:40:07 +02:00
|
|
|
return false;
|
2009-07-19 15:11:37 +02:00
|
|
|
}
|
|
|
|
|
2009-09-24 21:40:07 +02:00
|
|
|
if (*line != 0 && *line != CONF_COMMENT) {
|
|
|
|
g_set_error(error_r, config_quark(), 0,
|
|
|
|
"line %i: Unknown tokens after value",
|
|
|
|
count);
|
2011-07-18 12:38:43 +02:00
|
|
|
fclose(fp);
|
2009-09-24 21:40:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
2009-07-19 15:11:37 +02:00
|
|
|
|
|
|
|
param = config_new_param(value, count);
|
|
|
|
}
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2013-01-30 17:35:33 +01:00
|
|
|
params = g_slist_append(params, param);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2005-11-18 13:09:05 +01:00
|
|
|
fclose(fp);
|
2009-09-24 21:40:07 +02:00
|
|
|
|
|
|
|
return true;
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2011-09-09 23:59:43 +02:00
|
|
|
const struct config_param *
|
2013-01-30 17:52:51 +01:00
|
|
|
config_get_next_param(ConfigOption option, const struct config_param * last)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-01-30 17:52:51 +01:00
|
|
|
GSList *node = config_params[unsigned(option)];
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (last) {
|
2009-01-02 18:40:43 +01:00
|
|
|
node = g_slist_find(node, last);
|
|
|
|
if (node == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
node = g_slist_next(node);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (node == NULL)
|
|
|
|
return NULL;
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2013-01-30 17:52:51 +01:00
|
|
|
struct config_param *param = (struct config_param *)node->data;
|
2009-06-19 09:02:14 +02:00
|
|
|
param->used = true;
|
2004-10-28 07:14:55 +02:00
|
|
|
return param;
|
|
|
|
}
|
|
|
|
|
2009-01-17 20:23:58 +01:00
|
|
|
const char *
|
2013-01-30 17:52:51 +01:00
|
|
|
config_get_string(ConfigOption option, const char *default_value)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-01-30 17:52:51 +01:00
|
|
|
const struct config_param *param = config_get_param(option);
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2009-01-17 20:23:58 +01:00
|
|
|
if (param == NULL)
|
|
|
|
return default_value;
|
2004-10-28 07:14:55 +02:00
|
|
|
|
|
|
|
return param->value;
|
|
|
|
}
|
|
|
|
|
2011-09-09 21:32:12 +02:00
|
|
|
char *
|
2013-01-30 17:52:51 +01:00
|
|
|
config_dup_path(ConfigOption option, GError **error_r)
|
2009-01-18 17:54:46 +01:00
|
|
|
{
|
2011-09-09 21:32:12 +02:00
|
|
|
assert(error_r != NULL);
|
|
|
|
assert(*error_r == NULL);
|
2009-01-18 17:54:46 +01:00
|
|
|
|
2013-01-30 17:52:51 +01:00
|
|
|
const struct config_param *param = config_get_param(option);
|
2009-01-18 17:54:46 +01:00
|
|
|
if (param == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2011-09-09 22:35:15 +02:00
|
|
|
char *path = parsePath(param->value, error_r);
|
2011-09-09 21:32:12 +02:00
|
|
|
if (G_UNLIKELY(path == NULL))
|
2011-09-09 22:35:15 +02:00
|
|
|
g_prefix_error(error_r,
|
2013-01-30 17:52:51 +01:00
|
|
|
"Invalid path at line %i: ",
|
|
|
|
param->line);
|
2009-01-18 17:54:46 +01:00
|
|
|
|
2011-09-09 21:32:12 +02:00
|
|
|
return path;
|
2009-01-18 17:54:46 +01:00
|
|
|
}
|
|
|
|
|
2010-04-13 21:19:07 +02:00
|
|
|
unsigned
|
2013-01-30 17:52:51 +01:00
|
|
|
config_get_unsigned(ConfigOption option, unsigned default_value)
|
2010-04-13 21:19:07 +02:00
|
|
|
{
|
2013-01-30 17:52:51 +01:00
|
|
|
const struct config_param *param = config_get_param(option);
|
2010-04-13 21:19:07 +02:00
|
|
|
long value;
|
|
|
|
char *endptr;
|
|
|
|
|
|
|
|
if (param == NULL)
|
|
|
|
return default_value;
|
|
|
|
|
|
|
|
value = strtol(param->value, &endptr, 0);
|
|
|
|
if (*endptr != 0 || value < 0)
|
2010-09-25 15:00:43 +02:00
|
|
|
MPD_ERROR("Not a valid non-negative number in line %i",
|
|
|
|
param->line);
|
2010-04-13 21:19:07 +02:00
|
|
|
|
|
|
|
return (unsigned)value;
|
|
|
|
}
|
|
|
|
|
2009-01-21 08:46:59 +01:00
|
|
|
unsigned
|
2013-01-30 17:52:51 +01:00
|
|
|
config_get_positive(ConfigOption option, unsigned default_value)
|
2009-01-21 08:46:59 +01:00
|
|
|
{
|
2013-01-30 17:52:51 +01:00
|
|
|
const struct config_param *param = config_get_param(option);
|
2009-01-21 08:46:59 +01:00
|
|
|
long value;
|
|
|
|
char *endptr;
|
|
|
|
|
|
|
|
if (param == NULL)
|
|
|
|
return default_value;
|
|
|
|
|
|
|
|
value = strtol(param->value, &endptr, 0);
|
|
|
|
if (*endptr != 0)
|
2010-09-25 15:00:43 +02:00
|
|
|
MPD_ERROR("Not a valid number in line %i", param->line);
|
2009-01-21 08:46:59 +01:00
|
|
|
|
|
|
|
if (value <= 0)
|
2010-09-25 15:00:43 +02:00
|
|
|
MPD_ERROR("Not a positive number in line %i", param->line);
|
2009-01-21 08:46:59 +01:00
|
|
|
|
|
|
|
return (unsigned)value;
|
|
|
|
}
|
|
|
|
|
2013-01-30 17:52:51 +01:00
|
|
|
bool
|
|
|
|
config_get_bool(ConfigOption option, bool default_value)
|
2007-09-06 01:59:33 +02:00
|
|
|
{
|
2013-01-30 17:52:51 +01:00
|
|
|
const struct config_param *param = config_get_param(option);
|
2009-06-19 09:02:14 +02:00
|
|
|
bool success, value;
|
2007-09-06 01:59:33 +02:00
|
|
|
|
2009-01-17 20:23:33 +01:00
|
|
|
if (param == NULL)
|
|
|
|
return default_value;
|
2007-09-06 01:59:33 +02:00
|
|
|
|
2009-06-19 09:02:14 +02:00
|
|
|
success = get_bool(param->value, &value);
|
|
|
|
if (!success)
|
2013-01-30 17:52:51 +01:00
|
|
|
MPD_ERROR("Expected boolean value (yes, true, 1) or "
|
2010-09-25 15:00:43 +02:00
|
|
|
"(no, false, 0) on line %i\n",
|
2013-01-30 17:52:51 +01:00
|
|
|
param->line);
|
2008-11-27 19:19:34 +01:00
|
|
|
|
2009-06-19 09:02:14 +02:00
|
|
|
return value;
|
2008-11-27 19:19:34 +01:00
|
|
|
}
|