]> git.pond.sub.org Git - empserver/blob - src/lib/commands/xdump.c
(ichr_ca,pchr_ca,dchr_ca,mchr_ca,plchr_ca,lchr_ca,nchr_ca,
[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[] (TODO)
57  * - News page headings: page_headings[] (TODO)
58  * - Treaty clause characteristics: tchr[]
59  * - Commands: player_coms[] (TODO)
60  * - Options: Options[]
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 /* Selector descriptors for characteristics tables */
84
85 /* Characteristics table meta data */
86 struct camap {
87     char *name;                 /* name for lookup */
88     struct castr *ca;           /* selector descriptors */
89     void *chr;                  /* characteristics table */
90     size_t size;                /* size of characteristics table element */
91 };
92
93 /* Table of characteristics tables */
94 static struct camap chr_camap[] = {
95     {"sect chr", dchr_ca, dchr, sizeof(dchr[0])},
96     {"ship chr", mchr_ca, mchr, sizeof(mchr[0])},
97     {"plane chr", plchr_ca, plchr, sizeof(plchr[0])},
98     {"land chr", lchr_ca, lchr, sizeof(lchr[0])},
99     {"nuke chr", nchr_ca, nchr, sizeof(nchr[0])},
100 #if 0
101     /* FIXME rpt[] lacks sentinel, xdchr() doesn't terminate */
102     {"news chr", rpt_ca, rpt, sizeof(rpt[0])},
103 #endif
104     {"treaty chr", tchr_ca, tchr, sizeof(tchr[0])},
105     {"item", ichr_ca, ichr, sizeof(ichr[0])},
106     {"infrastructure", intrchr_ca, intrchr, sizeof(intrchr[0])},
107     {"product", pchr_ca, pchr, sizeof(pchr[0])},
108     {NULL, NULL, NULL, 0}
109 };
110
111 /*
112  * Search chr_camap[] for element named NAME, return its index.
113  * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
114  * several.
115  */
116 static int
117 chridx_by_name(char *name)
118 {
119     return stmtch(name, chr_camap, offsetof(struct camap, name),
120                   sizeof(chr_camap[0]));
121 }
122
123 /*
124  * Evaluate a attribute of an object into VAL, return VAL.
125  * TYPE is the attribute's type.
126  * PTR points to the context object.
127  * The attribute is stored there at offset OFF + IDX * S, where S is
128  * its size.
129  */
130 static struct valstr *
131 xdeval(struct valstr *val, nsc_type type, void *ptr, ptrdiff_t off, int idx)
132 {
133     val->val_type = type;
134     val->val_cat = NSC_OFF;
135     val->val_as.sym.off = off;
136     val->val_as.sym.idx = idx;
137     nstr_exec_val(val, player->cnum, ptr, NSC_NOTYPE);
138     return val;                 /* FIXME nstr_exec_val() should return VAL */
139 }
140
141 /* Dump VAL prefixed with SEP, return " ".  */
142 static char *
143 xdprval(struct valstr *val, char *sep)
144 {
145     unsigned char *s, *e, *l;
146
147     switch (val->val_type) {
148     case NSC_TYPEID:
149     case NSC_LONG:
150         pr("%s%ld", sep, val->val_as.lng);
151         break;
152     case NSC_DOUBLE:
153         pr("%s%#g", sep, val->val_as.dbl);
154         break;
155     case NSC_STRING:
156         s = (unsigned char *)val->val_as.str.base;
157         if (s) {
158             pr("%s\"", sep);
159             l = s + val->val_as.str.maxsz;
160             for (;;) {
161                 for (e=s; e<l && *e != '"' && *e != '\\' && isgraph(*e); ++e) ;
162                 pr("%.*s", (int)(e-s), s);
163                 if (e < l && *e)
164                     pr("\\%03o", *e++);
165                 else
166                     break;
167                 s = e;
168             }
169             pr("\"");
170         } else
171             pr("%snil", sep);
172         break;
173     default:
174         CANT_HAPPEN("Bad VAL type");
175         pr("0");
176     }
177     return " ";
178 }
179
180 /*
181  * Dump field values of a context object.
182  * CA[] describes fields.
183  * PTR points to context object.
184  */
185 static void
186 xdflds(struct castr ca[], void *ptr)
187 {
188     int i, j, n;
189     struct valstr val;
190     char *sep = "";
191
192     for (i = 0; ca[i].ca_name; ++i) {
193         if (ca[i].ca_flags & NSC_DEITY && !player->god)
194             continue;
195         if (ca[i].ca_flags & NSC_EXTRA)
196             continue;
197         n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
198         j = 0;
199         do {
200             xdeval(&val, ca[i].ca_type, ptr, ca[i].ca_off, j);
201             sep = xdprval(&val, sep);
202         } while (++j < n);
203     }
204 }
205
206 /* Dump field names; CA[] describes fields.  */
207 static void
208 xdfldnam(struct castr ca[])
209 {
210     int i;
211     char *sep = "";
212
213     for (i = 0; ca[i].ca_name; ++i) {
214         if (ca[i].ca_flags & NSC_DEITY && !player->god)
215             continue;
216         if (ca[i].ca_flags & NSC_EXTRA)
217             continue;
218         pr("%s%s", sep, ca[i].ca_name);
219         if (ca[i].ca_len && ca[i].ca_type != NSC_STRINGY)
220             pr(" %d", ca[i].ca_len);
221         sep = " ";
222     }
223 }
224
225 /* Dump first line of header for dump NAME.  */
226 static void
227 xdhdr1(char *name)
228 {
229     pr("XDUMP %s %ld\n", name, (long)time(NULL));
230 }
231
232 /* Dump header for dump NAME with fields described by CA[].  */
233 static void
234 xdhdr(char *name, struct castr ca[])
235 {
236     xdhdr1(name);
237     xdfldnam(ca);
238     pr("\n");
239 }
240
241 /* Dump footer for a dump that dumped N objects.  */
242 static void
243 xdftr(int n)
244 {
245     pr("dumped %d\n", n);
246 }
247
248 /*
249  * Dump items of type TYPE selected by ARG.
250  * Return RET_OK on success, RET_SYN on error.
251  */
252 static int
253 xditem(int type, char *arg)
254 {
255     int check_owner = !player->god && (ef_flags(type) & EFF_OWNER) != 0;
256     struct castr *ca;
257     struct nstr_item ni;
258     int n;
259     char buf[2048];             /* FIXME buffer size? */
260
261     ca = ef_cadef(type);
262     if (!ca)
263         return RET_SYN;
264
265     if (!snxtitem(&ni, type, arg))
266         return RET_SYN;
267
268     xdhdr(ef_nameof(type), ca);
269
270     n = 0;
271     while (nxtitem(&ni, buf)) {
272         if (check_owner && ((struct genitem *)buf)->own != player->cnum)
273             continue;
274         ++n;
275         xdflds(ca, buf);
276         pr("\n");
277     }
278
279     xdftr(n);
280
281     return RET_OK;
282 }
283
284 /*
285  * Dump characteristics described by chr_camap[IDX].
286  * Return RET_OK on success, RET_SYN if IDX < 0.
287  */
288 static int
289 xdchr(int chridx)
290 {
291     struct camap *cm;
292     char *p;
293     struct valstr val;
294     int n;
295
296     if (chridx < 0)
297         return RET_SYN;
298     cm = &chr_camap[chridx];
299
300     xdhdr(cm->name, cm->ca);
301
302     n = 0;
303     for (p = cm->chr; ; p += cm->size) {
304         val.val_type = cm->ca[0].ca_type;
305         val.val_cat = NSC_OFF;
306         val.val_as.sym.off = cm->ca[0].ca_off;
307         val.val_as.sym.idx = 0;
308         nstr_exec_val(&val, player->cnum, p, NSC_STRING);
309         if (!val.val_as.str.base || !*val.val_as.str.base)
310             break;
311         ++n;
312         xdflds(cm->ca, p);
313         pr("\n");
314     }
315
316     xdftr(n);
317
318     return RET_OK;
319 }
320
321 /* Dump Options[], return RET_OK.  */
322 static int
323 xdopt(void)
324 {
325     int i;
326     char *sep;
327
328     xdhdr1("options");
329
330     sep = "";
331     for (i = 0; Options[i].opt_key; ++i) {
332         pr("%s%s", sep, Options[i].opt_key);
333         sep = " ";
334     }
335     pr("\n");
336     
337     sep = "";
338     for (i = 0; Options[i].opt_key; ++i) {
339         pr("%s%d", sep, *Options[i].opt_valuep);
340         sep = " ";
341     }
342     pr("\n");
343
344     return RET_OK;
345 }
346
347 static int
348 xdver(void)
349 {
350     struct keymatch *kp;
351     char *sep;
352     struct valstr val;
353
354     xdhdr1("version");
355
356     sep = "";
357     for (kp = configkeys; kp->km_key; ++kp) {
358         if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
359             pr("%s%s", sep, kp->km_key);
360             sep = " ";
361         }
362     }
363     pr("\n");
364     
365     sep = "";
366     for (kp = configkeys; kp->km_key; ++kp) {
367         if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
368             xdeval(&val, kp->km_type, kp->km_data, 0, 0);
369             sep = xdprval(&val, sep);
370         }
371     }
372     pr("\n");
373
374     return RET_OK;
375 }
376
377 /* Experimental extended dump command */
378 int
379 xdump(void)
380 {
381     char *p;
382     char buf[1024];
383     int type;
384
385     if (!opt_GUINEA_PIGS) {
386         pr("You are not a guinea pig!\n");
387         return RET_FAIL;
388     }
389
390     p = getstarg(player->argp[1], "What? ", buf);
391     if (!p)
392         return RET_SYN;
393
394     type = ef_byname(p);
395     if (type >= 0) {
396         return xditem(type, player->argp[2]);
397     } else if (!strncmp(p, "chr", strlen(p)) && player->argp[2]) {
398         return xdchr(chridx_by_name(player->argp[2]));
399     } else if (!strncmp(p, "opt", strlen(p))) {
400         return xdopt();
401     } else if (!strncmp(p, "ver", strlen(p))) {
402         return xdver();
403     }
404
405     return RET_SYN;
406 }