2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2010-01-01 05:55:13 +01:00
|
|
|
* Copyright (C) 2003-2010 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"
|
|
|
|
#include "utils.h"
|
2009-07-19 15:11:37 +02:00
|
|
|
#include "tokenizer.h"
|
2007-12-28 03:56:25 +01:00
|
|
|
#include "path.h"
|
2009-11-10 21:14:22 +01:00
|
|
|
#include "glib_compat.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 '#'
|
|
|
|
|
2009-01-17 20:23:27 +01:00
|
|
|
struct config_entry {
|
2009-06-19 07:02:13 +02:00
|
|
|
const char *const name;
|
|
|
|
const bool repeatable;
|
|
|
|
const bool block;
|
2009-01-02 18:40:43 +01:00
|
|
|
|
|
|
|
GSList *params;
|
2009-01-17 20:23:27 +01:00
|
|
|
};
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2009-06-19 07:02:13 +02:00
|
|
|
static struct config_entry config_entries[] = {
|
|
|
|
{ .name = CONF_MUSIC_DIR, false, false },
|
|
|
|
{ .name = CONF_PLAYLIST_DIR, false, false },
|
|
|
|
{ .name = CONF_FOLLOW_INSIDE_SYMLINKS, false, false },
|
|
|
|
{ .name = CONF_FOLLOW_OUTSIDE_SYMLINKS, false, false },
|
|
|
|
{ .name = CONF_DB_FILE, false, false },
|
|
|
|
{ .name = CONF_STICKER_FILE, false, false },
|
|
|
|
{ .name = CONF_LOG_FILE, false, false },
|
|
|
|
{ .name = CONF_PID_FILE, false, false },
|
|
|
|
{ .name = CONF_STATE_FILE, false, false },
|
|
|
|
{ .name = CONF_USER, false, false },
|
2009-07-19 08:17:55 +02:00
|
|
|
{ .name = CONF_GROUP, false, false },
|
2009-06-19 07:02:13 +02:00
|
|
|
{ .name = CONF_BIND_TO_ADDRESS, true, false },
|
|
|
|
{ .name = CONF_PORT, false, false },
|
|
|
|
{ .name = CONF_LOG_LEVEL, false, false },
|
|
|
|
{ .name = CONF_ZEROCONF_NAME, false, false },
|
|
|
|
{ .name = CONF_ZEROCONF_ENABLED, false, false },
|
|
|
|
{ .name = CONF_PASSWORD, true, false },
|
|
|
|
{ .name = CONF_DEFAULT_PERMS, false, false },
|
|
|
|
{ .name = CONF_AUDIO_OUTPUT, true, true },
|
|
|
|
{ .name = CONF_AUDIO_OUTPUT_FORMAT, false, false },
|
|
|
|
{ .name = CONF_MIXER_TYPE, false, false },
|
|
|
|
{ .name = CONF_REPLAYGAIN, false, false },
|
|
|
|
{ .name = CONF_REPLAYGAIN_PREAMP, false, false },
|
|
|
|
{ .name = CONF_REPLAYGAIN_MISSING_PREAMP, false, false },
|
|
|
|
{ .name = CONF_VOLUME_NORMALIZATION, false, false },
|
|
|
|
{ .name = CONF_SAMPLERATE_CONVERTER, false, false },
|
|
|
|
{ .name = CONF_AUDIO_BUFFER_SIZE, false, false },
|
|
|
|
{ .name = CONF_BUFFER_BEFORE_PLAY, false, false },
|
|
|
|
{ .name = CONF_HTTP_PROXY_HOST, false, false },
|
|
|
|
{ .name = CONF_HTTP_PROXY_PORT, false, false },
|
|
|
|
{ .name = CONF_HTTP_PROXY_USER, false, false },
|
|
|
|
{ .name = CONF_HTTP_PROXY_PASSWORD, false, false },
|
|
|
|
{ .name = CONF_CONN_TIMEOUT, false, false },
|
|
|
|
{ .name = CONF_MAX_CONN, false, false },
|
|
|
|
{ .name = CONF_MAX_PLAYLIST_LENGTH, false, false },
|
|
|
|
{ .name = CONF_MAX_COMMAND_LIST_SIZE, false, false },
|
|
|
|
{ .name = CONF_MAX_OUTPUT_BUFFER_SIZE, false, false },
|
|
|
|
{ .name = CONF_FS_CHARSET, false, false },
|
|
|
|
{ .name = CONF_ID3V1_ENCODING, false, false },
|
|
|
|
{ .name = CONF_METADATA_TO_USE, false, false },
|
|
|
|
{ .name = CONF_SAVE_ABSOLUTE_PATHS, false, false },
|
|
|
|
{ .name = CONF_DECODER, true, true },
|
|
|
|
{ .name = CONF_INPUT, true, true },
|
|
|
|
{ .name = CONF_GAPLESS_MP3_PLAYBACK, false, false },
|
2009-10-13 16:19:21 +02:00
|
|
|
{ .name = CONF_PLAYLIST_PLUGIN, true, true },
|
2009-10-26 19:14:40 +01:00
|
|
|
{ .name = CONF_AUTO_UPDATE, false, false },
|
2009-07-05 06:54:45 +02:00
|
|
|
{ .name = "filter", true, true },
|
2009-06-19 07:02:13 +02:00
|
|
|
};
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2009-06-19 09:02:14 +02:00
|
|
|
static bool
|
|
|
|
get_bool(const char *value, bool *value_r)
|
2007-09-06 01:59:33 +02:00
|
|
|
{
|
|
|
|
static const char *t[] = { "yes", "true", "1", NULL };
|
|
|
|
static const char *f[] = { "no", "false", "0", NULL };
|
|
|
|
|
2009-06-19 09:02:14 +02:00
|
|
|
if (string_array_contains(t, value)) {
|
|
|
|
*value_r = true;
|
2009-06-19 09:02:13 +02:00
|
|
|
return true;
|
2009-06-19 09:02:14 +02:00
|
|
|
}
|
2009-06-19 09:02:13 +02:00
|
|
|
|
2009-06-19 09:02:14 +02:00
|
|
|
if (string_array_contains(f, value)) {
|
|
|
|
*value_r = false;
|
|
|
|
return true;
|
|
|
|
}
|
2009-06-19 09:02:13 +02:00
|
|
|
|
2009-06-19 09:02:14 +02:00
|
|
|
return false;
|
2007-09-06 01:59:33 +02:00
|
|
|
}
|
|
|
|
|
2009-01-17 20:23:27 +01:00
|
|
|
struct config_param *
|
2009-06-03 06:55:04 +02:00
|
|
|
config_new_param(const char *value, int line)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-01-17 20:23:27 +01:00
|
|
|
struct config_param *ret = g_new(struct config_param, 1);
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!value)
|
|
|
|
ret->value = NULL;
|
|
|
|
else
|
2008-12-28 19:54:49 +01:00
|
|
|
ret->value = g_strdup(value);
|
2004-10-28 07:14:55 +02:00
|
|
|
|
|
|
|
ret->line = line;
|
|
|
|
|
2009-01-17 20:23:27 +01:00
|
|
|
ret->num_block_params = 0;
|
|
|
|
ret->block_params = NULL;
|
2009-06-19 09:02:14 +02:00
|
|
|
ret->used = false;
|
2004-10-28 07:14:55 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-03-29 19:58:16 +02:00
|
|
|
static void
|
2009-09-24 21:40:05 +02:00
|
|
|
config_param_free(struct config_param *param)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-01-03 14:53:34 +01:00
|
|
|
g_free(param->value);
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2009-06-03 06:55:09 +02:00
|
|
|
for (unsigned i = 0; i < param->num_block_params; i++) {
|
2009-01-17 20:23:27 +01:00
|
|
|
g_free(param->block_params[i].name);
|
|
|
|
g_free(param->block_params[i].value);
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
|
|
|
|
2009-01-17 20:23:27 +01:00
|
|
|
if (param->num_block_params)
|
|
|
|
g_free(param->block_params);
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2009-01-03 14:53:34 +01:00
|
|
|
g_free(param);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2009-09-24 21:40:05 +02:00
|
|
|
static void
|
|
|
|
config_param_free_callback(gpointer data, G_GNUC_UNUSED gpointer user_data)
|
|
|
|
{
|
|
|
|
struct config_param *param = data;
|
|
|
|
|
|
|
|
config_param_free(param);
|
|
|
|
}
|
|
|
|
|
2009-01-17 20:23:27 +01:00
|
|
|
static struct config_entry *
|
2009-01-02 18:40:43 +01:00
|
|
|
config_entry_get(const char *name)
|
|
|
|
{
|
2009-06-19 07:02:13 +02:00
|
|
|
for (unsigned i = 0; i < G_N_ELEMENTS(config_entries); ++i) {
|
|
|
|
struct config_entry *entry = &config_entries[i];
|
2009-01-02 18:40:43 +01:00
|
|
|
if (strcmp(entry->name, name) == 0)
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-01-17 20:23:27 +01:00
|
|
|
void config_global_finish(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-06-19 07:02:13 +02:00
|
|
|
for (unsigned i = 0; i < G_N_ELEMENTS(config_entries); ++i) {
|
|
|
|
struct config_entry *entry = &config_entries[i];
|
|
|
|
|
2009-09-24 21:40:05 +02:00
|
|
|
g_slist_foreach(entry->params,
|
|
|
|
config_param_free_callback, NULL);
|
2009-06-19 07:02:13 +02:00
|
|
|
g_slist_free(entry->params);
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
struct config_param *param = data;
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < G_N_ELEMENTS(config_entries); ++i) {
|
|
|
|
struct config_entry *entry = &config_entries[i];
|
|
|
|
|
|
|
|
g_slist_foreach(entry->params, config_param_check, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-24 21:40:07 +02:00
|
|
|
bool
|
2009-06-03 06:55:04 +02:00
|
|
|
config_add_block_param(struct config_param * param, const char *name,
|
2009-09-24 21:40:07 +02:00
|
|
|
const char *value, int line, GError **error_r)
|
2004-10-28 07:14:55 +02:00
|
|
|
{
|
2009-01-17 20:23:27 +01:00
|
|
|
struct block_param *bp;
|
|
|
|
|
2009-06-19 07:02:14 +02:00
|
|
|
bp = config_get_block_param(param, name);
|
2009-09-24 21:40:07 +02:00
|
|
|
if (bp != NULL) {
|
|
|
|
g_set_error(error_r, config_quark(), 0,
|
|
|
|
"\"%s\" first defined on line %i, and "
|
|
|
|
"redefined on line %i\n", name,
|
|
|
|
bp->line, line);
|
|
|
|
return false;
|
|
|
|
}
|
2009-06-19 07:02:14 +02:00
|
|
|
|
2009-01-17 20:23:27 +01:00
|
|
|
param->num_block_params++;
|
|
|
|
|
|
|
|
param->block_params = g_realloc(param->block_params,
|
|
|
|
param->num_block_params *
|
|
|
|
sizeof(param->block_params[0]));
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2009-01-17 20:23:27 +01:00
|
|
|
bp = ¶m->block_params[param->num_block_params - 1];
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2009-01-17 20:23:27 +01:00
|
|
|
bp->name = g_strdup(name);
|
|
|
|
bp->value = g_strdup(value);
|
|
|
|
bp->line = line;
|
2009-06-19 09:02:14 +02:00
|
|
|
bp->used = false;
|
2009-09-24 21:40:07 +02:00
|
|
|
|
|
|
|
return true;
|
2004-10-28 07:14:55 +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;
|
|
|
|
bool success;
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2009-07-19 15:11:37 +02:00
|
|
|
while (true) {
|
|
|
|
char *line;
|
|
|
|
const char *name, *value;
|
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)++;
|
2009-07-19 15:11:37 +02:00
|
|
|
line = g_strchug(line);
|
|
|
|
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
|
|
|
|
2009-07-19 15:11:37 +02:00
|
|
|
line = g_strchug(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);
|
|
|
|
return false;
|
|
|
|
}
|
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
|
|
|
|
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
|
|
|
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
|
|
|
value = tokenizer_next_string(&line, &error);
|
|
|
|
if (value == NULL) {
|
2009-09-24 21:40:07 +02:00
|
|
|
config_param_free(ret);
|
2009-07-19 15:11:37 +02:00
|
|
|
if (*line == 0)
|
2009-09-24 21:40:07 +02:00
|
|
|
g_set_error(error_r, config_quark(), 0,
|
|
|
|
"line %i: Value missing", *count);
|
2009-07-19 15:11:37 +02:00
|
|
|
else
|
2009-09-24 21:40:07 +02:00
|
|
|
g_propagate_prefixed_error(error_r, error,
|
|
|
|
"line %i: ",
|
|
|
|
*count);
|
|
|
|
return NULL;
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
|
|
|
|
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 value",
|
|
|
|
*count);
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2009-09-24 21:40:07 +02:00
|
|
|
success = config_add_block_param(ret, name, value, *count,
|
|
|
|
error_r);
|
|
|
|
if (!success) {
|
|
|
|
config_param_free(ret);
|
|
|
|
return false;
|
|
|
|
}
|
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
|
|
|
|
config_read_file(const char *file, GError **error_r)
|
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_entry *entry;
|
|
|
|
struct config_param *param;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2009-02-19 08:35:22 +01:00
|
|
|
g_debug("loading file %s", file);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!(fp = fopen(file, "r"))) {
|
2009-09-24 21:40:07 +02:00
|
|
|
g_set_error(error_r, config_quark(), errno,
|
|
|
|
"Failed to open %s: %s",
|
|
|
|
file, strerror(errno));
|
|
|
|
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++;
|
|
|
|
|
2009-07-19 15:11:37 +02:00
|
|
|
line = g_strchug(string);
|
|
|
|
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);
|
|
|
|
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
|
|
|
|
2009-07-19 15:11:37 +02:00
|
|
|
entry = config_entry_get(name);
|
2009-09-24 21:40:07 +02:00
|
|
|
if (entry == NULL) {
|
|
|
|
g_set_error(error_r, config_quark(), 0,
|
|
|
|
"unrecognized parameter in config file at "
|
|
|
|
"line %i: %s\n", count, name);
|
|
|
|
return false;
|
|
|
|
}
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2009-06-19 07:02:13 +02:00
|
|
|
if (entry->params != NULL && !entry->repeatable) {
|
2009-01-02 18:40:43 +01:00
|
|
|
param = entry->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);
|
|
|
|
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 */
|
|
|
|
|
2009-06-19 07:02:13 +02:00
|
|
|
if (entry->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);
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-19 15:11:37 +02:00
|
|
|
|
|
|
|
line = g_strchug(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);
|
|
|
|
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);
|
|
|
|
if (param == NULL)
|
|
|
|
return false;
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-19 15:11:37 +02:00
|
|
|
|
|
|
|
param = config_new_param(value, count);
|
|
|
|
}
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2009-01-02 18:40:43 +01:00
|
|
|
entry->params = g_slist_append(entry->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
|
|
|
|
2009-01-17 20:23:27 +01:00
|
|
|
struct config_param *
|
2009-01-25 16:03:49 +01:00
|
|
|
config_get_next_param(const char *name, const struct config_param * last)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-01-17 20:23:27 +01:00
|
|
|
struct config_entry *entry;
|
2009-01-02 18:40:43 +01:00
|
|
|
GSList *node;
|
2009-01-17 20:23:27 +01:00
|
|
|
struct config_param *param;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2009-01-02 18:40:43 +01:00
|
|
|
entry = config_entry_get(name);
|
|
|
|
if (entry == NULL)
|
2006-07-20 18:02:40 +02:00
|
|
|
return NULL;
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2009-01-02 18:40:43 +01:00
|
|
|
node = entry->params;
|
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
|
|
|
|
|
|
|
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 *
|
|
|
|
config_get_string(const char *name, const char *default_value)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-01-25 16:03:49 +01:00
|
|
|
const struct config_param *param = config_get_param(name);
|
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;
|
|
|
|
}
|
|
|
|
|
2009-01-18 17:54:46 +01:00
|
|
|
const char *
|
|
|
|
config_get_path(const char *name)
|
|
|
|
{
|
|
|
|
struct config_param *param = config_get_param(name);
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
if (param == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
path = parsePath(param->value);
|
|
|
|
if (path == NULL)
|
|
|
|
g_error("error parsing \"%s\" at line %i\n",
|
|
|
|
name, param->line);
|
|
|
|
|
|
|
|
g_free(param->value);
|
|
|
|
return param->value = path;
|
|
|
|
}
|
|
|
|
|
2010-04-13 21:19:07 +02:00
|
|
|
unsigned
|
|
|
|
config_get_unsigned(const char *name, unsigned default_value)
|
|
|
|
{
|
|
|
|
const struct config_param *param = config_get_param(name);
|
|
|
|
long value;
|
|
|
|
char *endptr;
|
|
|
|
|
|
|
|
if (param == NULL)
|
|
|
|
return default_value;
|
|
|
|
|
|
|
|
value = strtol(param->value, &endptr, 0);
|
|
|
|
if (*endptr != 0 || value < 0)
|
|
|
|
g_error("Not a valid non-negative number in line %i", param->line);
|
|
|
|
|
|
|
|
return (unsigned)value;
|
|
|
|
}
|
|
|
|
|
2009-01-21 08:46:59 +01:00
|
|
|
unsigned
|
|
|
|
config_get_positive(const char *name, unsigned default_value)
|
|
|
|
{
|
2009-01-25 16:03:49 +01:00
|
|
|
const struct config_param *param = config_get_param(name);
|
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)
|
|
|
|
g_error("Not a valid number in line %i", param->line);
|
|
|
|
|
|
|
|
if (value <= 0)
|
|
|
|
g_error("Not a positive number in line %i", param->line);
|
|
|
|
|
|
|
|
return (unsigned)value;
|
|
|
|
}
|
|
|
|
|
2009-01-17 20:23:27 +01:00
|
|
|
struct block_param *
|
2009-06-03 06:55:04 +02:00
|
|
|
config_get_block_param(const struct config_param * param, const char *name)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-01-25 16:04:03 +01:00
|
|
|
if (param == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2009-06-03 06:55:09 +02:00
|
|
|
for (unsigned i = 0; i < param->num_block_params; i++) {
|
2009-01-17 20:23:27 +01:00
|
|
|
if (0 == strcmp(name, param->block_params[i].name)) {
|
2009-06-19 07:02:14 +02:00
|
|
|
struct block_param *bp = ¶m->block_params[i];
|
2009-06-19 09:02:14 +02:00
|
|
|
bp->used = true;
|
2009-06-19 07:02:14 +02:00
|
|
|
return bp;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
|
|
|
|
2009-06-19 07:02:14 +02:00
|
|
|
return NULL;
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
|
|
|
|
2009-01-17 20:23:33 +01:00
|
|
|
bool config_get_bool(const char *name, bool default_value)
|
2007-09-06 01:59:33 +02:00
|
|
|
{
|
2009-01-25 16:03:49 +01:00
|
|
|
const struct config_param *param = config_get_param(name);
|
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)
|
2008-12-28 19:54:49 +01:00
|
|
|
g_error("%s is not a boolean value (yes, true, 1) or "
|
|
|
|
"(no, false, 0) on line %i\n",
|
|
|
|
name, 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
|
|
|
}
|
|
|
|
|
2009-01-18 19:37:27 +01:00
|
|
|
const char *
|
2009-01-25 16:03:49 +01:00
|
|
|
config_get_block_string(const struct config_param *param, const char *name,
|
2009-01-18 19:37:27 +01:00
|
|
|
const char *default_value)
|
|
|
|
{
|
2009-06-03 06:55:04 +02:00
|
|
|
struct block_param *bp = config_get_block_param(param, name);
|
2009-01-18 19:37:27 +01:00
|
|
|
|
|
|
|
if (bp == NULL)
|
|
|
|
return default_value;
|
|
|
|
|
|
|
|
return bp->value;
|
|
|
|
}
|
|
|
|
|
2009-01-18 19:45:51 +01:00
|
|
|
unsigned
|
2009-01-25 16:03:49 +01:00
|
|
|
config_get_block_unsigned(const struct config_param *param, const char *name,
|
2009-01-18 19:45:51 +01:00
|
|
|
unsigned default_value)
|
|
|
|
{
|
2009-06-03 06:55:04 +02:00
|
|
|
struct block_param *bp = config_get_block_param(param, name);
|
2009-01-18 19:45:51 +01:00
|
|
|
long value;
|
|
|
|
char *endptr;
|
|
|
|
|
|
|
|
if (bp == NULL)
|
|
|
|
return default_value;
|
|
|
|
|
|
|
|
value = strtol(bp->value, &endptr, 0);
|
|
|
|
if (*endptr != 0)
|
|
|
|
g_error("Not a valid number in line %i", bp->line);
|
|
|
|
|
|
|
|
if (value < 0)
|
|
|
|
g_error("Not a positive number in line %i", bp->line);
|
|
|
|
|
|
|
|
return (unsigned)value;
|
|
|
|
}
|
|
|
|
|
2009-01-17 20:23:56 +01:00
|
|
|
bool
|
2009-01-25 16:03:49 +01:00
|
|
|
config_get_block_bool(const struct config_param *param, const char *name,
|
2009-01-17 20:23:56 +01:00
|
|
|
bool default_value)
|
2007-09-06 01:59:33 +02:00
|
|
|
{
|
2009-06-03 06:55:04 +02:00
|
|
|
struct block_param *bp = config_get_block_param(param, name);
|
2009-06-19 09:02:14 +02:00
|
|
|
bool success, value;
|
2007-09-06 01:59:33 +02:00
|
|
|
|
2009-01-17 20:23:56 +01:00
|
|
|
if (bp == NULL)
|
|
|
|
return default_value;
|
2007-09-06 01:59:33 +02:00
|
|
|
|
2009-07-06 07:37:32 +02:00
|
|
|
success = get_bool(bp->value, &value);
|
2009-06-19 09:02:14 +02:00
|
|
|
if (!success)
|
2008-12-28 19:54:49 +01:00
|
|
|
g_error("%s is not a boolean value (yes, true, 1) or "
|
|
|
|
"(no, false, 0) on line %i\n",
|
2009-01-17 20:23:56 +01:00
|
|
|
name, bp->line);
|
2007-09-06 01:59:33 +02:00
|
|
|
|
2009-06-19 09:02:14 +02:00
|
|
|
return value;
|
2009-01-17 20:23:56 +01:00
|
|
|
}
|