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(void);
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);
167 if (defellipsis() < 0)
170 if (ch != EOF && ch != '\n')
171 return gripe("Junk after ...");
176 if (getid(fp, buf) < 0)
177 return gripe("Junk in header field %d", i + 1);
181 return deffld(i, buf, -1);
185 if (isdigit(ch) || ch == '-' || ch == '+') {
186 if (fscanf(fp, "%d", &idx) != 1)
187 return gripe("Malformed number in index of header field %d",
190 return gripe("Index must not be negative in header field %d",
193 if (getid(fp, buf) < 0)
194 return gripe("Malformed index in header field %d", i + 1);
195 return gripe("Symbolic index in header field %d not yet implemented",
200 return gripe("Malformed index in header field %d", i + 1);
201 return deffld(i, buf, idx);
206 xufld(FILE *fp, int i)
216 return gripe("Unexpected EOF");
218 CANT_HAPPEN(i > nflds);
220 if (fldca[i]->ca_type != NSC_STRINGY && fldca[i]->ca_len)
221 return gripe("Field %s(%d) missing",
222 fldca[i]->ca_name, fldidx[i]);
223 return gripe("Field %s missing", fldca[i]->ca_name);
227 case '+': case '-': case '.':
228 case '0': case '1': case '2': case '3': case '4':
229 case '5': case '6': case '7': case '8': case '9':
231 if (fscanf(fp, "%lg", &dbl) != 1)
232 return gripe("Malformed number in field %d", i + 1);
233 return setnum(i, dbl);
240 if (fscanf(fp, "%1023[^\"\n]", buf) != 1 || getc(fp) != '"')
241 return gripe("Malformed string in field %d", i + 1);
243 return gripe("Invalid escape sequence in field %d",
246 return setstr(i, buf);
248 if (mtsymset(i, &set) < 0)
252 if (ch == EOF || ch == '\n')
253 return gripe("Unmatched '(' in field %d", i + 1);
257 if (getid(fp, buf) < 0)
258 return gripe("Junk in field %d", i + 1);
259 if (add2symset(i, &set, buf) < 0)
262 return setnum(i, set);
265 if (getid(fp, buf) < 0)
266 return gripe("Junk in field %d", i + 1);
267 if (!strcmp(buf, "nil"))
268 return setstr(i, NULL);
270 return setsym(i, buf);
275 xuflds(FILE *fp, int (*parse)(FILE *, int))
288 else if (ch != ' ' && ch != '\t')
289 return gripe("Bad field separator after field %d", i + 1);
294 deffld(int fldno, char *name, int idx)
296 struct castr *ca = ef_cadef(cur_type);
299 res = stmtch(name, ca, offsetof(struct castr, ca_name),
300 sizeof(struct castr));
302 return gripe("Header %s of field %d is %s", name, fldno + 1,
303 res == M_NOTUNIQUE ? "ambiguous" : "unknown");
304 if (ca[res].ca_type != NSC_STRINGY && ca[res].ca_len != 0) {
306 return gripe("Header %s requires an index in field %d",
307 ca[res].ca_name, fldno + 1);
308 if (idx >= ca[res].ca_len)
309 return gripe("Header %s(%d) index out of bounds in field %d",
310 ca[res].ca_name, idx, fldno + 1);
311 if (idx < caflds[res])
312 return gripe("Duplicate header %s(%d) in field %d",
313 ca[res].ca_name, idx, fldno + 1);
314 if (idx > caflds[res])
315 return gripe("Expected header %s(%d) in field %d",
316 ca[res].ca_name, caflds[res], fldno + 1);
319 return gripe("Header %s doesn't take an index in field %d",
320 ca[res].ca_name, fldno + 1);
323 return gripe("Duplicate header %s in field %d",
324 ca[res].ca_name, fldno + 1);
326 fldca[fldno] = &ca[res];
335 struct castr *ca = ef_cadef(cur_type);
337 if (ca[0].ca_table != cur_type)
338 return gripe("Table %s doesn't support ...", ef_nameof(cur_type));
339 ellipsis = is_partial = 1;
346 struct castr *ca = ef_cadef(cur_type);
347 int i, len, cafldsmax, res = 0;
350 /* Need a join field, use 0-th selector */
352 return gripe("Header field %s required with ...", ca[0].ca_name);
358 for (i = 0; ca[i].ca_name; i++) {
359 cafldsmax = MAX(caflds[i], cafldspp[i]);
360 if (ca[i].ca_flags & NSC_EXTRA)
362 len = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
363 if (!len && !cafldsmax)
364 res = gripe("Header field %s missing", ca[i].ca_name);
365 else if (len && cafldsmax == len - 1)
366 res = gripe("Header field %s(%d) missing",
367 ca[i].ca_name, len - 1);
368 else if (len && cafldsmax < len - 1)
369 res = gripe("Header fields %s(%d) ... %s(%d) missing",
370 ca[i].ca_name, cafldsmax, ca[i].ca_name, len - 1);
376 static struct castr *
377 getfld(int fldno, int *idx)
379 if (fldno >= nflds) {
380 gripe("Too many fields, expected only %d", nflds);
383 if (CANT_HAPPEN(fldno < 0))
386 *idx = fldidx[fldno];
391 fldval_must_match(int fldno)
393 struct castr *ca = ef_cadef(cur_type);
394 int i = fldca[fldno] - ca;
397 * Value must match if:
398 * it's for a const selector, unless the object is still blank, or
399 * it was already given in a previous part of a split table.
401 return (!cur_obj_is_blank && (fldca[fldno]->ca_flags & NSC_CONST))
402 || fldidx[fldno] < cafldspp[i];
408 struct empfile *ep = &empfile[cur_type];
409 int need_sentinel = !EF_IS_GAME_STATE(cur_type);
412 cur_obj_is_blank = cur_id >= ep->fids;
413 if (cur_obj_is_blank) {
414 /* TODO grow cache (and posssibly file) unless EFF_STATIC */
415 if (cur_id < ep->csize - !!need_sentinel)
416 ep->cids = ep->fids = cur_id + 1;
417 /* else: ef_ptr() will fail */
419 cur_obj = ef_ptr(cur_type, cur_id);
421 gripe("Can't put ID %d into table %s, it holds only 0..%d.",
422 cur_id, ep->name, ep->fids - 1);
429 setnum(int fldno, double dbl)
436 ca = getfld(fldno, &idx);
441 * If this is the record index, put it into cur_id.
443 if (fldno == 0 && ca->ca_table == cur_type)
449 memb_ptr += ca->ca_off;
451 /* FIXME check assignment preserves value */
452 switch (ca->ca_type) {
454 old = ((signed char *)memb_ptr)[idx];
455 ((signed char *)memb_ptr)[idx] = (signed char)dbl;
459 old = ((unsigned char *)memb_ptr)[idx];
460 ((unsigned char *)memb_ptr)[idx] = (unsigned char)dbl;
463 old = ((short *)memb_ptr)[idx];
464 ((short *)memb_ptr)[idx] = (short)dbl;
467 old = ((unsigned short *)memb_ptr)[idx];
468 ((unsigned short *)memb_ptr)[idx] = (unsigned short)dbl;
471 old = ((int *)memb_ptr)[idx];
472 ((int *)memb_ptr)[idx] = (int)dbl;
475 old = ((long *)memb_ptr)[idx];
476 ((long *)memb_ptr)[idx] = (long)dbl;
479 old = ((coord *)memb_ptr)[idx];
480 /* FIXME use variant of xrel() that takes orig instead of nation */
481 if (old >= WORLD_X / 2)
483 ((coord *)memb_ptr)[idx] = XNORM((coord)dbl);
486 old = ((coord *)memb_ptr)[idx];
487 /* FIXME use variant of yrel() that takes orig instead of nation */
488 if (old >= WORLD_Y / 2)
490 ((coord *)memb_ptr)[idx] = YNORM((coord)dbl);
493 old = ((float *)memb_ptr)[idx];
494 ((float *)memb_ptr)[idx] = (float)dbl;
497 old = ((double *)memb_ptr)[idx];
498 ((double *)memb_ptr)[idx] = dbl;
501 old = ((time_t *)memb_ptr)[idx];
502 ((time_t *)memb_ptr)[idx] = (time_t)dbl;
505 return gripe("Field %d doesn't take numbers", fldno + 1);
508 if (fldval_must_match(fldno) && old != dbl)
509 return gripe("Value for field %d must be %g", fldno + 1, old);
515 setstr(int fldno, char *str)
520 char *memb_ptr, *old;
522 ca = getfld(fldno, &idx);
529 memb_ptr += ca->ca_off;
530 must_match = fldval_must_match(fldno);
532 switch (ca->ca_type) {
534 old = ((char **)memb_ptr)[idx];
536 ((char **)memb_ptr)[idx] = str ? strdup(str) : NULL;
537 len = 65535; /* really SIZE_MAX, but that's C99 */
540 if (CANT_HAPPEN(idx))
543 return gripe("Field %d doesn't take nil", fldno + 1);
545 if (strlen(str) > len)
546 return gripe("Field %d takes at most %d characters",
547 fldno + 1, (int)len);
550 strncpy(memb_ptr, str, len);
553 return gripe("Field %d doesn't take strings", fldno + 1);
557 if (old && (!str || strncmp(old, str, len)))
558 return gripe("Value for field %d must be \"%.*s\"",
559 fldno + 1, (int)len, old);
561 return gripe("Value for field %d must be nil", fldno + 1);
568 xunsymbol(char *id, struct castr *ca, int n)
570 int i = ef_elt_byname(ca->ca_table, id);
572 return gripe("%s %s symbol `%s' in field %d",
573 i == M_NOTUNIQUE ? "Ambiguous" : "Unknown",
574 ca->ca_name, id, n + 1);
579 symval(struct castr *ca, int i)
581 int type = ca->ca_table;
583 if (ef_check(type) < 0)
585 if (ef_cadef(type) == symbol_ca)
586 /* symbol table, value is in the table */
587 return ((struct symbol *)ef_ptr(type, i))->value;
588 /* value is the table index */
593 setsym(int fldno, char *sym)
598 ca = getfld(fldno, NULL);
602 if (ca->ca_table == EF_BAD || (ca->ca_flags & NSC_BITS))
603 return gripe("Field %d doesn't take symbols", fldno + 1);
605 i = xunsymbol(sym, ca, fldno);
608 return setnum(fldno, symval(ca, i));
612 mtsymset(int fldno, long *set)
616 ca = getfld(fldno, NULL);
620 if (ca->ca_table == EF_BAD || ef_cadef(ca->ca_table) != symbol_ca
621 || !(ca->ca_flags & NSC_BITS))
622 return gripe("Field %d doesn't take symbol sets", fldno + 1);
628 add2symset(int fldno, long *set, char *sym)
633 ca = getfld(fldno, NULL);
637 i = xunsymbol(sym, ca, fldno);
640 *set |= symval(ca, i);
645 xuheader(FILE *fp, int expected_table)
651 while ((ch = skipfs(fp)) == '\n')
653 if (ch == EOF && expected_table == EF_BAD)
660 ? fscanf(fp, "config%*[ \t]%63[^ \t#\n]%n", name, &res) != 1
661 : fscanf(fp, "XDUMP%*[ \t]%63[^ \t#\n]%*[ \t]%*[^ \t#\n]%n",
662 name, &res) != 1) || res < 0)
663 return gripe("Expected xdump header");
665 type = ef_byname(name);
667 return gripe("Unknown table `%s'", name);
668 if (expected_table != EF_BAD && expected_table != type)
669 return gripe("Expected table `%s', not `%s'",
670 ef_nameof(expected_table), name);
672 if (!ef_cadef(type) || !(ef_flags(type) & EFF_MEM)) {
673 CANT_HAPPEN(expected_table != EF_BAD);
674 return gripe("Table `%s' is not permitted here", name);
677 if (skipfs(fp) != '\n')
678 return gripe("Junk after xdump header");
685 xufldhdr(FILE *fp, struct castr ca[])
691 for (i = 0; ca[i].ca_name; i++)
696 while ((ch = skipfs(fp)) == '\n')
699 nflds = xuflds(fp, xufldname);
702 nflds -= ellipsis != 0;
707 for (i = 0; ca[i].ca_name; i++) {
708 if ((ca[i].ca_flags & NSC_EXTRA))
710 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
718 nflds = fidx - fldidx;
725 xufooter(FILE *fp, struct castr ca[], int row)
731 if (fscanf(fp, "config%n", &res) != 0 || res < 0)
732 return gripe("Malformed table footer");
734 if (fscanf(fp, "%d", &rows) != 1)
735 return gripe("Malformed table footer");
737 return gripe("Read %d rows, which doesn't match footer "
738 "%d rows", row, rows);
740 if (skipfs(fp) != '\n')
741 return gripe("Junk after table footer");
744 for (i = 0; ca[i].ca_name; i++) {
745 if (cafldspp[i] < caflds[i])
746 cafldspp[i] = caflds[i];
753 xundump(FILE *fp, char *file, int *plno, int expected_table)
756 int type, nca, nf, i, ch;
761 if ((type = xuheader(fp, expected_table)) < 0)
765 if (CANT_HAPPEN(!ca))
769 for (i = 0; ca[i].ca_name; i++) {
771 if (!(ca[i].ca_flags & NSC_EXTRA))
772 nf += MAX(1, ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0);
774 fldca = calloc(nf, sizeof(*fldca));
775 fldidx = calloc(nf, sizeof(*fldidx));
776 caflds = malloc(nca * sizeof(*caflds));
777 cafldspp = calloc(nca, sizeof(*cafldspp));
780 if (xutail(fp, ca) < 0)
788 /* Skip empty lines so that callers can easily check for EOF */
789 while ((ch = skipfs(fp)) == '\n')
798 xutail(FILE *fp, struct castr *ca)
804 if (xufldhdr(fp, ca) < 0)
806 if ((recs = xubody(fp)) < 0)
808 if (xufooter(fp, ca, recs) < 0)
812 if (xuheader(fp, cur_type) < 0)
820 struct empfile *ep = &empfile[cur_type];
821 int need_sentinel = !EF_IS_GAME_STATE(cur_type);
825 for (row = 0;; ++row) {
826 while ((ch = skipfs(fp)) == '\n')
833 if (xuflds(fp, xufld) < 0)
835 n = MAX(n, cur_id + 1);
838 if (CANT_HAPPEN(n > ep->fids))
841 if (EF_IS_GAME_STATE(cur_type) && n != ep->csize)
842 /* TODO truncate file */
843 gripe("Warning: should resize table %s from %d to %d, not implemented",
844 ef_nameof(cur_type), ep->csize, n);
845 else if (cur_type >= EF_SHIP_CHR && cur_type <= EF_NUKE_CHR)
846 ep->cids = ep->fids = n;
848 return gripe("Table %s requires %d rows, got %d",
849 ef_nameof(cur_type), ep->fids, n);
853 if (CANT_HAPPEN(n >= ep->csize))
854 return gripe("No space for sentinel");
855 memset(ep->cache + ep->size * n, 0, ep->size);