]> git.pond.sub.org Git - empserver/blob - src/lib/commands/xdump.c
(xdump): Nicer prompts.
[empserver] / src / lib / commands / xdump.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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: Experimental extended dump
29  * 
30  *  Known contributors to this file:
31  *     Markus Armbruster, 2004-2006
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 "version.h"
44 #include "commands.h"
45
46 /*
47  * Dump everything under the sun
48  *
49  * Static game data (configuration):
50  * - Item characteristics: ichr[]
51  * - Land unit characteristics: lchr[]
52  * - Nuke characteristics: nchr[]
53  * - Plane characteristics: plchr[]
54  * - Product characteristics: pchr[]
55  * - Sector designation characteristics: dchr[]
56  * - Sector infrastructure characteristics: intrchr[]
57  * - Ship characteristics: mchr[]
58  * - News item characteristics: rpt[]
59  * - News page headings: page_headings[]
60  * - Commands: player_coms[] (TODO)
61  * - Configuration: configkeys[]
62  *
63  * Dynamic game data:
64  * - Sectors: EF_SECTOR (superseding dump)
65  * - Land units: EF_LAND (superseding ldump)
66  * - Lost: EF_LOST (superseding lost)
67  * - Nukes: EF_NUKE (superseding ndump)
68  * - Planes: EF_PLANE (superseding pdump)
69  * - Ships: EF_SHIP (superseding sdump)
70  * - News: EF_NEWS
71  * - Treaties: EF_TREATY
72  * - Power: EF_POWER (TODO)
73  * - Nations: EF_NATION
74  * - Loans: EF_LOAN
75  * - Map: EF_MAP (TODO)
76  * - Bmap: EF_BMAP (TODO)
77  * - Market: EF_COMM
78  */
79
80 /* FIXME document dump format */
81 /* TODO 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  * LEN is the #array elements if it is an array, else zero.
90  */
91 static struct valstr *
92 xdeval(struct valstr *val,
93        nsc_type type, void *ptr, ptrdiff_t off, int idx, int len)
94 {
95     val->val_type = type;
96     val->val_cat = NSC_OFF;
97     val->val_as.sym.off = off;
98     val->val_as.sym.len = len;
99     val->val_as.sym.idx = idx;
100     nstr_exec_val(val, player->cnum, ptr, NSC_NOTYPE);
101     return val;                 /* FIXME nstr_exec_val() should return VAL */
102 }
103
104 /* Dump VAL prefixed with SEP, return " ".  */
105 static char *
106 xdprval(struct valstr *val, char *sep)
107 {
108     unsigned char *s, *e, *l;
109
110     switch (val->val_type) {
111     case NSC_TYPEID:
112     case NSC_LONG:
113         pr("%s%ld", sep, val->val_as.lng);
114         break;
115     case NSC_DOUBLE:
116         pr("%s%#g", sep, val->val_as.dbl);
117         break;
118     case NSC_STRING:
119         s = (unsigned char *)val->val_as.str.base;
120         if (s) {
121             pr("%s\"", sep);
122             l = s + val->val_as.str.maxsz;
123             /* FIXME maxsz == INT_MAX ! */
124             for (;;) {
125                 for (e=s; e<l && *e != '"' && *e != '\\' && isgraph(*e); ++e) ;
126                 pr("%.*s", (int)(e-s), s);
127                 if (e < l && *e)
128                     pr("\\%03o", *e++);
129                 else
130                     break;
131                 s = e;
132             }
133             pr("\"");
134         } else
135             pr("%snil", sep);
136         break;
137     default:
138         CANT_HAPPEN("Bad VAL type");
139         pr("0");
140     }
141     return " ";
142 }
143
144 /*
145  * Dump field values of a context object.
146  * CA[] describes fields.
147  * PTR points to context object.
148  */
149 static void
150 xdflds(struct castr ca[], void *ptr)
151 {
152     int i, j, n;
153     struct valstr val;
154     char *sep = "";
155
156     for (i = 0; ca[i].ca_name; ++i) {
157         if (ca[i].ca_flags & NSC_DEITY && !player->god)
158             continue;
159         if (ca[i].ca_flags & NSC_EXTRA)
160             continue;
161         n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
162         j = 0;
163         do {
164             xdeval(&val, ca[i].ca_type, ptr, ca[i].ca_off, j, ca[i].ca_len);
165             sep = xdprval(&val, sep);
166         } while (++j < n);
167     }
168 }
169
170 /*
171  * Dump header for dump NAME.
172  * If META, it's for the meta-data dump rather than the data dump.
173  */
174 static void
175 xdhdr(char *name, int meta)
176 {
177     pr("XDUMP %s%s %ld\n", meta ? "meta " : "", name, (long)time(NULL));
178 }
179
180 /* Dump footer for a dump that dumped N objects.  */
181 static void
182 xdftr(int n)
183 {
184     pr("/%d\n", n);
185 }
186
187 /*
188  * Is object P of type TYPE visible to the player?
189  * TODO: Fold this into interators.
190  */
191 static int
192 xdvisible(int type, void *p)
193 {
194     struct genitem *gp = p;
195     struct trtstr *tp = p;
196     struct lonstr *lp = p;
197     struct natstr *natp;
198     int tlev;
199
200     switch (type) {
201     case EF_SECTOR:
202         return gp->own == player->cnum || player->god;
203     case EF_SHIP:
204     case EF_PLANE:
205     case EF_LAND:
206     case EF_NUKE:
207     case EF_LOST:
208         return gp->own != 0 && (gp->own == player->cnum || player->god);
209     case EF_NATION:
210         return gp->own == player->cnum;
211     case EF_COUNTRY:
212         return ((struct natstr *)p)->nat_stat != STAT_UNUSED;
213     case EF_NEWS:
214         return ((struct nwsstr *)p)->nws_vrb != 0
215             && (!opt_HIDDEN || player->god); /* FIXME */
216     case EF_TREATY:
217         return tp->trt_status != TS_FREE
218             && (tp->trt_cna == player->cnum || tp->trt_cnb == player->cnum
219                 || player->god);
220     case EF_LOAN:
221         if (lp->l_status == LS_FREE)
222             return 0;
223         if (lp->l_status == LS_SIGNED)
224             return 1;
225         return lp->l_loner == player->cnum || lp->l_lonee == player->cnum
226             || player->god;
227     case EF_TRADE:
228     case EF_COMM:
229         return gp->own != 0;
230     case EF_SHIP_CHR:
231         tlev = ((struct mchrstr *)p)->m_tech;
232         goto tech;
233     case EF_PLANE_CHR:
234         tlev = ((struct plchrstr *)p)->pl_tech;
235         goto tech;
236     case EF_LAND_CHR:
237         tlev = ((struct lchrstr *)p)->l_tech;
238     tech:
239         natp = getnatp(player->cnum);
240         return player->god || tlev <= (int)(1.25 * natp->nat_level[NAT_TLEV]);
241     case EF_NUKE_CHR:
242         tlev = ((struct nchrstr *)p)->n_tech;
243         if (drnuke_const > MIN_DRNUKE_CONST) {
244             natp = getnatp(player->cnum);
245             if (tlev > (int)((int)(1.25 * natp->nat_level[NAT_RLEV])
246                              / drnuke_const))
247                 return player->god;
248         }
249         goto tech;
250     case EF_NEWS_CHR:
251         return ((struct rptstr *)p)->r_newspage != 0;
252     case EF_TABLE:
253         return ((struct empfile *)p)->cadef != NULL;
254     default:
255         return 1;
256     }
257 }
258
259 /*
260  * Dump items of type TYPE selected by ARG.
261  * Return RET_OK on success, RET_SYN on error.
262  */
263 static int
264 xditem(int type, char *arg)
265 {
266     int check_owner = !player->god && (ef_flags(type) & EFF_OWNER) != 0;
267     struct castr *ca;
268     struct nstr_item ni;
269     int n;
270     char buf[2048];             /* FIXME buffer size? */
271
272     ca = ef_cadef(type);
273     if (!ca)
274         return RET_SYN;
275
276     if (!snxtitem(&ni, type, arg))
277         return RET_SYN;
278
279     xdhdr(ef_nameof(type), 0);
280
281     n = 0;
282     while (nxtitem(&ni, buf)) {
283         if (!xdvisible(type, buf))
284             continue;
285         ++n;
286         xdflds(ca, buf);
287         pr("\n");
288     }
289
290     xdftr(n);
291
292     return RET_OK;
293 }
294
295 /*
296  * Dump meta-data for items of type TYPE.
297  * Return RET_OK.
298  */
299 static int
300 xdmeta(int type)
301 {
302     struct castr *ca = ef_cadef(type);
303     int i;
304     int n = 0;
305
306     if (!ca)
307         return RET_SYN;
308
309     xdhdr(ef_nameof(type), 1);
310
311     for (i = 0; ca[i].ca_name; i++) {
312         if (ca[i].ca_flags & NSC_DEITY && !player->god)
313             continue;
314         if (ca[i].ca_flags & NSC_EXTRA)
315             continue;
316         xdflds(mdchr_ca, &ca[i]);
317         pr("\n");
318         n++;
319     }
320
321     xdftr(n);
322
323     return RET_OK;
324 }
325
326 /*
327  * Dump configkeys[], return RET_OK.
328  * If META, dump meta-data rather than data.
329  */
330 static int
331 xdver(int meta)
332 {
333     static struct castr vers_ca = {
334         NSC_STRINGY, 0, sizeof(PACKAGE_STRING), 0, "version", EF_BAD
335     };
336     struct keymatch *kp;
337     char *sep;
338     int n;
339     struct castr ca;
340     struct valstr val;
341
342     xdhdr("version", meta);
343
344     if (meta) {
345         n = 0;
346         xdflds(mdchr_ca, &vers_ca);
347         pr("\n");
348         n++;
349         for (kp = configkeys; kp->km_key; ++kp) {
350             if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
351                 ca.ca_type = kp->km_type;
352                 ca.ca_flags = 0;
353                 ca.ca_len = 0;
354                 ca.ca_off = 0;
355                 ca.ca_name = kp->km_key;
356                 ca.ca_table = EF_BAD;
357                 xdflds(mdchr_ca, &ca);
358                 pr("\n");
359                 n++;
360             }
361         }
362         xdftr(n);
363         return RET_OK;
364     }
365
366     xdeval(&val, vers_ca.ca_type, version, vers_ca.ca_off, 0, vers_ca.ca_len);
367     sep = xdprval(&val, "");
368     for (kp = configkeys; kp->km_key; ++kp) {
369         if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
370             xdeval(&val, kp->km_type, kp->km_data, 0, 0, 0);
371             sep = xdprval(&val, sep);
372         }
373     }
374     pr("\n");
375
376     xdftr(1);
377
378     return RET_OK;
379 }
380
381 /* Experimental extended dump command */
382 int
383 xdump(void)
384 {
385     char *p;
386     char buf[1024];
387     int type;
388     int meta = 0;
389
390     p = getstarg(player->argp[1], "Table name, or meta? ", buf);
391     if (p && strcmp(p, "meta") == 0) {
392         meta = 1;
393         p = getstarg(player->argp[2], "Table name? ", buf);
394     }
395     if (!p)
396         return RET_SYN;
397
398     type = isdigit(p[0]) ? atoi(p) : ef_byname(p);
399     if (type >= 0 && type < EF_MAX) {
400         if (meta)
401             return xdmeta(type);
402         else
403             return xditem(type, player->argp[2]);
404     } else if (!strncmp(p, "ver", strlen(p))) {
405         return xdver(meta);
406     }
407
408     return RET_SYN;
409 }