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);
54 * Compile conditions into array NP[LEN].
55 * Return number of conditions, or -1 on error.
56 * It is an error if there are more than LEN conditions.
57 * TYPE is the context type, a file type.
58 * STR is the condition string, in Empire syntax, without the leading
62 nstr_comp(struct nscstr *np, int len, int type, char *str)
64 struct castr *ca = ef_cadef(type);
69 int lft_caidx, rgt_caidx;
71 int lft_type, rgt_type;
74 for (i = 0; ; ++i, ++np) {
79 tail = nstr_parse_val(cond, &np->lft);
80 lft_caidx = nstr_match_ca(&np->lft, ca);
83 if (*tail != '<' && *tail != '=' && *tail != '>' && *tail != '#') {
85 pr("%s -- expected condition operator\n", cond);
87 pr("%s -- missing condition operator\n", cond);
94 tail = nstr_parse_val(tail, &np->rgt);
95 rgt_caidx = nstr_match_ca(&np->rgt, ca);
100 * An identifier can name a selector or, if the other operand
101 * is a selector, a value for that. The condition is
102 * ambiguous if both selector x value and value x selector are
103 * possible. Example: n<n for sectors could mean newdes<n or
106 lft_val = nstr_match_val(&np->lft, ca, rgt_caidx);
107 rgt_val = nstr_match_val(&np->rgt, ca, lft_caidx);
109 * if lft_val >= 0, then rhs names a selector and lhs names
110 * one of its values. Likewise for rgt_val.
112 if (lft_val >= 0 && rgt_val >= 0) {
113 pr("%.*s -- condition ambiguous\n", (int)(tail-cond), cond);
115 } else if (rgt_val >= 0) {
116 /* selector x value */
117 if (!nstr_resolve_sel(&np->lft, &ca[lft_caidx]))
119 nstr_mkselval(&np->rgt, rgt_val, &ca[lft_caidx]);
120 } else if (lft_val >= 0) {
121 /* value x selector */
122 nstr_mkselval(&np->lft, lft_val, &ca[rgt_caidx]);
123 if (!nstr_resolve_sel(&np->rgt, &ca[rgt_caidx]))
127 * Neither side works as selector value; any identifiers
128 * must name selectors.
130 if (!nstr_resolve_id(&np->lft, ca, lft_caidx,
131 nstr_string_ok(ca, rgt_caidx)))
133 if (!nstr_resolve_id(&np->rgt, ca, rgt_caidx,
134 nstr_string_ok(ca, lft_caidx)))
138 /* find operator type, coerce operands */
139 lft_type = nstr_promote(np->lft.val_type);
140 rgt_type = nstr_promote(np->rgt.val_type);
141 np->optype = NSC_NOTYPE;
142 if (lft_type == NSC_STRING) {
143 if (!nstr_coerce_val(&np->rgt, NSC_STRING, str))
144 np->optype = NSC_STRING;
145 } else if (rgt_type == NSC_STRING) {
146 if (!nstr_coerce_val(&np->lft, NSC_STRING, str))
147 np->optype = NSC_STRING;
148 } else if (lft_type == NSC_DOUBLE) {
149 if (!nstr_coerce_val(&np->rgt, NSC_DOUBLE, str))
150 np->optype = NSC_DOUBLE;
151 } else if (rgt_type == NSC_DOUBLE) {
152 if (!nstr_coerce_val(&np->lft, NSC_DOUBLE, str))
153 np->optype = NSC_DOUBLE;
155 if (!nstr_coerce_val(&np->lft, NSC_LONG, str)
156 && !nstr_coerce_val(&np->rgt, NSC_LONG, str))
157 np->optype = NSC_LONG;
159 if (np->optype == NSC_NOTYPE)
162 /* another condition? */
166 pr("%s -- expected `&'\n", cond);
173 /* could just return I and let caller gripe or enlarge buffer */
174 pr("%s -- too many conditions\n", str);
181 /* Like strcmp(S1, S2), but limit length of S1 to SZ1 and of S2 to SZ2. */
183 strnncmp(char *s1, size_t sz1, char *s2, size_t sz2)
186 if (sz1 == sz2) return strncmp(s1, s2, sz2);
187 if (sz1 < sz2) return -strnncmp(s2, sz2, s1, sz1);
188 res = strncmp(s1, s2, sz2);
189 return res ? res : s1[sz2];
192 #define EVAL(op, lft, rgt) \
193 ((op) == '<' ? (lft) < (rgt) \
194 : (op) == '=' ? (lft) == (rgt) \
195 : (op) == '>' ? (lft) > (rgt) \
196 : (op) == '#' ? (lft) != (rgt) \
200 * Evaluate compiled conditions in array NP[NCOND].
201 * Return non-zero iff they are all true.
202 * PTR points to a context object of the type that was used to compile
206 nstr_exec(struct nscstr *np, int ncond, void *ptr)
208 int i, op, optype, cmp;
209 struct valstr lft, rgt;
211 for (i = 0; i < ncond; ++i) {
213 optype = np[i].optype;
214 if (np[i].lft.val_cat == NSC_NOCAT || np[i].rgt.val_cat == NSC_NOCAT)
217 nstr_exec_val(&lft, player->cnum, ptr, optype);
219 nstr_exec_val(&rgt, player->cnum, ptr, optype);
222 if (!EVAL(op, lft.val_as.lng, rgt.val_as.lng))
226 if (!EVAL(op, lft.val_as.dbl, rgt.val_as.dbl))
230 cmp = strnncmp(lft.val_as.str.base, lft.val_as.str.maxsz,
231 rgt.val_as.str.base, rgt.val_as.str.maxsz);
232 if (!EVAL(op, cmp, 0))
245 * Parse a value in STR into VAL.
246 * Return a pointer to the first character after the value.
247 * Value is either evaluated into NSC_STRING, NSC_DOUBLE or NSC_LONG,
251 nstr_parse_val(char *str, struct valstr *val)
258 if (str[0] == '\'') {
259 for (tail = str + 1; *tail && *tail != '\''; ++tail) ;
260 /* FIXME implement \ quoting */
261 val->val_type = NSC_STRING;
262 val->val_cat = NSC_VAL;
263 val->val_as.str.base = str + 1;
264 val->val_as.str.maxsz = tail - (str + 1);
266 /* FIXME else unclosed string */
271 if (isalpha(str[0])) {
272 for (tail = str+1; isalnum(*tail) || *tail == '_'; ++tail) ;
273 val->val_type = NSC_NOTYPE;
274 val->val_cat = NSC_ID;
275 val->val_as.str.base = str;
276 val->val_as.str.maxsz = tail - str;
281 l = strtol(str, &tail, 0);
282 d = strtod(str, &tail2);
284 val->val_type = NSC_DOUBLE;
285 val->val_cat = NSC_VAL;
290 val->val_type = NSC_LONG;
291 val->val_cat = NSC_VAL;
296 /* funny character, interpret as identifier */
298 val->val_type = NSC_NOTYPE;
299 val->val_cat = NSC_ID;
300 val->val_as.str.base = str;
301 val->val_as.str.maxsz = tail - str;
306 * Match VAL in table of selector descriptors CA, return index.
307 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
309 * A VAL that is not an identifier doesn't match anything. A null CA
310 * is considered empty.
313 nstr_match_ca(struct valstr *val, struct castr *ca)
317 if (val->val_cat != NSC_ID || val->val_as.str.maxsz >= sizeof(id))
323 memcpy(id, val->val_as.str.base, val->val_as.str.maxsz);
324 id[val->val_as.str.maxsz] = 0;
326 return stmtch(id, ca, offsetof(struct castr, ca_name),
327 sizeof(struct castr));
331 * Match VAL in a selector's values, return its (non-negative) value.
332 * Match values of selector descriptor CA[IDX], provided IDX is not
333 * negative. CA may be null when IDX is negative.
334 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
338 nstr_match_val(struct valstr *val, struct castr *ca, int idx)
342 if (val->val_cat != NSC_ID || val->val_as.str.maxsz >= sizeof(id))
345 if (idx < 0 || ca[idx].ca_table == EF_BAD)
347 if (CANT_HAPPEN(nstr_promote(ca[idx].ca_type) != NSC_LONG))
350 memcpy(id, val->val_as.str.base, val->val_as.str.maxsz);
351 id[val->val_as.str.maxsz] = 0;
353 return ef_elt_byname(ca[idx].ca_table, id);
357 * Can CA[IDX] be compared to a string?
358 * Return 0 for negative IDX.
361 nstr_string_ok(struct castr *ca, int idx)
363 return idx >= 0 && nstr_promote(ca[idx].ca_type) == NSC_STRING;
367 * Change VAL to resolve identifier to selector or string.
368 * Return VAL on success, NULL on error.
369 * No change if VAL is not an identifier. Otherwise, change it as
371 * Error if IDX == M_NOTUNIQUE or IDX == M_NOTFOUND and !STRING_OK.
372 * Change into string if IDX == M_NOTFOUND and STRING_OK.
373 * Change into selector CA[IDX] if IDX >= 0.
375 static struct valstr *
376 nstr_resolve_id(struct valstr *val, struct castr *ca, int idx, int string_ok)
378 if (val->val_cat != NSC_ID)
381 if (idx == M_NOTUNIQUE && !string_ok) {
382 pr("%.*s -- ambiguous name\n",
383 (int)val->val_as.str.maxsz, val->val_as.str.base);
384 val->val_cat = NSC_NOCAT;
388 if (idx == M_NOTFOUND && !string_ok) {
389 pr("%.*s -- unknown name\n",
390 (int)val->val_as.str.maxsz, val->val_as.str.base);
391 val->val_cat = NSC_NOCAT;
396 CANT_HAPPEN(!string_ok);
397 /* interpret unresolvable identifier as string */
398 val->val_type = NSC_STRING;
399 val->val_cat = NSC_VAL;
400 /* map identifier ~ to empty string, like some commands do */
401 if (val->val_as.str.maxsz == 1 && val->val_as.str.base[0] == '~')
402 val->val_as.str.maxsz = 0;
406 return nstr_resolve_sel(val, &ca[idx]);
410 * Change VAL to resolve identifier to selector CA.
411 * Return VAL on success, NULL if the player is denied access to the
413 * VAL must be an identifier.
415 static struct valstr *
416 nstr_resolve_sel(struct valstr *val, struct castr *ca)
418 if (CANT_HAPPEN(val->val_cat != NSC_ID)) {
419 val->val_cat = NSC_NOCAT;
423 if ((ca->ca_flags & NSC_DEITY) && !player->god) {
424 pr("%.*s -- not accessible to mortals\n",
425 (int)val->val_as.str.maxsz, val->val_as.str.base);
426 val->val_cat = NSC_NOCAT;
430 val->val_type = ca->ca_type;
431 val->val_cat = NSC_OFF;
432 val->val_as.sym.off = ca->ca_off;
433 val->val_as.sym.len = ca->ca_len;
434 val->val_as.sym.idx = 0;
439 * Initialize VAL to value SELVAL for selector CA, return VAL.
441 static struct valstr *
442 nstr_mkselval(struct valstr *val, int selval, struct castr *ca)
444 if (CANT_HAPPEN(nstr_promote(ca->ca_type) != NSC_LONG
445 || ca->ca_table == EF_BAD)) {
446 val->val_type = NSC_NOTYPE;
447 val->val_cat = NSC_NOCAT;
451 val->val_type = ca->ca_type;
452 val->val_cat = NSC_VAL;
453 val->val_as.lng = selval;
458 * Compile a value in STR into VAL.
459 * Return a pointer to the first character after the value on success,
461 * TYPE is the context type, a file type.
462 * If STR names an array, VAL simply refers to the element with index
466 nstr_comp_val(char *str, struct valstr *val, int type)
468 struct castr *ca = ef_cadef(type);
469 char *tail = nstr_parse_val(str, val);
470 if (!nstr_resolve_id(val, ca, nstr_match_ca(val, ca), 0))
476 cond_type_mismatch(char *str)
479 pr("%s -- condition operand type mismatch\n", str);
484 * Coerce VAL to promoted value type TO.
485 * Return 0 on success, -1 on error.
486 * If VAL is evaluated, convert it, else only check.
487 * STR is the condition text to be used for error messages. Suppress
488 * messages if it is a null pointer.
491 nstr_coerce_val(struct valstr *val, nsc_type to, char *str)
493 /* FIXME get rid of promotion? */
494 nsc_type from = nstr_promote(val->val_type);
496 if (from == NSC_NOTYPE)
502 return cond_type_mismatch(str); /* FIXME implement */
504 if (from == NSC_LONG) {
505 if (val->val_cat == NSC_VAL)
506 val->val_as.dbl = val->val_as.lng;
508 return cond_type_mismatch(str);
511 return cond_type_mismatch(str);
518 if (val->val_cat == NSC_VAL) {
519 /* unimplemented conversions; don't currently occur here */
520 CANT_HAPPEN(val->val_type == NSC_XCOORD
521 || val->val_type == NSC_YCOORD
522 || val->val_type == NSC_HIDDEN);