]> 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-2014, 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-2013
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  * If SLOPPY, try to cope with invalid data (may result in invalid
88  * dump).
89  * Dump is to be delivered through callback PR.
90  * Return XD.
91  */
92 struct xdstr *
93 xdinit(struct xdstr *xd, natid cnum, int human, int sloppy,
94        void (*pr)(char *fmt, ...))
95 {
96     xd->cnum = cnum;
97     xd->divine = cnum == NATID_BAD || getnatp(cnum)->nat_stat == STAT_GOD;
98     xd->human = human;
99     xd->sloppy = sloppy;
100     xd->pr = pr;
101     return xd;
102 }
103
104 /*
105  * Evaluate a attribute of an object into VAL, return VAL.
106  * CA describes the attribute.
107  * XD is the xdump descriptor.
108  * PTR points to the context object.
109  * IDX is the index within the attribute.
110  */
111 static struct valstr *
112 xdeval(struct valstr *val, struct xdstr *xd,
113        struct castr *ca, void *ptr, int idx)
114 {
115     nstr_mksymval(val, ca, idx);
116     return nstr_exec_val(val, xd->cnum, ptr, NSC_NOTYPE);
117 }
118
119 /*
120  * Dump string STR to XD with funny characters escaped.
121  * Dump at most N characters.
122  */
123 static void
124 xdpresc(struct xdstr *xd, char *str, size_t n)
125 {
126     unsigned char *s, *e, *l;
127
128     s = (unsigned char *)str;
129     l = s + n;
130     for (;;) {
131         for (e = s;
132              e < l && *e != '"' && *e != '\\' && isgraph(*e);
133              ++e)
134             ;
135         xd->pr("%.*s", (int)(e-s), s);
136         if (e < l && *e)
137             xd->pr("\\%03o", *e++);
138         else
139             break;
140         s = e;
141     }
142 }
143
144 /*
145  * Dump VAL prefixed with SEP to XD, in machine readable format.
146  * VAL must be evaluated.
147  * Return " ".
148  */
149 static char *
150 xdprval_nosym(struct xdstr *xd, struct valstr *val, char *sep)
151 {
152     if (CANT_HAPPEN(val->val_cat != NSC_VAL)) {
153         xd->pr("%snil", sep);
154         return " ";
155     }
156
157     switch (val->val_type) {
158     case NSC_LONG:
159         xd->pr("%s%ld", sep, val->val_as.lng);
160         break;
161     case NSC_DOUBLE:
162         xd->pr("%s%#g", sep, val->val_as.dbl);
163         break;
164     case NSC_STRING:
165         if (val->val_as.str.base) {
166             xd->pr("%s\"", sep);
167             xdpresc(xd, val->val_as.str.base, val->val_as.str.maxsz);
168             xd->pr("\"");
169         } else
170             xd->pr("%snil", sep);
171         break;
172     default:
173         CANT_REACH();
174         xd->pr("%snil", sep);
175     }
176     return " ";
177 }
178
179 /*
180  * Dump symbol with value KEY from symbol table TYPE to XD.
181  * Prefix with SEP, return " ".
182  */
183 static char *
184 xdprsym(struct xdstr *xd, int key, int type, char *sep)
185 {
186     char *sym = symbol_by_value(key, ef_ptr(type, 0));
187
188     if (!sym) {
189         CANT_HAPPEN(!xd->sloppy);
190         xd->pr("%s%d", sep, key);
191     } else {
192         xd->pr("%s", sep);
193         xdpresc(xd, sym, INT_MAX);
194     }
195     return " ";
196 }
197
198 /*
199  * Dump VAL prefixed with SEP to XD, return " ".
200  * VAL must be evaluated.
201  * CA describes the field from which the value was fetched.
202  */
203 static char *
204 xdprval_sym(struct xdstr *xd, struct valstr *val, struct castr *ca,
205             char *sep)
206 {
207     unsigned long bit;
208
209     if (CANT_HAPPEN(val->val_cat != NSC_VAL)) {
210         xd->pr("%snil", sep);
211         return " ";
212     }
213
214     if (!xd->human || val->val_type != NSC_LONG
215         || ca->ca_table == EF_BAD || ef_cadef(ca->ca_table) != symbol_ca)
216         return xdprval_nosym(xd, val, sep);
217
218     if (ca->ca_flags & NSC_BITS) {
219         xd->pr("%s(", sep);
220         sep = "";
221         for (bit = 1; bit; bit <<= 1) {
222             if (bit & val->val_as.lng)
223                 sep = xdprsym(xd, bit, ca->ca_table, sep);
224         }
225         xd->pr(")");
226         return " ";
227     }
228
229     return xdprsym(xd, val->val_as.lng, ca->ca_table, sep);
230 }
231
232 /*
233  * Dump field values of a context object to XD.
234  * CA[] describes fields.
235  * PTR points to context object.
236  */
237 void
238 xdflds(struct xdstr *xd, struct castr ca[], void *ptr)
239 {
240     int i, j, n;
241     struct valstr val;
242     char *sep = "";
243
244     for (i = 0; ca[i].ca_name; ++i) {
245         if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
246             continue;
247         if (ca[i].ca_flags & NSC_EXTRA)
248             continue;
249         n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
250         j = 0;
251         do {
252             xdeval(&val, xd, &ca[i], ptr, j);
253             sep = xdprval_sym(xd, &val, &ca[i], sep);
254         } while (++j < n);
255     }
256 }
257
258 /*
259  * Dump header for dump NAME to XD.
260  * If META, it's for the meta-data dump rather than the data dump.
261  */
262 void
263 xdhdr(struct xdstr *xd, char *name, int meta)
264 {
265     if (xd->human) {
266         xd->pr("config %s\n", name);
267     } else
268         xd->pr("XDUMP %s%s %ld\n",
269                meta ? "meta " : "",
270                name, (long)time(NULL));
271 }
272
273 /*
274  * Dump column header to XD.
275  * CA[] describes fields.
276  * Does nothing unless XD is human-readable.
277  */
278 void
279 xdcolhdr(struct xdstr *xd, struct castr ca[])
280 {
281     int i, j, n;
282     char *sep = "";
283
284     if (!xd->human)
285         return;
286
287     for (i = 0; ca[i].ca_name; ++i) {
288         if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
289             continue;
290         if (ca[i].ca_flags & NSC_EXTRA)
291             continue;
292         n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
293         if (n) {
294             for (j = 0; j < n; j++) {
295                 xd->pr("%s%s(%d)", sep, ca[i].ca_name, j);
296                 sep = " ";
297             }
298         } else {
299             xd->pr("%s%s", sep, ca[i].ca_name);
300             sep = " ";
301         }
302     }
303     xd->pr("\n");
304 }
305
306 /* Dump footer for a dump that dumped N objects to XD.  */
307 void
308 xdftr(struct xdstr *xd, int n)
309 {
310     if (xd->human)
311         xd->pr("/config\n");
312     else
313         xd->pr("/%d\n", n);
314 }