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