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