]> git.pond.sub.org Git - empserver/blob - src/lib/gen/emp_config.c
(emp_config): Non-portable strndup() crept into rev. 1.33. Replace by
[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  * 1. Change other constants - such as Num Countries 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  * 2. Could look at loading in planes, units etc. Should be easy enough.
43  *
44  */
45
46 #include <config.h>
47
48 #include <assert.h>
49 #include <errno.h>
50 #include <stdio.h>
51 #include <stdlib.h>             /* atoi free atol */
52 #include <string.h>
53
54 #include "misc.h"
55 #include "file.h"
56 #include "optlist.h"
57 #include "gen.h"                /* parse */
58
59 /* Dummy one */
60 static int emp_config_dummy;
61
62 /* things that can be changed */
63 struct keymatch configkeys[] = {
64 #define EMP_CONFIG_C_OUTPUT
65 #include "econfig-spec.h"
66 #undef  EMP_CONFIG_C_OUTPUT
67 };
68
69 static struct keymatch *keylookup(s_char *key, struct keymatch tbl[]);
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     char *slash;
82     struct keymatch *kp;
83     int lno = 0;
84     int errors = 0;
85     int i;
86
87     if (!file)
88         file = dflt_econfig;
89     errno = 0;
90     if ((fp = fopen(file, "r")) == NULL) {
91         if (file == dflt_econfig && errno == ENOENT)
92             return 0;
93         fprintf(stderr, "Can't open %s for reading (%s)\n",
94                 file, strerror(errno));
95         return -1;
96     }
97
98     if ((slash = strrchr(file, '/'))) {
99         configdir = malloc(slash - file + 1);
100         memcpy(configdir, file, slash - file);
101         configdir[slash - file] = 0;
102     } else
103         configdir = NULL;
104
105     while (fgets(buf, sizeof buf, fp) != NULL) {
106         ++lno;
107         for (i = 0; buf[i] && isspace(buf[i]); ++i) ;
108         if (!buf[i] || buf[i] == '#')
109             continue;
110         if (parse(buf, av, NULL, scanspace, NULL) < 0) {
111             fprintf(stderr, "%s:%d: Can't parse line %s", file, lno, buf);
112             errors = 1;
113             continue;
114         }
115         if ((kp = keylookup(av[0], configkeys)) == NULL) {
116             fprintf(stderr, "%s:%d: Unknown config key %s\n",
117                     file, lno, av[0]);
118             errors = 1;
119             continue;
120         }
121         if (av[1] == NULL) {
122             fprintf(stderr, "%s:%d: Config key %s needs a value\n",
123                     file, lno, av[0]);
124             errors = 1;
125             continue;
126         }
127         i = 2;
128         switch (kp->km_type) {
129         case NSC_INT:
130             *(int *)kp->km_data = atoi(av[1]);
131             break;
132         case NSC_FLOAT:
133             *(float *)kp->km_data = atof(av[1]);
134             break;
135         case NSC_DOUBLE:
136             *(double *)kp->km_data = atof(av[1]);
137             break;
138         case NSC_LONG:
139             *(long *)kp->km_data = atol(av[1]);
140             break;
141         case NSC_STRING:
142             if (kp->km_flags & KM_ALLOC)
143                 free(*(char **)kp->km_data);
144             *(char **)kp->km_data = strdup(av[1]);
145             kp->km_flags |= KM_ALLOC;
146             break;
147         default:
148             assert(0);
149         }
150         if (av[i] != NULL) {
151             fprintf(stderr, "%s:%d: Junk after value of config key %s\n",
152                     file, lno, av[0]);
153             errors = 1;
154         }
155     }
156     fclose(fp);
157     WORLD_X &= ~1;              /* make even */
158
159     return -errors;
160 }
161
162 /* find the key in the table */
163 static struct keymatch *
164 keylookup(register s_char *command, struct keymatch *tbl)
165 {
166     register struct keymatch *kp;
167
168     if (command == 0 || *command == 0)
169         return 0;
170     for (kp = tbl; kp->km_key != 0; kp++) {
171         if (strcmp(kp->km_key, command) == 0)
172             return kp;
173     }
174     return NULL;
175 }
176
177 void
178 print_config(FILE *fp)
179 {
180     struct keymatch *kp;
181
182     fprintf(fp, "# Empire Configuration File:\n");
183     for (kp = configkeys; kp->km_key; kp++) {
184         if (kp->km_comment) {
185             if (kp->km_comment[0] != '\n' && kp->km_comment[0] != '#')
186                 fprintf(fp, "\n# ");
187             fprintf(fp, "%s\n", kp->km_comment);
188         }
189         if (!kp->km_key[0])
190             continue;
191         switch (kp->km_type) {
192         case NSC_STRING:
193             fprintf(fp, "%s \"%s\"\n", kp->km_key, *(char **)kp->km_data);
194             break;
195         case NSC_INT:
196             fprintf(fp, "%s %d\n", kp->km_key, *(int *)kp->km_data);
197             break;
198         case NSC_FLOAT:
199             fprintf(fp, "%s %g\n", kp->km_key, *(float *)kp->km_data);
200             break;
201         case NSC_DOUBLE:
202             fprintf(fp, "%s %g\n", kp->km_key, *(double *)kp->km_data);
203             break;
204         case NSC_LONG:
205             fprintf(fp, "%s %ld\n", kp->km_key, *(long *)kp->km_data);
206             break;
207         default:
208             assert(0);
209         }
210     }
211
212     fprintf(fp, "\n");
213 }