2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2014, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * xundump.c: Load back xdump output
29 * Known contributors to this file:
30 * Ron Koenderink, 2005
31 * Markus Armbruster, 2005-2014
35 * See doc/xdump! And keep it up-to-date.
37 * Parsing of machine-readable xdump is not precise: it recognizes
38 * comments, accepts whitespace in place of single space, and accepts
39 * the full human-readable field syntax instead of its machine-
43 * - Normalize terminology: table/rows/columns or file/records/fields
44 * - Loading tables with NSC_STRING elements more than once leaks memory
46 * - Symbolic array indexes
47 * - Option to treat missing and unknown fields as warning, not error
48 * TODO, but hardly worth the effort:
49 * - Permit reordering of array elements
67 static char *fname; /* Name of file being read */
68 static int lineno; /* Current line number */
70 static int cur_type; /* Current table's file type */
71 static int partno; /* Counts from 0..#parts-1 */
72 static void *cur_obj; /* The object being read into */
73 static int cur_id; /* and its index in the table */
75 static unsigned char *idgap; /* idgap && idgap[ID] iff part#0 lacks ID */
76 static int idgap_len; /* #elements in idgap[] */
78 static int human; /* Reading human-readable syntax? */
79 static int ellipsis; /* Header ended with ...? */
80 static int nflds; /* #fields in input records */
81 static struct castr **fldca; /* Map field number to selector */
82 static int *fldidx; /* Map field number to index */
83 static struct valstr *fldval; /* Map field number to value */
84 static int *caflds; /* Map selector number to #fields seen */
85 static int *cafldspp; /* ditto, in previous parts */
87 static int gripe(char *, ...) ATTRIBUTE((format (printf, 1, 2)));
88 static int deffld(int, char *, int);
89 static int chkflds(void);
90 static int setnum(int, double);
91 static int putnum(void *, int, double);
92 static int setstr(int, char *);
93 static int putstr(void *, int, char *);
94 static int setsym(int, char *);
95 static int mtsymset(int, long *);
96 static int add2symset(int, long *, char *);
97 static int xubody(FILE *);
98 static int xutail(FILE *, struct castr *);
101 * Does the code hardcode indexes for table TYPE?
104 have_hardcoded_indexes(int type)
106 return type == EF_ITEM || type == EF_SECTOR_CHR
107 || type == EF_INFRASTRUCTURE;
111 * Okay to truncate table TYPE?
114 may_truncate(int type)
116 return empfile[type].nent < 0 && !have_hardcoded_indexes(type);
120 * Is TYPE's 0-th selector a usable ID?
125 struct castr *ca = ef_cadef(type);
127 return ca[0].ca_table == type && !(ca[0].ca_flags & NSC_EXTRA);
131 * Can we fill in gaps in table TYPE?
134 can_fill_gaps(int type)
136 return ca0_is_id(type) && !have_hardcoded_indexes(type);
140 * Gripe about the current line to stderr, return -1.
143 gripe(char *fmt, ...)
147 fprintf(stderr, "%s:%d: ", fname, lineno);
149 vfprintf(stderr, fmt, ap);
156 /* Make TYPE the current table. */
164 old_nelem = type == EF_BAD ? 0 : ef_nelem(type);
169 /* End the current table. */
178 * Seek to current table's ID-th record.
179 * ID must be acceptable.
180 * Store it in cur_obj, and set cur_id accordingly.
181 * Return 0 on success, -1 on failure.
186 struct empfile *ep = &empfile[cur_type];
188 if (id >= ef_nelem(cur_type)) {
189 if (!ef_ensure_space(cur_type, id, 1))
190 return gripe("Can't put ID %d into table %s", id, ep->name);
193 cur_obj = ef_ptr(cur_type, id);
194 if (CANT_HAPPEN(!cur_obj))
202 * Reset the omitted objects to default state.
205 omit_ids(int id1, int id2)
212 idgap = realloc(idgap, id2 * sizeof(*idgap));
213 for (i = idgap_len; i < id1; i++)
215 for (i = id1; i < id2; i++) {
216 ef_blank(cur_type, i, ef_ptr(cur_type, i));
223 * Return the smallest non-omitted ID in ID1..ID2-1 if any, else -1.
226 expected_id(int id1, int id2)
230 for (i = id1; i < id2; i++) {
231 if (i >= idgap_len || !idgap[i])
239 * If the table has variable length, truncate it.
240 * Else ensure we're omitting the same objects as the previous parts.
241 * Reset any omitted objects to default state.
242 * Return 0 on success, -1 on failure.
247 struct empfile *ep = &empfile[cur_type];
249 if (cur_id + 1 < ep->fids) {
251 if (may_truncate(cur_type)) {
252 if (!ef_truncate(cur_type, cur_id + 1))
255 if (!can_fill_gaps(cur_type))
256 return gripe("Expected %d more rows",
257 ep->fids - (cur_id + 1));
258 omit_ids(cur_id + 1, ep->fids);
261 if (expected_id(cur_id + 1, ep->fids) >= 0)
262 return gripe("Table's first part has more rows");
273 * Find the field for selector CA with index IDX.
274 * Return the field number if it exists, else -1.
277 fld_find(struct castr *ca, int idx)
281 for (i = 0; i < nflds; i++) {
282 if (fldca[i] == ca && fldidx[i] == idx)
289 * Get the current row's ID.
290 * Current table's 0-th selector must be a usable ID.
291 * Return ID on success, -1 on failure.
296 struct castr *ca = ef_cadef(cur_type);
297 int fldno, id, max_id;
299 if (CANT_HAPPEN(partno != 0 || !ca0_is_id(cur_type)))
302 fldno = fld_find(ca, 0);
304 return cur_id + 1; /* ID not specified */
306 * Field values not representable as int will be rejected by
307 * putnum() or putstr(). Leave the error reporting to them, and
308 * simply pick the next ID here.
310 if (fldval[fldno].val_type != NSC_DOUBLE)
312 id = fldval[fldno].val_as.dbl;
313 if (id != fldval[fldno].val_as.dbl)
316 if (id != cur_id + 1 && !can_fill_gaps(cur_type))
317 return gripe("Expected %d in field %d",
318 cur_id + 1, fldno + 1);
320 return gripe("Field %d must be > %d", fldno + 1, cur_id);
321 max_id = ef_id_limit(cur_type);
323 return gripe("Field %d must be <= %d", fldno + 1, max_id);
329 * Get the current row's object.
330 * Store it in cur_obj, and set cur_id accordingly.
331 * Return 0 on success, -1 on failure.
336 int last_id = cur_id;
340 id = expected_id(cur_id + 1, empfile[cur_type].fids);
342 return gripe("Table's first part doesn't have this row");
343 } else if (ca0_is_id(cur_type)) {
349 if (id > ef_id_limit(cur_type))
350 return gripe("Too many rows");
351 if (tbl_seek(id) < 0)
355 omit_ids(last_id + 1, id);
360 * Save the current row's fields in its object.
361 * Return 0 on success, -1 on failure.
371 for (i = 0; i < nflds; i++) {
372 switch (fldval[i].val_type) {
374 ret |= putnum(cur_obj, i, fldval[i].val_as.dbl);
377 ret |= putstr(cur_obj, i, fldval[i].val_as.str.base);
378 free(fldval[i].val_as.str.base);
390 * Read and ignore field separators from FP.
391 * Return first character that is not a field separator.
400 } while (ch == ' ' || ch == '\t');
405 } while (ch != EOF && ch != '\n');
412 * Decode escape sequences in BUF.
413 * Return BUF on success, null pointer on failure.
425 if (sscanf(++src, "%3o%n", &octal_chr, &n) != 1 || n != 3)
427 *dst++ = (char)octal_chr;
437 * Read an identifier from FP into BUF.
438 * BUF must have space for 1024 characters.
439 * Return number of characters read on success, -1 on failure.
442 getid(FILE *fp, char *buf)
445 if (fscanf(fp, "%1023[^\"#()<>= \t\n]%n", buf, &n) != 1
453 * Try to read a field name from FP.
454 * I is the field number, counting from zero.
455 * If a name is read, set fldca[I] and fldidx[I] for it, and update
457 * Return 1 if a name or ... was read, 0 on end of line, -1 on error.
460 xufldname(FILE *fp, int i)
468 return gripe("Unexpected EOF");
470 nflds = i - (ellipsis != 0);
476 if (getc(fp) != '.' || getc(fp) != '.')
477 return gripe("Junk in header field %d", i + 1);
479 return gripe("Header fields expected");
482 if (ch != EOF && ch != '\n')
483 return gripe("Junk after ...");
488 if (getid(fp, buf) < 0)
489 return gripe("Junk in header field %d", i + 1);
493 return deffld(i, buf, -1);
497 if (isdigit(ch) || ch == '-' || ch == '+') {
498 if (fscanf(fp, "%d", &idx) != 1)
499 return gripe("Malformed number in index of header field %d",
502 return gripe("Index must not be negative in header field %d",
505 if (getid(fp, buf) < 0)
506 return gripe("Malformed index in header field %d", i + 1);
507 return gripe("Symbolic index in header field %d not yet implemented",
512 return gripe("Malformed index in header field %d", i + 1);
513 return deffld(i, buf, idx);
518 * Try to read a field value from FP.
519 * I is the field number, counting from zero.
520 * Return 1 if a value was read, 0 on end of line, -1 on error.
523 xufld(FILE *fp, int i)
533 return gripe("Unexpected EOF");
535 CANT_HAPPEN(i > nflds);
536 for (j = i; j < nflds; j++) {
537 if (CA_IS_ARRAY(fldca[j]))
538 gripe("Field %s(%d) missing",
539 fldca[j]->ca_name, fldidx[j]);
541 gripe("Field %s missing", fldca[j]->ca_name);
543 if (i != nflds || putrow() < 0)
546 return i < nflds ? -1 : 0;
547 case '+': case '-': case '.':
548 case '0': case '1': case '2': case '3': case '4':
549 case '5': case '6': case '7': case '8': case '9':
551 if (fscanf(fp, "%lg", &dbl) != 1)
552 return gripe("Malformed number in field %d", i + 1);
553 return setnum(i, dbl);
560 if (fscanf(fp, "%1023[^\"\n]", buf) != 1 || getc(fp) != '"')
561 return gripe("Malformed string in field %d", i + 1);
563 return gripe("Invalid escape sequence in field %d",
566 return setstr(i, buf);
568 if (mtsymset(i, &set) < 0)
572 if (ch == EOF || ch == '\n')
573 return gripe("Unmatched '(' in field %d", i + 1);
577 if (getid(fp, buf) < 0)
578 return gripe("Junk in field %d", i + 1);
579 if (add2symset(i, &set, buf) < 0)
582 return setnum(i, set);
585 if (getid(fp, buf) < 0)
586 return gripe("Junk in field %d", i + 1);
587 if (!strcmp(buf, "nil"))
588 return setstr(i, NULL);
590 return setsym(i, buf);
595 * Read fields from FP.
596 * Use PARSE() to read each field.
597 * Return number of fields read on success, -1 on error.
600 xuflds(FILE *fp, int (*parse)(FILE *, int))
613 else if (ch != ' ' && ch != '\t')
614 return gripe("Bad field separator after field %d", i + 1);
619 * Define the FLDNO-th field.
620 * If IDX is negative, define as selector NAME, else as NAME(IDX).
621 * Set fldca[FLDNO] and fldidx[FLDNO] accordingly.
623 * Return 1 on success, -1 on error.
626 deffld(int fldno, char *name, int idx)
628 struct castr *ca = ef_cadef(cur_type);
631 res = stmtch(name, ca, offsetof(struct castr, ca_name),
632 sizeof(struct castr));
634 return gripe("Header %s of field %d is %s", name, fldno + 1,
635 res == M_NOTUNIQUE ? "ambiguous" : "unknown");
636 if ((ca[res].ca_flags & NSC_EXTRA) || CANT_HAPPEN(ca[res].ca_get))
637 return gripe("Extraneous header %s in field %d", name, fldno + 1);
638 if (CA_IS_ARRAY(&ca[res])) {
640 return gripe("Header %s requires an index in field %d",
641 ca[res].ca_name, fldno + 1);
642 if (idx >= ca[res].ca_len)
643 return gripe("Header %s(%d) index out of bounds in field %d",
644 ca[res].ca_name, idx, fldno + 1);
645 if (idx < caflds[res])
646 return gripe("Duplicate header %s(%d) in field %d",
647 ca[res].ca_name, idx, fldno + 1);
648 if (idx > caflds[res])
649 return gripe("Expected header %s(%d) in field %d",
650 ca[res].ca_name, caflds[res], fldno + 1);
653 return gripe("Header %s doesn't take an index in field %d",
654 ca[res].ca_name, fldno + 1);
657 return gripe("Duplicate header %s in field %d",
658 ca[res].ca_name, fldno + 1);
660 fldca[fldno] = &ca[res];
667 * Check fields in xdump are sane.
668 * Return 0 on success, -1 on error.
673 struct castr *ca = ef_cadef(cur_type);
674 int i, len, cafldsmax, res = 0;
677 return res; /* table is split, another part expected */
679 /* Check for missing fields */
680 for (i = 0; ca[i].ca_name; i++) {
681 cafldsmax = MAX(caflds[i], cafldspp[i]);
682 if (ca[i].ca_flags & NSC_EXTRA)
684 len = CA_ARRAY_LEN(&ca[i]);
685 if (!len && !cafldsmax)
686 res = gripe("Header field %s missing", ca[i].ca_name);
687 else if (len && cafldsmax == len - 1)
688 res = gripe("Header field %s(%d) missing",
689 ca[i].ca_name, len - 1);
690 else if (len && cafldsmax < len - 1)
691 res = gripe("Header fields %s(%d) ... %s(%d) missing",
692 ca[i].ca_name, cafldsmax, ca[i].ca_name, len - 1);
699 * Get selector for field FLDNO.
700 * Assign the field's selector index to *IDX, unless it is null.
701 * Return the selector on success, null pointer on error.
703 static struct castr *
704 getfld(int fldno, int *idx)
706 if (fldno >= nflds) {
707 gripe("Too many fields, expected only %d", nflds);
710 if (CANT_HAPPEN(fldno < 0))
713 *idx = fldidx[fldno];
718 * Is a new value for field FLDNO required to match the old one?
721 fldval_must_match(int fldno)
723 struct castr *ca = ef_cadef(cur_type);
724 int i = fldca[fldno] - ca;
727 * Value must match if:
728 * it's for a const selector, unless the object is still blank, or
729 * it was already given in a previous part of a split table.
731 return (cur_id < old_nelem && (fldca[fldno]->ca_flags & NSC_CONST))
732 || fldidx[fldno] < cafldspp[i];
736 * Set value of field FLDNO in current row to DBL.
737 * Return 1 on success, -1 on error.
740 setnum(int fldno, double dbl)
742 if (!getfld(fldno, NULL))
744 fldval[fldno].val_cat = NSC_VAL;
745 fldval[fldno].val_type = NSC_DOUBLE;
746 fldval[fldno].val_as.dbl = dbl;
751 * Set OBJ's field FLDNO to DBL.
752 * Return 0 on success, -1 on error.
755 putnum(void *obj, int fldno, double dbl)
757 struct castr *ca = fldca[fldno];
758 int idx = fldidx[fldno];
762 memb_ptr = (char *)obj + ca->ca_off;
764 switch (ca->ca_type) {
766 old = ((signed char *)memb_ptr)[idx];
767 new = ((signed char *)memb_ptr)[idx] = (signed char)dbl;
770 old = ((unsigned char *)memb_ptr)[idx];
771 new = ((unsigned char *)memb_ptr)[idx] = (unsigned char)dbl;
774 old = ((short *)memb_ptr)[idx];
775 new = ((short *)memb_ptr)[idx] = (short)dbl;
778 old = ((unsigned short *)memb_ptr)[idx];
779 new = ((unsigned short *)memb_ptr)[idx] = (unsigned short)dbl;
782 old = ((int *)memb_ptr)[idx];
783 new = ((int *)memb_ptr)[idx] = (int)dbl;
786 old = ((long *)memb_ptr)[idx];
787 new = ((long *)memb_ptr)[idx] = (long)dbl;
790 old = ((coord *)memb_ptr)[idx];
791 /* FIXME use variant of xrel() that takes orig instead of nation */
792 if (old >= WORLD_X / 2)
794 new = ((coord *)memb_ptr)[idx] = XNORM((coord)dbl);
795 if (new >= WORLD_X / 2)
799 old = ((coord *)memb_ptr)[idx];
800 /* FIXME use variant of yrel() that takes orig instead of nation */
801 if (old >= WORLD_Y / 2)
803 new = ((coord *)memb_ptr)[idx] = YNORM((coord)dbl);
804 if (new >= WORLD_Y / 2)
808 old = ((float *)memb_ptr)[idx];
809 ((float *)memb_ptr)[idx] = (float)dbl;
810 new = dbl; /* suppress new != dbl check */
813 old = ((double *)memb_ptr)[idx];
814 ((double *)memb_ptr)[idx] = dbl;
815 new = dbl; /* suppress new != dbl check */
818 old = ((time_t *)memb_ptr)[idx];
819 new = ((time_t *)memb_ptr)[idx] = (time_t)dbl;
822 return gripe("Field %d doesn't take numbers", fldno + 1);
825 if (fldval_must_match(fldno) && old != dbl)
826 return gripe("Value for field %d must be %g", fldno + 1, old);
828 return gripe("Field %d can't hold this value", fldno + 1);
834 * Set value of field FLDNO in current row to STR.
835 * Return 1 on success, -1 on error.
838 setstr(int fldno, char *str)
840 if (!getfld(fldno, NULL))
842 fldval[fldno].val_cat = NSC_VAL;
843 fldval[fldno].val_type = NSC_STRING;
844 fldval[fldno].val_as.str.base = str ? strdup(str) : NULL;
845 fldval[fldno].val_as.str.maxsz = INT_MAX;
846 /* really SIZE_MAX, but that's C99 */
851 * Set obj's field FLDNO to STR.
852 * Return 0 on success, -1 on error.
855 putstr(void *obj, int fldno, char *str)
857 struct castr *ca = fldca[fldno];
858 int idx = fldidx[fldno];
859 int must_match, mismatch;
861 char *memb_ptr, *old;
863 memb_ptr = (char *)obj + ca->ca_off;
864 must_match = fldval_must_match(fldno);
867 switch (ca->ca_type) {
869 old = ((char **)memb_ptr)[idx];
871 mismatch = old ? !str || strcmp(old, str) : !!str;
873 /* FIXME may leak old value */
874 ((char **)memb_ptr)[idx] = str ? strdup(str) : NULL;
875 len = -1; /* unlimited */
878 if (CANT_HAPPEN(idx))
881 return gripe("Field %d doesn't take nil", fldno + 1);
882 /* Wart: if ca_len <= 1, the terminating null may be omitted */
884 len = sz > 1 ? sz - 1 : sz;
885 if (strlen(str) > len)
886 return gripe("Field %d takes at most %d characters",
887 fldno + 1, (int)len);
890 mismatch = !str || strncmp(old, str, len);
892 strncpy(memb_ptr, str, sz);
895 return gripe("Field %d doesn't take strings", fldno + 1);
900 return gripe("Value for field %d must be \"%.*s\"",
901 fldno + 1, (int)len, old);
903 return gripe("Value for field %d must be nil", fldno + 1);
910 * Resolve symbol name ID in table referred to by CA.
911 * Use field number N for error messages.
912 * Return index in referred table on success, -1 on failure.
915 xunsymbol(char *id, struct castr *ca, int n)
917 int i = ef_elt_byname(ca->ca_table, id);
919 return gripe("%s %s symbol `%s' in field %d",
920 i == M_NOTUNIQUE ? "Ambiguous" : "Unknown",
921 ca->ca_name, id, n + 1);
926 * Map symbol index to symbol value.
927 * CA is the table, and I is the index in it.
930 symval(struct castr *ca, int i)
932 int type = ca->ca_table;
934 if (type != EF_BAD && ef_cadef(type) == symbol_ca)
935 /* symbol table, value is in the table */
936 return ((struct symbol *)ef_ptr(type, i))->value;
937 /* value is the table index */
942 * Set value of field FLDNO in current object to value of symbol SYM.
943 * Return 1 on success, -1 on error.
946 setsym(int fldno, char *sym)
951 ca = getfld(fldno, NULL);
955 if (ca->ca_table == EF_BAD || (ca->ca_flags & NSC_BITS))
956 return gripe("Field %d doesn't take symbols", fldno + 1);
958 i = xunsymbol(sym, ca, fldno);
961 return setnum(fldno, symval(ca, i));
965 * Create an empty symbol set for field FLDNO in *SET.
966 * Return 1 on success, -1 on error.
969 mtsymset(int fldno, long *set)
973 ca = getfld(fldno, NULL);
977 if (ca->ca_table == EF_BAD || ef_cadef(ca->ca_table) != symbol_ca
978 || !(ca->ca_flags & NSC_BITS))
979 return gripe("Field %d doesn't take symbol sets", fldno + 1);
985 * Add a symbol to a symbol set for field FLDNO in *SET.
986 * SYM is the name of the symbol to add.
987 * Return 1 on success, -1 on error.
990 add2symset(int fldno, long *set, char *sym)
995 ca = getfld(fldno, NULL);
999 i = xunsymbol(sym, ca, fldno);
1002 *set |= symval(ca, i);
1007 * Read an xdump table header line from FP.
1008 * Expect header for EXPECTED_TABLE, unless it is EF_BAD.
1009 * Recognize header for machine- and human-readable syntax, and set
1010 * human accordingly.
1011 * Return table type on success, -2 on EOF before header, -1 on failure.
1014 xuheader(FILE *fp, int expected_table)
1020 while ((ch = skipfs(fp)) == '\n')
1022 if (ch == EOF && expected_table == EF_BAD)
1029 ? fscanf(fp, "config%*[ \t]%63[^ \t#\n]%n", name, &res) != 1
1030 : fscanf(fp, "XDUMP%*[ \t]%63[^ \t#\n]%*[ \t]%*[^ \t#\n]%n",
1031 name, &res) != 1) || res < 0)
1032 return gripe("Expected xdump header");
1034 type = ef_byname(name);
1036 return gripe("Unknown table `%s'", name);
1037 if (expected_table != EF_BAD && expected_table != type)
1038 return gripe("Expected table `%s', not `%s'",
1039 ef_nameof(expected_table), name);
1041 if (!empfile[type].file
1042 || !ef_cadef(type) || !(ef_flags(type) & EFF_MEM)) {
1043 CANT_HAPPEN(expected_table != EF_BAD);
1044 return gripe("Table `%s' is not permitted here", name);
1047 if (skipfs(fp) != '\n')
1048 return gripe("Junk after xdump header");
1055 * Find fields in this xdump.
1056 * If reading human-readable syntax, read a field header line from FP.
1057 * Else take fields from the table's selectors in CA[].
1058 * Set ellipsis, nflds, fldca[], fldidx[] and caflds[] accordingly.
1059 * Return 0 on success, -1 on failure.
1062 xufldhdr(FILE *fp, struct castr ca[])
1068 for (i = 0; ca[i].ca_name; i++)
1073 while ((ch = skipfs(fp)) == '\n')
1076 if (xuflds(fp, xufldname) < 0)
1082 for (i = 0; ca[i].ca_name; i++) {
1083 if ((ca[i].ca_flags & NSC_EXTRA))
1085 n = CA_ARRAY_LEN(&ca[i]);
1093 nflds = fidx - fldidx;
1100 * Read xdump footer from FP.
1101 * CA[] contains the table's selectors.
1102 * The body had RECS records.
1103 * Update cafldspp[] from caflds[].
1104 * Return 0 on success, -1 on failure.
1107 xufooter(FILE *fp, struct castr ca[], int recs)
1113 if (fscanf(fp, "config%n", &res) != 0 || res < 0)
1114 return gripe("Malformed table footer");
1116 if (fscanf(fp, "%d", &n) != 1)
1117 return gripe("Malformed table footer");
1119 return gripe("Read %d rows, which doesn't match footer "
1120 "%d rows", recs, n);
1122 if (skipfs(fp) != '\n')
1123 return gripe("Junk after table footer");
1124 if (tbl_part_done() < 0)
1128 for (i = 0; ca[i].ca_name; i++) {
1129 if (cafldspp[i] < caflds[i])
1130 cafldspp[i] = caflds[i];
1137 * Read an xdump table from FP.
1138 * Both machine- and human-readable xdump syntax are recognized.
1139 * Expect table EXPECTED_TABLE, unless it is EF_BAD.
1140 * Report errors to stderr.
1141 * Messages assume FP starts in the file FILE at line *PLNO.
1142 * Update *PLNO to reflect lines read from FP.
1143 * Return table type on success, -2 on EOF before header, -1 on failure.
1146 xundump(FILE *fp, char *file, int *plno, int expected_table)
1149 int type, nca, nf, i, ch;
1154 if ((type = xuheader(fp, expected_table)) < 0)
1157 ca = ef_cadef(type);
1158 if (CANT_HAPPEN(!ca))
1162 for (i = 0; ca[i].ca_name; i++) {
1164 if (!(ca[i].ca_flags & NSC_EXTRA))
1165 nf += MAX(1, CA_ARRAY_LEN(&ca[i]));
1167 fldca = malloc(nf * sizeof(*fldca));
1168 fldidx = malloc(nf * sizeof(*fldidx));
1169 fldval = malloc(nf * sizeof(*fldval));
1170 caflds = malloc(nca * sizeof(*caflds));
1171 cafldspp = calloc(nca, sizeof(*cafldspp));
1174 if (xutail(fp, ca) < 0)
1184 /* Skip empty lines so that callers can easily check for EOF */
1185 while ((ch = skipfs(fp)) == '\n')
1194 * Read the remainder of an xdump after the table header line from FP.
1195 * CA[] contains the table's selectors.
1196 * Return 0 on success, -1 on failure.
1199 xutail(FILE *fp, struct castr *ca)
1204 if (xufldhdr(fp, ca) < 0)
1206 if ((recs = xubody(fp)) < 0)
1208 if (xufooter(fp, ca, recs) < 0)
1212 if (xuheader(fp, cur_type) < 0)
1218 * Read the body of an xdump table from FP.
1219 * Return number of rows read on success, -1 on failure.
1227 while ((ch = skipfs(fp)) == '\n')
1232 if (xuflds(fp, xufld) < 0)