]> git.pond.sub.org Git - empserver/blob - src/lib/gen/emp_config.c
[NOSTRDUP]: Drop support for systems without strdup(). First commit
[empserver] / src / lib / gen / emp_config.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 /* Dummy one */
58 static int emp_config_dummy;
59
60 /* things that can be changed */
61 struct keymatch configkeys[] = {
62 #define EMP_CONFIG_C_OUTPUT
63 #include "econfig-spec.h"
64 #undef  EMP_CONFIG_C_OUTPUT
65 };
66
67 static struct keymatch *keylookup(s_char *key, struct keymatch tbl[]);
68 static int set_option(const char *, int);
69
70 /*
71  * read in empire configuration
72  */
73 int
74 emp_config(char *file)
75 {
76     FILE *fp;
77     char scanspace[1024];
78     char *av[128];
79     char buf[1024];
80     struct keymatch *kp;
81     int lno = 0;
82     int errors = 0;
83     int i;
84
85     if (!file)
86         file = dflt_econfig;
87     errno = 0;
88     if ((fp = fopen(file, "r")) == NULL) {
89         if (file == dflt_econfig && errno == ENOENT)
90             return 0;
91         fprintf(stderr, "Can't open %s for reading (%s)\n",
92                 file, strerror(errno));
93         return -1;
94     }
95
96     while (fgets(buf, sizeof buf, fp) != NULL) {
97         ++lno;
98         for (i = 0; buf[i] && isspace(buf[i]); ++i) ;
99         if (!buf[i] || buf[i] == '#')
100             continue;
101         if (parse(buf, av, NULL, scanspace, NULL) < 0) {
102             fprintf(stderr, "%s:%d: Can't parse line %s", file, lno, buf);
103             errors = 1;
104             continue;
105         }
106         if ((kp = keylookup(av[0], configkeys)) == NULL) {
107             fprintf(stderr, "%s:%d: Unknown config key %s\n",
108                     file, lno, av[0]);
109             errors = 1;
110             continue;
111         }
112         if (av[1] == NULL) {
113             fprintf(stderr, "%s:%d: Config key %s needs a value\n",
114                     file, lno, av[0]);
115             errors = 1;
116             continue;
117         }
118         i = 2;
119         switch (kp->km_type) {
120         case NSC_INT:
121             *(int *)kp->km_data = atoi(av[1]);
122             break;
123         case NSC_FLOAT:
124             *(float *)kp->km_data = atof(av[1]);
125             break;
126         case NSC_DOUBLE:
127             *(double *)kp->km_data = atof(av[1]);
128             break;
129         case NSC_LONG:
130             *(long *)kp->km_data = atol(av[1]);
131             break;
132         case NSC_STRING:
133             if (kp->km_flags & KM_ALLOC)
134                 free(*(char **)kp->km_data);
135             *(char **)kp->km_data = strdup(av[1]);
136             kp->km_flags |= KM_ALLOC;
137             break;
138         case NSC_NOTYPE:
139             for (i = 1; av[i]; ++i) {
140                 if (set_option(av[i], kp->km_key[0] != 'n') < 0) {
141                     fprintf(stderr, "%s:%d: Unknown option %s\n",
142                             file, lno, av[i]);
143                     errors = 1;
144                 }
145             }
146             break;
147         default:
148             assert(0);
149         }
150         if (av[i] != NULL) {
151             fprintf(stderr, "%s:%d: Junk after value of config key %s\n",
152                     file, lno, av[0]);
153             errors = 1;
154         }
155     }
156     fclose(fp);
157     WORLD_X &= ~1;              /* make even */
158
159     return -errors;
160 }
161
162 /* find the key in the table */
163 static struct keymatch *
164 keylookup(register s_char *command, struct keymatch *tbl)
165 {
166     register struct keymatch *kp;
167
168     if (command == 0 || *command == 0)
169         return 0;
170     for (kp = tbl; kp->km_key != 0; kp++) {
171         if (strcmp(kp->km_key, command) == 0)
172             return kp;
173     }
174     return NULL;
175 }
176
177 void
178 print_config(FILE *fp)
179 {
180     struct option_list *op;
181     struct keymatch *kp;
182
183     fprintf(fp, "# Empire Configuration File:\n");
184     for (kp = configkeys; kp->km_key; kp++) {
185         if (kp->km_comment) {
186             if (kp->km_comment[0] != '\n')
187                 fprintf(fp, "\n# ");
188             fprintf(fp, "%s\n", kp->km_comment);
189         }
190         if (!kp->km_key[0])
191             continue;
192         switch (kp->km_type) {
193         case NSC_STRING:
194             fprintf(fp, "%s \"%s\"\n", kp->km_key, *(char **)kp->km_data);
195             break;
196         case NSC_INT:
197             fprintf(fp, "%s %d\n", kp->km_key, *(int *)kp->km_data);
198             break;
199         case NSC_FLOAT:
200             fprintf(fp, "%s %g\n", kp->km_key, *(float *)kp->km_data);
201             break;
202         case NSC_DOUBLE:
203             fprintf(fp, "%s %g\n", kp->km_key, *(double *)kp->km_data);
204             break;
205         case NSC_LONG:
206             fprintf(fp, "%s %ld\n", kp->km_key, *(long *)kp->km_data);
207             break;
208         case NSC_NOTYPE:
209             for (op = Options; op->opt_key; op++)
210                 if (*op->opt_valuep != (kp->km_key[0] == 'n'))
211                     fprintf(fp, "%s %s\n", kp->km_key, op->opt_key);
212             break;
213         default:
214             assert(0);
215         }
216     }
217
218     fprintf(fp, "\n");
219 }
220
221
222 /* Set option S to value VAL; return 0 on success, -1 on failure.  */
223 static int
224 set_option(const char *s, int val)
225 {
226     struct option_list *op;
227
228     for (op = Options; op->opt_key; op++) {
229         if (strcmp(op->opt_key, s) == 0) {
230             *op->opt_valuep = val;
231             return 0;
232         }
233     }
234     return -1;
235 }