]> 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-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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
34  */
35
36 #include <limits.h>
37 #include "misc.h"
38 #include "file.h"
39 #include "match.h"
40 #include "nsc.h"
41 #include "prototypes.h"
42
43 static char *nstr_parse_val(char *, struct valstr *);
44 static int nstr_match_ca(struct valstr *, struct castr *);
45 static int nstr_match_val(struct valstr *, int, struct castr *, int);
46 static struct valstr *nstr_resolve_sel(struct valstr *, struct castr *);
47 static struct valstr *nstr_mkselval(struct valstr *, int, struct castr *);
48 static struct valstr *nstr_resolve_id(struct valstr *, struct castr *, int);
49 static int nstr_promote(int);
50
51
52 /*
53  * Compile conditions into array NP[LEN].
54  * Return number of conditions, or -1 on error.
55  * It is an error if there are more than LEN conditions.
56  * TYPE is the context type, a file type.
57  * STR is the condition string, in Empire syntax, without the leading
58  * '?'.
59  */
60 int
61 nstr_comp(struct nscstr *np, int len, int type, char *str)
62 {
63     struct castr *ca = ef_cadef(type);
64     char *cond;
65     char *tail;
66     int i;
67     struct nscstr dummy;
68     int lft_caidx, rgt_caidx;
69     int lft_val, rgt_val;
70     int lft_type, rgt_type;
71
72     cond = str;
73     for (i = 0; ; ++i, ++np) {
74         if (i >= len)
75             np = &dummy;
76
77         /* left operand */
78         tail = nstr_parse_val(cond, &np->lft);
79         lft_caidx = nstr_match_ca(&np->lft, ca);
80
81         /* operator */
82         if (*tail != '<' && *tail != '=' && *tail != '>' && *tail != '#') {
83             if (*tail)
84                 pr("%s -- expected condition operator\n", cond);
85             else
86                 pr("%s -- missing condition operator\n", cond);
87             return -1;
88         }
89         np->operator = *tail;
90         ++tail;
91
92         /* right operand */
93         tail = nstr_parse_val(tail, &np->rgt);
94         rgt_caidx = nstr_match_ca(&np->rgt, ca);
95
96         /*
97          * Resolve identifiers
98          *
99          * An identifier can name a selector or, if the other operand
100          * is a selector, a value for that.  The condition is
101          * ambiguous if both selector x value and value x selector are
102          * possible.  Example: n<n for sectors could mean newdes<n or
103          * n<newdes.
104          */
105         lft_val = nstr_match_val(&np->lft, type, ca, rgt_caidx);
106         rgt_val = nstr_match_val(&np->rgt, type, ca, lft_caidx);
107         /*
108          * if lft_val >= 0, then rhs names a selector and lhs names
109          * one of its values.  Likewise for rgt_val.
110          */
111         if (lft_val >= 0 && rgt_val >= 0) {
112             pr("%.*s -- condition ambiguous\n", (int)(tail-cond), cond);
113             return -1;
114         } else if (rgt_val >= 0) {
115             /* selector x value */
116             if (!nstr_resolve_sel(&np->lft, &ca[lft_caidx]))
117                 return -1;
118             nstr_mkselval(&np->rgt, rgt_val, &ca[lft_caidx]);
119         } else if (lft_val >= 0) {
120             /* value x selector */
121             nstr_mkselval(&np->lft, lft_val, &ca[rgt_caidx]);
122             if (!nstr_resolve_sel(&np->rgt, &ca[rgt_caidx]))
123                 return -1;
124         } else {
125             /*
126              * Neither side works as selector value; any identifiers
127              * must name selectors.
128              */
129             if (!nstr_resolve_id(&np->lft, ca, lft_caidx))
130                 return -1;
131             if (!nstr_resolve_id(&np->rgt, ca, rgt_caidx))
132                 return -1;
133         }
134
135         /* find operator type, coerce operands */
136         lft_type = nstr_promote(np->lft.val_type);
137         rgt_type = nstr_promote(np->rgt.val_type);
138         np->optype = NSC_NOTYPE;
139         if (lft_type == NSC_TYPEID) {
140             if (!nstr_coerce_val(&np->rgt, NSC_TYPEID, str))
141                 np->optype = NSC_TYPEID;
142         } else if (rgt_type == NSC_TYPEID) {
143             if (!nstr_coerce_val(&np->lft, NSC_TYPEID, str))
144                 np->optype = NSC_TYPEID;
145         } else if (lft_type == NSC_STRING) {
146             if (!nstr_coerce_val(&np->rgt, NSC_STRING, str))
147                 np->optype = NSC_STRING;
148         } else if (rgt_type == NSC_STRING) {
149             if (!nstr_coerce_val(&np->lft, NSC_STRING, str))
150                 np->optype = NSC_STRING;
151         } else if (lft_type == NSC_DOUBLE) {
152             if (!nstr_coerce_val(&np->rgt, NSC_DOUBLE, str))
153                 np->optype = NSC_DOUBLE;
154         } else if (rgt_type == NSC_DOUBLE) {
155             if (!nstr_coerce_val(&np->lft, NSC_DOUBLE, str))
156                 np->optype = NSC_DOUBLE;
157         } else {
158             if (!nstr_coerce_val(&np->lft, NSC_LONG, str)
159                 && !nstr_coerce_val(&np->rgt, NSC_LONG, str))
160                 np->optype = NSC_LONG;
161         }
162         if (np->optype == NSC_NOTYPE)
163             return -1;
164
165         /* another condition? */
166         if (*tail == 0)
167             break;
168         if (*tail != '&') {
169             pr("%s -- expected `&'\n", cond);
170             return -1;
171         }
172         cond = tail + 1;
173     }
174
175     if (i >= len) {
176         /* could just return I and let caller gripe or enlarge buffer */
177         pr("%s -- too many conditions\n", str);
178         return -1;
179     }
180
181     return i + 1;
182 }
183
184 /* Like strcmp(S1, S2), but limit length of S1 to SZ1 and of S2 to SZ2.  */
185 static int
186 strnncmp(char *s1, size_t sz1, char *s2, size_t sz2)
187 {
188     int res;
189     if (sz1 == sz2) return strncmp(s1, s2, sz2);
190     if (sz1 < sz2) return -strnncmp(s2, sz2, s1, sz1);
191     res = strncmp(s1, s2, sz2);
192     return res ? res : s1[sz2];
193 }
194
195 #define EVAL(op, lft, rgt)                      \
196     ((op) == '<' ? (lft) < (rgt)                \
197      : (op) == '=' ? (lft) == (rgt)             \
198      : (op) == '>' ? (lft) > (rgt)              \
199      : (op) == '#' ? (lft) != (rgt)             \
200      : 0)
201
202 /*
203  * Evaluate compiled conditions in array NP[NCOND].
204  * Return non-zero iff they are all true.
205  * PTR points to a context object of the type that was used to compile
206  * the conditions.
207  */
208 int
209 nstr_exec(struct nscstr *np, int ncond, void *ptr)
210 {
211     int i, op, optype, cmp;
212     struct valstr lft, rgt;
213
214     for (i = 0; i < ncond; ++i) {
215         op = np[i].operator;
216         optype = np[i].optype;
217         if (np[i].lft.val_cat == NSC_NOCAT || np[i].rgt.val_cat == NSC_NOCAT)
218             return 0;
219         lft = np[i].lft;
220         nstr_exec_val(&lft, player->cnum, ptr, optype);
221         rgt = np[i].rgt;
222         nstr_exec_val(&rgt, player->cnum, ptr, optype);
223         switch (optype) {
224         case NSC_TYPEID:
225         case NSC_LONG:
226             if (!EVAL(op, lft.val_as.lng, rgt.val_as.lng))
227                 return 0;
228             break;
229         case NSC_DOUBLE:
230             if (!EVAL(op, lft.val_as.dbl, rgt.val_as.dbl))
231                 return 0;
232             break;
233         case NSC_STRING:
234             cmp = strnncmp(lft.val_as.str.base, lft.val_as.str.maxsz,
235                            rgt.val_as.str.base, rgt.val_as.str.maxsz);
236             if (!EVAL(op, cmp, 0))
237                 return 0;
238             break;
239         default:
240             CANT_HAPPEN("bad OPTYPE");
241             return 0;
242         }
243     }
244
245     return 1;
246 }
247
248 /*
249  * Parse a value in STR into VAL.
250  * Return a pointer to the first character after the value.
251  * Value is either evaluated (but not NSC_TYPEID) or an identifier.
252  */
253 static char *
254 nstr_parse_val(char *str, struct valstr *val)
255 {
256     long l;
257     double d;
258     char *tail, *tail2;
259
260     /* string */
261     if (str[0] == '\'') {
262         for (tail = str + 1; *tail && *tail != '\''; ++tail) ;
263         /* FIXME implement \ quoting */
264         val->val_type = NSC_STRING;
265         val->val_cat = NSC_VAL;
266         val->val_as.str.base = str + 1;
267         val->val_as.str.maxsz = tail - (str + 1);
268         if (*tail) ++tail;
269         return tail;
270     }
271
272     /* number */
273     l = strtol(str, &tail, 0);
274     d = strtod(str, &tail2);
275     if (tail2 > tail) {
276         val->val_type = NSC_DOUBLE;
277         val->val_cat = NSC_VAL;
278         val->val_as.dbl = d;
279         return tail2;
280     }
281     if (tail != str) {
282         val->val_type = NSC_LONG;
283         val->val_cat = NSC_VAL;
284         val->val_as.lng = l;
285         return tail;
286     }
287
288     /* identifier */
289     for (tail = str; isalnum(*tail) || *tail == '_'; ++tail) ;
290     if (tail == str) ++tail;
291     val->val_type = NSC_NOTYPE;
292     val->val_cat = NSC_ID;
293     val->val_as.str.base = str;
294     val->val_as.str.maxsz = tail - str;
295     return tail;
296 }
297
298 /*
299  * Match VAL in table of selector descriptors CA, return index.
300  * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
301  * several.
302  * A VAL that is not an identifier doesn't match anything.  A null CA
303  * is considered empty.
304  */
305 static int
306 nstr_match_ca(struct valstr *val, struct castr *ca)
307 {
308     char id[32];
309
310     if (val->val_cat != NSC_ID || val->val_as.str.maxsz >= sizeof(id))
311         return M_NOTFOUND;
312
313     if (!ca)
314         return M_NOTFOUND;
315
316     memcpy(id, val->val_as.str.base, val->val_as.str.maxsz);
317     id[val->val_as.str.maxsz] = 0;
318
319     return stmtch(id, ca, offsetof(struct castr, ca_name),
320                   sizeof(struct castr));
321 }
322
323 /*
324  * Match VAL in a selector's values, return its (non-negative) value.
325  * TYPE is the context type, a file type.
326  * CA is ef_cadef(TYPE).
327  * Match values of selector descriptor CA[IDX], provided CA is not
328  * null and IDX is not negative.
329  * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
330  * several.
331  * TODO: This is just a stub and works only for NSC_TYPEID.
332  * Generalize: give struct castr enough info to find values, remove
333  * parameter `type'.
334  */
335 static int
336 nstr_match_val(struct valstr *val, int type, struct castr *ca, int idx)
337 {
338     char id[32];
339
340     if (val->val_cat != NSC_ID || val->val_as.str.maxsz >= sizeof(id))
341         return M_NOTFOUND;
342
343     if (idx < 0 || ca[idx].ca_type != NSC_TYPEID)
344         return M_NOTFOUND;
345
346     memcpy(id, val->val_as.str.base, val->val_as.str.maxsz);
347     id[val->val_as.str.maxsz] = 0;
348
349     return typematch(id, type);
350 }
351
352 /*
353  * Change VAL to resolve identifier to selector or string.
354  * Return VAL on success, NULL on error.
355  * No change if VAL is not an identifier.
356  * Else error if IDX == M_NOTUNIQUE, string if IDX == M_NOTFOUND, and
357  * selector CA[IDX] otherwise.
358  */
359 static struct valstr *
360 nstr_resolve_id(struct valstr *val, struct castr *ca, int idx)
361 {
362     if (val->val_cat != NSC_ID)
363         return val;
364
365     if (idx == M_NOTUNIQUE) {
366         pr("%.*s -- ambiguous name\n",
367            (int)val->val_as.str.maxsz, val->val_as.str.base);
368         val->val_cat = NSC_NOCAT;
369         return NULL;
370     }
371
372     if (idx == M_NOTFOUND) {
373         /* interpret unbound identifier as string */
374         val->val_type = NSC_STRING;
375         val->val_cat = NSC_VAL;
376         return val;
377     }
378
379     return nstr_resolve_sel(val, &ca[idx]);
380 }
381
382 /*
383  * Change VAL to resolve identifier to selector CA.
384  * Return VAL on success, NULL if the player is denied access to the
385  * selector.
386  * VAL must be an identifier.
387  */
388 static struct valstr *
389 nstr_resolve_sel(struct valstr *val, struct castr *ca)
390 {
391     if (CANT_HAPPEN(val->val_cat != NSC_ID)) {
392         val->val_cat = NSC_NOCAT;
393         return val;
394     }
395
396     if ((ca->ca_flags & NSC_DEITY) && !player->god) {
397         pr("%.*s -- not accessible to mortals\n",
398            (int)val->val_as.str.maxsz, val->val_as.str.base);
399         val->val_cat = NSC_NOCAT;
400         return NULL;
401     }
402
403     val->val_type = ca->ca_type;
404     val->val_cat = NSC_OFF;
405     val->val_as.sym.off = ca->ca_off;
406     val->val_as.sym.len = ca->ca_len;
407     val->val_as.sym.idx = 0;
408     return val;
409 }
410
411 /*
412  * Initialize VAL to value SELVAL for selector CA, return VAL.
413  */
414 static struct valstr *
415 nstr_mkselval(struct valstr *val, int selval, struct castr *ca)
416 {
417     if (CANT_HAPPEN(ca->ca_type != NSC_TYPEID)) {
418         val->val_type = NSC_NOTYPE;
419         val->val_cat = NSC_NOCAT;
420         return val;
421     }
422
423     val->val_type = ca->ca_type;
424     val->val_cat = NSC_VAL;
425     val->val_as.lng = selval;
426     return val;
427 }
428
429 /*
430  * Compile a value in STR into VAL.
431  * Return a pointer to the first character after the value on success,
432  * NULL on error.
433  * TYPE is the context type, a file type.
434  * If STR names an array, VAL simply refers to the element with index
435  * zero.
436  */
437 char *
438 nstr_comp_val(char *str, struct valstr *val, int type)
439 {
440     struct castr *ca = ef_cadef(type);
441     char *tail = nstr_parse_val(str, val);
442     return nstr_resolve_id(val, ca, nstr_match_ca(val, ca)) ? tail : NULL;
443 }
444
445
446 /*
447  * Promote VALTYPE.
448  * If VALTYPE is an integer type, return NSC_LONG.
449  * If VALTYPE is a floating-point type, return NSC_DOUBLE.
450  * If VALTYPE is NSC_STRINGY, return NSC_STRING.
451  * If VALTYPE is NSC_NOTYPE, NSC_STRING or NSC_TYPEID, return VALTYPE.
452  */
453 static int
454 nstr_promote(int valtype)
455 {
456     switch (valtype) {
457     case NSC_NOTYPE:
458     case NSC_LONG:
459     case NSC_DOUBLE:
460     case NSC_STRING:
461     case NSC_TYPEID:
462         break;
463     case NSC_CHAR:
464     case NSC_UCHAR:
465     case NSC_SHORT:
466     case NSC_USHORT:
467     case NSC_INT:
468     case NSC_XCOORD:
469     case NSC_YCOORD:
470     case NSC_TIME:
471         valtype = NSC_LONG;
472         break;
473     case NSC_FLOAT:
474         valtype = NSC_DOUBLE;
475         break;
476     case NSC_STRINGY:
477         valtype = NSC_STRING;
478         break;
479     default:
480         CANT_HAPPEN("bad VALTYPE");
481         valtype = NSC_NOTYPE;
482     }
483     return valtype;
484 }
485
486 static int
487 cond_type_mismatch(char *str)
488 {
489     if (str)
490         pr("%s -- condition operand type mismatch\n", str);
491     return -1;
492 }
493
494 /*
495  * Coerce VAL to promoted value type TO.
496  * Return 0 on success, -1 on error.
497  * If VAL is evaluated, convert it, else only check.
498  * STR is the condition text to be used for error messages.  Suppress
499  * messages if it is a null pointer.
500  */
501 int
502 nstr_coerce_val(struct valstr *val, nsc_type to, char *str)
503 {
504     /* FIXME get rid of promotion?  */
505     nsc_type from = nstr_promote(val->val_type);
506
507     if (from == NSC_NOTYPE)
508         return 0;
509
510     if (from != to) {
511         switch (to) {
512         case NSC_TYPEID:
513             return cond_type_mismatch(str);
514         case NSC_STRING:
515             return cond_type_mismatch(str); /* FIXME implement */
516         case NSC_DOUBLE:
517             if (from == NSC_LONG) {
518                 if (val->val_cat == NSC_VAL)
519                     val->val_as.dbl = val->val_as.lng;
520             } else
521                 return cond_type_mismatch(str);
522             break;
523         case NSC_LONG:
524             return cond_type_mismatch(str);
525         default:
526             CANT_HAPPEN("bad TO argument");
527             to = from;
528         }
529     }
530
531     if (val->val_cat == NSC_VAL) {
532         /* coord literals don't occur, conversion not implemented */
533         CANT_HAPPEN(val->val_type == NSC_XCOORD
534                     || val->val_type == NSC_YCOORD);
535         val->val_type = to;
536     }
537
538     return 0;
539 }
540
541 /*
542  * Evaluate VAL.
543  * If VAL is symbolic, evaluate it into a promoted value type.
544  * Use coordinate system of country CNUM.
545  * PTR points to a context object of the type that was used to compile
546  * the value.
547  * Unless WANT is NSC_NOTYPE, coerce the value to promoted value type
548  * WANT.  VAL must be coercible.  That's the case if a previous
549  * nstr_coerce_val(VAL, WANT, STR) succeeded.
550  */
551 void
552 nstr_exec_val(struct valstr *val, natid cnum, void *ptr, nsc_type want)
553 {
554     char *memb_ptr;
555     nsc_type valtype;
556     int idx;
557
558     switch (val->val_cat) {
559     default:
560         CANT_HAPPEN("Bad VAL category");
561         /* fall through */
562     case NSC_VAL:
563         valtype = val->val_type;
564         break;
565     case NSC_OFF:
566         valtype = NSC_LONG;
567         memb_ptr = ptr;
568         memb_ptr += val->val_as.sym.off;
569         idx = val->val_as.sym.idx;
570         switch (val->val_type) {
571         case NSC_CHAR:
572             val->val_as.lng = ((signed char *)memb_ptr)[idx];
573             break;
574         case NSC_UCHAR:
575             val->val_as.lng = ((unsigned char *)memb_ptr)[idx];
576             break;
577         case NSC_SHORT:
578             val->val_as.lng = ((short *)memb_ptr)[idx];
579             break;
580         case NSC_USHORT:
581             val->val_as.lng = ((unsigned short *)memb_ptr)[idx];
582             break;
583         case NSC_INT:
584             val->val_as.lng = ((int *)memb_ptr)[idx];
585             break;
586         case NSC_LONG:
587             val->val_as.lng = ((long *)memb_ptr)[idx];
588             break;
589         case NSC_XCOORD:
590             val->val_as.lng = xrel(getnatp(cnum), ((short *)memb_ptr)[idx]);
591             break;
592         case NSC_YCOORD:
593             val->val_as.lng = yrel(getnatp(cnum), ((short *)memb_ptr)[idx]);
594             break;
595         case NSC_FLOAT:
596             val->val_as.dbl = ((float *)memb_ptr)[idx];
597             valtype = NSC_DOUBLE;
598             break;
599         case NSC_DOUBLE:
600             val->val_as.dbl = ((double *)memb_ptr)[idx];
601             valtype = NSC_DOUBLE;
602             break;
603         case NSC_STRINGY:
604             CANT_HAPPEN(idx);
605             val->val_as.str.maxsz = val->val_as.sym.len;
606             val->val_as.str.base = (char *)memb_ptr;
607             valtype = NSC_STRING;
608             break;
609         case NSC_STRING:
610             val->val_as.str.base = ((char **)memb_ptr)[idx];
611             val->val_as.str.maxsz = INT_MAX;
612             valtype = NSC_STRING;
613             break;
614         case NSC_TIME:
615             val->val_as.lng = ((time_t *)memb_ptr)[idx];
616             break;
617         case NSC_TYPEID:
618             val->val_as.lng = ((signed char *)memb_ptr)[idx];
619             valtype = NSC_TYPEID;
620             break;
621         default:
622             CANT_HAPPEN("Bad VAL type");
623             val->val_as.lng = 0;
624         }
625         val->val_cat = NSC_VAL;
626     }
627
628     if (valtype == want)
629         ;
630     else if (want == NSC_DOUBLE) {
631         if (valtype == NSC_LONG) {
632             valtype = want;
633             val->val_as.dbl = val->val_as.lng;
634         }
635     } else if (want == NSC_STRING)
636         CANT_HAPPEN("unimplemented WANT"); /* FIXME */
637
638     if (CANT_HAPPEN(valtype != want && want != NSC_NOTYPE)) {
639         valtype = want;
640         switch (want) {
641         case NSC_TYPEID:
642         case NSC_LONG: val->val_as.lng = 0; break;
643         case NSC_DOUBLE: val->val_as.dbl = 0.0; break;
644         case NSC_STRING: val->val_as.str.base = NULL; break;
645         default:
646             CANT_HAPPEN("bad WANT argument");
647         }
648     }
649
650     val->val_type = valtype;
651 }