-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.c
152 lines (124 loc) · 4.29 KB
/
configuration.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* configuration.c -- Dotconf configuration for SpeechD-Up
*
* Copyright (C) 2001, 2002, 2003, 2006 Brailcom, o.p.s.
*
* This 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, or (at your option)
* any later version.
*
* This software 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 package; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* $Id: configuration.c,v 1.2 2008-05-05 09:15:33 hanke Exp $
*/
#include <stdio.h>
#include <string.h>
#include <dotconf.h>
#include <assert.h>
#include "log.h"
#include "configuration.h"
#include "options.h"
/* == CONFIGURATION MANAGEMENT FUNCTIONS */
/* Add dotconf configuration option */
configoption_t *
add_config_option(configoption_t *options, int *num_config_options,
char *name, int type,
dotconf_callback_t callback, info_t *info,
unsigned long context)
{
configoption_t *opts;
(*num_config_options)++;
opts = (configoption_t*) realloc(options, (*num_config_options)
* sizeof(configoption_t));
opts[*num_config_options-1].name = strdup(name);
opts[*num_config_options-1].type = type;
opts[*num_config_options-1].callback = callback;
opts[*num_config_options-1].info = info;
opts[*num_config_options-1].context = context;
return opts;
}
/* Free all configuration options. */
void
free_config_options(configoption_t *opts, int *num)
{
int i = 0;
if (opts == NULL) return;
for(i=0; i<=(*num)-1; i++){
free((char*) opts[i].name);
}
free(opts);
*num = 0;
opts = NULL;
}
/* == CALLBACK DEFINITION MACROS == */
#define DOTCONF_CB_STR(name, arg) \
DOTCONF_CB(cb_ ## name) \
{ \
assert(cmd->data.str != NULL); \
if (options.arg ## _set != COMMAND_LINE){ \
LOG(3, "Setting " #name " to %s", cmd->data.str); \
options.arg = strdup(cmd->data.str); \
options.arg ## _set = CONFIG_FILE; \
LOG(3, "Setting " #name " set to %d", options.arg ## _set); \
} \
return NULL; \
}
#define DOTCONF_CB_INT(name, arg, cond, str) \
DOTCONF_CB(cb_ ## name) \
{ \
int val = cmd->data.value; \
if (!(cond)) FATAL(-1, str); \
if (options.arg ## _set != COMMAND_LINE){ \
LOG(3, "Setting " #name " to %d", val); \
options.arg = val; \
options.arg ## _set = CONFIG_FILE; \
} \
return NULL; \
}
/* == CALLBACK DEFINITIONS == */
DOTCONF_CB_INT(LogLevel, log_level, ((val>=0)&&(val<=5)),
"Log level must be between 1 (least verbose) and 5 (very verbose, "
"only for debugging).");
DOTCONF_CB_STR(LogFile, log_file_name);
DOTCONF_CB_STR(SpeakupDevice, speakup_device);
DOTCONF_CB_STR(SpeakupChartab, speakup_chartab);
DOTCONF_CB_STR(SpeakupCharacters, speakup_characters);
DOTCONF_CB_STR(SpeakupCoding, speakup_coding);
DOTCONF_CB_STR(Language, language);
DOTCONF_CB_INT(DontInitTables, dont_init_tables,
(val==0)||(val==1),
"DontInitTables must be either 0 or 1.");
DOTCONF_CB(cb_unknown)
{
LOG(2,"Unknown option in configuration!");
return NULL;
}
/* == LOAD CALLBACKS == */
#define ADD_CONFIG_OPTION(name, arg_type) \
options = add_config_option(options, num_options, #name, arg_type, cb_ ## name, 0, 0);
#define ADD_LAST_OPTION() \
options = add_config_option(options, num_options, "", 0, NULL, NULL, 0);
configoption_t*
load_config_options(int *num_options)
{
configoption_t *options = NULL;
ADD_CONFIG_OPTION(LogLevel, ARG_INT);
ADD_CONFIG_OPTION(LogFile, ARG_STR);
ADD_CONFIG_OPTION(SpeakupDevice, ARG_STR);
ADD_CONFIG_OPTION(SpeakupCoding, ARG_STR);
ADD_CONFIG_OPTION(SpeakupChartab, ARG_STR);
ADD_CONFIG_OPTION(SpeakupCharacters, ARG_STR);
ADD_CONFIG_OPTION(Language, ARG_STR);
ADD_CONFIG_OPTION(DontInitTables, ARG_TOGGLE);
ADD_LAST_OPTION();
return options;
}