]> git.pond.sub.org Git - empserver/blob - src/lib/common/emp_config.c
Virtual selectors
[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 static struct keymatch *keylookup(char *key, struct keymatch tbl[]);
59 static int set_paths(char *);
60
61 /*
62  * read in empire configuration
63  */
64 int
65 emp_config(char *file)
66 {
67     FILE *fp;
68     char scanspace[1024];
69     char *av[128];
70     char buf[1024];
71     struct keymatch *kp;
72     int lno = 0;
73     int errors = 0;
74     int i;
75
76     if (!file)
77         file = dflt_econfig;
78     errno = 0;
79     if ((fp = fopen(file, "r")) == NULL) {
80         if (file == dflt_econfig && errno == ENOENT)
81             goto done;
82         fprintf(stderr, "Can't open %s for reading (%s)\n",
83                 file, strerror(errno));
84         return -1;
85     }
86
87     while (fgets(buf, sizeof(buf), fp) != NULL) {
88         ++lno;
89         for (i = 0; buf[i] && isspace(buf[i]); ++i) ;
90         if (!buf[i] || buf[i] == '#')
91             continue;
92         if (parse(buf, scanspace, av, NULL, NULL, NULL) < 0) {
93             fprintf(stderr, "%s:%d: Can't parse line %s", file, lno, buf);
94             errors = 1;
95             continue;
96         }
97         if ((kp = keylookup(av[0], configkeys)) == NULL) {
98             fprintf(stderr, "%s:%d: Unknown config key %s\n",
99                     file, lno, av[0]);
100             errors = 1;
101             continue;
102         }
103         if (av[1] == NULL) {
104             fprintf(stderr, "%s:%d: Config key %s needs a value\n",
105                     file, lno, av[0]);
106             errors = 1;
107             continue;
108         }
109         i = 2;
110         switch (kp->km_type) {
111         case NSC_INT:
112             *(int *)kp->km_data = atoi(av[1]);
113             break;
114         case NSC_FLOAT:
115             *(float *)kp->km_data = atof(av[1]);
116             break;
117         case NSC_DOUBLE:
118             *(double *)kp->km_data = atof(av[1]);
119             break;
120         case NSC_LONG:
121             *(long *)kp->km_data = atol(av[1]);
122             break;
123         case NSC_STRING:
124             if (kp->km_flags & KM_ALLOC)
125                 free(*(char **)kp->km_data);
126             *(char **)kp->km_data = strdup(av[1]);
127             kp->km_flags |= KM_ALLOC;
128             break;
129         default:
130             assert(0);
131         }
132         if (av[i] != NULL) {
133             fprintf(stderr, "%s:%d: Junk after value of config key %s\n",
134                     file, lno, av[0]);
135             errors = 1;
136         }
137     }
138
139     fclose(fp);
140
141 done:
142     WORLD_X &= ~1;              /* force even */
143     if (set_paths(file) < 0)
144         return -1;
145
146     return -errors;
147 }
148
149 /* find the key in the table */
150 static struct keymatch *
151 keylookup(char *command, struct keymatch *tbl)
152 {
153     struct keymatch *kp;
154
155     if (command == 0 || *command == 0)
156         return 0;
157     for (kp = tbl; kp->km_key != 0; kp++) {
158         if (strcmp(kp->km_key, command) == 0)
159             return kp;
160     }
161     return NULL;
162 }
163
164 static int
165 set_paths(char *econfig)
166 {
167     char *p, *slash;
168
169 #ifdef _WIN32
170     p = _fullpath(NULL, econfig, 0);
171     slash = strrchr(p, '\\');
172 #else
173     char buf[1024];
174     char *cwd;
175
176     cwd = getcwd(buf, sizeof(buf));
177     p = fnameat(econfig, cwd);
178     if (p[0] != '/') {
179         fprintf(stderr, "Can't get current working directory (%s)\n",
180                 strerror(errno));
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 - configdir);
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 }