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