]> git.pond.sub.org Git - empserver/blob - src/lib/common/xundump.c
(xufld): Clarify.
[empserver] / src / lib / common / xundump.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  xundump.c: Load back xdump output
29  * 
30  *  Known contributors to this file:
31  *     Ron Koenderink, 2005
32  *     Markus Armbruster, 2005-2007
33  */
34
35 /*
36  * FIXME:
37  * - Normalize terminology: table/rows/columns or file/records/fields
38  * - Loading tables with NSC_STRING elements more than once leaks memory
39  * TODO:
40  * - Check each partial table supplies the same rows
41  * - Check EFF_CFG tables are dense
42  * - Symbolic references to non-symbol tables
43  * - Symbolic array indexes
44  * TODO, but hardly worth the effort:
45  * - Permit reordering of array elements
46  */
47
48 #include <config.h>
49
50 #include <ctype.h>
51 #include <stdarg.h>
52 #include <stdio.h>
53 #include <time.h>
54 #include "file.h"
55 #include "match.h"
56 #include "nsc.h"
57 #include "optlist.h"
58 #include "prototypes.h"
59
60 static char *fname;
61 static int lineno;
62 static int human;
63 static int ellipsis, is_partial;
64 static int cur_type, cur_id;
65 static void *cur_obj;
66 static int cur_obj_is_blank;
67 static int nflds;
68 static struct castr **fldca;
69 static int *fldidx;
70 static int *caflds;             /* Map selector number to #fields seen */
71 static int *cafldspp;           /* ditto, in previous parts */
72
73 static int gripe(char *, ...) ATTRIBUTE((format (printf, 1, 2)));
74 static int deffld(int, char *, int);
75 static int defellipsis(void);
76 static int chkflds(void);
77 static int setnum(int, double);
78 static int setstr(int, char *);
79 static int xunsymbol(char *, struct castr *, int);
80 static int setsym(int, char *);
81 static int mtsymset(int, long *);
82 static int add2symset(int, long *, char *);
83 static int xubody(FILE *);
84 static int xutail(FILE *, struct castr *);
85
86 static int
87 gripe(char *fmt, ...)
88 {
89     va_list ap;
90
91     fprintf(stderr, "%s:%d: ", fname, lineno);
92     va_start(ap, fmt);
93     vfprintf(stderr, fmt, ap);
94     va_end(ap);
95     putc('\n', stderr);
96
97     return -1;
98 }
99
100 static int
101 skipfs(FILE *fp)
102 {
103     int ch;
104
105     do {
106         ch = getc(fp);
107     } while (ch == ' ' || ch == '\t');
108
109     if (ch == '#') {
110         do {
111             ch = getc(fp);
112         } while (ch != EOF && ch != '\n');
113     }
114
115     return ch;
116 }
117
118 static int
119 getid(FILE *fp, char *buf)
120 {
121     int n;
122     if (fscanf(fp, "%1023[^#()<>=#\" \t\n]%n", buf, &n) != 1
123         || !isalpha(buf[0]))
124         return -1;
125     return n;
126 }
127
128 static char *
129 xuesc(char *buf)
130 {
131     char *src, *dst;
132     int octal_chr, n;
133
134     dst = buf;
135     src = buf;
136     while (*src) {
137         if (*src == '\\') {
138             if (sscanf(++src, "%3o%n", &octal_chr, &n) != 1 || n != 3)
139                 return NULL;
140             *dst++ = (char)octal_chr;
141             src += 3;
142         } else
143             *dst++ = *src++;
144     }
145     *dst = '\0';
146     return buf;
147 }
148
149 static int
150 xufldname(FILE *fp, int i)
151 {
152     int ch, idx;
153     char buf[1024];
154
155     ch = skipfs(fp);
156     switch (ch) {
157     case EOF:
158         return gripe("Unexpected EOF");
159     case '\n':
160         if (chkflds() < 0)
161             return -1;
162         lineno++;
163         return 0;
164     case '.':
165         if (getc(fp) != '.' || getc(fp) != '.')
166             return gripe("Junk in header field %d", i + 1);
167         if (defellipsis() < 0)
168             return -1;
169         ch = skipfs(fp);
170         if (ch != EOF && ch != '\n')
171             return gripe("Junk after ...");
172         ungetc(ch, fp);
173         return 1;
174     default:
175         ungetc(ch, fp);
176         if (getid(fp, buf) < 0)
177             return gripe("Junk in header field %d", i + 1);
178         ch = getc(fp);
179         if (ch != '(') {
180             ungetc(ch, fp);
181             return deffld(i, buf, -1);
182         }
183         ch = getc(fp);
184         ungetc(ch, fp);
185         if (isdigit(ch) || ch == '-' || ch == '+') {
186             if (fscanf(fp, "%d", &idx) != 1)
187                 return gripe("Malformed number in index of header field %d",
188                              i + 1);
189             if (idx < 0)
190                 return gripe("Index must not be negative in header field %d",
191                              i + 1);
192         } else {
193             if (getid(fp, buf) < 0)
194                 return gripe("Malformed index in header field %d", i + 1);
195             return gripe("Symbolic index in header field %d not yet implemented",
196                          i + 1);
197         }
198         ch = getc(fp);
199         if (ch != ')')
200             return gripe("Malformed index in header field %d", i + 1);
201         return deffld(i, buf, idx);
202     }
203 }
204
205 static int
206 xufld(FILE *fp, int i)
207 {
208     int ch;
209     char buf[1024];
210     double dbl;
211     long set;
212
213     ch = skipfs(fp);
214     switch (ch) {
215     case EOF:
216         return gripe("Unexpected EOF");
217     case '\n':
218         CANT_HAPPEN(i > nflds);
219         if (i < nflds) {
220             if (fldca[i]->ca_type != NSC_STRINGY && fldca[i]->ca_len)
221                 return gripe("Field %s(%d) missing",
222                              fldca[i]->ca_name, fldidx[i]);
223             return gripe("Field %s missing", fldca[i]->ca_name);
224         }
225         lineno++;
226         return 0;
227     case '+': case '-': case '.':
228     case '0': case '1': case '2': case '3': case '4':
229     case '5': case '6': case '7': case '8': case '9':
230         ungetc(ch, fp);
231         if (fscanf(fp, "%lg", &dbl) != 1)
232             return gripe("Malformed number in field %d", i + 1);
233         return setnum(i, dbl);
234     case '"':
235         ch = getc(fp);
236         if (ch == '"')
237             buf[0] = 0;
238         else {
239             ungetc(ch, fp);
240             if (fscanf(fp, "%1023[^\"\n]", buf) != 1 || getc(fp) != '"')
241                 return gripe("Malformed string in field %d", i + 1);
242             if (!xuesc(buf))
243                 return gripe("Invalid escape sequence in field %d",
244                              i + 1);
245         }
246         return setstr(i, buf);
247     case '(':
248         if (mtsymset(i, &set) < 0)
249             return -1;
250         for (;;) {
251             ch = skipfs(fp);
252             if (ch == EOF || ch == '\n')
253                 return gripe("Unmatched '(' in field %d", i + 1);
254             if (ch == ')')
255                 break;
256             ungetc(ch, fp);
257             if (getid(fp, buf) < 0)
258                 return gripe("Junk in field %d", i + 1);
259             if (add2symset(i, &set, buf) < 0)
260                 return -1;
261         }
262         return setnum(i, set);
263     default:
264         ungetc(ch, fp);
265         if (getid(fp, buf) < 0)
266             return gripe("Junk in field %d", i + 1);
267         if (!strcmp(buf, "nil"))
268             return setstr(i, NULL);
269         else
270             return setsym(i, buf);
271     }
272 }
273
274 static int
275 xuflds(FILE *fp, int (*parse)(FILE *, int))
276 {
277     int i, ch, res;
278
279     for (i = 0; ; i++) {
280         res = parse(fp, i);
281         if (res < 0)
282             return -1;
283         if (res == 0)
284             return i;
285         ch = getc(fp);
286         if (ch == '\n')
287             ungetc(ch, fp);
288         else if (ch != ' ' && ch != '\t')
289             return gripe("Bad field separator after field %d", i + 1);
290     }
291 }
292
293 static int
294 deffld(int fldno, char *name, int idx)
295 {
296     struct castr *ca = ef_cadef(cur_type);
297     int res;
298
299     res = stmtch(name, ca, offsetof(struct castr, ca_name),
300                      sizeof(struct castr));
301     if (res < 0)
302         return gripe("Header %s of field %d is %s", name, fldno + 1,
303                      res == M_NOTUNIQUE ? "ambiguous" : "unknown");
304     if (ca[res].ca_type != NSC_STRINGY && ca[res].ca_len != 0) {
305         if (idx < 0)
306             return gripe("Header %s requires an index in field %d",
307                          ca[res].ca_name, fldno + 1);
308         if (idx >= ca[res].ca_len)
309             return gripe("Header %s(%d) index out of bounds in field %d",
310                          ca[res].ca_name, idx, fldno + 1);
311         if (idx < caflds[res])
312             return gripe("Duplicate header %s(%d) in field %d",
313                          ca[res].ca_name, idx, fldno + 1);
314         if (idx > caflds[res])
315             return gripe("Expected header %s(%d) in field %d",
316                          ca[res].ca_name, caflds[res], fldno + 1);
317     } else {
318         if (idx >= 0)
319             return gripe("Header %s doesn't take an index in field %d",
320                          ca[res].ca_name, fldno + 1);
321         idx = 0;
322         if (caflds[res])
323             return gripe("Duplicate header %s in field %d",
324                          ca[res].ca_name, fldno + 1);
325     }
326     fldca[fldno] = &ca[res];
327     fldidx[fldno] = idx;
328     caflds[res]++;
329     return 1;
330 }
331
332 static int
333 defellipsis(void)
334 {
335     struct castr *ca = ef_cadef(cur_type);
336
337     if (ca[0].ca_table != cur_type)
338         return gripe("Table %s doesn't support ...", ef_nameof(cur_type));
339     ellipsis = is_partial = 1;
340     return 0;
341 }
342
343 static int
344 chkflds(void)
345 {
346     struct castr *ca = ef_cadef(cur_type);
347     int i, len, cafldsmax, res = 0;
348
349     if (is_partial) {
350         /* Need a join field, use 0-th selector */
351         if (!caflds[0])
352             return gripe("Header field %s required with ...", ca[0].ca_name);
353     }
354
355     if (ellipsis)
356         return 0;
357
358     for (i = 0; ca[i].ca_name; i++) {
359         cafldsmax = MAX(caflds[i], cafldspp[i]);
360         if (ca[i].ca_flags & NSC_EXTRA)
361             continue;
362         len = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
363         if (!len && !cafldsmax)
364             res = gripe("Header field %s missing", ca[i].ca_name);
365         else if (len && cafldsmax == len - 1)
366             res = gripe("Header field %s(%d) missing",
367                         ca[i].ca_name, len - 1);
368         else if (len && cafldsmax < len - 1)
369             res = gripe("Header fields %s(%d) ... %s(%d) missing",
370                         ca[i].ca_name, cafldsmax, ca[i].ca_name, len - 1);
371     }
372
373     return res;
374 }
375
376 static struct castr *
377 getfld(int fldno, int *idx)
378 {
379     if (fldno >= nflds) {
380         gripe("Too many fields, expected only %d", nflds);
381         return NULL;
382     }
383     if (CANT_HAPPEN(fldno < 0))
384         return NULL;
385     if (idx)
386         *idx = fldidx[fldno];
387     return fldca[fldno];
388 }
389
390 static int
391 fldval_must_match(int fldno)
392 {
393     struct castr *ca = ef_cadef(cur_type);
394     int i = fldca[fldno] - ca;
395
396     /*
397      * Value must match if:
398      * it's for a const selector, unless the object is still blank, or
399      * it was already given in a previous part of a split table.
400      */
401     return (!cur_obj_is_blank && (fldca[fldno]->ca_flags & NSC_CONST))
402         || fldidx[fldno] < cafldspp[i];
403 }
404
405 static void *
406 getobj(void)
407 {
408     struct empfile *ep = &empfile[cur_type];
409     int need_sentinel = !EF_IS_GAME_STATE(cur_type);
410
411     if (!cur_obj) {
412         cur_obj_is_blank = cur_id >= ep->fids;
413         if (cur_obj_is_blank) {
414             /* TODO grow cache (and posssibly file) unless EFF_STATIC */
415             if (cur_id < ep->csize - !!need_sentinel)
416                 ep->cids = ep->fids = cur_id + 1;
417             /* else: ef_ptr() will fail */
418         }
419         cur_obj = ef_ptr(cur_type, cur_id);
420         if (!cur_obj)
421             gripe("Can't put ID %d into table %s, it holds only 0..%d.",
422                   cur_id, ep->name, ep->fids - 1);
423     }
424
425     return cur_obj;
426 }
427
428 static int
429 setnum(int fldno, double dbl)
430 {
431     struct castr *ca;
432     int idx;
433     char *memb_ptr;
434     double old;
435
436     ca = getfld(fldno, &idx);
437     if (!ca)
438         return -1;
439
440     /*
441      * If this is the record index, put it into cur_id.
442      */
443     if (fldno == 0 && ca->ca_table == cur_type)
444         cur_id = (int)dbl;
445
446     memb_ptr = getobj();
447     if (!memb_ptr)
448         return -1;
449     memb_ptr += ca->ca_off;
450
451     /* FIXME check assignment preserves value */
452     switch (ca->ca_type) {
453     case NSC_CHAR:
454         old = ((signed char *)memb_ptr)[idx];
455         ((signed char *)memb_ptr)[idx] = (signed char)dbl;
456         break;
457     case NSC_UCHAR:
458     case NSC_HIDDEN:
459         old = ((unsigned char *)memb_ptr)[idx];
460         ((unsigned char *)memb_ptr)[idx] = (unsigned char)dbl;
461         break;
462     case NSC_SHORT:
463         old = ((short *)memb_ptr)[idx];
464         ((short *)memb_ptr)[idx] = (short)dbl;
465         break;
466     case NSC_USHORT:
467         old = ((unsigned short *)memb_ptr)[idx];
468         ((unsigned short *)memb_ptr)[idx] = (unsigned short)dbl;
469         break;
470     case NSC_INT:
471         old = ((int *)memb_ptr)[idx];
472         ((int *)memb_ptr)[idx] = (int)dbl;
473         break;
474     case NSC_LONG:
475         old = ((long *)memb_ptr)[idx];
476         ((long *)memb_ptr)[idx] = (long)dbl;
477         break;
478     case NSC_XCOORD:
479         old = ((coord *)memb_ptr)[idx];
480         /* FIXME use variant of xrel() that takes orig instead of nation */
481         if (old >= WORLD_X / 2)
482             old -= WORLD_X;
483         ((coord *)memb_ptr)[idx] = XNORM((coord)dbl);
484         break;
485     case NSC_YCOORD:
486         old = ((coord *)memb_ptr)[idx];
487         /* FIXME use variant of yrel() that takes orig instead of nation */
488         if (old >= WORLD_Y / 2)
489             old -= WORLD_Y;
490         ((coord *)memb_ptr)[idx] = YNORM((coord)dbl);
491         break;
492     case NSC_FLOAT:
493         old = ((float *)memb_ptr)[idx];
494         ((float *)memb_ptr)[idx] = (float)dbl;
495         break;
496     case NSC_DOUBLE:
497         old = ((double *)memb_ptr)[idx];
498         ((double *)memb_ptr)[idx] = dbl;
499         break;
500     case NSC_TIME:
501         old = ((time_t *)memb_ptr)[idx];
502         ((time_t *)memb_ptr)[idx] = (time_t)dbl;
503         break;
504     default:
505         return gripe("Field %d doesn't take numbers", fldno + 1);
506     }
507
508     if (fldval_must_match(fldno) && old != dbl)
509         return gripe("Value for field %d must be %g", fldno + 1, old);
510
511     return 1;
512 }
513
514 static int
515 setstr(int fldno, char *str)
516 {
517     struct castr *ca;
518     int must_match, idx;
519     size_t len;
520     char *memb_ptr, *old;
521
522     ca = getfld(fldno, &idx);
523     if (!ca)
524         return -1;
525
526     memb_ptr = getobj();
527     if (!memb_ptr)
528         return -1;
529     memb_ptr += ca->ca_off;
530     must_match = fldval_must_match(fldno);
531
532     switch (ca->ca_type) {
533     case NSC_STRING:
534         old = ((char **)memb_ptr)[idx];
535         if (!must_match)
536             ((char **)memb_ptr)[idx] = str ? strdup(str) : NULL;
537         len = 65535;            /* really SIZE_MAX, but that's C99 */
538         break;
539     case NSC_STRINGY:
540         if (CANT_HAPPEN(idx))
541             return -1;
542         if (!str)
543             return gripe("Field %d doesn't take nil", fldno + 1);
544         len = ca->ca_len;
545         if (strlen(str) > len)
546             return gripe("Field %d takes at most %d characters",
547                          fldno + 1, (int)len);
548         old = memb_ptr;
549         if (!must_match)
550             strncpy(memb_ptr, str, len);
551         break;
552     default:
553         return gripe("Field %d doesn't take strings", fldno + 1);
554     }
555
556     if (must_match) {
557         if (old && (!str || strncmp(old, str, len)))
558             return gripe("Value for field %d must be \"%.*s\"",
559                          fldno + 1, (int)len, old);
560         if (!old && str)
561             return gripe("Value for field %d must be nil", fldno + 1);
562     }
563
564     return 1;
565 }
566
567 static int
568 xunsymbol(char *id, struct castr *ca, int n)
569 {
570     int i = ef_elt_byname(ca->ca_table, id);
571     if (i < 0)
572         return gripe("%s %s symbol `%s' in field %d",
573                      i == M_NOTUNIQUE ? "Ambiguous" : "Unknown",
574                      ca->ca_name, id, n + 1);
575     return i;
576 }
577
578 static int
579 symval(struct castr *ca, int i)
580 {
581     int type = ca->ca_table;
582
583     if (ef_check(type) < 0)
584         return -1;
585     if (ef_cadef(type) == symbol_ca)
586         /* symbol table, value is in the table */
587         return ((struct symbol *)ef_ptr(type, i))->value;
588     /* value is the table index */
589     return i;
590 }
591
592 static int
593 setsym(int fldno, char *sym)
594 {
595     struct castr *ca;
596     int i;
597
598     ca = getfld(fldno, NULL);
599     if (!ca)
600         return -1;
601
602     if (ca->ca_table == EF_BAD || (ca->ca_flags & NSC_BITS))
603         return gripe("Field %d doesn't take symbols", fldno + 1);
604
605     i = xunsymbol(sym, ca, fldno);
606     if (i < 0)
607         return -1;
608     return setnum(fldno, symval(ca, i));
609 }
610
611 static int
612 mtsymset(int fldno, long *set)
613 {
614     struct castr *ca;
615
616     ca = getfld(fldno, NULL);
617     if (!ca)
618         return -1;
619
620     if (ca->ca_table == EF_BAD || ef_cadef(ca->ca_table) != symbol_ca
621         || !(ca->ca_flags & NSC_BITS))
622         return gripe("Field %d doesn't take symbol sets", fldno + 1);
623     *set = 0;
624     return 0;
625 }
626
627 static int
628 add2symset(int fldno, long *set, char *sym)
629 {
630     struct castr *ca;
631     int i;
632
633     ca = getfld(fldno, NULL);
634     if (!ca)
635         return -1;
636
637     i = xunsymbol(sym, ca, fldno);
638     if (i < 0)
639         return -1;
640     *set |= symval(ca, i);
641     return 0;
642 }
643
644 static int
645 xuheader(FILE *fp, int expected_table)
646 {
647     char name[64];
648     int res, ch;
649     int type;
650
651     while ((ch = skipfs(fp)) == '\n')
652         lineno++;
653     if (ch == EOF && expected_table == EF_BAD)
654         return -2;
655     ungetc(ch, fp);
656
657     human = ch == 'c';
658     res = -1;
659     if ((human
660          ? fscanf(fp, "config%*[ \t]%63[^ \t#\n]%n", name, &res) != 1
661          : fscanf(fp, "XDUMP%*[ \t]%63[^ \t#\n]%*[ \t]%*[^ \t#\n]%n",
662                   name, &res) != 1) || res < 0)
663         return gripe("Expected xdump header");
664
665     type = ef_byname(name);
666     if (type < 0)
667         return gripe("Unknown table `%s'", name);
668     if (expected_table != EF_BAD && expected_table != type)
669         return gripe("Expected table `%s', not `%s'",
670                      ef_nameof(expected_table), name);
671
672     if (!ef_cadef(type) || !(ef_flags(type) & EFF_MEM)) {
673         CANT_HAPPEN(expected_table != EF_BAD);
674         return gripe("Table `%s' is not permitted here", name);
675     }
676
677     if (skipfs(fp) != '\n')
678         return gripe("Junk after xdump header");
679     lineno++;
680
681     return type;
682 }
683
684 static int
685 xufldhdr(FILE *fp, struct castr ca[])
686 {
687     struct castr **fca;
688     int *fidx;
689     int ch, i, j, n;
690
691     for (i = 0; ca[i].ca_name; i++)
692         caflds[i] = 0;
693     ellipsis = 0;
694
695     if (human) {
696         while ((ch = skipfs(fp)) == '\n')
697             lineno++;
698         ungetc(ch, fp);
699         nflds = xuflds(fp, xufldname);
700         if (nflds < 0)
701             return -1;
702         nflds -= ellipsis != 0;
703     } else {
704         fca = fldca;
705         fidx = fldidx;
706
707         for (i = 0; ca[i].ca_name; i++) {
708             if ((ca[i].ca_flags & NSC_EXTRA))
709                 continue;
710             n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
711             j = 0;
712             do {
713                 *fca++ = &ca[i];
714                 *fidx++ = j;
715             } while (++j < n);
716         }
717
718         nflds = fidx - fldidx;
719     }
720
721     return 0;
722 }
723
724 static int
725 xufooter(FILE *fp, struct castr ca[], int row)
726 {
727     int res, rows, i;
728
729     res = -1;
730     if (human) {
731         if (fscanf(fp, "config%n",  &res) != 0 || res < 0)
732             return gripe("Malformed table footer");
733     } else {
734         if (fscanf(fp, "%d", &rows) != 1)
735             return gripe("Malformed table footer");
736         if (row != rows)
737             return gripe("Read %d rows, which doesn't match footer "
738                          "%d rows", row, rows);
739     }
740     if (skipfs(fp) != '\n')
741         return gripe("Junk after table footer");
742     lineno++;
743
744     for (i = 0; ca[i].ca_name; i++) {
745         if (cafldspp[i] < caflds[i])
746             cafldspp[i] = caflds[i];
747     }
748
749     return 0;
750 }
751
752 int
753 xundump(FILE *fp, char *file, int *plno, int expected_table)
754 {
755     struct castr *ca;
756     int type, nca, nf, i, ch;
757
758     fname = file;
759     lineno = *plno;
760
761     if ((type = xuheader(fp, expected_table)) < 0)
762         return type;
763
764     ca = ef_cadef(type);
765     if (CANT_HAPPEN(!ca))
766         return -1;
767
768     nca = nf = 0;
769     for (i = 0; ca[i].ca_name; i++) {
770         nca++;
771         if (!(ca[i].ca_flags & NSC_EXTRA))
772             nf += MAX(1, ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0);
773     }
774     fldca = calloc(nf, sizeof(*fldca));
775     fldidx = calloc(nf, sizeof(*fldidx));
776     caflds = malloc(nca * sizeof(*caflds));
777     cafldspp = calloc(nca, sizeof(*cafldspp));
778     cur_type = type;
779
780     if (xutail(fp, ca) < 0)
781         type = EF_BAD;
782
783     free(cafldspp);
784     free(caflds);
785     free(fldidx);
786     free(fldca);
787
788     /* Skip empty lines so that callers can easily check for EOF */
789     while ((ch = skipfs(fp)) == '\n')
790         lineno++;
791     ungetc(ch, fp);
792
793     *plno = lineno;
794     return type;
795 }
796
797 static int
798 xutail(FILE *fp, struct castr *ca)
799 {
800     int recs;
801
802     is_partial = 0;
803     for (;;) {
804         if (xufldhdr(fp, ca) < 0)
805             return -1;
806         if ((recs = xubody(fp)) < 0)
807             return -1;
808         if (xufooter(fp, ca, recs) < 0)
809             return -1;
810         if (!ellipsis)
811             return 0;
812         if (xuheader(fp, cur_type) < 0)
813             return -1;
814     }
815 }
816
817 static int
818 xubody(FILE *fp)
819 {
820     struct empfile *ep = &empfile[cur_type];
821     int need_sentinel = !EF_IS_GAME_STATE(cur_type);
822     int row, n, ch;
823
824     n = 0;
825     for (row = 0;; ++row) {
826         while ((ch = skipfs(fp)) == '\n')
827             lineno++;
828         if (ch == '/')
829             break;
830         ungetc(ch, fp);
831         cur_obj = NULL;
832         cur_id = row;
833         if (xuflds(fp, xufld) < 0)
834             return -1;
835         n = MAX(n, cur_id + 1);
836     }
837
838     if (CANT_HAPPEN(n > ep->fids))
839         n = ep->fids;
840     if (n < ep->fids) {
841         if (EF_IS_GAME_STATE(cur_type) && n != ep->csize)
842             /* TODO truncate file */
843             gripe("Warning: should resize table %s from %d to %d, not implemented",
844                   ef_nameof(cur_type), ep->csize, n);
845         else if (cur_type >= EF_SHIP_CHR && cur_type <= EF_NUKE_CHR)
846             ep->cids = ep->fids = n;
847         else
848             return gripe("Table %s requires %d rows, got %d",
849                          ef_nameof(cur_type), ep->fids, n);
850     }
851
852     if (need_sentinel) {
853         if (CANT_HAPPEN(n >= ep->csize))
854             return gripe("No space for sentinel");
855         memset(ep->cache + ep->size * n, 0, ep->size);
856     }
857
858     return row;
859 }