]> git.pond.sub.org Git - empserver/blob - src/lib/common/ef_verify.c
Break inclusion cycle: prototypes.h and commands.h included each
[empserver] / src / lib / common / ef_verify.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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  *  ef_verify.c: Verify game configuration
29  * 
30  *  Known contributors to this file:
31  *     Ron Koenderink, 2005
32  */
33
34 #include <config.h>
35
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include "file.h"
40 #include "misc.h"
41 #include "nsc.h"
42 #include "product.h"
43
44 static void verify_fail(int, int, struct castr *, int, char *, ...)
45     ATTRIBUTE((format (printf, 5, 6)));
46
47 static void
48 verify_fail(int type, int row, struct castr *ca, int idx, char *fmt, ...)
49 {
50     int i;
51     va_list ap;
52
53     /* Find base table of view, if any */
54     for (i = 0; empfile[i].cache == empfile[type].cache; i++) ;
55
56     fprintf(stderr, "%s %s uid %d field %s",
57             EF_IS_GAME_STATE(i) ? "File" : "Config",
58             ef_nameof(type), row, ca->ca_name);
59     if (ca->ca_type != NSC_STRINGY && ca->ca_len != 0)
60         fprintf(stderr, " index %d", idx);
61     fprintf(stderr, ": ");
62     va_start(ap, fmt);
63     vfprintf(stderr, fmt, ap);
64     va_end(ap);
65     putc('\n', stderr);
66 }
67
68 static int
69 verify_row(int type, int row)
70 {
71     struct castr *ca = ef_cadef(type);
72     void *row_ref;
73     int i, j, k, n;
74     struct castr *ca_sym;
75     struct valstr val;
76     int ret_val = 0; 
77     int in_mem = (ef_flags(type) & EFF_MEM) != 0; 
78  
79     if (in_mem)
80         row_ref = ef_ptr(type, row); 
81     else {
82         row_ref = malloc(empfile[type].size); 
83         ef_read(type, row, row_ref);
84     }
85
86     for (i = 0; ca[i].ca_name; ++i) {
87         if (ca[i].ca_flags & NSC_EXTRA)
88             continue;
89         n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
90         j = 0;
91         do {
92             if (ca[i].ca_table == EF_BAD)
93                 continue;
94             /* FIXME use xdeval() */
95             val.val_type = ca[i].ca_type;
96             val.val_cat = NSC_OFF;
97             val.val_as.sym.off = ca[i].ca_off;
98             val.val_as.sym.len = ca[i].ca_len;
99             val.val_as.sym.idx = j;
100             nstr_exec_val(&val, 0, row_ref, NSC_NOTYPE);
101             if (val.val_type != NSC_LONG)
102                 continue;
103             ca_sym = ef_cadef(ca[i].ca_table);
104             if (ca[i].ca_flags & NSC_BITS) {
105                 /* symbol set */
106                 if (CANT_HAPPEN(ca_sym != symbol_ca)) {
107                     ret_val = -1;
108                     continue;
109                 }
110                 for (k = 0; k < (int)sizeof(long) * 8; k++) {
111                     if (val.val_as.lng & (1L << k))
112                         if (!symbol_by_value(1L << k,
113                                              ef_ptr(ca[i].ca_table, 0))) {
114                             verify_fail(type, row, &ca[i], j,
115                                         "bit %d is not in symbol table %s",
116                                         k, ef_nameof(ca[i].ca_table));
117                             ret_val = -1;
118                         }
119                 }
120             } else if (ca[i].ca_table == type && i == 0) {
121                 /* uid */
122                 /* Some files contain zeroed records, cope */
123                 /* TODO tighten this check */
124                 if (val.val_as.lng == 0)
125                     continue;
126                 if (val.val_as.lng != row) {
127                     verify_fail(type, row, &ca[i], j,
128                                 "value is %ld instead of %d",
129                                 val.val_as.lng, row);
130                     ret_val = -1;
131                 }
132
133             } else if (ca_sym == symbol_ca) {
134                 /* symbol */
135                 if (!symbol_by_value(val.val_as.lng,
136                                      ef_ptr(ca[i].ca_table, 0))) {
137                     verify_fail(type, row, &ca[i], j,
138                                 "value %ld is not in symbol table %s",
139                                 val.val_as.lng, ef_nameof(ca[i].ca_table));
140                     ret_val = -1;
141                 }
142             } else {
143                 /* table index */
144                 if (val.val_as.lng >= ef_nelem(ca[i].ca_table)
145                     || val.val_as.lng < -1) {
146                     verify_fail(type, row, &ca[i], j,
147                         "value %ld indexes table %s out of bounds 0..%d",
148                         val.val_as.lng, ef_nameof(ca[i].ca_table),
149                         ef_nelem(ca[i].ca_table));
150                     ret_val = -1;
151                 }
152             }
153         } while (++j < n);
154     } 
155     if (!in_mem)
156         free(row_ref);
157     return ret_val;
158 }
159
160 int
161 ef_verify()
162 {
163     struct empfile *ep;
164     int retval = 0;
165     int i;
166
167     for (ep = empfile; ep->name; ep++) {
168         if (!ef_cadef(ep->uid))
169             continue;
170         for (i = 0; i < ef_nelem(ep->uid); i++) {
171             retval += verify_row(ep->uid, i);
172         }
173     }
174
175     /* Special checks */
176     for (i = 0; pchr[i].p_sname; i++) {
177         if ((pchr[i].p_type >= 0) == (pchr[i].p_level >= 0)) {
178             fprintf(stderr,
179                 "Config %s uid %d field level doesn't match field type\n",
180                 ef_nameof(EF_PRODUCT), i);
181             retval = -1;
182         }
183     }
184
185     return retval;
186 }