2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, 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-2008
36 * See doc/xdump! And keep it up-to-date.
38 * Parsing of machine-readable xdump is not precise: it recognizes
39 * comments, accepts whitespace in place of single space, and accepts
40 * the full human-readable field syntax instead of its machine-
44 * - Normalize terminology: table/rows/columns or file/records/fields
45 * - Loading tables with NSC_STRING elements more than once leaks memory
47 * - Check each partial table supplies the same rows
48 * - Check EFF_CFG tables are dense
49 * - Symbolic array indexes
50 * - Option to treat missing and unknown fields as warning, not error
51 * TODO, but hardly worth the effort:
52 * - Permit reordering of array elements
69 static char *fname; /* Name of file being read */
70 static int lineno; /* Current line number */
71 static int human; /* Reading human-readable syntax? */
72 static int ellipsis; /* Header ended with ...? */
73 static int is_partial; /* Is input split into parts? */
74 static int cur_type; /* Current table's file type */
75 static void *cur_obj; /* The object being read into */
76 static int cur_id; /* and its index in the table */
77 static int cur_obj_is_blank;
78 static int nflds; /* #fields in input records */
79 static struct castr **fldca; /* Map field number to selector */
80 static int *fldidx; /* Map field number to index */
81 static int *caflds; /* Map selector number to #fields seen */
82 static int *cafldspp; /* ditto, in previous parts */
84 static int gripe(char *, ...) ATTRIBUTE((format (printf, 1, 2)));
85 static int deffld(int, char *, int);
86 static int defellipsis(void);
87 static int chkflds(void);
88 static int setnum(int, double);
89 static int setstr(int, char *);
90 static int xunsymbol(char *, struct castr *, int);
91 static int setsym(int, char *);
92 static int mtsymset(int, long *);
93 static int add2symset(int, long *, char *);
94 static int xubody(FILE *);
95 static int xutail(FILE *, struct castr *);
98 * Gripe about the current line to stderr, return -1.
101 gripe(char *fmt, ...)
105 fprintf(stderr, "%s:%d: ", fname, lineno);
107 vfprintf(stderr, fmt, ap);
115 * Read and ignore field separators from FP.
116 * Return first character that is not a field separator.
125 } while (ch == ' ' || ch == '\t');
130 } while (ch != EOF && ch != '\n');
137 * Decode escape sequences in BUF.
138 * Return BUF on success, null pointer on failure.
150 if (sscanf(++src, "%3o%n", &octal_chr, &n) != 1 || n != 3)
152 *dst++ = (char)octal_chr;
162 * Read an identifier from FP into BUF.
163 * BUF must have space for 1024 characters.
164 * Return number of characters read on success, -1 on failure.
167 getid(FILE *fp, char *buf)
170 if (fscanf(fp, "%1023[^\"#()<>= \t\n]%n", buf, &n) != 1
178 * Try to read a field name from FP.
179 * I is the field number, counting from zero.
180 * If a name is read, set fldca[I] and fldidx[I] for it, and update
182 * Return 1 if a name or ... was read, 0 on end of line, -1 on error.
185 xufldname(FILE *fp, int i)
193 return gripe("Unexpected EOF");
200 if (getc(fp) != '.' || getc(fp) != '.')
201 return gripe("Junk in header field %d", i + 1);
202 if (defellipsis() < 0)
205 if (ch != EOF && ch != '\n')
206 return gripe("Junk after ...");
211 if (getid(fp, buf) < 0)
212 return gripe("Junk in header field %d", i + 1);
216 return deffld(i, buf, -1);
220 if (isdigit(ch) || ch == '-' || ch == '+') {
221 if (fscanf(fp, "%d", &idx) != 1)
222 return gripe("Malformed number in index of header field %d",
225 return gripe("Index must not be negative in header field %d",
228 if (getid(fp, buf) < 0)
229 return gripe("Malformed index in header field %d", i + 1);
230 return gripe("Symbolic index in header field %d not yet implemented",
235 return gripe("Malformed index in header field %d", i + 1);
236 return deffld(i, buf, idx);
241 * Try to read a field value from FP.
242 * I is the field number, counting from zero.
243 * Return 1 if a value was read, 0 on end of line, -1 on error.
246 xufld(FILE *fp, int i)
256 return gripe("Unexpected EOF");
258 CANT_HAPPEN(i > nflds);
260 if (fldca[i]->ca_type != NSC_STRINGY && fldca[i]->ca_len)
261 return gripe("Field %s(%d) missing",
262 fldca[i]->ca_name, fldidx[i]);
263 return gripe("Field %s missing", fldca[i]->ca_name);
267 case '+': case '-': case '.':
268 case '0': case '1': case '2': case '3': case '4':
269 case '5': case '6': case '7': case '8': case '9':
271 if (fscanf(fp, "%lg", &dbl) != 1)
272 return gripe("Malformed number in field %d", i + 1);
273 return setnum(i, dbl);
280 if (fscanf(fp, "%1023[^\"\n]", buf) != 1 || getc(fp) != '"')
281 return gripe("Malformed string in field %d", i + 1);
283 return gripe("Invalid escape sequence in field %d",
286 return setstr(i, buf);
288 if (mtsymset(i, &set) < 0)
292 if (ch == EOF || ch == '\n')
293 return gripe("Unmatched '(' in field %d", i + 1);
297 if (getid(fp, buf) < 0)
298 return gripe("Junk in field %d", i + 1);
299 if (add2symset(i, &set, buf) < 0)
302 return setnum(i, set);
305 if (getid(fp, buf) < 0)
306 return gripe("Junk in field %d", i + 1);
307 if (!strcmp(buf, "nil"))
308 return setstr(i, NULL);
310 return setsym(i, buf);
315 * Read fields from FP.
316 * Use PARSE() to read each field.
317 * Return number of fields read on success, -1 on error.
320 xuflds(FILE *fp, int (*parse)(FILE *, int))
333 else if (ch != ' ' && ch != '\t')
334 return gripe("Bad field separator after field %d", i + 1);
339 * Define the FLDNO-th field.
340 * If IDX is negative, define as selector NAME, else as NAME(IDX).
341 * Set fldca[FLDNO] and fldidx[FLDNO] accordingly.
343 * Return 1 on success, -1 on error.
346 deffld(int fldno, char *name, int idx)
348 struct castr *ca = ef_cadef(cur_type);
351 res = stmtch(name, ca, offsetof(struct castr, ca_name),
352 sizeof(struct castr));
354 return gripe("Header %s of field %d is %s", name, fldno + 1,
355 res == M_NOTUNIQUE ? "ambiguous" : "unknown");
356 if (ca[res].ca_flags == NSC_EXTRA || CANT_HAPPEN(ca[res].ca_get))
357 return gripe("Extraneous header %s in field %d", name, fldno + 1);
358 if (ca[res].ca_type != NSC_STRINGY && ca[res].ca_len != 0) {
360 return gripe("Header %s requires an index in field %d",
361 ca[res].ca_name, fldno + 1);
362 if (idx >= ca[res].ca_len)
363 return gripe("Header %s(%d) index out of bounds in field %d",
364 ca[res].ca_name, idx, fldno + 1);
365 if (idx < caflds[res])
366 return gripe("Duplicate header %s(%d) in field %d",
367 ca[res].ca_name, idx, fldno + 1);
368 if (idx > caflds[res])
369 return gripe("Expected header %s(%d) in field %d",
370 ca[res].ca_name, caflds[res], fldno + 1);
373 return gripe("Header %s doesn't take an index in field %d",
374 ca[res].ca_name, fldno + 1);
377 return gripe("Duplicate header %s in field %d",
378 ca[res].ca_name, fldno + 1);
380 fldca[fldno] = &ca[res];
387 * Record that header ends with ...
388 * Set ellipsis and is_partial.
389 * Return 0 on success, -1 on error.
394 struct castr *ca = ef_cadef(cur_type);
396 if (ca[0].ca_table != cur_type)
397 return gripe("Table %s doesn't support ...", ef_nameof(cur_type));
398 ellipsis = is_partial = 1;
403 * Check fields in xdump are sane.
404 * Return 0 on success, -1 on error.
409 struct castr *ca = ef_cadef(cur_type);
410 int i, len, cafldsmax, res = 0;
413 /* Need a join field, use 0-th selector */
415 return gripe("Header field %s required with ...", ca[0].ca_name);
421 for (i = 0; ca[i].ca_name; i++) {
422 cafldsmax = MAX(caflds[i], cafldspp[i]);
423 if (ca[i].ca_flags & NSC_EXTRA)
425 len = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
426 if (!len && !cafldsmax)
427 res = gripe("Header field %s missing", ca[i].ca_name);
428 else if (len && cafldsmax == len - 1)
429 res = gripe("Header field %s(%d) missing",
430 ca[i].ca_name, len - 1);
431 else if (len && cafldsmax < len - 1)
432 res = gripe("Header fields %s(%d) ... %s(%d) missing",
433 ca[i].ca_name, cafldsmax, ca[i].ca_name, len - 1);
440 * Get selector for field FLDNO.
441 * Assign the field's selector index to *IDX, unless it is null.
442 * Return the selector on success, null pointer on error.
444 static struct castr *
445 getfld(int fldno, int *idx)
447 if (fldno >= nflds) {
448 gripe("Too many fields, expected only %d", nflds);
451 if (CANT_HAPPEN(fldno < 0))
454 *idx = fldidx[fldno];
459 * Is a new value for field FLDNO required to match the old one?
462 fldval_must_match(int fldno)
464 struct castr *ca = ef_cadef(cur_type);
465 int i = fldca[fldno] - ca;
468 * Value must match if:
469 * it's for a const selector, unless the object is still blank, or
470 * it was already given in a previous part of a split table.
472 return (!cur_obj_is_blank && (fldca[fldno]->ca_flags & NSC_CONST))
473 || fldidx[fldno] < cafldspp[i];
477 * Get the current object.
478 * Store it in cur_obj, and set cur_obj_is_blank accordingly.
479 * Return cur_obj, which is null on error.
484 struct empfile *ep = &empfile[cur_type];
485 int need_sentinel = !EF_IS_GAME_STATE(cur_type);
488 cur_obj_is_blank = cur_id >= ep->fids - !!need_sentinel;
489 if (cur_obj_is_blank) {
490 if (ef_ensure_space(cur_type, cur_id + !!need_sentinel, 1))
491 cur_obj = ef_ptr(cur_type, cur_id);
492 /* FIXME diagnose out of dynamic memory vs. static table full */
494 gripe("Can't put ID %d into table %s, it holds only 0..%d.",
495 cur_id, ep->name, ep->fids - !!need_sentinel - 1);
497 cur_obj = ef_ptr(cur_type, cur_id);
504 * Set value of field FLDNO in current object to DBL.
505 * Return 1 on success, -1 on error.
508 setnum(int fldno, double dbl)
515 ca = getfld(fldno, &idx);
520 * If this is the record index, put it into cur_id.
522 if (fldno == 0 && ca->ca_table == cur_type)
528 memb_ptr += ca->ca_off;
530 /* FIXME check assignment preserves value */
531 switch (ca->ca_type) {
533 old = ((signed char *)memb_ptr)[idx];
534 ((signed char *)memb_ptr)[idx] = (signed char)dbl;
538 old = ((unsigned char *)memb_ptr)[idx];
539 ((unsigned char *)memb_ptr)[idx] = (unsigned char)dbl;
542 old = ((short *)memb_ptr)[idx];
543 ((short *)memb_ptr)[idx] = (short)dbl;
546 old = ((unsigned short *)memb_ptr)[idx];
547 ((unsigned short *)memb_ptr)[idx] = (unsigned short)dbl;
550 old = ((int *)memb_ptr)[idx];
551 ((int *)memb_ptr)[idx] = (int)dbl;
554 old = ((long *)memb_ptr)[idx];
555 ((long *)memb_ptr)[idx] = (long)dbl;
558 old = ((coord *)memb_ptr)[idx];
559 /* FIXME use variant of xrel() that takes orig instead of nation */
560 if (old >= WORLD_X / 2)
562 ((coord *)memb_ptr)[idx] = XNORM((coord)dbl);
565 old = ((coord *)memb_ptr)[idx];
566 /* FIXME use variant of yrel() that takes orig instead of nation */
567 if (old >= WORLD_Y / 2)
569 ((coord *)memb_ptr)[idx] = YNORM((coord)dbl);
572 old = ((float *)memb_ptr)[idx];
573 ((float *)memb_ptr)[idx] = (float)dbl;
576 old = ((double *)memb_ptr)[idx];
577 ((double *)memb_ptr)[idx] = dbl;
580 old = ((time_t *)memb_ptr)[idx];
581 ((time_t *)memb_ptr)[idx] = (time_t)dbl;
584 return gripe("Field %d doesn't take numbers", fldno + 1);
587 if (fldval_must_match(fldno) && old != dbl)
588 return gripe("Value for field %d must be %g", fldno + 1, old);
594 * Set value of field FLDNO in current object to STR.
595 * Return 1 on success, -1 on error.
598 setstr(int fldno, char *str)
603 char *memb_ptr, *old;
605 ca = getfld(fldno, &idx);
612 memb_ptr += ca->ca_off;
613 must_match = fldval_must_match(fldno);
615 switch (ca->ca_type) {
617 old = ((char **)memb_ptr)[idx];
619 ((char **)memb_ptr)[idx] = str ? strdup(str) : NULL;
620 len = 65535; /* really SIZE_MAX, but that's C99 */
623 if (CANT_HAPPEN(idx))
626 return gripe("Field %d doesn't take nil", fldno + 1);
628 if (strlen(str) > len)
629 return gripe("Field %d takes at most %d characters",
630 fldno + 1, (int)len);
633 strncpy(memb_ptr, str, len);
636 return gripe("Field %d doesn't take strings", fldno + 1);
640 if (old && (!str || strncmp(old, str, len)))
641 return gripe("Value for field %d must be \"%.*s\"",
642 fldno + 1, (int)len, old);
644 return gripe("Value for field %d must be nil", fldno + 1);
651 * Resolve symbol name ID in table referred to by CA.
652 * Use field number N for error messages.
653 * Return index in referred table on success, -1 on failure.
656 xunsymbol(char *id, struct castr *ca, int n)
658 int i = ef_elt_byname(ca->ca_table, id);
660 return gripe("%s %s symbol `%s' in field %d",
661 i == M_NOTUNIQUE ? "Ambiguous" : "Unknown",
662 ca->ca_name, id, n + 1);
667 * Map symbol index to symbol value.
668 * CA is the table, and I is the index in it.
671 symval(struct castr *ca, int i)
673 int type = ca->ca_table;
675 if (ef_check(type) < 0)
677 if (ef_cadef(type) == symbol_ca)
678 /* symbol table, value is in the table */
679 return ((struct symbol *)ef_ptr(type, i))->value;
680 /* value is the table index */
685 * Set value of field FLDNO in current object to value of symbol SYM.
686 * Return 1 on success, -1 on error.
689 setsym(int fldno, char *sym)
694 ca = getfld(fldno, NULL);
698 if (ca->ca_table == EF_BAD || (ca->ca_flags & NSC_BITS))
699 return gripe("Field %d doesn't take symbols", fldno + 1);
701 i = xunsymbol(sym, ca, fldno);
704 return setnum(fldno, symval(ca, i));
708 * Create an empty symbol set for field FLDNO in *SET.
709 * Return 1 on success, -1 on error.
712 mtsymset(int fldno, long *set)
716 ca = getfld(fldno, NULL);
720 if (ca->ca_table == EF_BAD || ef_cadef(ca->ca_table) != symbol_ca
721 || !(ca->ca_flags & NSC_BITS))
722 return gripe("Field %d doesn't take symbol sets", fldno + 1);
728 * Add a symbol to a symbol set for field FLDNO in *SET.
729 * SYM is the name of the symbol to add.
730 * Return 1 on success, -1 on error.
733 add2symset(int fldno, long *set, char *sym)
738 ca = getfld(fldno, NULL);
742 i = xunsymbol(sym, ca, fldno);
745 *set |= symval(ca, i);
750 * Read an xdump table header line from FP.
751 * Expect header for EXPECTED_TABLE, unless it is EF_BAD.
752 * Recognize header for machine- and human-readable syntax, and set
754 * Return table type on success, -2 on EOF before header, -1 on failure.
757 xuheader(FILE *fp, int expected_table)
763 while ((ch = skipfs(fp)) == '\n')
765 if (ch == EOF && expected_table == EF_BAD)
772 ? fscanf(fp, "config%*[ \t]%63[^ \t#\n]%n", name, &res) != 1
773 : fscanf(fp, "XDUMP%*[ \t]%63[^ \t#\n]%*[ \t]%*[^ \t#\n]%n",
774 name, &res) != 1) || res < 0)
775 return gripe("Expected xdump header");
777 type = ef_byname(name);
779 return gripe("Unknown table `%s'", name);
780 if (expected_table != EF_BAD && expected_table != type)
781 return gripe("Expected table `%s', not `%s'",
782 ef_nameof(expected_table), name);
784 if (!ef_cadef(type) || !(ef_flags(type) & EFF_MEM)) {
785 CANT_HAPPEN(expected_table != EF_BAD);
786 return gripe("Table `%s' is not permitted here", name);
789 if (skipfs(fp) != '\n')
790 return gripe("Junk after xdump header");
797 * Find fields in this xdump.
798 * If reading human-readable syntax, read a field header line from FP.
799 * Else take fields from the table's selectors in CA[].
800 * Set ellipsis, nflds, fldca[], fldidx[] and caflds[] accordingly.
801 * Return 0 on success, -1 on failure.
804 xufldhdr(FILE *fp, struct castr ca[])
810 for (i = 0; ca[i].ca_name; i++)
815 while ((ch = skipfs(fp)) == '\n')
818 nflds = xuflds(fp, xufldname);
821 nflds -= ellipsis != 0;
826 for (i = 0; ca[i].ca_name; i++) {
827 if ((ca[i].ca_flags & NSC_EXTRA))
829 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
837 nflds = fidx - fldidx;
844 * Read xdump footer from FP.
845 * CA[] contains the table's selectors.
846 * The body had RECS records.
847 * Update cafldspp[] from caflds[].
848 * Return 0 on success, -1 on failure.
851 xufooter(FILE *fp, struct castr ca[], int recs)
857 if (fscanf(fp, "config%n", &res) != 0 || res < 0)
858 return gripe("Malformed table footer");
860 if (fscanf(fp, "%d", &n) != 1)
861 return gripe("Malformed table footer");
863 return gripe("Read %d rows, which doesn't match footer "
866 if (skipfs(fp) != '\n')
867 return gripe("Junk after table footer");
870 for (i = 0; ca[i].ca_name; i++) {
871 if (cafldspp[i] < caflds[i])
872 cafldspp[i] = caflds[i];
879 * Read an xdump table from FP.
880 * Both machine- and human-readable xdump syntax are recognized.
881 * Expect table EXPECTED_TABLE, unless it is EF_BAD.
882 * Report errors to stderr.
883 * Messages assume FP starts in the file FILE at line *PLNO.
884 * Update *PLNO to reflect lines read from FP.
885 * Return table type on success, -2 on EOF before header, -1 on failure.
888 xundump(FILE *fp, char *file, int *plno, int expected_table)
891 int type, nca, nf, i, ch;
896 if ((type = xuheader(fp, expected_table)) < 0)
900 if (CANT_HAPPEN(!ca))
904 for (i = 0; ca[i].ca_name; i++) {
906 if (!(ca[i].ca_flags & NSC_EXTRA))
907 nf += MAX(1, ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0);
909 fldca = malloc(nf * sizeof(*fldca));
910 fldidx = malloc(nf * sizeof(*fldidx));
911 caflds = malloc(nca * sizeof(*caflds));
912 cafldspp = calloc(nca, sizeof(*cafldspp));
915 if (xutail(fp, ca) < 0)
923 /* Skip empty lines so that callers can easily check for EOF */
924 while ((ch = skipfs(fp)) == '\n')
933 * Read the remainder of an xdump after the table header line from FP.
934 * CA[] contains the table's selectors.
935 * Return 0 on success, -1 on failure.
938 xutail(FILE *fp, struct castr *ca)
944 if (xufldhdr(fp, ca) < 0)
946 if ((recs = xubody(fp)) < 0)
948 if (xufooter(fp, ca, recs) < 0)
952 if (xuheader(fp, cur_type) < 0)
958 * Read the body of an xdump table from FP.
959 * Return number of rows read on success, -1 on failure.
964 struct empfile *ep = &empfile[cur_type];
965 int need_sentinel = !EF_IS_GAME_STATE(cur_type);
966 int old_maxid = ep->fids;
969 if (old_maxid == 0 && need_sentinel)
970 ef_ensure_space(cur_type, 0, 1);
974 while ((ch = skipfs(fp)) == '\n')
981 if (xuflds(fp, xufld) < 0)
983 maxid = MAX(maxid, cur_id + 1);
986 if (maxid >= old_maxid && need_sentinel) {
987 /* appended a sentinel, strip it off */
992 if (CANT_HAPPEN(maxid > ep->fids))
994 if (maxid < ep->fids) {
995 if (EF_IS_GAME_STATE(cur_type)
996 || (cur_type >= EF_SHIP_CHR && cur_type <= EF_NUKE_CHR))
997 ef_truncate(cur_type, maxid);
999 return gripe("Table %s requires %d rows, got %d",
1000 ef_nameof(cur_type), ep->fids, maxid);