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