]> git.pond.sub.org Git - empserver/blob - src/lib/commands/xdump.c
Virtual selectors
[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-2007
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 /*
89  * Evaluate a attribute of an object into VAL, return VAL.
90  * CA describes the attribute.
91  * PTR points to the context object.
92  * IDX is the index within the attribute.
93  */
94 static struct valstr *
95 xdeval(struct valstr *val, struct castr *ca, void *ptr, int idx)
96 {
97     val->val_type = ca->ca_type;
98     val->val_cat = NSC_OFF;
99     val->val_as.sym.off = ca->ca_off;
100     val->val_as.sym.len = ca->ca_len;
101     val->val_as.sym.idx = idx;
102     val->val_as.sym.get = ca->ca_get;
103     return nstr_exec_val(val, player->cnum, ptr, NSC_NOTYPE);
104 }
105
106 /*
107  * Dump VAL prefixed with SEP, return " ".
108  * VAL must be evaluated.
109  */
110 static char *
111 xdprval(struct valstr *val, char *sep)
112 {
113     unsigned char *s, *e, *l;
114
115     if (CANT_HAPPEN(val->val_cat != NSC_VAL)) {
116         pr("%snil", sep);
117         return " ";
118     }
119
120     switch (val->val_type) {
121     case NSC_LONG:
122         pr("%s%ld", sep, val->val_as.lng);
123         break;
124     case NSC_DOUBLE:
125         pr("%s%#g", sep, val->val_as.dbl);
126         break;
127     case NSC_STRING:
128         s = (unsigned char *)val->val_as.str.base;
129         if (s) {
130             pr("%s\"", sep);
131             l = s + val->val_as.str.maxsz;
132             /* FIXME maxsz == INT_MAX ! */
133             for (;;) {
134                 for (e = s;
135                      e < l && *e != '"' && *e != '\\' && isgraph(*e);
136                      ++e)
137                     ;
138                 pr("%.*s", (int)(e-s), s);
139                 if (e < l && *e)
140                     pr("\\%03o", *e++);
141                 else
142                     break;
143                 s = e;
144             }
145             pr("\"");
146         } else
147             pr("%snil", sep);
148         break;
149     default:
150         CANT_REACH();
151         pr("%snil", sep);
152     }
153     return " ";
154 }
155
156 /*
157  * Dump field values of a context object.
158  * CA[] describes fields.
159  * PTR points to context object.
160  */
161 static void
162 xdflds(struct castr ca[], void *ptr)
163 {
164     int i, j, n;
165     struct valstr val;
166     char *sep = "";
167
168     for (i = 0; ca[i].ca_name; ++i) {
169         if (ca[i].ca_flags & NSC_DEITY && !player->god)
170             continue;
171         if (ca[i].ca_flags & NSC_EXTRA)
172             continue;
173         n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
174         j = 0;
175         do {
176             xdeval(&val, &ca[i], ptr, j);
177             sep = xdprval(&val, sep);
178         } while (++j < n);
179     }
180 }
181
182 /*
183  * Dump header for dump NAME.
184  * If META, it's for the meta-data dump rather than the data dump.
185  */
186 static void
187 xdhdr(char *name, int meta)
188 {
189     pr("XDUMP %s%s %ld\n", meta ? "meta " : "", name, (long)time(NULL));
190 }
191
192 /* Dump footer for a dump that dumped N objects.  */
193 static void
194 xdftr(int n)
195 {
196     pr("/%d\n", n);
197 }
198
199 /*
200  * Is object P of type TYPE visible to the player?
201  * TODO: Fold this into interators.
202  */
203 static int
204 xdvisible(int type, void *p)
205 {
206     struct empobj *gp = p;
207     struct trtstr *tp = p;
208     struct lonstr *lp = p;
209     struct natstr *natp;
210     int tlev;
211
212     switch (type) {
213     case EF_SECTOR:
214     case EF_REALM:
215         return gp->own == player->cnum || player->god;
216     case EF_SHIP:
217     case EF_PLANE:
218     case EF_LAND:
219     case EF_NUKE:
220     case EF_LOST:
221         return gp->own != 0 && (gp->own == player->cnum || player->god);
222     case EF_NATION:
223         return ((struct natstr *)p)->nat_stat != STAT_UNUSED;
224     case EF_COUNTRY:
225         return gp->own == player->cnum;
226     case EF_NEWS:
227         return ((struct nwsstr *)p)->nws_vrb != 0
228             && (!opt_HIDDEN || player->god); /* FIXME */
229     case EF_TREATY:
230         return tp->trt_status != TS_FREE
231             && (tp->trt_cna == player->cnum || tp->trt_cnb == player->cnum
232                 || player->god);
233     case EF_LOAN:
234         if (lp->l_status == LS_FREE)
235             return 0;
236         if (lp->l_status == LS_SIGNED)
237             return 1;
238         return lp->l_loner == player->cnum || lp->l_lonee == player->cnum
239             || player->god;
240     case EF_TRADE:
241     case EF_COMM:
242         return gp->own != 0;
243     case EF_SHIP_CHR:
244         tlev = ((struct mchrstr *)p)->m_tech;
245         goto tech;
246     case EF_PLANE_CHR:
247         tlev = ((struct plchrstr *)p)->pl_tech;
248         goto tech;
249     case EF_LAND_CHR:
250         tlev = ((struct lchrstr *)p)->l_tech;
251     tech:
252         natp = getnatp(player->cnum);
253         return player->god || tlev <= (int)(1.25 * natp->nat_level[NAT_TLEV]);
254     case EF_NUKE_CHR:
255         tlev = ((struct nchrstr *)p)->n_tech;
256         if (drnuke_const > MIN_DRNUKE_CONST) {
257             natp = getnatp(player->cnum);
258             if (tlev > (int)((int)(1.25 * natp->nat_level[NAT_RLEV])
259                              / drnuke_const))
260                 return player->god;
261         }
262         goto tech;
263     case EF_NEWS_CHR:
264         return ((struct rptstr *)p)->r_newspage != 0;
265     case EF_TABLE:
266         return ((struct empfile *)p)->cadef != NULL;
267     default:
268         return 1;
269     }
270 }
271
272 /*
273  * Dump items of type TYPE selected by ARG.
274  * Return RET_OK on success, RET_SYN on error.
275  */
276 static int
277 xditem(int type, char *arg)
278 {
279     struct castr *ca;
280     struct nstr_item ni;
281     int n;
282     char buf[2048];             /* FIXME buffer size? */
283
284     ca = ef_cadef(type);
285     if (!ca)
286         return RET_SYN;
287
288     if (!snxtitem(&ni, type, arg))
289         return RET_SYN;
290
291     xdhdr(ef_nameof(type), 0);
292
293     n = 0;
294     while (nxtitem(&ni, buf)) {
295         if (!xdvisible(type, buf))
296             continue;
297         ++n;
298         xdflds(ca, buf);
299         pr("\n");
300     }
301
302     xdftr(n);
303
304     return RET_OK;
305 }
306
307 /*
308  * Dump meta-data for items of type TYPE.
309  * Return RET_OK.
310  */
311 static int
312 xdmeta(int type)
313 {
314     struct castr *ca = ef_cadef(type);
315     int i;
316     int n = 0;
317
318     if (!ca)
319         return RET_SYN;
320
321     xdhdr(ef_nameof(type), 1);
322
323     for (i = 0; ca[i].ca_name; i++) {
324         if (ca[i].ca_flags & NSC_DEITY && !player->god)
325             continue;
326         if (ca[i].ca_flags & NSC_EXTRA)
327             continue;
328         xdflds(mdchr_ca, &ca[i]);
329         pr("\n");
330         n++;
331     }
332
333     xdftr(n);
334
335     return RET_OK;
336 }
337
338 /* Extended dump command */
339 int
340 xdump(void)
341 {
342     char *p;
343     char buf[1024];
344     int type;
345     int meta = 0;
346     struct natstr *natp;
347
348     p = getstarg(player->argp[1], "Table name, or meta? ", buf);
349     if (p && strcmp(p, "meta") == 0) {
350         meta = 1;
351         p = getstarg(player->argp[2], "Table name? ", buf);
352     }
353     if (!p || !*p)
354         return RET_SYN;
355
356     natp = getnatp(player->cnum);
357     type = isdigit(p[0]) ? atoi(p) : ef_byname(p);
358     if (type < 0 || type >= EF_MAX)
359         return RET_SYN;
360
361     if (meta)
362         return xdmeta(type);
363     if ((EF_IS_GAME_STATE(type) || EF_IS_VIEW(type))
364         && !(natp->nat_stat == STAT_ACTIVE || player->god)) {
365         pr("Access to table %s denied\n", ef_nameof(type));
366         return RET_FAIL;
367     }
368     if (type == EF_VERSION && !player->argp[2])
369         return xditem(type, "*"); /* backward compatibility */
370     return xditem(type, player->argp[2]);
371 }