2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2020, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
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.
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.
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/>.
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.
27 * empdump.c: Export/import Empire game state
29 * Known contributors to this file:
30 * Markus Armbruster, 2008-2014
42 #include "prototypes.h"
46 static void exit_bad_arg(char *, ...)
47 ATTRIBUTE((noreturn, format (printf, 1, 2)));
48 static void dump_table(int, int, int, int);
51 main(int argc, char *argv[])
53 char *config_file = NULL;
59 int opt, i, lineno, type, verified;
63 while ((opt = getopt(argc, argv, "ce:i:mnxhv")) != EOF) {
78 private = EFF_PRIVATE;
84 printf("Usage: %s [OPTION]...\n"
85 " -c use complete export format\n"
86 " -e CONFIG-FILE configuration file\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);
97 printf("%s\n\n%s", version, legal);
105 exit_bad_arg("%s: extra operand %s\n", argv[0], argv[optind]);
107 if (!import && !export)
108 exit_bad_arg("%s: nothing to do!\n", argv[0]);
111 impf = fopen(import, "r");
113 fprintf(stderr, "%s: Can't open %s for reading (%s)\n",
114 argv[0], import, strerror(errno));
118 private = EFF_PRIVATE;
120 /* read configuration & initialize */
122 if (emp_config(config_file) < 0)
126 if (read_builtin_tables() < 0)
128 if (read_custom_tables() < 0)
130 if (chdir(gamedir)) {
131 fprintf(stderr, "%s: Can't chdir to %s (%s)\n",
132 argv[0], gamedir, strerror(errno));
135 if (ef_verify_config() < 0)
139 for (i = 0; i < EF_MAX; i++) {
140 if (EF_IS_GAME_STATE(i)) {
141 if (!ef_open(i, EFF_MEM | private))
143 } else if (EF_IS_VIEW(i)) {
144 if (ef_open_view(i) < 0)
149 /* import from IMPORT */
150 memset(dirty, 0, sizeof(dirty));
153 while ((type = xundump(impf, import, &lineno, EF_BAD)) >= 0)
159 verified = ef_verify_state(0) >= 0;
161 /* export to stdout */
163 for (i = 0; i < EF_MAX; i++) {
164 if (!EF_IS_GAME_STATE(i))
166 dump_table(i, human, !verified, complete);
168 if (fclose(stdout) != 0) {
169 fprintf(stderr, "%s: error writing export (%s)\n",
170 argv[0], strerror(errno));
175 if (!verified && export)
177 "%s: warning: export has errors, not importable as is\n",
179 if (!verified && import)
182 /* write out imported data */
183 for (i = 0; i < EF_MAX; i++) {
184 if (!EF_IS_GAME_STATE(i))
196 exit_bad_arg(char *complaint, ...)
201 va_start(ap, complaint);
202 vfprintf(stderr, complaint, ap);
205 fprintf(stderr, "Try -h for help.\n");
210 printf_wrapper(char *fmt, ...)
220 dump_table(int type, int human, int sloppy, int complete)
231 xdinit(&xd, NATID_BAD, human, sloppy, printf_wrapper);
232 xdhdr(&xd, ef_nameof(type), 0);
235 for (i = 0; (p = ef_ptr(type, i)); i++) {
236 if (!complete && xundump_redundant(type, i, p))