]> git.pond.sub.org Git - empserver/blob - src/lib/subs/nstr.c
(NSC_OPMASK, NSC_ISNUM1, NSC_ISNUM2, nscstr, nstr_comp, nstr_exec):
[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  */
34
35 #include <ctype.h>
36 #include "struct.h"
37 #include "misc.h"
38 #include "var.h"
39 #include "xy.h"
40 #include "sect.h"
41 #include "nsc.h"
42 #include "nat.h"
43 #include "match.h"
44 #include "file.h"
45 #include "player.h"
46 #include "prototypes.h"
47
48 static int legal_val(s_char *str, int val);
49
50 /*
51  * Compiles and adds "str" to the list of conditionals.
52  * type is the EF typename of the item type we're selecting.
53  * returns amount of "str" used by nstr_comp (i.e. how far
54  * the pointer was advanced).  The last is only meaningful
55  * if several conditionals are expected in one string.
56  */
57 s_char *
58 nstr_comp(struct nscstr *np, int *size, int type, s_char *str)
59 {
60     register s_char *bp;
61     register s_char *cp;
62     register int c;
63     s_char ident[80];
64     s_char arg[255];
65     int val;
66
67     strncpy(arg, str, sizeof(arg) - 1);
68     arg[sizeof(arg) - 1] = 0;
69     cp = arg;
70     bp = ident;
71     while ((c = *cp++) && bp < &ident[sizeof(ident) - 1]) {
72         if (c == '<' || c == '=' || c == '>' || c == '#')
73             break;
74         *bp++ = c;
75     }
76     *bp = 0;
77     if (c == 0) {
78         pr("'%s'? -- meaningless condition?\n", arg);
79         return 0;
80     }
81     np[*size].oper = c & NSC_OPMASK;
82     if ((val = encode(ident, &np[*size].fld1, type)) < 0)
83         return 0;
84     if (val == 2)
85         np[*size].oper |= NSC_ISNUM1;
86     bp = ident;
87     while ((c = *cp++) && bp < &ident[sizeof(ident) - 1]) {
88         if (c == '&')
89             break;
90         *bp++ = c;
91     }
92     *bp = 0;
93     if ((val = encode(ident, &np[*size].fld2, type)) < 0)
94         return 0;
95     if (val == 2)
96         np[*size].oper |= NSC_ISNUM2;
97     if (c == 0)
98         cp--;
99     (*size)++;
100     return str + (cp - arg);
101 }
102
103 /*
104  * return true if the conditions on this item
105  * are all true.
106  */
107 int
108 nstr_exec(struct nscstr *conds, register int ncond, void *ptr, int type)
109 {
110     register struct nscstr *nsc;
111     register int op;
112     register int lhs;
113     register int rhs;
114     register int oper;
115
116     for (nsc = conds; --ncond >= 0; nsc++) {
117         oper = nsc->oper;
118         if (oper & NSC_ISNUM2) {
119             rhs = nsc->fld2;
120         } else
121             rhs = decode(player->cnum, nsc->fld2, ptr, type);
122
123         if (oper & NSC_ISNUM1) {
124             lhs = nsc->fld1;
125         } else
126             lhs = decode(player->cnum, nsc->fld1, ptr, type);
127
128         op = oper & NSC_OPMASK;
129         if ((op == '<' && lhs >= rhs)
130             || (op == '=' && lhs != rhs)
131             || (op == '>' && lhs <= rhs)
132             || (op == '#' && lhs == rhs))
133             return 0;
134     }
135     return 1;
136 }
137
138 int
139 encode(register s_char *str, long int *val, int type)
140 {
141     register int i;
142     struct castr *cap;
143
144     if (str == 0) {
145         *val = 0;
146         return 0;
147     }
148     if (isdigit(*str) || ((*str == '-') && isdigit(str[1]))) {
149         *val = atoi(str);
150         return 2;
151     }
152     /*
153      * FIXME This accepts the first match found, even if there are
154      * more matches in other tables, i.e. it quietly accepts ambiguous
155      * matches (matches in multiple tables), and fails to prefer an
156      * exact match to partial match in an earlier table.
157      */
158     if ((i = typematch(str, type)) >= 0) {
159         *val = i;
160         return 1;
161     }
162     if ((cap = ef_cadef(type)) != 0) {
163         i = stmtch(str, (caddr_t)cap, fldoff(castr, ca_name),
164                    sizeof(struct castr));
165         if (i >= 0) {
166             *val = cap[i].ca_code;
167             return legal_val(str, *val);
168         }
169         if (i == M_NOTUNIQUE) {
170             pr("%s -- ambiguous type selector\n", str);
171             return 0;
172         }
173     }
174     /*
175      * Only check for commodity selectors on objects which
176      * are allowed to have commodities.
177      */
178     if (ef_flags(type) & EFF_COM) {
179         i = stmtch(str, (caddr_t)var_ca, fldoff(castr, ca_name),
180                    sizeof(struct castr));
181         if (i >= 0) {
182             *val = var_ca[i].ca_code;
183             return legal_val(str, *val);
184         }
185         if (i == M_NOTUNIQUE) {
186             pr("%s -- ambiguous commodity selector\n", str);
187             return 0;
188         }
189     }
190     pr("%s -- not a valid selector\n", str);
191     return 0;
192 }
193
194 static int
195 legal_val(s_char *str, int val)
196 {
197     if (val & NSC_DEITY && !player->god) {
198         pr("%s -- permission denied\n", str);
199         return -1;
200     }
201     return 1;
202 }
203
204 int
205 decode(natid cnum, long int code, void *addr, int type)
206 {
207     register int val;
208     register int nsc_code;
209     struct natstr *np;
210     long code_type = (code & NSC_TMASK);
211
212     val = (code & ~NSC_MASK) & 0xffff;
213
214     /* handle negative numbers properly */
215     /* this assumes a binary two's complement number representation */
216     if (val >= 0x8000)
217         val -= 0x10000;
218
219     nsc_code = code & NSC_CMASK;
220     if (nsc_code == NSC_VAR) {
221         val = getvar(val, addr, type);
222     } else if (nsc_code == NSC_OFF) {
223         /*
224          * add offset to value
225          */
226         addr = (s_char *)addr + val;
227         switch (code_type) {
228         case NSC_TIME:
229             val = *((time_t *) addr);
230             break;
231         case NSC_CHAR:
232             val = *((s_char *)addr);
233             break;
234         case NSC_UCHAR:
235             val = (int)*((unsigned char *)addr);
236             break;
237         case NSC_SHORT:
238             val = *((short *)addr);
239             break;
240         case NSC_USHORT:
241             val = *((u_short *)addr);
242             break;
243         case NSC_INT:
244             val = *((int *)addr);
245             break;
246         case NSC_LONG:
247             val = *((long *)addr);
248             break;
249         case NSC_XCOORD:
250             val = *((short *)addr);
251             np = getnatp(cnum);
252             val = xrel(np, val);
253             break;
254         case NSC_YCOORD:
255             val = *((short *)addr);
256             np = getnatp(cnum);
257             val = yrel(np, val);
258             break;
259         default:
260             logerror("bad type in decode: %x!\n", code & NSC_TMASK);
261             val = 0;
262             break;
263         }
264     }
265     return val;
266 }
267
268 s_char *
269 decodep(long int code, void *addr)
270 {
271     addr = (char *)addr + ((code & ~NSC_MASK) & 0xffff);
272
273     if ((code & NSC_TMASK) == NSC_CHARP)
274         return *(s_char **)addr ? *((s_char **)addr) : (s_char *)"";
275     return addr;
276 }