]> git.pond.sub.org Git - empserver/blob - src/lib/subs/nstr.c
Update copyright notice
[empserver] / src / lib / subs / nstr.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2012, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  *  ---
20  *
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.
24  *
25  *  ---
26  *
27  *  nstr.c: compile and execute the item selections on sectors
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1989
31  *     Steve McClure, 1997
32  *     Markus Armbruster, 2004-2010
33  */
34
35 #include <config.h>
36
37 #include <ctype.h>
38 #include <limits.h>
39 #include "file.h"
40 #include "match.h"
41 #include "nsc.h"
42 #include "player.h"
43 #include "prototypes.h"
44
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);
53
54 /*
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
60  * '?'.
61  */
62 int
63 nstr_comp(struct nscstr *np, int len, int type, char *str)
64 {
65     struct castr *ca = ef_cadef(type);
66     char *cond;
67     char *tail;
68     int i;
69     struct nscstr dummy;
70     int lft_caidx, rgt_caidx;
71     int lft_val, rgt_val;
72     int two_sels;
73
74     cond = str;
75     for (i = 0; ; ++i, ++np) {
76         if (i >= len)
77             np = &dummy;
78
79         /* left operand */
80         tail = nstr_parse_val(cond, &np->lft);
81         lft_caidx = nstr_match_ca(&np->lft, ca);
82
83         /* operator */
84         if (*tail != '<' && *tail != '=' && *tail != '>' && *tail != '#') {
85             if (*tail)
86                 pr("%s -- expected condition operator\n", cond);
87             else
88                 pr("%s -- missing condition operator\n", cond);
89             return -1;
90         }
91         np->operator = *tail;
92         ++tail;
93
94         /* right operand */
95         tail = nstr_parse_val(tail, &np->rgt);
96         rgt_caidx = nstr_match_ca(&np->rgt, ca);
97
98         /*
99          * Resolve identifiers
100          *
101          * If just one operand is an identifier, it names a selector.
102          * If both operands are identifiers, things get complicated:
103          * either can then name a selector or a symbolic value for the
104          * selector named by the other operand.
105          */
106         if (np->lft.val_cat == NSC_ID && np->rgt.val_cat == NSC_ID) {
107             lft_val = nstr_match_val(&np->lft, ca, rgt_caidx);
108             rgt_val = nstr_match_val(&np->rgt, ca, lft_caidx);
109             two_sels = nstr_ca_comparable(ca, lft_caidx, rgt_caidx);
110             /*
111              * If lft_val >= 0 interpreting rgt as a selector and lft
112              * as one of its values works.  Likewise for rgt_val >= 0.
113              * If two_sels, interpreting both lft and rgt as selector
114              * works.
115              */
116             switch ((lft_val >= 0) + (rgt_val >= 0) + !!two_sels) {
117             case 0:             /* no interpretation */
118                 if (lft_caidx >= 0 && rgt_caidx >= 0) {
119                     /*
120                      * Both identifiers name selectors.  Since
121                      * !two_sels, they can't be comparable.
122                      * Example: type=civil.
123                      */
124                     pr("%.*s -- not comparable\n", (int)(tail-cond), cond);
125                     return -1;
126                 }
127                 /*
128                  * At least one identifier doesn't name a selector,
129                  * and nstr_resolve_id() will fail for it below
130                  */
131                 break;
132             case 1:             /* one unambigous interpretation */
133                 break;
134             default:            /* multiple interpretations */
135                 /*
136                  * Last-resort disambiguation: if the identifier is
137                  * the unabbreviated name of a selector, discard
138                  * value, else discard selector interpretation.
139                  * Example: resolve wing=g to wing='g', not wing=group
140                  * or 'wing'=group.
141                  */
142                 if (nstr_is_name_of_ca(&np->lft, ca, lft_caidx))
143                     lft_val = -1;
144                 else
145                     two_sels = 0;
146                 if (nstr_is_name_of_ca(&np->rgt, ca, rgt_caidx))
147                     rgt_val = -1;
148                 else
149                     two_sels = 0;
150                 if ((lft_val >= 0) + (rgt_val >= 0) + !!two_sels == 1)
151                     break;      /* last-resort disambiguation worked */
152                 /*
153                  * Example: n<n for sectors could mean newdes<n or
154                  * n<newdes.
155                  */
156                 pr("%.*s -- condition ambiguous\n", (int)(tail-cond), cond);
157                 return -1;
158             }
159             /* resolve identifiers naming values */
160             if (lft_val >= 0)
161                 nstr_resolve_val(&np->lft, lft_val, &ca[rgt_caidx]);
162             if (rgt_val >= 0)
163                 nstr_resolve_val(&np->rgt, rgt_val, &ca[lft_caidx]);
164         }
165         /* remaining identifiers name selectors */
166         if (!nstr_resolve_id(&np->lft, ca, lft_caidx))
167             return -1;
168         if (!nstr_resolve_id(&np->rgt, ca, rgt_caidx))
169             return -1;
170
171         /* find operator type */
172         np->optype = nstr_optype(np->lft.val_type, np->rgt.val_type);
173         if (np->optype == NSC_NOTYPE) {
174             pr("%.*s -- not comparable\n", (int)(tail-cond), cond);
175             return -1;
176         }
177
178         /* another condition? */
179         if (*tail == 0)
180             break;
181         if (*tail != '&') {
182             pr("%s -- expected `&'\n", cond);
183             return -1;
184         }
185         cond = tail + 1;
186     }
187
188     if (i >= len) {
189         /* could just return I and let caller gripe or enlarge buffer */
190         pr("%s -- too many conditions\n", str);
191         return -1;
192     }
193
194     return i + 1;
195 }
196
197 /* Like strcmp(S1, S2), but limit length of S1 to SZ1 and of S2 to SZ2.  */
198 static int
199 strnncmp(char *s1, size_t sz1, char *s2, size_t sz2)
200 {
201     int res;
202     if (sz1 == sz2)
203         return strncmp(s1, s2, sz2);
204     if (sz1 < sz2)
205         return -strnncmp(s2, sz2, s1, sz1);
206     res = strncmp(s1, s2, sz2);
207     return res ? res : s1[sz2];
208 }
209
210 #define EVAL(op, lft, rgt)                      \
211     ((op) == '<' ? (lft) < (rgt)                \
212      : (op) == '=' ? (lft) == (rgt)             \
213      : (op) == '>' ? (lft) > (rgt)              \
214      : (op) == '#' ? (lft) != (rgt)             \
215      : (CANT_REACH(), 0))
216
217 /*
218  * Evaluate compiled conditions in array NP[NCOND].
219  * Return non-zero iff they are all true.
220  * PTR points to a context object of the type that was used to compile
221  * the conditions.
222  */
223 int
224 nstr_exec(struct nscstr *np, int ncond, void *ptr)
225 {
226     int i, op, optype, cmp;
227     struct valstr lft, rgt;
228
229     for (i = 0; i < ncond; ++i) {
230         op = np[i].operator;
231         optype = np[i].optype;
232         if (np[i].lft.val_cat == NSC_NOCAT || np[i].rgt.val_cat == NSC_NOCAT)
233             return 0;
234         lft = np[i].lft;
235         nstr_exec_val(&lft, player->cnum, ptr, optype);
236         rgt = np[i].rgt;
237         nstr_exec_val(&rgt, player->cnum, ptr, optype);
238         switch (optype) {
239         case NSC_LONG:
240             if (!EVAL(op, lft.val_as.lng, rgt.val_as.lng))
241                 return 0;
242             break;
243         case NSC_DOUBLE:
244             if (!EVAL(op, lft.val_as.dbl, rgt.val_as.dbl))
245                 return 0;
246             break;
247         case NSC_STRING:
248             cmp = strnncmp(lft.val_as.str.base, lft.val_as.str.maxsz,
249                            rgt.val_as.str.base, rgt.val_as.str.maxsz);
250             if (!EVAL(op, cmp, 0))
251                 return 0;
252             break;
253         default:
254             CANT_REACH();
255             return 0;
256         }
257     }
258
259     return 1;
260 }
261
262 /*
263  * Parse a value in STR into VAL.
264  * Return a pointer to the first character after the value.
265  * Value is either evaluated into NSC_STRING, NSC_DOUBLE or NSC_LONG,
266  * or an identifier.
267  */
268 static char *
269 nstr_parse_val(char *str, struct valstr *val)
270 {
271     long l;
272     double d;
273     char *tail, *tail2;
274
275     /* string */
276     if (str[0] == '\'') {
277         for (tail = str + 1; *tail && *tail != '\''; ++tail) ;
278         /* FIXME implement \ quoting */
279         val->val_type = NSC_STRING;
280         val->val_cat = NSC_VAL;
281         val->val_as.str.base = str + 1;
282         val->val_as.str.maxsz = tail - (str + 1);
283         if (*tail)
284             ++tail;
285         /* FIXME else unclosed string */
286         return tail;
287     }
288
289     /* identifier */
290     if (isalpha(str[0])) {
291         for (tail = str+1; isalnum(*tail) || *tail == '_'; ++tail) ;
292         val->val_type = NSC_NOTYPE;
293         val->val_cat = NSC_ID;
294         val->val_as.str.base = str;
295         val->val_as.str.maxsz = tail - str;
296         return tail;
297     }
298
299     /* number */
300     l = strtol(str, &tail, 0);
301     d = strtod(str, &tail2);
302     if (tail2 > tail) {
303         val->val_type = NSC_DOUBLE;
304         val->val_cat = NSC_VAL;
305         val->val_as.dbl = d;
306         return tail2;
307     }
308     if (tail != str) {
309         val->val_type = NSC_LONG;
310         val->val_cat = NSC_VAL;
311         val->val_as.lng = l;
312         return tail;
313     }
314
315     /* funny character, interpret as identifier */
316     tail = str+1;
317     val->val_type = NSC_NOTYPE;
318     val->val_cat = NSC_ID;
319     val->val_as.str.base = str;
320     val->val_as.str.maxsz = tail - str;
321     return tail;
322 }
323
324 /*
325  * Match VAL in table of selector descriptors CA, return index.
326  * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
327  * several.
328  * A VAL that is not an identifier doesn't match anything.  A null CA
329  * is considered empty.
330  */
331 static int
332 nstr_match_ca(struct valstr *val, struct castr *ca)
333 {
334     char id[32];
335
336     if (val->val_cat != NSC_ID || val->val_as.str.maxsz >= sizeof(id))
337         return M_NOTFOUND;
338
339     if (!ca)
340         return M_NOTFOUND;
341
342     memcpy(id, val->val_as.str.base, val->val_as.str.maxsz);
343     id[val->val_as.str.maxsz] = 0;
344
345     return stmtch(id, ca, offsetof(struct castr, ca_name),
346                   sizeof(struct castr));
347 }
348
349 /*
350  * Is identifier VAL the name of the selector given by CA and IDX?
351  * Return non-zero if and only if IDX is non-negative and VAL is the
352  * name of CA[IDX].
353  * IDX must have been obtained from nstr_match_ca(VAL, CA).
354  */
355 static int
356 nstr_is_name_of_ca(struct valstr *val, struct castr *ca, int idx)
357 {
358     if (CANT_HAPPEN(val->val_cat != NSC_ID && idx >= 0))
359         return 0;
360     return idx >= 0 && strlen(ca[idx].ca_name) == val->val_as.str.maxsz;
361 }
362
363 /*
364  * Do we have two comparable selectors?
365  * Check selector descriptors CA[LFT_IDX] (unless LFT_IDX is negative)
366  * and CA[RGT_IDX] (unless RGT_IDX is negative).  CA may be null when
367  * both are negative.
368  */
369 static int
370 nstr_ca_comparable(struct castr *ca, int lft_idx, int rgt_idx)
371 {
372     if (lft_idx < 0 || rgt_idx < 0)
373         return 0;
374     if (ca[lft_idx].ca_table != ca[rgt_idx].ca_table)
375         return 0;               /* Example: land type=spy */
376     return nstr_optype(ca[lft_idx].ca_type, ca[rgt_idx].ca_type)
377         != NSC_NOTYPE;          /* Example: ship name=effic */
378 }
379
380 /*
381  * Match VAL in a selector's values, return its (non-negative) value.
382  * Match values of selector descriptor CA[IDX], provided IDX is not
383  * negative.  CA may be null when IDX is negative.
384  * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
385  * several.
386  */
387 static int
388 nstr_match_val(struct valstr *val, struct castr *ca, int idx)
389 {
390     char id[32];
391     enum nsc_type type;
392
393     if (val->val_cat != NSC_ID || idx < 0)
394         return M_NOTFOUND;
395
396     type = nstr_promote(ca[idx].ca_type);
397     if (type == NSC_STRING)
398         return 0;
399
400     if (ca[idx].ca_table == EF_BAD || CANT_HAPPEN(type != NSC_LONG))
401         return M_NOTFOUND;
402
403     if (val->val_as.str.maxsz >= sizeof(id))
404         return M_NOTFOUND;
405     memcpy(id, val->val_as.str.base, val->val_as.str.maxsz);
406     id[val->val_as.str.maxsz] = 0;
407     return ef_elt_byname(ca[idx].ca_table, id);
408 }
409
410 /*
411  * Change VAL to resolve identifier to selector.
412  * Return VAL on success, NULL on error.
413  * No change if VAL is not an identifier.
414  * Else change VAL into symbolic value for selector CA[IDX] if IDX >=
415  * 0, and error if not.
416  */
417 static struct valstr *
418 nstr_resolve_id(struct valstr *val, struct castr *ca, int idx)
419 {
420     if (val->val_cat != NSC_ID)
421         return val;
422
423     if (idx == M_NOTUNIQUE) {
424         pr("%.*s -- ambiguous name\n",
425            (int)val->val_as.str.maxsz, val->val_as.str.base);
426         val->val_cat = NSC_NOCAT;
427         return NULL;
428     }
429
430     if (idx == M_NOTFOUND) {
431         pr("%.*s -- unknown name\n",
432            (int)val->val_as.str.maxsz, val->val_as.str.base);
433         val->val_cat = NSC_NOCAT;
434         return NULL;
435     }
436
437     if ((ca[idx].ca_flags & NSC_DEITY) && !player->god) {
438         pr("%.*s -- not accessible to mortals\n",
439            (int)val->val_as.str.maxsz, val->val_as.str.base);
440         val->val_cat = NSC_NOCAT;
441         return NULL;
442     }
443
444     return nstr_mksymval(val, &ca[idx], 0);
445 }
446
447 /*
448  * Change VAL to resolve identifier to value SELVAL for selector CA.
449  * Return VAL.
450  * VAL must be an identifier, and SELVAL must have been obtained from
451  * nstr_match_val(VAL, CA0, IDX), where CA = &CA0[IDX].
452  */
453 static struct valstr *
454 nstr_resolve_val(struct valstr *val, int selval, struct castr *ca)
455 {
456     enum nsc_type type = nstr_promote(ca->ca_type);
457
458     if (CANT_HAPPEN(val->val_cat != NSC_ID)) {
459         val->val_cat = NSC_NOCAT;
460         return val;
461     }
462
463     if (type == NSC_STRING) {
464         val->val_type = NSC_STRING;
465         val->val_cat = NSC_VAL;
466         /* map identifier ~ to empty string, like some commands do */
467         if (val->val_as.str.maxsz == 1 && val->val_as.str.base[0] == '~')
468             val->val_as.str.maxsz = 0;
469         return val;
470     }
471
472     if (CANT_HAPPEN(type != NSC_LONG || ca->ca_table == EF_BAD)) {
473         val->val_type = NSC_NOTYPE;
474         val->val_cat = NSC_NOCAT;
475         return val;
476     }
477
478     val->val_type = type;
479     val->val_cat = NSC_VAL;
480     val->val_as.lng = selval;
481     return val;
482 }
483
484 /*
485  * Return operator type for operand types LFT, RGT.
486  */
487 static int
488 nstr_optype(enum nsc_type lft, enum nsc_type rgt)
489 {
490     lft = nstr_promote(lft);
491     rgt = nstr_promote(rgt);
492     if (lft == rgt)
493         return lft;
494     if (lft == NSC_DOUBLE && rgt == NSC_LONG)
495         return NSC_DOUBLE;
496     if (rgt == NSC_DOUBLE && lft == NSC_LONG)
497         return NSC_DOUBLE;
498     return NSC_NOTYPE;
499 }
500
501 /*
502  * Compile a value in STR into VAL.
503  * Return a pointer to the first character after the value on success,
504  * NULL on error.
505  * TYPE is the context type, a file type.
506  * If STR names an array, VAL simply refers to the element with index
507  * zero.
508  */
509 char *
510 nstr_comp_val(char *str, struct valstr *val, int type)
511 {
512     struct castr *ca = ef_cadef(type);
513     char *tail = nstr_parse_val(str, val);
514     if (!nstr_resolve_id(val, ca, nstr_match_ca(val, ca)))
515         return NULL;
516     return tail;
517 }