2004-02-24 00:41:20 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2004-02-24 00:41:20 +01:00
|
|
|
* This project's homepage is: 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "conf.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "buffer2array.h"
|
2007-12-28 03:56:25 +01:00
|
|
|
#include "path.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
|
|
|
|
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 '#'
|
|
|
|
#define CONF_BLOCK_BEGIN "{"
|
|
|
|
#define CONF_BLOCK_END "}"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-10-28 07:14:55 +02:00
|
|
|
#define CONF_REPEATABLE_MASK 0x01
|
|
|
|
#define CONF_BLOCK_MASK 0x02
|
2006-10-06 12:33:27 +02:00
|
|
|
#define CONF_LINE_TOKEN_MAX 3
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2009-01-17 20:23:27 +01:00
|
|
|
struct config_entry {
|
2009-01-02 18:40:43 +01:00
|
|
|
const char *name;
|
2004-10-28 07:14:55 +02:00
|
|
|
unsigned char mask;
|
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-01-02 18:40:43 +01:00
|
|
|
static GSList *config_entries;
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2007-09-06 01:59:33 +02:00
|
|
|
static int get_bool(const char *value)
|
|
|
|
{
|
|
|
|
const char **x;
|
|
|
|
static const char *t[] = { "yes", "true", "1", NULL };
|
|
|
|
static const char *f[] = { "no", "false", "0", NULL };
|
|
|
|
|
|
|
|
for (x = t; *x; x++) {
|
|
|
|
if (!strcasecmp(*x, value))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
for (x = f; *x; x++) {
|
|
|
|
if (!strcasecmp(*x, value))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return CONF_BOOL_INVALID;
|
|
|
|
}
|
|
|
|
|
2009-01-17 20:23:27 +01:00
|
|
|
struct config_param *
|
|
|
|
newConfigParam(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;
|
2004-10-28 07:14:55 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-01-10 21:33:38 +01:00
|
|
|
void
|
2009-01-02 18:40:43 +01:00
|
|
|
config_param_free(gpointer data, G_GNUC_UNUSED gpointer user_data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-01-17 20:23:27 +01:00
|
|
|
struct config_param *param = data;
|
2004-02-24 00:41:20 +01:00
|
|
|
int i;
|
|
|
|
|
2009-01-03 14:53:34 +01:00
|
|
|
g_free(param->value);
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2009-01-17 20:23:27 +01:00
|
|
|
for (i = 0; i < param->num_block_params; i++) {
|
|
|
|
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-01-17 20:23:27 +01:00
|
|
|
static struct config_entry *
|
2009-01-02 18:40:43 +01:00
|
|
|
newConfigEntry(const char *name, int repeatable, int block)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-01-17 20:23:27 +01:00
|
|
|
struct config_entry *ret = g_new(struct config_entry, 1);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2009-01-02 18:40:43 +01:00
|
|
|
ret->name = name;
|
2004-10-28 07:14:55 +02:00
|
|
|
ret->mask = 0;
|
2009-01-02 18:40:43 +01:00
|
|
|
ret->params = NULL;
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (repeatable)
|
|
|
|
ret->mask |= CONF_REPEATABLE_MASK;
|
|
|
|
if (block)
|
|
|
|
ret->mask |= CONF_BLOCK_MASK;
|
2004-10-28 07:14:55 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-01-02 18:40:43 +01:00
|
|
|
static void
|
|
|
|
config_entry_free(gpointer data, G_GNUC_UNUSED gpointer user_data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-01-17 20:23:27 +01:00
|
|
|
struct config_entry *entry = data;
|
2009-01-02 18:40:43 +01:00
|
|
|
|
|
|
|
g_slist_foreach(entry->params, config_param_free, NULL);
|
|
|
|
g_slist_free(entry->params);
|
|
|
|
|
2009-01-03 14:53:34 +01:00
|
|
|
g_free(entry);
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
GSList *list;
|
|
|
|
|
|
|
|
for (list = config_entries; list != NULL;
|
|
|
|
list = g_slist_next(list)) {
|
2009-01-17 20:23:27 +01:00
|
|
|
struct config_entry *entry = list->data;
|
2009-01-02 18:40:43 +01:00
|
|
|
if (strcmp(entry->name, name) == 0)
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
static void registerConfigParam(const char *name, int repeatable, int block)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-01-17 20:23:27 +01:00
|
|
|
struct config_entry *entry;
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2009-01-02 18:40:43 +01:00
|
|
|
entry = config_entry_get(name);
|
|
|
|
if (entry != NULL)
|
2008-12-28 19:54:49 +01:00
|
|
|
g_error("config parameter \"%s\" already registered\n", name);
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2009-01-02 18:40:43 +01:00
|
|
|
entry = newConfigEntry(name, repeatable, block);
|
|
|
|
config_entries = g_slist_prepend(config_entries, entry);
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
|
|
|
|
2009-01-17 20:23:27 +01:00
|
|
|
void config_global_finish(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-01-02 18:40:43 +01:00
|
|
|
g_slist_foreach(config_entries, config_entry_free, NULL);
|
|
|
|
g_slist_free(config_entries);
|
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
|
|
|
{
|
2009-01-02 18:40:43 +01:00
|
|
|
config_entries = NULL;
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2006-07-27 21:10:08 +02:00
|
|
|
/* registerConfigParam(name, repeatable, block); */
|
|
|
|
registerConfigParam(CONF_MUSIC_DIR, 0, 0);
|
|
|
|
registerConfigParam(CONF_PLAYLIST_DIR, 0, 0);
|
2008-11-28 10:57:39 +01:00
|
|
|
registerConfigParam(CONF_FOLLOW_INSIDE_SYMLINKS, 0, 0);
|
|
|
|
registerConfigParam(CONF_FOLLOW_OUTSIDE_SYMLINKS, 0, 0);
|
2006-08-28 22:47:50 +02:00
|
|
|
registerConfigParam(CONF_DB_FILE, 0, 0);
|
2009-01-19 18:51:57 +01:00
|
|
|
registerConfigParam(CONF_STICKER_FILE, false, false);
|
2006-07-27 21:10:08 +02:00
|
|
|
registerConfigParam(CONF_LOG_FILE, 0, 0);
|
|
|
|
registerConfigParam(CONF_ERROR_FILE, 0, 0);
|
2006-08-28 22:47:50 +02:00
|
|
|
registerConfigParam(CONF_PID_FILE, 0, 0);
|
2006-07-27 21:10:08 +02:00
|
|
|
registerConfigParam(CONF_STATE_FILE, 0, 0);
|
|
|
|
registerConfigParam(CONF_USER, 0, 0);
|
2006-08-28 22:47:50 +02:00
|
|
|
registerConfigParam(CONF_BIND_TO_ADDRESS, 1, 0);
|
|
|
|
registerConfigParam(CONF_PORT, 0, 0);
|
2006-07-27 21:10:08 +02:00
|
|
|
registerConfigParam(CONF_LOG_LEVEL, 0, 0);
|
2007-01-11 21:41:17 +01:00
|
|
|
registerConfigParam(CONF_ZEROCONF_NAME, 0, 0);
|
2007-06-03 20:08:51 +02:00
|
|
|
registerConfigParam(CONF_ZEROCONF_ENABLED, 0, 0);
|
2006-07-27 21:10:08 +02:00
|
|
|
registerConfigParam(CONF_PASSWORD, 1, 0);
|
|
|
|
registerConfigParam(CONF_DEFAULT_PERMS, 0, 0);
|
2006-08-28 22:47:50 +02:00
|
|
|
registerConfigParam(CONF_AUDIO_OUTPUT, 1, 1);
|
2006-07-27 21:10:08 +02:00
|
|
|
registerConfigParam(CONF_AUDIO_OUTPUT_FORMAT, 0, 0);
|
2006-08-28 22:47:50 +02:00
|
|
|
registerConfigParam(CONF_MIXER_TYPE, 0, 0);
|
|
|
|
registerConfigParam(CONF_MIXER_DEVICE, 0, 0);
|
|
|
|
registerConfigParam(CONF_MIXER_CONTROL, 0, 0);
|
|
|
|
registerConfigParam(CONF_REPLAYGAIN, 0, 0);
|
|
|
|
registerConfigParam(CONF_REPLAYGAIN_PREAMP, 0, 0);
|
|
|
|
registerConfigParam(CONF_VOLUME_NORMALIZATION, 0, 0);
|
2007-02-02 04:51:07 +01:00
|
|
|
registerConfigParam(CONF_SAMPLERATE_CONVERTER, 0, 0);
|
2006-08-28 22:47:50 +02:00
|
|
|
registerConfigParam(CONF_AUDIO_BUFFER_SIZE, 0, 0);
|
|
|
|
registerConfigParam(CONF_BUFFER_BEFORE_PLAY, 0, 0);
|
2006-07-27 21:10:08 +02:00
|
|
|
registerConfigParam(CONF_HTTP_PROXY_HOST, 0, 0);
|
|
|
|
registerConfigParam(CONF_HTTP_PROXY_PORT, 0, 0);
|
|
|
|
registerConfigParam(CONF_HTTP_PROXY_USER, 0, 0);
|
|
|
|
registerConfigParam(CONF_HTTP_PROXY_PASSWORD, 0, 0);
|
2006-08-28 22:47:50 +02:00
|
|
|
registerConfigParam(CONF_CONN_TIMEOUT, 0, 0);
|
|
|
|
registerConfigParam(CONF_MAX_CONN, 0, 0);
|
|
|
|
registerConfigParam(CONF_MAX_PLAYLIST_LENGTH, 0, 0);
|
|
|
|
registerConfigParam(CONF_MAX_COMMAND_LIST_SIZE, 0, 0);
|
|
|
|
registerConfigParam(CONF_MAX_OUTPUT_BUFFER_SIZE, 0, 0);
|
|
|
|
registerConfigParam(CONF_FS_CHARSET, 0, 0);
|
2006-07-27 21:10:08 +02:00
|
|
|
registerConfigParam(CONF_ID3V1_ENCODING, 0, 0);
|
2006-08-28 22:47:50 +02:00
|
|
|
registerConfigParam(CONF_METADATA_TO_USE, 0, 0);
|
|
|
|
registerConfigParam(CONF_SAVE_ABSOLUTE_PATHS, 0, 0);
|
2006-12-23 19:00:15 +01:00
|
|
|
registerConfigParam(CONF_GAPLESS_MP3_PLAYBACK, 0, 0);
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
|
|
|
|
2009-01-17 20:23:27 +01:00
|
|
|
void
|
|
|
|
addBlockParam(struct config_param * param, const char *name, const char *value,
|
|
|
|
int line)
|
2004-10-28 07:14:55 +02:00
|
|
|
{
|
2009-01-17 20:23:27 +01:00
|
|
|
struct block_param *bp;
|
|
|
|
|
|
|
|
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;
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
|
|
|
|
2009-01-17 20:23:27 +01:00
|
|
|
static struct config_param *
|
|
|
|
config_read_fileigBlock(FILE * fp, int *count, char *string)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-01-17 20:23:27 +01:00
|
|
|
struct config_param *ret = newConfigParam(NULL, *count);
|
2004-10-28 07:14:55 +02:00
|
|
|
|
|
|
|
int i;
|
|
|
|
int numberOfArgs;
|
|
|
|
int argsMinusComment;
|
|
|
|
|
2008-12-28 19:54:49 +01:00
|
|
|
while (fgets(string, MAX_STRING_SIZE, fp)) {
|
2006-10-06 12:33:27 +02:00
|
|
|
char *array[CONF_LINE_TOKEN_MAX] = { NULL };
|
|
|
|
|
2004-10-28 07:14:55 +02:00
|
|
|
(*count)++;
|
|
|
|
|
2006-10-06 12:33:27 +02:00
|
|
|
numberOfArgs = buffer2array(string, array, CONF_LINE_TOKEN_MAX);
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < numberOfArgs; i++) {
|
|
|
|
if (array[i][0] == CONF_COMMENT)
|
|
|
|
break;
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
argsMinusComment = i;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (0 == argsMinusComment) {
|
2004-11-11 04:20:49 +01:00
|
|
|
continue;
|
|
|
|
}
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (1 == argsMinusComment &&
|
|
|
|
0 == strcmp(array[0], CONF_BLOCK_END)) {
|
2004-10-28 07:14:55 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (2 != argsMinusComment) {
|
2008-12-28 19:54:49 +01:00
|
|
|
g_error("improperly formatted config file at line %i:"
|
|
|
|
" %s\n", *count, string);
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (0 == strcmp(array[0], CONF_BLOCK_BEGIN) ||
|
|
|
|
0 == strcmp(array[1], CONF_BLOCK_BEGIN) ||
|
|
|
|
0 == strcmp(array[0], CONF_BLOCK_END) ||
|
|
|
|
0 == strcmp(array[1], CONF_BLOCK_END)) {
|
2008-12-28 19:54:49 +01:00
|
|
|
g_error("improperly formatted config file at line %i: %s "
|
|
|
|
"in block beginning at line %i\n",
|
|
|
|
*count, string, ret->line);
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
addBlockParam(ret, array[0], array[1], *count);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-01-17 20:23:27 +01:00
|
|
|
void config_read_file(const char *file)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char string[MAX_STRING_SIZE + 1];
|
2004-02-24 00:41:20 +01:00
|
|
|
int i;
|
|
|
|
int numberOfArgs;
|
2004-10-28 07:14:55 +02:00
|
|
|
int argsMinusComment;
|
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
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!(fp = fopen(file, "r"))) {
|
2008-12-28 19:54:49 +01:00
|
|
|
g_error("problems opening file %s for reading: %s\n",
|
|
|
|
file, strerror(errno));
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-12-28 19:54:49 +01:00
|
|
|
while (fgets(string, MAX_STRING_SIZE, fp)) {
|
2006-10-06 12:33:27 +02:00
|
|
|
char *array[CONF_LINE_TOKEN_MAX] = { NULL };
|
2008-12-28 19:54:49 +01:00
|
|
|
|
2004-08-10 18:03:03 +02:00
|
|
|
count++;
|
|
|
|
|
2006-10-06 12:33:27 +02:00
|
|
|
numberOfArgs = buffer2array(string, array, CONF_LINE_TOKEN_MAX);
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < numberOfArgs; i++) {
|
|
|
|
if (array[i][0] == CONF_COMMENT)
|
|
|
|
break;
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
argsMinusComment = i;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (0 == argsMinusComment) {
|
2004-11-11 04:20:49 +01:00
|
|
|
continue;
|
|
|
|
}
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (2 != argsMinusComment) {
|
2008-12-28 19:54:49 +01:00
|
|
|
g_error("improperly formatted config file at line %i:"
|
|
|
|
" %s\n", count, string);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2009-01-02 18:40:43 +01:00
|
|
|
entry = config_entry_get(array[0]);
|
|
|
|
if (entry == NULL)
|
2008-12-28 19:54:49 +01:00
|
|
|
g_error("unrecognized parameter in config file at "
|
|
|
|
"line %i: %s\n", count, string);
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!(entry->mask & CONF_REPEATABLE_MASK) &&
|
2009-01-02 18:40:43 +01:00
|
|
|
entry->params != NULL) {
|
|
|
|
param = entry->params->data;
|
2008-12-28 19:54:49 +01:00
|
|
|
g_error("config parameter \"%s\" is first defined on "
|
|
|
|
"line %i and redefined on line %i\n",
|
|
|
|
array[0], param->line, count);
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (entry->mask & CONF_BLOCK_MASK) {
|
|
|
|
if (0 != strcmp(array[1], CONF_BLOCK_BEGIN)) {
|
2008-12-28 19:54:49 +01:00
|
|
|
g_error("improperly formatted config file at "
|
|
|
|
"line %i: %s\n", count, string);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2009-01-17 20:23:27 +01:00
|
|
|
param = config_read_fileigBlock(fp, &count, string);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else
|
|
|
|
param = newConfigParam(array[1], 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);
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2009-01-25 17:37:45 +01:00
|
|
|
void
|
|
|
|
config_add_param(const char *name, struct config_param *param)
|
|
|
|
{
|
|
|
|
struct config_entry *entry = config_entry_get(name);
|
|
|
|
assert(entry != NULL);
|
|
|
|
|
|
|
|
entry->params = g_slist_append(entry->params, param);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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-01-25 16:03:49 +01:00
|
|
|
getBlockParam(const struct config_param * param, const char *name)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-01-17 20:23:27 +01:00
|
|
|
struct block_param *ret = NULL;
|
2004-10-28 07:14:55 +02:00
|
|
|
int i;
|
|
|
|
|
2009-01-25 16:04:03 +01:00
|
|
|
if (param == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2009-01-17 20:23:27 +01:00
|
|
|
for (i = 0; i < param->num_block_params; i++) {
|
|
|
|
if (0 == strcmp(name, param->block_params[i].name)) {
|
2006-07-20 18:02:40 +02:00
|
|
|
if (ret) {
|
2008-12-28 19:54:49 +01:00
|
|
|
g_warning("\"%s\" first defined on line %i, and "
|
|
|
|
"redefined on line %i\n", name,
|
2009-01-17 20:23:27 +01:00
|
|
|
ret->line, param->block_params[i].line);
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
2009-01-17 20:23:27 +01:00
|
|
|
ret = param->block_params + i;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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-01-17 20:23:33 +01:00
|
|
|
int 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-01-17 20:23:33 +01:00
|
|
|
value = get_bool(param->value);
|
|
|
|
if (value == CONF_BOOL_INVALID)
|
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
|
|
|
|
|
|
|
if (value == CONF_BOOL_UNSET)
|
|
|
|
return default_value;
|
|
|
|
|
2009-01-17 20:23:33 +01: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)
|
|
|
|
{
|
|
|
|
struct block_param *bp = getBlockParam(param, name);
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
struct block_param *bp = getBlockParam(param, name);
|
|
|
|
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-01-17 20:23:27 +01:00
|
|
|
struct block_param *bp = getBlockParam(param, name);
|
2009-01-17 20:23:56 +01:00
|
|
|
int 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-01-17 20:23:56 +01:00
|
|
|
value = get_bool(bp->value);
|
|
|
|
if (value == CONF_BOOL_INVALID)
|
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-01-17 20:23:56 +01:00
|
|
|
if (value == CONF_BOOL_UNSET)
|
|
|
|
return default_value;
|
|
|
|
|
|
|
|
return !!value;
|
|
|
|
}
|