]> git.pond.sub.org Git - empserver/blob - src/lib/subs/nstr.c
Clean up library dependencies
[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 static int nstr_promote(int);
53
54
55 /*
56  * Compile conditions into array NP[LEN].
57  * Return number of conditions, or -1 on error.
58  * It is an error if there are more than LEN conditions.
59  * TYPE is the context type, a file type.
60  * STR is the condition string, in Empire syntax, without the leading
61  * '?'.
62  */
63 int
64 nstr_comp(struct nscstr *np, int len, int type, char *str)
65 {
66     struct castr *ca = ef_cadef(type);
67     char *cond;
68     char *tail;
69     int i;
70     struct nscstr dummy;
71     int lft_caidx, rgt_caidx;
72     int lft_val, rgt_val;
73     int lft_type, rgt_type;
74
75     cond = str;
76     for (i = 0; ; ++i, ++np) {
77         if (i >= len)
78             np = &dummy;
79
80         /* left operand */
81         tail = nstr_parse_val(cond, &np->lft);
82         lft_caidx = nstr_match_ca(&np->lft, ca);
83
84         /* operator */
85         if (*tail != '<' && *tail != '=' && *tail != '>' && *tail != '#') {
86             if (*tail)
87                 pr("%s -- expected condition operator\n", cond);
88             else
89                 pr("%s -- missing condition operator\n", cond);
90             return -1;
91         }
92         np->operator = *tail;
93         ++tail;
94
95         /* right operand */
96         tail = nstr_parse_val(tail, &np->rgt);
97         rgt_caidx = nstr_match_ca(&np->rgt, ca);
98
99         /*
100          * Resolve identifiers
101          *
102          * An identifier can name a selector or, if the other operand
103          * is a selector, a value for that.  The condition is
104          * ambiguous if both selector x value and value x selector are
105          * possible.  Example: n<n for sectors could mean newdes<n or
106          * n<newdes.
107          */
108         lft_val = nstr_match_val(&np->lft, ca, rgt_caidx);
109         rgt_val = nstr_match_val(&np->rgt, ca, lft_caidx);
110         /*
111          * if lft_val >= 0, then rhs names a selector and lhs names
112          * one of its values.  Likewise for rgt_val.
113          */
114         if (lft_val >= 0 && rgt_val >= 0) {
115             pr("%.*s -- condition ambiguous\n", (int)(tail-cond), cond);
116             return -1;
117         } else if (rgt_val >= 0) {
118             /* selector x value */
119             if (!nstr_resolve_sel(&np->lft, &ca[lft_caidx]))
120                 return -1;
121             nstr_mkselval(&np->rgt, rgt_val, &ca[lft_caidx]);
122         } else if (lft_val >= 0) {
123             /* value x selector */
124             nstr_mkselval(&np->lft, lft_val, &ca[rgt_caidx]);
125             if (!nstr_resolve_sel(&np->rgt, &ca[rgt_caidx]))
126                 return -1;
127         } else {
128             /*
129              * Neither side works as selector value; any identifiers
130              * must name selectors.
131              */
132             if (!nstr_resolve_id(&np->lft, ca, lft_caidx,
133                                  nstr_string_ok(ca, rgt_caidx)))
134                 return -1;
135             if (!nstr_resolve_id(&np->rgt, ca, rgt_caidx,
136                                  nstr_string_ok(ca, lft_caidx)))
137                 return -1;
138         }
139
140         /* find operator type, coerce operands */
141         lft_type = nstr_promote(np->lft.val_type);
142         rgt_type = nstr_promote(np->rgt.val_type);
143         np->optype = NSC_NOTYPE;
144         if (lft_type == NSC_STRING) {
145             if (!nstr_coerce_val(&np->rgt, NSC_STRING, str))
146                 np->optype = NSC_STRING;
147         } else if (rgt_type == NSC_STRING) {
148             if (!nstr_coerce_val(&np->lft, NSC_STRING, str))
149                 np->optype = NSC_STRING;
150         } else if (lft_type == NSC_DOUBLE) {
151             if (!nstr_coerce_val(&np->rgt, NSC_DOUBLE, str))
152                 np->optype = NSC_DOUBLE;
153         } else if (rgt_type == NSC_DOUBLE) {
154             if (!nstr_coerce_val(&np->lft, NSC_DOUBLE, str))
155                 np->optype = NSC_DOUBLE;
156         } else {
157             if (!nstr_coerce_val(&np->lft, NSC_LONG, str)
158                 && !nstr_coerce_val(&np->rgt, NSC_LONG, str))
159                 np->optype = NSC_LONG;
160         }
161         if (np->optype == NSC_NOTYPE)
162             return -1;
163
164         /* another condition? */
165         if (*tail == 0)
166             break;
167         if (*tail != '&') {
168             pr("%s -- expected `&'\n", cond);
169             return -1;
170         }
171         cond = tail + 1;
172     }
173
174     if (i >= len) {
175         /* could just return I and let caller gripe or enlarge buffer */
176         pr("%s -- too many conditions\n", str);
177         return -1;
178     }
179
180     return i + 1;
181 }
182
183 /* Like strcmp(S1, S2), but limit length of S1 to SZ1 and of S2 to SZ2.  */
184 static int
185 strnncmp(char *s1, size_t sz1, char *s2, size_t sz2)
186 {
187     int res;
188     if (sz1 == sz2) return strncmp(s1, s2, sz2);
189     if (sz1 < sz2) return -strnncmp(s2, sz2, s1, sz1);
190     res = strncmp(s1, s2, sz2);
191     return res ? res : s1[sz2];
192 }
193
194 #define EVAL(op, lft, rgt)                      \
195     ((op) == '<' ? (lft) < (rgt)                \
196      : (op) == '=' ? (lft) == (rgt)             \
197      : (op) == '>' ? (lft) > (rgt)              \
198      : (op) == '#' ? (lft) != (rgt)             \
199      : 0)
200
201 /*
202  * Evaluate compiled conditions in array NP[NCOND].
203  * Return non-zero iff they are all true.
204  * PTR points to a context object of the type that was used to compile
205  * the conditions.
206  */
207 int
208 nstr_exec(struct nscstr *np, int ncond, void *ptr)
209 {
210     int i, op, optype, cmp;
211     struct valstr lft, rgt;
212
213     for (i = 0; i < ncond; ++i) {
214         op = np[i].operator;
215         optype = np[i].optype;
216         if (np[i].lft.val_cat == NSC_NOCAT || np[i].rgt.val_cat == NSC_NOCAT)
217             return 0;
218         lft = np[i].lft;
219         nstr_exec_val(&lft, player->cnum, ptr, optype);
220         rgt = np[i].rgt;
221         nstr_exec_val(&rgt, player->cnum, ptr, optype);
222         switch (optype) {
223         case NSC_LONG:
224             if (!EVAL(op, lft.val_as.lng, rgt.val_as.lng))
225                 return 0;
226             break;
227         case NSC_DOUBLE:
228             if (!EVAL(op, lft.val_as.dbl, rgt.val_as.dbl))
229                 return 0;
230             break;
231         case NSC_STRING:
232             cmp = strnncmp(lft.val_as.str.base, lft.val_as.str.maxsz,
233                            rgt.val_as.str.base, rgt.val_as.str.maxsz);
234             if (!EVAL(op, cmp, 0))
235                 return 0;
236             break;
237         default:
238             CANT_REACH();
239             return 0;
240         }
241     }
242
243     return 1;
244 }
245
246 /*
247  * Parse a value in STR into VAL.
248  * Return a pointer to the first character after the value.
249  * Value is either evaluated into NSC_STRING, NSC_DOUBLE or NSC_LONG,
250  * or an identifier.
251  */
252 static char *
253 nstr_parse_val(char *str, struct valstr *val)
254 {
255     long l;
256     double d;
257     char *tail, *tail2;
258
259     /* string */
260     if (str[0] == '\'') {
261         for (tail = str + 1; *tail && *tail != '\''; ++tail) ;
262         /* FIXME implement \ quoting */
263         val->val_type = NSC_STRING;
264         val->val_cat = NSC_VAL;
265         val->val_as.str.base = str + 1;
266         val->val_as.str.maxsz = tail - (str + 1);
267         if (*tail) ++tail;
268         /* FIXME else unclosed string */
269         return tail;
270     }
271
272     /* identifier */
273     if (isalpha(str[0])) {
274         for (tail = str+1; isalnum(*tail) || *tail == '_'; ++tail) ;
275         val->val_type = NSC_NOTYPE;
276         val->val_cat = NSC_ID;
277         val->val_as.str.base = str;
278         val->val_as.str.maxsz = tail - str;
279         return tail;
280     }
281
282     /* number */
283     l = strtol(str, &tail, 0);
284     d = strtod(str, &tail2);
285     if (tail2 > tail) {
286         val->val_type = NSC_DOUBLE;
287         val->val_cat = NSC_VAL;
288         val->val_as.dbl = d;
289         return tail2;
290     }
291     if (tail != str) {
292         val->val_type = NSC_LONG;
293         val->val_cat = NSC_VAL;
294         val->val_as.lng = l;
295         return tail;
296     }
297
298     /* funny character, interpret as identifier */
299     tail = str+1;
300     val->val_type = NSC_NOTYPE;
301     val->val_cat = NSC_ID;
302     val->val_as.str.base = str;
303     val->val_as.str.maxsz = tail - str;
304     return tail;
305 }
306
307 /*
308  * Match VAL in table of selector descriptors CA, return index.
309  * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
310  * several.
311  * A VAL that is not an identifier doesn't match anything.  A null CA
312  * is considered empty.
313  */
314 static int
315 nstr_match_ca(struct valstr *val, struct castr *ca)
316 {
317     char id[32];
318
319     if (val->val_cat != NSC_ID || val->val_as.str.maxsz >= sizeof(id))
320         return M_NOTFOUND;
321
322     if (!ca)
323         return M_NOTFOUND;
324
325     memcpy(id, val->val_as.str.base, val->val_as.str.maxsz);
326     id[val->val_as.str.maxsz] = 0;
327
328     return stmtch(id, ca, offsetof(struct castr, ca_name),
329                   sizeof(struct castr));
330 }
331
332 /*
333  * Match VAL in a selector's values, return its (non-negative) value.
334  * Match values of selector descriptor CA[IDX], provided IDX is not
335  * negative.  CA may be null when IDX is negative.
336  * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
337  * several.
338  */
339 static int
340 nstr_match_val(struct valstr *val, struct castr *ca, int idx)
341 {
342     char id[32];
343
344     if (val->val_cat != NSC_ID || val->val_as.str.maxsz >= sizeof(id))
345         return M_NOTFOUND;
346
347     if (idx < 0 || ca[idx].ca_table == EF_BAD)
348         return M_NOTFOUND;
349     if (CANT_HAPPEN(nstr_promote(ca[idx].ca_type) != NSC_LONG))
350         return M_NOTFOUND;
351
352     memcpy(id, val->val_as.str.base, val->val_as.str.maxsz);
353     id[val->val_as.str.maxsz] = 0;
354
355     return ef_elt_byname(ca[idx].ca_table, id);
356 }
357
358 /*
359  * Can CA[IDX] be compared to a string?
360  * Return 0 for negative IDX.
361  */
362 static int
363 nstr_string_ok(struct castr *ca, int idx)
364 {
365     return idx >= 0 && nstr_promote(ca[idx].ca_type) == NSC_STRING;
366 }
367
368 /*
369  * Change VAL to resolve identifier to selector or string.
370  * Return VAL on success, NULL on error.
371  * No change if VAL is not an identifier.  Otherwise, change it as
372  * follows.
373  * Error if IDX == M_NOTUNIQUE or IDX == M_NOTFOUND and !STRING_OK.
374  * Change into string if IDX == M_NOTFOUND and STRING_OK.
375  * Change into selector CA[IDX] if IDX >= 0.
376  */
377 static struct valstr *
378 nstr_resolve_id(struct valstr *val, struct castr *ca, int idx, int string_ok)
379 {
380     if (val->val_cat != NSC_ID)
381         return val;
382
383     if (idx == M_NOTUNIQUE && !string_ok) {
384         pr("%.*s -- ambiguous name\n",
385            (int)val->val_as.str.maxsz, val->val_as.str.base);
386         val->val_cat = NSC_NOCAT;
387         return NULL;
388     }
389
390     if (idx == M_NOTFOUND && !string_ok) {
391         pr("%.*s -- unknown name\n",
392            (int)val->val_as.str.maxsz, val->val_as.str.base);
393         val->val_cat = NSC_NOCAT;
394         return NULL;
395     }
396
397     if (idx < 0) {
398         CANT_HAPPEN(!string_ok);
399         /* interpret unresolvable identifier as string */
400         val->val_type = NSC_STRING;
401         val->val_cat = NSC_VAL;
402         /* map identifier ~ to empty string, like some commands do */
403         if (val->val_as.str.maxsz == 1 && val->val_as.str.base[0] == '~')
404             val->val_as.str.maxsz = 0;
405         return val;
406     }
407
408     return nstr_resolve_sel(val, &ca[idx]);
409 }
410
411 /*
412  * Change VAL to resolve identifier to selector CA.
413  * Return VAL on success, NULL if the player is denied access to the
414  * selector.
415  * VAL must be an identifier.
416  */
417 static struct valstr *
418 nstr_resolve_sel(struct valstr *val, struct castr *ca)
419 {
420     if (CANT_HAPPEN(val->val_cat != NSC_ID)) {
421         val->val_cat = NSC_NOCAT;
422         return val;
423     }
424
425     if ((ca->ca_flags & NSC_DEITY) && !player->god) {
426         pr("%.*s -- not accessible to mortals\n",
427            (int)val->val_as.str.maxsz, val->val_as.str.base);
428         val->val_cat = NSC_NOCAT;
429         return NULL;
430     }
431
432     val->val_type = ca->ca_type;
433     val->val_cat = NSC_OFF;
434     val->val_as.sym.off = ca->ca_off;
435     val->val_as.sym.len = ca->ca_len;
436     val->val_as.sym.idx = 0;
437     return val;
438 }
439
440 /*
441  * Initialize VAL to value SELVAL for selector CA, return VAL.
442  */
443 static struct valstr *
444 nstr_mkselval(struct valstr *val, int selval, struct castr *ca)
445 {
446     if (CANT_HAPPEN(nstr_promote(ca->ca_type) != NSC_LONG
447                     || ca->ca_table == EF_BAD)) {
448         val->val_type = NSC_NOTYPE;
449         val->val_cat = NSC_NOCAT;
450         return val;
451     }
452
453     val->val_type = ca->ca_type;
454     val->val_cat = NSC_VAL;
455     val->val_as.lng = selval;
456     return val;
457 }
458
459 /*
460  * Compile a value in STR into VAL.
461  * Return a pointer to the first character after the value on success,
462  * NULL on error.
463  * TYPE is the context type, a file type.
464  * If STR names an array, VAL simply refers to the element with index
465  * zero.
466  */
467 char *
468 nstr_comp_val(char *str, struct valstr *val, int type)
469 {
470     struct castr *ca = ef_cadef(type);
471     char *tail = nstr_parse_val(str, val);
472     if (!nstr_resolve_id(val, ca, nstr_match_ca(val, ca), 0))
473         return NULL;
474     return tail;
475 }
476
477
478 /*
479  * Promote VALTYPE.
480  * If VALTYPE is an integer type, return NSC_LONG.
481  * If VALTYPE is a floating-point type, return NSC_DOUBLE.
482  * If VALTYPE is a string type, return NSC_STRING.
483  */
484 static int
485 nstr_promote(int valtype)
486 {
487     switch (valtype) {
488     case NSC_LONG:
489     case NSC_DOUBLE:
490     case NSC_STRING:
491         break;
492     case NSC_CHAR:
493     case NSC_UCHAR:
494     case NSC_SHORT:
495     case NSC_USHORT:
496     case NSC_INT:
497     case NSC_XCOORD:
498     case NSC_YCOORD:
499     case NSC_HIDDEN:
500     case NSC_TIME:
501         valtype = NSC_LONG;
502         break;
503     case NSC_FLOAT:
504         valtype = NSC_DOUBLE;
505         break;
506     case NSC_STRINGY:
507         valtype = NSC_STRING;
508         break;
509     default:
510         CANT_REACH();
511         valtype = NSC_NOTYPE;
512     }
513     return valtype;
514 }
515
516 static int
517 cond_type_mismatch(char *str)
518 {
519     if (str)
520         pr("%s -- condition operand type mismatch\n", str);
521     return -1;
522 }
523
524 /*
525  * Coerce VAL to promoted value type TO.
526  * Return 0 on success, -1 on error.
527  * If VAL is evaluated, convert it, else only check.
528  * STR is the condition text to be used for error messages.  Suppress
529  * messages if it is a null pointer.
530  */
531 int
532 nstr_coerce_val(struct valstr *val, nsc_type to, char *str)
533 {
534     /* FIXME get rid of promotion?  */
535     nsc_type from = nstr_promote(val->val_type);
536
537     if (from == NSC_NOTYPE)
538         return 0;
539
540     if (from != to) {
541         switch (to) {
542         case NSC_STRING:
543             return cond_type_mismatch(str); /* FIXME implement */
544         case NSC_DOUBLE:
545             if (from == NSC_LONG) {
546                 if (val->val_cat == NSC_VAL)
547                     val->val_as.dbl = val->val_as.lng;
548             } else
549                 return cond_type_mismatch(str);
550             break;
551         case NSC_LONG:
552             return cond_type_mismatch(str);
553         default:
554             CANT_REACH();
555             to = from;
556         }
557     }
558
559     if (val->val_cat == NSC_VAL) {
560         /* unimplemented conversions; don't currently occur here */
561         CANT_HAPPEN(val->val_type == NSC_XCOORD
562                     || val->val_type == NSC_YCOORD
563                     || val->val_type == NSC_HIDDEN);
564         val->val_type = to;
565     }
566
567     return 0;
568 }