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-2006
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;
71 static int cur_obj_is_blank;
73 static struct castr **fldca;
76 static unsigned char *caseen;
78 static int gripe(char *, ...) ATTRIBUTE((format (printf, 1, 2)));
79 static int deffld(int, char *, int);
80 static int defellipsis(int fldno);
81 static int chkflds(void);
82 static int setnum(int, double);
83 static int setstr(int, char *);
84 static int xunsymbol(char *, struct castr *, int);
85 static int setsym(int, char *);
86 static int mtsymset(int, long *);
87 static int add2symset(int, long *, char *);
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
143 if (sscanf(++src, "%3o%n", &octal_chr, &n) != 1 || n != 3)
145 *dst++ = (char)octal_chr;
155 xufldname(FILE *fp, int i)
163 return gripe("Unexpected EOF");
170 if (getc(fp) != '.' || getc(fp) != '.')
171 return gripe("Junk in header field %d", i + 1);
173 return gripe("... not allowed in field 1");
174 if (defellipsis(i) < 0)
177 if (ch != EOF && ch != '\n')
178 return gripe("Junk after ...");
183 if (getid(fp, buf) < 0)
184 return gripe("Junk in header field %d", i + 1);
188 return deffld(i, buf, -1);
192 if (isdigit(ch) || ch == '-' || ch == '+') {
193 if (fscanf(fp, "%d", &idx) != 1)
194 return gripe("Malformed number in index of header field %d",
197 return gripe("Index must not be negative in header field %d",
200 if (getid(fp, buf) < 0)
201 return gripe("Malformed index in header field %d", i + 1);
202 return gripe("Symbolic index in header field %d not yet implemented",
207 return gripe("Malformed index in header field %d", i + 1);
208 return deffld(i, buf, idx);
213 xufld(FILE *fp, int i)
223 return gripe("Unexpected EOF");
226 if (fldca[i]->ca_type != NSC_STRINGY && fldca[i]->ca_len)
227 return gripe("Field %s(%d) missing",
228 fldca[i]->ca_name, fldidx[i]);
229 return gripe("Field %s missing", fldca[i]->ca_name);
233 case '+': case '-': case '.':
234 case '0': case '1': case '2': case '3': case '4':
235 case '5': case '6': case '7': case '8': case '9':
237 if (fscanf(fp, "%lg", &dbl) != 1)
238 return gripe("Malformed number in field %d", i + 1);
239 return setnum(i, dbl);
246 if (fscanf(fp, "%1023[^\"\n]", buf) != 1 || getc(fp) != '"')
247 return gripe("Malformed string in field %d", i + 1);
249 return gripe("Invalid escape sequence in field %d",
252 return setstr(i, buf);
254 if (mtsymset(i, &set) < 0)
258 if (ch == EOF || ch == '\n')
259 return gripe("Unmatched '(' in field %d", i + 1);
263 if (getid(fp, buf) < 0)
264 return gripe("Junk in field %d", i + 1);
265 if (add2symset(i, &set, buf) < 0)
268 return setnum(i, set);
271 if (getid(fp, buf) < 0)
272 return gripe("Junk in field %d", i + 1);
273 if (!strcmp(buf, "nil"))
274 return setstr(i, NULL);
276 return setsym(i, buf);
281 xuflds(FILE *fp, int (*parse)(FILE *, int))
294 else if (ch != ' ' && ch != '\t')
295 return gripe("Bad field separator after field %d", i + 1);
300 deffld(int fldno, char *name, int idx)
302 struct castr *ca = ef_cadef(cur_type);
305 res = stmtch(name, ca, offsetof(struct castr, ca_name),
306 sizeof(struct castr));
308 return gripe("Header %s of field %d is %s", name, fldno + 1,
309 res == M_NOTUNIQUE ? "ambiguous" : "unknown");
310 if (ca[res].ca_type != NSC_STRINGY && ca[res].ca_len != 0) {
312 return gripe("Header %s requires an index in field %d",
313 ca[res].ca_name, fldno + 1);
314 if (idx >= ca[res].ca_len)
315 return gripe("Header %s(%d) index out of bounds in field %d",
316 ca[res].ca_name, idx, fldno + 1);
317 if (idx < caflds[res])
318 return gripe("Duplicate header %s(%d) in field %d",
319 ca[res].ca_name, idx, fldno + 1);
320 if (idx > caflds[res])
321 return gripe("Expected header %s(%d) in field %d",
322 ca[res].ca_name, caflds[res], fldno + 1);
325 return gripe("Header %s doesn't take an index in field %d",
326 ca[res].ca_name, fldno + 1);
328 if (caflds[res] && !caseen[res])
329 /* FIXME doesn't catch dupes within table part when caseen[res] */
330 return gripe("Duplicate header %s in field %d",
331 ca[res].ca_name, fldno + 1);
333 fldca[fldno] = &ca[res];
341 defellipsis(int fldno)
343 struct castr *ca = ef_cadef(cur_type);
345 if (ca[0].ca_table != cur_type)
346 return gripe("Table %s doesn't support ...", ef_nameof(cur_type));
355 struct castr *ca = ef_cadef(cur_type);
359 /* Require index field */
361 return gripe("Header field %s required with ...", ca[0].ca_name);
362 /* Want the index field again in continued table: */
367 for (i = 0; ca[i].ca_name; i++) {
368 if (ca[i].ca_flags & NSC_EXTRA)
370 len = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
371 if (!len && !caflds[i])
372 res = gripe("Header field %s missing", ca[i].ca_name);
373 else if (len && caflds[i] == len - 1)
374 res = gripe("Header field %s(%d) missing",
375 ca[i].ca_name, len - 1);
376 else if (len && caflds[i] < len - 1)
377 res = gripe("Header fields %s(%d) ... %s(%d) missing",
378 ca[i].ca_name, caflds[i], ca[i].ca_name, len - 1);
384 static struct castr *
385 getfld(int fldno, int *idx)
387 if (fldno >= nflds) {
388 gripe("Too many fields, expected only %d", nflds);
391 if (CANT_HAPPEN(fldno < 0))
394 *idx = fldidx[fldno];
399 fldval_must_match(int fldno)
401 struct castr *ca = ef_cadef(cur_type);
402 int i = fldca[fldno] - ca;
404 return (!cur_obj_is_blank && (fldca[fldno]->ca_flags & NSC_CONST))
409 getobj(struct castr *ca, int altid)
411 struct empfile *ep = &empfile[cur_type];
412 int need_sentinel = !EF_IS_GAME_STATE(cur_type);
415 if (ca->ca_table == cur_type)
417 cur_obj_is_blank = cur_id >= ep->fids;
418 if (cur_obj_is_blank) {
419 /* TODO grow cache (and posssibly file) unless EFF_STATIC */
420 if (cur_id < ep->csize - !!need_sentinel)
421 ep->cids = ep->fids = cur_id + 1;
422 /* else: ef_ptr() will fail */
424 cur_obj = ef_ptr(cur_type, cur_id);
426 gripe("Can't put ID %d into table %s, it holds only 0..%d.",
427 cur_id, ep->name, ep->fids - 1);
434 setnum(int fldno, double dbl)
441 ca = getfld(fldno, &idx);
445 memb_ptr = getobj(ca, (int)dbl);
448 memb_ptr += ca->ca_off;
450 /* FIXME check assignment preserves value */
451 switch (ca->ca_type) {
453 old = ((signed char *)memb_ptr)[idx];
454 ((signed char *)memb_ptr)[idx] = (signed char)dbl;
458 old = ((unsigned char *)memb_ptr)[idx];
459 ((unsigned char *)memb_ptr)[idx] = (unsigned char)dbl;
462 old = ((short *)memb_ptr)[idx];
463 ((short *)memb_ptr)[idx] = (short)dbl;
466 old = ((unsigned short *)memb_ptr)[idx];
467 ((unsigned short *)memb_ptr)[idx] = (unsigned short)dbl;
470 old = ((int *)memb_ptr)[idx];
471 ((int *)memb_ptr)[idx] = (int)dbl;
474 old = ((long *)memb_ptr)[idx];
475 ((long *)memb_ptr)[idx] = (long)dbl;
478 old = ((coord *)memb_ptr)[idx];
479 /* FIXME use variant of xrel() that takes orig instead of nation */
480 if (old >= WORLD_X / 2)
482 ((coord *)memb_ptr)[idx] = XNORM((coord)dbl);
485 old = ((coord *)memb_ptr)[idx];
486 /* FIXME use variant of yrel() that takes orig instead of nation */
487 if (old >= WORLD_Y / 2)
489 ((coord *)memb_ptr)[idx] = YNORM((coord)dbl);
492 old = ((float *)memb_ptr)[idx];
493 ((float *)memb_ptr)[idx] = (float)dbl;
496 old = ((double *)memb_ptr)[idx];
497 ((double *)memb_ptr)[idx] = dbl;
500 old = ((time_t *)memb_ptr)[idx];
501 ((time_t *)memb_ptr)[idx] = (time_t)dbl;
504 return gripe("Field %d doesn't take numbers", fldno + 1);
507 if (fldval_must_match(fldno) && old != dbl)
508 return gripe("Value for field %d must be %g", fldno + 1, old);
514 setstr(int fldno, char *str)
519 char *memb_ptr, *old;
521 ca = getfld(fldno, &idx);
525 memb_ptr = getobj(ca, cur_id);
528 memb_ptr += ca->ca_off;
529 must_match = fldval_must_match(fldno);
531 switch (ca->ca_type) {
533 old = ((char **)memb_ptr)[idx];
535 ((char **)memb_ptr)[idx] = str ? strdup(str) : NULL;
536 len = 65535; /* really SIZE_MAX, but that's C99 */
539 if (CANT_HAPPEN(idx))
542 return gripe("Field %d doesn't take nil", fldno + 1);
544 if (strlen(str) > len)
545 return gripe("Field %d takes at most %d characters",
549 strncpy(memb_ptr, str, len);
552 return gripe("Field %d doesn't take strings", fldno + 1);
556 if (old && (!str || strncmp(old, str, len)))
557 return gripe("Value for field %d must be \"%.*s\"",
558 fldno + 1, len, old);
560 return gripe("Value for field %d must be nil", fldno + 1);
567 xunsymbol(char *id, struct castr *ca, int n)
569 int i = ef_elt_byname(ca->ca_table, id);
571 return gripe("%s %s symbol `%s' in field %d",
572 i == M_NOTUNIQUE ? "Ambiguous" : "Unknown",
573 ca->ca_name, id, n + 1);
578 symval(struct castr *ca, int i)
580 int type = ca->ca_table;
582 if (ef_check(type) < 0)
584 if (ef_cadef(type) == symbol_ca)
585 /* symbol table, value is in the table */
586 return ((struct symbol *)ef_ptr(type, i))->value;
587 /* value is the table index */
592 setsym(int fldno, char *sym)
597 ca = getfld(fldno, NULL);
601 if (ca->ca_table == EF_BAD || (ca->ca_flags & NSC_BITS))
602 return gripe("Field %d doesn't take symbols", fldno + 1);
604 i = xunsymbol(sym, ca, fldno);
607 return setnum(fldno, symval(ca, i));
611 mtsymset(int fldno, long *set)
615 ca = getfld(fldno, NULL);
619 if (ca->ca_table == EF_BAD || ef_cadef(ca->ca_table) != symbol_ca
620 || !(ca->ca_flags & NSC_BITS))
621 return gripe("Field %d doesn't take symbol sets", fldno + 1);
627 add2symset(int fldno, long *set, char *sym)
632 ca = getfld(fldno, NULL);
636 i = xunsymbol(sym, ca, fldno);
639 *set |= symval(ca, i);
644 xuheader(FILE *fp, int expected_table)
650 while ((ch = skipfs(fp)) == '\n')
652 if (ch == EOF && expected_table == EF_BAD)
659 ? fscanf(fp, "config%*[ \t]%63[^ \t#\n]%n", name, &res) != 1
660 : fscanf(fp, "XDUMP%*[ \t]%63[^ \t#\n]%*[ \t]%*[^ \t#\n]%n",
661 name, &res) != 1) || res < 0)
662 return gripe("Expected xdump header");
664 type = ef_byname(name);
666 return gripe("Unknown table `%s'", name);
667 if (expected_table != EF_BAD && expected_table != type)
668 return gripe("Expected table `%s', not `%s'",
669 ef_nameof(expected_table), name);
671 if (!ef_cadef(type) || !(ef_flags(type) & EFF_MEM)) {
672 CANT_HAPPEN(expected_table != EF_BAD);
673 return gripe("Table `%s' is not permitted here", name);
676 if (skipfs(fp) != '\n')
677 return gripe("Junk after xdump header");
684 xuheader1(FILE *fp, int type, struct castr ca[])
691 while ((ch = skipfs(fp)) == '\n')
695 nflds = xuflds(fp, xufldname);
698 nflds -= ellipsis != 0;
703 for (i = 0; ca[i].ca_name; i++) {
704 if ((ca[i].ca_flags & NSC_EXTRA))
706 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
714 nflds = fidx - fldidx;
721 xutrailer(FILE *fp, int type, int row)
727 if (fscanf(fp, "config%n", &res) != 0 || res < 0)
728 return gripe("Malformed table footer");
730 if (fscanf(fp, "%d", &rows) != 1)
731 return gripe("Malformed table footer");
733 return gripe("Read %d rows, which doesn't match footer "
734 "%d rows", row, rows);
736 if (skipfs(fp) != '\n')
737 return gripe("Junk after table footer");
744 xundump(FILE *fp, char *file, int expected_table)
747 int type, nca, nf, i, ch;
754 if ((type = xuheader(fp, expected_table)) < 0)
758 if (CANT_HAPPEN(!ca))
762 for (i = 0; ca[i].ca_name; i++) {
764 if (!(ca[i].ca_flags & NSC_EXTRA))
765 nf += MAX(1, ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0);
767 fldca = calloc(nf, sizeof(*fldca));
768 fldidx = calloc(nf, sizeof(*fldidx));
769 caflds = calloc(nca, sizeof(*caflds));
770 caseen = calloc(nca, sizeof(*caseen));
773 if (xundump2(fp, type, ca) < 0)
781 /* Skip empty lines so that callers can easily check for EOF */
782 while ((ch = skipfs(fp)) == '\n')
790 xundump2(FILE *fp, int type, struct castr *ca)
796 if (xuheader1(fp, type, ca) < 0)
798 if (xundump1(fp, type, ca) < 0)
802 for (i = 0; ca[i].ca_name; i++)
803 caseen[i] = caflds[i] != 0;
804 if (xuheader(fp, type) < 0)
810 xundump1(FILE *fp, int type, struct castr *ca)
812 struct empfile *ep = &empfile[type];
813 int need_sentinel = !EF_IS_GAME_STATE(type);
817 for (row = 0;; ++row) {
818 while ((ch = skipfs(fp)) == '\n')
825 if (xuflds(fp, xufld) < 0)
827 n = MAX(n, cur_id + 1);
830 if (CANT_HAPPEN(n > ep->fids))
833 if (EF_IS_GAME_STATE(type) && n != ep->csize)
834 /* TODO truncate file */
835 gripe("Warning: should resize table %s from %d to %d, not implemented",
836 ef_nameof(type), ep->csize, n);
837 else if (type >= EF_SHIP_CHR && type <= EF_NUKE_CHR)
838 ep->cids = ep->fids = n;
840 return gripe("Table %s requires %d rows, got %d",
841 ef_nameof(type), ep->fids, n);
845 if (CANT_HAPPEN(n >= ep->csize))
846 return gripe("No space for sentinel");
847 memset(ep->cache + ep->size * n, 0, ep->size);
850 if (xutrailer(fp, type, row) < 0)