2 global configuration option handler for lasker chess server
4 Copyright (C) Andrew Tridgell 2002
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 /* database for holding config info */
24 static TDB_CONTEXT *config_db;
26 static int config_set(const char *name, char *value);
29 tdb logging function, just in case something goes wrong we log database errors to stderr
31 static void tdb_log_fn(TDB_CONTEXT *t, int level, const char *format, ...)
36 vfprintf(stderr, format, ap);
42 initialise the config database with some default values. This is only called once
43 on initial installation
46 static int config_init(void)
48 config_db = tdb_open_ex(CONFIG_DB, 0, 0, O_RDWR|O_CREAT, 0600, tdb_log_fn);
50 d_printf("Failed to create the config database!\n");
54 /* a few important defaults */
55 config_set("DEFAULT_PROMPT", "fics% ");
56 config_set("GUEST_LOGIN", "guest");
58 /* this allows an initial admin connection */
59 return config_set("HEAD_ADMIN", "admin");
62 /* close the config database */
63 void config_close(void)
65 if (!config_db) return;
71 open the config database
76 config_db = tdb_open_ex(CONFIG_DB, 0, 0, O_RDWR, 0600, tdb_log_fn);
77 if (!config_db && errno == ENOENT) {
87 fetch a value from the config database - caller must free
89 const char *config_get(const char *name)
95 key.dsize = strlen(name)+1;
97 data = tdb_fetch(config_db, key);
99 /* this trick allows config variables to show up in 'aconfig' as
100 soon as they are used */
101 if (!data.dptr && config_set(name, "NOT_CONFIGURED") == 0) {
102 data = tdb_fetch(config_db, key);
110 fetch an integer value from the config database, with a default value
112 int config_get_int(const char *name, int default_v)
119 key.dsize = strlen(name)+1;
121 data = tdb_fetch(config_db, key);
123 /* this trick allows config variables to show up in 'aconfig' as
124 soon as they are used */
127 data.dsize = asprintf(&s, "%d", default_v) + 1;
129 tdb_store(config_db, key, data, TDB_REPLACE);
140 set a configration variable.
143 static int config_set(const char *name, char *value)
150 key.dsize = strlen(name)+1;
152 if (strcmp(value, "-") == 0) {
153 return tdb_delete(config_db, key);
157 data.dsize = strlen(value)+1;
159 ret = tdb_store(config_db, key, data, TDB_REPLACE);
164 fetch a value from the config database - will be auto-freed on a future call
167 const char *config_get_tmp(const char *name)
169 static const char *ret[10];
171 const char **p = &ret[idx];
176 *p = config_get(name);
182 dump the current config
184 static void config_dump(int p)
186 TDB_DATA data1, data2;
188 /* looping using firstkey/nextkey is a pain ... */
189 for (data1 = tdb_firstkey(config_db);
192 data1 = tdb_nextkey(config_db, data2),
194 pprintf(p, "%s = %s\n", data1.dptr, config_get_tmp(data1.dptr));
201 * Usage: aconfig variable value
203 * sets the config variable 'variable' to 'value'
205 int com_aconfig(int p, param_list param)
207 if (param[0].type == TYPE_NULL) {
208 /* if no parameters are given then dump the current config */
213 if (config_set(param[0].val.word, param[1].val.string) != 0) {