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