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