]> git.pond.sub.org Git - empserver/blob - src/lib/common/xdump.c
33a37837a60949f5045ab89dfb1428aaa73a14dc
[empserver] / src / lib / common / xdump.c
1 /*
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
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-2008
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  * - Treaties: EF_TREATY
60  * - Power: EF_POWER (TODO)
61  * - Nations: EF_NATION
62  * - Loans: EF_LOAN
63  * - Map: EF_MAP (TODO)
64  * - Bmap: EF_BMAP (TODO)
65  * - Market: EF_COMM
66  */
67
68 /*
69  * See doc/xdump for motivation, syntax, semantics, and rationale.
70  * Make sure to keep it up-to-date!
71  */
72
73 /* TODO don't dump stuff that's useless due to options */
74
75 #include <config.h>
76
77 #include <ctype.h>
78 #include <limits.h>
79 #include "file.h"
80 #include "nat.h"
81 #include "xdump.h"
82
83 /*
84  * Initialize XD.
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.
88  * Return XD.
89  */
90 struct xdstr *
91 xdinit(struct xdstr *xd, natid cnum, int human, void (*pr)(char *fmt, ...))
92 {
93     xd->cnum = cnum;
94     xd->divine = cnum == NATID_BAD || getnatp(cnum)->nat_stat == STAT_GOD;
95     xd->human = human;
96     xd->pr = pr;
97     return xd;
98 }
99
100 /*
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.
106  */
107 static struct valstr *
108 xdeval(struct valstr *val, struct xdstr *xd,
109        struct castr *ca, void *ptr, int idx)
110 {
111     nstr_mksymval(val, ca, idx);
112     return nstr_exec_val(val, xd->cnum, ptr, NSC_NOTYPE);
113 }
114
115 /*
116  * Dump string STR to XD with funny characters escaped.
117  * Dump at most N characters.
118  */
119 static void
120 xdpresc(struct xdstr *xd, char *str, size_t n)
121 {
122     unsigned char *s, *e, *l;
123
124     s = (unsigned char *)str;
125     l = s + n;
126     for (;;) {
127         for (e = s;
128              e < l && *e != '"' && *e != '\\' && isgraph(*e);
129              ++e)
130             ;
131         xd->pr("%.*s", (int)(e-s), s);
132         if (e < l && *e)
133             xd->pr("\\%03o", *e++);
134         else
135             break;
136         s = e;
137     }
138 }
139
140 /*
141  * Dump VAL prefixed with SEP to XD, in machine readable format.
142  * VAL must be evaluated.
143  * Return " ".
144  */
145 static char *
146 xdprval_nosym(struct xdstr *xd, struct valstr *val, char *sep)
147 {
148     if (CANT_HAPPEN(val->val_cat != NSC_VAL)) {
149         xd->pr("%snil", sep);
150         return " ";
151     }
152
153     switch (val->val_type) {
154     case NSC_LONG:
155         xd->pr("%s%ld", sep, val->val_as.lng);
156         break;
157     case NSC_DOUBLE:
158         xd->pr("%s%#g", sep, val->val_as.dbl);
159         break;
160     case NSC_STRING:
161         if (val->val_as.str.base) {
162             xd->pr("%s\"", sep);
163             xdpresc(xd, val->val_as.str.base, val->val_as.str.maxsz);
164             xd->pr("\"");
165         } else
166             xd->pr("%snil", sep);
167         break;
168     default:
169         CANT_REACH();
170         xd->pr("%snil", sep);
171     }
172     return " ";
173 }
174
175 /*
176  * Dump symbol with value KEY from symbol table TYPE to XD.
177  * Prefix with SEP, return " ".
178  */
179 static char *
180 xdprsym(struct xdstr *xd, int key, int type, char *sep)
181 {
182     char *sym = symbol_by_value(key, ef_ptr(type, 0));
183
184     if (CANT_HAPPEN(!sym))
185         xd->pr("%s%d", sep, key);
186     else {
187         xd->pr("%s", sep);
188         xdpresc(xd, sym, INT_MAX);
189     }
190     return " ";
191 }
192
193 /*
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.
197  */
198 static char *
199 xdprval_sym(struct xdstr *xd, struct valstr *val, struct castr *ca,
200             char *sep)
201 {
202     unsigned long bit;
203
204     if (CANT_HAPPEN(val->val_cat != NSC_VAL)) {
205         xd->pr("%snil", sep);
206         return " ";
207     }
208
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);
212
213     if (ca->ca_flags & NSC_BITS) {
214         xd->pr("%s(", sep);
215         sep = "";
216         for (bit = 1; bit; bit <<= 1) {
217             if (bit & val->val_as.lng)
218                 sep = xdprsym(xd, bit, ca->ca_table, sep);
219         }
220         xd->pr(")");
221         return " ";
222     }
223
224     return xdprsym(xd, val->val_as.lng, ca->ca_table, sep);
225 }
226
227 /*
228  * Dump field values of a context object to XD.
229  * CA[] describes fields.
230  * PTR points to context object.
231  */
232 void
233 xdflds(struct xdstr *xd, struct castr ca[], void *ptr)
234 {
235     int i, j, n;
236     struct valstr val;
237     char *sep = "";
238
239     for (i = 0; ca[i].ca_name; ++i) {
240         if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
241             continue;
242         if (ca[i].ca_flags & NSC_EXTRA)
243             continue;
244         n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
245         j = 0;
246         do {
247             xdeval(&val, xd, &ca[i], ptr, j);
248             sep = xdprval_sym(xd, &val, &ca[i], sep);
249         } while (++j < n);
250     }
251 }
252
253 /*
254  * Dump header for dump NAME to XD.
255  * If META, it's for the meta-data dump rather than the data dump.
256  */
257 void
258 xdhdr(struct xdstr *xd, char *name, int meta)
259 {
260     if (xd->human) {
261         xd->pr("config %s\n", name);
262     } else
263         xd->pr("XDUMP %s%s %ld\n",
264                meta ? "meta " : "",
265                name, (long)time(NULL));
266 }
267
268 /*
269  * Dump column header to XD.
270  * CA[] describes fields.
271  * Does nothing unless XD is human-readable.
272  */
273 void
274 xdcolhdr(struct xdstr *xd, struct castr ca[])
275 {
276     int i, j, n;
277     char *sep = "";
278
279     if (!xd->human)
280         return;
281
282     for (i = 0; ca[i].ca_name; ++i) {
283         if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
284             continue;
285         if (ca[i].ca_flags & NSC_EXTRA)
286             continue;
287         n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
288         if (n) {
289             for (j = 0; j < n; j++) {
290                 xd->pr("%s%s(%d)", sep, ca[i].ca_name, j);
291                 sep = " ";
292             }
293         } else {
294             xd->pr("%s%s", sep, ca[i].ca_name);
295             sep = " ";
296         }
297     }
298     xd->pr("\n");
299 }
300
301 /* Dump footer for a dump that dumped N objects to XD.  */
302 void
303 xdftr(struct xdstr *xd, int n)
304 {
305     if (xd->human)
306         xd->pr("/config\n");
307     else
308         xd->pr("/%d\n", n);
309 }