]> git.pond.sub.org Git - empserver/blob - src/lib/common/emp_config.c
Clean up library dependencies
[empserver] / src / lib / common / emp_config.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future 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  * Change other constants - such as MAXNOC 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  */
43
44 #include <config.h>
45
46 #include <assert.h>
47 #include <ctype.h>
48 #include <errno.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <unistd.h>
52
53 #include "file.h"
54 #include "misc.h"
55 #include "optlist.h"
56 #include "prototypes.h"
57
58 /* Dummy one */
59 static int emp_config_dummy;
60
61 /* things that can be changed */
62 struct keymatch configkeys[] = {
63 #define EMP_CONFIG_C_OUTPUT
64 #include "econfig-spec.h"
65 #undef  EMP_CONFIG_C_OUTPUT
66 };
67
68 static struct keymatch *keylookup(char *key, struct keymatch tbl[]);
69 static void set_paths(char *);
70
71 /*
72  * read in empire configuration
73  */
74 int
75 emp_config(char *file)
76 {
77     FILE *fp;
78     char scanspace[1024];
79     char *av[128];
80     char buf[1024];
81     struct keymatch *kp;
82     int lno = 0;
83     int errors = 0;
84     int i;
85
86     if (!file)
87         file = dflt_econfig;
88     errno = 0;
89     if ((fp = fopen(file, "r")) == NULL) {
90         if (file == dflt_econfig && errno == ENOENT)
91             goto done;
92         fprintf(stderr, "Can't open %s for reading (%s)\n",
93                 file, strerror(errno));
94         return -1;
95     }
96
97     while (fgets(buf, sizeof(buf), fp) != NULL) {
98         ++lno;
99         for (i = 0; buf[i] && isspace(buf[i]); ++i) ;
100         if (!buf[i] || buf[i] == '#')
101             continue;
102         if (parse(buf, scanspace, av, NULL, NULL, NULL) < 0) {
103             fprintf(stderr, "%s:%d: Can't parse line %s", file, lno, buf);
104             errors = 1;
105             continue;
106         }
107         if ((kp = keylookup(av[0], configkeys)) == NULL) {
108             fprintf(stderr, "%s:%d: Unknown config key %s\n",
109                     file, lno, av[0]);
110             errors = 1;
111             continue;
112         }
113         if (av[1] == NULL) {
114             fprintf(stderr, "%s:%d: Config key %s needs a value\n",
115                     file, lno, av[0]);
116             errors = 1;
117             continue;
118         }
119         i = 2;
120         switch (kp->km_type) {
121         case NSC_INT:
122             *(int *)kp->km_data = atoi(av[1]);
123             break;
124         case NSC_FLOAT:
125             *(float *)kp->km_data = atof(av[1]);
126             break;
127         case NSC_DOUBLE:
128             *(double *)kp->km_data = atof(av[1]);
129             break;
130         case NSC_LONG:
131             *(long *)kp->km_data = atol(av[1]);
132             break;
133         case NSC_STRING:
134             if (kp->km_flags & KM_ALLOC)
135                 free(*(char **)kp->km_data);
136             *(char **)kp->km_data = strdup(av[1]);
137             kp->km_flags |= KM_ALLOC;
138             break;
139         default:
140             assert(0);
141         }
142         if (av[i] != NULL) {
143             fprintf(stderr, "%s:%d: Junk after value of config key %s\n",
144                     file, lno, av[0]);
145             errors = 1;
146         }
147     }
148
149     fclose(fp);
150
151 done:
152     WORLD_X &= ~1;              /* force even */
153     set_paths(file);
154
155     return -errors;
156 }
157
158 /* find the key in the table */
159 static struct keymatch *
160 keylookup(char *command, struct keymatch *tbl)
161 {
162     struct keymatch *kp;
163
164     if (command == 0 || *command == 0)
165         return 0;
166     for (kp = tbl; kp->km_key != 0; kp++) {
167         if (strcmp(kp->km_key, command) == 0)
168             return kp;
169     }
170     return NULL;
171 }
172
173 static void
174 set_paths(char *econfig)
175 {
176     char *slash;
177     char *cwd = getcwd(NULL, 0);
178
179 #ifdef _WIN32
180     /* normalize path separator to '\\', for easier searching: */
181     econfig = _fullpath(NULL, econfig, 0);
182     slash = strrchr(econfig, '\\');
183     configdir = malloc(slash - econfig + 1);
184     memcpy(configdir, econfig, slash - econfig);
185     configdir[slash - econfig] = 0;
186 #else
187     if ((slash = strrchr(econfig, '/'))) {
188         configdir = malloc(slash - econfig + 1);
189         memcpy(configdir, econfig, slash - econfig);
190         configdir[slash - econfig] = 0;
191     } else
192         configdir = strdup(cwd);
193
194     if (configdir[0] != '/') {
195         char *tmp = configdir;
196         size_t len = strlen(cwd);
197
198         configdir = malloc(len + 1 + strlen(tmp) + 1);
199         sprintf(configdir, "%s/%s", cwd, tmp);
200         free(tmp);
201     }
202 #endif /* !_WIN32 */
203
204     schedulefil = malloc(strlen(configdir) + 10);
205     sprintf(schedulefil, "%s/schedule", configdir);
206
207     free(cwd);
208 }
209
210 void
211 print_config(FILE *fp)
212 {
213     struct keymatch *kp;
214
215     fprintf(fp, "# Empire Configuration File:\n");
216     for (kp = configkeys; kp->km_key; kp++) {
217         if (kp->km_comment) {
218             if (kp->km_comment[0] != '\n' && kp->km_comment[0] != '#')
219                 fprintf(fp, "\n# ");
220             fprintf(fp, "%s\n", kp->km_comment);
221         }
222         if (!kp->km_key[0])
223             continue;
224         switch (kp->km_type) {
225         case NSC_STRING:
226             fprintf(fp, "%s \"%s\"\n", kp->km_key, *(char **)kp->km_data);
227             break;
228         case NSC_INT:
229             fprintf(fp, "%s %d\n", kp->km_key, *(int *)kp->km_data);
230             break;
231         case NSC_FLOAT:
232             fprintf(fp, "%s %g\n", kp->km_key, *(float *)kp->km_data);
233             break;
234         case NSC_DOUBLE:
235             fprintf(fp, "%s %g\n", kp->km_key, *(double *)kp->km_data);
236             break;
237         case NSC_LONG:
238             fprintf(fp, "%s %ld\n", kp->km_key, *(long *)kp->km_data);
239             break;
240         default:
241             assert(0);
242         }
243     }
244
245     fprintf(fp, "\n");
246 }