]> git.pond.sub.org Git - empserver/blob - src/util/empdump.c
bb42f548e143697e3db188119c51667934b97dec
[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
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             continue;
136         if (!ef_open(i, EFF_MEM | private, -1))
137             exit(1);
138     }
139
140     /* import from IMPORT */
141     memset(dirty, 0, sizeof(dirty));
142     if (import) {
143         lineno = 1;
144         while ((type = xundump(impf, import, &lineno, EF_BAD)) >= 0)
145             dirty[type] = 1;
146         if (type == EF_BAD)
147             exit(1);
148     }
149
150     if (ef_verify() < 0)
151         exit(1);
152
153     /* export to stdout */
154     if (export) {
155         for (i = 0; i < EF_MAX; i++) {
156             if (!EF_IS_GAME_STATE(i))
157                 continue;
158             dump_table(i, human);
159         }
160         if (fclose(stdout) != 0) {
161             fprintf(stderr, "%s: error writing export (%s)\n",
162                     argv[0], strerror(errno));
163             exit(1);
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 (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, NATID_BAD, 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 }