]> git.pond.sub.org Git - empserver/blob - src/lib/gen/emp_config.c
Combine the definitions of include/optlist.h and the use thereof in
[empserver] / src / lib / gen / emp_config.c
1 /*
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
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  emp_config.c: Allows config file to control server config. from a file
29  * 
30  *  Known contributors to this file:
31  *     Julian Onions, 1995
32  *     Steve McClure, 1998-2000
33  */
34
35 /*
36  * STILL TO DO
37  *
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.
43  *
44  */
45
46 #include <stdio.h>
47 #include <stdlib.h>             /* atoi free atol */
48 #include <string.h>
49
50 #include "misc.h"
51 #include "com.h"
52 #include "match.h"
53 #include "file.h"
54 #include "optlist.h"
55 #include "tel.h"
56 #include "gen.h"                /* parse */
57
58 /* for systems without strdup  */
59 #ifdef NOSTRDUP
60 extern char *strdup();
61 #endif /* NOSTRDUP */
62
63 /* Dummy one */
64 static int emp_config_dummy;
65
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);
74
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
80 };
81
82 static void fixup_files(void);
83 static struct keymatch *keylookup(s_char *key, struct keymatch tbl[]);
84
85
86 /*
87  * read in empire configuration
88  */
89 int
90 emp_config(char *file)
91 {
92     FILE *fp;
93     s_char scanspace[1024];
94     s_char *av[65];
95     char buf[BUFSIZ];
96     struct keymatch *kp;
97
98     if (file == NULL || (fp = fopen(file, "r")) == NULL) {
99         fixup_files();
100         return RET_OK;
101     }
102     while (fgets(buf, sizeof buf, fp) != NULL) {
103         if (buf[0] == '#' || buf[0] == '\n')
104             continue;
105         if (parse(buf, av, 0, scanspace, 0) < 0) {
106             fprintf(stderr, "Can't parse line %s", buf);
107             continue;
108         }
109         if ((kp = keylookup(av[0], configkeys)) != NULL) {
110             (*kp->km_func) (kp, av + 1);
111         } else {
112             fprintf(stderr, "Unknown config key %s\n", av[0]);
113         }
114     }
115     fclose(fp);
116     fixup_files();
117
118     return RET_OK;
119 }
120
121 struct otherfiles {
122     s_char **files;
123     char *name;
124 };
125
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[] = {
129     {&upfil, "up"},
130     {&downfil, "down"},
131     {&disablefil, "disable"},
132     {&banfil, "ban"},
133     {&authfil, "auth"},
134     {&annfil, "ann"},
135     {&timestampfil, "timestamp"},
136     {&teldir, "tel"},
137 #if !defined(_WIN32)
138     {&telfil, "tel/tel"},
139 #else
140     {&telfil, "tel\\tel"},
141 #endif
142     {NULL, NULL}
143 };
144
145 /* fix up the empfile struct to reference full path names */
146 static void
147 fixup_files(void)
148 {
149     struct empfile *ep;
150     struct otherfiles *op;
151     s_char buf[1024];
152
153     for (ep = empfile; ep < &empfile[EF_MAX]; ep++) {
154 #if !defined(_WIN32)
155         sprintf(buf, "%s/%s", datadir, ep->name);
156 #else
157         sprintf(buf, "%s\\%s", datadir, ep->name);
158 #endif
159         ep->file = strdup(buf);
160     }
161
162     for (op = ofiles; op->files; op++) {
163 #if !defined(_WIN32)
164         sprintf(buf, "%s/%s", datadir, op->name);
165 #else
166         sprintf(buf, "%s\\%s", datadir, op->name);
167 #endif
168         *op->files = strdup(buf);
169     }
170 }
171
172 /* find the key in the table */
173 static struct keymatch *
174 keylookup(register s_char *command, struct keymatch *tbl)
175 {
176     register struct keymatch *kp;
177
178     if (command == 0 || *command == 0)
179         return 0;
180     for (kp = tbl; kp->km_key != 0; kp++) {
181         if (strcmp(kp->km_key, command) == 0)
182             return kp;
183     }
184     return NULL;
185 }
186
187 /* worldx int setting function */
188 static void
189 worldxset(struct keymatch *kp, s_char **av)
190 {
191     int *intptr = (int *)kp->km_data;
192
193     if (*av == NULL || intptr == NULL)
194         return;
195     *intptr = atoi(*av);
196     if (!((*intptr % 2) == 0)) {
197         /* Must be div / 2, so subtract one */
198         *intptr = *intptr - 1;
199     }
200 }
201
202 /* generic int setting function */
203 static void
204 intset(struct keymatch *kp, s_char **av)
205 {
206     int *intptr = (int *)kp->km_data;
207
208     if (*av == NULL || intptr == NULL)
209         return;
210     *intptr = atoi(*av);
211 }
212
213 /* generic float set function */
214 static void
215 floatset(struct keymatch *kp, s_char **av)
216 {
217     float *floatptr = (float *)kp->km_data;
218
219     if (*av == NULL || floatptr == NULL)
220         return;
221     *floatptr = atof(*av);
222 }
223
224
225
226 /* generic string set function */
227 static void
228 optstrset(struct keymatch *kp, s_char **av)
229 {
230     s_char **confstrp = (s_char **)kp->km_data;
231
232     if (*av == NULL || confstrp == NULL)
233         return;
234     if (kp->km_flags & KM_ALLOC)
235         free(*confstrp);
236     *confstrp = strdup(*av);
237     kp->km_flags |= KM_ALLOC;
238 }
239
240 /* generic double set function */
241 static void
242 doubleset(struct keymatch *kp, s_char **av)
243 {
244     double *doublep = (double *)kp->km_data;
245
246     if (*av == NULL || doublep == NULL)
247         return;
248     *doublep = atof(*av);
249 }
250
251 /* generic long set function */
252 static void
253 longset(struct keymatch *kp, s_char **av)
254 {
255     long int *longp = (long int *)kp->km_data;
256
257     if (*av == NULL || longp == NULL)
258         return;
259     *longp = atol(*av);
260 }
261
262 void
263 print_config(FILE * fp)
264 {
265     struct empfile *ep;
266     struct otherfiles *op;
267     struct keymatch *kp;
268
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')
275                     fprintf(fp, "\n# ");
276                 fprintf(fp, "%s\n", kp->km_comment);
277             }
278         }
279         if (!kp->km_key[0])
280             continue;
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;
296
297             for (op = Options; op->opt_key; op++) {
298                 if (*op->opt_valuep)
299                     fprintf(fp, "%s %s\n", kp->km_key, op->opt_key);
300             }
301         } else if (kp->km_func == optiondel) {
302             struct option_list *op;
303
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);
307             }
308         } else
309             fprintf(fp, "# Unknown format %s\n", kp->km_key);
310     }
311
312     fprintf(fp, "\n");
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));
317
318 }
319
320
321 /* add an option to the list */
322 static void
323 set_option(const char *s)
324 {
325     struct option_list *op;
326
327     for (op = Options; op->opt_key; op++) {
328         if (strcmp(op->opt_key, s) == 0) {
329             *op->opt_valuep = 1;
330             return;
331         }
332     }
333     fprintf(stderr, "Unknown option %s\n", s);
334 }
335
336 /* delete an option from the list */
337 static void
338 delete_option(const char *s)
339 {
340     struct option_list *op;
341
342     for (op = Options; op->opt_key; op++) {
343         if (strcmp(op->opt_key, s) == 0) {
344             *op->opt_valuep = 0;
345             return;
346         }
347     }
348     fprintf(stderr, "Unknown option %s\n", s);
349 }
350
351 /* config interface */
352 static void
353 optionset(struct keymatch *kp, s_char **av)
354                                 /* unused - we have a well known global */
355 {
356     char **cpp;
357
358     for (cpp = (char **)av; *cpp; cpp++)
359         set_option(*cpp);
360 }
361
362 /* config interface */
363 static void
364 optiondel(struct keymatch *kp, s_char **av)
365                                 /* unused - we have a well known global */
366 {
367     char **cpp;
368
369     for (cpp = (char **)av; *cpp; cpp++)
370         delete_option(*cpp);
371 }