]> git.pond.sub.org Git - empserver/blobdiff - src/lib/common/xundump.c
Update copyright notice
[empserver] / src / lib / common / xundump.c
index 56241172218dd20c5cf5f40f618b4357d78ecd27..82e08b9b43e8dd02e9f7097f410881d1d7f9841d 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2014, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2020, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                Ken Stevens, Steve McClure, Markus Armbruster
  *
  *  Empire is free software: you can redistribute it and/or modify
@@ -28,7 +28,7 @@
  *
  *  Known contributors to this file:
  *     Ron Koenderink, 2005
- *     Markus Armbruster, 2005-2014
+ *     Markus Armbruster, 2005-2016
  */
 
 /*
@@ -58,8 +58,8 @@
 #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 "xdump.h"
@@ -69,7 +69,6 @@ static int lineno;            /* Current line number */
 
 static int cur_type;           /* Current table's file type */
 static int partno;             /* Counts from 0..#parts-1 */
-static void *cur_obj;          /* The object being read into */
 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 */
@@ -96,7 +95,7 @@ static int xubody(FILE *);
 static int xutail(FILE *, struct castr *);
 
 /*
- * Does the code hardcode indexes for table TYPE?
+ * Does the code hardcode indexes for table @type?
  */
 static int
 have_hardcoded_indexes(int type)
@@ -106,7 +105,7 @@ have_hardcoded_indexes(int type)
 }
 
 /*
- * Okay to truncate table TYPE?
+ * Okay to truncate table @type?
  */
 static int
 may_truncate(int type)
@@ -115,23 +114,45 @@ may_truncate(int type)
 }
 
 /*
- * Is TYPE's 0-th selector a usable ID?
+ * 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);
+    return ca[0].ca_table == type && ca[0].ca_dump <= CA_DUMP_CONST;
 }
 
 /*
- * Can we fill in gaps in table TYPE?
+ * Can we fill in gaps in table @type?
  */
 static int
 can_fill_gaps(int type)
 {
-    return ca0_is_id(type) && !have_hardcoded_indexes(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);
 }
 
 /*
@@ -151,20 +172,19 @@ gripe(char *fmt, ...)
     return -1;
 }
 
-/* Make TYPE the current table.  */
+/* Make TYPE the current table. */
 static void
 tbl_start(int type)
 {
     cur_type = type;
     partno = 0;
     cur_id = -1;
-    cur_obj = NULL;
     old_nelem = type == EF_BAD ? 0 : ef_nelem(type);
     idgap = NULL;
     idgap_len = 0;
 }
 
-/* End the current table.  */
+/* End the current table. */
 static void
 tbl_end(void)
 {
@@ -173,30 +193,32 @@ tbl_end(void)
 }
 
 /*
- * Seek to current table's ID-th record.
- * ID must be acceptable.
- * Store it in cur_obj, and set cur_id accordingly.
- * Return 0 on success, -1 on failure.
+ * 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 int
+static void *
 tbl_seek(int id)
 {
-    struct empfile *ep = &empfile[cur_type];
+    void *obj;
 
     if (id >= ef_nelem(cur_type)) {
-       if (!ef_ensure_space(cur_type, id, 1))
-           return gripe("Can't put ID %d into table %s", id, ep->name);
+       if (!ef_ensure_space(cur_type, id, 1)) {
+           gripe("can't grow table to hold this row");
+           return NULL;
+       }
     }
 
-    cur_obj = ef_ptr(cur_type, id);
-    if (CANT_HAPPEN(!cur_obj))
-       return -1;
+    obj = ef_ptr(cur_type, id);
+    if (CANT_HAPPEN(!obj))
+       return NULL;
     cur_id = id;
-    return 0;
+    return obj;
 }
 
 /*
- * Omit ID1..ID2-1.
+ * Omit @id1..@id2-1.
  * Reset the omitted objects to default state.
  */
 static void
@@ -218,7 +240,7 @@ omit_ids(int id1, int id2)
 }
 
 /*
- * Return the smallest non-omitted ID in ID1..ID2-1 if any, else -1.
+ * Return the smallest non-omitted ID in @id1..@id2-1 if any, else -1.
  */
 static int
 expected_id(int id1, int id2)
@@ -251,32 +273,31 @@ tbl_part_done(void)
                    return -1;
            } else {
                if (!can_fill_gaps(cur_type))
-                   return gripe("Expected %d more rows",
+                   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");
+               return gripe("table's first part has more rows");
        }
     }
 
     partno++;
     cur_id = -1;
-    cur_obj = NULL;
     return 0;
 }
 
 /*
- * Get selector for field FLDNO.
- * Assign the field's selector index to *IDX, unless it is null.
+ * 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);
+       gripe("too many fields, expected only %d", nflds);
        return NULL;
     }
     if (CANT_HAPPEN(fldno < 0))
@@ -287,7 +308,7 @@ getfld(int fldno, int *idx)
 }
 
 /*
- * Find the field for selector CA with index IDX.
+ * Find the field for selector @ca with index @idx.
  * Return the field number if it exists, else -1.
  */
 static int
@@ -331,50 +352,147 @@ rowid(void)
        return cur_id + 1;
 
     if (id != cur_id + 1 && !can_fill_gaps(cur_type))
-       return gripe("Expected %d in field %d",
+       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);
+       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 gripe("field %d must be <= %d", fldno + 1, max_id);
 
     return id;
 }
 
 /*
- * Get the current row's object.
- * Store it in cur_obj, and set cur_id accordingly.
- * Return 0 on success, -1 on failure.
+ * 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)
-           return gripe("Table's first part doesn't have this row");
+       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 -1;
+           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))
-       return gripe("Too many rows");
-    if (tbl_seek(id) < 0)
-       return -1;
+    if (id > ef_id_limit(cur_type)) {
+       gripe("too many rows");
+       return NULL;
+    }
 
-    if (!partno)
+    obj = tbl_seek(id);
+    if (obj && !partno)
        omit_ids(last_id + 1, id);
-    return 0;
+    return obj;
 }
 
 /*
- * Is a new value for field FLDNO required to match the old one?
+ * Is a new value for field @fldno required to match the old one?
  */
 static int
 fldval_must_match(int fldno)
@@ -387,12 +505,12 @@ fldval_must_match(int fldno)
      * 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))
+    return (cur_id < old_nelem && (fldca[fldno]->ca_dump == CA_DUMP_CONST))
        || fldidx[fldno] < cafldspp[i];
 }
 
 /*
- * Set OBJ's field FLDNO to DBL.
+ * Set @obj's field @fldno to @dbl.
  * Return 0 on success, -1 on error.
  */
 static int
@@ -463,19 +581,19 @@ putnum(void *obj, int fldno, double dbl)
        new = ((time_t *)memb_ptr)[idx] = (time_t)dbl;
        break;
     default:
-       return gripe("Field %d doesn't take numbers", fldno + 1);
+       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);
+       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 gripe("field %d can't hold this value", fldno + 1);
 
     return 0;
 }
 
 /*
- * Set obj's field FLDNO to STR.
+ * Set obj's field @fldno to @str.
  * Return 0 on success, -1 on error.
  */
 static int
@@ -505,12 +623,12 @@ putstr(void *obj, int fldno, char *str)
        if (CANT_HAPPEN(idx))
            return -1;
        if (!str)
-           return gripe("Field %d doesn't take nil", fldno + 1);
+           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",
+           return gripe("field %d takes at most %d characters",
                         fldno + 1, (int)len);
        old = memb_ptr;
        if (must_match)
@@ -519,15 +637,15 @@ putstr(void *obj, int fldno, char *str)
            strncpy(memb_ptr, str, sz);
        break;
     default:
-       return gripe("Field %d doesn't take strings", fldno + 1);
+       return gripe("field %d doesn't take strings", fldno + 1);
     }
 
     if (mismatch) {
        if (old)
-           return gripe("Value for field %d must be \"%.*s\"",
+           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 gripe("value for field %d must be nil", fldno + 1);
     }
 
     return 0;
@@ -541,17 +659,19 @@ static int
 putrow(void)
 {
     int i, ret = 0;
+    void *obj;
 
-    if (rowobj() < 0)
+    obj = rowobj();
+    if (!obj)
        return -1;
 
     for (i = 0; i < nflds; i++) {
        switch (fldval[i].val_type) {
        case NSC_DOUBLE:
-           ret |= putnum(cur_obj, i, fldval[i].val_as.dbl);
+           ret |= putnum(obj, i, fldval[i].val_as.dbl);
            break;
        case NSC_STRING:
-           ret |= putstr(cur_obj, i, fldval[i].val_as.str.base);
+           ret |= putstr(obj, i, fldval[i].val_as.str.base);
            free(fldval[i].val_as.str.base);
            break;
        default:
@@ -564,7 +684,7 @@ putrow(void)
 }
 
 /*
- * Read and ignore field separators from FP.
+ * Read and ignore field separators from @fp.
  * Return first character that is not a field separator.
  */
 static int
@@ -586,14 +706,15 @@ skipfs(FILE *fp)
 }
 
 /*
- * Decode escape sequences in BUF.
- * Return BUF on success, null pointer on failure.
+ * 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;
@@ -611,8 +732,8 @@ xuesc(char *buf)
 }
 
 /*
- * Read an identifier from FP into BUF.
- * BUF must have space for 1024 characters.
+ * 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
@@ -627,9 +748,9 @@ getid(FILE *fp, char *buf)
 }
 
 /*
- * 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
+ * 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.
  */
@@ -642,7 +763,7 @@ 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)
@@ -651,19 +772,19 @@ xufldname(FILE *fp, int i)
        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("Header fields expected");
+           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);
@@ -673,27 +794,27 @@ 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.
+ * 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
@@ -707,15 +828,15 @@ xufld(FILE *fp, int i)
     ch = skipfs(fp);
     switch (ch) {
     case EOF:
-       return gripe("Unexpected EOF");
+       return gripe("unexpected EOF");
     case '\n':
        CANT_HAPPEN(i > nflds);
        for (j = i; j < nflds; j++) {
            if (CA_IS_ARRAY(fldca[j]))
-               gripe("Field %s(%d) missing",
+               gripe("field '%s(%d)' missing",
                      fldca[j]->ca_name, fldidx[j]);
            else
-               gripe("Field %s missing", fldca[j]->ca_name);
+               gripe("field '%s' missing", fldca[j]->ca_name);
        }
        if (i != nflds || putrow() < 0)
            return -1;
@@ -726,7 +847,7 @@ xufld(FILE *fp, int i)
     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);
@@ -735,9 +856,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);
@@ -747,12 +868,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;
        }
@@ -760,7 +881,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
@@ -769,8 +890,8 @@ xufld(FILE *fp, int i)
 }
 
 /*
- * Read fields from FP.
- * Use PARSE() to read each field.
+ * Read fields from @fp.
+ * Use @parse() to read each field.
  * Return number of fields read on success, -1 on error.
  */
 static int
@@ -788,14 +909,14 @@ 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.
+ * 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.
  */
@@ -808,30 +929,28 @@ deffld(int fldno, char *name, int idx)
     res = stmtch(name, ca, offsetof(struct castr, ca_name),
                 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_flags & NSC_EXTRA) || CANT_HAPPEN(ca[res].ca_get))
-       return gripe("Extraneous header %s in field %d", name, fldno + 1);
+       return gripe("%s header '%s' in field %d",
+                    res == M_NOTUNIQUE ? "ambiguous" : "unknown",
+                    name, fldno + 1);
+    if (ca[res].ca_dump > CA_DUMP_CONST || 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])
-           return gripe("Duplicate header %s in field %d",
+           return gripe("duplicate header '%s' in field %d",
                         ca[res].ca_name, fldno + 1);
     }
     fldca[fldno] = &ca[res];
@@ -856,16 +975,16 @@ chkflds(void)
     /* 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)
+       if (ca[i].ca_dump > CA_DUMP_CONST)
            continue;
        len = CA_ARRAY_LEN(&ca[i]);
        if (!len && !cafldsmax)
-           res = gripe("Header field %s missing", ca[i].ca_name);
+           res = gripe("header '%s' missing", ca[i].ca_name);
        else if (len && cafldsmax == len - 1)
-           res = gripe("Header field %s(%d) missing",
+           res = gripe("header '%s(%d)' missing",
                        ca[i].ca_name, len - 1);
        else if (len && cafldsmax < len - 1)
-           res = gripe("Header fields %s(%d) ... %s(%d) missing",
+           res = gripe("header '%s(%d)' ... '%s(%d)' missing",
                        ca[i].ca_name, cafldsmax, ca[i].ca_name, len - 1);
     }
 
@@ -873,7 +992,7 @@ chkflds(void)
 }
 
 /*
- * Set value of field FLDNO in current row to DBL.
+ * Set value of field @fldno in current row to @dbl.
  * Return 1 on success, -1 on error.
  */
 static int
@@ -888,7 +1007,7 @@ setnum(int fldno, double dbl)
 }
 
 /*
- * Set value of field FLDNO in current row to STR.
+ * Set value of field @fldno in current row to @str.
  * Return 1 on success, -1 on error.
  */
 static int
@@ -905,8 +1024,8 @@ setstr(int fldno, char *str)
 }
 
 /*
- * Resolve symbol name ID in table referred to by CA.
- * Use field number N for error messages.
+ * 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
@@ -914,15 +1033,15 @@ 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.
+ * @ca is the table, and @i is the index in it.
  */
 static int
 symval(struct castr *ca, int i)
@@ -937,7 +1056,7 @@ symval(struct castr *ca, int i)
 }
 
 /*
- * Set value of field FLDNO in current object to value of symbol SYM.
+ * Set value of field @fldno in current object to value of symbol @sym.
  * Return 1 on success, -1 on error.
  */
 static int
@@ -951,7 +1070,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)
@@ -960,7 +1079,7 @@ setsym(int fldno, char *sym)
 }
 
 /*
- * Create an empty symbol set for field FLDNO in *SET.
+ * Create an empty symbol set for field @fldno in *@set.
  * Return 1 on success, -1 on error.
  */
 static int
@@ -974,14 +1093,14 @@ 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.
+ * 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
@@ -1002,8 +1121,8 @@ add2symset(int fldno, long *set, char *sym)
 }
 
 /*
- * Read an xdump table header line from FP.
- * Expect header for EXPECTED_TABLE, unless it is EF_BAD.
+ * 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.
@@ -1027,32 +1146,41 @@ xuheader(FILE *fp, int expected_table)
         ? 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");
+       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 (!empfile[type].file
-       || !ef_cadef(type) || !(ef_flags(type) & EFF_MEM)) {
+    if (!xundumpable(type)) {
        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;
 }
 
+/*
+ * Can table @type be xundumped?
+ */
+int
+xundumpable(int type)
+{
+    return empfile[type].file && ef_cadef(type)
+       && (ef_flags(type) & EFF_MEM);
+}
+
 /*
  * 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[].
+ * 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.
  */
@@ -1078,7 +1206,7 @@ xufldhdr(FILE *fp, struct castr ca[])
        fidx = fldidx;
 
        for (i = 0; ca[i].ca_name; i++) {
-           if ((ca[i].ca_flags & NSC_EXTRA))
+           if (ca[i].ca_dump > CA_DUMP_CONST)
                continue;
            n = CA_ARRAY_LEN(&ca[i]);
            j = 0;
@@ -1095,9 +1223,9 @@ xufldhdr(FILE *fp, struct castr ca[])
 }
 
 /*
- * Read xdump footer from FP.
- * CA[] contains the table's selectors.
- * The body had RECS records.
+ * 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.
  */
@@ -1109,16 +1237,15 @@ xufooter(FILE *fp, struct castr ca[], int recs)
     res = -1;
     if (human) {
        if (fscanf(fp, "config%n", &res) != 0 || res < 0)
-           return gripe("Malformed table footer");
+           return gripe("malformed table footer");
     } else {
        if (fscanf(fp, "%d", &n) != 1)
-           return gripe("Malformed table footer");
+           return gripe("malformed table footer");
        if (recs != n)
-           return gripe("Read %d rows, which doesn't match footer "
-                        "%d rows", 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++;
@@ -1132,12 +1259,12 @@ xufooter(FILE *fp, struct castr ca[], int recs)
 }
 
 /*
- * Read an xdump table from FP.
+ * Read an xdump table from @fp.
  * Both machine- and human-readable xdump syntax are recognized.
- * Expect table EXPECTED_TABLE, unless it is EF_BAD.
+ * 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.
+ * 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
@@ -1159,7 +1286,7 @@ xundump(FILE *fp, char *file, int *plno, int expected_table)
     nca = nf = 0;
     for (i = 0; ca[i].ca_name; i++) {
        nca++;
-       if (!(ca[i].ca_flags & NSC_EXTRA))
+       if (ca[i].ca_dump <= CA_DUMP_CONST)
            nf += MAX(1, CA_ARRAY_LEN(&ca[i]));
     }
     fldca = malloc(nf * sizeof(*fldca));
@@ -1189,8 +1316,8 @@ xundump(FILE *fp, char *file, int *plno, int expected_table)
 }
 
 /*
- * Read the remainder of an xdump after the table header line from FP.
- * CA[] contains the table's selectors.
+ * 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
@@ -1213,7 +1340,7 @@ xutail(FILE *fp, struct castr *ca)
 }
 
 /*
- * Read the body of an xdump table from FP.
+ * Read the body of an xdump table from @fp.
  * Return number of rows read on success, -1 on failure.
  */
 static int