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