2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * emp_config.c: Allows config file to control server config. from a file
30 * Known contributors to this file:
32 * Steve McClure, 1998-2000
38 * 1. Change other constants - such as Num Countries 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.
42 * 2. Could look at loading in planes, units etc. Should be easy enough.
49 #include <stdlib.h> /* atoi free atol */
58 #include "gen.h" /* parse */
60 /* for systems without strdup */
62 extern char *strdup();
66 static int emp_config_dummy;
68 /* things that can be changed */
69 struct keymatch configkeys[] = {
70 #define EMP_CONFIG_C_OUTPUT
71 #include "econfig-spec.h"
72 #undef EMP_CONFIG_C_OUTPUT
75 static void fixup_files(void);
76 static struct keymatch *keylookup(s_char *key, struct keymatch tbl[]);
77 static int set_option(const char *, int);
80 * read in empire configuration
83 emp_config(char *file)
98 if ((fp = fopen(file, "r")) == NULL) {
99 fprintf(stderr, "Can't open %s for reading (%s)\n",
100 file, strerror(errno));
104 while (fgets(buf, sizeof buf, fp) != NULL) {
106 for (i = 0; buf[i] && isspace(buf[i]); ++i) ;
107 if (!buf[i] || buf[i] == '#')
109 if (parse(buf, av, 0, scanspace, 0) < 0) {
110 fprintf(stderr, "%s:%d: Can't parse line %s", file, lno, buf);
114 if ((kp = keylookup(av[0], configkeys)) == NULL) {
115 fprintf(stderr, "%s:%d: Unknown config key %s\n",
121 fprintf(stderr, "%s:%d: Config key %s needs a value\n",
127 switch (kp->km_type) {
129 *(int *)kp->km_data = atoi(av[1]);
132 *(float *)kp->km_data = atof(av[1]);
135 *(double *)kp->km_data = atof(av[1]);
138 *(long *)kp->km_data = atol(av[1]);
141 if (kp->km_flags & KM_ALLOC)
142 free(*(char **)kp->km_data);
143 *(char **)kp->km_data = strdup(av[1]);
144 kp->km_flags |= KM_ALLOC;
147 for (i = 1; av[i]; ++i) {
148 if (set_option(av[i], kp->km_key[0] != 'n') < 0) {
149 fprintf(stderr, "%s:%d: Unknown option %s\n",
159 fprintf(stderr, "%s:%d: Junk after value of config key %s\n",
166 WORLD_X &= ~1; /* make even */
176 /* list of other well known files... -maybe tailor these oneday
177 * anyway - meantime they are all relative to datadir */
178 static struct otherfiles ofiles[] = {
181 {&disablefil, "disable"},
185 {×tampfil, "timestamp"},
188 {&telfil, "tel/tel"},
190 {&telfil, "tel\\tel"},
195 /* fix up the empfile struct to reference full path names */
200 struct otherfiles *op;
203 for (ep = empfile; ep < &empfile[EF_MAX]; ep++) {
205 sprintf(buf, "%s/%s", datadir, ep->name);
207 sprintf(buf, "%s\\%s", datadir, ep->name);
209 ep->file = strdup(buf);
212 for (op = ofiles; op->files; op++) {
214 sprintf(buf, "%s/%s", datadir, op->name);
216 sprintf(buf, "%s\\%s", datadir, op->name);
218 *op->files = strdup(buf);
222 /* find the key in the table */
223 static struct keymatch *
224 keylookup(register s_char *command, struct keymatch *tbl)
226 register struct keymatch *kp;
228 if (command == 0 || *command == 0)
230 for (kp = tbl; kp->km_key != 0; kp++) {
231 if (strcmp(kp->km_key, command) == 0)
238 print_config(FILE *fp)
241 struct option_list *op;
242 struct otherfiles *ofp;
245 fprintf(fp, "# Empire Configuration File:\n");
246 for (kp = configkeys; kp->km_key; kp++) {
248 fprintf(fp, "\n# %s\n", kp->km_comment);
251 switch (kp->km_type) {
253 fprintf(fp, "%s \"%s\"\n", kp->km_key, *(char **)kp->km_data);
256 fprintf(fp, "%s %d\n", kp->km_key, *(int *)kp->km_data);
259 fprintf(fp, "%s %g\n", kp->km_key, *(float *)kp->km_data);
262 fprintf(fp, "%s %g\n", kp->km_key, *(double *)kp->km_data);
265 fprintf(fp, "%s %ld\n", kp->km_key, *(long *)kp->km_data);
268 for (op = Options; op->opt_key; op++)
269 if (*op->opt_valuep != (kp->km_key[0] == 'n'))
270 fprintf(fp, "%s %s\n", kp->km_key, op->opt_key);
278 for (ep = empfile; ep < &empfile[EF_MAX]; ep++)
279 fprintf(fp, "# File %s -> %s\n", ep->name, ep->file);
280 for (ofp = ofiles; ofp->files; ofp++)
281 fprintf(fp, "# File %s -> %s\n", ofp->name, *(ofp->files));
286 /* Set option S to value VAL; return 0 on success, -1 on failure. */
288 set_option(const char *s, int val)
290 struct option_list *op;
292 for (op = Options; op->opt_key; op++) {
293 if (strcmp(op->opt_key, s) == 0) {
294 *op->opt_valuep = val;