]> git.pond.sub.org Git - empserver/blobdiff - src/lib/common/xundump.c
Update copyright notice.
[empserver] / src / lib / common / xundump.c
index 8aa292b7c2c3e51374c1285600737193025fde19..44590591bf803942538710866b996417c9dbbaf1 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -19,9 +19,9 @@
  *
  *  ---
  *
- *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- *  related information and legal notices. It is expected that any future
- *  projects/authors will amend these files as needed.
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
  *
  *  ---
  *
  * 
  *  Known contributors to this file:
  *     Ron Koenderink, 2005
- *     Markus Armbruster, 2005
+ *     Markus Armbruster, 2005-2006
  */
 
-/* FIXME normalize terminology: table/rows/columns or file/records/fields */
+/*
+ * 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
+ * TODO, but hardly worth the effort:
+ * - Permit reordering of array elements
+ * - Permit repetition of array elements in split tables
+ */
 
-#include <stdio.h>
-#include <stdlib.h>
+#include <config.h>
 
 #include <ctype.h>
-#include <string.h>
 #include <stdarg.h>
+#include <stdio.h>
 #include <time.h>
-
-#include "prototypes.h"
 #include "file.h"
-#include "nsc.h"
 #include "match.h"
+#include "nsc.h"
+#include "optlist.h"
+#include "prototypes.h"
 
-#define MAX_NUM_COLUMNS 256
-
-static char *fname = "";
-static int lineno = 0;
-
-/*
- * TODO
- * This structure could be replaced with struct valstr.
- */
-enum enum_value {
-    VAL_NOTUSED,
-    VAL_STRING,
-    VAL_SYMBOL,
-    VAL_SYMBOL_SET,
-    VAL_DOUBLE
-};
-
-struct value {
-    enum enum_value v_type;
-    union {
-       char *v_string;
-       double v_double;
-    } v_field;
-};
-
-static int gripe(char *fmt, ...) ATTRIBUTE((format (printf, 1, 2)));
+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;
+
+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 *, int, struct castr *);
+static int xundump2(FILE *, int, struct castr *);
 
 static int
 gripe(char *fmt, ...)
@@ -110,7 +120,8 @@ static int
 getid(FILE *fp, char *buf)
 {
     int n;
-    if (fscanf(fp, "%1023[^#() \t\n]%n", buf, &n) != 1 || !isalpha(buf[0]))
+    if (fscanf(fp, "%1023[^#()<>=#\" \t\n]%n", buf, &n) != 1
+       || !isalpha(buf[0]))
        return -1;
     return n;
 }
@@ -137,84 +148,142 @@ xuesc(char *buf)
 }
 
 static int
-xuflds(FILE *fp, struct value values[])
+xufldname(FILE *fp, int i)
 {
-    int i, ch;
+    int ch, idx;
     char buf[1024];
-    char *p;
-    int len, l1;
-
-    for (i = 0; ; i++) {
-       values[i].v_type = VAL_NOTUSED;
-       if (i >= MAX_NUM_COLUMNS)
-           return gripe("Too many columns");
 
+    ch = skipfs(fp);
+    switch (ch) {
+    case EOF:
+       return gripe("Unexpected EOF");
+    case '\n':
+       if (chkflds() < 0)
+           return -1;
+       lineno++;
+       return 0;
+    case '.':
+       if (getc(fp) != '.' || getc(fp) != '.')
+           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;
        ch = skipfs(fp);
-       switch (ch) {
-       case EOF:
-           return gripe("Unexpected EOF");
-       case '\n':
-           return i;
-       case '+': case '-': case '.':
-       case '0': case '1': case '2': case '3': case '4':
-       case '5': case '6': case '7': case '8': case '9':
+       if (ch != EOF && ch != '\n')
+           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);
+       ch = getc(fp);
+       if (ch != '(') {
            ungetc(ch, fp);
-           if (fscanf(fp, "%lg", &values[i].v_field.v_double) != 1)
-               return gripe("Malformed number in field %d", i + 1);
-           values[i].v_type = VAL_DOUBLE;
-           break;
-       case '"':
-           ch = getc(fp);
-           if (ch == '"')
-               buf[0] = 0;
-           else {
-               ungetc(ch, fp);
-               if (fscanf(fp, "%1023[^\"\n]", buf) != 1 || getc(fp) != '"')
-                   return gripe("Malformed string in field %d", i + 1);
-               if (!xuesc(buf))
-                   return gripe("Invalid escape sequence in field %d",
-                                i + 1);
-           }
-           values[i].v_type = VAL_STRING;
-           values[i].v_field.v_string = strdup(buf);
-           break;
-       case '(':
-           p = strdup("");
-           len = 0;
-           for (;;) {
-               ch = skipfs(fp);
-               if (ch == EOF || ch == '\n')
-                   return gripe("Unmatched '(' in field %d", i + 1);
-               if (ch == ')')
-                   break;
-               ungetc(ch, fp);
-               l1 = getid(fp, buf);
-               if (l1 < 0)
-                   return gripe("Junk in field %d", i + 1);
-               p = realloc(p, len + l1 + 2);
-               strcpy(p + len, buf);
-               strcpy(p + len + l1, " ");
-               len += l1 + 1;
-           }
-           if (len)
-               p[len - 1] = 0;
-           values[i].v_type = VAL_SYMBOL_SET;
-           values[i].v_field.v_string = p;
-           break;
-       default:
+           return deffld(i, buf, -1);
+       }
+       ch = getc(fp);
+       ungetc(ch, fp);
+       if (isdigit(ch) || ch == '-' || ch == '+') {
+           if (fscanf(fp, "%d", &idx) != 1)
+               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",
+                            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",
+                        i + 1);
+       }
+       ch = getc(fp);
+       if (ch != ')')
+           return gripe("Malformed index in header field %d", i + 1);
+       return deffld(i, buf, idx);
+    }
+}
+
+static int
+xufld(FILE *fp, int i)
+{
+    int ch;
+    char buf[1024];
+    double dbl;
+    long set;
+
+    ch = skipfs(fp);
+    switch (ch) {
+    case 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);
+       }
+       lineno++;
+       return 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 setnum(i, dbl);
+    case '"':
+       ch = getc(fp);
+       if (ch == '"')
+           buf[0] = 0;
+       else {
+           ungetc(ch, fp);
+           if (fscanf(fp, "%1023[^\"\n]", buf) != 1 || getc(fp) != '"')
+               return gripe("Malformed string in field %d", i + 1);
+           if (!xuesc(buf))
+               return gripe("Invalid escape sequence in field %d",
+                            i + 1);
+       }
+       return setstr(i, buf);
+    case '(':
+       if (mtsymset(i, &set) < 0)
+           return -1;
+       for (;;) {
+           ch = skipfs(fp);
+           if (ch == EOF || ch == '\n')
+               return gripe("Unmatched '(' in field %d", i + 1);
+           if (ch == ')')
+               break;
            ungetc(ch, fp);
-           if (getid(fp, buf) < 0) {
+           if (getid(fp, buf) < 0)
                return gripe("Junk in field %d", i + 1);
-           }
-           if (!strcmp(buf, "nil")) {
-               values[i].v_type = VAL_STRING;
-               values[i].v_field.v_string = NULL;
-           }
-           else {
-               values[i].v_type = VAL_SYMBOL;
-               values[i].v_field.v_string = strdup(buf);
-           }
+           if (add2symset(i, &set, buf) < 0)
+               return -1;
        }
+       return setnum(i, set);
+    default:
+       ungetc(ch, fp);
+       if (getid(fp, buf) < 0)
+           return gripe("Junk in field %d", i + 1);
+       if (!strcmp(buf, "nil"))
+           return setstr(i, NULL);
+       else
+           return setsym(i, buf);
+    }
+}
+
+static int
+xuflds(FILE *fp, int (*parse)(FILE *, int))
+{
+    int i, ch, res;
+
+    for (i = 0; ; i++) {
+       res = parse(fp, i);
+       if (res < 0)
+           return -1;
+       if (res == 0)
+           return i;
        ch = getc(fp);
        if (ch == '\n')
            ungetc(ch, fp);
@@ -223,345 +292,559 @@ xuflds(FILE *fp, struct value values[])
     }
 }
 
-static void
-freeflds(struct value values[])
+static int
+deffld(int fldno, char *name, int idx)
+{
+    struct castr *ca = ef_cadef(cur_type);
+    int res;
+
+    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_type != NSC_STRINGY && ca[res].ca_len != 0) {
+       if (idx < 0)
+           return gripe("Header %s requires an index in field %d",
+                        ca[res].ca_name, 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",
+                        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",
+                        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",
+                        ca[res].ca_name, fldno + 1);
+    }
+    fldca[fldno] = &ca[res];
+    fldidx[fldno] = idx;
+    if (!caseen[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;
+}
+
+static int
+chkflds(void)
 {
-    struct value *vp;
+    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;
+    }
+
+    for (i = 0; ca[i].ca_name; 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",
+                       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);
+    }
+
+    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;
+}
+
+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)
+       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);
+
+    return 1;
+}
+
+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)
+       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);
+    }
 
-    for (vp = values; vp->v_type != VAL_NOTUSED; vp++) {
-       if (vp->v_type != VAL_DOUBLE)
-           free(vp->v_field.v_string);
+    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);
     }
+
+    return 1;
 }
 
 static int
-xunsymbol1(char *id, struct symbol *symtab, struct castr *ca, int n)
+xunsymbol(char *id, struct castr *ca, int n)
 {
-    int i = stmtch(id, symtab, offsetof(struct symbol, name),
-                  sizeof(struct symbol));
+    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",
-                    ca->ca_name, id, n);
+                    ca->ca_name, id, n + 1);
     return i;
 }
 
 static int
-xunsymbol(struct castr *ca, struct value *vp, int n)
+symval(struct castr *ca, int i)
 {
-    struct symbol *symtab = (struct symbol *)empfile[ca->ca_table].cache;
-    char *buf = vp->v_field.v_string;
-    int i;
-    int value;
-    char *token;
-
-    if (vp->v_type == VAL_SYMBOL_SET) {
-       if (!(ca->ca_flags & NSC_BITS) || ca->ca_table == EF_BAD)
-           return gripe("%s doesn't take a symbol set in field %d",
-                        ca->ca_name, n);
-       value = 0;
-       for (token = strtok(buf, " "); token; token = strtok(NULL, " ")) {
-           i = xunsymbol1(token, symtab, ca, n);
-           if (i < 0)
-               return -1;
-           value |= symtab[i].value;
-       }
-    } else if (vp->v_type == VAL_SYMBOL) {
-       if ((ca->ca_flags & NSC_BITS) || ca->ca_table == EF_BAD)
-           return gripe("%s doesn't take a symbol in field %d",
-                        ca->ca_name, n);
-       i = xunsymbol1(buf, symtab, ca, n);
-       if (i < 0)
-           return -1;
-       value = symtab[i].value;
-    } else
-       return 0;
+    int type = ca->ca_table;
 
-    vp->v_type = VAL_DOUBLE;
-    vp->v_field.v_double = value;
-    return 0;
+    if (ef_check(type) < 0)
+       return -1;
+    if (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;
 }
 
 static int
-has_const(struct castr ca[])
+setsym(int fldno, char *sym)
 {
+    struct castr *ca;
     int i;
 
-    for (i = 0; ca[i].ca_name; i++) {
-       if (ca[i].ca_flags & NSC_CONST)
-           return 1;
-    }
-    return 0;
+    ca = getfld(fldno, NULL);
+    if (!ca)
+       return -1;
+
+    if (ca->ca_table == EF_BAD || (ca->ca_flags & NSC_BITS))
+       return gripe("Field %d doesn't take symbols", fldno + 1);
+
+    i = xunsymbol(sym, ca, fldno);
+    if (i < 0)
+       return -1;
+    return setnum(fldno, symval(ca, i));
 }
 
-static void
-xuinitrow(int type, int row)
+static int
+mtsymset(int fldno, long *set)
 {
-    struct empfile *ep = &empfile[type];
-    char *ptr = ep->cache + ep->size * row;
+    struct castr *ca;
 
-    memset(ptr, 0, ep->size);
+    ca = getfld(fldno, NULL);
+    if (!ca)
+       return -1;
 
-    if (ep->init)
-       ep->init(row, ptr);
+    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);
+    *set = 0;
+    return 0;
 }
 
 static int
-xuloadrow(int type, int row, struct value values[])
+add2symset(int fldno, long *set, char *sym)
 {
-    int i,j,k;
-    struct empfile *ep = &empfile[type];
-    char *ptr = ep->cache + ep->size * row;
-    struct castr *ca = ep->cadef;
-    void *row_ref;
-
-    i = 0;
-    j = 0;
-    while (ca[i].ca_type != NSC_NOTYPE &&
-          values[j].v_type != VAL_NOTUSED) {
-       if (ca[i].ca_flags & NSC_EXTRA) {
-           i++;
-           continue;
-       }
-       row_ref = (char *)ptr + ca[i].ca_off;
-       k = 0;
-       do {
-           /*
-            * TODO
-            * factor out NSC_CONST comparsion
-            */
-           switch (values[j].v_type) {
-           case VAL_SYMBOL:
-           case VAL_SYMBOL_SET:
-               if (xunsymbol(&ca[i], &values[j], j) < 0)
-                   return -1;
-               /* fall through */
-           case VAL_DOUBLE:
-               switch (ca[i].ca_type) {
-               case NSC_INT:
-                   if (ca[i].ca_flags & NSC_CONST) {
-                       if (((int *)row_ref)[k] != (int)
-                            values[j].v_field.v_double)
-                           gripe("Field %s must be same, "
-                               "read %d != expected %d",
-                               ca[i].ca_name,
-                               ((int *)row_ref)[k],
-                               (int)values[j].v_field.v_double);
-
-                   } else
-                       ((int *)row_ref)[k] =
-                           (int)values[j].v_field.v_double;
-                   break;
-               case NSC_LONG:
-                   if (ca[i].ca_flags & NSC_CONST) {
-                       if (((long *)row_ref)[k] != (long)
-                            values[j].v_field.v_double)
-                           gripe("Field %s must be same, "
-                               "read %ld != expected %ld",
-                               ca[i].ca_name,
-                               ((long *)row_ref)[k],
-                               (long)values[j].v_field.v_double);
-                   } else
-                       ((long *)row_ref)[k] = (long)
-                           values[j].v_field.v_double;
-                   break;
-               case NSC_USHORT:
-                   if (ca[i].ca_flags & NSC_CONST) {
-                       if (((unsigned short *)row_ref)[k] !=
-                            (unsigned short)values[j].v_field.v_double)
-                           gripe("Field %s must be same, "
-                               "read %d != expected %d",
-                               ca[i].ca_name,
-                               ((unsigned short *)row_ref)[k],
-                               (unsigned short)values[j].v_field.v_double);
-                   } else
-                       ((unsigned short *)row_ref)[k] = (unsigned short)
-                           values[j].v_field.v_double;
-                   break;
-               case NSC_UCHAR:
-                   if (ca[i].ca_flags & NSC_CONST) {
-                       if (((unsigned char *)row_ref)[k] != (unsigned char)
-                            values[j].v_field.v_double)
-                           gripe("Field %s must be same, "
-                               "read %d != expected %d",
-                               ca[i].ca_name,
-                               ((unsigned char *)row_ref)[k],
-                               (unsigned char)values[j].v_field.v_double);
-                   } else
-                       ((unsigned char *)row_ref)[k] = (unsigned char)
-                           values[j].v_field.v_double;
-                   break;
-               case NSC_FLOAT:
-                   if (ca[i].ca_flags & NSC_CONST) {
-                       if (((float *)row_ref)[k] != (float)
-                            values[j].v_field.v_double)
-                           gripe("Field %s must be same, "
-                               "read %g != expected %g",
-                               ca[i].ca_name,
-                               ((float *)row_ref)[k],
-                               (float)values[j].v_field.v_double);
-                   } else
-                       ((float *)row_ref)[k] = (float)
-                           values[j].v_field.v_double;
-                   break;
-               case NSC_STRING:
-                   return gripe("Field %s is a string type, "
-                       "but %lg was read which is a number",
-                       ca[i].ca_name, values[j].v_field.v_double);
-               default:
-                   return gripe("Field %s's type %d is not supported",
-                       ca[i].ca_name, ca[i].ca_type);
-               }
-               break;
-           case VAL_STRING:
-               switch(ca[i].ca_type) {
-               case NSC_STRING:
-                   if (ca[i].ca_flags & NSC_CONST) {
-                       if (strcmp(((char **)row_ref)[k],
-                                  values[j].v_field.v_string) != 0)
-                           gripe("Field %s must be same, "
-                               "read %s != expected %s",
-                               ca[i].ca_name,
-                               ((char **)row_ref)[k],
-                               values[j].v_field.v_string);
-                   } else
-                       ((char **)row_ref)[k]
-                           = strdup(values[j].v_field.v_string);
-                   break;
-               case NSC_INT:
-               case NSC_LONG:
-               case NSC_USHORT:
-               case NSC_UCHAR:
-               case NSC_FLOAT:
-                   return gripe("Field %s is a number type %d, "
-                       "but %s was read which is a string",
-                       ca[i].ca_name, ca[i].ca_type,
-                       values[j].v_field.v_string);
-               default:
-                   return gripe("Field %s's type %d is not supported",
-                           ca[i].ca_name, ca[i].ca_type);
-               }
-               break;
-           case VAL_NOTUSED:
-               return gripe("Missing column %s in file", ca[i].ca_name);
-           default:
-               return gripe("Unknown value type %d", values[j].v_type);
-           }
-           k++;
-           j++;
-       } while (k < ca[i].ca_len);
-       i++;
-    }
-    if (ca[i].ca_type != NSC_NOTYPE)
-       return gripe("Missing column %s in file", ca[i].ca_name);
-    switch  (values[j].v_type) {
-    case VAL_NOTUSED:
-       break;
-    case VAL_STRING:
-    case VAL_SYMBOL:
-    case VAL_SYMBOL_SET:
-       return gripe("Extra junk after the last column, read %s",
-           values[j].v_field.v_string);
-    case VAL_DOUBLE:
-       return gripe("Extra junk after the last column, read %lg",
-           values[j].v_field.v_double);
-    default:
-       return gripe("Extra junk after the last column, "
-           "unknown value type %d", values[j].v_type);
-    }
+    struct castr *ca;
+    int i;
+
+    ca = getfld(fldno, NULL);
+    if (!ca)
+       return -1;
+
+    i = xunsymbol(sym, ca, fldno);
+    if (i < 0)
+       return -1;
+    *set |= symval(ca, i);
     return 0;
 }
 
-int
-xundump(FILE *fp, char *file, int expected_table)
+static int
+xuheader(FILE *fp, int expected_table)
 {
     char name[64];
-    struct empfile *ep;
-    int row, res, rows, ch;
-    struct value values[MAX_NUM_COLUMNS + 1];
+    int res, ch;
     int type;
-    int fixed_rows, need_sentinel;
-
-    if (strcmp(fname, file) != 0) {
-        fname = file;
-       lineno = 1;
-    } else
-       lineno++;
 
     while ((ch = skipfs(fp)) == '\n')
        lineno++;
     if (ch == EOF && expected_table == EF_BAD)
-       return -1;
+       return -2;
     ungetc(ch, fp);
 
+    human = ch == 'c';
     res = -1;
-    if (fscanf(fp, "XDUMP%*[ \t]%63[^ \t#\n]%*[ \t]%*[^ \t#\n]%n",
-              name, &res) != 1
-       || res < 0)
-       return gripe("Expected XDUMP header");
-    if (skipfs(fp) != '\n')
-       return gripe("Junk after XDUMP header");
+    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");
 
     type = ef_byname(name);
     if (type < 0)
        return gripe("Unknown table `%s'", name);
-    ep = &empfile[type];
-    if (CANT_HAPPEN(!(ep->flags & EFF_MEM)))
-       return -1;              /* not implemented */
-
     if (expected_table != EF_BAD && expected_table != type)
        return gripe("Expected table `%s', not `%s'",
                     ef_nameof(expected_table), name);
 
-    fixed_rows = has_const(ef_cadef(type));
-    need_sentinel = !fixed_rows; /* FIXME only approximation */
+    if (!ef_cadef(type) || !(ef_flags(type) & EFF_MEM)) {
+       CANT_HAPPEN(expected_table != EF_BAD);
+       return gripe("Table `%s' is not permitted here", name);
+    }
 
-    row = 0;
-    for (;;) {
-       lineno++;
-       ch = skipfs(fp);
-       if (ch == '/')
-           break;
+    if (skipfs(fp) != '\n')
+       return gripe("Junk after xdump header");
+    lineno++;
+
+    return type;
+}
+
+static int
+xuheader1(FILE *fp, int type, struct castr ca[])
+{
+    struct castr **fca;
+    int *fidx;
+    int ch, i, j, n;
+
+    if (human) {
+       while ((ch = skipfs(fp)) == '\n')
+           lineno++;
        ungetc(ch, fp);
-       /*
-        * TODO
-        * Add column count check to the return value of xuflds()
-        */
-       res = xuflds(fp, values);
-       if (res > 0 && row >= ep->csize - 1) {
-           /* TODO grow cache unless EFF_STATIC */
-           gripe("Too many rows for table %s", name);
-           res = -1;
-       }
-       if (res > 0) {
-           if (!fixed_rows)
-               xuinitrow(type, row);
-           res = xuloadrow(type, row, values);
-           row++;
-       }
-       freeflds(values);
-       if (res < 0)
+       ellipsis = 0;
+       nflds = xuflds(fp, xufldname);
+       if (nflds < 0)
            return -1;
+       nflds -= ellipsis != 0;
+    } else {
+       fca = fldca;
+       fidx = fldidx;
+
+       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;
+           j = 0;
+           do {
+               *fca++ = &ca[i];
+               *fidx++ = j;
+           } while (++j < n);
+       }
+
+       nflds = fidx - fldidx;
     }
 
-    ch = getc(fp);
-    ungetc(ch, fp);
-    if (!isdigit(ch) || fscanf(fp, "%d", &rows) != 1)
-       return gripe("Malformed table footer");
+    return 0;
+}
+
+static int
+xutrailer(FILE *fp, int type, int row)
+{
+    int rows, res;
+
+    res = -1;
+    if (human) {
+       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 (skipfs(fp) != '\n')
        return gripe("Junk after table footer");
-    if (row != rows)
-       return gripe("Read %d rows, which doesn't match footer",
-                    row);
-    if (fixed_rows && row != ep->csize -1)
-       return gripe("Table %s requires %d rows, got %d",
-                    name, ep->csize - 1, row);
-    if (need_sentinel)
-       xuinitrow(type, row);
-
-    while ((ch = skipfs(fp)) == '\n') ;
+    lineno++;
+
+    return 0;
+}
+
+int
+xundump(FILE *fp, char *file, int expected_table)
+{
+    struct castr *ca;
+    int type, nca, nf, i, ch;
+
+    if (fname != file) {
+        fname = file;
+       lineno = 1;
+    }
+
+    if ((type = xuheader(fp, expected_table)) < 0)
+       return type;
+
+    ca = ef_cadef(type);
+    if (CANT_HAPPEN(!ca))
+       return -1;
+
+    nca = nf = 0;
+    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);
+    }
+    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)
+       type = EF_BAD;
+
+    free(caseen);
+    free(caflds);
+    free(fldidx);
+    free(fldca);
+
+    /* Skip empty lines so that callers can easily check for EOF */
+    while ((ch = skipfs(fp)) == '\n')
+       lineno++;
     ungetc(ch, fp);
 
-    ep->fids = ep->cids = row;
     return type;
 }
+
+static int
+xundump2(FILE *fp, int type, struct castr *ca)
+{
+    int i;
+
+    is_partial = 0;
+    for (;;) {
+       if (xuheader1(fp, type, ca) < 0)
+           return -1;
+       if (xundump1(fp, type, ca) < 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)
+           return -1;
+    }
+}
+
+static int
+xundump1(FILE *fp, int type, struct castr *ca)
+{
+    struct empfile *ep = &empfile[type];
+    int need_sentinel = !EF_IS_GAME_STATE(type);
+    int row, n, ch;
+
+    n = 0;
+    for (row = 0;; ++row) {
+       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(type) && n != ep->csize)
+           /* TODO truncate file */
+           gripe("Warning: should resize table %s from %d to %d, not implemented",
+                 ef_nameof(type), ep->csize, n);
+       else if (type >= EF_SHIP_CHR && type <= EF_NUKE_CHR)
+           ep->cids = ep->fids = n;
+       else
+           return gripe("Table %s requires %d rows, got %d",
+                        ef_nameof(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, type, row) < 0)
+       return -1;
+
+    return 0;
+}