]> git.pond.sub.org Git - empserver/blob - src/lib/common/xdump.c
Remove option TREATIES
[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  * - 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_exec_val(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, *l;
126
127     s = (unsigned char *)str;
128     l = s + n;
129     for (;;) {
130         for (e = s;
131              e < l && *e != '"' && *e != '\\' && isgraph(*e);
132              ++e)
133             ;
134         xd->pr("%.*s", (int)(e-s), s);
135         if (e < l && *e)
136             xd->pr("\\%03o", *e++);
137         else
138             break;
139         s = e;
140     }
141 }
142
143 /*
144  * Dump VAL prefixed with SEP to XD, in machine readable format.
145  * VAL must be evaluated.
146  * Return " ".
147  */
148 static char *
149 xdprval_nosym(struct xdstr *xd, struct valstr *val, char *sep)
150 {
151     if (CANT_HAPPEN(val->val_cat != NSC_VAL)) {
152         xd->pr("%snil", sep);
153         return " ";
154     }
155
156     switch (val->val_type) {
157     case NSC_LONG:
158         xd->pr("%s%ld", sep, val->val_as.lng);
159         break;
160     case NSC_DOUBLE:
161         xd->pr("%s%#g", sep, val->val_as.dbl);
162         break;
163     case NSC_STRING:
164         if (val->val_as.str.base) {
165             xd->pr("%s\"", sep);
166             xdpresc(xd, val->val_as.str.base, val->val_as.str.maxsz);
167             xd->pr("\"");
168         } else
169             xd->pr("%snil", sep);
170         break;
171     default:
172         CANT_REACH();
173         xd->pr("%snil", sep);
174     }
175     return " ";
176 }
177
178 /*
179  * Dump symbol with value KEY from symbol table TYPE to XD.
180  * Prefix with SEP, return " ".
181  */
182 static char *
183 xdprsym(struct xdstr *xd, int key, int type, char *sep)
184 {
185     char *sym = symbol_by_value(key, ef_ptr(type, 0));
186
187     if (!sym) {
188         CANT_HAPPEN(!xd->sloppy);
189         xd->pr("%s%d", sep, key);
190     } else {
191         xd->pr("%s", sep);
192         xdpresc(xd, sym, INT_MAX);
193     }
194     return " ";
195 }
196
197 /*
198  * Dump VAL prefixed with SEP to XD, return " ".
199  * VAL must be evaluated.
200  * CA describes the field from which the value was fetched.
201  */
202 static char *
203 xdprval_sym(struct xdstr *xd, struct valstr *val, struct castr *ca,
204             char *sep)
205 {
206     unsigned long bit;
207
208     if (CANT_HAPPEN(val->val_cat != NSC_VAL)) {
209         xd->pr("%snil", sep);
210         return " ";
211     }
212
213     if (!xd->human || val->val_type != NSC_LONG
214         || ca->ca_table == EF_BAD || ef_cadef(ca->ca_table) != symbol_ca)
215         return xdprval_nosym(xd, val, sep);
216
217     if (ca->ca_flags & NSC_BITS) {
218         xd->pr("%s(", sep);
219         sep = "";
220         for (bit = 1; bit; bit <<= 1) {
221             if (bit & val->val_as.lng)
222                 sep = xdprsym(xd, bit, ca->ca_table, sep);
223         }
224         xd->pr(")");
225         return " ";
226     }
227
228     return xdprsym(xd, val->val_as.lng, ca->ca_table, sep);
229 }
230
231 /*
232  * Dump field values of a context object to XD.
233  * CA[] describes fields.
234  * PTR points to context object.
235  */
236 void
237 xdflds(struct xdstr *xd, struct castr ca[], void *ptr)
238 {
239     int i, j, n;
240     struct valstr val;
241     char *sep = "";
242
243     for (i = 0; ca[i].ca_name; ++i) {
244         if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
245             continue;
246         if (ca[i].ca_flags & NSC_EXTRA)
247             continue;
248         n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
249         j = 0;
250         do {
251             xdeval(&val, xd, &ca[i], ptr, j);
252             sep = xdprval_sym(xd, &val, &ca[i], sep);
253         } while (++j < n);
254     }
255 }
256
257 /*
258  * Dump header for dump NAME to XD.
259  * If META, it's for the meta-data dump rather than the data dump.
260  */
261 void
262 xdhdr(struct xdstr *xd, char *name, int meta)
263 {
264     if (xd->human) {
265         xd->pr("config %s\n", name);
266     } else
267         xd->pr("XDUMP %s%s %ld\n",
268                meta ? "meta " : "",
269                name, (long)time(NULL));
270 }
271
272 /*
273  * Dump column header to XD.
274  * CA[] describes fields.
275  * Does nothing unless XD is human-readable.
276  */
277 void
278 xdcolhdr(struct xdstr *xd, struct castr ca[])
279 {
280     int i, j, n;
281     char *sep = "";
282
283     if (!xd->human)
284         return;
285
286     for (i = 0; ca[i].ca_name; ++i) {
287         if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
288             continue;
289         if (ca[i].ca_flags & NSC_EXTRA)
290             continue;
291         n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
292         if (n) {
293             for (j = 0; j < n; j++) {
294                 xd->pr("%s%s(%d)", sep, ca[i].ca_name, j);
295                 sep = " ";
296             }
297         } else {
298             xd->pr("%s%s", sep, ca[i].ca_name);
299             sep = " ";
300         }
301     }
302     xd->pr("\n");
303 }
304
305 /* Dump footer for a dump that dumped N objects to XD.  */
306 void
307 xdftr(struct xdstr *xd, int n)
308 {
309     if (xd->human)
310         xd->pr("/config\n");
311     else
312         xd->pr("/%d\n", n);
313 }