]> git.pond.sub.org Git - empserver/blob - src/lib/commands/xdump.c
f09fcb03d31263993d1b12655c4fd8dcbc123d2a
[empserver] / src / lib / commands / xdump.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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: Experimental extended dump
29  * 
30  *  Known contributors to this file:
31  *     Markus Armbruster, 2004-2006
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  * - 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 <stddef.h>
79 #include "commands.h"
80 #include "empobj.h"
81 #include "match.h"
82 #include "news.h"
83 #include "optlist.h"
84 #include "treaty.h"
85 #include "version.h"
86
87 /*
88  * Evaluate a attribute of an object into VAL, return VAL.
89  * TYPE is the attribute's type.
90  * PTR points to the context object.
91  * The attribute is stored there at offset OFF + IDX * S, where S is
92  * its size.
93  * LEN is the #array elements if it is an array, else zero.
94  */
95 static struct valstr *
96 xdeval(struct valstr *val,
97        nsc_type type, void *ptr, ptrdiff_t off, int idx, int len)
98 {
99     val->val_type = type;
100     val->val_cat = NSC_OFF;
101     val->val_as.sym.off = off;
102     val->val_as.sym.len = len;
103     val->val_as.sym.idx = idx;
104     nstr_exec_val(val, player->cnum, ptr, NSC_NOTYPE);
105     return val;                 /* FIXME nstr_exec_val() should return VAL */
106 }
107
108 /* Dump VAL prefixed with SEP, return " ".  */
109 static char *
110 xdprval(struct valstr *val, char *sep)
111 {
112     unsigned char *s, *e, *l;
113
114     switch (val->val_type) {
115     case NSC_LONG:
116         pr("%s%ld", sep, val->val_as.lng);
117         break;
118     case NSC_DOUBLE:
119         pr("%s%#g", sep, val->val_as.dbl);
120         break;
121     case NSC_STRING:
122         s = (unsigned char *)val->val_as.str.base;
123         if (s) {
124             pr("%s\"", sep);
125             l = s + val->val_as.str.maxsz;
126             /* FIXME maxsz == INT_MAX ! */
127             for (;;) {
128                 for (e = s;
129                      e < l && *e != '"' && *e != '\\' && isgraph(*e);
130                      ++e)
131                     ;
132                 pr("%.*s", (int)(e-s), s);
133                 if (e < l && *e)
134                     pr("\\%03o", *e++);
135                 else
136                     break;
137                 s = e;
138             }
139             pr("\"");
140         } else
141             pr("%snil", sep);
142         break;
143     default:
144         CANT_REACH();
145         pr("0");
146     }
147     return " ";
148 }
149
150 /*
151  * Dump field values of a context object.
152  * CA[] describes fields.
153  * PTR points to context object.
154  */
155 static void
156 xdflds(struct castr ca[], void *ptr)
157 {
158     int i, j, n;
159     struct valstr val;
160     char *sep = "";
161
162     for (i = 0; ca[i].ca_name; ++i) {
163         if (ca[i].ca_flags & NSC_DEITY && !player->god)
164             continue;
165         if (ca[i].ca_flags & NSC_EXTRA)
166             continue;
167         n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
168         j = 0;
169         do {
170             xdeval(&val, ca[i].ca_type, ptr, ca[i].ca_off, j, ca[i].ca_len);
171             sep = xdprval(&val, sep);
172         } while (++j < n);
173     }
174 }
175
176 /*
177  * Dump header for dump NAME.
178  * If META, it's for the meta-data dump rather than the data dump.
179  */
180 static void
181 xdhdr(char *name, int meta)
182 {
183     pr("XDUMP %s%s %ld\n", meta ? "meta " : "", name, (long)time(NULL));
184 }
185
186 /* Dump footer for a dump that dumped N objects.  */
187 static void
188 xdftr(int n)
189 {
190     pr("/%d\n", n);
191 }
192
193 /*
194  * Is object P of type TYPE visible to the player?
195  * TODO: Fold this into interators.
196  */
197 static int
198 xdvisible(int type, void *p)
199 {
200     struct empobj *gp = p;
201     struct trtstr *tp = p;
202     struct lonstr *lp = p;
203     struct natstr *natp;
204     int tlev;
205
206     switch (type) {
207     case EF_SECTOR:
208     case EF_REALM:
209         return gp->own == player->cnum || player->god;
210     case EF_SHIP:
211     case EF_PLANE:
212     case EF_LAND:
213     case EF_NUKE:
214     case EF_LOST:
215         return gp->own != 0 && (gp->own == player->cnum || player->god);
216     case EF_NATION:
217         return ((struct natstr *)p)->nat_stat != STAT_UNUSED;
218     case EF_COUNTRY:
219         return gp->own == player->cnum;
220     case EF_NEWS:
221         return ((struct nwsstr *)p)->nws_vrb != 0
222             && (!opt_HIDDEN || player->god); /* FIXME */
223     case EF_TREATY:
224         return tp->trt_status != TS_FREE
225             && (tp->trt_cna == player->cnum || tp->trt_cnb == player->cnum
226                 || player->god);
227     case EF_LOAN:
228         if (lp->l_status == LS_FREE)
229             return 0;
230         if (lp->l_status == LS_SIGNED)
231             return 1;
232         return lp->l_loner == player->cnum || lp->l_lonee == player->cnum
233             || player->god;
234     case EF_TRADE:
235     case EF_COMM:
236         return gp->own != 0;
237     case EF_SHIP_CHR:
238         tlev = ((struct mchrstr *)p)->m_tech;
239         goto tech;
240     case EF_PLANE_CHR:
241         tlev = ((struct plchrstr *)p)->pl_tech;
242         goto tech;
243     case EF_LAND_CHR:
244         tlev = ((struct lchrstr *)p)->l_tech;
245     tech:
246         natp = getnatp(player->cnum);
247         return player->god || tlev <= (int)(1.25 * natp->nat_level[NAT_TLEV]);
248     case EF_NUKE_CHR:
249         tlev = ((struct nchrstr *)p)->n_tech;
250         if (drnuke_const > MIN_DRNUKE_CONST) {
251             natp = getnatp(player->cnum);
252             if (tlev > (int)((int)(1.25 * natp->nat_level[NAT_RLEV])
253                              / drnuke_const))
254                 return player->god;
255         }
256         goto tech;
257     case EF_NEWS_CHR:
258         return ((struct rptstr *)p)->r_newspage != 0;
259     case EF_TABLE:
260         return ((struct empfile *)p)->cadef != NULL;
261     default:
262         return 1;
263     }
264 }
265
266 /*
267  * Dump items of type TYPE selected by ARG.
268  * Return RET_OK on success, RET_SYN on error.
269  */
270 static int
271 xditem(int type, char *arg)
272 {
273     struct castr *ca;
274     struct nstr_item ni;
275     int n;
276     char buf[2048];             /* FIXME buffer size? */
277
278     ca = ef_cadef(type);
279     if (!ca)
280         return RET_SYN;
281
282     if (!snxtitem(&ni, type, arg))
283         return RET_SYN;
284
285     xdhdr(ef_nameof(type), 0);
286
287     n = 0;
288     while (nxtitem(&ni, buf)) {
289         if (!xdvisible(type, buf))
290             continue;
291         ++n;
292         xdflds(ca, buf);
293         pr("\n");
294     }
295
296     xdftr(n);
297
298     return RET_OK;
299 }
300
301 /*
302  * Dump meta-data for items of type TYPE.
303  * Return RET_OK.
304  */
305 static int
306 xdmeta(int type)
307 {
308     struct castr *ca = ef_cadef(type);
309     int i;
310     int n = 0;
311
312     if (!ca)
313         return RET_SYN;
314
315     xdhdr(ef_nameof(type), 1);
316
317     for (i = 0; ca[i].ca_name; i++) {
318         if (ca[i].ca_flags & NSC_DEITY && !player->god)
319             continue;
320         if (ca[i].ca_flags & NSC_EXTRA)
321             continue;
322         xdflds(mdchr_ca, &ca[i]);
323         pr("\n");
324         n++;
325     }
326
327     xdftr(n);
328
329     return RET_OK;
330 }
331
332 /*
333  * Dump configkeys[], return RET_OK.
334  * If META, dump meta-data rather than data.
335  */
336 static int
337 xdver(int meta)
338 {
339     static struct castr vers_ca = {
340         NSC_STRINGY, 0, sizeof(PACKAGE_STRING), 0, "version", EF_BAD
341     };
342     struct keymatch *kp;
343     char *sep;
344     int n;
345     struct castr ca;
346     struct valstr val;
347
348     xdhdr("version", meta);
349
350     if (meta) {
351         n = 0;
352         xdflds(mdchr_ca, &vers_ca);
353         pr("\n");
354         n++;
355         for (kp = configkeys; kp->km_key; ++kp) {
356             if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
357                 ca.ca_type = kp->km_type;
358                 ca.ca_flags = 0;
359                 ca.ca_len = 0;
360                 ca.ca_off = 0;
361                 ca.ca_name = kp->km_key;
362                 ca.ca_table = EF_BAD;
363                 xdflds(mdchr_ca, &ca);
364                 pr("\n");
365                 n++;
366             }
367         }
368         xdftr(n);
369         return RET_OK;
370     }
371
372     xdeval(&val, vers_ca.ca_type, version, vers_ca.ca_off, 0, vers_ca.ca_len);
373     sep = xdprval(&val, "");
374     for (kp = configkeys; kp->km_key; ++kp) {
375         if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
376             xdeval(&val, kp->km_type, kp->km_data, 0, 0, 0);
377             sep = xdprval(&val, sep);
378         }
379     }
380     pr("\n");
381
382     xdftr(1);
383
384     return RET_OK;
385 }
386
387 /* Experimental extended dump command */
388 int
389 xdump(void)
390 {
391     char *p;
392     char buf[1024];
393     int type;
394     int meta = 0;
395
396     p = getstarg(player->argp[1], "Table name, or meta? ", buf);
397     if (p && strcmp(p, "meta") == 0) {
398         meta = 1;
399         p = getstarg(player->argp[2], "Table name? ", buf);
400     }
401     if (!p || !*p)
402         return RET_SYN;
403
404     type = isdigit(p[0]) ? atoi(p) : ef_byname(p);
405     if (type >= 0 && type < EF_MAX) {
406         if (meta)
407             return xdmeta(type);
408         else if ((EF_IS_GAME_STATE(type) || EF_IS_VIEW(type))
409                  && (player->ncomstat & NORM) != NORM) {
410             pr("Access to table %s denied\n", ef_nameof(type));
411             return RET_FAIL;
412         } else
413             return xditem(type, player->argp[2]);
414     } else if (!strncmp(p, "ver", strlen(p))) {
415         return xdver(meta);
416     }
417
418     return RET_SYN;
419 }