]> git.pond.sub.org Git - empserver/blob - src/lib/subs/nstr.c
Virtual selectors
[empserver] / src / lib / subs / nstr.c
1 /*
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
5  *
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.
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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  nstr.c: compile and execute the item selections on sectors
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  *     Steve McClure, 1997
33  *     Markus Armbruster, 2004-2006
34  */
35
36 #include <config.h>
37
38 #include <ctype.h>
39 #include <limits.h>
40 #include "file.h"
41 #include "match.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_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
53 /*
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
59  * '?'.
60  */
61 int
62 nstr_comp(struct nscstr *np, int len, int type, char *str)
63 {
64     struct castr *ca = ef_cadef(type);
65     char *cond;
66     char *tail;
67     int i;
68     struct nscstr dummy;
69     int lft_caidx, rgt_caidx;
70     int lft_val, rgt_val;
71     int lft_type, rgt_type;
72
73     cond = str;
74     for (i = 0; ; ++i, ++np) {
75         if (i >= len)
76             np = &dummy;
77
78         /* left operand */
79         tail = nstr_parse_val(cond, &np->lft);
80         lft_caidx = nstr_match_ca(&np->lft, ca);
81
82         /* operator */
83         if (*tail != '<' && *tail != '=' && *tail != '>' && *tail != '#') {
84             if (*tail)
85                 pr("%s -- expected condition operator\n", cond);
86             else
87                 pr("%s -- missing condition operator\n", cond);
88             return -1;
89         }
90         np->operator = *tail;
91         ++tail;
92
93         /* right operand */
94         tail = nstr_parse_val(tail, &np->rgt);
95         rgt_caidx = nstr_match_ca(&np->rgt, ca);
96
97         /*
98          * Resolve identifiers
99          *
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
104          * n<newdes.
105          */
106         lft_val = nstr_match_val(&np->lft, ca, rgt_caidx);
107         rgt_val = nstr_match_val(&np->rgt, ca, lft_caidx);
108         /*
109          * if lft_val >= 0, then rhs names a selector and lhs names
110          * one of its values.  Likewise for rgt_val.
111          */
112         if (lft_val >= 0 && rgt_val >= 0) {
113             pr("%.*s -- condition ambiguous\n", (int)(tail-cond), cond);
114             return -1;
115         } else if (rgt_val >= 0) {
116             /* selector x value */
117             if (!nstr_resolve_sel(&np->lft, &ca[lft_caidx]))
118                 return -1;
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]))
124                 return -1;
125         } else {
126             /*
127              * Neither side works as selector value; any identifiers
128              * must name selectors.
129              */
130             if (!nstr_resolve_id(&np->lft, ca, lft_caidx,
131                                  nstr_string_ok(ca, rgt_caidx)))
132                 return -1;
133             if (!nstr_resolve_id(&np->rgt, ca, rgt_caidx,
134                                  nstr_string_ok(ca, lft_caidx)))
135                 return -1;
136         }
137
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;
154         } else {
155             if (!nstr_coerce_val(&np->lft, NSC_LONG, str)
156                 && !nstr_coerce_val(&np->rgt, NSC_LONG, str))
157                 np->optype = NSC_LONG;
158         }
159         if (np->optype == NSC_NOTYPE)
160             return -1;
161
162         /* another condition? */
163         if (*tail == 0)
164             break;
165         if (*tail != '&') {
166             pr("%s -- expected `&'\n", cond);
167             return -1;
168         }
169         cond = tail + 1;
170     }
171
172     if (i >= len) {
173         /* could just return I and let caller gripe or enlarge buffer */
174         pr("%s -- too many conditions\n", str);
175         return -1;
176     }
177
178     return i + 1;
179 }
180
181 /* Like strcmp(S1, S2), but limit length of S1 to SZ1 and of S2 to SZ2.  */
182 static int
183 strnncmp(char *s1, size_t sz1, char *s2, size_t sz2)
184 {
185     int res;
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];
190 }
191
192 #define EVAL(op, lft, rgt)                      \
193     ((op) == '<' ? (lft) < (rgt)                \
194      : (op) == '=' ? (lft) == (rgt)             \
195      : (op) == '>' ? (lft) > (rgt)              \
196      : (op) == '#' ? (lft) != (rgt)             \
197      : (CANT_REACH(), 0))
198
199 /*
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
203  * the conditions.
204  */
205 int
206 nstr_exec(struct nscstr *np, int ncond, void *ptr)
207 {
208     int i, op, optype, cmp;
209     struct valstr lft, rgt;
210
211     for (i = 0; i < ncond; ++i) {
212         op = np[i].operator;
213         optype = np[i].optype;
214         if (np[i].lft.val_cat == NSC_NOCAT || np[i].rgt.val_cat == NSC_NOCAT)
215             return 0;
216         lft = np[i].lft;
217         nstr_exec_val(&lft, player->cnum, ptr, optype);
218         rgt = np[i].rgt;
219         nstr_exec_val(&rgt, player->cnum, ptr, optype);
220         switch (optype) {
221         case NSC_LONG:
222             if (!EVAL(op, lft.val_as.lng, rgt.val_as.lng))
223                 return 0;
224             break;
225         case NSC_DOUBLE:
226             if (!EVAL(op, lft.val_as.dbl, rgt.val_as.dbl))
227                 return 0;
228             break;
229         case NSC_STRING:
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))
233                 return 0;
234             break;
235         default:
236             CANT_REACH();
237             return 0;
238         }
239     }
240
241     return 1;
242 }
243
244 /*
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,
248  * or an identifier.
249  */
250 static char *
251 nstr_parse_val(char *str, struct valstr *val)
252 {
253     long l;
254     double d;
255     char *tail, *tail2;
256
257     /* string */
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);
265         if (*tail) ++tail;
266         /* FIXME else unclosed string */
267         return tail;
268     }
269
270     /* identifier */
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;
277         return tail;
278     }
279
280     /* number */
281     l = strtol(str, &tail, 0);
282     d = strtod(str, &tail2);
283     if (tail2 > tail) {
284         val->val_type = NSC_DOUBLE;
285         val->val_cat = NSC_VAL;
286         val->val_as.dbl = d;
287         return tail2;
288     }
289     if (tail != str) {
290         val->val_type = NSC_LONG;
291         val->val_cat = NSC_VAL;
292         val->val_as.lng = l;
293         return tail;
294     }
295
296     /* funny character, interpret as identifier */
297     tail = str+1;
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;
302     return tail;
303 }
304
305 /*
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
308  * several.
309  * A VAL that is not an identifier doesn't match anything.  A null CA
310  * is considered empty.
311  */
312 static int
313 nstr_match_ca(struct valstr *val, struct castr *ca)
314 {
315     char id[32];
316
317     if (val->val_cat != NSC_ID || val->val_as.str.maxsz >= sizeof(id))
318         return M_NOTFOUND;
319
320     if (!ca)
321         return M_NOTFOUND;
322
323     memcpy(id, val->val_as.str.base, val->val_as.str.maxsz);
324     id[val->val_as.str.maxsz] = 0;
325
326     return stmtch(id, ca, offsetof(struct castr, ca_name),
327                   sizeof(struct castr));
328 }
329
330 /*
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
335  * several.
336  */
337 static int
338 nstr_match_val(struct valstr *val, struct castr *ca, int idx)
339 {
340     char id[32];
341
342     if (val->val_cat != NSC_ID || val->val_as.str.maxsz >= sizeof(id))
343         return M_NOTFOUND;
344
345     if (idx < 0 || ca[idx].ca_table == EF_BAD)
346         return M_NOTFOUND;
347     if (CANT_HAPPEN(nstr_promote(ca[idx].ca_type) != NSC_LONG))
348         return M_NOTFOUND;
349
350     memcpy(id, val->val_as.str.base, val->val_as.str.maxsz);
351     id[val->val_as.str.maxsz] = 0;
352
353     return ef_elt_byname(ca[idx].ca_table, id);
354 }
355
356 /*
357  * Can CA[IDX] be compared to a string?
358  * Return 0 for negative IDX.
359  */
360 static int
361 nstr_string_ok(struct castr *ca, int idx)
362 {
363     return idx >= 0 && nstr_promote(ca[idx].ca_type) == NSC_STRING;
364 }
365
366 /*
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
370  * follows.
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.
374  */
375 static struct valstr *
376 nstr_resolve_id(struct valstr *val, struct castr *ca, int idx, int string_ok)
377 {
378     if (val->val_cat != NSC_ID)
379         return val;
380
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;
385         return NULL;
386     }
387
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;
392         return NULL;
393     }
394
395     if (idx < 0) {
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;
403         return val;
404     }
405
406     return nstr_resolve_sel(val, &ca[idx]);
407 }
408
409 /*
410  * Change VAL to resolve identifier to selector CA.
411  * Return VAL on success, NULL if the player is denied access to the
412  * selector.
413  * VAL must be an identifier.
414  */
415 static struct valstr *
416 nstr_resolve_sel(struct valstr *val, struct castr *ca)
417 {
418     if (CANT_HAPPEN(val->val_cat != NSC_ID)) {
419         val->val_cat = NSC_NOCAT;
420         return val;
421     }
422
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;
427         return NULL;
428     }
429
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;
435     val->val_as.sym.get = ca->ca_get;
436     return val;
437 }
438
439 /*
440  * Initialize VAL to value SELVAL for selector CA, return VAL.
441  */
442 static struct valstr *
443 nstr_mkselval(struct valstr *val, int selval, struct castr *ca)
444 {
445     if (CANT_HAPPEN(nstr_promote(ca->ca_type) != NSC_LONG
446                     || ca->ca_table == EF_BAD)) {
447         val->val_type = NSC_NOTYPE;
448         val->val_cat = NSC_NOCAT;
449         return val;
450     }
451
452     val->val_type = ca->ca_type;
453     val->val_cat = NSC_VAL;
454     val->val_as.lng = selval;
455     return val;
456 }
457
458 /*
459  * Compile a value in STR into VAL.
460  * Return a pointer to the first character after the value on success,
461  * NULL on error.
462  * TYPE is the context type, a file type.
463  * If STR names an array, VAL simply refers to the element with index
464  * zero.
465  */
466 char *
467 nstr_comp_val(char *str, struct valstr *val, int type)
468 {
469     struct castr *ca = ef_cadef(type);
470     char *tail = nstr_parse_val(str, val);
471     if (!nstr_resolve_id(val, ca, nstr_match_ca(val, ca), 0))
472         return NULL;
473     return tail;
474 }
475
476 static int
477 cond_type_mismatch(char *str)
478 {
479     if (str)
480         pr("%s -- condition operand type mismatch\n", str);
481     return -1;
482 }
483
484 /*
485  * Coerce VAL to promoted value type TO.
486  * Return 0 on success, -1 on error.
487  * If VAL is evaluated, convert it, else only check.
488  * STR is the condition text to be used for error messages.  Suppress
489  * messages if it is a null pointer.
490  */
491 int
492 nstr_coerce_val(struct valstr *val, nsc_type to, char *str)
493 {
494     /* FIXME get rid of promotion?  */
495     nsc_type from = nstr_promote(val->val_type);
496
497     if (from == NSC_NOTYPE)
498         return 0;
499
500     if (from != to) {
501         switch (to) {
502         case NSC_STRING:
503             return cond_type_mismatch(str); /* FIXME implement */
504         case NSC_DOUBLE:
505             if (from == NSC_LONG) {
506                 if (val->val_cat == NSC_VAL)
507                     val->val_as.dbl = val->val_as.lng;
508             } else
509                 return cond_type_mismatch(str);
510             break;
511         case NSC_LONG:
512             return cond_type_mismatch(str);
513         default:
514             CANT_REACH();
515             to = from;
516         }
517     }
518
519     if (val->val_cat == NSC_VAL) {
520         /* unimplemented conversions; don't currently occur here */
521         CANT_HAPPEN(val->val_type == NSC_XCOORD
522                     || val->val_type == NSC_YCOORD
523                     || val->val_type == NSC_HIDDEN);
524         val->val_type = to;
525     }
526
527     return 0;
528 }