]> 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-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  emp_config.c: Allows config file to control server config. from a file
28  *
29  *  Known contributors to this file:
30  *     Julian Onions, 1995
31  *     Steve McClure, 1998-2000
32  *     Markus Armbruster, 2004-2011
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 "misc.h"
54 #include "optlist.h"
55 #include "prototypes.h"
56
57 static struct keymatch *keylookup(char *key, struct keymatch tbl[]);
58 static int set_paths(char *);
59
60 /*
61  * read in empire configuration
62  */
63 int
64 emp_config(char *file)
65 {
66     FILE *fp;
67     char scanspace[1024];
68     char *av[128];
69     char buf[1024];
70     struct keymatch *kp;
71     int lno = 0;
72     int errors = 0;
73     int i;
74
75     if (!file)
76         file = dflt_econfig;
77     errno = 0;
78     if ((fp = fopen(file, "r")) == NULL) {
79         if (file == dflt_econfig && errno == ENOENT)
80             goto done;
81         fprintf(stderr, "Can't open %s for reading (%s)\n",
82                 file, strerror(errno));
83         return -1;
84     }
85
86     while (fgets(buf, sizeof(buf), fp) != NULL) {
87         ++lno;
88         for (i = 0; buf[i] && isspace(buf[i]); ++i) ;
89         if (!buf[i] || buf[i] == '#')
90             continue;
91         if (parse(buf, scanspace, av, NULL, NULL, NULL) < 0) {
92             fprintf(stderr, "%s:%d: Can't parse line %s", file, lno, buf);
93             errors = 1;
94             continue;
95         }
96         if ((kp = keylookup(av[0], configkeys)) == NULL) {
97             fprintf(stderr, "%s:%d: Unknown config key %s\n",
98                     file, lno, av[0]);
99             errors = 1;
100             continue;
101         }
102         if (av[1] == NULL) {
103             fprintf(stderr, "%s:%d: Config key %s needs a value\n",
104                     file, lno, av[0]);
105             errors = 1;
106             continue;
107         }
108         i = 2;
109         switch (kp->km_type) {
110         case NSC_INT:
111             *(int *)kp->km_data = atoi(av[1]);
112             break;
113         case NSC_FLOAT:
114             *(float *)kp->km_data = atof(av[1]);
115             break;
116         case NSC_DOUBLE:
117             *(double *)kp->km_data = atof(av[1]);
118             break;
119         case NSC_LONG:
120             *(long *)kp->km_data = atol(av[1]);
121             break;
122         case NSC_STRING:
123             if (kp->km_flags & KM_ALLOC)
124                 free(*(char **)kp->km_data);
125             *(char **)kp->km_data = strdup(av[1]);
126             kp->km_flags |= KM_ALLOC;
127             break;
128         default:
129             assert(0);
130         }
131         if (av[i] != NULL) {
132             fprintf(stderr, "%s:%d: Junk after value of config key %s\n",
133                     file, lno, av[0]);
134             errors = 1;
135         }
136     }
137
138     fclose(fp);
139
140 done:
141     if (set_paths(file) < 0)
142         return -1;
143
144     return -errors;
145 }
146
147 /* find the key in the table */
148 static struct keymatch *
149 keylookup(char *command, struct keymatch *tbl)
150 {
151     struct keymatch *kp;
152
153     if (!command || !*command)
154         return NULL;
155     for (kp = tbl; kp->km_key; kp++) {
156         if (strcmp(kp->km_key, command) == 0)
157             return kp;
158     }
159     return NULL;
160 }
161
162 static int
163 set_paths(char *econfig)
164 {
165     char *p, *slash;
166
167 #ifdef _WIN32
168     p = _fullpath(NULL, econfig, 0);
169     slash = strrchr(p, '\\');
170 #else
171     char buf[1024];
172     char *cwd;
173
174     cwd = getcwd(buf, sizeof(buf));
175     p = fnameat(econfig, cwd);
176     if (p[0] != '/') {
177         fprintf(stderr, "Can't get current working directory (%s)\n",
178                 strerror(errno));
179         if (p != econfig)
180             free(p);
181         return -1;
182     }
183     if (p == econfig)
184         p = strdup(p);
185     slash = strrchr(p, '/');
186 #endif /* !_WIN32 */
187
188     *slash = 0;
189     configdir = realloc(p, slash + 1 - p);
190
191     infodir = fnameat(infodir_conf, configdir);
192     gamedir = fnameat(gamedir_conf, configdir);
193     builtindir = fnameat(builtindir_conf, configdir);
194     schedulefil = fnameat("schedule", configdir);
195
196     return 0;
197 }
198
199 void
200 print_config(FILE *fp)
201 {
202     struct keymatch *kp;
203
204     fprintf(fp, "# Empire Configuration File:\n");
205     for (kp = configkeys; kp->km_key; kp++) {
206         if (kp->km_comment) {
207             if (kp->km_comment[0] != '\n' && kp->km_comment[0] != '#')
208                 fprintf(fp, "\n# ");
209             fprintf(fp, "%s\n", kp->km_comment);
210         }
211         if (!kp->km_key[0])
212             continue;
213         switch (kp->km_type) {
214         case NSC_STRING:
215             fprintf(fp, "%s \"%s\"\n", kp->km_key, *(char **)kp->km_data);
216             break;
217         case NSC_INT:
218             fprintf(fp, "%s %d\n", kp->km_key, *(int *)kp->km_data);
219             break;
220         case NSC_FLOAT:
221             fprintf(fp, "%s %g\n", kp->km_key, *(float *)kp->km_data);
222             break;
223         case NSC_DOUBLE:
224             fprintf(fp, "%s %g\n", kp->km_key, *(double *)kp->km_data);
225             break;
226         case NSC_LONG:
227             fprintf(fp, "%s %ld\n", kp->km_key, *(long *)kp->km_data);
228             break;
229         default:
230             assert(0);
231         }
232     }
233
234     fprintf(fp, "\n");
235 }