]> git.pond.sub.org Git - empserver/blob - src/lib/gen/emp_config.c
(mchr, plchr, lchr, nchr): Move initializer to new builtin config
[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
52 #include "misc.h"
53 #include "file.h"
54 #include "optlist.h"
55 #include "gen.h"
56
57 /* Dummy one */
58 static int emp_config_dummy;
59
60 /* things that can be changed */
61 struct keymatch configkeys[] = {
62 #define EMP_CONFIG_C_OUTPUT
63 #include "econfig-spec.h"
64 #undef  EMP_CONFIG_C_OUTPUT
65 };
66
67 static struct keymatch *keylookup(s_char *key, struct keymatch tbl[]);
68
69 /*
70  * read in empire configuration
71  */
72 int
73 emp_config(char *file)
74 {
75     FILE *fp;
76     char scanspace[1024];
77     char *av[128];
78     char buf[1024];
79     char *slash;
80     struct keymatch *kp;
81     int lno = 0;
82     int errors = 0;
83     int i;
84
85     if (!file)
86         file = dflt_econfig;
87     errno = 0;
88     if ((fp = fopen(file, "r")) == NULL) {
89         if (file == dflt_econfig && errno == ENOENT)
90             return 0;
91         fprintf(stderr, "Can't open %s for reading (%s)\n",
92                 file, strerror(errno));
93         return -1;
94     }
95
96     if ((slash = strrchr(file, '/'))) {
97         configdir = malloc(slash - file + 1);
98         memcpy(configdir, file, slash - file);
99         configdir[slash - file] = 0;
100     } else
101         configdir = NULL;
102
103     while (fgets(buf, sizeof buf, fp) != NULL) {
104         ++lno;
105         for (i = 0; buf[i] && isspace(buf[i]); ++i) ;
106         if (!buf[i] || buf[i] == '#')
107             continue;
108         if (parse(buf, av, NULL, scanspace, NULL) < 0) {
109             fprintf(stderr, "%s:%d: Can't parse line %s", file, lno, buf);
110             errors = 1;
111             continue;
112         }
113         if ((kp = keylookup(av[0], configkeys)) == NULL) {
114             fprintf(stderr, "%s:%d: Unknown config key %s\n",
115                     file, lno, av[0]);
116             errors = 1;
117             continue;
118         }
119         if (av[1] == NULL) {
120             fprintf(stderr, "%s:%d: Config key %s needs a value\n",
121                     file, lno, av[0]);
122             errors = 1;
123             continue;
124         }
125         i = 2;
126         switch (kp->km_type) {
127         case NSC_INT:
128             *(int *)kp->km_data = atoi(av[1]);
129             break;
130         case NSC_FLOAT:
131             *(float *)kp->km_data = atof(av[1]);
132             break;
133         case NSC_DOUBLE:
134             *(double *)kp->km_data = atof(av[1]);
135             break;
136         case NSC_LONG:
137             *(long *)kp->km_data = atol(av[1]);
138             break;
139         case NSC_STRING:
140             if (kp->km_flags & KM_ALLOC)
141                 free(*(char **)kp->km_data);
142             *(char **)kp->km_data = strdup(av[1]);
143             kp->km_flags |= KM_ALLOC;
144             break;
145         default:
146             assert(0);
147         }
148         if (av[i] != NULL) {
149             fprintf(stderr, "%s:%d: Junk after value of config key %s\n",
150                     file, lno, av[0]);
151             errors = 1;
152         }
153     }
154     fclose(fp);
155     WORLD_X &= ~1;              /* make even */
156
157     return -errors;
158 }
159
160 /* find the key in the table */
161 static struct keymatch *
162 keylookup(register s_char *command, struct keymatch *tbl)
163 {
164     register 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 void
176 print_config(FILE *fp)
177 {
178     struct keymatch *kp;
179
180     fprintf(fp, "# Empire Configuration File:\n");
181     for (kp = configkeys; kp->km_key; kp++) {
182         if (kp->km_comment) {
183             if (kp->km_comment[0] != '\n' && kp->km_comment[0] != '#')
184                 fprintf(fp, "\n# ");
185             fprintf(fp, "%s\n", kp->km_comment);
186         }
187         if (!kp->km_key[0])
188             continue;
189         switch (kp->km_type) {
190         case NSC_STRING:
191             fprintf(fp, "%s \"%s\"\n", kp->km_key, *(char **)kp->km_data);
192             break;
193         case NSC_INT:
194             fprintf(fp, "%s %d\n", kp->km_key, *(int *)kp->km_data);
195             break;
196         case NSC_FLOAT:
197             fprintf(fp, "%s %g\n", kp->km_key, *(float *)kp->km_data);
198             break;
199         case NSC_DOUBLE:
200             fprintf(fp, "%s %g\n", kp->km_key, *(double *)kp->km_data);
201             break;
202         case NSC_LONG:
203             fprintf(fp, "%s %ld\n", kp->km_key, *(long *)kp->km_data);
204             break;
205         default:
206             assert(0);
207         }
208     }
209
210     fprintf(fp, "\n");
211 }