2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, 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-2006
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 *, 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, ca, rgt_caidx);
109 rgt_val = nstr_match_val(&np->rgt, 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_STRING) {
145 if (!nstr_coerce_val(&np->rgt, NSC_STRING, str))
146 np->optype = NSC_STRING;
147 } else if (rgt_type == NSC_STRING) {
148 if (!nstr_coerce_val(&np->lft, NSC_STRING, str))
149 np->optype = NSC_STRING;
150 } else if (lft_type == NSC_DOUBLE) {
151 if (!nstr_coerce_val(&np->rgt, NSC_DOUBLE, str))
152 np->optype = NSC_DOUBLE;
153 } else if (rgt_type == NSC_DOUBLE) {
154 if (!nstr_coerce_val(&np->lft, NSC_DOUBLE, str))
155 np->optype = NSC_DOUBLE;
157 if (!nstr_coerce_val(&np->lft, NSC_LONG, str)
158 && !nstr_coerce_val(&np->rgt, NSC_LONG, str))
159 np->optype = NSC_LONG;
161 if (np->optype == NSC_NOTYPE)
164 /* another condition? */
168 pr("%s -- expected `&'\n", cond);
175 /* could just return I and let caller gripe or enlarge buffer */
176 pr("%s -- too many conditions\n", str);
183 /* Like strcmp(S1, S2), but limit length of S1 to SZ1 and of S2 to SZ2. */
185 strnncmp(char *s1, size_t sz1, char *s2, size_t sz2)
188 if (sz1 == sz2) return strncmp(s1, s2, sz2);
189 if (sz1 < sz2) return -strnncmp(s2, sz2, s1, sz1);
190 res = strncmp(s1, s2, sz2);
191 return res ? res : s1[sz2];
194 #define EVAL(op, lft, rgt) \
195 ((op) == '<' ? (lft) < (rgt) \
196 : (op) == '=' ? (lft) == (rgt) \
197 : (op) == '>' ? (lft) > (rgt) \
198 : (op) == '#' ? (lft) != (rgt) \
202 * Evaluate compiled conditions in array NP[NCOND].
203 * Return non-zero iff they are all true.
204 * PTR points to a context object of the type that was used to compile
208 nstr_exec(struct nscstr *np, int ncond, void *ptr)
210 int i, op, optype, cmp;
211 struct valstr lft, rgt;
213 for (i = 0; i < ncond; ++i) {
215 optype = np[i].optype;
216 if (np[i].lft.val_cat == NSC_NOCAT || np[i].rgt.val_cat == NSC_NOCAT)
219 nstr_exec_val(&lft, player->cnum, ptr, optype);
221 nstr_exec_val(&rgt, player->cnum, ptr, optype);
224 if (!EVAL(op, lft.val_as.lng, rgt.val_as.lng))
228 if (!EVAL(op, lft.val_as.dbl, rgt.val_as.dbl))
232 cmp = strnncmp(lft.val_as.str.base, lft.val_as.str.maxsz,
233 rgt.val_as.str.base, rgt.val_as.str.maxsz);
234 if (!EVAL(op, cmp, 0))
247 * Parse a value in STR into VAL.
248 * Return a pointer to the first character after the value.
249 * Value is either evaluated into NSC_STRING, NSC_DOUBLE or NSC_LONG,
253 nstr_parse_val(char *str, struct valstr *val)
260 if (str[0] == '\'') {
261 for (tail = str + 1; *tail && *tail != '\''; ++tail) ;
262 /* FIXME implement \ quoting */
263 val->val_type = NSC_STRING;
264 val->val_cat = NSC_VAL;
265 val->val_as.str.base = str + 1;
266 val->val_as.str.maxsz = tail - (str + 1);
268 /* FIXME else unclosed string */
273 if (isalpha(str[0])) {
274 for (tail = str+1; isalnum(*tail) || *tail == '_'; ++tail) ;
275 val->val_type = NSC_NOTYPE;
276 val->val_cat = NSC_ID;
277 val->val_as.str.base = str;
278 val->val_as.str.maxsz = tail - str;
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;
298 /* funny character, interpret as identifier */
300 val->val_type = NSC_NOTYPE;
301 val->val_cat = NSC_ID;
302 val->val_as.str.base = str;
303 val->val_as.str.maxsz = tail - str;
308 * Match VAL in table of selector descriptors CA, return index.
309 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
311 * A VAL that is not an identifier doesn't match anything. A null CA
312 * is considered empty.
315 nstr_match_ca(struct valstr *val, struct castr *ca)
319 if (val->val_cat != NSC_ID || val->val_as.str.maxsz >= sizeof(id))
325 memcpy(id, val->val_as.str.base, val->val_as.str.maxsz);
326 id[val->val_as.str.maxsz] = 0;
328 return stmtch(id, ca, offsetof(struct castr, ca_name),
329 sizeof(struct castr));
333 * Match VAL in a selector's values, return its (non-negative) value.
334 * Match values of selector descriptor CA[IDX], provided IDX is not
335 * negative. CA may be null when IDX is negative.
336 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
340 nstr_match_val(struct valstr *val, struct castr *ca, int idx)
344 if (val->val_cat != NSC_ID || val->val_as.str.maxsz >= sizeof(id))
347 if (idx < 0 || ca[idx].ca_table == EF_BAD)
349 if (CANT_HAPPEN(nstr_promote(ca[idx].ca_type) != NSC_LONG))
352 memcpy(id, val->val_as.str.base, val->val_as.str.maxsz);
353 id[val->val_as.str.maxsz] = 0;
355 return ef_elt_byname(ca[idx].ca_table, id);
359 * Can CA[IDX] be compared to a string?
360 * Return 0 for negative IDX.
363 nstr_string_ok(struct castr *ca, int idx)
365 return idx >= 0 && nstr_promote(ca[idx].ca_type) == NSC_STRING;
369 * Change VAL to resolve identifier to selector or string.
370 * Return VAL on success, NULL on error.
371 * No change if VAL is not an identifier. Otherwise, change it as
373 * Error if IDX == M_NOTUNIQUE or IDX == M_NOTFOUND and !STRING_OK.
374 * Change into string if IDX == M_NOTFOUND and STRING_OK.
375 * Change into selector CA[IDX] if IDX >= 0.
377 static struct valstr *
378 nstr_resolve_id(struct valstr *val, struct castr *ca, int idx, int string_ok)
380 if (val->val_cat != NSC_ID)
383 if (idx == M_NOTUNIQUE && !string_ok) {
384 pr("%.*s -- ambiguous name\n",
385 (int)val->val_as.str.maxsz, val->val_as.str.base);
386 val->val_cat = NSC_NOCAT;
390 if (idx == M_NOTFOUND && !string_ok) {
391 pr("%.*s -- unknown name\n",
392 (int)val->val_as.str.maxsz, val->val_as.str.base);
393 val->val_cat = NSC_NOCAT;
398 CANT_HAPPEN(!string_ok);
399 /* interpret unresolvable identifier as string */
400 val->val_type = NSC_STRING;
401 val->val_cat = NSC_VAL;
402 /* map identifier ~ to empty string, like some commands do */
403 if (val->val_as.str.maxsz == 1 && val->val_as.str.base[0] == '~')
404 val->val_as.str.maxsz = 0;
408 return nstr_resolve_sel(val, &ca[idx]);
412 * Change VAL to resolve identifier to selector CA.
413 * Return VAL on success, NULL if the player is denied access to the
415 * VAL must be an identifier.
417 static struct valstr *
418 nstr_resolve_sel(struct valstr *val, struct castr *ca)
420 if (CANT_HAPPEN(val->val_cat != NSC_ID)) {
421 val->val_cat = NSC_NOCAT;
425 if ((ca->ca_flags & NSC_DEITY) && !player->god) {
426 pr("%.*s -- not accessible to mortals\n",
427 (int)val->val_as.str.maxsz, val->val_as.str.base);
428 val->val_cat = NSC_NOCAT;
432 val->val_type = ca->ca_type;
433 val->val_cat = NSC_OFF;
434 val->val_as.sym.off = ca->ca_off;
435 val->val_as.sym.len = ca->ca_len;
436 val->val_as.sym.idx = 0;
441 * Initialize VAL to value SELVAL for selector CA, return VAL.
443 static struct valstr *
444 nstr_mkselval(struct valstr *val, int selval, struct castr *ca)
446 if (CANT_HAPPEN(nstr_promote(ca->ca_type) != NSC_LONG
447 || ca->ca_table == EF_BAD)) {
448 val->val_type = NSC_NOTYPE;
449 val->val_cat = NSC_NOCAT;
453 val->val_type = ca->ca_type;
454 val->val_cat = NSC_VAL;
455 val->val_as.lng = selval;
460 * Compile a value in STR into VAL.
461 * Return a pointer to the first character after the value on success,
463 * TYPE is the context type, a file type.
464 * If STR names an array, VAL simply refers to the element with index
468 nstr_comp_val(char *str, struct valstr *val, int type)
470 struct castr *ca = ef_cadef(type);
471 char *tail = nstr_parse_val(str, val);
472 if (!nstr_resolve_id(val, ca, nstr_match_ca(val, ca), 0))
480 * If VALTYPE is an integer type, return NSC_LONG.
481 * If VALTYPE is a floating-point type, return NSC_DOUBLE.
482 * If VALTYPE is a string type, return NSC_STRING.
485 nstr_promote(int valtype)
504 valtype = NSC_DOUBLE;
507 valtype = NSC_STRING;
511 valtype = NSC_NOTYPE;
517 cond_type_mismatch(char *str)
520 pr("%s -- condition operand type mismatch\n", str);
525 * Coerce VAL to promoted value type TO.
526 * Return 0 on success, -1 on error.
527 * If VAL is evaluated, convert it, else only check.
528 * STR is the condition text to be used for error messages. Suppress
529 * messages if it is a null pointer.
532 nstr_coerce_val(struct valstr *val, nsc_type to, char *str)
534 /* FIXME get rid of promotion? */
535 nsc_type from = nstr_promote(val->val_type);
537 if (from == NSC_NOTYPE)
543 return cond_type_mismatch(str); /* FIXME implement */
545 if (from == NSC_LONG) {
546 if (val->val_cat == NSC_VAL)
547 val->val_as.dbl = val->val_as.lng;
549 return cond_type_mismatch(str);
552 return cond_type_mismatch(str);
559 if (val->val_cat == NSC_VAL) {
560 /* unimplemented conversions; don't currently occur here */
561 CANT_HAPPEN(val->val_type == NSC_XCOORD
562 || val->val_type == NSC_YCOORD
563 || val->val_type == NSC_HIDDEN);