]> git.pond.sub.org Git - empserver/blob - src/lib/common/xdump.c
Update copyright notice
[empserver] / src / lib / common / xdump.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  xdump.c: Extended dumps
28  *
29  *  Known contributors to this file:
30  *     Markus Armbruster, 2004-2014
31  */
32
33 /*
34  * Dump everything under the sun
35  *
36  * Static game data (configuration):
37  * - Item characteristics: ichr[]
38  * - Land unit characteristics: lchr[]
39  * - Nuke characteristics: nchr[]
40  * - Plane characteristics: plchr[]
41  * - Product characteristics: pchr[]
42  * - Sector designation characteristics: dchr[]
43  * - Sector infrastructure characteristics: intrchr[]
44  * - Ship characteristics: mchr[]
45  * - News item characteristics: rpt[]
46  * - News page headings: page_headings[]
47  * - Commands: player_coms[] (TODO)
48  * - Update schedule: update_time[] (not really static)
49  * - Configuration: configkeys[]
50  *
51  * Dynamic game data:
52  * - Sectors: EF_SECTOR (superseding dump)
53  * - Land units: EF_LAND (superseding ldump)
54  * - Lost: EF_LOST (superseding lost)
55  * - Nukes: EF_NUKE (superseding ndump)
56  * - Planes: EF_PLANE (superseding pdump)
57  * - Ships: EF_SHIP (superseding sdump)
58  * - News: EF_NEWS
59  * - Power: EF_POWER (TODO)
60  * - Nations: EF_NATION
61  * - Loans: EF_LOAN
62  * - Map: EF_MAP (TODO)
63  * - Bmap: EF_BMAP (TODO)
64  * - Market: EF_COMM
65  */
66
67 /*
68  * See doc/xdump for motivation, syntax, semantics, and rationale.
69  * Make sure to keep it up-to-date!
70  */
71
72 /* TODO don't dump stuff that's useless due to options */
73
74 #include <config.h>
75
76 #include <ctype.h>
77 #include <limits.h>
78 #include "file.h"
79 #include "nat.h"
80 #include "xdump.h"
81
82 /*
83  * Initialize @xd.
84  * Translate dump for country @cnum, except when @cnum is NATID_BAD.
85  * If @human, dump in human-readable format.
86  * If @sloppy, try to cope with invalid data (may result in invalid
87  * dump).
88  * Dump is to be delivered through callback @pr.
89  * Return @xd.
90  */
91 struct xdstr *
92 xdinit(struct xdstr *xd, natid cnum, int human, int sloppy,
93        void (*pr)(char *fmt, ...))
94 {
95     xd->cnum = cnum;
96     xd->divine = cnum == NATID_BAD || getnatp(cnum)->nat_stat == STAT_GOD;
97     xd->human = human;
98     xd->sloppy = sloppy;
99     xd->pr = pr;
100     return xd;
101 }
102
103 /*
104  * Evaluate a attribute of an object into @val, return @val.
105  * @ca describes the attribute.
106  * @xd is the xdump descriptor.
107  * @ptr points to the context object.
108  * @idx is the index within the attribute.
109  */
110 static struct valstr *
111 xdeval(struct valstr *val, struct xdstr *xd,
112        struct castr *ca, void *ptr, int idx)
113 {
114     nstr_mksymval(val, ca, idx);
115     return nstr_eval(val, xd->cnum, ptr, NSC_NOTYPE);
116 }
117
118 /*
119  * Dump string @str to @xd with funny characters escaped.
120  * Dump at most @n characters.
121  */
122 static void
123 xdpresc(struct xdstr *xd, char *str, size_t n)
124 {
125     unsigned char *s, *e;
126
127     s = (unsigned char *)str;
128     for (;;) {
129         for (e = s;
130              n && *e != '"' && *e != '\\' && isgraph(*e);
131              e++, n--)
132             ;
133         xd->pr("%.*s", (int)(e-s), s);
134         if (!n || !*e)
135             break;
136         xd->pr("\\%03o", *e++);
137         n--;
138         s = e;
139     }
140 }
141
142 /*
143  * Dump @val prefixed with @sep to @xd, in machine readable format.
144  * @val must be evaluated.
145  * Return " ".
146  */
147 static char *
148 xdprval_nosym(struct xdstr *xd, struct valstr *val, char *sep)
149 {
150     if (CANT_HAPPEN(val->val_cat != NSC_VAL)) {
151         xd->pr("%snil", sep);
152         return " ";
153     }
154
155     switch (val->val_type) {
156     case NSC_LONG:
157         xd->pr("%s%ld", sep, val->val_as.lng);
158         break;
159     case NSC_DOUBLE:
160         xd->pr("%s%#g", sep, val->val_as.dbl);
161         break;
162     case NSC_STRING:
163         if (val->val_as.str.base) {
164             xd->pr("%s\"", sep);
165             xdpresc(xd, val->val_as.str.base, val->val_as.str.maxsz);
166             xd->pr("\"");
167         } else
168             xd->pr("%snil", sep);
169         break;
170     default:
171         CANT_REACH();
172         xd->pr("%snil", sep);
173     }
174     return " ";
175 }
176
177 /*
178  * Dump symbol with value @key from symbol table @type to @xd.
179  * Prefix with @sep, return " ".
180  */
181 static char *
182 xdprsym(struct xdstr *xd, int key, int type, char *sep)
183 {
184     char *sym = symbol_by_value(key, ef_ptr(type, 0));
185
186     if (!sym) {
187         CANT_HAPPEN(!xd->sloppy);
188         xd->pr("%s%d", sep, key);
189     } else {
190         xd->pr("%s", sep);
191         xdpresc(xd, sym, INT_MAX);
192     }
193     return " ";
194 }
195
196 /*
197  * Dump @val prefixed with @sep to @xd, return " ".
198  * @val must be evaluated.
199  * @ca describes the field from which the value was fetched.
200  */
201 static char *
202 xdprval_sym(struct xdstr *xd, struct valstr *val, struct castr *ca,
203             char *sep)
204 {
205     unsigned long bit;
206
207     if (CANT_HAPPEN(val->val_cat != NSC_VAL)) {
208         xd->pr("%snil", sep);
209         return " ";
210     }
211
212     if (!xd->human || val->val_type != NSC_LONG
213         || ca->ca_table == EF_BAD || ef_cadef(ca->ca_table) != symbol_ca)
214         return xdprval_nosym(xd, val, sep);
215
216     if (ca->ca_flags & NSC_BITS) {
217         xd->pr("%s(", sep);
218         sep = "";
219         for (bit = 1; bit; bit <<= 1) {
220             if (bit & val->val_as.lng)
221                 sep = xdprsym(xd, bit, ca->ca_table, sep);
222         }
223         xd->pr(")");
224         return " ";
225     }
226
227     return xdprsym(xd, val->val_as.lng, ca->ca_table, sep);
228 }
229
230 /*
231  * Dump field values of a context object to @xd.
232  * @ca[] describes fields.
233  * @ptr points to context object.
234  */
235 void
236 xdflds(struct xdstr *xd, struct castr ca[], void *ptr)
237 {
238     int i, j, n;
239     struct valstr val;
240     char *sep = "";
241
242     for (i = 0; ca[i].ca_name; ++i) {
243         if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
244             continue;
245         if (ca[i].ca_flags & NSC_EXTRA)
246             continue;
247         n = CA_ARRAY_LEN(&ca[i]);
248         j = 0;
249         do {
250             xdeval(&val, xd, &ca[i], ptr, j);
251             sep = xdprval_sym(xd, &val, &ca[i], sep);
252         } while (++j < n);
253     }
254 }
255
256 /*
257  * Dump header for dump @name to @xd.
258  * If @meta, it's for the meta-data dump rather than the data dump.
259  */
260 void
261 xdhdr(struct xdstr *xd, char *name, int meta)
262 {
263     if (xd->human) {
264         xd->pr("config %s\n", name);
265     } else
266         xd->pr("XDUMP %s%s %ld\n",
267                meta ? "meta " : "",
268                name, (long)time(NULL));
269 }
270
271 /*
272  * Dump column header to @xd.
273  * @ca[] describes fields.
274  * Does nothing unless @xd is human-readable.
275  */
276 void
277 xdcolhdr(struct xdstr *xd, struct castr ca[])
278 {
279     int i, j, n;
280     char *sep = "";
281
282     if (!xd->human)
283         return;
284
285     for (i = 0; ca[i].ca_name; ++i) {
286         if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
287             continue;
288         if (ca[i].ca_flags & NSC_EXTRA)
289             continue;
290         n = CA_ARRAY_LEN(&ca[i]);
291         if (n) {
292             for (j = 0; j < n; j++) {
293                 xd->pr("%s%s(%d)", sep, ca[i].ca_name, j);
294                 sep = " ";
295             }
296         } else {
297             xd->pr("%s%s", sep, ca[i].ca_name);
298             sep = " ";
299         }
300     }
301     xd->pr("\n");
302 }
303
304 /* Dump footer for a dump that dumped N objects to XD. */
305 void
306 xdftr(struct xdstr *xd, int n)
307 {
308     if (xd->human)
309         xd->pr("/config\n");
310     else
311         xd->pr("/%d\n", n);
312 }