2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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.
47 #include <stdlib.h> /* atoi free atol */
56 #include "gen.h" /* parse */
58 /* for systems without strdup */
60 extern char *strdup();
64 static int emp_config_dummy;
66 static void optstrset(struct keymatch *kp, s_char **av);
67 static void intset(struct keymatch *kp, s_char **av);
68 static void floatset(struct keymatch *kp, s_char **av);
69 static void doubleset(struct keymatch *kp, s_char **av);
70 static void longset(struct keymatch *kp, s_char **av);
71 static void optionset(struct keymatch *kp, s_char **av);
72 static void optiondel(struct keymatch *kp, s_char **av);
73 static void worldxset(struct keymatch *kp, s_char **av);
75 /* things that can be changed */
76 struct keymatch configkeys[] = {
77 #define EMP_CONFIG_C_OUTPUT
78 #include "econfig-spec.h"
79 #undef EMP_CONFIG_C_OUTPUT
82 static void fixup_files(void);
83 static struct keymatch *keylookup(s_char *key, struct keymatch tbl[]);
87 * read in empire configuration
90 emp_config(char *file)
93 s_char scanspace[1024];
98 if (file == NULL || (fp = fopen(file, "r")) == NULL) {
102 while (fgets(buf, sizeof buf, fp) != NULL) {
103 if (buf[0] == '#' || buf[0] == '\n')
105 if (parse(buf, av, 0, scanspace, 0) < 0) {
106 fprintf(stderr, "Can't parse line %s", buf);
109 if ((kp = keylookup(av[0], configkeys)) != NULL) {
110 (*kp->km_func) (kp, av + 1);
112 fprintf(stderr, "Unknown config key %s\n", av[0]);
126 /* list of other well known files... -maybe tailor these oneday
127 * anyway - meantime they are all relative to datadir */
128 static struct otherfiles ofiles[] = {
131 {&disablefil, "disable"},
135 {×tampfil, "timestamp"},
138 {&telfil, "tel/tel"},
140 {&telfil, "tel\\tel"},
145 /* fix up the empfile struct to reference full path names */
150 struct otherfiles *op;
153 for (ep = empfile; ep < &empfile[EF_MAX]; ep++) {
155 sprintf(buf, "%s/%s", datadir, ep->name);
157 sprintf(buf, "%s\\%s", datadir, ep->name);
159 ep->file = strdup(buf);
162 for (op = ofiles; op->files; op++) {
164 sprintf(buf, "%s/%s", datadir, op->name);
166 sprintf(buf, "%s\\%s", datadir, op->name);
168 *op->files = strdup(buf);
172 /* find the key in the table */
173 static struct keymatch *
174 keylookup(register s_char *command, struct keymatch *tbl)
176 register struct keymatch *kp;
178 if (command == 0 || *command == 0)
180 for (kp = tbl; kp->km_key != 0; kp++) {
181 if (strcmp(kp->km_key, command) == 0)
187 /* worldx int setting function */
189 worldxset(struct keymatch *kp, s_char **av)
191 int *intptr = (int *)kp->km_data;
193 if (*av == NULL || intptr == NULL)
196 if (!((*intptr % 2) == 0)) {
197 /* Must be div / 2, so subtract one */
198 *intptr = *intptr - 1;
202 /* generic int setting function */
204 intset(struct keymatch *kp, s_char **av)
206 int *intptr = (int *)kp->km_data;
208 if (*av == NULL || intptr == NULL)
213 /* generic float set function */
215 floatset(struct keymatch *kp, s_char **av)
217 float *floatptr = (float *)kp->km_data;
219 if (*av == NULL || floatptr == NULL)
221 *floatptr = atof(*av);
226 /* generic string set function */
228 optstrset(struct keymatch *kp, s_char **av)
230 s_char **confstrp = (s_char **)kp->km_data;
232 if (*av == NULL || confstrp == NULL)
234 if (kp->km_flags & KM_ALLOC)
236 *confstrp = strdup(*av);
237 kp->km_flags |= KM_ALLOC;
240 /* generic double set function */
242 doubleset(struct keymatch *kp, s_char **av)
244 double *doublep = (double *)kp->km_data;
246 if (*av == NULL || doublep == NULL)
248 *doublep = atof(*av);
251 /* generic long set function */
253 longset(struct keymatch *kp, s_char **av)
255 long int *longp = (long int *)kp->km_data;
257 if (*av == NULL || longp == NULL)
263 print_config(FILE * fp)
266 struct otherfiles *op;
269 fprintf(fp, "# Empire Configuration File:\n");
270 for (kp = configkeys; kp->km_key; kp++) {
271 /* We print a few special things here */
272 if (kp->km_comment) {
273 if (kp->km_comment[0]) {
274 if (kp->km_comment[0] != '\n')
276 fprintf(fp, "%s\n", kp->km_comment);
281 if (kp->km_func == optstrset) {
282 fprintf(fp, "%s \"%s\"\n", kp->km_key,
283 *(s_char **)kp->km_data);
284 } else if (kp->km_func == intset) {
285 fprintf(fp, "%s %d\n", kp->km_key, *(int *)kp->km_data);
286 } else if (kp->km_func == worldxset) {
287 fprintf(fp, "%s %d\n", kp->km_key, *(int *)kp->km_data);
288 } else if (kp->km_func == floatset) {
289 fprintf(fp, "%s %g\n", kp->km_key, *(float *)kp->km_data);
290 } else if (kp->km_func == doubleset) {
291 fprintf(fp, "%s %g\n", kp->km_key, *(double *)kp->km_data);
292 } else if (kp->km_func == longset) {
293 fprintf(fp, "%s %ld\n", kp->km_key, *(long *)kp->km_data);
294 } else if (kp->km_func == optionset) {
295 struct option_list *op;
297 for (op = Options; op->opt_key; op++) {
299 fprintf(fp, "%s %s\n", kp->km_key, op->opt_key);
301 } else if (kp->km_func == optiondel) {
302 struct option_list *op;
304 for (op = Options; op->opt_key; op++) {
305 if (*op->opt_valuep == 0)
306 fprintf(fp, "%s %s\n", kp->km_key, op->opt_key);
309 fprintf(fp, "# Unknown format %s\n", kp->km_key);
313 for (ep = empfile; ep < &empfile[EF_MAX]; ep++)
314 fprintf(fp, "# File %s -> %s\n", ep->name, ep->file);
315 for (op = ofiles; op->files; op++)
316 fprintf(fp, "# File %s -> %s\n", op->name, *(op->files));
321 /* add an option to the list */
323 set_option(const char *s)
325 struct option_list *op;
327 for (op = Options; op->opt_key; op++) {
328 if (strcmp(op->opt_key, s) == 0) {
333 fprintf(stderr, "Unknown option %s\n", s);
336 /* delete an option from the list */
338 delete_option(const char *s)
340 struct option_list *op;
342 for (op = Options; op->opt_key; op++) {
343 if (strcmp(op->opt_key, s) == 0) {
348 fprintf(stderr, "Unknown option %s\n", s);
351 /* config interface */
353 optionset(struct keymatch *kp, s_char **av)
354 /* unused - we have a well known global */
358 for (cpp = (char **)av; *cpp; cpp++)
362 /* config interface */
364 optiondel(struct keymatch *kp, s_char **av)
365 /* unused - we have a well known global */
369 for (cpp = (char **)av; *cpp; cpp++)