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
37 * - Normalize terminology: table/rows/columns or file/records/fields
38 * - Loading tables with NSC_STRING elements more than once leaks memory
40 * - Check each partial table supplies the same rows
41 * - Check EFF_CFG tables are dense
42 * - Symbolic references to non-symbol tables
43 * - Symbolic array indexes
44 * TODO, but hardly worth the effort:
45 * - Permit reordering of array elements
46 * - Permit repetition of array elements in split tables
63 #include "prototypes.h"
68 static int ellipsis, is_partial;
69 static int cur_type, cur_id;
72 static struct castr **fldca;
75 static unsigned char *caseen;
77 static int gripe(char *, ...) ATTRIBUTE((format (printf, 1, 2)));
78 static int deffld(int, char *, int);
79 static int defellipsis(int fldno);
80 static int chkflds(void);
81 static int setnum(int, double);
82 static int setstr(int, char *);
83 static int xunsymbol1(char *, struct symbol *, struct castr *, int);
84 static int setsym(int, char *);
85 static int mtsymset(int, long *);
86 static int add2symset(int, long *, char *);
87 static struct symbol *get_symtab(struct castr *);
88 static int xundump1(FILE *, int, struct castr *);
89 static int xundump2(FILE *, int, struct castr *);
96 fprintf(stderr, "%s:%d: ", fname, lineno);
98 vfprintf(stderr, fmt, ap);
112 } while (ch == ' ' || ch == '\t');
117 } while (ch != EOF && ch != '\n');
124 getid(FILE *fp, char *buf)
127 if (fscanf(fp, "%1023[^#() \t\n]%n", buf, &n) != 1 || !isalpha(buf[0]))
142 if (sscanf(++src, "%3o%n", &octal_chr, &n) != 1 || n != 3)
144 *dst++ = (char)octal_chr;
154 xufldname(FILE *fp, int i)
162 return gripe("Unexpected EOF");
169 if (getc(fp) != '.' || getc(fp) != '.')
170 return gripe("Junk in header field %d", i + 1);
172 return gripe("... not allowed in field 1");
173 if (defellipsis(i) < 0)
176 if (ch != EOF && ch != '\n')
177 return gripe("Junk after ...");
182 if (getid(fp, buf) < 0)
183 return gripe("Junk in header field %d", i + 1);
187 return deffld(i, buf, -1);
191 if (isdigit(ch) || ch == '-' || ch == '+') {
192 if (fscanf(fp, "%d", &idx) != 1)
193 return gripe("Malformed number in index of header field %d",
196 return gripe("Index must not be negative in header field %d",
199 if (getid(fp, buf) < 0)
200 return gripe("Malformed index in header field %d", i + 1);
201 return gripe("Symbolic index in header field %d not yet implemented",
206 return gripe("Malformed index in header field %d", i + 1);
207 return deffld(i, buf, idx);
212 xufld(FILE *fp, int i)
222 return gripe("Unexpected EOF");
225 return gripe("Field %s missing", fldca[i]->ca_name);
228 case '+': case '-': case '.':
229 case '0': case '1': case '2': case '3': case '4':
230 case '5': case '6': case '7': case '8': case '9':
232 if (fscanf(fp, "%lg", &dbl) != 1)
233 return gripe("Malformed number in field %d", i + 1);
234 return setnum(i, dbl);
241 if (fscanf(fp, "%1023[^\"\n]", buf) != 1 || getc(fp) != '"')
242 return gripe("Malformed string in field %d", i + 1);
244 return gripe("Invalid escape sequence in field %d",
247 return setstr(i, buf);
249 if (mtsymset(i, &set) < 0)
253 if (ch == EOF || ch == '\n')
254 return gripe("Unmatched '(' in field %d", i + 1);
258 if (getid(fp, buf) < 0)
259 return gripe("Junk in field %d", i + 1);
260 if (add2symset(i, &set, buf) < 0)
263 return setnum(i, set);
266 if (getid(fp, buf) < 0)
267 return gripe("Junk in field %d", i + 1);
268 if (!strcmp(buf, "nil"))
269 return setstr(i, NULL);
271 return setsym(i, buf);
276 xuflds(FILE *fp, int (*parse)(FILE *, int))
289 else if (ch != ' ' && ch != '\t')
290 return gripe("Bad field separator after field %d", i + 1);
295 deffld(int fldno, char *name, int idx)
297 struct castr *ca = ef_cadef(cur_type);
300 res = stmtch(name, ca, offsetof(struct castr, ca_name),
301 sizeof(struct castr));
303 return gripe("Header %s of field %d is %s", name, fldno + 1,
304 res == M_NOTUNIQUE ? "ambiguous" : "unknown");
305 if (ca[res].ca_type != NSC_STRINGY && ca[res].ca_len != 0) {
307 return gripe("Header %s requires an index in field %d",
308 ca[res].ca_name, fldno + 1);
309 if (idx >= ca[res].ca_len)
310 return gripe("Header %s(%d) index out of bounds in field %d",
311 ca[res].ca_name, idx, fldno + 1);
312 if (idx < caflds[res])
313 return gripe("Duplicate header %s(%d) in field %d",
314 ca[res].ca_name, idx, fldno + 1);
315 if (idx > caflds[res])
316 return gripe("Expected header %s(%d) in field %d",
317 ca[res].ca_name, caflds[res], fldno + 1);
320 return gripe("Header %s doesn't take an index in field %d",
321 ca[res].ca_name, fldno + 1);
323 if (caflds[res] && !caseen[res])
324 /* FIXME doesn't catch dupes within table part when caseen[res] */
325 return gripe("Duplicate header %s in field %d",
326 ca[res].ca_name, fldno + 1);
328 fldca[fldno] = &ca[res];
336 defellipsis(int fldno)
338 struct castr *ca = ef_cadef(cur_type);
340 if (ca[0].ca_table != cur_type)
341 return gripe("Table %s doesn't support ...", ef_nameof(cur_type));
350 struct castr *ca = ef_cadef(cur_type);
354 /* Require index field */
356 return gripe("Header field %s required with ...", ca[0].ca_name);
357 /* Want the index field again in continued table: */
362 for (i = 0; ca[i].ca_name; i++) {
363 if (ca[i].ca_flags & NSC_EXTRA)
365 len = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
366 if (!len && !caflds[i])
367 res = gripe("Header field %s missing", ca[i].ca_name);
368 else if (len && caflds[i] == len - 1)
369 res = gripe("Header field %s(%d) missing",
370 ca[i].ca_name, len - 1);
371 else if (len && caflds[i] < len - 1)
372 res = gripe("Header fields %s(%d) ... %s(%d) missing",
373 ca[i].ca_name, caflds[i], ca[i].ca_name, len - 1);
379 static struct castr *
380 getfld(int fldno, int *idx)
382 if (fldno >= nflds) {
383 gripe("Too many fields, expected only %d", nflds);
386 if (CANT_HAPPEN(fldno < 0))
389 *idx = fldidx[fldno];
394 fldval_must_match(int fldno)
396 struct castr *ca = ef_cadef(cur_type);
397 int i = fldca[fldno] - ca;
399 return (fldca[fldno]->ca_flags & NSC_CONST) || caseen[i];
403 getobj(struct castr *ca, int altid)
405 struct empfile *ep = &empfile[cur_type];
406 int need_sentinel = !EF_IS_GAME_STATE(cur_type);
409 if (ca->ca_table == cur_type)
411 if (cur_id >= ep->fids) {
412 /* TODO grow cache (and posssibly file) unless EFF_STATIC */
413 if (cur_id < ep->csize - !!need_sentinel)
414 ep->cids = ep->fids = cur_id + 1;
415 /* else: ef_ptr() will fail */
417 cur_obj = ef_ptr(cur_type, cur_id);
419 gripe("Can't put ID %d into table %s, it holds only 0..%d.",
420 cur_id, ep->name, ep->fids - 1);
427 setnum(int fldno, double dbl)
434 ca = getfld(fldno, &idx);
438 memb_ptr = getobj(ca, (int)dbl);
441 memb_ptr += ca->ca_off;
443 switch (ca->ca_type) {
446 old = ((signed char *)memb_ptr)[idx];
447 ((signed char *)memb_ptr)[idx] = (signed char)dbl;
450 old = ((unsigned char *)memb_ptr)[idx];
451 ((unsigned char *)memb_ptr)[idx] = (unsigned char)dbl;
454 old = ((short *)memb_ptr)[idx];
455 ((short *)memb_ptr)[idx] = (short)dbl;
458 old = ((unsigned short *)memb_ptr)[idx];
459 ((unsigned short *)memb_ptr)[idx] = (unsigned short)dbl;
462 old = ((int *)memb_ptr)[idx];
463 ((int *)memb_ptr)[idx] = (int)dbl;
466 old = ((long *)memb_ptr)[idx];
467 ((long *)memb_ptr)[idx] = (long)dbl;
470 old = ((coord *)memb_ptr)[idx];
471 /* FIXME use variant of xrel() that takes orig instead of nation */
472 if (old >= WORLD_X / 2)
474 ((coord *)memb_ptr)[idx] = XNORM((coord)dbl);
477 old = ((coord *)memb_ptr)[idx];
478 /* FIXME use variant of yrel() that takes orig instead of nation */
479 if (old >= WORLD_Y / 2)
481 ((coord *)memb_ptr)[idx] = YNORM((coord)dbl);
484 old = ((float *)memb_ptr)[idx];
485 ((float *)memb_ptr)[idx] = (float)dbl;
488 old = ((double *)memb_ptr)[idx];
489 ((double *)memb_ptr)[idx] = dbl;
492 old = ((time_t *)memb_ptr)[idx];
493 ((time_t *)memb_ptr)[idx] = (time_t)dbl;
496 return gripe("Field %d doesn't take numbers", fldno + 1);
499 if (fldval_must_match(fldno) && old != dbl)
500 return gripe("Value for field %d must be %g", fldno + 1, old);
506 setstr(int fldno, char *str)
511 char *memb_ptr, *old;
513 ca = getfld(fldno, &idx);
516 must_match = fldval_must_match(fldno);
518 memb_ptr = getobj(ca, cur_id);
521 memb_ptr += ca->ca_off;
523 switch (ca->ca_type) {
525 old = ((char **)memb_ptr)[idx];
527 ((char **)memb_ptr)[idx] = str ? strdup(str) : NULL;
528 len = 65535; /* really SIZE_MAX, but that's C99 */
531 if (CANT_HAPPEN(idx))
534 return gripe("Field doesn't take nil");
536 if (strlen(str) > len)
537 return gripe("Field %d takes at most %d characters",
541 strncpy(memb_ptr, str, len);
544 return gripe("Field %d doesn't take strings", fldno + 1);
548 if (old && (!str || strncmp(old, str, len)))
549 return gripe("Value for field %d must be \"%.*s\"",
550 fldno + 1, len, old);
552 return gripe("Value for field %d must be nil", fldno + 1);
559 xunsymbol1(char *id, struct symbol *symtab, struct castr *ca, int n)
561 int i = stmtch(id, symtab, offsetof(struct symbol, name),
562 sizeof(struct symbol));
564 return gripe("%s %s symbol `%s' in field %d",
565 i == M_NOTUNIQUE ? "Ambiguous" : "Unknown",
571 setsym(int fldno, char *sym)
574 struct symbol *symtab;
577 ca = getfld(fldno, NULL);
581 symtab = get_symtab(ca);
582 if (!symtab || (ca->ca_flags & NSC_BITS))
583 return gripe("Field %d doesn't take symbols", fldno + 1);
585 i = xunsymbol1(sym, symtab, ca, fldno);
588 return setnum(fldno, symtab[i].value);
592 mtsymset(int fldno, long *set)
595 struct symbol *symtab;
597 ca = getfld(fldno, NULL);
601 symtab = get_symtab(ca);
602 if (!symtab || !(ca->ca_flags & NSC_BITS)) {
603 return gripe("Field %d doesn't take symbol sets", fldno + 1);
610 add2symset(int fldno, long *set, char *sym)
613 struct symbol *symtab;
616 ca = getfld(fldno, NULL);
620 symtab = get_symtab(ca);
621 i = xunsymbol1(sym, symtab, ca, fldno);
624 *set |= symtab[i].value;
628 static struct symbol *
629 get_symtab(struct castr *ca)
631 int symtype = ca->ca_table;
632 struct symbol *symtab;
634 if (symtype == EF_BAD || ef_cadef(symtype) != symbol_ca)
637 symtab = ef_ptr(symtype, 0);
638 CANT_HAPPEN(!symtab);
643 xuheader(FILE *fp, int expected_table)
649 while ((ch = skipfs(fp)) == '\n')
651 if (ch == EOF && expected_table == EF_BAD)
658 ? fscanf(fp, "config%*[ \t]%63[^ \t#\n]%n", name, &res) != 1
659 : fscanf(fp, "XDUMP%*[ \t]%63[^ \t#\n]%*[ \t]%*[^ \t#\n]%n",
660 name, &res) != 1) || res < 0)
661 return gripe("Expected xdump header");
663 type = ef_byname(name);
665 return gripe("Unknown table `%s'", name);
666 if (expected_table != EF_BAD && expected_table != type)
667 return gripe("Expected table `%s', not `%s'",
668 ef_nameof(expected_table), name);
670 if (!ef_cadef(type) || !(ef_flags(type) & EFF_MEM)) {
671 CANT_HAPPEN(expected_table != EF_BAD);
672 return gripe("Table `%s' is not permitted here", name);
675 if (skipfs(fp) != '\n')
676 return gripe("Junk after xdump header");
683 xuheader1(FILE *fp, int type, struct castr ca[])
690 while ((ch = skipfs(fp)) == '\n')
694 nflds = xuflds(fp, xufldname);
697 nflds -= ellipsis != 0;
702 for (i = 0; ca[i].ca_name; i++) {
703 if ((ca[i].ca_flags & NSC_EXTRA))
705 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
713 nflds = fidx - fldidx;
720 xutrailer(FILE *fp, int type, int row)
726 if (fscanf(fp, "config%n", &res) != 0 || res < 0)
727 return gripe("Malformed table footer");
729 if (fscanf(fp, "%d", &rows) != 1)
730 return gripe("Malformed table footer");
732 return gripe("Read %d rows, which doesn't match footer "
733 "%d rows", row, rows);
735 if (skipfs(fp) != '\n')
736 return gripe("Junk after table footer");
743 xundump(FILE *fp, char *file, int expected_table)
746 int type, nca, nf, i, ch;
753 if ((type = xuheader(fp, expected_table)) < 0)
757 if (CANT_HAPPEN(!ca))
761 for (i = 0; ca[i].ca_name; i++) {
763 if (!(ca[i].ca_flags & NSC_EXTRA))
764 nf += MAX(1, ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0);
766 fldca = calloc(nf, sizeof(*fldca));
767 fldidx = calloc(nf, sizeof(*fldidx));
768 caflds = calloc(nca, sizeof(*caflds));
769 caseen = calloc(nca, sizeof(*caseen));
772 if (xundump2(fp, type, ca) < 0)
780 /* Skip empty lines so that callers can easily check for EOF */
781 while ((ch = skipfs(fp)) == '\n')
789 xundump2(FILE *fp, int type, struct castr *ca)
795 if (xuheader1(fp, type, ca) < 0)
797 if (xundump1(fp, type, ca) < 0)
801 for (i = 0; ca[i].ca_name; i++)
802 caseen[i] = caflds[i] != 0;
803 if (xuheader(fp, type) < 0)
809 xundump1(FILE *fp, int type, struct castr *ca)
811 struct empfile *ep = &empfile[type];
812 int need_sentinel = !EF_IS_GAME_STATE(type);
816 for (row = 0;; ++row) {
817 while ((ch = skipfs(fp)) == '\n')
824 if (xuflds(fp, xufld) < 0)
826 n = MAX(n, cur_id + 1);
829 if (CANT_HAPPEN(n > ep->fids))
832 if (EF_IS_GAME_STATE(type) && n != ep->csize)
833 /* TODO truncate file */
834 gripe("Warning: should resize table %s from %d to %d, not implemented",
835 ef_nameof(type), ep->csize, n);
836 else if (type >= EF_SHIP_CHR && type <= EF_NUKE_CHR)
837 ep->cids = ep->fids = n;
839 return gripe("Table %s requires %d rows, got %d",
840 ef_nameof(type), ep->fids, n);
844 if (CANT_HAPPEN(n >= ep->csize))
845 return gripe("No space for sentinel");
846 memset(ep->cache + ep->size * n, 0, ep->size);
849 if (xutrailer(fp, type, row) < 0)