]> git.pond.sub.org Git - empserver/blob - src/lib/commands/xdump.c
Include config.h.
[empserver] / src / lib / commands / xdump.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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
32  */
33
34 #include <config.h>
35
36 #include <stddef.h>
37 #include "misc.h"
38 #include "file.h"
39 #include "match.h"
40 #include "news.h"
41 #include "nsc.h"
42 #include "optlist.h"
43 #include "commands.h"
44
45 /*
46  * Dump everything under the sun
47  *
48  * Static game data (configuration):
49  * - Item characteristics: ichr[]
50  * - Land unit characteristics: lchr[]
51  * - Nuke characteristics: nchr[]
52  * - Plane characteristics: plchr[]
53  * - Product characteristics: pchr[]
54  * - Sector designation characteristics: dchr[]
55  * - Sector infrastructure characteristics: intrchr[]
56  * - Ship characteristics: mchr[]
57  * Less important:
58  * - News item characteristics: rpt[]
59  * - News page headings: page_headings[] (TODO)
60  * - Commands: player_coms[] (TODO)
61  * - Configuration: configkeys[]
62  *
63  * Dynamic game data:
64  * - Sectors: EF_SECTOR, sect_ca[] (already have dump)
65  * - Land units: EF_LAND, land_ca[] (already have ldump)
66  * - Lost: EF_LOST, lost_ca[] (already have lost)
67  * - Nukes: EF_NUKE, nuke_ca[] (already have ndump)
68  * - Planes: EF_PLANE, plane_ca[] (already have pdump)
69  * - Ships: EF_SHIP, ship_ca[] (already have sdump)
70  * - News: EF_NEWS, news_ca[]
71  * - Treaties: EF_TREATY, treaty_ca[]
72  * - Power: EF_POWER
73  * - Nations: EF_NATION, nat_ca[]
74  * - Loans: EF_LOAN, loan_ca[]
75  * - Map: EF_MAP (TODO)
76  * - Bmap: EF_BMAP (TODO)
77  * - Market: EF_COMM, commodity_ca[]
78  */
79
80 /* FIXME document dump format */
81 /* FIXME don't dump stuff that's useless due to options */
82
83 /*
84  * Evaluate a attribute of an object into VAL, return VAL.
85  * TYPE is the attribute's type.
86  * PTR points to the context object.
87  * The attribute is stored there at offset OFF + IDX * S, where S is
88  * its size.
89  */
90 static struct valstr *
91 xdeval(struct valstr *val, nsc_type type, void *ptr, ptrdiff_t off, int idx)
92 {
93     val->val_type = type;
94     val->val_cat = NSC_OFF;
95     val->val_as.sym.off = off;
96     val->val_as.sym.idx = idx;
97     nstr_exec_val(val, player->cnum, ptr, NSC_NOTYPE);
98     return val;                 /* FIXME nstr_exec_val() should return VAL */
99 }
100
101 /* Dump VAL prefixed with SEP, return " ".  */
102 static char *
103 xdprval(struct valstr *val, char *sep)
104 {
105     unsigned char *s, *e, *l;
106
107     switch (val->val_type) {
108     case NSC_TYPEID:
109     case NSC_LONG:
110         pr("%s%ld", sep, val->val_as.lng);
111         break;
112     case NSC_DOUBLE:
113         pr("%s%#g", sep, val->val_as.dbl);
114         break;
115     case NSC_STRING:
116         s = (unsigned char *)val->val_as.str.base;
117         if (s) {
118             pr("%s\"", sep);
119             l = s + val->val_as.str.maxsz;
120             /* FIXME maxsz == INT_MAX ! */
121             for (;;) {
122                 for (e=s; e<l && *e != '"' && *e != '\\' && isgraph(*e); ++e) ;
123                 pr("%.*s", (int)(e-s), s);
124                 if (e < l && *e)
125                     pr("\\%03o", *e++);
126                 else
127                     break;
128                 s = e;
129             }
130             pr("\"");
131         } else
132             pr("%snil", sep);
133         break;
134     default:
135         CANT_HAPPEN("Bad VAL type");
136         pr("0");
137     }
138     return " ";
139 }
140
141 /*
142  * Dump field values of a context object.
143  * CA[] describes fields.
144  * PTR points to context object.
145  */
146 static void
147 xdflds(struct castr ca[], void *ptr)
148 {
149     int i, j, n;
150     struct valstr val;
151     char *sep = "";
152
153     for (i = 0; ca[i].ca_name; ++i) {
154         if (ca[i].ca_flags & NSC_DEITY && !player->god)
155             continue;
156         if (ca[i].ca_flags & NSC_EXTRA)
157             continue;
158         n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
159         j = 0;
160         do {
161             xdeval(&val, ca[i].ca_type, ptr, ca[i].ca_off, j);
162             sep = xdprval(&val, sep);
163         } while (++j < n);
164     }
165 }
166
167 /*
168  * Dump header for dump NAME.
169  * If META, it's for the meta-data dump rather than the data dump.
170  */
171 static void
172 xdhdr(char *name, int meta)
173 {
174     pr("XDUMP %s%s %ld\n", meta ? "meta " : "", name, (long)time(NULL));
175 }
176
177 /* Dump footer for a dump that dumped N objects.  */
178 static void
179 xdftr(int n)
180 {
181     pr("/%d\n", n);
182 }
183
184 /*
185  * Dump items of type TYPE selected by ARG.
186  * Return RET_OK on success, RET_SYN on error.
187  */
188 static int
189 xditem(int type, char *arg)
190 {
191     int check_owner = !player->god && (ef_flags(type) & EFF_OWNER) != 0;
192     struct castr *ca;
193     struct nstr_item ni;
194     int n;
195     char buf[2048];             /* FIXME buffer size? */
196
197     ca = ef_cadef(type);
198     if (!ca)
199         return RET_SYN;
200
201     if (!snxtitem(&ni, type, arg))
202         return RET_SYN;
203
204     xdhdr(ef_nameof(type), 0);
205
206     n = 0;
207     while (nxtitem(&ni, buf)) {
208         if (check_owner && ((struct genitem *)buf)->own != player->cnum)
209             continue;
210         ++n;
211         xdflds(ca, buf);
212         pr("\n");
213     }
214
215     xdftr(n);
216
217     return RET_OK;
218 }
219
220 /*
221  * Dump meta-data for items of type TYPE.
222  * Return RET_OK.
223  */
224 static int
225 xdmeta(int type)
226 {
227     struct castr *ca = ef_cadef(type);
228     int i;
229     int n = 0;
230
231     if (!ca)
232         return RET_SYN;
233
234     xdhdr(ef_nameof(type), 1);
235
236     for (i = 0; ca[i].ca_name; i++) {
237         if (ca[i].ca_flags & NSC_DEITY && !player->god)
238             continue;
239         if (ca[i].ca_flags & NSC_EXTRA)
240             continue;
241         xdflds(mdchr_ca, &ca[i]);
242         n++;
243         pr("\n");
244     }
245
246     xdftr(n);
247
248     return RET_OK;
249 }
250
251 /*
252  * Dump configkeys[], return RET_OK.
253  * If META, dump meta-data rather than data.
254  */
255 static int
256 xdver(int meta)
257 {
258     struct keymatch *kp;
259     char *sep;
260     int n;
261     struct castr ca;
262     struct valstr val;
263
264     xdhdr("version", meta);
265
266     if (meta) {
267         n = 0;
268         for (kp = configkeys; kp->km_key; ++kp) {
269             if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
270                 ca.ca_type = kp->km_type;
271                 ca.ca_flags = 0;
272                 ca.ca_len = 0;
273                 ca.ca_off = 0;
274                 ca.ca_name = kp->km_key;
275                 ca.ca_table = EF_BAD;
276                 xdflds(mdchr_ca, &ca);
277                 pr("\n");
278                 n++;
279             }
280         }
281         xdftr(n);
282         return RET_OK;
283     }
284
285     sep = "";
286     for (kp = configkeys; kp->km_key; ++kp) {
287         if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
288             xdeval(&val, kp->km_type, kp->km_data, 0, 0);
289             sep = xdprval(&val, sep);
290         }
291     }
292     pr("\n");
293
294     xdftr(1);
295
296     return RET_OK;
297 }
298
299 /* Experimental extended dump command */
300 int
301 xdump(void)
302 {
303     char *p;
304     char buf[1024];
305     int type;
306     int meta = 0;
307
308     if (!opt_GUINEA_PIGS) {
309         pr("You are not a guinea pig!\n");
310         return RET_FAIL;
311     }
312
313     p = getstarg(player->argp[1], "What? ", buf);
314     if (p && strcmp(p, "meta") == 0) {
315         meta = 1;
316         p = getstarg(player->argp[2], "What? ", buf);
317     }
318     if (!p)
319         return RET_SYN;
320
321     type = isdigit(p[0]) ? atoi(p) : ef_byname(p);
322     if (type >= 0 && type < EF_MAX) {
323         if (meta)
324             return xdmeta(type);
325         else
326             return xditem(type, player->argp[2]);
327     } else if (!strncmp(p, "ver", strlen(p))) {
328         return xdver(meta);
329     }
330
331     return RET_SYN;
332 }