2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * nstr.c: compile and execute the item selections on sectors
29 * Known contributors to this file:
32 * Markus Armbruster, 2004-2014
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_is_name_of_ca(struct valstr *, struct castr *, int);
48 static int nstr_ca_comparable(struct castr *, int, int);
49 static int nstr_match_val(struct valstr *, struct castr *, int);
50 static struct valstr *nstr_resolve_id(struct valstr *, struct castr *, int);
51 static struct valstr *nstr_resolve_val(struct valstr *, int, struct castr *);
52 static int nstr_optype(enum nsc_type, enum nsc_type);
55 * Compile conditions into array @np[@len].
56 * Return number of conditions, or -1 on error.
57 * It is an error if there are more than @len conditions.
58 * @type is the context type, a file type.
59 * @str is the condition string, in Empire syntax, without the leading
63 nstr_comp(struct nscstr *np, int len, int type, char *str)
65 struct castr *ca = ef_cadef(type);
70 int lft_caidx, rgt_caidx;
75 for (i = 0; ; ++i, ++np) {
81 pr("%s -- %scondition expected\n", str, i ? "another " : "");
84 tail = nstr_parse_val(cond, &np->lft);
85 lft_caidx = nstr_match_ca(&np->lft, ca);
88 if (*tail != '<' && *tail != '=' && *tail != '>' && *tail != '#') {
90 pr("%s -- expected condition operator\n", cond);
92 pr("%s -- missing condition operator\n", cond);
100 pr("%s -- operand expected\n", cond);
103 tail = nstr_parse_val(tail, &np->rgt);
104 rgt_caidx = nstr_match_ca(&np->rgt, ca);
107 * Resolve identifiers
109 * If just one operand is an identifier, it names a selector.
110 * If both operands are identifiers, things get complicated:
111 * either can then name a selector or a symbolic value for the
112 * selector named by the other operand.
114 if (np->lft.val_cat == NSC_ID && np->rgt.val_cat == NSC_ID) {
115 lft_val = nstr_match_val(&np->lft, ca, rgt_caidx);
116 rgt_val = nstr_match_val(&np->rgt, ca, lft_caidx);
117 two_sels = nstr_ca_comparable(ca, lft_caidx, rgt_caidx);
119 * If lft_val >= 0 interpreting rgt as a selector and lft
120 * as one of its values works. Likewise for rgt_val >= 0.
121 * If two_sels, interpreting both lft and rgt as selector
124 switch ((lft_val >= 0) + (rgt_val >= 0) + !!two_sels) {
125 case 0: /* no interpretation */
126 if (lft_caidx >= 0 && rgt_caidx >= 0) {
128 * Both identifiers name selectors. Since
129 * !two_sels, they can't be comparable.
130 * Example: type=civil.
132 pr("%.*s -- not comparable\n", (int)(tail-cond), cond);
136 * At least one identifier doesn't name a selector,
137 * and nstr_resolve_id() will fail for it below
140 case 1: /* one unambigous interpretation */
142 default: /* multiple interpretations */
144 * Last-resort disambiguation: if the identifier is
145 * the unabbreviated name of a selector, discard
146 * value, else discard selector interpretation.
147 * Example: resolve wing=g to wing='g', not wing=group
150 if (nstr_is_name_of_ca(&np->lft, ca, lft_caidx))
154 if (nstr_is_name_of_ca(&np->rgt, ca, rgt_caidx))
158 if ((lft_val >= 0) + (rgt_val >= 0) + !!two_sels == 1)
159 break; /* last-resort disambiguation worked */
161 * Example: n<n for sectors could mean newdes<n or
164 pr("%.*s -- condition ambiguous\n", (int)(tail-cond), cond);
167 /* resolve identifiers naming values */
169 nstr_resolve_val(&np->lft, lft_val, &ca[rgt_caidx]);
171 nstr_resolve_val(&np->rgt, rgt_val, &ca[lft_caidx]);
173 /* remaining identifiers name selectors */
174 if (!nstr_resolve_id(&np->lft, ca, lft_caidx))
176 if (!nstr_resolve_id(&np->rgt, ca, rgt_caidx))
179 /* find operator type */
180 np->optype = nstr_optype(np->lft.val_type, np->rgt.val_type);
181 if (np->optype == NSC_NOTYPE) {
182 pr("%.*s -- not comparable\n", (int)(tail-cond), cond);
186 /* another condition? */
190 pr("%s -- expected `&'\n", cond);
197 /* could just return I and let caller gripe or enlarge buffer */
198 pr("%s -- too many conditions\n", str);
205 /* Like strcmp(S1, S2), but limit length of S1 to SZ1 and of S2 to SZ2. */
207 strnncmp(char *s1, size_t sz1, char *s2, size_t sz2)
211 return strncmp(s1, s2, sz2);
213 return -strnncmp(s2, sz2, s1, sz1);
214 res = strncmp(s1, s2, sz2);
215 return res ? res : s1[sz2];
218 #define EVAL(op, lft, rgt) \
219 ((op) == '<' ? (lft) < (rgt) \
220 : (op) == '=' ? (lft) == (rgt) \
221 : (op) == '>' ? (lft) > (rgt) \
222 : (op) == '#' ? (lft) != (rgt) \
226 * Evaluate compiled conditions in array @np[@ncond].
227 * Return non-zero iff they are all true.
228 * @ptr points to a context object of the type that was used to compile
232 nstr_exec(struct nscstr *np, int ncond, void *ptr)
235 enum nsc_type optype;
236 struct valstr lft, rgt;
238 for (i = 0; i < ncond; ++i) {
240 optype = np[i].optype;
241 if (np[i].lft.val_cat == NSC_NOCAT || np[i].rgt.val_cat == NSC_NOCAT)
244 nstr_eval(&lft, player->cnum, ptr, optype);
246 nstr_eval(&rgt, player->cnum, ptr, optype);
247 if (CANT_HAPPEN(lft.val_type != optype || rgt.val_type != optype))
251 if (!EVAL(op, lft.val_as.lng, rgt.val_as.lng))
255 if (!EVAL(op, lft.val_as.dbl, rgt.val_as.dbl))
259 cmp = strnncmp(lft.val_as.str.base, lft.val_as.str.maxsz,
260 rgt.val_as.str.base, rgt.val_as.str.maxsz);
261 if (!EVAL(op, cmp, 0))
274 * Parse a value in @str into @val.
275 * Return a pointer to the first character after the value.
276 * Value is either evaluated into NSC_STRING, NSC_DOUBLE or NSC_LONG,
280 nstr_parse_val(char *str, struct valstr *val)
287 if (str[0] == '\'') {
288 for (tail = str + 1; *tail && *tail != '\''; ++tail) ;
289 /* FIXME implement \ quoting */
290 val->val_type = NSC_STRING;
291 val->val_cat = NSC_VAL;
292 val->val_as.str.base = str + 1;
293 val->val_as.str.maxsz = tail - (str + 1);
296 /* FIXME else unclosed string */
301 if (isalpha(str[0])) {
302 for (tail = str+1; isalnum(*tail) || *tail == '_'; ++tail) ;
303 val->val_type = NSC_NOTYPE;
304 val->val_cat = NSC_ID;
305 val->val_as.str.base = str;
306 val->val_as.str.maxsz = tail - str;
311 l = strtol(str, &tail, 0);
312 d = strtod(str, &tail2);
314 val->val_type = NSC_DOUBLE;
315 val->val_cat = NSC_VAL;
320 val->val_type = NSC_LONG;
321 val->val_cat = NSC_VAL;
326 /* funny character, interpret as identifier */
327 tail = CANT_HAPPEN(!*str) ? str : str + 1;
328 val->val_type = NSC_NOTYPE;
329 val->val_cat = NSC_ID;
330 val->val_as.str.base = str;
331 val->val_as.str.maxsz = tail - str;
336 * Match @val in table of selector descriptors @ca, return index.
337 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
339 * A @val that is not an identifier doesn't match anything. A null @ca
340 * is considered empty.
343 nstr_match_ca(struct valstr *val, struct castr *ca)
347 if (val->val_cat != NSC_ID || val->val_as.str.maxsz >= sizeof(id))
353 memcpy(id, val->val_as.str.base, val->val_as.str.maxsz);
354 id[val->val_as.str.maxsz] = 0;
356 return stmtch(id, ca, offsetof(struct castr, ca_name),
357 sizeof(struct castr));
361 * Is identifier @val the name of the selector given by @ca and @idx?
362 * Return non-zero if and only if @idx is non-negative and @val is the
364 * @idx must have been obtained from nstr_match_ca(@val, @ca).
367 nstr_is_name_of_ca(struct valstr *val, struct castr *ca, int idx)
369 if (CANT_HAPPEN(val->val_cat != NSC_ID && idx >= 0))
371 return idx >= 0 && strlen(ca[idx].ca_name) == val->val_as.str.maxsz;
375 * Do we have two comparable selectors?
376 * Check selector descriptors @ca[@lft_idx] (unless @lft_idx is negative)
377 * and @ca[@rgt_idx] (unless @rgt_idx is negative). @ca may be null when
381 nstr_ca_comparable(struct castr *ca, int lft_idx, int rgt_idx)
383 if (lft_idx < 0 || rgt_idx < 0)
385 if (ca[lft_idx].ca_table != ca[rgt_idx].ca_table)
386 return 0; /* Example: land type=spy */
387 return nstr_optype(ca[lft_idx].ca_type, ca[rgt_idx].ca_type)
388 != NSC_NOTYPE; /* Example: ship name=effic */
392 * Match @val in a selector's values, return its (non-negative) value.
393 * Match values of selector descriptor @ca[@idx], provided @idx is not
394 * negative. @ca may be null when @idx is negative.
395 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
399 nstr_match_val(struct valstr *val, struct castr *ca, int idx)
404 if (val->val_cat != NSC_ID || idx < 0)
407 type = nstr_promote(ca[idx].ca_type);
408 if (type == NSC_STRING)
411 if (ca[idx].ca_table == EF_BAD || CANT_HAPPEN(type != NSC_LONG))
414 if (val->val_as.str.maxsz >= sizeof(id))
416 memcpy(id, val->val_as.str.base, val->val_as.str.maxsz);
417 id[val->val_as.str.maxsz] = 0;
418 return ef_elt_byname(ca[idx].ca_table, id);
422 * Change @val to resolve identifier to selector.
423 * Return @val on success, NULL on error.
424 * No change if @val is not an identifier.
425 * Else change @val into symbolic value for selector @ca[@idx] if @idx >=
426 * 0, and error if not.
428 static struct valstr *
429 nstr_resolve_id(struct valstr *val, struct castr *ca, int idx)
431 if (val->val_cat != NSC_ID)
434 if (idx == M_NOTUNIQUE) {
435 pr("%.*s -- ambiguous name\n",
436 (int)val->val_as.str.maxsz, val->val_as.str.base);
437 val->val_cat = NSC_NOCAT;
441 if (idx == M_NOTFOUND) {
442 pr("%.*s -- unknown name\n",
443 (int)val->val_as.str.maxsz, val->val_as.str.base);
444 val->val_cat = NSC_NOCAT;
448 if (CA_IS_ARRAY(&ca[idx])) {
449 pr("%.*s -- not usable here\n",
450 (int)val->val_as.str.maxsz, val->val_as.str.base);
451 val->val_cat = NSC_NOCAT;
455 if ((ca[idx].ca_flags & NSC_DEITY) && !player->god) {
456 pr("%.*s -- not accessible to mortals\n",
457 (int)val->val_as.str.maxsz, val->val_as.str.base);
458 val->val_cat = NSC_NOCAT;
462 return nstr_mksymval(val, &ca[idx], 0);
466 * Change @val to resolve identifier to value @selval for selector @ca.
468 * @val must be an identifier, and @selval must have been obtained from
469 * nstr_match_val(@val, CA0, @idx), where @ca = &CA0[@IDX].
471 static struct valstr *
472 nstr_resolve_val(struct valstr *val, int selval, struct castr *ca)
474 enum nsc_type type = nstr_promote(ca->ca_type);
476 if (CANT_HAPPEN(val->val_cat != NSC_ID)) {
477 val->val_cat = NSC_NOCAT;
481 if (type == NSC_STRING) {
482 val->val_type = NSC_STRING;
483 val->val_cat = NSC_VAL;
484 /* map identifier ~ to empty string, like some commands do */
485 if (val->val_as.str.maxsz == 1 && val->val_as.str.base[0] == '~')
486 val->val_as.str.maxsz = 0;
490 if (CANT_HAPPEN(type != NSC_LONG || ca->ca_table == EF_BAD)) {
491 val->val_type = NSC_NOTYPE;
492 val->val_cat = NSC_NOCAT;
496 val->val_type = type;
497 val->val_cat = NSC_VAL;
498 val->val_as.lng = selval;
503 * Return operator type for operand types @lft, @rgt.
506 nstr_optype(enum nsc_type lft, enum nsc_type rgt)
508 lft = nstr_promote(lft);
509 rgt = nstr_promote(rgt);
512 if (lft == NSC_DOUBLE && rgt == NSC_LONG)
514 if (rgt == NSC_DOUBLE && lft == NSC_LONG)
520 * Compile a value in @str into @val.
521 * Return a pointer to the first character after the value on success,
523 * @type is the context type, a file type.
526 nstr_comp_val(char *str, struct valstr *val, int type)
528 struct castr *ca = ef_cadef(type);
529 char *tail = nstr_parse_val(str, val);
530 if (!nstr_resolve_id(val, ca, nstr_match_ca(val, ca)))