2 * David Leonard <d@openbsd.org>, 1999. Public domain.
4 * $OpenBSD: conf.c,v 1.7 2007/03/20 03:43:50 tedu Exp $
5 * $DragonFly: src/games/hunt/huntd/conf.c,v 1.1 2008/09/02 21:50:21 dillon Exp $
21 /* Configuration option variables for the server: */
36 int conf_scoredecay = 15;
37 int conf_maxremove = 40;
40 int conf_flytime = 20;
42 int conf_volcano_max = 50;
43 int conf_ptrip_face = 2;
44 int conf_ptrip_back = 95;
45 int conf_ptrip_side = 50;
47 int conf_preflect = 1;
48 int conf_pshot_coll = 5;
49 int conf_pgren_coll = 10;
50 int conf_pgren_catch = 10;
52 int conf_pdroneabsorb = 1;
53 int conf_fall_frac = 5;
58 int conf_maxncshot = 2;
62 int conf_killgain = 2;
63 int conf_slimefactor = 3;
64 int conf_slimespeed = 5;
65 int conf_lavaspeed = 1;
66 int conf_cloaklen = 20;
67 int conf_scanlen = 20;
68 int conf_mindshot = 2;
75 enum vartype { Vint, Vchar, Vstring, Vdouble } type;
78 static struct kwvar keywords[] = {
79 { "random", &conf_random, Vint },
80 { "reflect", &conf_reflect, Vint },
81 { "monitor", &conf_monitor, Vint },
82 { "ooze", &conf_ooze, Vint },
83 { "fly", &conf_fly, Vint },
84 { "volcano", &conf_volcano, Vint },
85 { "drone", &conf_drone, Vint },
86 { "boots", &conf_boots, Vint },
87 { "scan", &conf_scan, Vint },
88 { "cloak", &conf_cloak, Vint },
89 { "logerr", &conf_logerr, Vint },
90 { "syslog", &conf_syslog, Vint },
91 { "scoredecay", &conf_scoredecay, Vint },
92 { "maxremove", &conf_maxremove, Vint },
93 { "linger", &conf_linger, Vint },
95 { "flytime", &conf_flytime, Vint },
96 { "flystep", &conf_flystep, Vint },
97 { "volcano_max", &conf_volcano_max, Vint },
98 { "ptrip_face", &conf_ptrip_face, Vint },
99 { "ptrip_back", &conf_ptrip_back, Vint },
100 { "ptrip_side", &conf_ptrip_side, Vint },
101 { "prandom", &conf_prandom, Vint },
102 { "preflect", &conf_preflect, Vint },
103 { "pshot_coll", &conf_pshot_coll, Vint },
104 { "pgren_coll", &conf_pgren_coll, Vint },
105 { "pgren_catch", &conf_pgren_catch, Vint },
106 { "pmiss", &conf_pmiss, Vint },
107 { "pdroneabsorb", &conf_pdroneabsorb, Vint },
108 { "fall_frac", &conf_fall_frac, Vint },
110 { "bulspd", &conf_bulspd, Vint },
111 { "ishots", &conf_ishots, Vint },
112 { "nshots", &conf_nshots, Vint },
113 { "maxncshot", &conf_maxncshot, Vint },
114 { "maxdam", &conf_maxdam, Vint },
115 { "mindam", &conf_mindam, Vint },
116 { "stabdam", &conf_stabdam, Vint },
117 { "killgain", &conf_killgain, Vint },
118 { "slimefactor", &conf_slimefactor, Vint },
119 { "slimespeed", &conf_slimespeed, Vint },
120 { "lavaspeed", &conf_lavaspeed, Vint },
121 { "cloaklen", &conf_cloaklen, Vint },
122 { "scanlen", &conf_scanlen, Vint },
123 { "mindshot", &conf_mindshot, Vint },
124 { "simstep", &conf_simstep, Vint },
130 parse_int(char *p, struct kwvar *kvp, const char *fnm, int *linep)
132 char *valuestart, *digitstart;
136 /* expect a number */
143 if ((*p == '\0' || isspace(*p) || *p == '#') && digitstart != p) {
146 newval = atoi(valuestart);
148 logx(LOG_INFO, "%s:%d: %s: %d -> %d",
149 fnm, *linep, kvp->kw, *(int *)kvp->var, newval);
150 *(int *)kvp->var = newval;
153 logx(LOG_ERR, "%s:%d: invalid integer value \"%s\"",
154 fnm, *linep, valuestart);
160 parse_value(char *p, struct kwvar *kvp, const char *fnm, int *linep)
165 return parse_int(p, kvp, fnm, linep);
176 parse_line(char *buf, const char *fnm, int *line)
186 /* skip leading white */
189 /* allow blank lines and comment lines */
190 if (*p == '\0' || *p == '#')
193 /* walk to the end of the word: */
195 if (isalpha(*p) || *p == '_') {
197 while (isalpha(*p) || isdigit(*p) || *p == '_')
202 if (endword == word) {
203 logx(LOG_ERR, "%s:%d: expected variable name",
208 /* match the configuration variable name */
211 for (kvp = keywords; kvp->kw; kvp++)
212 if (strcmp(kvp->kw, word) == 0)
216 if (kvp->kw == NULL) {
218 "%s:%d: unrecognised variable \"%.*s\"",
219 fnm, *line, endword - word, word);
223 /* skip whitespace */
228 logx(LOG_ERR, "%s:%d: expected `=' after %s", fnm, *line, word);
232 /* skip whitespace */
236 /* parse the value */
237 p = parse_value(p, kvp, fnm, line);
241 /* skip trailing whitespace */
245 if (*p && *p != '#') {
246 logx(LOG_WARNING, "%s:%d: trailing garbage ignored",
253 load_config(FILE *f, char *fnm)
261 while ((p = fgetln(f, &len)) != NULL) {
263 if (p[len-1] == '\n')
265 if (len >= sizeof(buf)) {
266 logx(LOG_ERR, "%s:%d: line too long", fnm, line);
269 (void)memcpy(buf, p, len);
271 parse_line(buf, fnm, &line);
276 * load various config file, allowing later ones to
277 * overwrite earlier values
283 char nm[MAXPATHLEN + 1];
284 static const char *fnms[] = {
293 /* All the %s's get converted to $HOME */
294 if ((home = getenv("HOME")) == NULL)
297 for (fn = 0; fnms[fn]; fn++) {
298 snprintf(nm, sizeof nm, fnms[fn], home);
299 if ((f = fopen(nm, "r")) != NULL) {
303 else if (errno != ENOENT)
304 logit(LOG_WARNING, "%s", nm);
309 * Parse a single configuration argument given on the command line
317 parse_line(arg, "*Initialisation*", &line);