]> git.pond.sub.org Git - empserver/blob - src/util/empdump.c
Fix empdump error messages to include program name
[empserver] / src / util / empdump.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  *  empdump.c: Export/import Empire game state
29  * 
30  *  Known contributors to this file:
31  *     Markus Armbruster, 2008
32  */
33
34 #include <config.h>
35
36 #include <errno.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include "file.h"
42 #include "optlist.h"
43 #include "prototypes.h"
44 #include "version.h"
45 #include "xdump.h"
46
47 static void exit_bad_arg(char *, ...) ATTRIBUTE((noreturn));
48 static void dump_table(int, int);
49 static void pln_fixup(void);
50 static void lnd_fixup(void);
51 static void nuk_fixup(void);
52
53 int
54 main(int argc, char *argv[])
55 {
56     char *config_file = NULL;
57     char *import = NULL;
58     int export = 0;
59     int private = 0;
60     int human = 1;
61     int opt, i, lineno, type;
62     FILE *impf = NULL;
63     int dirty[EF_MAX];
64
65     while ((opt = getopt(argc, argv, "e:i:mnxhv")) != EOF) {
66         switch (opt) {
67         case 'e':
68             config_file = optarg;
69             break;
70         case 'i':
71             import = optarg;
72             break;
73         case 'm':
74             human = 0;
75             break;
76         case 'n':
77             private = EFF_PRIVATE;
78             break;
79         case 'x':
80             export = 1;
81             break;
82         case 'h':
83             printf("Usage: %s [OPTION]...\n"
84                    "  -e CONFIG-FILE  configuration file\n"
85                    "                  (default %s)\n"
86                    "  -i DUMP-FILE    import from DUMP-FILE\n"
87                    "  -m              use machine-readable format\n"
88                    "  -n              dry run, don't update game state\n"
89                    "  -x              export to standard output\n"
90                    "  -h              display this help and exit\n"
91                    "  -v              display version information and exit\n",
92                    argv[0], dflt_econfig);
93             exit(0);
94         case 'v':
95             printf("%s\n\n%s", version, legal);
96             exit(0);
97         default:
98             exit_bad_arg(NULL);
99         }
100     }
101
102     if (argv[optind])
103         exit_bad_arg("%s: extra operand %s\n", argv[0], argv[optind]);
104
105     if (!import && !export)
106         exit_bad_arg("%s: nothing to do!\n", argv[0]);
107
108     if (import) {
109         impf = fopen(import, "r");
110         if (!impf) {
111             fprintf(stderr, "%s: Can't open %s for reading (%s)\n",
112                     argv[0], import, strerror(errno));
113             exit(1);
114         }
115     } else
116         private = EFF_PRIVATE;
117
118     /* read configuration & initialize */
119     empfile_init();
120     if (emp_config(config_file) < 0)
121         exit(1);
122     empfile_fixup();
123     nsc_init();
124     if (read_builtin_tables() < 0)
125         exit(1);
126     if (read_custom_tables() < 0)
127         exit(1);
128     if (chdir(gamedir)) {
129         fprintf(stderr, "%s: Can't chdir to %s (%s)\n",
130                 argv[0], gamedir, strerror(errno));
131         exit(1);
132     }
133     global_init();
134
135     for (i = 0; i < EF_MAX; i++) {
136         if (!EF_IS_GAME_STATE(i))
137             continue;
138         if (!ef_open(i, EFF_MEM | private))
139             exit(1);
140     }
141
142     /* import from IMPORT */
143     memset(dirty, 0, sizeof(dirty));
144     if (import) {
145         lineno = 1;
146         while ((type = xundump(impf, import, &lineno, EF_BAD)) >= 0)
147             dirty[type] = 1;
148         if (type == EF_BAD)
149             exit(1);
150         pln_fixup();
151         lnd_fixup();
152         nuk_fixup();
153     }
154
155     if (ef_verify() < 0)
156         exit(1);
157
158     /* export to stdout */
159     if (export) {
160         for (i = 0; i < EF_MAX; i++) {
161             if (!EF_IS_GAME_STATE(i))
162                 continue;
163             dump_table(i, human);
164         }
165     }
166
167     /* write out imported data */
168     for (i = 0; i < EF_MAX; i++) {
169         if (!EF_IS_GAME_STATE(i))
170             continue;
171         if (!private && dirty[i]) {
172             if (!ef_close(i))
173                 exit(1);
174         }
175     }
176
177     return 0;
178 }
179
180 static void
181 exit_bad_arg(char *complaint, ...)
182 {
183     va_list ap;
184
185     if (complaint) {
186         va_start(ap, complaint);
187         vfprintf(stderr, complaint, ap);
188         va_end(ap);
189     }
190     fprintf(stderr, "Try -h for help.\n");
191     exit(1);
192 }
193
194 static void
195 printf_wrapper(char *fmt, ...)
196 {
197     va_list ap;
198
199     va_start(ap, fmt);
200     vprintf(fmt, ap);
201     va_end(ap);
202 }
203
204 static void
205 dump_table(int type, int human)
206 {
207     struct xdstr xd;
208     struct castr *ca;
209     int i;
210     void *p;
211
212     ca = ef_cadef(type);
213     if (!ca)
214         return;
215
216     xdinit(&xd, 0, human, printf_wrapper);
217     xdhdr(&xd, ef_nameof(type), 0);
218     xdcolhdr(&xd, ca);
219     for (i = 0; (p = ef_ptr(type, i)); i++) {
220         xdflds(&xd, ca, p);
221         printf("\n");
222     }
223     xdftr(&xd, i);
224 }
225
226
227 /* TODO remove need for this */
228
229 #include <math.h>
230 #include "ship.h"
231 #include "plane.h"
232 #include "land.h"
233 #include "nuke.h"
234
235 static int fit_plane_on_ship(struct plnstr *, struct shpstr *);
236 static int fit_plane_on_land(struct plnstr *, struct lndstr *);
237
238 static void
239 pln_fixup(void)
240 {
241     int i;
242     struct plnstr *pp;
243     struct shpstr *csp;
244     struct lndstr *clp;
245
246     for (i = 0; (pp = ef_ptr(EF_PLANE, i)); i++) {
247         if (!pp->pln_own)
248             continue;
249         csp = ef_ptr(EF_SHIP, pp->pln_ship);
250         clp = ef_ptr(EF_LAND, pp->pln_land);
251         if (csp)
252             fit_plane_on_ship(pp, csp);
253         else if (clp)
254             fit_plane_on_land(pp, clp);
255     }
256 }
257
258 static void
259 lnd_fixup(void)
260 {
261     int i;
262     struct lndstr *lp;
263     struct shpstr *csp;
264     struct lndstr *clp;
265
266     for (i = 0; (lp = ef_ptr(EF_LAND, i)); i++) {
267         if (!lp->lnd_own)
268             continue;
269         csp = ef_ptr(EF_SHIP, lp->lnd_ship);
270         clp = ef_ptr(EF_LAND, lp->lnd_land);
271         if (csp)
272             csp->shp_nland++;
273         else if (clp)
274             clp->lnd_nland++;
275     }
276 }
277
278 static void
279 nuk_fixup(void)
280 {
281     int i;
282     struct nukstr *np;
283     struct plnstr *cpp;
284
285     for (i = 0; (np = ef_ptr(EF_NUKE, i)); i++) {
286         if (!np->nuk_own)
287             continue;
288         cpp = ef_ptr(EF_PLANE, np->nuk_plane);
289         if (cpp)
290             cpp->pln_nuketype = np->nuk_type;
291     }
292 }
293
294 /* Temporarily copied from src/lib/subs/???sub.c */
295
296 /*
297  * Fit a plane of PP's type on ship SP.
298  * Adjust SP's plane counters.
299  * Updating the plane accordingly is the caller's job.
300  * Return whether it fits.
301  */
302 static int
303 fit_plane_on_ship(struct plnstr *pp, struct shpstr *sp)
304 {
305     struct plchrstr *pcp = plchr + pp->pln_type;
306     struct mchrstr *mcp = mchr + sp->shp_type;
307     int wanted;
308
309     if (pcp->pl_flags & P_K) {
310         /* chopper, try chopper slot first */
311         if (sp->shp_nchoppers < mcp->m_nchoppers)
312             return ++sp->shp_nchoppers;
313         /* else try plane slot */
314         wanted = M_FLY;
315     } else if (pcp->pl_flags & P_E) {
316         /* x-light, try x-light slot first */
317         if (sp->shp_nxlight < mcp->m_nxlight)
318             return ++sp->shp_nxlight;
319         /* else try plane slot */
320         wanted = M_MSL | M_FLY;
321     } else if (!(pcp->pl_flags & P_L)) {
322         /* not light, no go */
323         wanted = 0;
324     } else if (pcp->pl_flags & P_M) {
325         /* missile, use plane slot */
326         wanted = M_MSL | M_FLY;
327     } else {
328         /* fixed-wing plane, use plane slot */
329         wanted = M_FLY;
330     }
331
332     if ((mcp->m_flags & wanted) == 0)
333         return 0;               /* ship not capable */
334
335     if (sp->shp_nplane < mcp->m_nplanes)
336         return ++sp->shp_nplane;
337
338     return 0;
339 }
340
341 /*
342  * Fit a plane of PP's type on land unit LP.
343  * Adjust LP's plane counters.
344  * Updating the plane accordingly is the caller's job.
345  * Return whether it fits.
346  */
347 static int
348 fit_plane_on_land(struct plnstr *pp, struct lndstr *lp)
349 {
350     struct plchrstr *pcp = plchr + pp->pln_type;
351     struct lchrstr *lcp = lchr + lp->lnd_type;
352
353     if ((pcp->pl_flags & P_E) && lp->lnd_nxlight < lcp->l_nxlight)
354         return ++lp->lnd_nxlight;
355
356     return 0;
357 }