]> git.pond.sub.org Git - empserver/blob - src/lib/subs/nstr.c
(nstr_comp): Fix handling of condition type mismatch. This bug could
[empserver] / src / lib / subs / nstr.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 "misc.h"
37 #include "file.h"
38 #include "match.h"
39 #include "nsc.h"
40
41 static int nstr_promote(int valtype);
42
43 /*
44  * Compile conditions into array NP[LEN].
45  * Return number of conditions, or -1 on error.
46  * It is an error if there are more than LEN conditions.
47  * TYPE is the context type, a file type.
48  * STR is the condition string, in Empire syntax, without the leading
49  * '?'.
50  */
51 int
52 nstr_comp(struct nscstr *np, int len, int type, char *str)
53 {
54     char *cond;
55     char *tail;
56     int i;
57     struct nscstr dummy;
58     int lft_type, rgt_type;
59
60     cond = str;
61     for (i = 0; ; ++i, ++np) {
62         if (i >= len)
63             np = &dummy;
64
65         /* left operand */
66         tail = nstr_comp_val(cond, &np->lft, type);
67         if (!tail)
68             return -1;
69
70         /* operator */
71         if (*tail != '<' && *tail != '=' && *tail != '>' && *tail != '#') {
72             if (*tail)
73                 pr("%s -- expected condition operator\n", cond);
74             else
75                 pr("%s -- missing condition operator\n", cond);
76             return -1;
77         }
78         np->operator = *tail;
79         ++tail;
80
81         /* right operand */
82         tail = nstr_comp_val(tail, &np->rgt, type);
83         if (!tail)
84             return -1;
85
86         /* find operator type, coerce operands */
87         lft_type = nstr_promote(np->lft.val_type);
88         rgt_type = nstr_promote(np->rgt.val_type);
89         np->optype = NSC_NOTYPE;
90         if (lft_type == NSC_TYPEID) {
91             if (!nstr_coerce_val(&np->rgt, NSC_TYPEID, str))
92                 np->optype = NSC_TYPEID;
93         } else if (rgt_type == NSC_TYPEID) {
94             if (!nstr_coerce_val(&np->lft, NSC_TYPEID, str))
95                 np->optype = NSC_TYPEID;
96         } else if (lft_type == NSC_STRING) {
97             if (!nstr_coerce_val(&np->rgt, NSC_STRING, str))
98                 np->optype = NSC_STRING;
99         } else if (rgt_type == NSC_STRING) {
100             if (!nstr_coerce_val(&np->lft, NSC_STRING, str))
101                 np->optype = NSC_STRING;
102         } else if (lft_type == NSC_DOUBLE) {
103             if (!nstr_coerce_val(&np->rgt, NSC_DOUBLE, str))
104                 np->optype = NSC_DOUBLE;
105         } else if (rgt_type == NSC_DOUBLE) {
106             if (!nstr_coerce_val(&np->lft, NSC_DOUBLE, str))
107                 np->optype = NSC_DOUBLE;
108         } else {
109             if (!nstr_coerce_val(&np->lft, NSC_LONG, str)
110                 && !nstr_coerce_val(&np->rgt, NSC_LONG, str))
111                 np->optype = NSC_LONG;
112         }
113         if (np->optype == NSC_NOTYPE)
114             return -1;
115
116         /* another condition? */
117         if (*tail == 0)
118             break;
119         if (*tail != '&') {
120             pr("%s -- expected `&'\n", cond);
121             return -1;
122         }
123         cond = tail + 1;
124     }
125
126     if (i >= len) {
127         /* could just return I and let caller gripe or enlarge buffer */
128         pr("%s -- too many conditions\n", str);
129         return -1;
130     }
131
132     return i + 1;
133 }
134
135 #define EVAL(op, lft, rgt)                      \
136     ((op) == '<' ? (lft) < (rgt)                \
137      : (op) == '=' ? (lft) == (rgt)             \
138      : (op) == '>' ? (lft) > (rgt)              \
139      : (op) == '#' ? (lft) != (rgt)             \
140      : 0)
141
142 /*
143  * Evaluate compiled conditions in array NP[NCOND].
144  * Return non-zero iff they are all true.
145  * PTR points to a context object of the type that was used to compile
146  * the conditions.
147  */
148 int
149 nstr_exec(struct nscstr *np, int ncond, void *ptr)
150 {
151     int i, op, optype;
152     struct valstr lft, rgt;
153
154     for (i = 0; i < ncond; ++i) {
155         op = np[i].operator;
156         optype = np[i].optype;
157         if (np[i].lft.val_cat == NSC_NOCAT || np[i].rgt.val_cat == NSC_NOCAT)
158             return 0;
159         lft = np[i].lft;
160         nstr_exec_val(&lft, player->cnum, ptr, optype);
161         rgt = np[i].rgt;
162         nstr_exec_val(&rgt, player->cnum, ptr, optype);
163         switch (optype) {
164         case NSC_TYPEID:
165         case NSC_LONG:
166             if (!EVAL(op, lft.val_as.lng, rgt.val_as.lng))
167                 return 0;
168             break;
169         case NSC_DOUBLE:
170             if (!EVAL(op, lft.val_as.dbl, rgt.val_as.dbl))
171                 return 0;
172             break;
173         case NSC_STRING:
174             CANT_HAPPEN("unimplemented OPTYPE"); /* FIXME */
175             return 0;
176         default:
177             CANT_HAPPEN("bad OPTYPE");
178             return 0;
179         }
180     }
181
182     return 1;
183 }
184
185 /*
186  * Compile a value in STR into VAL.
187  * Return a pointer to the first character after the value on success,
188  * NULL on error.
189  * TYPE is the context type, a file type.
190  */
191 char *
192 nstr_comp_val(char *str, struct valstr*val, int type)
193 {
194     char id[32];
195     long l;
196     double d;
197     char *tail, *tail2;
198     struct castr *cap;
199     unsigned i;
200     int j;
201
202     val->val_type = NSC_NOTYPE;
203     val->val_cat = NSC_NOCAT;
204     val->val_as_type = -1;
205
206     if (isalpha(str[0])) {
207         /* identifier */
208         for (i = 0; isalnum(str[i]) || str[i] == '_'; ++i) {
209             if (i < sizeof(id) - 1)
210                 id[i] = str[i];
211         }
212         tail = str + i;
213         if (i < sizeof(id)) {
214             id[i] = 0;
215
216             val->val_as_type = typematch(id, type);
217
218             cap = ef_cadef(type);
219             if (cap) {
220                 j = stmtch(id, cap, offsetof(struct castr, ca_name),
221                            sizeof(struct castr));
222                 if (j >= 0
223                     && (!(cap[j].ca_flags & NSC_DEITY) || player->god)) {
224                     val->val_type = cap[j].ca_type;
225                     val->val_cat = NSC_OFF;
226                     val->val_as.off = cap[j].ca_off;
227                 }
228             } else
229                 j = M_NOTFOUND;
230         } else
231             j = M_NOTFOUND;
232
233         if (val->val_type == NSC_NOTYPE) {
234             if (val->val_as_type >= 0) {
235                 val->val_type = NSC_TYPEID;
236                 val->val_cat = NSC_VAL;
237                 val->val_as.lng = val->val_as_type;
238             } else if (j >= 0)
239                 pr("%s -- selector access denied\n", id);
240             else if (j == M_NOTUNIQUE)
241                 pr("%s -- ambiguous selector name\n", id);
242             else
243                 pr("%s -- unknown selector name\n", id);
244         }
245
246         return val->val_type == NSC_NOTYPE ? NULL : tail;
247     }
248
249     /* single character type */
250     id[0] = str[0];
251     id[1] = 0;
252     val->val_as_type = typematch(id, type);
253
254     /* literals */
255     l = strtol(str, &tail, 0);
256     d = strtod(str, &tail2);
257     if (tail2 > tail) {
258         val->val_type = NSC_DOUBLE;
259         val->val_cat = NSC_VAL;
260         val->val_as.dbl = d;
261         return tail2;
262     }
263     if (tail != str) {
264         val->val_type = NSC_LONG;
265         val->val_cat = NSC_VAL;
266         val->val_as.lng = l;
267         return tail;
268     }
269     /* FIXME implement NSC_STRING literals */
270
271     CANT_HAPPEN(val->val_type != NSC_NOTYPE);
272     if (val->val_as_type >= 0) {
273         val->val_type = NSC_TYPEID;
274         val->val_cat = NSC_VAL;
275         val->val_as.lng = val->val_as_type;
276         return str + 1;
277     }
278
279     pr("%s -- invalid value for condition\n", str);
280     return NULL;
281 }
282
283 /*
284  * Promote VALTYPE.
285  * If VALTYPE is an integer type, return NSC_LONG.
286  * If VALTYPE is a floating-point type, return NSC_DOUBLE.
287  * If VALTYPE is NSC_NOTYPE, NSC_STRING or NSC_TYPEID, return VALTYPE.
288  */
289 static int
290 nstr_promote(int valtype)
291 {
292     switch (valtype) {
293     case NSC_NOTYPE:
294     case NSC_LONG:
295     case NSC_DOUBLE:
296     case NSC_STRING:
297     case NSC_TYPEID:
298         break;
299     case NSC_CHAR:
300     case NSC_UCHAR:
301     case NSC_SHORT:
302     case NSC_USHORT:
303     case NSC_INT:
304     case NSC_XCOORD:
305     case NSC_YCOORD:
306     case NSC_TIME:
307         valtype = NSC_LONG;
308         break;
309     case NSC_FLOAT:
310         valtype = NSC_DOUBLE;
311         break;
312     default:
313         CANT_HAPPEN("bad VALTYPE");
314         valtype = NSC_NOTYPE;
315     }
316     return valtype;
317 }
318
319 static int
320 cond_type_mismatch(char *str)
321 {
322     if (str)
323         pr("%s -- condition operand type mismatch\n", str);
324     return -1;
325 }
326
327 /*
328  * Coerce VAL to promoted value type TO.
329  * Return 0 on success, -1 on error.
330  * If VAL is evaluated, convert it, else only check.
331  * STR is the condition text to be used for error messages.  Suppress
332  * messages if it is a null pointer.
333  */
334 int
335 nstr_coerce_val(struct valstr *val, nsc_type to, char *str)
336 {
337     /* FIXME get rid of promotion?  */
338     nsc_type from = nstr_promote(val->val_type);
339
340     if (from == NSC_NOTYPE)
341         return 0;
342
343     if (from != to) {
344         switch (to) {
345         case NSC_TYPEID:
346             if (val->val_as_type >= 0) {
347                 val->val_cat = NSC_VAL;
348                 val->val_as.lng = val->val_as_type;
349             } else
350                 return cond_type_mismatch(str);
351             break;
352         case NSC_STRING:
353             return cond_type_mismatch(str); /* FIXME implement */
354         case NSC_DOUBLE:
355             if (from == NSC_LONG) {
356                 if (val->val_cat == NSC_VAL)
357                     val->val_as.dbl = val->val_as.lng;
358             } else
359                 return cond_type_mismatch(str);
360             break;
361         case NSC_LONG:
362             return cond_type_mismatch(str);
363         default:
364             CANT_HAPPEN("bad TO argument");
365             to = from;
366         }
367     }
368
369     if (val->val_cat == NSC_VAL) {
370         /* coord literals don't occur, conversion not implemented */
371         CANT_HAPPEN(val->val_type == NSC_XCOORD
372                     || val->val_type == NSC_YCOORD);
373         val->val_type = to;
374     }
375
376     return 0;
377 }
378
379 /*
380  * Evaluate VAL.
381  * Use coordinate system of country CNUM.
382  * PTR points to a context object of the type that was used to compile
383  * the value.
384  * If WANT is not zero, coerce the value to promoted value type WANT.
385  * VAL must be coercible.  That's the case if a previous
386  * nstr_coerce_val(VAL, WANT, STR) succeeded.
387  */
388 void
389 nstr_exec_val(struct valstr *val, natid cnum, void *ptr, nsc_type want)
390 {
391     char *memb_ptr;
392     nsc_type valtype = NSC_LONG;
393
394     switch (val->val_cat) {
395     default:
396         CANT_HAPPEN("Bad VAL category");
397         /* fall through */
398     case NSC_VAL:
399         valtype = val->val_type;
400         break;
401     case NSC_OFF:
402         memb_ptr = ptr;
403         memb_ptr += val->val_as.off;
404         switch (val->val_type) {
405         case NSC_CHAR:
406             val->val_as.lng = *(signed char *)memb_ptr;
407             break;
408         case NSC_UCHAR:
409             val->val_as.lng = *(unsigned char *)memb_ptr;
410             break;
411         case NSC_SHORT:
412             val->val_as.lng = *(short *)memb_ptr;
413             break;
414         case NSC_USHORT:
415             val->val_as.lng = *(unsigned short *)memb_ptr;
416             break;
417         case NSC_INT:
418             val->val_as.lng = *(int *)memb_ptr;
419             break;
420         case NSC_LONG:
421             val->val_as.lng = *(long *)memb_ptr;
422             break;
423         case NSC_XCOORD:
424             val->val_as.lng = xrel(getnatp(cnum), *(short *)memb_ptr);
425             break;
426         case NSC_YCOORD:
427             val->val_as.lng = yrel(getnatp(cnum), *(short *)memb_ptr);
428             break;
429         case NSC_FLOAT:
430             val->val_as.dbl = *(float *)memb_ptr;
431             valtype = NSC_DOUBLE;
432             break;
433         case NSC_DOUBLE:
434             val->val_as.dbl = *(double *)memb_ptr;
435             valtype = NSC_DOUBLE;
436             break;
437         case NSC_STRING:
438             val->val_as.str = *(char **)memb_ptr;
439             valtype = NSC_STRING;
440             break;
441         case NSC_TIME:
442             val->val_as.lng = *(time_t *)memb_ptr;
443             break;
444         case NSC_TYPEID:
445             val->val_as.lng = *(signed char *)memb_ptr;
446             valtype = NSC_TYPEID;
447             break;
448         default:
449             CANT_HAPPEN("Bad VAL type");
450             val->val_as.lng = 0;
451         }
452     }
453
454     if (want) {
455         if (valtype == want)
456             ;
457         else if (want == NSC_DOUBLE) {
458             if (valtype == NSC_LONG) {
459                 valtype = want;
460                 val->val_as.dbl = val->val_as.lng;
461             }
462         } else if (want == NSC_STRING)
463             CANT_HAPPEN("unimplemented WANT"); /* FIXME */
464         if (CANT_HAPPEN(valtype != want)) {
465             valtype = want;
466             switch (want) {
467             case NSC_TYPEID:
468             case NSC_LONG: val->val_as.lng = 0; break;
469             case NSC_DOUBLE: val->val_as.dbl = 0.0; break;
470             case NSC_STRING: val->val_as.str = ""; break;
471             default:
472                 CANT_HAPPEN("bad WANT argument");
473             }
474         }
475     }
476
477     val->val_type = valtype;
478 }