2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * emp_config.c: Allows config file to control server config. from a file
29 * Known contributors to this file:
31 * Steve McClure, 1998-2000
32 * Markus Armbruster, 2004-2009
38 * Change other constants - such as MAXNOC etc.
39 * Just requires variables to be assigned, then dynamic allocation in
40 * a few places. Some checks needed in the server to check the world
41 * hasn't changed size etc.
56 #include "prototypes.h"
58 static struct keymatch *keylookup(char *key, struct keymatch tbl[]);
59 static int set_paths(char *);
62 * read in empire configuration
65 emp_config(char *file)
79 if ((fp = fopen(file, "r")) == NULL) {
80 if (file == dflt_econfig && errno == ENOENT)
82 fprintf(stderr, "Can't open %s for reading (%s)\n",
83 file, strerror(errno));
87 while (fgets(buf, sizeof(buf), fp) != NULL) {
89 for (i = 0; buf[i] && isspace(buf[i]); ++i) ;
90 if (!buf[i] || buf[i] == '#')
92 if (parse(buf, scanspace, av, NULL, NULL, NULL) < 0) {
93 fprintf(stderr, "%s:%d: Can't parse line %s", file, lno, buf);
97 if ((kp = keylookup(av[0], configkeys)) == NULL) {
98 fprintf(stderr, "%s:%d: Unknown config key %s\n",
104 fprintf(stderr, "%s:%d: Config key %s needs a value\n",
110 switch (kp->km_type) {
112 *(int *)kp->km_data = atoi(av[1]);
115 *(float *)kp->km_data = atof(av[1]);
118 *(double *)kp->km_data = atof(av[1]);
121 *(long *)kp->km_data = atol(av[1]);
124 if (kp->km_flags & KM_ALLOC)
125 free(*(char **)kp->km_data);
126 *(char **)kp->km_data = strdup(av[1]);
127 kp->km_flags |= KM_ALLOC;
133 fprintf(stderr, "%s:%d: Junk after value of config key %s\n",
142 WORLD_X &= ~1; /* force even */
143 if (set_paths(file) < 0)
149 /* find the key in the table */
150 static struct keymatch *
151 keylookup(char *command, struct keymatch *tbl)
155 if (!command || !*command)
157 for (kp = tbl; kp->km_key; kp++) {
158 if (strcmp(kp->km_key, command) == 0)
165 set_paths(char *econfig)
170 p = _fullpath(NULL, econfig, 0);
171 slash = strrchr(p, '\\');
176 cwd = getcwd(buf, sizeof(buf));
177 p = fnameat(econfig, cwd);
179 fprintf(stderr, "Can't get current working directory (%s)\n",
185 slash = strrchr(p, '/');
189 configdir = realloc(p, slash + 1 - p);
191 infodir = fnameat(infodir_conf, configdir);
192 gamedir = fnameat(gamedir_conf, configdir);
193 builtindir = fnameat(builtindir_conf, configdir);
194 schedulefil = fnameat("schedule", configdir);
200 print_config(FILE *fp)
204 fprintf(fp, "# Empire Configuration File:\n");
205 for (kp = configkeys; kp->km_key; kp++) {
206 if (kp->km_comment) {
207 if (kp->km_comment[0] != '\n' && kp->km_comment[0] != '#')
209 fprintf(fp, "%s\n", kp->km_comment);
213 switch (kp->km_type) {
215 fprintf(fp, "%s \"%s\"\n", kp->km_key, *(char **)kp->km_data);
218 fprintf(fp, "%s %d\n", kp->km_key, *(int *)kp->km_data);
221 fprintf(fp, "%s %g\n", kp->km_key, *(float *)kp->km_data);
224 fprintf(fp, "%s %g\n", kp->km_key, *(double *)kp->km_data);
227 fprintf(fp, "%s %ld\n", kp->km_key, *(long *)kp->km_data);