]> git.pond.sub.org Git - empserver/blob - src/lib/commands/xdump.c
f1ca0076180d98dbbeb4435e35bbed670b8777c3
[empserver] / src / lib / commands / xdump.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program 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 2 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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  xdump.c: Extended dump
29  * 
30  *  Known contributors to this file:
31  *     Markus Armbruster, 2004-2008
32  */
33
34 /*
35  * Dump everything under the sun
36  *
37  * Static game data (configuration):
38  * - Item characteristics: ichr[]
39  * - Land unit characteristics: lchr[]
40  * - Nuke characteristics: nchr[]
41  * - Plane characteristics: plchr[]
42  * - Product characteristics: pchr[]
43  * - Sector designation characteristics: dchr[]
44  * - Sector infrastructure characteristics: intrchr[]
45  * - Ship characteristics: mchr[]
46  * - News item characteristics: rpt[]
47  * - News page headings: page_headings[]
48  * - Commands: player_coms[] (TODO)
49  * - Update schedule: update_time[] (not really static)
50  * - Configuration: configkeys[]
51  *
52  * Dynamic game data:
53  * - Sectors: EF_SECTOR (superseding dump)
54  * - Land units: EF_LAND (superseding ldump)
55  * - Lost: EF_LOST (superseding lost)
56  * - Nukes: EF_NUKE (superseding ndump)
57  * - Planes: EF_PLANE (superseding pdump)
58  * - Ships: EF_SHIP (superseding sdump)
59  * - News: EF_NEWS
60  * - Treaties: EF_TREATY
61  * - Power: EF_POWER (TODO)
62  * - Nations: EF_NATION
63  * - Loans: EF_LOAN
64  * - Map: EF_MAP (TODO)
65  * - Bmap: EF_BMAP (TODO)
66  * - Market: EF_COMM
67  */
68
69 /*
70  * See doc/xdump for motivation, syntax, semantics, and rationale.
71  * Make sure to keep it up-to-date!
72  */
73
74 /* TODO don't dump stuff that's useless due to options */
75
76 #include <config.h>
77
78 #include <ctype.h>
79 #include <stddef.h>
80 #include "commands.h"
81 #include "empobj.h"
82 #include "match.h"
83 #include "news.h"
84 #include "optlist.h"
85 #include "treaty.h"
86 #include "version.h"
87
88 /* xdump descriptor */
89 struct xdstr {
90     natid cnum;                 /* dump for this country */
91     int divine;                 /* is this a deity dump? */
92     void (*pr)(char *fmt, ...); /* callback for printing dump */
93 };
94
95 /*
96  * Initialize XD to dump for country CNUM.
97  * Dump is to be delivered through callback PR.
98  * Return XD.
99  */
100 static struct xdstr *
101 xdinit(struct xdstr *xd, natid cnum, void (*pr)(char *fmt, ...))
102 {
103     xd->cnum = cnum;
104     xd->divine = getnatp(cnum)->nat_stat == STAT_GOD;
105     xd->pr = pr;
106     return xd;
107 }
108
109 /*
110  * Evaluate a attribute of an object into VAL, return VAL.
111  * XD is the xdump descriptor.
112  * TYPE is the attribute's type.
113  * PTR points to the context object.
114  * The attribute is stored there at offset OFF + IDX * S, where S is
115  * its size.
116  * LEN is the #array elements if it is an array, else zero.
117  */
118 static struct valstr *
119 xdeval(struct valstr *val, struct xdstr *xd,
120        nsc_type type, void *ptr, ptrdiff_t off, int idx, int len)
121 {
122     val->val_type = type;
123     val->val_cat = NSC_OFF;
124     val->val_as.sym.off = off;
125     val->val_as.sym.len = len;
126     val->val_as.sym.idx = idx;
127     nstr_exec_val(val, xd->cnum, ptr, NSC_NOTYPE);
128     return val;                 /* FIXME nstr_exec_val() should return VAL */
129 }
130
131 /* Dump VAL prefixed with SEP, return " ".  */
132 static char *
133 xdprval(struct xdstr *xd, struct valstr *val, char *sep)
134 {
135     unsigned char *s, *e, *l;
136
137     switch (val->val_type) {
138     case NSC_LONG:
139         xd->pr("%s%ld", sep, val->val_as.lng);
140         break;
141     case NSC_DOUBLE:
142         xd->pr("%s%#g", sep, val->val_as.dbl);
143         break;
144     case NSC_STRING:
145         s = (unsigned char *)val->val_as.str.base;
146         if (s) {
147             xd->pr("%s\"", sep);
148             l = s + val->val_as.str.maxsz;
149             /* FIXME maxsz == INT_MAX ! */
150             for (;;) {
151                 for (e = s;
152                      e < l && *e != '"' && *e != '\\' && isgraph(*e);
153                      ++e)
154                     ;
155                 xd->pr("%.*s", (int)(e-s), s);
156                 if (e < l && *e)
157                     xd->pr("\\%03o", *e++);
158                 else
159                     break;
160                 s = e;
161             }
162             xd->pr("\"");
163         } else
164             xd->pr("%snil", sep);
165         break;
166     default:
167         CANT_REACH();
168         xd->pr("0");
169     }
170     return " ";
171 }
172
173 /*
174  * Dump field values of a context object to XD.
175  * CA[] describes fields.
176  * PTR points to context object.
177  */
178 static void
179 xdflds(struct xdstr *xd, struct castr ca[], void *ptr)
180 {
181     int i, j, n;
182     struct valstr val;
183     char *sep = "";
184
185     for (i = 0; ca[i].ca_name; ++i) {
186         if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
187             continue;
188         if (ca[i].ca_flags & NSC_EXTRA)
189             continue;
190         n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
191         j = 0;
192         do {
193             xdeval(&val, xd,
194                    ca[i].ca_type, ptr, ca[i].ca_off, j, ca[i].ca_len);
195             sep = xdprval(xd, &val, sep);
196         } while (++j < n);
197     }
198 }
199
200 /*
201  * Dump header for dump NAME.
202  * If META, it's for the meta-data dump rather than the data dump.
203  */
204 static void
205 xdhdr(struct xdstr *xd, char *name, int meta)
206 {
207     xd->pr("XDUMP %s%s %ld\n", meta ? "meta " : "", name, (long)time(NULL));
208 }
209
210 /* Dump footer for a dump that dumped N objects.  */
211 static void
212 xdftr(struct xdstr *xd, int n)
213 {
214     xd->pr("/%d\n", n);
215 }
216
217 /*
218  * Is object P of type TYPE visible to the player?
219  * TODO: Fold this into interators.
220  */
221 static int
222 xdvisible(int type, void *p)
223 {
224     struct empobj *gp = p;
225     struct trtstr *tp = p;
226     struct lonstr *lp = p;
227     struct natstr *natp;
228     int tlev;
229
230     switch (type) {
231     case EF_SECTOR:
232     case EF_REALM:
233         return gp->own == player->cnum || player->god;
234     case EF_SHIP:
235     case EF_PLANE:
236     case EF_LAND:
237     case EF_NUKE:
238     case EF_LOST:
239         return gp->own != 0 && (gp->own == player->cnum || player->god);
240     case EF_NATION:
241         return ((struct natstr *)p)->nat_stat != STAT_UNUSED;
242     case EF_COUNTRY:
243         return gp->own == player->cnum;
244     case EF_NEWS:
245         return ((struct nwsstr *)p)->nws_vrb != 0
246             && (!opt_HIDDEN || player->god); /* FIXME */
247     case EF_TREATY:
248         return tp->trt_status != TS_FREE
249             && (tp->trt_cna == player->cnum || tp->trt_cnb == player->cnum
250                 || player->god);
251     case EF_LOAN:
252         if (lp->l_status == LS_FREE)
253             return 0;
254         if (lp->l_status == LS_SIGNED)
255             return 1;
256         return lp->l_loner == player->cnum || lp->l_lonee == player->cnum
257             || player->god;
258     case EF_TRADE:
259     case EF_COMM:
260         return gp->own != 0;
261     case EF_SHIP_CHR:
262         tlev = ((struct mchrstr *)p)->m_tech;
263         goto tech;
264     case EF_PLANE_CHR:
265         tlev = ((struct plchrstr *)p)->pl_tech;
266         goto tech;
267     case EF_LAND_CHR:
268         tlev = ((struct lchrstr *)p)->l_tech;
269     tech:
270         natp = getnatp(player->cnum);
271         return player->god || tlev <= (int)(1.25 * natp->nat_level[NAT_TLEV]);
272     case EF_NUKE_CHR:
273         tlev = ((struct nchrstr *)p)->n_tech;
274         if (drnuke_const > MIN_DRNUKE_CONST) {
275             natp = getnatp(player->cnum);
276             if (tlev > (int)((int)(1.25 * natp->nat_level[NAT_RLEV])
277                              / drnuke_const))
278                 return player->god;
279         }
280         goto tech;
281     case EF_NEWS_CHR:
282         return ((struct rptstr *)p)->r_newspage != 0;
283     case EF_TABLE:
284         return ((struct empfile *)p)->cadef != NULL;
285     default:
286         return 1;
287     }
288 }
289
290 /*
291  * Dump items of type TYPE selected by ARG to XD.
292  * Return RET_OK on success, RET_SYN on error.
293  */
294 static int
295 xditem(struct xdstr *xd, int type, char *arg)
296 {
297     struct castr *ca;
298     struct nstr_item ni;
299     int n;
300     char buf[2048];             /* FIXME buffer size? */
301
302     ca = ef_cadef(type);
303     if (!ca)
304         return RET_SYN;
305
306     if (!snxtitem(&ni, type, arg))
307         return RET_SYN;
308
309     xdhdr(xd, ef_nameof(type), 0);
310
311     n = 0;
312     while (nxtitem(&ni, buf)) {
313         if (!xdvisible(type, buf))
314             continue;
315         ++n;
316         xdflds(xd, ca, buf);
317         xd->pr("\n");
318     }
319
320     xdftr(xd, n);
321
322     return RET_OK;
323 }
324
325 /*
326  * Dump meta-data for items of type TYPE to XD.
327  * Return RET_OK.
328  */
329 static int
330 xdmeta(struct xdstr *xd, int type)
331 {
332     struct castr *ca = ef_cadef(type);
333     int i;
334     int n = 0;
335
336     if (!ca)
337         return RET_SYN;
338
339     xdhdr(xd, ef_nameof(type), 1);
340
341     for (i = 0; ca[i].ca_name; i++) {
342         if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
343             continue;
344         if (ca[i].ca_flags & NSC_EXTRA)
345             continue;
346         xdflds(xd, mdchr_ca, &ca[i]);
347         xd->pr("\n");
348         n++;
349     }
350
351     xdftr(xd, n);
352
353     return RET_OK;
354 }
355
356 /*
357  * Dump configkeys[] to XD.
358  * If META, dump meta-data rather than data.
359  * Return RET_OK.
360  */
361 static int
362 xdver(struct xdstr *xd, int meta)
363 {
364     static struct castr vers_ca = {
365         NSC_STRINGY, 0, sizeof(PACKAGE_STRING), 0, "version", EF_BAD
366     };
367     struct keymatch *kp;
368     char *sep;
369     int n;
370     struct castr ca;
371     struct valstr val;
372
373     xdhdr(xd, "version", meta);
374
375     if (meta) {
376         n = 0;
377         xdflds(xd, mdchr_ca, &vers_ca);
378         pr("\n");
379         n++;
380         for (kp = configkeys; kp->km_key; ++kp) {
381             if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
382                 ca.ca_type = kp->km_type;
383                 ca.ca_flags = 0;
384                 ca.ca_len = 0;
385                 ca.ca_off = 0;
386                 ca.ca_name = kp->km_key;
387                 ca.ca_table = EF_BAD;
388                 xdflds(xd, mdchr_ca, &ca);
389                 pr("\n");
390                 n++;
391             }
392         }
393         xdftr(xd, n);
394         return RET_OK;
395     }
396
397     xdeval(&val, xd,
398            vers_ca.ca_type, version, vers_ca.ca_off, 0, vers_ca.ca_len);
399     sep = xdprval(xd, &val, "");
400     for (kp = configkeys; kp->km_key; ++kp) {
401         if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
402             xdeval(&val, xd, kp->km_type, kp->km_data, 0, 0, 0);
403             sep = xdprval(xd, &val, sep);
404         }
405     }
406     pr("\n");
407
408     xdftr(xd, 1);
409
410     return RET_OK;
411 }
412
413 /* Experimental extended dump command */
414 int
415 xdump(void)
416 {
417     char *p;
418     char buf[1024];
419     struct xdstr xd;
420     struct natstr *natp;
421     int type;
422     int meta = 0;
423
424     p = getstarg(player->argp[1], "Table name, or meta? ", buf);
425     if (p && strcmp(p, "meta") == 0) {
426         meta = 1;
427         p = getstarg(player->argp[2], "Table name? ", buf);
428     }
429     if (!p || !*p)
430         return RET_SYN;
431
432     xdinit(&xd, player->cnum, pr);
433     natp = getnatp(player->cnum);
434     type = isdigit(p[0]) ? atoi(p) : ef_byname(p);
435     if (type >= 0 && type < EF_MAX) {
436         if (meta)
437             return xdmeta(&xd, type);
438         else if ((EF_IS_GAME_STATE(type) || EF_IS_VIEW(type))
439                  && !(natp->nat_stat == STAT_ACTIVE || player->god)) {
440             pr("Access to table %s denied\n", ef_nameof(type));
441             return RET_FAIL;
442         } else
443             return xditem(&xd, type, player->argp[2]);
444     } else if (!strncmp(p, "ver", strlen(p))) {
445         return xdver(&xd, meta);
446     }
447
448     return RET_SYN;
449 }