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