]> 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-2016, 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 "file.h"
41 #include "optlist.h"
42 #include "nat.h"
43 #include "prototypes.h"
44 #include "version.h"
45 #include "xdump.h"
46
47 static void exit_bad_arg(char *, ...)
48     ATTRIBUTE((noreturn, format (printf, 1, 2)));
49 static void dump_table(int, int, int, int);
50
51 int
52 main(int argc, char *argv[])
53 {
54     char *config_file = NULL;
55     char *import = NULL;
56     int complete = 0;
57     int export = 0;
58     int private = 0;
59     int human = 1;
60     int opt, i, lineno, type, verified;
61     FILE *impf = NULL;
62     int dirty[EF_MAX];
63
64     while ((opt = getopt(argc, argv, "ce:i:mnxhv")) != EOF) {
65         switch (opt) {
66         case 'c':
67             complete = 1;
68             break;
69         case 'e':
70             config_file = optarg;
71             break;
72         case 'i':
73             import = optarg;
74             break;
75         case 'm':
76             human = 0;
77             break;
78         case 'n':
79             private = EFF_PRIVATE;
80             break;
81         case 'x':
82             export = 1;
83             break;
84         case 'h':
85             printf("Usage: %s [OPTION]...\n"
86                    "  -c              use complete export format\n"
87                    "  -e CONFIG-FILE  configuration file\n"
88                    "                  (default %s)\n"
89                    "  -i DUMP-FILE    import from DUMP-FILE\n"
90                    "  -m              use machine-readable format\n"
91                    "  -n              dry run, don't update game state\n"
92                    "  -x              export to standard output\n"
93                    "  -h              display this help and exit\n"
94                    "  -v              display version information and exit\n",
95                    argv[0], dflt_econfig);
96             exit(0);
97         case 'v':
98             printf("%s\n\n%s", version, legal);
99             exit(0);
100         default:
101             exit_bad_arg(NULL);
102         }
103     }
104
105     if (argv[optind])
106         exit_bad_arg("%s: extra operand %s\n", argv[0], argv[optind]);
107
108     if (!import && !export)
109         exit_bad_arg("%s: nothing to do!\n", argv[0]);
110
111     if (import) {
112         impf = fopen(import, "r");
113         if (!impf) {
114             fprintf(stderr, "%s: Can't open %s for reading (%s)\n",
115                     argv[0], import, strerror(errno));
116             exit(1);
117         }
118     } else
119         private = EFF_PRIVATE;
120
121     /* read configuration & initialize */
122     empfile_init();
123     if (emp_config(config_file) < 0)
124         exit(1);
125     empfile_fixup();
126     nsc_init();
127     if (read_builtin_tables() < 0)
128         exit(1);
129     if (read_custom_tables() < 0)
130         exit(1);
131     if (chdir(gamedir)) {
132         fprintf(stderr, "%s: Can't chdir to %s (%s)\n",
133                 argv[0], gamedir, strerror(errno));
134         exit(1);
135     }
136     if (ef_verify_config() < 0)
137         exit(1);
138     global_init();
139
140     for (i = 0; i < EF_MAX; i++) {
141         if (EF_IS_GAME_STATE(i)) {
142             if (!ef_open(i, EFF_MEM | private))
143                 exit(1);
144         } else if (EF_IS_VIEW(i)) {
145             if (ef_open_view(i) < 0)
146                 exit(1);
147         }
148     }
149
150     /* import from IMPORT */
151     memset(dirty, 0, sizeof(dirty));
152     if (import) {
153         lineno = 1;
154         while ((type = xundump(impf, import, &lineno, EF_BAD)) >= 0)
155             dirty[type] = 1;
156         if (type == EF_BAD)
157             exit(1);
158     }
159
160     verified = ef_verify_state(0) >= 0;
161
162     /* export to stdout */
163     if (export) {
164         for (i = 0; i < EF_MAX; i++) {
165             if (!EF_IS_GAME_STATE(i))
166                 continue;
167             dump_table(i, human, !verified, complete);
168         }
169         if (fclose(stdout) != 0) {
170             fprintf(stderr, "%s: error writing export (%s)\n",
171                     argv[0], strerror(errno));
172             exit(1);
173         }
174     }
175
176     if (!verified && export)
177         fprintf(stderr,
178                 "%s: warning: export has errors, not importable as is\n",
179                 argv[0]);
180     if (!verified && import)
181         exit(1);
182
183     /* write out imported data */
184     for (i = 0; i < EF_MAX; i++) {
185         if (!EF_IS_GAME_STATE(i))
186             continue;
187         if (dirty[i]) {
188             if (!ef_close(i))
189                 exit(1);
190         }
191     }
192
193     return 0;
194 }
195
196 static void
197 exit_bad_arg(char *complaint, ...)
198 {
199     va_list ap;
200
201     if (complaint) {
202         va_start(ap, complaint);
203         vfprintf(stderr, complaint, ap);
204         va_end(ap);
205     }
206     fprintf(stderr, "Try -h for help.\n");
207     exit(1);
208 }
209
210 static void
211 printf_wrapper(char *fmt, ...)
212 {
213     va_list ap;
214
215     va_start(ap, fmt);
216     vprintf(fmt, ap);
217     va_end(ap);
218 }
219
220 static void
221 dump_table(int type, int human, int sloppy, int complete)
222 {
223     struct xdstr xd;
224     struct castr *ca;
225     int i, n;
226     void *p;
227
228     ca = ef_cadef(type);
229     if (!ca)
230         return;
231
232     xdinit(&xd, NATID_BAD, human, sloppy, printf_wrapper);
233     xdhdr(&xd, ef_nameof(type), 0);
234     xdcolhdr(&xd, ca);
235     n = 0;
236     for (i = 0; (p = ef_ptr(type, i)); i++) {
237         if (!complete && xundump_redundant(type, i, p))
238             continue;
239         xdflds(&xd, ca, p);
240         n++;
241         printf("\n");
242     }
243     xdftr(&xd, n);
244 }