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
44 #include "prototypes.h"
46 static char *nstr_parse_val(char *, struct valstr *);
47 static int nstr_match_ca(struct valstr *, struct castr *);
48 static int nstr_match_val(struct valstr *, int, struct castr *, int);
49 static int nstr_string_ok(struct castr *ca, int idx);
50 static struct valstr *nstr_resolve_sel(struct valstr *, struct castr *);
51 static struct valstr *nstr_mkselval(struct valstr *, int, struct castr *);
52 static struct valstr *nstr_resolve_id(struct valstr *, struct castr *, int, int);
53 static int nstr_promote(int);
57 * Compile conditions into array NP[LEN].
58 * Return number of conditions, or -1 on error.
59 * It is an error if there are more than LEN conditions.
60 * TYPE is the context type, a file type.
61 * STR is the condition string, in Empire syntax, without the leading
65 nstr_comp(struct nscstr *np, int len, int type, char *str)
67 struct castr *ca = ef_cadef(type);
72 int lft_caidx, rgt_caidx;
74 int lft_type, rgt_type;
77 for (i = 0; ; ++i, ++np) {
82 tail = nstr_parse_val(cond, &np->lft);
83 lft_caidx = nstr_match_ca(&np->lft, ca);
86 if (*tail != '<' && *tail != '=' && *tail != '>' && *tail != '#') {
88 pr("%s -- expected condition operator\n", cond);
90 pr("%s -- missing condition operator\n", cond);
97 tail = nstr_parse_val(tail, &np->rgt);
98 rgt_caidx = nstr_match_ca(&np->rgt, ca);
101 * Resolve identifiers
103 * An identifier can name a selector or, if the other operand
104 * is a selector, a value for that. The condition is
105 * ambiguous if both selector x value and value x selector are
106 * possible. Example: n<n for sectors could mean newdes<n or
109 lft_val = nstr_match_val(&np->lft, type, ca, rgt_caidx);
110 rgt_val = nstr_match_val(&np->rgt, type, ca, lft_caidx);
112 * if lft_val >= 0, then rhs names a selector and lhs names
113 * one of its values. Likewise for rgt_val.
115 if (lft_val >= 0 && rgt_val >= 0) {
116 pr("%.*s -- condition ambiguous\n", (int)(tail-cond), cond);
118 } else if (rgt_val >= 0) {
119 /* selector x value */
120 if (!nstr_resolve_sel(&np->lft, &ca[lft_caidx]))
122 nstr_mkselval(&np->rgt, rgt_val, &ca[lft_caidx]);
123 } else if (lft_val >= 0) {
124 /* value x selector */
125 nstr_mkselval(&np->lft, lft_val, &ca[rgt_caidx]);
126 if (!nstr_resolve_sel(&np->rgt, &ca[rgt_caidx]))
130 * Neither side works as selector value; any identifiers
131 * must name selectors.
133 if (!nstr_resolve_id(&np->lft, ca, lft_caidx,
134 nstr_string_ok(ca, rgt_caidx)))
136 if (!nstr_resolve_id(&np->rgt, ca, rgt_caidx,
137 nstr_string_ok(ca, lft_caidx)))
141 /* find operator type, coerce operands */
142 lft_type = nstr_promote(np->lft.val_type);
143 rgt_type = nstr_promote(np->rgt.val_type);
144 np->optype = NSC_NOTYPE;
145 if (lft_type == NSC_TYPEID) {
146 if (!nstr_coerce_val(&np->rgt, NSC_TYPEID, str))
147 np->optype = NSC_TYPEID;
148 } else if (rgt_type == NSC_TYPEID) {
149 if (!nstr_coerce_val(&np->lft, NSC_TYPEID, str))
150 np->optype = NSC_TYPEID;
151 } else if (lft_type == NSC_STRING) {
152 if (!nstr_coerce_val(&np->rgt, NSC_STRING, str))
153 np->optype = NSC_STRING;
154 } else if (rgt_type == NSC_STRING) {
155 if (!nstr_coerce_val(&np->lft, NSC_STRING, str))
156 np->optype = NSC_STRING;
157 } else if (lft_type == NSC_DOUBLE) {
158 if (!nstr_coerce_val(&np->rgt, NSC_DOUBLE, str))
159 np->optype = NSC_DOUBLE;
160 } else if (rgt_type == NSC_DOUBLE) {
161 if (!nstr_coerce_val(&np->lft, NSC_DOUBLE, str))
162 np->optype = NSC_DOUBLE;
164 if (!nstr_coerce_val(&np->lft, NSC_LONG, str)
165 && !nstr_coerce_val(&np->rgt, NSC_LONG, str))
166 np->optype = NSC_LONG;
168 if (np->optype == NSC_NOTYPE)
171 /* another condition? */
175 pr("%s -- expected `&'\n", cond);
182 /* could just return I and let caller gripe or enlarge buffer */
183 pr("%s -- too many conditions\n", str);
190 /* Like strcmp(S1, S2), but limit length of S1 to SZ1 and of S2 to SZ2. */
192 strnncmp(char *s1, size_t sz1, char *s2, size_t sz2)
195 if (sz1 == sz2) return strncmp(s1, s2, sz2);
196 if (sz1 < sz2) return -strnncmp(s2, sz2, s1, sz1);
197 res = strncmp(s1, s2, sz2);
198 return res ? res : s1[sz2];
201 #define EVAL(op, lft, rgt) \
202 ((op) == '<' ? (lft) < (rgt) \
203 : (op) == '=' ? (lft) == (rgt) \
204 : (op) == '>' ? (lft) > (rgt) \
205 : (op) == '#' ? (lft) != (rgt) \
209 * Evaluate compiled conditions in array NP[NCOND].
210 * Return non-zero iff they are all true.
211 * PTR points to a context object of the type that was used to compile
215 nstr_exec(struct nscstr *np, int ncond, void *ptr)
217 int i, op, optype, cmp;
218 struct valstr lft, rgt;
220 for (i = 0; i < ncond; ++i) {
222 optype = np[i].optype;
223 if (np[i].lft.val_cat == NSC_NOCAT || np[i].rgt.val_cat == NSC_NOCAT)
226 nstr_exec_val(&lft, player->cnum, ptr, optype);
228 nstr_exec_val(&rgt, player->cnum, ptr, optype);
232 if (!EVAL(op, lft.val_as.lng, rgt.val_as.lng))
236 if (!EVAL(op, lft.val_as.dbl, rgt.val_as.dbl))
240 cmp = strnncmp(lft.val_as.str.base, lft.val_as.str.maxsz,
241 rgt.val_as.str.base, rgt.val_as.str.maxsz);
242 if (!EVAL(op, cmp, 0))
255 * Parse a value in STR into VAL.
256 * Return a pointer to the first character after the value.
257 * Value is either evaluated (but not NSC_TYPEID) or an identifier.
260 nstr_parse_val(char *str, struct valstr *val)
267 if (str[0] == '\'') {
268 for (tail = str + 1; *tail && *tail != '\''; ++tail) ;
269 /* FIXME implement \ quoting */
270 val->val_type = NSC_STRING;
271 val->val_cat = NSC_VAL;
272 val->val_as.str.base = str + 1;
273 val->val_as.str.maxsz = tail - (str + 1);
279 if (isalpha(str[0])) {
280 for (tail = str+1; isalnum(*tail) || *tail == '_'; ++tail) ;
281 val->val_type = NSC_NOTYPE;
282 val->val_cat = NSC_ID;
283 val->val_as.str.base = str;
284 val->val_as.str.maxsz = tail - str;
289 l = strtol(str, &tail, 0);
290 d = strtod(str, &tail2);
292 val->val_type = NSC_DOUBLE;
293 val->val_cat = NSC_VAL;
298 val->val_type = NSC_LONG;
299 val->val_cat = NSC_VAL;
304 /* funny character, interpret as identifier */
306 val->val_type = NSC_NOTYPE;
307 val->val_cat = NSC_ID;
308 val->val_as.str.base = str;
309 val->val_as.str.maxsz = tail - str;
314 * Match VAL in table of selector descriptors CA, return index.
315 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
317 * A VAL that is not an identifier doesn't match anything. A null CA
318 * is considered empty.
321 nstr_match_ca(struct valstr *val, struct castr *ca)
325 if (val->val_cat != NSC_ID || val->val_as.str.maxsz >= sizeof(id))
331 memcpy(id, val->val_as.str.base, val->val_as.str.maxsz);
332 id[val->val_as.str.maxsz] = 0;
334 return stmtch(id, ca, offsetof(struct castr, ca_name),
335 sizeof(struct castr));
339 * Match VAL in a selector's values, return its (non-negative) value.
340 * TYPE is the context type, a file type.
341 * CA is ef_cadef(TYPE). If it is null, then IDX must be negative.
342 * Match values of selector descriptor CA[IDX], provided IDX is not
344 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
346 * TODO: This is just a stub and works only for NSC_TYPEID.
347 * Generalize: give struct castr enough info to find values, remove
351 nstr_match_val(struct valstr *val, int type, struct castr *ca, int idx)
355 if (val->val_cat != NSC_ID || val->val_as.str.maxsz >= sizeof(id))
358 if (idx < 0 || ca[idx].ca_type != NSC_TYPEID)
361 memcpy(id, val->val_as.str.base, val->val_as.str.maxsz);
362 id[val->val_as.str.maxsz] = 0;
364 return typematch(id, type);
368 * Can CA[IDX] be compared to a string?
369 * Return 0 for negative IDX.
372 nstr_string_ok(struct castr *ca, int idx)
374 return idx >= 0 && nstr_promote(ca[idx].ca_type) == NSC_STRING;
378 * Change VAL to resolve identifier to selector or string.
379 * Return VAL on success, NULL on error.
380 * No change if VAL is not an identifier. Otherwise, change it as
382 * Error if IDX == M_NOTUNIQUE or IDX == M_NOTFOUND and !STRING_OK.
383 * Change into string if IDX == M_NOTFOUND and STRING_OK.
384 * Change into selector CA[IDX] if IDX >= 0.
386 static struct valstr *
387 nstr_resolve_id(struct valstr *val, struct castr *ca, int idx, int string_ok)
389 if (val->val_cat != NSC_ID)
392 if (idx == M_NOTUNIQUE && !string_ok) {
393 pr("%.*s -- ambiguous name\n",
394 (int)val->val_as.str.maxsz, val->val_as.str.base);
395 val->val_cat = NSC_NOCAT;
399 if (idx == M_NOTFOUND && !string_ok) {
400 pr("%.*s -- unknown name\n",
401 (int)val->val_as.str.maxsz, val->val_as.str.base);
402 val->val_cat = NSC_NOCAT;
407 CANT_HAPPEN(!string_ok);
408 /* interpret unresolvable identifier as string */
409 val->val_type = NSC_STRING;
410 val->val_cat = NSC_VAL;
411 /* map identifier ~ to empty string, like some commands do */
412 if (val->val_as.str.maxsz == 1 && val->val_as.str.base[0] == '~')
413 val->val_as.str.maxsz = 0;
417 return nstr_resolve_sel(val, &ca[idx]);
421 * Change VAL to resolve identifier to selector CA.
422 * Return VAL on success, NULL if the player is denied access to the
424 * VAL must be an identifier.
426 static struct valstr *
427 nstr_resolve_sel(struct valstr *val, struct castr *ca)
429 if (CANT_HAPPEN(val->val_cat != NSC_ID)) {
430 val->val_cat = NSC_NOCAT;
434 if ((ca->ca_flags & NSC_DEITY) && !player->god) {
435 pr("%.*s -- not accessible to mortals\n",
436 (int)val->val_as.str.maxsz, val->val_as.str.base);
437 val->val_cat = NSC_NOCAT;
441 val->val_type = ca->ca_type;
442 val->val_cat = NSC_OFF;
443 val->val_as.sym.off = ca->ca_off;
444 val->val_as.sym.len = ca->ca_len;
445 val->val_as.sym.idx = 0;
450 * Initialize VAL to value SELVAL for selector CA, return VAL.
452 static struct valstr *
453 nstr_mkselval(struct valstr *val, int selval, struct castr *ca)
455 if (CANT_HAPPEN(ca->ca_type != NSC_TYPEID)) {
456 val->val_type = NSC_NOTYPE;
457 val->val_cat = NSC_NOCAT;
461 val->val_type = ca->ca_type;
462 val->val_cat = NSC_VAL;
463 val->val_as.lng = selval;
468 * Compile a value in STR into VAL.
469 * Return a pointer to the first character after the value on success,
471 * TYPE is the context type, a file type.
472 * If STR names an array, VAL simply refers to the element with index
476 nstr_comp_val(char *str, struct valstr *val, int type)
478 struct castr *ca = ef_cadef(type);
479 char *tail = nstr_parse_val(str, val);
480 if (!nstr_resolve_id(val, ca, nstr_match_ca(val, ca), 0))
488 * If VALTYPE is an integer type, return NSC_LONG.
489 * If VALTYPE is a floating-point type, return NSC_DOUBLE.
490 * If VALTYPE is NSC_STRINGY, return NSC_STRING.
491 * If VALTYPE is NSC_NOTYPE, NSC_STRING or NSC_TYPEID, return VALTYPE.
494 nstr_promote(int valtype)
515 valtype = NSC_DOUBLE;
518 valtype = NSC_STRING;
522 valtype = NSC_NOTYPE;
528 cond_type_mismatch(char *str)
531 pr("%s -- condition operand type mismatch\n", str);
536 * Coerce VAL to promoted value type TO.
537 * Return 0 on success, -1 on error.
538 * If VAL is evaluated, convert it, else only check.
539 * STR is the condition text to be used for error messages. Suppress
540 * messages if it is a null pointer.
543 nstr_coerce_val(struct valstr *val, nsc_type to, char *str)
545 /* FIXME get rid of promotion? */
546 nsc_type from = nstr_promote(val->val_type);
548 if (from == NSC_NOTYPE)
554 return cond_type_mismatch(str);
556 return cond_type_mismatch(str); /* FIXME implement */
558 if (from == NSC_LONG) {
559 if (val->val_cat == NSC_VAL)
560 val->val_as.dbl = val->val_as.lng;
562 return cond_type_mismatch(str);
565 return cond_type_mismatch(str);
572 if (val->val_cat == NSC_VAL) {
573 /* unimplemented conversions; don't currently occur here */
574 CANT_HAPPEN(val->val_type == NSC_XCOORD
575 || val->val_type == NSC_YCOORD
576 || val->val_type == NSC_HIDDEN);
585 * If VAL is symbolic, evaluate it into a promoted value type.
586 * Use coordinate system of country CNUM.
587 * PTR points to a context object of the type that was used to compile
589 * Unless WANT is NSC_NOTYPE, coerce the value to promoted value type
590 * WANT. VAL must be coercible. That's the case if a previous
591 * nstr_coerce_val(VAL, WANT, STR) succeeded.
594 nstr_exec_val(struct valstr *val, natid cnum, void *ptr, nsc_type want)
601 switch (val->val_cat) {
606 valtype = val->val_type;
611 memb_ptr += val->val_as.sym.off;
612 idx = val->val_as.sym.idx;
613 switch (val->val_type) {
615 val->val_as.lng = ((signed char *)memb_ptr)[idx];
618 val->val_as.lng = ((unsigned char *)memb_ptr)[idx];
621 val->val_as.lng = ((short *)memb_ptr)[idx];
624 val->val_as.lng = ((unsigned short *)memb_ptr)[idx];
627 val->val_as.lng = ((int *)memb_ptr)[idx];
630 val->val_as.lng = ((long *)memb_ptr)[idx];
633 val->val_as.lng = xrel(getnatp(cnum), ((short *)memb_ptr)[idx]);
636 val->val_as.lng = yrel(getnatp(cnum), ((short *)memb_ptr)[idx]);
639 val->val_as.lng = -1;
640 if (CANT_HAPPEN(((struct natstr *)ptr)->ef_type != EF_NATION))
642 natp = getnatp(cnum);
644 || natp->nat_stat == STAT_GOD
645 || (getcontact(natp, idx) && getcontact(ptr, idx)))
646 val->val_as.lng = ((unsigned char *)memb_ptr)[idx];
649 val->val_as.dbl = ((float *)memb_ptr)[idx];
650 valtype = NSC_DOUBLE;
653 val->val_as.dbl = ((double *)memb_ptr)[idx];
654 valtype = NSC_DOUBLE;
658 val->val_as.str.maxsz = val->val_as.sym.len;
659 val->val_as.str.base = (char *)memb_ptr;
660 valtype = NSC_STRING;
663 val->val_as.str.base = ((char **)memb_ptr)[idx];
664 val->val_as.str.maxsz = INT_MAX;
665 valtype = NSC_STRING;
668 val->val_as.lng = ((time_t *)memb_ptr)[idx];
671 val->val_as.lng = ((signed char *)memb_ptr)[idx];
672 valtype = NSC_TYPEID;
678 val->val_cat = NSC_VAL;
683 else if (want == NSC_DOUBLE) {
684 if (valtype == NSC_LONG) {
686 val->val_as.dbl = val->val_as.lng;
688 } else if (want == NSC_STRING)
689 CANT_REACH(); /* FIXME implement */
691 if (CANT_HAPPEN(valtype != want && want != NSC_NOTYPE)) {
695 case NSC_LONG: val->val_as.lng = 0; break;
696 case NSC_DOUBLE: val->val_as.dbl = 0.0; break;
697 case NSC_STRING: val->val_as.str.base = NULL; break;
703 val->val_type = valtype;
707 symbol_by_value(int key, struct symbol *table)
711 for (i = 0; table[i].name; i++)
712 if (key == table[i].value)
713 return table[i].name;