2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2007, 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-2007
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
58 #include "prototypes.h"
63 static int ellipsis, is_partial;
64 static int cur_type, cur_id;
66 static int cur_obj_is_blank;
68 static struct castr **fldca;
70 static int *caflds; /* Map selector number to #fields seen */
71 static int *cafldspp; /* ditto, in previous parts */
73 static int gripe(char *, ...) ATTRIBUTE((format (printf, 1, 2)));
74 static int deffld(int, char *, int);
75 static int defellipsis(int fldno);
76 static int chkflds(void);
77 static int setnum(int, double);
78 static int setstr(int, char *);
79 static int xunsymbol(char *, struct castr *, int);
80 static int setsym(int, char *);
81 static int mtsymset(int, long *);
82 static int add2symset(int, long *, char *);
83 static int xubody(FILE *);
84 static int xutail(FILE *, struct castr *);
91 fprintf(stderr, "%s:%d: ", fname, lineno);
93 vfprintf(stderr, fmt, ap);
107 } while (ch == ' ' || ch == '\t');
112 } while (ch != EOF && ch != '\n');
119 getid(FILE *fp, char *buf)
122 if (fscanf(fp, "%1023[^#()<>=#\" \t\n]%n", buf, &n) != 1
138 if (sscanf(++src, "%3o%n", &octal_chr, &n) != 1 || n != 3)
140 *dst++ = (char)octal_chr;
150 xufldname(FILE *fp, int i)
158 return gripe("Unexpected EOF");
165 if (getc(fp) != '.' || getc(fp) != '.')
166 return gripe("Junk in header field %d", i + 1);
168 return gripe("... not allowed in field 1");
169 if (defellipsis(i) < 0)
172 if (ch != EOF && ch != '\n')
173 return gripe("Junk after ...");
178 if (getid(fp, buf) < 0)
179 return gripe("Junk in header field %d", i + 1);
183 return deffld(i, buf, -1);
187 if (isdigit(ch) || ch == '-' || ch == '+') {
188 if (fscanf(fp, "%d", &idx) != 1)
189 return gripe("Malformed number in index of header field %d",
192 return gripe("Index must not be negative in header field %d",
195 if (getid(fp, buf) < 0)
196 return gripe("Malformed index in header field %d", i + 1);
197 return gripe("Symbolic index in header field %d not yet implemented",
202 return gripe("Malformed index in header field %d", i + 1);
203 return deffld(i, buf, idx);
208 xufld(FILE *fp, int i)
218 return gripe("Unexpected EOF");
221 if (fldca[i]->ca_type != NSC_STRINGY && fldca[i]->ca_len)
222 return gripe("Field %s(%d) missing",
223 fldca[i]->ca_name, fldidx[i]);
224 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);
324 return gripe("Duplicate header %s in field %d",
325 ca[res].ca_name, fldno + 1);
327 fldca[fldno] = &ca[res];
334 defellipsis(int fldno)
336 struct castr *ca = ef_cadef(cur_type);
338 if (ca[0].ca_table != cur_type)
339 return gripe("Table %s doesn't support ...", ef_nameof(cur_type));
348 struct castr *ca = ef_cadef(cur_type);
349 int i, len, cafldsmax, res = 0;
352 /* Need a join field, use 0-th selector */
354 return gripe("Header field %s required with ...", ca[0].ca_name);
360 for (i = 0; ca[i].ca_name; i++) {
361 cafldsmax = MAX(caflds[i], cafldspp[i]);
362 if (ca[i].ca_flags & NSC_EXTRA)
364 len = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
365 if (!len && !cafldsmax)
366 res = gripe("Header field %s missing", ca[i].ca_name);
367 else if (len && cafldsmax == len - 1)
368 res = gripe("Header field %s(%d) missing",
369 ca[i].ca_name, len - 1);
370 else if (len && cafldsmax < len - 1)
371 res = gripe("Header fields %s(%d) ... %s(%d) missing",
372 ca[i].ca_name, cafldsmax, ca[i].ca_name, len - 1);
378 static struct castr *
379 getfld(int fldno, int *idx)
381 if (fldno >= nflds) {
382 gripe("Too many fields, expected only %d", nflds);
385 if (CANT_HAPPEN(fldno < 0))
388 *idx = fldidx[fldno];
393 fldval_must_match(int fldno)
395 struct castr *ca = ef_cadef(cur_type);
396 int i = fldca[fldno] - ca;
399 * Value must match if:
400 * it's for a const selector, unless the object is still blank, or
401 * it was already given in a previous part of a split table.
403 return (!cur_obj_is_blank && (fldca[fldno]->ca_flags & NSC_CONST))
404 || fldidx[fldno] < cafldspp[i];
410 struct empfile *ep = &empfile[cur_type];
411 int need_sentinel = !EF_IS_GAME_STATE(cur_type);
414 cur_obj_is_blank = cur_id >= ep->fids;
415 if (cur_obj_is_blank) {
416 /* TODO grow cache (and posssibly file) unless EFF_STATIC */
417 if (cur_id < ep->csize - !!need_sentinel)
418 ep->cids = ep->fids = cur_id + 1;
419 /* else: ef_ptr() will fail */
421 cur_obj = ef_ptr(cur_type, cur_id);
423 gripe("Can't put ID %d into table %s, it holds only 0..%d.",
424 cur_id, ep->name, ep->fids - 1);
431 setnum(int fldno, double dbl)
438 ca = getfld(fldno, &idx);
443 * If this is the record index, put it into cur_id.
445 if (fldno == 0 && ca->ca_table == cur_type)
451 memb_ptr += ca->ca_off;
453 /* FIXME check assignment preserves value */
454 switch (ca->ca_type) {
456 old = ((signed char *)memb_ptr)[idx];
457 ((signed char *)memb_ptr)[idx] = (signed char)dbl;
461 old = ((unsigned char *)memb_ptr)[idx];
462 ((unsigned char *)memb_ptr)[idx] = (unsigned char)dbl;
465 old = ((short *)memb_ptr)[idx];
466 ((short *)memb_ptr)[idx] = (short)dbl;
469 old = ((unsigned short *)memb_ptr)[idx];
470 ((unsigned short *)memb_ptr)[idx] = (unsigned short)dbl;
473 old = ((int *)memb_ptr)[idx];
474 ((int *)memb_ptr)[idx] = (int)dbl;
477 old = ((long *)memb_ptr)[idx];
478 ((long *)memb_ptr)[idx] = (long)dbl;
481 old = ((coord *)memb_ptr)[idx];
482 /* FIXME use variant of xrel() that takes orig instead of nation */
483 if (old >= WORLD_X / 2)
485 ((coord *)memb_ptr)[idx] = XNORM((coord)dbl);
488 old = ((coord *)memb_ptr)[idx];
489 /* FIXME use variant of yrel() that takes orig instead of nation */
490 if (old >= WORLD_Y / 2)
492 ((coord *)memb_ptr)[idx] = YNORM((coord)dbl);
495 old = ((float *)memb_ptr)[idx];
496 ((float *)memb_ptr)[idx] = (float)dbl;
499 old = ((double *)memb_ptr)[idx];
500 ((double *)memb_ptr)[idx] = dbl;
503 old = ((time_t *)memb_ptr)[idx];
504 ((time_t *)memb_ptr)[idx] = (time_t)dbl;
507 return gripe("Field %d doesn't take numbers", fldno + 1);
510 if (fldval_must_match(fldno) && old != dbl)
511 return gripe("Value for field %d must be %g", fldno + 1, old);
517 setstr(int fldno, char *str)
522 char *memb_ptr, *old;
524 ca = getfld(fldno, &idx);
531 memb_ptr += ca->ca_off;
532 must_match = fldval_must_match(fldno);
534 switch (ca->ca_type) {
536 old = ((char **)memb_ptr)[idx];
538 ((char **)memb_ptr)[idx] = str ? strdup(str) : NULL;
539 len = 65535; /* really SIZE_MAX, but that's C99 */
542 if (CANT_HAPPEN(idx))
545 return gripe("Field %d doesn't take nil", fldno + 1);
547 if (strlen(str) > len)
548 return gripe("Field %d takes at most %d characters",
549 fldno + 1, (int)len);
552 strncpy(memb_ptr, str, len);
555 return gripe("Field %d doesn't take strings", fldno + 1);
559 if (old && (!str || strncmp(old, str, len)))
560 return gripe("Value for field %d must be \"%.*s\"",
561 fldno + 1, (int)len, old);
563 return gripe("Value for field %d must be nil", fldno + 1);
570 xunsymbol(char *id, struct castr *ca, int n)
572 int i = ef_elt_byname(ca->ca_table, id);
574 return gripe("%s %s symbol `%s' in field %d",
575 i == M_NOTUNIQUE ? "Ambiguous" : "Unknown",
576 ca->ca_name, id, n + 1);
581 symval(struct castr *ca, int i)
583 int type = ca->ca_table;
585 if (ef_check(type) < 0)
587 if (ef_cadef(type) == symbol_ca)
588 /* symbol table, value is in the table */
589 return ((struct symbol *)ef_ptr(type, i))->value;
590 /* value is the table index */
595 setsym(int fldno, char *sym)
600 ca = getfld(fldno, NULL);
604 if (ca->ca_table == EF_BAD || (ca->ca_flags & NSC_BITS))
605 return gripe("Field %d doesn't take symbols", fldno + 1);
607 i = xunsymbol(sym, ca, fldno);
610 return setnum(fldno, symval(ca, i));
614 mtsymset(int fldno, long *set)
618 ca = getfld(fldno, NULL);
622 if (ca->ca_table == EF_BAD || ef_cadef(ca->ca_table) != symbol_ca
623 || !(ca->ca_flags & NSC_BITS))
624 return gripe("Field %d doesn't take symbol sets", fldno + 1);
630 add2symset(int fldno, long *set, char *sym)
635 ca = getfld(fldno, NULL);
639 i = xunsymbol(sym, ca, fldno);
642 *set |= symval(ca, i);
647 xuheader(FILE *fp, int expected_table)
653 while ((ch = skipfs(fp)) == '\n')
655 if (ch == EOF && expected_table == EF_BAD)
662 ? fscanf(fp, "config%*[ \t]%63[^ \t#\n]%n", name, &res) != 1
663 : fscanf(fp, "XDUMP%*[ \t]%63[^ \t#\n]%*[ \t]%*[^ \t#\n]%n",
664 name, &res) != 1) || res < 0)
665 return gripe("Expected xdump header");
667 type = ef_byname(name);
669 return gripe("Unknown table `%s'", name);
670 if (expected_table != EF_BAD && expected_table != type)
671 return gripe("Expected table `%s', not `%s'",
672 ef_nameof(expected_table), name);
674 if (!ef_cadef(type) || !(ef_flags(type) & EFF_MEM)) {
675 CANT_HAPPEN(expected_table != EF_BAD);
676 return gripe("Table `%s' is not permitted here", name);
679 if (skipfs(fp) != '\n')
680 return gripe("Junk after xdump header");
687 xufldhdr(FILE *fp, struct castr ca[])
693 for (i = 0; ca[i].ca_name; i++)
698 while ((ch = skipfs(fp)) == '\n')
701 nflds = xuflds(fp, xufldname);
704 nflds -= ellipsis != 0;
709 for (i = 0; ca[i].ca_name; i++) {
710 if ((ca[i].ca_flags & NSC_EXTRA))
712 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
720 nflds = fidx - fldidx;
727 xufooter(FILE *fp, struct castr ca[], int row)
733 if (fscanf(fp, "config%n", &res) != 0 || res < 0)
734 return gripe("Malformed table footer");
736 if (fscanf(fp, "%d", &rows) != 1)
737 return gripe("Malformed table footer");
739 return gripe("Read %d rows, which doesn't match footer "
740 "%d rows", row, rows);
742 if (skipfs(fp) != '\n')
743 return gripe("Junk after table footer");
746 for (i = 0; ca[i].ca_name; i++) {
747 if (cafldspp[i] < caflds[i])
748 cafldspp[i] = caflds[i];
755 xundump(FILE *fp, char *file, int *plno, int expected_table)
758 int type, nca, nf, i, ch;
763 if ((type = xuheader(fp, expected_table)) < 0)
767 if (CANT_HAPPEN(!ca))
771 for (i = 0; ca[i].ca_name; i++) {
773 if (!(ca[i].ca_flags & NSC_EXTRA))
774 nf += MAX(1, ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0);
776 fldca = calloc(nf, sizeof(*fldca));
777 fldidx = calloc(nf, sizeof(*fldidx));
778 caflds = malloc(nca * sizeof(*caflds));
779 cafldspp = calloc(nca, sizeof(*cafldspp));
782 if (xutail(fp, ca) < 0)
790 /* Skip empty lines so that callers can easily check for EOF */
791 while ((ch = skipfs(fp)) == '\n')
800 xutail(FILE *fp, struct castr *ca)
806 if (xufldhdr(fp, ca) < 0)
808 if ((recs = xubody(fp)) < 0)
810 if (xufooter(fp, ca, recs) < 0)
814 if (xuheader(fp, cur_type) < 0)
822 struct empfile *ep = &empfile[cur_type];
823 int need_sentinel = !EF_IS_GAME_STATE(cur_type);
827 for (row = 0;; ++row) {
828 while ((ch = skipfs(fp)) == '\n')
835 if (xuflds(fp, xufld) < 0)
837 n = MAX(n, cur_id + 1);
840 if (CANT_HAPPEN(n > ep->fids))
843 if (EF_IS_GAME_STATE(cur_type) && n != ep->csize)
844 /* TODO truncate file */
845 gripe("Warning: should resize table %s from %d to %d, not implemented",
846 ef_nameof(cur_type), ep->csize, n);
847 else if (cur_type >= EF_SHIP_CHR && cur_type <= EF_NUKE_CHR)
848 ep->cids = ep->fids = n;
850 return gripe("Table %s requires %d rows, got %d",
851 ef_nameof(cur_type), ep->fids, n);
855 if (CANT_HAPPEN(n >= ep->csize))
856 return gripe("No space for sentinel");
857 memset(ep->cache + ep->size * n, 0, ep->size);