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 * nstr.c: compile and execute the item selections on sectors
30 * Known contributors to this file:
33 * Markus Armbruster, 2004-2005
43 #include "prototypes.h"
45 static char *nstr_parse_val(char *, struct valstr *);
46 static int nstr_match_ca(struct valstr *, struct castr *);
47 static int nstr_match_val(struct valstr *, int, struct castr *, int);
48 static int nstr_string_ok(struct castr *ca, int idx);
49 static struct valstr *nstr_resolve_sel(struct valstr *, struct castr *);
50 static struct valstr *nstr_mkselval(struct valstr *, int, struct castr *);
51 static struct valstr *nstr_resolve_id(struct valstr *, struct castr *, int, int);
52 static int nstr_promote(int);
56 * Compile conditions into array NP[LEN].
57 * Return number of conditions, or -1 on error.
58 * It is an error if there are more than LEN conditions.
59 * TYPE is the context type, a file type.
60 * STR is the condition string, in Empire syntax, without the leading
64 nstr_comp(struct nscstr *np, int len, int type, char *str)
66 struct castr *ca = ef_cadef(type);
71 int lft_caidx, rgt_caidx;
73 int lft_type, rgt_type;
76 for (i = 0; ; ++i, ++np) {
81 tail = nstr_parse_val(cond, &np->lft);
82 lft_caidx = nstr_match_ca(&np->lft, ca);
85 if (*tail != '<' && *tail != '=' && *tail != '>' && *tail != '#') {
87 pr("%s -- expected condition operator\n", cond);
89 pr("%s -- missing condition operator\n", cond);
96 tail = nstr_parse_val(tail, &np->rgt);
97 rgt_caidx = nstr_match_ca(&np->rgt, ca);
100 * Resolve identifiers
102 * An identifier can name a selector or, if the other operand
103 * is a selector, a value for that. The condition is
104 * ambiguous if both selector x value and value x selector are
105 * possible. Example: n<n for sectors could mean newdes<n or
108 lft_val = nstr_match_val(&np->lft, type, ca, rgt_caidx);
109 rgt_val = nstr_match_val(&np->rgt, type, ca, lft_caidx);
111 * if lft_val >= 0, then rhs names a selector and lhs names
112 * one of its values. Likewise for rgt_val.
114 if (lft_val >= 0 && rgt_val >= 0) {
115 pr("%.*s -- condition ambiguous\n", (int)(tail-cond), cond);
117 } else if (rgt_val >= 0) {
118 /* selector x value */
119 if (!nstr_resolve_sel(&np->lft, &ca[lft_caidx]))
121 nstr_mkselval(&np->rgt, rgt_val, &ca[lft_caidx]);
122 } else if (lft_val >= 0) {
123 /* value x selector */
124 nstr_mkselval(&np->lft, lft_val, &ca[rgt_caidx]);
125 if (!nstr_resolve_sel(&np->rgt, &ca[rgt_caidx]))
129 * Neither side works as selector value; any identifiers
130 * must name selectors.
132 if (!nstr_resolve_id(&np->lft, ca, lft_caidx,
133 nstr_string_ok(ca, rgt_caidx)))
135 if (!nstr_resolve_id(&np->rgt, ca, rgt_caidx,
136 nstr_string_ok(ca, lft_caidx)))
140 /* find operator type, coerce operands */
141 lft_type = nstr_promote(np->lft.val_type);
142 rgt_type = nstr_promote(np->rgt.val_type);
143 np->optype = NSC_NOTYPE;
144 if (lft_type == NSC_TYPEID) {
145 if (!nstr_coerce_val(&np->rgt, NSC_TYPEID, str))
146 np->optype = NSC_TYPEID;
147 } else if (rgt_type == NSC_TYPEID) {
148 if (!nstr_coerce_val(&np->lft, NSC_TYPEID, str))
149 np->optype = NSC_TYPEID;
150 } else if (lft_type == NSC_STRING) {
151 if (!nstr_coerce_val(&np->rgt, NSC_STRING, str))
152 np->optype = NSC_STRING;
153 } else if (rgt_type == NSC_STRING) {
154 if (!nstr_coerce_val(&np->lft, NSC_STRING, str))
155 np->optype = NSC_STRING;
156 } else if (lft_type == NSC_DOUBLE) {
157 if (!nstr_coerce_val(&np->rgt, NSC_DOUBLE, str))
158 np->optype = NSC_DOUBLE;
159 } else if (rgt_type == NSC_DOUBLE) {
160 if (!nstr_coerce_val(&np->lft, NSC_DOUBLE, str))
161 np->optype = NSC_DOUBLE;
163 if (!nstr_coerce_val(&np->lft, NSC_LONG, str)
164 && !nstr_coerce_val(&np->rgt, NSC_LONG, str))
165 np->optype = NSC_LONG;
167 if (np->optype == NSC_NOTYPE)
170 /* another condition? */
174 pr("%s -- expected `&'\n", cond);
181 /* could just return I and let caller gripe or enlarge buffer */
182 pr("%s -- too many conditions\n", str);
189 /* Like strcmp(S1, S2), but limit length of S1 to SZ1 and of S2 to SZ2. */
191 strnncmp(char *s1, size_t sz1, char *s2, size_t sz2)
194 if (sz1 == sz2) return strncmp(s1, s2, sz2);
195 if (sz1 < sz2) return -strnncmp(s2, sz2, s1, sz1);
196 res = strncmp(s1, s2, sz2);
197 return res ? res : s1[sz2];
200 #define EVAL(op, lft, rgt) \
201 ((op) == '<' ? (lft) < (rgt) \
202 : (op) == '=' ? (lft) == (rgt) \
203 : (op) == '>' ? (lft) > (rgt) \
204 : (op) == '#' ? (lft) != (rgt) \
208 * Evaluate compiled conditions in array NP[NCOND].
209 * Return non-zero iff they are all true.
210 * PTR points to a context object of the type that was used to compile
214 nstr_exec(struct nscstr *np, int ncond, void *ptr)
216 int i, op, optype, cmp;
217 struct valstr lft, rgt;
219 for (i = 0; i < ncond; ++i) {
221 optype = np[i].optype;
222 if (np[i].lft.val_cat == NSC_NOCAT || np[i].rgt.val_cat == NSC_NOCAT)
225 nstr_exec_val(&lft, player->cnum, ptr, optype);
227 nstr_exec_val(&rgt, player->cnum, ptr, optype);
231 if (!EVAL(op, lft.val_as.lng, rgt.val_as.lng))
235 if (!EVAL(op, lft.val_as.dbl, rgt.val_as.dbl))
239 cmp = strnncmp(lft.val_as.str.base, lft.val_as.str.maxsz,
240 rgt.val_as.str.base, rgt.val_as.str.maxsz);
241 if (!EVAL(op, cmp, 0))
254 * Parse a value in STR into VAL.
255 * Return a pointer to the first character after the value.
256 * Value is either evaluated (but not NSC_TYPEID) or an identifier.
259 nstr_parse_val(char *str, struct valstr *val)
266 if (str[0] == '\'') {
267 for (tail = str + 1; *tail && *tail != '\''; ++tail) ;
268 /* FIXME implement \ quoting */
269 val->val_type = NSC_STRING;
270 val->val_cat = NSC_VAL;
271 val->val_as.str.base = str + 1;
272 val->val_as.str.maxsz = tail - (str + 1);
278 if (isalpha(str[0])) {
279 for (tail = str+1; isalnum(*tail) || *tail == '_'; ++tail) ;
280 val->val_type = NSC_NOTYPE;
281 val->val_cat = NSC_ID;
282 val->val_as.str.base = str;
283 val->val_as.str.maxsz = tail - str;
288 l = strtol(str, &tail, 0);
289 d = strtod(str, &tail2);
291 val->val_type = NSC_DOUBLE;
292 val->val_cat = NSC_VAL;
297 val->val_type = NSC_LONG;
298 val->val_cat = NSC_VAL;
303 /* funny character, interpret as identifier */
305 val->val_type = NSC_NOTYPE;
306 val->val_cat = NSC_ID;
307 val->val_as.str.base = str;
308 val->val_as.str.maxsz = tail - str;
313 * Match VAL in table of selector descriptors CA, return index.
314 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
316 * A VAL that is not an identifier doesn't match anything. A null CA
317 * is considered empty.
320 nstr_match_ca(struct valstr *val, struct castr *ca)
324 if (val->val_cat != NSC_ID || val->val_as.str.maxsz >= sizeof(id))
330 memcpy(id, val->val_as.str.base, val->val_as.str.maxsz);
331 id[val->val_as.str.maxsz] = 0;
333 return stmtch(id, ca, offsetof(struct castr, ca_name),
334 sizeof(struct castr));
338 * Match VAL in a selector's values, return its (non-negative) value.
339 * TYPE is the context type, a file type.
340 * CA is ef_cadef(TYPE). If it is null, then IDX must be negative.
341 * Match values of selector descriptor CA[IDX], provided IDX is not
343 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
345 * TODO: This is just a stub and works only for NSC_TYPEID.
346 * Generalize: give struct castr enough info to find values, remove
350 nstr_match_val(struct valstr *val, int type, struct castr *ca, int idx)
354 if (val->val_cat != NSC_ID || val->val_as.str.maxsz >= sizeof(id))
357 if (idx < 0 || ca[idx].ca_type != NSC_TYPEID)
360 memcpy(id, val->val_as.str.base, val->val_as.str.maxsz);
361 id[val->val_as.str.maxsz] = 0;
363 return typematch(id, type);
367 * Can CA[IDX] be compared to a string?
368 * Return 0 for negative IDX.
371 nstr_string_ok(struct castr *ca, int idx)
373 return idx >= 0 && nstr_promote(ca[idx].ca_type) == NSC_STRING;
377 * Change VAL to resolve identifier to selector or string.
378 * Return VAL on success, NULL on error.
379 * No change if VAL is not an identifier. Otherwise, change it as
381 * Error if IDX == M_NOTUNIQUE or IDX == M_NOTFOUND and !STRING_OK.
382 * Change into string if IDX == M_NOTFOUND and STRING_OK.
383 * Change into selector CA[IDX] if IDX >= 0.
385 static struct valstr *
386 nstr_resolve_id(struct valstr *val, struct castr *ca, int idx, int string_ok)
388 if (val->val_cat != NSC_ID)
391 if (idx == M_NOTUNIQUE) {
392 pr("%.*s -- ambiguous name\n",
393 (int)val->val_as.str.maxsz, val->val_as.str.base);
394 val->val_cat = NSC_NOCAT;
399 CANT_HAPPEN(idx != M_NOTFOUND);
401 pr("%.*s -- unknown name\n",
402 (int)val->val_as.str.maxsz, val->val_as.str.base);
403 val->val_cat = NSC_NOCAT;
406 /* interpret unbound identifier as string */
407 val->val_type = NSC_STRING;
408 val->val_cat = NSC_VAL;
412 return nstr_resolve_sel(val, &ca[idx]);
416 * Change VAL to resolve identifier to selector CA.
417 * Return VAL on success, NULL if the player is denied access to the
419 * VAL must be an identifier.
421 static struct valstr *
422 nstr_resolve_sel(struct valstr *val, struct castr *ca)
424 if (CANT_HAPPEN(val->val_cat != NSC_ID)) {
425 val->val_cat = NSC_NOCAT;
429 if ((ca->ca_flags & NSC_DEITY) && !player->god) {
430 pr("%.*s -- not accessible to mortals\n",
431 (int)val->val_as.str.maxsz, val->val_as.str.base);
432 val->val_cat = NSC_NOCAT;
436 val->val_type = ca->ca_type;
437 val->val_cat = NSC_OFF;
438 val->val_as.sym.off = ca->ca_off;
439 val->val_as.sym.len = ca->ca_len;
440 val->val_as.sym.idx = 0;
445 * Initialize VAL to value SELVAL for selector CA, return VAL.
447 static struct valstr *
448 nstr_mkselval(struct valstr *val, int selval, struct castr *ca)
450 if (CANT_HAPPEN(ca->ca_type != NSC_TYPEID)) {
451 val->val_type = NSC_NOTYPE;
452 val->val_cat = NSC_NOCAT;
456 val->val_type = ca->ca_type;
457 val->val_cat = NSC_VAL;
458 val->val_as.lng = selval;
463 * Compile a value in STR into VAL.
464 * Return a pointer to the first character after the value on success,
466 * TYPE is the context type, a file type.
467 * If STR names an array, VAL simply refers to the element with index
471 nstr_comp_val(char *str, struct valstr *val, int type)
473 struct castr *ca = ef_cadef(type);
474 char *tail = nstr_parse_val(str, val);
475 return nstr_resolve_id(val, ca, nstr_match_ca(val, ca), 0) ? tail : NULL;
481 * If VALTYPE is an integer type, return NSC_LONG.
482 * If VALTYPE is a floating-point type, return NSC_DOUBLE.
483 * If VALTYPE is NSC_STRINGY, return NSC_STRING.
484 * If VALTYPE is NSC_NOTYPE, NSC_STRING or NSC_TYPEID, return VALTYPE.
487 nstr_promote(int valtype)
507 valtype = NSC_DOUBLE;
510 valtype = NSC_STRING;
514 valtype = NSC_NOTYPE;
520 cond_type_mismatch(char *str)
523 pr("%s -- condition operand type mismatch\n", str);
528 * Coerce VAL to promoted value type TO.
529 * Return 0 on success, -1 on error.
530 * If VAL is evaluated, convert it, else only check.
531 * STR is the condition text to be used for error messages. Suppress
532 * messages if it is a null pointer.
535 nstr_coerce_val(struct valstr *val, nsc_type to, char *str)
537 /* FIXME get rid of promotion? */
538 nsc_type from = nstr_promote(val->val_type);
540 if (from == NSC_NOTYPE)
546 return cond_type_mismatch(str);
548 return cond_type_mismatch(str); /* FIXME implement */
550 if (from == NSC_LONG) {
551 if (val->val_cat == NSC_VAL)
552 val->val_as.dbl = val->val_as.lng;
554 return cond_type_mismatch(str);
557 return cond_type_mismatch(str);
564 if (val->val_cat == NSC_VAL) {
565 /* coord literals don't occur, conversion not implemented */
566 CANT_HAPPEN(val->val_type == NSC_XCOORD
567 || val->val_type == NSC_YCOORD);
576 * If VAL is symbolic, evaluate it into a promoted value type.
577 * Use coordinate system of country CNUM.
578 * PTR points to a context object of the type that was used to compile
580 * Unless WANT is NSC_NOTYPE, coerce the value to promoted value type
581 * WANT. VAL must be coercible. That's the case if a previous
582 * nstr_coerce_val(VAL, WANT, STR) succeeded.
585 nstr_exec_val(struct valstr *val, natid cnum, void *ptr, nsc_type want)
591 switch (val->val_cat) {
596 valtype = val->val_type;
601 memb_ptr += val->val_as.sym.off;
602 idx = val->val_as.sym.idx;
603 switch (val->val_type) {
605 val->val_as.lng = ((signed char *)memb_ptr)[idx];
608 val->val_as.lng = ((unsigned char *)memb_ptr)[idx];
611 val->val_as.lng = ((short *)memb_ptr)[idx];
614 val->val_as.lng = ((unsigned short *)memb_ptr)[idx];
617 val->val_as.lng = ((int *)memb_ptr)[idx];
620 val->val_as.lng = ((long *)memb_ptr)[idx];
623 val->val_as.lng = xrel(getnatp(cnum), ((short *)memb_ptr)[idx]);
626 val->val_as.lng = yrel(getnatp(cnum), ((short *)memb_ptr)[idx]);
629 val->val_as.dbl = ((float *)memb_ptr)[idx];
630 valtype = NSC_DOUBLE;
633 val->val_as.dbl = ((double *)memb_ptr)[idx];
634 valtype = NSC_DOUBLE;
638 val->val_as.str.maxsz = val->val_as.sym.len;
639 val->val_as.str.base = (char *)memb_ptr;
640 valtype = NSC_STRING;
643 val->val_as.str.base = ((char **)memb_ptr)[idx];
644 val->val_as.str.maxsz = INT_MAX;
645 valtype = NSC_STRING;
648 val->val_as.lng = ((time_t *)memb_ptr)[idx];
651 val->val_as.lng = ((signed char *)memb_ptr)[idx];
652 valtype = NSC_TYPEID;
658 val->val_cat = NSC_VAL;
663 else if (want == NSC_DOUBLE) {
664 if (valtype == NSC_LONG) {
666 val->val_as.dbl = val->val_as.lng;
668 } else if (want == NSC_STRING)
669 CANT_REACH(); /* FIXME implement */
671 if (CANT_HAPPEN(valtype != want && want != NSC_NOTYPE)) {
675 case NSC_LONG: val->val_as.lng = 0; break;
676 case NSC_DOUBLE: val->val_as.dbl = 0.0; break;
677 case NSC_STRING: val->val_as.str.base = NULL; break;
683 val->val_type = valtype;
687 symbol_by_value(int key, struct symbol *table)
691 for (i = 0; table[i].name; i++)
692 if (key == table[i].value)
693 return table[i].name;