]> git.pond.sub.org Git - empserver/blobdiff - src/lib/common/xundump.c
Update copyright notice
[empserver] / src / lib / common / xundump.c
index ab408b4c4d5f6d8856bdf43b695db50db0befc16..34bf50aa7899e6d8007aec3ddfabdaa3ad3775d3 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
  *  ---
  *
  *  xundump.c: Load back xdump output
- * 
+ *
  *  Known contributors to this file:
  *     Ron Koenderink, 2005
- *     Markus Armbruster, 2005-2007
+ *     Markus Armbruster, 2005-2014
  */
 
 /*
+ * See doc/xdump!  And keep it up-to-date.
+ *
+ * Parsing of machine-readable xdump is not precise: it recognizes
+ * comments, accepts whitespace in place of single space, and accepts
+ * the full human-readable field syntax instead of its machine-
+ * readable subset.
+ *
  * FIXME:
  * - Normalize terminology: table/rows/columns or file/records/fields
  * - Loading tables with NSC_STRING elements more than once leaks memory
  * TODO:
- * - Check each partial table supplies the same rows
- * - Check EFF_CFG tables are dense
- * - Symbolic references to non-symbol tables
  * - Symbolic array indexes
+ * - Option to treat missing and unknown fields as warning, not error
  * TODO, but hardly worth the effort:
  * - Permit reordering of array elements
- * - Permit repetition of array elements in split tables
  */
 
 #include <config.h>
 
 #include <ctype.h>
+#include <limits.h>
 #include <stdarg.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <time.h>
 #include "file.h"
 #include "match.h"
+#include "nat.h"
 #include "nsc.h"
 #include "optlist.h"
-#include "prototypes.h"
-
-static char *fname;
-static int lineno;
-static int human;
-static int ellipsis, is_partial;
-static int cur_type, cur_id;
-static void *cur_obj;
-static int cur_obj_is_blank;
-static int nflds;
-static struct castr **fldca;
-static int *fldidx;
-static int *caflds;
-static unsigned char *caseen;
+#include "xdump.h"
+
+static char *fname;            /* Name of file being read */
+static int lineno;             /* Current line number */
+
+static int cur_type;           /* Current table's file type */
+static int partno;             /* Counts from 0..#parts-1 */
+static int cur_id;             /* and its index in the table */
+static int old_nelem;
+static unsigned char *idgap;   /* idgap && idgap[ID] iff part#0 lacks ID */
+static int idgap_len;          /* #elements in idgap[] */
+
+static int human;              /* Reading human-readable syntax? */
+static int ellipsis;           /* Header ended with ...? */
+static int nflds;              /* #fields in input records */
+static struct castr **fldca;   /* Map field number to selector */
+static int *fldidx;            /* Map field number to index */
+static struct valstr *fldval;  /* Map field number to value */
+static int *caflds;            /* Map selector number to #fields seen */
+static int *cafldspp;          /* ditto, in previous parts */
 
 static int gripe(char *, ...) ATTRIBUTE((format (printf, 1, 2)));
 static int deffld(int, char *, int);
-static int defellipsis(int fldno);
 static int chkflds(void);
 static int setnum(int, double);
 static int setstr(int, char *);
-static int xunsymbol(char *, struct castr *, int);
 static int setsym(int, char *);
 static int mtsymset(int, long *);
 static int add2symset(int, long *, char *);
-static int xundump1(FILE *);
-static int xundump2(FILE *, int, struct castr *);
+static int xubody(FILE *);
+static int xutail(FILE *, struct castr *);
+
+/*
+ * Does the code hardcode indexes for table @type?
+ */
+static int
+have_hardcoded_indexes(int type)
+{
+    return type == EF_ITEM || type == EF_SECTOR_CHR
+       || type == EF_INFRASTRUCTURE;
+}
+
+/*
+ * Okay to truncate table @type?
+ */
+static int
+may_truncate(int type)
+{
+    return empfile[type].nent < 0 && !have_hardcoded_indexes(type);
+}
+
+/*
+ * Is @type's 0-th selector a usable ID?
+ */
+static int
+ca0_is_id(int type)
+{
+    struct castr *ca = ef_cadef(type);
+
+    return ca[0].ca_table == type && !(ca[0].ca_flags & NSC_EXTRA);
+}
+
+/*
+ * Can we fill in gaps in table @type?
+ */
+static int
+can_fill_gaps(int type)
+{
+    return (ca0_is_id(type) || type == EF_SECTOR || type == EF_REALM)
+       && !have_hardcoded_indexes(type);
+}
+
+/*
+ * Is table @type's @id-th record @obj redundant for xundump()?
+ */
+int
+xundump_redundant(int type, int id, void *obj)
+{
+    char buf[EF_WITH_CADEF_MAX_ENTRY_SIZE];
+
+    if (!can_fill_gaps(type))
+       return 0;
+
+    if (may_truncate(type) && id == ef_nelem(type) - 1)
+       return 0;
+
+    ef_blank(type, id, buf);
+    if (ef_flags(type) & EFF_TYPED)
+       return ef_typedstr_eq((struct ef_typedstr *)buf,
+                             (struct ef_typedstr *)obj);
+    return !memcmp(obj, buf, empfile[type].size);
+}
 
+/*
+ * Gripe about the current line to stderr, return -1.
+ */
 static int
 gripe(char *fmt, ...)
 {
@@ -98,6 +173,521 @@ gripe(char *fmt, ...)
     return -1;
 }
 
+/* Make TYPE the current table. */
+static void
+tbl_start(int type)
+{
+    cur_type = type;
+    partno = 0;
+    cur_id = -1;
+    old_nelem = type == EF_BAD ? 0 : ef_nelem(type);
+    idgap = NULL;
+    idgap_len = 0;
+}
+
+/* End the current table. */
+static void
+tbl_end(void)
+{
+    free(idgap);
+    tbl_start(EF_BAD);
+}
+
+/*
+ * Seek to current table's @id-th object.
+ * Extend the table if necessary.
+ * Save @id in cur_id.
+ * Return the object on success, NULL on failure.
+ */
+static void *
+tbl_seek(int id)
+{
+    void *obj;
+
+    if (id >= ef_nelem(cur_type)) {
+       if (!ef_ensure_space(cur_type, id, 1)) {
+           gripe("can't grow table to hold this row");
+           return NULL;
+       }
+    }
+
+    obj = ef_ptr(cur_type, id);
+    if (CANT_HAPPEN(!obj))
+       return NULL;
+    cur_id = id;
+    return obj;
+}
+
+/*
+ * Omit @id1..@id2-1.
+ * Reset the omitted objects to default state.
+ */
+static void
+omit_ids(int id1, int id2)
+{
+    int i;
+
+    if (id1 >= id2)
+       return;
+
+    idgap = realloc(idgap, id2 * sizeof(*idgap));
+    for (i = idgap_len; i < id1; i++)
+       idgap[i] = 0;
+    for (i = id1; i < id2; i++) {
+       ef_blank(cur_type, i, ef_ptr(cur_type, i));
+       idgap[i] = 1;
+    }
+    idgap_len = id2;
+}
+
+/*
+ * Return the smallest non-omitted ID in @id1..@id2-1 if any, else -1.
+ */
+static int
+expected_id(int id1, int id2)
+{
+    int i;
+
+    for (i = id1; i < id2; i++) {
+       if (i >= idgap_len || !idgap[i])
+           return i;
+    }
+    return -1;
+}
+
+/*
+ * Finish table part.
+ * If the table has variable length, truncate it.
+ * Else ensure we're omitting the same objects as the previous parts.
+ * Reset any omitted objects to default state.
+ * Return 0 on success, -1 on failure.
+ */
+static int
+tbl_part_done(void)
+{
+    struct empfile *ep = &empfile[cur_type];
+
+    if (cur_id + 1 < ep->fids) {
+       if (partno == 0) {
+           if (may_truncate(cur_type)) {
+               if (!ef_truncate(cur_type, cur_id + 1))
+                   return -1;
+           } else {
+               if (!can_fill_gaps(cur_type))
+                   return gripe("expected %d more rows",
+                                ep->fids - (cur_id + 1));
+               omit_ids(cur_id + 1, ep->fids);
+           }
+       } else {
+           if (expected_id(cur_id + 1, ep->fids) >= 0)
+               return gripe("table's first part has more rows");
+       }
+    }
+
+    partno++;
+    cur_id = -1;
+    return 0;
+}
+
+/*
+ * Get selector for field @fldno.
+ * Assign the field's selector index to *@idx, unless it is null.
+ * Return the selector on success, null pointer on error.
+ */
+static struct castr *
+getfld(int fldno, int *idx)
+{
+    if (fldno >= nflds) {
+       gripe("too many fields, expected only %d", nflds);
+       return NULL;
+    }
+    if (CANT_HAPPEN(fldno < 0))
+       return NULL;
+    if (idx)
+       *idx = fldidx[fldno];
+    return fldca[fldno];
+}
+
+/*
+ * Find the field for selector @ca with index @idx.
+ * Return the field number if it exists, else -1.
+ */
+static int
+fld_find(struct castr *ca, int idx)
+{
+    int i;
+
+    for (i = 0; i < nflds; i++) {
+       if (fldca[i] == ca && fldidx[i] == idx)
+           return i;
+    }
+    return -1;
+}
+
+/*
+ * Get the current row's ID.
+ * Current table's 0-th selector must be a usable ID.
+ * Return ID on success, -1 on failure.
+ */
+static int
+rowid(void)
+{
+    struct castr *ca = ef_cadef(cur_type);
+    int fldno, id, max_id;
+
+    if (CANT_HAPPEN(partno != 0 || !ca0_is_id(cur_type)))
+       return -1;
+
+    fldno = fld_find(ca, 0);
+    if (fldno < 0)
+       return cur_id + 1;      /* ID not specified */
+    /*
+     * Field values not representable as int will be rejected by
+     * putnum() or putstr().  Leave the error reporting to them, and
+     * simply pick the next ID here.
+     */
+    if (fldval[fldno].val_type != NSC_DOUBLE)
+       return cur_id + 1;
+    id = fldval[fldno].val_as.dbl;
+    if (id != fldval[fldno].val_as.dbl)
+       return cur_id + 1;
+
+    if (id != cur_id + 1 && !can_fill_gaps(cur_type))
+       return gripe("expected %d in field %d",
+                    cur_id + 1, fldno + 1);
+    if (id <= cur_id)
+       return gripe("field %d must be > %d", fldno + 1, cur_id);
+    max_id = ef_id_limit(cur_type);
+    if (id > max_id)
+       return gripe("field %d must be <= %d", fldno + 1, max_id);
+
+    return id;
+}
+
+/*
+ * Find the field @name with index @idx and value representable as long.
+ * Return the field number if it exists, else -1.
+ */
+static int
+fld_find_long_by_name(char *name, int idx)
+{
+    int i;
+
+    for (i = 0; i < nflds; i++) {
+       if (!strcmp(fldca[i]->ca_name, name) && fldidx[i] == idx)
+           break;
+    }
+
+    if (i == nflds || fldval[i].val_type != NSC_DOUBLE
+       || (long)fldval[i].val_as.dbl != fldval[i].val_as.dbl)
+       return -1;
+    return i;
+}
+
+/*
+ * Get the current row's ID.
+ * Current table's type must be EF_SECTOR.
+ * Return ID on success, -1 on failure.
+ */
+static int
+rowid_sect(void)
+{
+    int fldno_x, fldno_y, id;
+    coord x, y;
+
+    if (CANT_HAPPEN(partno != 0 || cur_type != EF_SECTOR))
+       return -1;
+
+    fldno_x = fld_find_long_by_name("xloc", 0);
+    fldno_y = fld_find_long_by_name("yloc", 0);
+    if (fldno_x < 0 || fldno_y < 0)
+       return cur_id + 1;
+
+    id = sctoff((long)fldval[fldno_x].val_as.dbl,
+               (long)fldval[fldno_y].val_as.dbl);
+    /* Note: reporting values out of range left to putnum() */
+    if (id <= cur_id) {
+       sctoff2xy(&x, &y, cur_id);
+       return gripe("coordinates in fields %d,%d must be > %d,%d",
+                    fldno_x + 1, fldno_y + 1, x, y);
+    }
+    return id;
+}
+
+/*
+ * Get the current row's ID.
+ * Current table's type must be EF_REALM.
+ * Return ID on success, -1 on failure.
+ */
+static int
+rowid_realm(void)
+{
+    int fldno_cnum, fldno_realm, id;
+    long realm, cnum;
+
+    if (CANT_HAPPEN(partno != 0 || cur_type != EF_REALM))
+       return -1;
+
+    fldno_cnum = fld_find_long_by_name("cnum", 0);
+    fldno_realm = fld_find_long_by_name("realm", 0);
+    if (fldno_cnum < 0 || fldno_realm < 0)
+       return cur_id + 1;
+
+    realm = (long)fldval[fldno_realm].val_as.dbl;
+    cnum = (long)fldval[fldno_cnum].val_as.dbl;
+    if (cnum < 0 || cnum >= MAXNOC)
+       return gripe("field %d must be between 0 and %d",
+                    fldno_cnum, MAXNOC);
+    if (realm < 0 || realm >= MAXNOR)
+       return gripe("field %d must be between 0 and %d",
+                    fldno_realm, MAXNOR);
+    id = realm + cnum * MAXNOR;
+    if (id <= cur_id)
+       return gripe("fields %d,%d must be > (%d,%d)",
+                    fldno_cnum + 1, fldno_realm + 1,
+                    cur_id / MAXNOR, cur_id % MAXNOR);
+    return id;
+}
+/*
+ * Get the current row's object.
+ * Extend the table if necessary.
+ * Save ID in cur_id.
+ * Return the object on success, NULL on failure.
+ */
+static void *
+rowobj(void)
+{
+    int last_id = cur_id;
+    int id;
+    void *obj;
+
+    if (partno) {
+       id = expected_id(cur_id + 1, empfile[cur_type].fids);
+       if (id < 0) {
+           gripe("table's first part doesn't have this row");
+           return NULL;
+       }
+    } else if (ca0_is_id(cur_type)) {
+       id = rowid();
+       if (id < 0)
+           return NULL;
+    } else if (cur_type == EF_SECTOR) {
+       id = rowid_sect();
+       if (id < 0)
+           return NULL;
+    } else if (cur_type == EF_REALM) {
+       id = rowid_realm();
+       if (id < 0)
+           return NULL;
+    } else
+       id = last_id + 1;
+    if (id > ef_id_limit(cur_type)) {
+       gripe("too many rows");
+       return NULL;
+    }
+
+    obj = tbl_seek(id);
+    if (obj && !partno)
+       omit_ids(last_id + 1, id);
+    return obj;
+}
+
+/*
+ * Is a new value for field @fldno required to match the old one?
+ */
+static int
+fldval_must_match(int fldno)
+{
+    struct castr *ca = ef_cadef(cur_type);
+    int i = fldca[fldno] - ca;
+
+    /*
+     * Value must match if:
+     * it's for a const selector, unless the object is still blank, or
+     * it was already given in a previous part of a split table.
+     */
+    return (cur_id < old_nelem && (fldca[fldno]->ca_flags & NSC_CONST))
+       || fldidx[fldno] < cafldspp[i];
+}
+
+/*
+ * Set @obj's field @fldno to @dbl.
+ * Return 0 on success, -1 on error.
+ */
+static int
+putnum(void *obj, int fldno, double dbl)
+{
+    struct castr *ca = fldca[fldno];
+    int idx = fldidx[fldno];
+    char *memb_ptr;
+    double old, new;
+
+    memb_ptr = (char *)obj + ca->ca_off;
+
+    switch (ca->ca_type) {
+    case NSC_CHAR:
+       old = ((signed char *)memb_ptr)[idx];
+       new = ((signed char *)memb_ptr)[idx] = (signed char)dbl;
+       break;
+    case NSC_UCHAR:
+       old = ((unsigned char *)memb_ptr)[idx];
+       new = ((unsigned char *)memb_ptr)[idx] = (unsigned char)dbl;
+       break;
+    case NSC_SHORT:
+       old = ((short *)memb_ptr)[idx];
+       new = ((short *)memb_ptr)[idx] = (short)dbl;
+       break;
+    case NSC_USHORT:
+       old = ((unsigned short *)memb_ptr)[idx];
+       new = ((unsigned short *)memb_ptr)[idx] = (unsigned short)dbl;
+       break;
+    case NSC_INT:
+       old = ((int *)memb_ptr)[idx];
+       new = ((int *)memb_ptr)[idx] = (int)dbl;
+       break;
+    case NSC_LONG:
+       old = ((long *)memb_ptr)[idx];
+       new = ((long *)memb_ptr)[idx] = (long)dbl;
+       break;
+    case NSC_XCOORD:
+       old = ((coord *)memb_ptr)[idx];
+       /* FIXME use variant of xrel() that takes orig instead of nation */
+       if (old >= WORLD_X / 2)
+           old -= WORLD_X;
+       new = ((coord *)memb_ptr)[idx] = XNORM((coord)dbl);
+       if (new >= WORLD_X / 2)
+           new -= WORLD_X;
+       break;
+    case NSC_YCOORD:
+       old = ((coord *)memb_ptr)[idx];
+       /* FIXME use variant of yrel() that takes orig instead of nation */
+       if (old >= WORLD_Y / 2)
+           old -= WORLD_Y;
+       new = ((coord *)memb_ptr)[idx] = YNORM((coord)dbl);
+       if (new >= WORLD_Y / 2)
+           new -= WORLD_Y;
+       break;
+    case NSC_FLOAT:
+       old = ((float *)memb_ptr)[idx];
+       ((float *)memb_ptr)[idx] = (float)dbl;
+       new = dbl;              /* suppress new != dbl check */
+       break;
+    case NSC_DOUBLE:
+       old = ((double *)memb_ptr)[idx];
+       ((double *)memb_ptr)[idx] = dbl;
+       new = dbl;              /* suppress new != dbl check */
+       break;
+    case NSC_TIME:
+       old = ((time_t *)memb_ptr)[idx];
+       new = ((time_t *)memb_ptr)[idx] = (time_t)dbl;
+       break;
+    default:
+       return gripe("field %d doesn't take numbers", fldno + 1);
+    }
+
+    if (fldval_must_match(fldno) && old != dbl)
+       return gripe("value for field %d must be %g", fldno + 1, old);
+    if (new != dbl)
+       return gripe("field %d can't hold this value", fldno + 1);
+
+    return 0;
+}
+
+/*
+ * Set obj's field @fldno to @str.
+ * Return 0 on success, -1 on error.
+ */
+static int
+putstr(void *obj, int fldno, char *str)
+{
+    struct castr *ca = fldca[fldno];
+    int idx = fldidx[fldno];
+    int must_match, mismatch;
+    size_t sz, len;
+    char *memb_ptr, *old;
+
+    memb_ptr = (char *)obj + ca->ca_off;
+    must_match = fldval_must_match(fldno);
+    mismatch = 0;
+
+    switch (ca->ca_type) {
+    case NSC_STRING:
+       old = ((char **)memb_ptr)[idx];
+       if (must_match)
+           mismatch = old ? !str || strcmp(old, str) : !!str;
+       else
+           /* FIXME may leak old value */
+           ((char **)memb_ptr)[idx] = str ? strdup(str) : NULL;
+       len = -1;               /* unlimited */
+       break;
+    case NSC_STRINGY:
+       if (CANT_HAPPEN(idx))
+           return -1;
+       if (!str)
+           return gripe("field %d doesn't take nil", fldno + 1);
+       /* Wart: if ca_len <= 1, the terminating null may be omitted */
+       sz = ca->ca_len;
+       len = sz > 1 ? sz - 1 : sz;
+       if (strlen(str) > len)
+           return gripe("field %d takes at most %d characters",
+                        fldno + 1, (int)len);
+       old = memb_ptr;
+       if (must_match)
+           mismatch = !str || strncmp(old, str, len);
+       else
+           strncpy(memb_ptr, str, sz);
+       break;
+    default:
+       return gripe("field %d doesn't take strings", fldno + 1);
+    }
+
+    if (mismatch) {
+       if (old)
+           return gripe("value for field %d must be \"%.*s\"",
+                        fldno + 1, (int)len, old);
+       else
+           return gripe("value for field %d must be nil", fldno + 1);
+    }
+
+    return 0;
+}
+
+/*
+ * Save the current row's fields in its object.
+ * Return 0 on success, -1 on failure.
+ */
+static int
+putrow(void)
+{
+    int i, ret = 0;
+    void *obj;
+
+    obj = rowobj();
+    if (!obj)
+       return -1;
+
+    for (i = 0; i < nflds; i++) {
+       switch (fldval[i].val_type) {
+       case NSC_DOUBLE:
+           ret |= putnum(obj, i, fldval[i].val_as.dbl);
+           break;
+       case NSC_STRING:
+           ret |= putstr(obj, i, fldval[i].val_as.str.base);
+           free(fldval[i].val_as.str.base);
+           break;
+       default:
+           CANT_REACH();
+           ret = -1;
+       }
+    }
+
+    return ret;
+}
+
+/*
+ * Read and ignore field separators from @fp.
+ * Return first character that is not a field separator.
+ */
 static int
 skipfs(FILE *fp)
 {
@@ -116,21 +706,16 @@ skipfs(FILE *fp)
     return ch;
 }
 
-static int
-getid(FILE *fp, char *buf)
-{
-    int n;
-    if (fscanf(fp, "%1023[^#()<>=#\" \t\n]%n", buf, &n) != 1
-       || !isalpha(buf[0]))
-       return -1;
-    return n;
-}
-
+/*
+ * Decode escape sequences in @buf.
+ * Return @buf on success, null pointer on failure.
+ */
 static char *
 xuesc(char *buf)
 {
     char *src, *dst;
-    int octal_chr, n;
+    unsigned octal_chr;
+    int n;
 
     dst = buf;
     src = buf;
@@ -147,6 +732,29 @@ xuesc(char *buf)
     return buf;
 }
 
+/*
+ * Read an identifier from @fp into @buf.
+ * @buf must have space for 1024 characters.
+ * Return number of characters read on success, -1 on failure.
+ */
+static int
+getid(FILE *fp, char *buf)
+{
+    int n;
+    if (fscanf(fp, "%1023[^\"#()<>= \t\n]%n", buf, &n) != 1
+       || !isalpha(buf[0]))
+       return -1;
+    xuesc(buf);
+    return n;
+}
+
+/*
+ * Try to read a field name from @fp.
+ * @i is the field number, counting from zero.
+ * If a name is read, set fldca[@i] and fldidx[@i] for it, and update
+ * caflds[].
+ * Return 1 if a name or ... was read, 0 on end of line, -1 on error.
+ */
 static int
 xufldname(FILE *fp, int i)
 {
@@ -156,28 +764,28 @@ xufldname(FILE *fp, int i)
     ch = skipfs(fp);
     switch (ch) {
     case EOF:
-       return gripe("Unexpected EOF");
+       return gripe("unexpected EOF");
     case '\n':
+       nflds = i - (ellipsis != 0);
        if (chkflds() < 0)
            return -1;
        lineno++;
        return 0;
     case '.':
        if (getc(fp) != '.' || getc(fp) != '.')
-           return gripe("Junk in header field %d", i + 1);
+           return gripe("junk in header field %d", i + 1);
        if (i == 0)
-           return gripe("... not allowed in field 1");
-       if (defellipsis(i) < 0)
-           return -1;
+           return gripe("header fields expected");
+       ellipsis = 1;
        ch = skipfs(fp);
        if (ch != EOF && ch != '\n')
-           return gripe("Junk after ...");
+           return gripe("junk after ...");
        ungetc(ch, fp);
        return 1;
     default:
        ungetc(ch, fp);
        if (getid(fp, buf) < 0)
-           return gripe("Junk in header field %d", i + 1);
+           return gripe("junk in header field %d", i + 1);
        ch = getc(fp);
        if (ch != '(') {
            ungetc(ch, fp);
@@ -187,28 +795,33 @@ xufldname(FILE *fp, int i)
        ungetc(ch, fp);
        if (isdigit(ch) || ch == '-' || ch == '+') {
            if (fscanf(fp, "%d", &idx) != 1)
-               return gripe("Malformed number in index of header field %d",
+               return gripe("malformed number in index of header field %d",
                             i + 1);
            if (idx < 0)
-               return gripe("Index must not be negative in header field %d",
+               return gripe("index must not be negative in header field %d",
                             i + 1);
        } else {
            if (getid(fp, buf) < 0)
-               return gripe("Malformed index in header field %d", i + 1);
-           return gripe("Symbolic index in header field %d not yet implemented",
+               return gripe("malformed index in header field %d", i + 1);
+           return gripe("symbolic index in header field %d not yet implemented",
                         i + 1);
        }
        ch = getc(fp);
        if (ch != ')')
-           return gripe("Malformed index in header field %d", i + 1);
+           return gripe("malformed index in header field %d", i + 1);
        return deffld(i, buf, idx);
     }
 }
 
+/*
+ * Try to read a field value from @fp.
+ * @i is the field number, counting from zero.
+ * Return 1 if a value was read, 0 on end of line, -1 on error.
+ */
 static int
 xufld(FILE *fp, int i)
 {
-    int ch;
+    int ch, j;
     char buf[1024];
     double dbl;
     long set;
@@ -216,22 +829,26 @@ xufld(FILE *fp, int i)
     ch = skipfs(fp);
     switch (ch) {
     case EOF:
-       return gripe("Unexpected EOF");
+       return gripe("unexpected EOF");
     case '\n':
-       if (i != nflds) {
-           if (fldca[i]->ca_type != NSC_STRINGY && fldca[i]->ca_len)
-               return gripe("Field %s(%d) missing",
-                            fldca[i]->ca_name, fldidx[i]);
-           return gripe("Field %s missing", fldca[i]->ca_name);
+       CANT_HAPPEN(i > nflds);
+       for (j = i; j < nflds; j++) {
+           if (CA_IS_ARRAY(fldca[j]))
+               gripe("field '%s(%d)' missing",
+                     fldca[j]->ca_name, fldidx[j]);
+           else
+               gripe("field '%s' missing", fldca[j]->ca_name);
        }
+       if (i != nflds || putrow() < 0)
+           return -1;
        lineno++;
-       return 0;
+       return i < nflds ? -1 : 0;
     case '+': case '-': case '.':
     case '0': case '1': case '2': case '3': case '4':
     case '5': case '6': case '7': case '8': case '9':
        ungetc(ch, fp);
        if (fscanf(fp, "%lg", &dbl) != 1)
-           return gripe("Malformed number in field %d", i + 1);
+           return gripe("malformed number in field %d", i + 1);
        return setnum(i, dbl);
     case '"':
        ch = getc(fp);
@@ -240,9 +857,9 @@ xufld(FILE *fp, int i)
        else {
            ungetc(ch, fp);
            if (fscanf(fp, "%1023[^\"\n]", buf) != 1 || getc(fp) != '"')
-               return gripe("Malformed string in field %d", i + 1);
+               return gripe("malformed string in field %d", i + 1);
            if (!xuesc(buf))
-               return gripe("Invalid escape sequence in field %d",
+               return gripe("invalid escape sequence in field %d",
                             i + 1);
        }
        return setstr(i, buf);
@@ -252,12 +869,12 @@ xufld(FILE *fp, int i)
        for (;;) {
            ch = skipfs(fp);
            if (ch == EOF || ch == '\n')
-               return gripe("Unmatched '(' in field %d", i + 1);
+               return gripe("unmatched '(' in field %d", i + 1);
            if (ch == ')')
                break;
            ungetc(ch, fp);
            if (getid(fp, buf) < 0)
-               return gripe("Junk in field %d", i + 1);
+               return gripe("junk in field %d", i + 1);
            if (add2symset(i, &set, buf) < 0)
                return -1;
        }
@@ -265,7 +882,7 @@ xufld(FILE *fp, int i)
     default:
        ungetc(ch, fp);
        if (getid(fp, buf) < 0)
-           return gripe("Junk in field %d", i + 1);
+           return gripe("junk in field %d", i + 1);
        if (!strcmp(buf, "nil"))
            return setstr(i, NULL);
        else
@@ -273,6 +890,11 @@ xufld(FILE *fp, int i)
     }
 }
 
+/*
+ * Read fields from @fp.
+ * Use @parse() to read each field.
+ * Return number of fields read on success, -1 on error.
+ */
 static int
 xuflds(FILE *fp, int (*parse)(FILE *, int))
 {
@@ -288,10 +910,17 @@ xuflds(FILE *fp, int (*parse)(FILE *, int))
        if (ch == '\n')
            ungetc(ch, fp);
        else if (ch != ' ' && ch != '\t')
-           return gripe("Bad field separator after field %d", i + 1);
+           return gripe("bad field separator after field %d", i + 1);
     }
 }
 
+/*
+ * Define the @fldno-th field.
+ * If @idx is negative, define as selector @name, else as @name(@idx).
+ * Set fldca[@fldno] and fldidx[@fldno] accordingly.
+ * Update caflds[].
+ * Return 1 on success, -1 on error.
+ */
 static int
 deffld(int fldno, char *name, int idx)
 {
@@ -299,291 +928,138 @@ deffld(int fldno, char *name, int idx)
     int res;
 
     res = stmtch(name, ca, offsetof(struct castr, ca_name),
-                    sizeof(struct castr));
+                sizeof(struct castr));
     if (res < 0)
-       return gripe("Header %s of field %d is %s", name, fldno + 1,
-                    res == M_NOTUNIQUE ? "ambiguous" : "unknown");
-    if (ca[res].ca_type != NSC_STRINGY && ca[res].ca_len != 0) {
+       return gripe("%s header '%s' in field %d",
+                    res == M_NOTUNIQUE ? "ambiguous" : "unknown",
+                    name, fldno + 1);
+    if ((ca[res].ca_flags & NSC_EXTRA) || CANT_HAPPEN(ca[res].ca_get))
+       return gripe("extraneous header '%s' in field %d", name, fldno + 1);
+    if (CA_IS_ARRAY(&ca[res])) {
        if (idx < 0)
-           return gripe("Header %s requires an index in field %d",
+           return gripe("header '%s' requires an index in field %d",
                         ca[res].ca_name, fldno + 1);
+       if (idx != caflds[res] && idx < ca[res].ca_len)
+           return gripe("expected header '%s(%d)' in field %d",
+                        ca[res].ca_name, caflds[res], fldno + 1);
        if (idx >= ca[res].ca_len)
-           return gripe("Header %s(%d) index out of bounds in field %d",
-                        ca[res].ca_name, idx, fldno + 1);
-       if (idx < caflds[res])
-           return gripe("Duplicate header %s(%d) in field %d",
+           return gripe("unexpected header '%s(%d)' in field %d",
                         ca[res].ca_name, idx, fldno + 1);
-       if (idx > caflds[res])
-           return gripe("Expected header %s(%d) in field %d",
-                        ca[res].ca_name, caflds[res], fldno + 1);
     } else {
        if (idx >= 0)
-           return gripe("Header %s doesn't take an index in field %d",
+           return gripe("header '%s' doesn't take an index in field %d",
                         ca[res].ca_name, fldno + 1);
        idx = 0;
-       if (caflds[res] && !caseen[res])
-           /* FIXME doesn't catch dupes within table part when caseen[res] */
-           return gripe("Duplicate header %s in field %d",
+       if (caflds[res])
+           return gripe("duplicate header '%s' in field %d",
                         ca[res].ca_name, fldno + 1);
     }
     fldca[fldno] = &ca[res];
     fldidx[fldno] = idx;
-    if (!caseen[res])
-       caflds[res]++;
+    caflds[res]++;
     return 1;
 }
 
-static int
-defellipsis(int fldno)
-{
-    struct castr *ca = ef_cadef(cur_type);
-
-    if (ca[0].ca_table != cur_type)
-       return gripe("Table %s doesn't support ...", ef_nameof(cur_type));
-    ellipsis = fldno;
-    is_partial = 1;
-    return 0;
-}
-
+/*
+ * Check fields in xdump are sane.
+ * Return 0 on success, -1 on error.
+ */
 static int
 chkflds(void)
 {
     struct castr *ca = ef_cadef(cur_type);
-    int i, len, res = 0;
-
-    if (is_partial) {
-       /* Require index field */
-       if (!caflds[0])
-           return gripe("Header field %s required with ...", ca[0].ca_name);
-       /* Want the index field again in continued table: */
-       caflds[0] = 0;
-       return 0;
-    }
+    int i, len, cafldsmax, res = 0;
 
+    if (ellipsis)
+       return res;             /* table is split, another part expected */
+
+    /* Check for missing fields */
     for (i = 0; ca[i].ca_name; i++) {
+       cafldsmax = MAX(caflds[i], cafldspp[i]);
        if (ca[i].ca_flags & NSC_EXTRA)
            continue;
-       len = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
-       if (!len && !caflds[i])
-           res = gripe("Header field %s missing", ca[i].ca_name);
-       else if (len && caflds[i] == len - 1)
-           res = gripe("Header field %s(%d) missing",
+       len = CA_ARRAY_LEN(&ca[i]);
+       if (!len && !cafldsmax)
+           res = gripe("header '%s' missing", ca[i].ca_name);
+       else if (len && cafldsmax == len - 1)
+           res = gripe("header '%s(%d)' missing",
                        ca[i].ca_name, len - 1);
-       else if (len && caflds[i] < len - 1)
-           res = gripe("Header fields %s(%d) ... %s(%d) missing",
-                       ca[i].ca_name, caflds[i], ca[i].ca_name, len - 1);
+       else if (len && cafldsmax < len - 1)
+           res = gripe("header '%s(%d)' ... '%s(%d)' missing",
+                       ca[i].ca_name, cafldsmax, ca[i].ca_name, len - 1);
     }
 
     return res;
 }
 
-static struct castr *
-getfld(int fldno, int *idx)
-{
-    if (fldno >= nflds) {
-       gripe("Too many fields, expected only %d", nflds);
-       return NULL;
-    }
-    if (CANT_HAPPEN(fldno < 0))
-       return NULL;
-    if (idx)
-       *idx = fldidx[fldno];
-    return fldca[fldno];
-}
-
-static int
-fldval_must_match(int fldno)
-{
-    struct castr *ca = ef_cadef(cur_type);
-    int i = fldca[fldno] - ca;
-
-    return (!cur_obj_is_blank && (fldca[fldno]->ca_flags & NSC_CONST))
-       || caseen[i];
-}
-
-static void *
-getobj(struct castr *ca, int altid)
-{
-    struct empfile *ep = &empfile[cur_type];
-    int need_sentinel = !EF_IS_GAME_STATE(cur_type);
-
-    if (!cur_obj) {
-       if (ca->ca_table == cur_type)
-           cur_id = altid;
-       cur_obj_is_blank = cur_id >= ep->fids;
-       if (cur_obj_is_blank) {
-           /* TODO grow cache (and posssibly file) unless EFF_STATIC */
-           if (cur_id < ep->csize - !!need_sentinel)
-               ep->cids = ep->fids = cur_id + 1;
-           /* else: ef_ptr() will fail */
-       }
-       cur_obj = ef_ptr(cur_type, cur_id);
-       if (!cur_obj)
-           gripe("Can't put ID %d into table %s, it holds only 0..%d.",
-                 cur_id, ep->name, ep->fids - 1);
-    }
-
-    return cur_obj;
-}
-
+/*
+ * Set value of field @fldno in current row to @dbl.
+ * Return 1 on success, -1 on error.
+ */
 static int
 setnum(int fldno, double dbl)
 {
-    struct castr *ca;
-    int idx;
-    char *memb_ptr;
-    double old;
-
-    ca = getfld(fldno, &idx);
-    if (!ca)
-       return -1;
-
-    memb_ptr = getobj(ca, (int)dbl);
-    if (!memb_ptr)
+    if (!getfld(fldno, NULL))
        return -1;
-    memb_ptr += ca->ca_off;
-
-    /* FIXME check assignment preserves value */
-    switch (ca->ca_type) {
-    case NSC_CHAR:
-       old = ((signed char *)memb_ptr)[idx];
-       ((signed char *)memb_ptr)[idx] = (signed char)dbl;
-       break;
-    case NSC_UCHAR:
-    case NSC_HIDDEN:
-       old = ((unsigned char *)memb_ptr)[idx];
-       ((unsigned char *)memb_ptr)[idx] = (unsigned char)dbl;
-       break;
-    case NSC_SHORT:
-       old = ((short *)memb_ptr)[idx];
-       ((short *)memb_ptr)[idx] = (short)dbl;
-       break;
-    case NSC_USHORT:
-       old = ((unsigned short *)memb_ptr)[idx];
-       ((unsigned short *)memb_ptr)[idx] = (unsigned short)dbl;
-       break;
-    case NSC_INT:
-       old = ((int *)memb_ptr)[idx];
-       ((int *)memb_ptr)[idx] = (int)dbl;
-       break;
-    case NSC_LONG:
-       old = ((long *)memb_ptr)[idx];
-       ((long *)memb_ptr)[idx] = (long)dbl;
-       break;
-    case NSC_XCOORD:
-       old = ((coord *)memb_ptr)[idx];
-       /* FIXME use variant of xrel() that takes orig instead of nation */
-       if (old >= WORLD_X / 2)
-           old -= WORLD_X;
-       ((coord *)memb_ptr)[idx] = XNORM((coord)dbl);
-       break;
-    case NSC_YCOORD:
-       old = ((coord *)memb_ptr)[idx];
-       /* FIXME use variant of yrel() that takes orig instead of nation */
-       if (old >= WORLD_Y / 2)
-           old -= WORLD_Y;
-       ((coord *)memb_ptr)[idx] = YNORM((coord)dbl);
-       break;
-    case NSC_FLOAT:
-       old = ((float *)memb_ptr)[idx];
-       ((float *)memb_ptr)[idx] = (float)dbl;
-       break;
-    case NSC_DOUBLE:
-       old = ((double *)memb_ptr)[idx];
-       ((double *)memb_ptr)[idx] = dbl;
-       break;
-    case NSC_TIME:
-       old = ((time_t *)memb_ptr)[idx];
-       ((time_t *)memb_ptr)[idx] = (time_t)dbl;
-       break;
-    default:
-       return gripe("Field %d doesn't take numbers", fldno + 1);
-    }
-
-    if (fldval_must_match(fldno) && old != dbl)
-       return gripe("Value for field %d must be %g", fldno + 1, old);
-
+    fldval[fldno].val_cat = NSC_VAL;
+    fldval[fldno].val_type = NSC_DOUBLE;
+    fldval[fldno].val_as.dbl = dbl;
     return 1;
 }
 
+/*
+ * Set value of field @fldno in current row to @str.
+ * Return 1 on success, -1 on error.
+ */
 static int
 setstr(int fldno, char *str)
 {
-    struct castr *ca;
-    int must_match, idx;
-    size_t len;
-    char *memb_ptr, *old;
-
-    ca = getfld(fldno, &idx);
-    if (!ca)
-       return -1;
-
-    memb_ptr = getobj(ca, cur_id);
-    if (!memb_ptr)
+    if (!getfld(fldno, NULL))
        return -1;
-    memb_ptr += ca->ca_off;
-    must_match = fldval_must_match(fldno);
-
-    switch (ca->ca_type) {
-    case NSC_STRING:
-       old = ((char **)memb_ptr)[idx];
-       if (!must_match)
-           ((char **)memb_ptr)[idx] = str ? strdup(str) : NULL;
-       len = 65535;            /* really SIZE_MAX, but that's C99 */
-       break;
-    case NSC_STRINGY:
-       if (CANT_HAPPEN(idx))
-           return -1;
-       if (!str)
-           return gripe("Field %d doesn't take nil", fldno + 1);
-       len = ca->ca_len;
-       if (strlen(str) > len)
-           return gripe("Field %d takes at most %d characters",
-                        fldno + 1, (int)len);
-       old = memb_ptr;
-       if (!must_match)
-           strncpy(memb_ptr, str, len);
-       break;
-    default:
-       return gripe("Field %d doesn't take strings", fldno + 1);
-    }
-
-    if (must_match) {
-       if (old && (!str || strncmp(old, str, len)))
-           return gripe("Value for field %d must be \"%.*s\"",
-                        fldno + 1, (int)len, old);
-       if (!old && str)
-           return gripe("Value for field %d must be nil", fldno + 1);
-    }
-
+    fldval[fldno].val_cat = NSC_VAL;
+    fldval[fldno].val_type = NSC_STRING;
+    fldval[fldno].val_as.str.base = str ? strdup(str) : NULL;
+    fldval[fldno].val_as.str.maxsz = INT_MAX;
+                               /* really SIZE_MAX, but that's C99 */
     return 1;
 }
 
+/*
+ * Resolve symbol name @id in table referred to by @ca.
+ * Use field number @n for error messages.
+ * Return index in referred table on success, -1 on failure.
+ */
 static int
 xunsymbol(char *id, struct castr *ca, int n)
 {
     int i = ef_elt_byname(ca->ca_table, id);
     if (i < 0)
-       return gripe("%s %s symbol `%s' in field %d",
-                    i == M_NOTUNIQUE ? "Ambiguous" : "Unknown",
+       return gripe("%s %s symbol '%s' in field %d",
+                    i == M_NOTUNIQUE ? "ambiguous" : "unknown",
                     ca->ca_name, id, n + 1);
     return i;
 }
 
+/*
+ * Map symbol index to symbol value.
+ * @ca is the table, and @i is the index in it.
+ */
 static int
 symval(struct castr *ca, int i)
 {
     int type = ca->ca_table;
 
-    if (ef_check(type) < 0)
-       return -1;
-    if (ef_cadef(type) == symbol_ca)
+    if (type != EF_BAD && ef_cadef(type) == symbol_ca)
        /* symbol table, value is in the table */
        return ((struct symbol *)ef_ptr(type, i))->value;
     /* value is the table index */
     return i;
 }
 
+/*
+ * Set value of field @fldno in current object to value of symbol @sym.
+ * Return 1 on success, -1 on error.
+ */
 static int
 setsym(int fldno, char *sym)
 {
@@ -595,7 +1071,7 @@ setsym(int fldno, char *sym)
        return -1;
 
     if (ca->ca_table == EF_BAD || (ca->ca_flags & NSC_BITS))
-       return gripe("Field %d doesn't take symbols", fldno + 1);
+       return gripe("field %d doesn't take symbols", fldno + 1);
 
     i = xunsymbol(sym, ca, fldno);
     if (i < 0)
@@ -603,6 +1079,10 @@ setsym(int fldno, char *sym)
     return setnum(fldno, symval(ca, i));
 }
 
+/*
+ * Create an empty symbol set for field @fldno in *@set.
+ * Return 1 on success, -1 on error.
+ */
 static int
 mtsymset(int fldno, long *set)
 {
@@ -614,11 +1094,16 @@ mtsymset(int fldno, long *set)
 
     if (ca->ca_table == EF_BAD || ef_cadef(ca->ca_table) != symbol_ca
        || !(ca->ca_flags & NSC_BITS))
-       return gripe("Field %d doesn't take symbol sets", fldno + 1);
+       return gripe("field %d doesn't take symbol sets", fldno + 1);
     *set = 0;
     return 0;
 }
 
+/*
+ * Add a symbol to a symbol set for field @fldno in *@set.
+ * @sym is the name of the symbol to add.
+ * Return 1 on success, -1 on error.
+ */
 static int
 add2symset(int fldno, long *set, char *sym)
 {
@@ -636,6 +1121,13 @@ add2symset(int fldno, long *set, char *sym)
     return 0;
 }
 
+/*
+ * Read an xdump table header line from @fp.
+ * Expect header for @expected_table, unless it is EF_BAD.
+ * Recognize header for machine- and human-readable syntax, and set
+ * human accordingly.
+ * Return table type on success, -2 on EOF before header, -1 on failure.
+ */
 static int
 xuheader(FILE *fp, int expected_table)
 {
@@ -652,46 +1144,55 @@ xuheader(FILE *fp, int expected_table)
     human = ch == 'c';
     res = -1;
     if ((human
-         ? fscanf(fp, "config%*[ \t]%63[^ \t#\n]%n", name, &res) != 1
-         : fscanf(fp, "XDUMP%*[ \t]%63[^ \t#\n]%*[ \t]%*[^ \t#\n]%n",
-                  name, &res) != 1) || res < 0)
-       return gripe("Expected xdump header");
+        ? fscanf(fp, "config%*[ \t]%63[^ \t#\n]%n", name, &res) != 1
+        : fscanf(fp, "XDUMP%*[ \t]%63[^ \t#\n]%*[ \t]%*[^ \t#\n]%n",
+                 name, &res) != 1) || res < 0)
+       return gripe("expected xdump header");
 
     type = ef_byname(name);
     if (type < 0)
-       return gripe("Unknown table `%s'", name);
+       return gripe("unknown table '%s'", name);
     if (expected_table != EF_BAD && expected_table != type)
-       return gripe("Expected table `%s', not `%s'",
+       return gripe("expected table '%s', not '%s'",
                     ef_nameof(expected_table), name);
 
-    if (!ef_cadef(type) || !(ef_flags(type) & EFF_MEM)) {
+    if (!empfile[type].file
+       || !ef_cadef(type) || !(ef_flags(type) & EFF_MEM)) {
        CANT_HAPPEN(expected_table != EF_BAD);
-       return gripe("Table `%s' is not permitted here", name);
+       return gripe("table '%s' is not permitted here", name);
     }
 
     if (skipfs(fp) != '\n')
-       return gripe("Junk after xdump header");
+       return gripe("junk after xdump header");
     lineno++;
 
     return type;
 }
 
+/*
+ * Find fields in this xdump.
+ * If reading human-readable syntax, read a field header line from @fp.
+ * Else take fields from the table's selectors in @ca[].
+ * Set ellipsis, nflds, fldca[], fldidx[] and caflds[] accordingly.
+ * Return 0 on success, -1 on failure.
+ */
 static int
-xuheader1(FILE *fp, struct castr ca[])
+xufldhdr(FILE *fp, struct castr ca[])
 {
     struct castr **fca;
     int *fidx;
     int ch, i, j, n;
 
+    for (i = 0; ca[i].ca_name; i++)
+       caflds[i] = 0;
+    ellipsis = 0;
+
     if (human) {
        while ((ch = skipfs(fp)) == '\n')
            lineno++;
        ungetc(ch, fp);
-       ellipsis = 0;
-       nflds = xuflds(fp, xufldname);
-       if (nflds < 0)
+       if (xuflds(fp, xufldname) < 0)
            return -1;
-       nflds -= ellipsis != 0;
     } else {
        fca = fldca;
        fidx = fldidx;
@@ -699,7 +1200,7 @@ xuheader1(FILE *fp, struct castr ca[])
        for (i = 0; ca[i].ca_name; i++) {
            if ((ca[i].ca_flags & NSC_EXTRA))
                continue;
-           n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
+           n = CA_ARRAY_LEN(&ca[i]);
            j = 0;
            do {
                *fca++ = &ca[i];
@@ -713,29 +1214,51 @@ xuheader1(FILE *fp, struct castr ca[])
     return 0;
 }
 
+/*
+ * Read xdump footer from @fp.
+ * @ca[] contains the table's selectors.
+ * The body had @recs records.
+ * Update cafldspp[] from caflds[].
+ * Return 0 on success, -1 on failure.
+ */
 static int
-xutrailer(FILE *fp, int row)
+xufooter(FILE *fp, struct castr ca[], int recs)
 {
-    int rows, res;
+    int res, n, i;
 
     res = -1;
     if (human) {
-       if (fscanf(fp, "config%n",  &res) != 0 || res < 0)
-           return gripe("Malformed table footer");
+       if (fscanf(fp, "config%n", &res) != 0 || res < 0)
+           return gripe("malformed table footer");
     } else {
-       if (fscanf(fp, "%d", &rows) != 1)
-           return gripe("Malformed table footer");
-       if (row != rows)
-           return gripe("Read %d rows, which doesn't match footer "
-                        "%d rows", row, rows);
+       if (fscanf(fp, "%d", &n) != 1)
+           return gripe("malformed table footer");
+       if (recs != n)
+           return gripe("expected footer /%d", recs);
     }
     if (skipfs(fp) != '\n')
-       return gripe("Junk after table footer");
+       return gripe("junk after table footer");
+    if (tbl_part_done() < 0)
+       return -1;
     lineno++;
 
+    for (i = 0; ca[i].ca_name; i++) {
+       if (cafldspp[i] < caflds[i])
+           cafldspp[i] = caflds[i];
+    }
+
     return 0;
 }
 
+/*
+ * Read an xdump table from @fp.
+ * Both machine- and human-readable xdump syntax are recognized.
+ * Expect table @expected_table, unless it is EF_BAD.
+ * Report errors to stderr.
+ * Messages assume @fp starts in the file @file at line *@plno.
+ * Update *@plno to reflect lines read from @fp.
+ * Return table type on success, -2 on EOF before header, -1 on failure.
+ */
 int
 xundump(FILE *fp, char *file, int *plno, int expected_table)
 {
@@ -756,19 +1279,22 @@ xundump(FILE *fp, char *file, int *plno, int expected_table)
     for (i = 0; ca[i].ca_name; i++) {
        nca++;
        if (!(ca[i].ca_flags & NSC_EXTRA))
-           nf += MAX(1, ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0);
+           nf += MAX(1, CA_ARRAY_LEN(&ca[i]));
     }
-    fldca = calloc(nf, sizeof(*fldca));
-    fldidx = calloc(nf, sizeof(*fldidx));
-    caflds = calloc(nca, sizeof(*caflds));
-    caseen = calloc(nca, sizeof(*caseen));
-    cur_type = type;
-
-    if (xundump2(fp, type, ca) < 0)
+    fldca = malloc(nf * sizeof(*fldca));
+    fldidx = malloc(nf * sizeof(*fldidx));
+    fldval = malloc(nf * sizeof(*fldval));
+    caflds = malloc(nca * sizeof(*caflds));
+    cafldspp = calloc(nca, sizeof(*cafldspp));
+
+    tbl_start(type);
+    if (xutail(fp, ca) < 0)
        type = EF_BAD;
+    tbl_end();
 
-    free(caseen);
+    free(cafldspp);
     free(caflds);
+    free(fldval);
     free(fldidx);
     free(fldca);
 
@@ -781,69 +1307,47 @@ xundump(FILE *fp, char *file, int *plno, int expected_table)
     return type;
 }
 
+/*
+ * Read the remainder of an xdump after the table header line from @fp.
+ * @ca[] contains the table's selectors.
+ * Return 0 on success, -1 on failure.
+ */
 static int
-xundump2(FILE *fp, int type, struct castr *ca)
+xutail(FILE *fp, struct castr *ca)
 {
-    int i;
+    int recs;
 
-    is_partial = 0;
     for (;;) {
-       if (xuheader1(fp, ca) < 0)
+       if (xufldhdr(fp, ca) < 0)
+           return -1;
+       if ((recs = xubody(fp)) < 0)
            return -1;
-       if (xundump1(fp) < 0)
+       if (xufooter(fp, ca, recs) < 0)
            return -1;
        if (!ellipsis)
            return 0;
-       for (i = 0; ca[i].ca_name; i++)
-           caseen[i] = caflds[i] != 0;
-       if (xuheader(fp, type) < 0)
+       if (xuheader(fp, cur_type) < 0)
            return -1;
     }
 }
 
+/*
+ * Read the body of an xdump table from @fp.
+ * Return number of rows read on success, -1 on failure.
+ */
 static int
-xundump1(FILE *fp)
+xubody(FILE *fp)
 {
-    struct empfile *ep = &empfile[cur_type];
-    int need_sentinel = !EF_IS_GAME_STATE(cur_type);
-    int row, n, ch;
+    int i, ch;
 
-    n = 0;
-    for (row = 0;; ++row) {
+    for (i = 0;; ++i) {
        while ((ch = skipfs(fp)) == '\n')
            lineno++;
        if (ch == '/')
            break;
        ungetc(ch, fp);
-       cur_obj = NULL;
-       cur_id = row;
        if (xuflds(fp, xufld) < 0)
            return -1;
-       n = MAX(n, cur_id + 1);
-    }
-
-    if (CANT_HAPPEN(n > ep->fids))
-       n = ep->fids;
-    if (n < ep->fids) {
-       if (EF_IS_GAME_STATE(cur_type) && n != ep->csize)
-           /* TODO truncate file */
-           gripe("Warning: should resize table %s from %d to %d, not implemented",
-                 ef_nameof(cur_type), ep->csize, n);
-       else if (cur_type >= EF_SHIP_CHR && cur_type <= EF_NUKE_CHR)
-           ep->cids = ep->fids = n;
-       else
-           return gripe("Table %s requires %d rows, got %d",
-                        ef_nameof(cur_type), ep->fids, n);
     }
-
-    if (need_sentinel) {
-       if (CANT_HAPPEN(n >= ep->csize))
-           return gripe("No space for sentinel");
-       memset(ep->cache + ep->size * n, 0, ep->size);
-    }
-
-    if (xutrailer(fp, row) < 0)
-       return -1;
-
-    return 0;
+    return i;
 }