]> git.pond.sub.org Git - empserver/blob - src/lib/gen/emp_config.c
(dflt_econfig): New.
[empserver] / src / lib / gen / emp_config.c
1 /*
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
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 <assert.h>
47 #include <errno.h>
48 #include <stdio.h>
49 #include <stdlib.h>             /* atoi free atol */
50 #include <string.h>
51
52 #include "misc.h"
53 #include "file.h"
54 #include "optlist.h"
55 #include "gen.h"                /* parse */
56
57 /* for systems without strdup  */
58 #ifdef NOSTRDUP
59 extern char *strdup();
60 #endif /* NOSTRDUP */
61
62 /* Dummy one */
63 static int emp_config_dummy;
64
65 /* things that can be changed */
66 struct keymatch configkeys[] = {
67 #define EMP_CONFIG_C_OUTPUT
68 #include "econfig-spec.h"
69 #undef  EMP_CONFIG_C_OUTPUT
70 };
71
72 static void fixup_files(void);
73 static struct keymatch *keylookup(s_char *key, struct keymatch tbl[]);
74 static int set_option(const char *, int);
75
76 /*
77  * read in empire configuration
78  */
79 int
80 emp_config(char *file)
81 {
82     FILE *fp;
83     char scanspace[1024];
84     char *av[65];
85     char buf[BUFSIZ];
86     struct keymatch *kp;
87     int lno = 0;
88     int errors = 0;
89     int i;
90
91     if (!file) {
92         if (!*dflt_econfig) {
93             /* No default econfig, use compiled in configuration */
94             fixup_files();
95             return 0;
96         }
97         file = dflt_econfig;
98     }
99     if ((fp = fopen(file, "r")) == NULL) {
100         fprintf(stderr, "Can't open %s for reading (%s)\n",
101                 file, strerror(errno));
102         return -1;
103     }
104
105     while (fgets(buf, sizeof buf, fp) != NULL) {
106         ++lno;
107         for (i = 0; buf[i] && isspace(buf[i]); ++i) ;
108         if (!buf[i] || buf[i] == '#')
109             continue;
110         if (parse(buf, av, 0, scanspace, 0) < 0) {
111             fprintf(stderr, "%s:%d: Can't parse line %s", file, lno, buf);
112             errors = 1;
113             continue;
114         }
115         if ((kp = keylookup(av[0], configkeys)) == NULL) {
116             fprintf(stderr, "%s:%d: Unknown config key %s\n",
117                     file, lno, av[0]);
118             errors = 1;
119             continue;
120         }
121         if (av[1] == NULL) {
122             fprintf(stderr, "%s:%d: Config key %s needs a value\n",
123                     file, lno, av[0]);
124             errors = 1;
125             continue;
126         }
127         i = 2;
128         switch (kp->km_type) {
129         case NSC_INT:
130             *(int *)kp->km_data = atoi(av[1]);
131             break;
132         case NSC_FLOAT:
133             *(float *)kp->km_data = atof(av[1]);
134             break;
135         case NSC_DOUBLE:
136             *(double *)kp->km_data = atof(av[1]);
137             break;
138         case NSC_LONG:
139             *(long *)kp->km_data = atol(av[1]);
140             break;
141         case NSC_STRING:
142             if (kp->km_flags & KM_ALLOC)
143                 free(*(char **)kp->km_data);
144             *(char **)kp->km_data = strdup(av[1]);
145             kp->km_flags |= KM_ALLOC;
146             break;
147         case NSC_NOTYPE:
148             for (i = 1; av[i]; ++i) {
149                 if (set_option(av[i], kp->km_key[0] != 'n') < 0) {
150                     fprintf(stderr, "%s:%d: Unknown option %s\n",
151                             file, lno, av[i]);
152                     errors = 1;
153                 }
154             }
155             break;
156         default:
157             assert(0);
158         }
159         if (av[i] != NULL) {
160             fprintf(stderr, "%s:%d: Junk after value of config key %s\n",
161                     file, lno, av[0]);
162             errors = 1;
163         }
164     }
165     fclose(fp);
166     fixup_files();
167     WORLD_X &= ~1;              /* make even */
168
169     return -errors;
170 }
171
172 struct otherfiles {
173     char **files;
174     char *name;
175 };
176
177 /* list of other well known files... -maybe tailor these oneday
178  * anyway - meantime they are all relative to datadir */
179 static struct otherfiles ofiles[] = {
180     {&motdfil, "motd"},
181     {&downfil, "down"},
182     {&disablefil, "disable"},
183     {&banfil, "ban"},
184     {&authfil, "auth"},
185     {&annfil, "ann"},
186     {&timestampfil, "timestamp"},
187     {&teldir, "tel"},
188     {&telfil, "tel/tel"},
189     {NULL, NULL}
190 };
191
192 /* fix up the empfile struct to reference full path names */
193 static void
194 fixup_files(void)
195 {
196     struct empfile *ep;
197     struct otherfiles *op;
198     s_char buf[1024];
199
200     for (ep = empfile; ep < &empfile[EF_MAX]; ep++) {
201         sprintf(buf, "%s/%s", datadir, ep->name);
202         ep->file = strdup(buf);
203     }
204
205     for (op = ofiles; op->files; op++) {
206         sprintf(buf, "%s/%s", datadir, op->name);
207         *op->files = strdup(buf);
208     }
209 }
210
211 /* find the key in the table */
212 static struct keymatch *
213 keylookup(register s_char *command, struct keymatch *tbl)
214 {
215     register struct keymatch *kp;
216
217     if (command == 0 || *command == 0)
218         return 0;
219     for (kp = tbl; kp->km_key != 0; kp++) {
220         if (strcmp(kp->km_key, command) == 0)
221             return kp;
222     }
223     return NULL;
224 }
225
226 void
227 print_config(FILE *fp)
228 {
229     struct empfile *ep;
230     struct option_list *op;
231     struct otherfiles *ofp;
232     struct keymatch *kp;
233
234     fprintf(fp, "# Empire Configuration File:\n");
235     for (kp = configkeys; kp->km_key; kp++) {
236         if (kp->km_comment) {
237             if (kp->km_comment[0] != '\n')
238                 fprintf(fp, "\n# ");
239             fprintf(fp, "%s\n", kp->km_comment);
240         }
241         if (!kp->km_key[0])
242             continue;
243         switch (kp->km_type) {
244         case NSC_STRING:
245             fprintf(fp, "%s \"%s\"\n", kp->km_key, *(char **)kp->km_data);
246             break;
247         case NSC_INT:
248             fprintf(fp, "%s %d\n", kp->km_key, *(int *)kp->km_data);
249             break;
250         case NSC_FLOAT:
251             fprintf(fp, "%s %g\n", kp->km_key, *(float *)kp->km_data);
252             break;
253         case NSC_DOUBLE:
254             fprintf(fp, "%s %g\n", kp->km_key, *(double *)kp->km_data);
255             break;
256         case NSC_LONG:
257             fprintf(fp, "%s %ld\n", kp->km_key, *(long *)kp->km_data);
258             break;
259         case NSC_NOTYPE:
260             for (op = Options; op->opt_key; op++)
261                 if (*op->opt_valuep != (kp->km_key[0] == 'n'))
262                     fprintf(fp, "%s %s\n", kp->km_key, op->opt_key);
263             break;
264         default:
265             assert(0);
266         }
267     }
268
269     fprintf(fp, "\n");
270     for (ep = empfile; ep < &empfile[EF_MAX]; ep++)
271         fprintf(fp, "# File %s -> %s\n", ep->name, ep->file);
272     for (ofp = ofiles; ofp->files; ofp++)
273         fprintf(fp, "# File %s -> %s\n", ofp->name, *(ofp->files));
274
275 }
276
277
278 /* Set option S to value VAL; return 0 on success, -1 on failure.  */
279 static int
280 set_option(const char *s, int val)
281 {
282     struct option_list *op;
283
284     for (op = Options; op->opt_key; op++) {
285         if (strcmp(op->opt_key, s) == 0) {
286             *op->opt_valuep = val;
287             return 0;
288         }
289     }
290     return -1;
291 }