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
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.
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.
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
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.
28 * emp_config.c: Allows config file to control server config. from a file
30 * Known contributors to this file:
32 * Steve McClure, 1998-2000
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.
60 #include "prototypes.h"
63 static int emp_config_dummy;
65 /* things that can be changed */
66 struct keymatch configkeys[] = {
67 #define EMP_CONFIG_C_OUTPUT
68 #include "econfig-spec.h"
69 #undef EMP_CONFIG_C_OUTPUT
72 static struct keymatch *keylookup(char *key, struct keymatch tbl[]);
73 static void set_dirs(char *);
76 * read in empire configuration
79 emp_config(char *file)
93 if ((fp = fopen(file, "r")) == NULL) {
94 if (file == dflt_econfig && errno == ENOENT)
96 fprintf(stderr, "Can't open %s for reading (%s)\n",
97 file, strerror(errno));
101 while (fgets(buf, sizeof(buf), fp) != NULL) {
103 for (i = 0; buf[i] && isspace(buf[i]); ++i) ;
104 if (!buf[i] || buf[i] == '#')
106 if (parse(buf, av, NULL, scanspace, NULL) < 0) {
107 fprintf(stderr, "%s:%d: Can't parse line %s", file, lno, buf);
111 if ((kp = keylookup(av[0], configkeys)) == NULL) {
112 fprintf(stderr, "%s:%d: Unknown config key %s\n",
118 fprintf(stderr, "%s:%d: Config key %s needs a value\n",
124 switch (kp->km_type) {
126 *(int *)kp->km_data = atoi(av[1]);
129 *(float *)kp->km_data = atof(av[1]);
132 *(double *)kp->km_data = atof(av[1]);
135 *(long *)kp->km_data = atol(av[1]);
138 if (kp->km_flags & KM_ALLOC)
139 free(*(char **)kp->km_data);
140 *(char **)kp->km_data = strdup(av[1]);
141 kp->km_flags |= KM_ALLOC;
147 fprintf(stderr, "%s:%d: Junk after value of config key %s\n",
156 WORLD_X &= ~1; /* force even */
162 /* find the key in the table */
163 static struct keymatch *
164 keylookup(char *command, struct keymatch *tbl)
168 if (command == 0 || *command == 0)
170 for (kp = tbl; kp->km_key != 0; kp++) {
171 if (strcmp(kp->km_key, command) == 0)
178 set_dirs(char *econfig)
181 char *cwd = getcwd(NULL, 0);
184 econfig = _fullpath(NULL, econfig, 0);
185 slash = strrchr(econfig, '\\');
186 configdir = malloc(slash - econfig + 1);
187 memcpy(configdir, econfig, slash - econfig);
188 configdir[slash - econfig] = 0;
190 if ((slash = strrchr(econfig, '/'))) {
191 configdir = malloc(slash - econfig + 1);
192 memcpy(configdir, econfig, slash - econfig);
193 configdir[slash - econfig] = 0;
195 configdir = strdup(cwd);
197 if (configdir[0] != '/') {
198 char *tmp = configdir;
199 size_t len = strlen(cwd);
201 configdir = malloc(len + 1 + strlen(tmp) + 1);
202 sprintf(configdir, "%s/%s", cwd, tmp);
211 print_config(FILE *fp)
215 fprintf(fp, "# Empire Configuration File:\n");
216 for (kp = configkeys; kp->km_key; kp++) {
217 if (kp->km_comment) {
218 if (kp->km_comment[0] != '\n' && kp->km_comment[0] != '#')
220 fprintf(fp, "%s\n", kp->km_comment);
224 switch (kp->km_type) {
226 fprintf(fp, "%s \"%s\"\n", kp->km_key, *(char **)kp->km_data);
229 fprintf(fp, "%s %d\n", kp->km_key, *(int *)kp->km_data);
232 fprintf(fp, "%s %g\n", kp->km_key, *(float *)kp->km_data);
235 fprintf(fp, "%s %g\n", kp->km_key, *(double *)kp->km_data);
238 fprintf(fp, "%s %ld\n", kp->km_key, *(long *)kp->km_data);