]> git.pond.sub.org Git - empserver/blob - src/util/empdump.c
Update copyright notice
[empserver] / src / util / empdump.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  empdump.c: Export/import Empire game state
28  *
29  *  Known contributors to this file:
30  *     Markus Armbruster, 2008-2014
31  */
32
33 #include <config.h>
34
35 #include <errno.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include "optlist.h"
41 #include "nat.h"
42 #include "prototypes.h"
43 #include "version.h"
44 #include "xdump.h"
45
46 static void exit_bad_arg(char *, ...)
47     ATTRIBUTE((noreturn, format (printf, 1, 2)));
48 static void dump_table(int, int, int, int);
49
50 int
51 main(int argc, char *argv[])
52 {
53     char *config_file = NULL;
54     char *import = NULL;
55     int complete = 0;
56     int export = 0;
57     int private = 0;
58     int human = 1;
59     int opt, i, lineno, type, verified;
60     FILE *impf = NULL;
61     int dirty[EF_MAX];
62
63     while ((opt = getopt(argc, argv, "ce:i:mnxhv")) != EOF) {
64         switch (opt) {
65         case 'c':
66             complete = 1;
67             break;
68         case 'e':
69             config_file = optarg;
70             break;
71         case 'i':
72             import = optarg;
73             break;
74         case 'm':
75             human = 0;
76             break;
77         case 'n':
78             private = EFF_PRIVATE;
79             break;
80         case 'x':
81             export = 1;
82             break;
83         case 'h':
84             printf("Usage: %s [OPTION]...\n"
85                    "  -c              use complete export format\n"
86                    "  -e CONFIG-FILE  configuration file\n"
87                    "                  (default %s)\n"
88                    "  -i DUMP-FILE    import from DUMP-FILE\n"
89                    "  -m              use machine-readable format\n"
90                    "  -n              dry run, don't update game state\n"
91                    "  -x              export to standard output\n"
92                    "  -h              display this help and exit\n"
93                    "  -v              display version information and exit\n",
94                    argv[0], dflt_econfig);
95             exit(0);
96         case 'v':
97             printf("%s\n\n%s", version, legal);
98             exit(0);
99         default:
100             exit_bad_arg(NULL);
101         }
102     }
103
104     if (argv[optind])
105         exit_bad_arg("%s: extra operand %s\n", argv[0], argv[optind]);
106
107     if (!import && !export)
108         exit_bad_arg("%s: nothing to do!\n", argv[0]);
109
110     if (import) {
111         impf = fopen(import, "r");
112         if (!impf) {
113             fprintf(stderr, "%s: Can't open %s for reading (%s)\n",
114                     argv[0], import, strerror(errno));
115             exit(1);
116         }
117     } else
118         private = EFF_PRIVATE;
119
120     /* read configuration & initialize */
121     empfile_init();
122     if (emp_config(config_file) < 0)
123         exit(1);
124     empfile_fixup();
125     nsc_init();
126     if (read_builtin_tables() < 0)
127         exit(1);
128     if (read_custom_tables() < 0)
129         exit(1);
130     if (chdir(gamedir)) {
131         fprintf(stderr, "%s: Can't chdir to %s (%s)\n",
132                 argv[0], gamedir, strerror(errno));
133         exit(1);
134     }
135     if (ef_verify_config() < 0)
136         exit(1);
137     global_init();
138
139     for (i = 0; i < EF_MAX; i++) {
140         if (EF_IS_GAME_STATE(i)) {
141             if (!ef_open(i, EFF_MEM | private))
142                 exit(1);
143         } else if (EF_IS_VIEW(i)) {
144             if (ef_open_view(i) < 0)
145                 exit(1);
146         }
147     }
148
149     /* import from IMPORT */
150     memset(dirty, 0, sizeof(dirty));
151     if (import) {
152         lineno = 1;
153         while ((type = xundump(impf, import, &lineno, EF_BAD)) >= 0)
154             dirty[type] = 1;
155         if (type == EF_BAD)
156             exit(1);
157     }
158
159     verified = ef_verify_state(0) >= 0;
160
161     /* export to stdout */
162     if (export) {
163         for (i = 0; i < EF_MAX; i++) {
164             if (!EF_IS_GAME_STATE(i))
165                 continue;
166             dump_table(i, human, !verified, complete);
167         }
168         if (fclose(stdout) != 0) {
169             fprintf(stderr, "%s: error writing export (%s)\n",
170                     argv[0], strerror(errno));
171             exit(1);
172         }
173     }
174
175     if (!verified && export)
176         fprintf(stderr,
177                 "%s: warning: export has errors, not importable as is\n",
178                 argv[0]);
179     if (!verified && import)
180         exit(1);
181
182     /* write out imported data */
183     for (i = 0; i < EF_MAX; i++) {
184         if (!EF_IS_GAME_STATE(i))
185             continue;
186         if (dirty[i]) {
187             if (!ef_close(i))
188                 exit(1);
189         }
190     }
191
192     return 0;
193 }
194
195 static void
196 exit_bad_arg(char *complaint, ...)
197 {
198     va_list ap;
199
200     if (complaint) {
201         va_start(ap, complaint);
202         vfprintf(stderr, complaint, ap);
203         va_end(ap);
204     }
205     fprintf(stderr, "Try -h for help.\n");
206     exit(1);
207 }
208
209 static void
210 printf_wrapper(char *fmt, ...)
211 {
212     va_list ap;
213
214     va_start(ap, fmt);
215     vprintf(fmt, ap);
216     va_end(ap);
217 }
218
219 static void
220 dump_table(int type, int human, int sloppy, int complete)
221 {
222     struct xdstr xd;
223     struct castr *ca;
224     int i, n;
225     void *p;
226
227     ca = ef_cadef(type);
228     if (!ca)
229         return;
230
231     xdinit(&xd, NATID_BAD, human, sloppy, printf_wrapper);
232     xdhdr(&xd, ef_nameof(type), 0);
233     xdcolhdr(&xd, ca);
234     n = 0;
235     for (i = 0; (p = ef_ptr(type, i)); i++) {
236         if (!complete && xundump_redundant(type, i, p))
237             continue;
238         xdflds(&xd, ca, p);
239         n++;
240         printf("\n");
241     }
242     xdftr(&xd, n);
243 }