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
42 static int nstr_promote(int valtype);
45 * Compile conditions into array NP[LEN].
46 * Return number of conditions, or -1 on error.
47 * It is an error if there are more than LEN conditions.
48 * TYPE is the context type, a file type.
49 * STR is the condition string, in Empire syntax, without the leading
53 nstr_comp(struct nscstr *np, int len, int type, char *str)
59 int lft_type, rgt_type;
62 for (i = 0; ; ++i, ++np) {
67 tail = nstr_comp_val(cond, &np->lft, type);
72 if (*tail != '<' && *tail != '=' && *tail != '>' && *tail != '#') {
74 pr("%s -- expected condition operator\n", cond);
76 pr("%s -- missing condition operator\n", cond);
83 tail = nstr_comp_val(tail, &np->rgt, type);
87 /* find operator type, coerce operands */
88 lft_type = nstr_promote(np->lft.val_type);
89 rgt_type = nstr_promote(np->rgt.val_type);
90 np->optype = NSC_NOTYPE;
91 if (lft_type == NSC_TYPEID) {
92 if (!nstr_coerce_val(&np->rgt, NSC_TYPEID, str))
93 np->optype = NSC_TYPEID;
94 } else if (rgt_type == NSC_TYPEID) {
95 if (!nstr_coerce_val(&np->lft, NSC_TYPEID, str))
96 np->optype = NSC_TYPEID;
97 } else if (lft_type == NSC_STRING) {
98 if (!nstr_coerce_val(&np->rgt, NSC_STRING, str))
99 np->optype = NSC_STRING;
100 } else if (rgt_type == NSC_STRING) {
101 if (!nstr_coerce_val(&np->lft, NSC_STRING, str))
102 np->optype = NSC_STRING;
103 } else if (lft_type == NSC_DOUBLE) {
104 if (!nstr_coerce_val(&np->rgt, NSC_DOUBLE, str))
105 np->optype = NSC_DOUBLE;
106 } else if (rgt_type == NSC_DOUBLE) {
107 if (!nstr_coerce_val(&np->lft, NSC_DOUBLE, str))
108 np->optype = NSC_DOUBLE;
110 if (!nstr_coerce_val(&np->lft, NSC_LONG, str)
111 && !nstr_coerce_val(&np->rgt, NSC_LONG, str))
112 np->optype = NSC_LONG;
114 if (np->optype == NSC_NOTYPE)
117 /* another condition? */
121 pr("%s -- expected `&'\n", cond);
128 /* could just return I and let caller gripe or enlarge buffer */
129 pr("%s -- too many conditions\n", str);
137 strnncmp(char *s1, size_t sz1, char *s2, size_t sz2)
140 if (sz1 == sz2) return strncmp(s1, s2, sz2);
141 if (sz1 < sz2) return -strnncmp(s2, sz2, s1, sz1);
142 res = strncmp(s1, s2, sz2);
143 return res ? res : s1[sz2];
146 #define EVAL(op, lft, rgt) \
147 ((op) == '<' ? (lft) < (rgt) \
148 : (op) == '=' ? (lft) == (rgt) \
149 : (op) == '>' ? (lft) > (rgt) \
150 : (op) == '#' ? (lft) != (rgt) \
154 * Evaluate compiled conditions in array NP[NCOND].
155 * Return non-zero iff they are all true.
156 * PTR points to a context object of the type that was used to compile
160 nstr_exec(struct nscstr *np, int ncond, void *ptr)
162 int i, op, optype, cmp;
163 struct valstr lft, rgt;
165 for (i = 0; i < ncond; ++i) {
167 optype = np[i].optype;
168 if (np[i].lft.val_cat == NSC_NOCAT || np[i].rgt.val_cat == NSC_NOCAT)
171 nstr_exec_val(&lft, player->cnum, ptr, optype);
173 nstr_exec_val(&rgt, player->cnum, ptr, optype);
177 if (!EVAL(op, lft.val_as.lng, rgt.val_as.lng))
181 if (!EVAL(op, lft.val_as.dbl, rgt.val_as.dbl))
185 cmp = strnncmp(lft.val_as.str.base, lft.val_as.str.maxsz,
186 rgt.val_as.str.base, rgt.val_as.str.maxsz);
187 if (!EVAL(op, cmp, 0))
191 CANT_HAPPEN("bad OPTYPE");
200 * Compile a value in STR into VAL.
201 * Return a pointer to the first character after the value on success,
203 * TYPE is the context type, a file type.
204 * If STR names an array, VAL simply refers to the element with index
208 nstr_comp_val(char *str, struct valstr*val, int type)
218 val->val_type = NSC_NOTYPE;
219 val->val_cat = NSC_NOCAT;
220 val->val_as_type = -1;
222 if (isalpha(str[0])) {
224 for (i = 0; isalnum(str[i]) || str[i] == '_'; ++i) {
225 if (i < sizeof(id) - 1)
229 if (i < sizeof(id)) {
232 val->val_as_type = typematch(id, type);
234 cap = ef_cadef(type);
236 j = stmtch(id, cap, offsetof(struct castr, ca_name),
237 sizeof(struct castr));
239 && (!(cap[j].ca_flags & NSC_DEITY) || player->god)) {
240 if (cap[j].ca_type == NSC_TYPEID && val->val_as_type >= 0)
242 * Got two matches of type NSC_TYPEID, need to
243 * choose. Prefer typematch(), because ?des=n
244 * would be interpreted as ?des=newdes
249 val->val_type = cap[j].ca_type;
250 val->val_cat = NSC_OFF;
251 val->val_as.sym.off = cap[j].ca_off;
252 val->val_as.sym.len = cap[j].ca_len;
253 val->val_as.sym.idx = 0;
261 if (val->val_type == NSC_NOTYPE) {
262 if (val->val_as_type >= 0) {
263 val->val_type = NSC_TYPEID;
264 val->val_cat = NSC_VAL;
265 val->val_as.lng = val->val_as_type;
267 pr("%s -- selector access denied\n", id);
268 else if (j == M_NOTUNIQUE)
269 pr("%s -- ambiguous selector name\n", id);
271 pr("%s -- unknown selector name\n", id);
274 return val->val_type == NSC_NOTYPE ? NULL : tail;
277 /* single character type */
280 val->val_as_type = typematch(id, type);
283 l = strtol(str, &tail, 0);
284 d = strtod(str, &tail2);
286 val->val_type = NSC_DOUBLE;
287 val->val_cat = NSC_VAL;
292 val->val_type = NSC_LONG;
293 val->val_cat = NSC_VAL;
297 /* FIXME implement NSC_STRING literals */
299 CANT_HAPPEN(val->val_type != NSC_NOTYPE);
300 if (val->val_as_type >= 0) {
301 val->val_type = NSC_TYPEID;
302 val->val_cat = NSC_VAL;
303 val->val_as.lng = val->val_as_type;
307 pr("%s -- invalid value for condition\n", str);
313 * If VALTYPE is an integer type, return NSC_LONG.
314 * If VALTYPE is a floating-point type, return NSC_DOUBLE.
315 * If VALTYPE is NSC_STRINGY, return NSC_STRING.
316 * If VALTYPE is NSC_NOTYPE, NSC_STRING or NSC_TYPEID, return VALTYPE.
319 nstr_promote(int valtype)
339 valtype = NSC_DOUBLE;
342 valtype = NSC_STRING;
345 CANT_HAPPEN("bad VALTYPE");
346 valtype = NSC_NOTYPE;
352 cond_type_mismatch(char *str)
355 pr("%s -- condition operand type mismatch\n", str);
360 * Coerce VAL to promoted value type TO.
361 * Return 0 on success, -1 on error.
362 * If VAL is evaluated, convert it, else only check.
363 * STR is the condition text to be used for error messages. Suppress
364 * messages if it is a null pointer.
367 nstr_coerce_val(struct valstr *val, nsc_type to, char *str)
369 /* FIXME get rid of promotion? */
370 nsc_type from = nstr_promote(val->val_type);
372 if (from == NSC_NOTYPE)
378 if (val->val_as_type >= 0) {
379 val->val_cat = NSC_VAL;
380 val->val_as.lng = val->val_as_type;
382 return cond_type_mismatch(str);
385 return cond_type_mismatch(str); /* FIXME implement */
387 if (from == NSC_LONG) {
388 if (val->val_cat == NSC_VAL)
389 val->val_as.dbl = val->val_as.lng;
391 return cond_type_mismatch(str);
394 return cond_type_mismatch(str);
396 CANT_HAPPEN("bad TO argument");
401 if (val->val_cat == NSC_VAL) {
402 /* coord literals don't occur, conversion not implemented */
403 CANT_HAPPEN(val->val_type == NSC_XCOORD
404 || val->val_type == NSC_YCOORD);
413 * If VAL is symbolic, evaluate it into a promoted value type.
414 * Use coordinate system of country CNUM.
415 * PTR points to a context object of the type that was used to compile
417 * Unless WANT is NSC_NOTYPE, coerce the value to promoted value type
418 * WANT. VAL must be coercible. That's the case if a previous
419 * nstr_coerce_val(VAL, WANT, STR) succeeded.
422 nstr_exec_val(struct valstr *val, natid cnum, void *ptr, nsc_type want)
428 switch (val->val_cat) {
430 CANT_HAPPEN("Bad VAL category");
433 valtype = val->val_type;
438 memb_ptr += val->val_as.sym.off;
439 idx = val->val_as.sym.idx;
440 switch (val->val_type) {
442 val->val_as.lng = ((signed char *)memb_ptr)[idx];
445 val->val_as.lng = ((unsigned char *)memb_ptr)[idx];
448 val->val_as.lng = ((short *)memb_ptr)[idx];
451 val->val_as.lng = ((unsigned short *)memb_ptr)[idx];
454 val->val_as.lng = ((int *)memb_ptr)[idx];
457 val->val_as.lng = ((long *)memb_ptr)[idx];
460 val->val_as.lng = xrel(getnatp(cnum), ((short *)memb_ptr)[idx]);
463 val->val_as.lng = yrel(getnatp(cnum), ((short *)memb_ptr)[idx]);
466 val->val_as.dbl = ((float *)memb_ptr)[idx];
467 valtype = NSC_DOUBLE;
470 val->val_as.dbl = ((double *)memb_ptr)[idx];
471 valtype = NSC_DOUBLE;
475 val->val_as.str.maxsz = val->val_as.sym.len;
476 val->val_as.str.base = (char *)memb_ptr;
477 valtype = NSC_STRING;
480 val->val_as.str.base = ((char **)memb_ptr)[idx];
481 val->val_as.str.maxsz = INT_MAX;
482 valtype = NSC_STRING;
485 val->val_as.lng = ((time_t *)memb_ptr)[idx];
488 val->val_as.lng = ((signed char *)memb_ptr)[idx];
489 valtype = NSC_TYPEID;
492 CANT_HAPPEN("Bad VAL type");
495 val->val_cat = NSC_VAL;
500 else if (want == NSC_DOUBLE) {
501 if (valtype == NSC_LONG) {
503 val->val_as.dbl = val->val_as.lng;
505 } else if (want == NSC_STRING)
506 CANT_HAPPEN("unimplemented WANT"); /* FIXME */
508 if (CANT_HAPPEN(valtype != want && want != NSC_NOTYPE)) {
512 case NSC_LONG: val->val_as.lng = 0; break;
513 case NSC_DOUBLE: val->val_as.dbl = 0.0; break;
514 case NSC_STRING: val->val_as.str.base = NULL; break;
516 CANT_HAPPEN("bad WANT argument");
520 val->val_type = valtype;