]> git.pond.sub.org Git - empserver/blob - src/lib/common/emp_config.c
Update copyright notice
[empserver] / src / lib / common / emp_config.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, 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  *     Markus Armbruster, 2004-2009
34  */
35
36 /*
37  * STILL TO DO
38  *
39  * Change other constants - such as MAXNOC etc.
40  * Just requires variables to be assigned, then dynamic allocation in
41  * a few places.  Some checks needed in the server to check the world
42  * hasn't changed size etc.
43  */
44
45 #include <config.h>
46
47 #include <assert.h>
48 #include <ctype.h>
49 #include <errno.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <unistd.h>
53
54 #include "file.h"
55 #include "misc.h"
56 #include "optlist.h"
57 #include "prototypes.h"
58
59 static struct keymatch *keylookup(char *key, struct keymatch tbl[]);
60 static int set_paths(char *);
61
62 /*
63  * read in empire configuration
64  */
65 int
66 emp_config(char *file)
67 {
68     FILE *fp;
69     char scanspace[1024];
70     char *av[128];
71     char buf[1024];
72     struct keymatch *kp;
73     int lno = 0;
74     int errors = 0;
75     int i;
76
77     if (!file)
78         file = dflt_econfig;
79     errno = 0;
80     if ((fp = fopen(file, "r")) == NULL) {
81         if (file == dflt_econfig && errno == ENOENT)
82             goto done;
83         fprintf(stderr, "Can't open %s for reading (%s)\n",
84                 file, strerror(errno));
85         return -1;
86     }
87
88     while (fgets(buf, sizeof(buf), fp) != NULL) {
89         ++lno;
90         for (i = 0; buf[i] && isspace(buf[i]); ++i) ;
91         if (!buf[i] || buf[i] == '#')
92             continue;
93         if (parse(buf, scanspace, av, NULL, NULL, NULL) < 0) {
94             fprintf(stderr, "%s:%d: Can't parse line %s", file, lno, buf);
95             errors = 1;
96             continue;
97         }
98         if ((kp = keylookup(av[0], configkeys)) == NULL) {
99             fprintf(stderr, "%s:%d: Unknown config key %s\n",
100                     file, lno, av[0]);
101             errors = 1;
102             continue;
103         }
104         if (av[1] == NULL) {
105             fprintf(stderr, "%s:%d: Config key %s needs a value\n",
106                     file, lno, av[0]);
107             errors = 1;
108             continue;
109         }
110         i = 2;
111         switch (kp->km_type) {
112         case NSC_INT:
113             *(int *)kp->km_data = atoi(av[1]);
114             break;
115         case NSC_FLOAT:
116             *(float *)kp->km_data = atof(av[1]);
117             break;
118         case NSC_DOUBLE:
119             *(double *)kp->km_data = atof(av[1]);
120             break;
121         case NSC_LONG:
122             *(long *)kp->km_data = atol(av[1]);
123             break;
124         case NSC_STRING:
125             if (kp->km_flags & KM_ALLOC)
126                 free(*(char **)kp->km_data);
127             *(char **)kp->km_data = strdup(av[1]);
128             kp->km_flags |= KM_ALLOC;
129             break;
130         default:
131             assert(0);
132         }
133         if (av[i] != NULL) {
134             fprintf(stderr, "%s:%d: Junk after value of config key %s\n",
135                     file, lno, av[0]);
136             errors = 1;
137         }
138     }
139
140     fclose(fp);
141
142 done:
143     WORLD_X &= ~1;              /* force even */
144     if (set_paths(file) < 0)
145         return -1;
146
147     return -errors;
148 }
149
150 /* find the key in the table */
151 static struct keymatch *
152 keylookup(char *command, struct keymatch *tbl)
153 {
154     struct keymatch *kp;
155
156     if (!command || !*command)
157         return NULL;
158     for (kp = tbl; kp->km_key; kp++) {
159         if (strcmp(kp->km_key, command) == 0)
160             return kp;
161     }
162     return NULL;
163 }
164
165 static int
166 set_paths(char *econfig)
167 {
168     char *p, *slash;
169
170 #ifdef _WIN32
171     p = _fullpath(NULL, econfig, 0);
172     slash = strrchr(p, '\\');
173 #else
174     char buf[1024];
175     char *cwd;
176
177     cwd = getcwd(buf, sizeof(buf));
178     p = fnameat(econfig, cwd);
179     if (p[0] != '/') {
180         fprintf(stderr, "Can't get current working directory (%s)\n",
181                 strerror(errno));
182         return -1;
183     }
184     if (p == econfig)
185         p = strdup(p);
186     slash = strrchr(p, '/');
187 #endif /* !_WIN32 */
188
189     *slash = 0;
190     configdir = realloc(p, slash + 1 - p);
191
192     infodir = fnameat(infodir_conf, configdir);
193     gamedir = fnameat(gamedir_conf, configdir);
194     builtindir = fnameat(builtindir_conf, configdir);
195     schedulefil = fnameat("schedule", configdir);
196
197     return 0;
198 }
199
200 void
201 print_config(FILE *fp)
202 {
203     struct keymatch *kp;
204
205     fprintf(fp, "# Empire Configuration File:\n");
206     for (kp = configkeys; kp->km_key; kp++) {
207         if (kp->km_comment) {
208             if (kp->km_comment[0] != '\n' && kp->km_comment[0] != '#')
209                 fprintf(fp, "\n# ");
210             fprintf(fp, "%s\n", kp->km_comment);
211         }
212         if (!kp->km_key[0])
213             continue;
214         switch (kp->km_type) {
215         case NSC_STRING:
216             fprintf(fp, "%s \"%s\"\n", kp->km_key, *(char **)kp->km_data);
217             break;
218         case NSC_INT:
219             fprintf(fp, "%s %d\n", kp->km_key, *(int *)kp->km_data);
220             break;
221         case NSC_FLOAT:
222             fprintf(fp, "%s %g\n", kp->km_key, *(float *)kp->km_data);
223             break;
224         case NSC_DOUBLE:
225             fprintf(fp, "%s %g\n", kp->km_key, *(double *)kp->km_data);
226             break;
227         case NSC_LONG:
228             fprintf(fp, "%s %ld\n", kp->km_key, *(long *)kp->km_data);
229             break;
230         default:
231             assert(0);
232         }
233     }
234
235     fprintf(fp, "\n");
236 }