2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program 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 2 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * xundump.c: Load back xdump output
30 * Known contributors to this file:
31 * Ron Koenderink, 2005
32 * Markus Armbruster, 2005
35 /* FIXME normalize terminology: table/rows/columns or file/records/fields */
51 #include "prototypes.h"
56 static int ellipsis, need_uid;
57 static int cur_type, cur_id;
60 static struct castr **fldca;
64 static int gripe(char *, ...) ATTRIBUTE((format (printf, 1, 2)));
65 static int deffld(int, char *, int);
66 static int defellipsis(int fldno);
67 static int chkflds(void);
68 static int setnum(int, double);
69 static int setstr(int, char *);
70 static int xunsymbol1(char *, struct symbol *, struct castr *, int);
71 static int setsym(int, char *);
72 static int mtsymset(int, long *);
73 static int add2symset(int, long *, char *);
74 static struct symbol *get_symtab(struct castr *);
75 static int xundump1(FILE *, int, struct castr *);
76 static int xundump2(FILE *, int, struct castr *);
83 fprintf(stderr, "%s:%d: ", fname, lineno);
85 vfprintf(stderr, fmt, ap);
99 } while (ch == ' ' || ch == '\t');
104 } while (ch != EOF && ch != '\n');
111 getid(FILE *fp, char *buf)
114 if (fscanf(fp, "%1023[^#() \t\n]%n", buf, &n) != 1 || !isalpha(buf[0]))
129 if (sscanf(++src, "%3o%n", &octal_chr, &n) != 1 || n != 3)
131 *dst++ = (char)octal_chr;
141 xufldname(FILE *fp, int i)
149 return gripe("Unexpected EOF");
156 if (getc(fp) != '.' || getc(fp) != '.')
157 return gripe("Junk in header field %d", i + 1);
159 return gripe("... not allowed in field 1");
160 if (defellipsis(i) < 0)
163 if (ch != EOF && ch != '\n')
164 return gripe("Junk after ...");
169 if (getid(fp, buf) < 0)
170 return gripe("Junk in header field %d", i + 1);
174 return deffld(i, buf, -1);
178 if (isdigit(ch) || ch == '-' || ch == '+') {
179 if (fscanf(fp, "%d", &idx) != 1)
180 return gripe("Malformed number in index of header field %d",
183 return gripe("Index must not be negative in header field %d",
186 if (getid(fp, buf) < 0)
187 return gripe("Malformed index in header field %d", i + 1);
188 return gripe("Symbolic index in header field %d not yet implemented",
193 return gripe("Malformed index in header field %d", i + 1);
194 return deffld(i, buf, idx);
199 xufld(FILE *fp, int i)
209 return gripe("Unexpected EOF");
212 return gripe("Field %s missing", fldca[i]->ca_name);
215 case '+': case '-': case '.':
216 case '0': case '1': case '2': case '3': case '4':
217 case '5': case '6': case '7': case '8': case '9':
219 if (fscanf(fp, "%lg", &dbl) != 1)
220 return gripe("Malformed number in field %d", i + 1);
221 return setnum(i, dbl);
228 if (fscanf(fp, "%1023[^\"\n]", buf) != 1 || getc(fp) != '"')
229 return gripe("Malformed string in field %d", i + 1);
231 return gripe("Invalid escape sequence in field %d",
234 return setstr(i, buf);
236 if (mtsymset(i, &set) < 0)
240 if (ch == EOF || ch == '\n')
241 return gripe("Unmatched '(' in field %d", i + 1);
245 if (getid(fp, buf) < 0)
246 return gripe("Junk in field %d", i + 1);
247 if (add2symset(i, &set, buf) < 0)
250 return setnum(i, set);
253 if (getid(fp, buf) < 0)
254 return gripe("Junk in field %d", i + 1);
255 if (!strcmp(buf, "nil"))
256 return setstr(i, NULL);
258 return setsym(i, buf);
263 xuflds(FILE *fp, int (*parse)(FILE *, int))
276 else if (ch != ' ' && ch != '\t')
277 return gripe("Bad field separator after field %d", i + 1);
282 deffld(int fldno, char *name, int idx)
284 struct castr *ca = ef_cadef(cur_type);
287 res = stmtch(name, ca, offsetof(struct castr, ca_name),
288 sizeof(struct castr));
290 return gripe("Header %s of field %d is %s", name, fldno + 1,
291 res == M_NOTUNIQUE ? "ambiguous" : "unknown");
292 if (ca[res].ca_type != NSC_STRINGY && ca[res].ca_len != 0) {
294 return gripe("Header %s requires an index in field %d",
295 ca[res].ca_name, fldno + 1);
296 if (idx >= ca[res].ca_len)
297 return gripe("Header %s(%d) index out of bounds in field %d",
298 ca[res].ca_name, idx, fldno + 1);
299 if (idx < caflds[res])
300 return gripe("Duplicate header %s(%d) in field %d",
301 ca[res].ca_name, idx, fldno + 1);
302 if (idx > caflds[res])
303 return gripe("Expected header %s(%d) in field %d",
304 ca[res].ca_name, caflds[res], fldno + 1);
307 return gripe("Header %s doesn't take an index in field %d",
308 ca[res].ca_name, fldno + 1);
311 return gripe("Duplicate header %s in field %d",
312 ca[res].ca_name, fldno + 1);
314 fldca[fldno] = &ca[res];
321 defellipsis(int fldno)
323 struct castr *ca = ef_cadef(cur_type);
325 if (ca[0].ca_table != cur_type)
326 return gripe("Table %s doesn't support ...", ef_nameof(cur_type));
335 struct castr *ca = ef_cadef(cur_type);
340 return gripe("Header field %s required with ...", ca[0].ca_name);
344 for (i = 0; ca[i].ca_name; i++) {
345 if (ca[i].ca_flags & NSC_EXTRA)
347 len = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
348 if (!len && !caflds[i])
349 res = gripe("Header field %s missing", ca[i].ca_name);
350 else if (len && caflds[i] == len - 1)
351 res = gripe("Header field %s(%d) missing",
352 ca[i].ca_name, len - 1);
353 else if (len && caflds[i] < len - 1)
354 res = gripe("Header fields %s(%d) ... %s(%d) missing",
355 ca[i].ca_name, caflds[i], ca[i].ca_name, len - 1);
361 static struct castr *
362 getfld(int fldno, int *idx)
364 if (fldno >= nflds) {
365 gripe("Too many fields, expected only %d", nflds);
368 if (CANT_HAPPEN(fldno < 0))
371 *idx = fldidx[fldno];
376 getobj(struct castr *ca, int altid)
378 struct empfile *ep = &empfile[cur_type];
379 int need_sentinel = !EF_IS_GAME_STATE(cur_type);
382 if (ca->ca_table == cur_type)
384 if (cur_id >= ep->fids) {
385 /* TODO grow cache (and posssibly file) unless EFF_STATIC */
386 if (cur_id < ep->csize - !!need_sentinel)
387 ep->cids = ep->fids = cur_id + 1;
388 /* else: ef_ptr() will fail */
390 cur_obj = ef_ptr(cur_type, cur_id);
392 gripe("Can't put ID %d into table %s, it holds only 0..%d.",
393 cur_id, ep->name, ep->fids - 1);
400 setnum(int fldno, double dbl)
407 ca = getfld(fldno, &idx);
411 memb_ptr = getobj(ca, (int)dbl);
414 memb_ptr += ca->ca_off;
416 switch (ca->ca_type) {
419 old = ((signed char *)memb_ptr)[idx];
420 ((signed char *)memb_ptr)[idx] = (signed char)dbl;
423 old = ((unsigned char *)memb_ptr)[idx];
424 ((unsigned char *)memb_ptr)[idx] = (unsigned char)dbl;
427 old = ((short *)memb_ptr)[idx];
428 ((short *)memb_ptr)[idx] = (short)dbl;
431 old = ((unsigned short *)memb_ptr)[idx];
432 ((unsigned short *)memb_ptr)[idx] = (unsigned short)dbl;
435 old = ((int *)memb_ptr)[idx];
436 ((int *)memb_ptr)[idx] = (int)dbl;
439 old = ((long *)memb_ptr)[idx];
440 ((long *)memb_ptr)[idx] = (long)dbl;
443 old = ((coord *)memb_ptr)[idx];
444 /* FIXME use variant of xrel() that takes orig instead of nation */
445 if (old >= WORLD_X / 2)
447 ((coord *)memb_ptr)[idx] = XNORM((coord)dbl);
450 old = ((coord *)memb_ptr)[idx];
451 /* FIXME use variant of yrel() that takes orig instead of nation */
452 if (old >= WORLD_Y / 2)
454 ((coord *)memb_ptr)[idx] = YNORM((coord)dbl);
457 old = ((float *)memb_ptr)[idx];
458 ((float *)memb_ptr)[idx] = (float)dbl;
461 old = ((double *)memb_ptr)[idx];
462 ((double *)memb_ptr)[idx] = dbl;
465 old = ((time_t *)memb_ptr)[idx];
466 ((time_t *)memb_ptr)[idx] = (time_t)dbl;
469 return gripe("Field %d doesn't take numbers", fldno + 1);
472 if ((ca->ca_flags & NSC_CONST) && old != dbl)
473 return gripe("Value for field %d must be %g", fldno + 1, old);
479 setstr(int fldno, char *str)
484 char *memb_ptr, *old;
486 ca = getfld(fldno, &idx);
490 memb_ptr = getobj(ca, cur_id);
493 memb_ptr += ca->ca_off;
495 switch (ca->ca_type) {
497 old = ((char **)memb_ptr)[idx];
498 if (!(ca->ca_flags & NSC_CONST))
499 ((char **)memb_ptr)[idx] = str ? strdup(str) : NULL;
500 len = 65535; /* really SIZE_MAX, but it's C99 */
503 if (CANT_HAPPEN(idx))
506 return gripe("Field doesn't take nil");
508 if (strlen(str) > len)
509 return gripe("Field %d takes at most %d characters",
512 if (!(ca->ca_flags & NSC_CONST))
513 strncpy(memb_ptr, str, len);
516 return gripe("Field %d doesn't take strings", fldno + 1);
519 if (ca->ca_flags & NSC_CONST) {
520 if (old && (!str || strncmp(old, str, len)))
521 return gripe("Value for field %d must be \"%.*s\"",
522 fldno + 1, len, old);
524 return gripe("Value for field %d must be nil", fldno + 1);
531 xunsymbol1(char *id, struct symbol *symtab, struct castr *ca, int n)
533 int i = stmtch(id, symtab, offsetof(struct symbol, name),
534 sizeof(struct symbol));
536 return gripe("%s %s symbol `%s' in field %d",
537 i == M_NOTUNIQUE ? "Ambiguous" : "Unknown",
543 setsym(int fldno, char *sym)
546 struct symbol *symtab;
549 ca = getfld(fldno, NULL);
553 symtab = get_symtab(ca);
554 if (!symtab || (ca->ca_flags & NSC_BITS))
555 return gripe("Field %d doesn't take symbols", fldno + 1);
557 i = xunsymbol1(sym, symtab, ca, fldno);
560 return setnum(fldno, symtab[i].value);
564 mtsymset(int fldno, long *set)
567 struct symbol *symtab;
569 ca = getfld(fldno, NULL);
573 symtab = get_symtab(ca);
574 if (!symtab || !(ca->ca_flags & NSC_BITS)) {
575 return gripe("Field %d doesn't take symbol sets", fldno + 1);
582 add2symset(int fldno, long *set, char *sym)
585 struct symbol *symtab;
588 ca = getfld(fldno, NULL);
592 symtab = get_symtab(ca);
593 i = xunsymbol1(sym, symtab, ca, fldno);
596 *set |= symtab[i].value;
600 static struct symbol *
601 get_symtab(struct castr *ca)
603 int symtype = ca->ca_table;
604 struct symbol *symtab;
606 if (symtype == EF_BAD || ef_cadef(symtype) != symbol_ca)
609 symtab = ef_ptr(symtype, 0);
610 CANT_HAPPEN(!symtab);
615 xuheader(FILE *fp, int expected_table)
621 while ((ch = skipfs(fp)) == '\n')
623 if (ch == EOF && expected_table == EF_BAD)
630 ? fscanf(fp, "config%*[ \t]%63[^ \t#\n]%n", name, &res) != 1
631 : fscanf(fp, "XDUMP%*[ \t]%63[^ \t#\n]%*[ \t]%*[^ \t#\n]%n",
632 name, &res) != 1) || res < 0)
633 return gripe("Expected xdump header");
635 type = ef_byname(name);
637 return gripe("Unknown table `%s'", name);
638 if (expected_table != EF_BAD && expected_table != type)
639 return gripe("Expected table `%s', not `%s'",
640 ef_nameof(expected_table), name);
642 if (!ef_cadef(type) || !(ef_flags(type) & EFF_MEM)) {
643 CANT_HAPPEN(expected_table != EF_BAD);
644 return gripe("Undumping of table `%s' not implemented", name);
647 if (skipfs(fp) != '\n')
648 return gripe("Junk after xdump header");
655 xuheader1(FILE *fp, int type, struct castr ca[])
662 /* Allow repetition of the index field in continued table: */
664 while ((ch = skipfs(fp)) == '\n')
668 nflds = xuflds(fp, xufldname);
671 nflds -= ellipsis != 0;
676 for (i = 0; ca[i].ca_name; i++) {
677 if ((ca[i].ca_flags & NSC_EXTRA))
679 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
687 nflds = fidx - fldidx;
694 xutrailer(FILE *fp, int type, int row)
700 if (fscanf(fp, "config%n", &res) != 0 || res < 0)
701 return gripe("Malformed table footer");
703 if (fscanf(fp, "%d", &rows) != 1)
704 return gripe("Malformed table footer");
706 return gripe("Read %d rows, which doesn't match footer "
707 "%d rows", row, rows);
709 if (skipfs(fp) != '\n')
710 return gripe("Junk after table footer");
717 xundump(FILE *fp, char *file, int expected_table)
720 int type, nca, nf, i, ch;
727 if ((type = xuheader(fp, expected_table)) < 0)
731 if (CANT_HAPPEN(!ca))
735 for (i = 0; ca[i].ca_name; i++) {
737 if (!(ca[i].ca_flags & NSC_EXTRA))
738 nf += MAX(1, ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0);
740 fldca = calloc(nf, sizeof(*fldca));
741 fldidx = calloc(nf, sizeof(*fldidx));
742 caflds = calloc(nca, sizeof(*caflds));
745 if (xundump2(fp, type, ca) < 0)
752 /* Skip empty lines so that callers can easily check for EOF */
753 while ((ch = skipfs(fp)) == '\n')
761 xundump2(FILE *fp, int type, struct castr *ca)
766 if (xuheader1(fp, type, ca) < 0)
768 if (xundump1(fp, type, ca) < 0)
772 if (xuheader(fp, type) < 0)
778 xundump1(FILE *fp, int type, struct castr *ca)
780 struct empfile *ep = &empfile[type];
781 int need_sentinel = !EF_IS_GAME_STATE(type);
785 for (row = 0;; ++row) {
786 while ((ch = skipfs(fp)) == '\n')
793 if (xuflds(fp, xufld) < 0)
795 n = MAX(n, cur_id + 1);
798 if (CANT_HAPPEN(n > ep->fids))
801 if (EF_IS_GAME_STATE(type) && n != ep->csize)
802 /* TODO truncate file */
803 gripe("Warning: should resize table %s from %d to %d, not implemented",
804 ef_nameof(type), ep->csize, n);
805 else if (type >= EF_SHIP_CHR && type <= EF_NUKE_CHR)
806 ; /* shrinking these is okay */
808 return gripe("Table %s requires %d rows, got %d",
809 ef_nameof(type), ep->fids, n);
813 if (CANT_HAPPEN(n >= ep->csize))
814 return gripe("No space for sentinel");
815 memset(ep->cache + ep->size * n, 0, ep->size);
818 if (xutrailer(fp, type, row) < 0)