]> git.pond.sub.org Git - empserver/blob - src/lib/commands/xdump.c
Prepare some xdump code for future use outside the server
[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  * CA describes the attribute.
112  * XD is the xdump descriptor.
113  * PTR points to the context object.
114  * IDX is the index within the attribute.
115  */
116 static struct valstr *
117 xdeval(struct valstr *val, struct xdstr *xd,
118        struct castr *ca, void *ptr, int idx)
119 {
120     nstr_mksymval(val, ca, idx);
121     return nstr_exec_val(val, xd->cnum, ptr, NSC_NOTYPE);
122 }
123
124 /*
125  * Dump VAL prefixed with SEP, return " ".
126  * VAL must be evaluated.
127  */
128 static char *
129 xdprval(struct xdstr *xd, struct valstr *val, char *sep)
130 {
131     unsigned char *s, *e, *l;
132
133     if (CANT_HAPPEN(val->val_cat != NSC_VAL)) {
134         xd->pr("%snil", sep);
135         return " ";
136     }
137
138     switch (val->val_type) {
139     case NSC_LONG:
140         xd->pr("%s%ld", sep, val->val_as.lng);
141         break;
142     case NSC_DOUBLE:
143         xd->pr("%s%#g", sep, val->val_as.dbl);
144         break;
145     case NSC_STRING:
146         s = (unsigned char *)val->val_as.str.base;
147         if (s) {
148             xd->pr("%s\"", sep);
149             l = s + val->val_as.str.maxsz;
150             /* FIXME maxsz == INT_MAX ! */
151             for (;;) {
152                 for (e = s;
153                      e < l && *e != '"' && *e != '\\' && isgraph(*e);
154                      ++e)
155                     ;
156                 xd->pr("%.*s", (int)(e-s), s);
157                 if (e < l && *e)
158                     xd->pr("\\%03o", *e++);
159                 else
160                     break;
161                 s = e;
162             }
163             xd->pr("\"");
164         } else
165             xd->pr("%snil", sep);
166         break;
167     default:
168         CANT_REACH();
169         xd->pr("%snil", sep);
170     }
171     return " ";
172 }
173
174 /*
175  * Dump field values of a context object to XD.
176  * CA[] describes fields.
177  * PTR points to context object.
178  */
179 static void
180 xdflds(struct xdstr *xd, struct castr ca[], void *ptr)
181 {
182     int i, j, n;
183     struct valstr val;
184     char *sep = "";
185
186     for (i = 0; ca[i].ca_name; ++i) {
187         if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
188             continue;
189         if (ca[i].ca_flags & NSC_EXTRA)
190             continue;
191         n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
192         j = 0;
193         do {
194             xdeval(&val, xd, &ca[i], ptr, j);
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 /* Extended dump command */
357 int
358 xdump(void)
359 {
360     char *p;
361     char buf[1024];
362     struct xdstr xd;
363     struct natstr *natp;
364     int type;
365     int meta = 0;
366
367     p = getstarg(player->argp[1], "Table name, or meta? ", buf);
368     if (p && strcmp(p, "meta") == 0) {
369         meta = 1;
370         p = getstarg(player->argp[2], "Table name? ", buf);
371     }
372     if (!p || !*p)
373         return RET_SYN;
374
375     xdinit(&xd, player->cnum, pr);
376     natp = getnatp(player->cnum);
377     type = isdigit(p[0]) ? atoi(p) : ef_byname(p);
378     if (type < 0 || type >= EF_MAX)
379         return RET_SYN;
380     if (meta)
381         return xdmeta(&xd, type);
382     if ((EF_IS_GAME_STATE(type) || EF_IS_VIEW(type))
383         && !(natp->nat_stat == STAT_ACTIVE || player->god)) {
384         pr("Access to table %s denied\n", ef_nameof(type));
385         return RET_FAIL;
386     }
387     if (type == EF_VERSION && !player->argp[2])
388         return xditem(&xd, type, "*"); /* backward compatibility */
389     return xditem(&xd, type, player->argp[2]);
390 }