2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * nstr.c: compile and execute the item selections on sectors
30 * Known contributors to this file:
33 * Markus Armbruster, 2004
41 #include "prototypes.h"
43 static int nstr_promote(int valtype);
46 * Compile conditions into array NP[LEN].
47 * Return number of conditions, or -1 on error.
48 * It is an error if there are more than LEN conditions.
49 * TYPE is the context type, a file type.
50 * STR is the condition string, in Empire syntax, without the leading
54 nstr_comp(struct nscstr *np, int len, int type, char *str)
60 int lft_type, rgt_type;
63 for (i = 0; ; ++i, ++np) {
68 tail = nstr_comp_val(cond, &np->lft, type);
73 if (*tail != '<' && *tail != '=' && *tail != '>' && *tail != '#') {
75 pr("%s -- expected condition operator\n", cond);
77 pr("%s -- missing condition operator\n", cond);
84 tail = nstr_comp_val(tail, &np->rgt, type);
88 /* find operator type, coerce operands */
89 lft_type = nstr_promote(np->lft.val_type);
90 rgt_type = nstr_promote(np->rgt.val_type);
91 np->optype = NSC_NOTYPE;
92 if (lft_type == NSC_TYPEID) {
93 if (!nstr_coerce_val(&np->rgt, NSC_TYPEID, str))
94 np->optype = NSC_TYPEID;
95 } else if (rgt_type == NSC_TYPEID) {
96 if (!nstr_coerce_val(&np->lft, NSC_TYPEID, str))
97 np->optype = NSC_TYPEID;
98 } else if (lft_type == NSC_STRING) {
99 if (!nstr_coerce_val(&np->rgt, NSC_STRING, str))
100 np->optype = NSC_STRING;
101 } else if (rgt_type == NSC_STRING) {
102 if (!nstr_coerce_val(&np->lft, NSC_STRING, str))
103 np->optype = NSC_STRING;
104 } else if (lft_type == NSC_DOUBLE) {
105 if (!nstr_coerce_val(&np->rgt, NSC_DOUBLE, str))
106 np->optype = NSC_DOUBLE;
107 } else if (rgt_type == NSC_DOUBLE) {
108 if (!nstr_coerce_val(&np->lft, NSC_DOUBLE, str))
109 np->optype = NSC_DOUBLE;
111 if (!nstr_coerce_val(&np->lft, NSC_LONG, str)
112 && !nstr_coerce_val(&np->rgt, NSC_LONG, str))
113 np->optype = NSC_LONG;
115 if (np->optype == NSC_NOTYPE)
118 /* another condition? */
122 pr("%s -- expected `&'\n", cond);
129 /* could just return I and let caller gripe or enlarge buffer */
130 pr("%s -- too many conditions\n", str);
138 strnncmp(char *s1, size_t sz1, char *s2, size_t sz2)
141 if (sz1 == sz2) return strncmp(s1, s2, sz2);
142 if (sz1 < sz2) return -strnncmp(s2, sz2, s1, sz1);
143 res = strncmp(s1, s2, sz2);
144 return res ? res : s1[sz2];
147 #define EVAL(op, lft, rgt) \
148 ((op) == '<' ? (lft) < (rgt) \
149 : (op) == '=' ? (lft) == (rgt) \
150 : (op) == '>' ? (lft) > (rgt) \
151 : (op) == '#' ? (lft) != (rgt) \
155 * Evaluate compiled conditions in array NP[NCOND].
156 * Return non-zero iff they are all true.
157 * PTR points to a context object of the type that was used to compile
161 nstr_exec(struct nscstr *np, int ncond, void *ptr)
163 int i, op, optype, cmp;
164 struct valstr lft, rgt;
166 for (i = 0; i < ncond; ++i) {
168 optype = np[i].optype;
169 if (np[i].lft.val_cat == NSC_NOCAT || np[i].rgt.val_cat == NSC_NOCAT)
172 nstr_exec_val(&lft, player->cnum, ptr, optype);
174 nstr_exec_val(&rgt, player->cnum, ptr, optype);
178 if (!EVAL(op, lft.val_as.lng, rgt.val_as.lng))
182 if (!EVAL(op, lft.val_as.dbl, rgt.val_as.dbl))
186 cmp = strnncmp(lft.val_as.str.base, lft.val_as.str.maxsz,
187 rgt.val_as.str.base, rgt.val_as.str.maxsz);
188 if (!EVAL(op, cmp, 0))
192 CANT_HAPPEN("bad OPTYPE");
201 * Compile a value in STR into VAL.
202 * Return a pointer to the first character after the value on success,
204 * TYPE is the context type, a file type.
205 * If STR names an array, VAL simply refers to the element with index
209 nstr_comp_val(char *str, struct valstr*val, int type)
219 val->val_type = NSC_NOTYPE;
220 val->val_cat = NSC_NOCAT;
221 val->val_as_type = -1;
223 if (isalpha(str[0])) {
225 for (i = 0; isalnum(str[i]) || str[i] == '_'; ++i) {
226 if (i < sizeof(id) - 1)
230 if (i < sizeof(id)) {
233 val->val_as_type = typematch(id, type);
235 cap = ef_cadef(type);
237 j = stmtch(id, cap, offsetof(struct castr, ca_name),
238 sizeof(struct castr));
240 && (!(cap[j].ca_flags & NSC_DEITY) || player->god)) {
241 if (cap[j].ca_type == NSC_TYPEID && val->val_as_type >= 0)
243 * Got two matches of type NSC_TYPEID, need to
244 * choose. Prefer typematch(), because ?des=n
245 * would be interpreted as ?des=newdes
250 val->val_type = cap[j].ca_type;
251 val->val_cat = NSC_OFF;
252 val->val_as.sym.off = cap[j].ca_off;
253 val->val_as.sym.len = cap[j].ca_len;
254 val->val_as.sym.idx = 0;
262 if (val->val_type == NSC_NOTYPE) {
263 if (val->val_as_type >= 0) {
264 val->val_type = NSC_TYPEID;
265 val->val_cat = NSC_VAL;
266 val->val_as.lng = val->val_as_type;
268 pr("%s -- selector access denied\n", id);
269 else if (j == M_NOTUNIQUE)
270 pr("%s -- ambiguous selector name\n", id);
272 pr("%s -- unknown selector name\n", id);
275 return val->val_type == NSC_NOTYPE ? NULL : tail;
278 /* single character type */
281 val->val_as_type = typematch(id, type);
284 l = strtol(str, &tail, 0);
285 d = strtod(str, &tail2);
287 val->val_type = NSC_DOUBLE;
288 val->val_cat = NSC_VAL;
293 val->val_type = NSC_LONG;
294 val->val_cat = NSC_VAL;
298 /* FIXME implement NSC_STRING literals */
300 CANT_HAPPEN(val->val_type != NSC_NOTYPE);
301 if (val->val_as_type >= 0) {
302 val->val_type = NSC_TYPEID;
303 val->val_cat = NSC_VAL;
304 val->val_as.lng = val->val_as_type;
308 pr("%s -- invalid value for condition\n", str);
314 * If VALTYPE is an integer type, return NSC_LONG.
315 * If VALTYPE is a floating-point type, return NSC_DOUBLE.
316 * If VALTYPE is NSC_STRINGY, return NSC_STRING.
317 * If VALTYPE is NSC_NOTYPE, NSC_STRING or NSC_TYPEID, return VALTYPE.
320 nstr_promote(int valtype)
340 valtype = NSC_DOUBLE;
343 valtype = NSC_STRING;
346 CANT_HAPPEN("bad VALTYPE");
347 valtype = NSC_NOTYPE;
353 cond_type_mismatch(char *str)
356 pr("%s -- condition operand type mismatch\n", str);
361 * Coerce VAL to promoted value type TO.
362 * Return 0 on success, -1 on error.
363 * If VAL is evaluated, convert it, else only check.
364 * STR is the condition text to be used for error messages. Suppress
365 * messages if it is a null pointer.
368 nstr_coerce_val(struct valstr *val, nsc_type to, char *str)
370 /* FIXME get rid of promotion? */
371 nsc_type from = nstr_promote(val->val_type);
373 if (from == NSC_NOTYPE)
379 if (val->val_as_type >= 0) {
380 val->val_cat = NSC_VAL;
381 val->val_as.lng = val->val_as_type;
383 return cond_type_mismatch(str);
386 return cond_type_mismatch(str); /* FIXME implement */
388 if (from == NSC_LONG) {
389 if (val->val_cat == NSC_VAL)
390 val->val_as.dbl = val->val_as.lng;
392 return cond_type_mismatch(str);
395 return cond_type_mismatch(str);
397 CANT_HAPPEN("bad TO argument");
402 if (val->val_cat == NSC_VAL) {
403 /* coord literals don't occur, conversion not implemented */
404 CANT_HAPPEN(val->val_type == NSC_XCOORD
405 || val->val_type == NSC_YCOORD);
414 * If VAL is symbolic, evaluate it into a promoted value type.
415 * Use coordinate system of country CNUM.
416 * PTR points to a context object of the type that was used to compile
418 * Unless WANT is NSC_NOTYPE, coerce the value to promoted value type
419 * WANT. VAL must be coercible. That's the case if a previous
420 * nstr_coerce_val(VAL, WANT, STR) succeeded.
423 nstr_exec_val(struct valstr *val, natid cnum, void *ptr, nsc_type want)
429 switch (val->val_cat) {
431 CANT_HAPPEN("Bad VAL category");
434 valtype = val->val_type;
439 memb_ptr += val->val_as.sym.off;
440 idx = val->val_as.sym.idx;
441 switch (val->val_type) {
443 val->val_as.lng = ((signed char *)memb_ptr)[idx];
446 val->val_as.lng = ((unsigned char *)memb_ptr)[idx];
449 val->val_as.lng = ((short *)memb_ptr)[idx];
452 val->val_as.lng = ((unsigned short *)memb_ptr)[idx];
455 val->val_as.lng = ((int *)memb_ptr)[idx];
458 val->val_as.lng = ((long *)memb_ptr)[idx];
461 val->val_as.lng = xrel(getnatp(cnum), ((short *)memb_ptr)[idx]);
464 val->val_as.lng = yrel(getnatp(cnum), ((short *)memb_ptr)[idx]);
467 val->val_as.dbl = ((float *)memb_ptr)[idx];
468 valtype = NSC_DOUBLE;
471 val->val_as.dbl = ((double *)memb_ptr)[idx];
472 valtype = NSC_DOUBLE;
476 val->val_as.str.maxsz = val->val_as.sym.len;
477 val->val_as.str.base = (char *)memb_ptr;
478 valtype = NSC_STRING;
481 val->val_as.str.base = ((char **)memb_ptr)[idx];
482 val->val_as.str.maxsz = INT_MAX;
483 valtype = NSC_STRING;
486 val->val_as.lng = ((time_t *)memb_ptr)[idx];
489 val->val_as.lng = ((signed char *)memb_ptr)[idx];
490 valtype = NSC_TYPEID;
493 CANT_HAPPEN("Bad VAL type");
496 val->val_cat = NSC_VAL;
501 else if (want == NSC_DOUBLE) {
502 if (valtype == NSC_LONG) {
504 val->val_as.dbl = val->val_as.lng;
506 } else if (want == NSC_STRING)
507 CANT_HAPPEN("unimplemented WANT"); /* FIXME */
509 if (CANT_HAPPEN(valtype != want && want != NSC_NOTYPE)) {
513 case NSC_LONG: val->val_as.lng = 0; break;
514 case NSC_DOUBLE: val->val_as.dbl = 0.0; break;
515 case NSC_STRING: val->val_as.str.base = NULL; break;
517 CANT_HAPPEN("bad WANT argument");
521 val->val_type = valtype;