]> 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-2021, 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-2016
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 "nat.h"
79 #include "xdump.h"
80
81 /*
82  * Initialize @xd.
83  * If @cnum is NATID_BAD, this is an empdump export rather than an
84  * xdump command.
85  * Translate dump for country @cnum, except when @cnum is NATID_BAD.
86  * Ignore CA_DUMP_ONLY selectors when @cnum is NATID_BAD.
87  * If @human, dump in human-readable format.
88  * If @sloppy, try to cope with invalid data (may result in invalid
89  * dump).
90  * Dump is to be delivered through callback @pr.
91  * Return @xd.
92  */
93 struct xdstr *
94 xdinit(struct xdstr *xd, natid cnum, int human, int sloppy,
95        void (*pr)(char *fmt, ...))
96 {
97     xd->cnum = cnum;
98     xd->divine = cnum == NATID_BAD || getnatp(cnum)->nat_stat == STAT_GOD;
99     xd->human = human;
100     xd->sloppy = sloppy;
101     xd->pr = pr;
102     return xd;
103 }
104
105 /*
106  * Evaluate a attribute of an object into @val, return @val.
107  * @ca describes the attribute.
108  * @xd is the xdump descriptor.
109  * @ptr points to the context object.
110  * @idx is the index within the attribute.
111  */
112 static struct valstr *
113 xdeval(struct valstr *val, struct xdstr *xd,
114        struct castr *ca, void *ptr, int idx)
115 {
116     nstr_mksymval(val, ca, idx);
117     return nstr_eval(val, xd->cnum, ptr, NSC_NOTYPE);
118 }
119
120 /*
121  * Dump string @str to @xd with funny characters escaped.
122  * Dump at most @n characters.
123  */
124 static void
125 xdpresc(struct xdstr *xd, char *str, size_t n)
126 {
127     unsigned char *s, *e;
128
129     s = (unsigned char *)str;
130     for (;;) {
131         for (e = s;
132              n && *e != '"' && *e != '\\' && isgraph(*e);
133              e++, n--)
134             ;
135         xd->pr("%.*s", (int)(e-s), s);
136         if (!n || !*e)
137             break;
138         xd->pr("\\%03o", *e++);
139         n--;
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_dump == CA_DUMP_NONE)
248             continue;
249         if (ca[i].ca_dump == CA_DUMP_ONLY && xd->cnum == NATID_BAD)
250             continue;
251         n = CA_ARRAY_LEN(&ca[i]);
252         j = 0;
253         do {
254             xdeval(&val, xd, &ca[i], ptr, j);
255             sep = xdprval_sym(xd, &val, &ca[i], sep);
256         } while (++j < n);
257     }
258 }
259
260 /*
261  * Dump header for dump @name to @xd.
262  * If @meta, it's for the meta-data dump rather than the data dump.
263  */
264 void
265 xdhdr(struct xdstr *xd, char *name, int meta)
266 {
267     if (xd->human) {
268         xd->pr("config %s\n", name);
269     } else
270         xd->pr("XDUMP %s%s %ld\n",
271                meta ? "meta " : "",
272                name, (long)time(NULL));
273 }
274
275 /*
276  * Dump column header to @xd.
277  * @ca[] describes fields.
278  * Does nothing unless @xd is human-readable.
279  */
280 void
281 xdcolhdr(struct xdstr *xd, struct castr ca[])
282 {
283     int i, j, n;
284     char *sep = "";
285
286     if (!xd->human)
287         return;
288
289     for (i = 0; ca[i].ca_name; ++i) {
290         if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
291             continue;
292         if (ca[i].ca_dump == CA_DUMP_NONE)
293             continue;
294         n = CA_ARRAY_LEN(&ca[i]);
295         if (n) {
296             for (j = 0; j < n; j++) {
297                 xd->pr("%s%s(%d)", sep, ca[i].ca_name, j);
298                 sep = " ";
299             }
300         } else {
301             xd->pr("%s%s", sep, ca[i].ca_name);
302             sep = " ";
303         }
304     }
305     xd->pr("\n");
306 }
307
308 /* Dump footer for a dump that dumped N objects to XD. */
309 void
310 xdftr(struct xdstr *xd, int n)
311 {
312     if (xd->human)
313         xd->pr("/config\n");
314     else
315         xd->pr("/%d\n", n);
316 }