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