2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
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.
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.
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/>.
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.
27 * xdump.c: Extended dumps
29 * Known contributors to this file:
30 * Markus Armbruster, 2004-2010
34 * Dump everything under the sun
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[]
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)
59 * - Treaties: EF_TREATY
60 * - Power: EF_POWER (TODO)
61 * - Nations: EF_NATION
63 * - Map: EF_MAP (TODO)
64 * - Bmap: EF_BMAP (TODO)
69 * See doc/xdump for motivation, syntax, semantics, and rationale.
70 * Make sure to keep it up-to-date!
73 /* TODO don't dump stuff that's useless due to options */
85 * Translate dump for country CNUM, except when CNUM is NATID_BAD.
86 * If HUMAN, dump in human-readable format.
87 * Dump is to be delivered through callback PR.
91 xdinit(struct xdstr *xd, natid cnum, int human, void (*pr)(char *fmt, ...))
94 xd->divine = cnum == NATID_BAD || getnatp(cnum)->nat_stat == STAT_GOD;
101 * Evaluate a attribute of an object into VAL, return VAL.
102 * CA describes the attribute.
103 * XD is the xdump descriptor.
104 * PTR points to the context object.
105 * IDX is the index within the attribute.
107 static struct valstr *
108 xdeval(struct valstr *val, struct xdstr *xd,
109 struct castr *ca, void *ptr, int idx)
111 nstr_mksymval(val, ca, idx);
112 return nstr_exec_val(val, xd->cnum, ptr, NSC_NOTYPE);
116 * Dump string STR to XD with funny characters escaped.
117 * Dump at most N characters.
120 xdpresc(struct xdstr *xd, char *str, size_t n)
122 unsigned char *s, *e, *l;
124 s = (unsigned char *)str;
128 e < l && *e != '"' && *e != '\\' && isgraph(*e);
131 xd->pr("%.*s", (int)(e-s), s);
133 xd->pr("\\%03o", *e++);
141 * Dump VAL prefixed with SEP to XD, in machine readable format.
142 * VAL must be evaluated.
146 xdprval_nosym(struct xdstr *xd, struct valstr *val, char *sep)
148 if (CANT_HAPPEN(val->val_cat != NSC_VAL)) {
149 xd->pr("%snil", sep);
153 switch (val->val_type) {
155 xd->pr("%s%ld", sep, val->val_as.lng);
158 xd->pr("%s%#g", sep, val->val_as.dbl);
161 if (val->val_as.str.base) {
163 xdpresc(xd, val->val_as.str.base, val->val_as.str.maxsz);
166 xd->pr("%snil", sep);
170 xd->pr("%snil", sep);
176 * Dump symbol with value KEY from symbol table TYPE to XD.
177 * Prefix with SEP, return " ".
180 xdprsym(struct xdstr *xd, int key, int type, char *sep)
182 char *sym = symbol_by_value(key, ef_ptr(type, 0));
184 if (CANT_HAPPEN(!sym))
185 xd->pr("%s%d", sep, key);
188 xdpresc(xd, sym, INT_MAX);
194 * Dump VAL prefixed with SEP to XD, return " ".
195 * VAL must be evaluated.
196 * CA describes the field from which the value was fetched.
199 xdprval_sym(struct xdstr *xd, struct valstr *val, struct castr *ca,
204 if (CANT_HAPPEN(val->val_cat != NSC_VAL)) {
205 xd->pr("%snil", sep);
209 if (!xd->human || val->val_type != NSC_LONG
210 || ca->ca_table == EF_BAD || ef_cadef(ca->ca_table) != symbol_ca)
211 return xdprval_nosym(xd, val, sep);
213 if (ca->ca_flags & NSC_BITS) {
216 for (bit = 1; bit; bit <<= 1) {
217 if (bit & val->val_as.lng)
218 sep = xdprsym(xd, bit, ca->ca_table, sep);
224 return xdprsym(xd, val->val_as.lng, ca->ca_table, sep);
228 * Dump field values of a context object to XD.
229 * CA[] describes fields.
230 * PTR points to context object.
233 xdflds(struct xdstr *xd, struct castr ca[], void *ptr)
239 for (i = 0; ca[i].ca_name; ++i) {
240 if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
242 if (ca[i].ca_flags & NSC_EXTRA)
244 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
247 xdeval(&val, xd, &ca[i], ptr, j);
248 sep = xdprval_sym(xd, &val, &ca[i], sep);
254 * Dump header for dump NAME to XD.
255 * If META, it's for the meta-data dump rather than the data dump.
258 xdhdr(struct xdstr *xd, char *name, int meta)
261 xd->pr("config %s\n", name);
263 xd->pr("XDUMP %s%s %ld\n",
265 name, (long)time(NULL));
269 * Dump column header to XD.
270 * CA[] describes fields.
271 * Does nothing unless XD is human-readable.
274 xdcolhdr(struct xdstr *xd, struct castr ca[])
282 for (i = 0; ca[i].ca_name; ++i) {
283 if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
285 if (ca[i].ca_flags & NSC_EXTRA)
287 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
289 for (j = 0; j < n; j++) {
290 xd->pr("%s%s(%d)", sep, ca[i].ca_name, j);
294 xd->pr("%s%s", sep, ca[i].ca_name);
301 /* Dump footer for a dump that dumped N objects to XD. */
303 xdftr(struct xdstr *xd, int n)