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
68 static char *fname; /* Name of file being read */
69 static int lineno; /* Current line number */
71 static int cur_type; /* Current table's file type */
72 static int partno; /* Counts from 0..#parts-1 */
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 setstr(int, char *);
92 static int setsym(int, char *);
93 static int mtsymset(int, long *);
94 static int add2symset(int, long *, char *);
95 static int xubody(FILE *);
96 static int xutail(FILE *, struct castr *);
99 * Does the code hardcode indexes for table TYPE?
102 have_hardcoded_indexes(int type)
104 return type == EF_ITEM || type == EF_SECTOR_CHR
105 || type == EF_INFRASTRUCTURE;
109 * Okay to truncate table TYPE?
112 may_truncate(int type)
114 return empfile[type].nent < 0 && !have_hardcoded_indexes(type);
118 * Is TYPE's 0-th selector a usable ID?
123 struct castr *ca = ef_cadef(type);
125 return ca[0].ca_table == type && !(ca[0].ca_flags & NSC_EXTRA);
129 * Can we fill in gaps in table TYPE?
132 can_fill_gaps(int type)
134 return (ca0_is_id(type) || type == EF_SECTOR || type == EF_REALM)
135 && !have_hardcoded_indexes(type);
139 * Gripe about the current line to stderr, return -1.
142 gripe(char *fmt, ...)
146 fprintf(stderr, "%s:%d: ", fname, lineno);
148 vfprintf(stderr, fmt, ap);
155 /* Make TYPE the current table. */
162 old_nelem = type == EF_BAD ? 0 : ef_nelem(type);
167 /* End the current table. */
176 * Seek to current table's ID-th object.
177 * Extend the table if necessary.
179 * Return the object on success, NULL on failure.
184 struct empfile *ep = &empfile[cur_type];
187 if (id >= ef_nelem(cur_type)) {
188 if (!ef_ensure_space(cur_type, id, 1)) {
189 gripe("Can't put ID %d into table %s", id, ep->name);
194 obj = ef_ptr(cur_type, id);
195 if (CANT_HAPPEN(!obj))
203 * Reset the omitted objects to default state.
206 omit_ids(int id1, int id2)
213 idgap = realloc(idgap, id2 * sizeof(*idgap));
214 for (i = idgap_len; i < id1; i++)
216 for (i = id1; i < id2; i++) {
217 ef_blank(cur_type, i, ef_ptr(cur_type, i));
224 * Return the smallest non-omitted ID in ID1..ID2-1 if any, else -1.
227 expected_id(int id1, int id2)
231 for (i = id1; i < id2; i++) {
232 if (i >= idgap_len || !idgap[i])
240 * If the table has variable length, truncate it.
241 * Else ensure we're omitting the same objects as the previous parts.
242 * Reset any omitted objects to default state.
243 * Return 0 on success, -1 on failure.
248 struct empfile *ep = &empfile[cur_type];
250 if (cur_id + 1 < ep->fids) {
252 if (may_truncate(cur_type)) {
253 if (!ef_truncate(cur_type, cur_id + 1))
256 if (!can_fill_gaps(cur_type))
257 return gripe("Expected %d more rows",
258 ep->fids - (cur_id + 1));
259 omit_ids(cur_id + 1, ep->fids);
262 if (expected_id(cur_id + 1, ep->fids) >= 0)
263 return gripe("Table's first part has more rows");
273 * Get selector for field FLDNO.
274 * Assign the field's selector index to *IDX, unless it is null.
275 * Return the selector on success, null pointer on error.
277 static struct castr *
278 getfld(int fldno, int *idx)
280 if (fldno >= nflds) {
281 gripe("Too many fields, expected only %d", nflds);
284 if (CANT_HAPPEN(fldno < 0))
287 *idx = fldidx[fldno];
292 * Find the field for selector CA with index IDX.
293 * Return the field number if it exists, else -1.
296 fld_find(struct castr *ca, int idx)
300 for (i = 0; i < nflds; i++) {
301 if (fldca[i] == ca && fldidx[i] == idx)
308 * Get the current row's ID.
309 * Current table's 0-th selector must be a usable ID.
310 * Return ID on success, -1 on failure.
315 struct castr *ca = ef_cadef(cur_type);
316 int fldno, id, max_id;
318 if (CANT_HAPPEN(partno != 0 || !ca0_is_id(cur_type)))
321 fldno = fld_find(ca, 0);
323 return cur_id + 1; /* ID not specified */
325 * Field values not representable as int will be rejected by
326 * putnum() or putstr(). Leave the error reporting to them, and
327 * simply pick the next ID here.
329 if (fldval[fldno].val_type != NSC_DOUBLE)
331 id = fldval[fldno].val_as.dbl;
332 if (id != fldval[fldno].val_as.dbl)
335 if (id != cur_id + 1 && !can_fill_gaps(cur_type))
336 return gripe("Expected %d in field %d",
337 cur_id + 1, fldno + 1);
339 return gripe("Field %d must be > %d", fldno + 1, cur_id);
340 max_id = ef_id_limit(cur_type);
342 return gripe("Field %d must be <= %d", fldno + 1, max_id);
348 * Find the field NAME with index IDX and value representable as long.
349 * Return the field number if it exists, else -1.
352 fld_find_long_by_name(char *name, int idx)
356 for (i = 0; i < nflds; i++) {
357 if (!strcmp(fldca[i]->ca_name, name) && fldidx[i] == idx)
361 if (i == nflds || fldval[i].val_type != NSC_DOUBLE
362 || (long)fldval[i].val_as.dbl != fldval[i].val_as.dbl)
368 * Get the current row's ID.
369 * Current table's type must be EF_SECTOR.
370 * Return ID on success, -1 on failure.
375 int fldno_x, fldno_y, id;
378 if (CANT_HAPPEN(partno != 0 || cur_type != EF_SECTOR))
381 fldno_x = fld_find_long_by_name("xloc", 0);
382 fldno_y = fld_find_long_by_name("yloc", 0);
383 if (fldno_x < 0 || fldno_y < 0)
386 id = sctoff((long)fldval[fldno_x].val_as.dbl,
387 (long)fldval[fldno_y].val_as.dbl);
388 /* Note: reporting values out of range left to putnum() */
390 sctoff2xy(&x, &y, cur_id);
391 return gripe("Coordinates in fields %d,%d must be > %d,%d",
392 fldno_x + 1, fldno_y + 1, x, y);
398 * Get the current row's ID.
399 * Current table's type must be EF_REALM.
400 * Return ID on success, -1 on failure.
405 int fldno_cnum, fldno_realm, id;
408 if (CANT_HAPPEN(partno != 0 || cur_type != EF_REALM))
411 fldno_cnum = fld_find_long_by_name("cnum", 0);
412 fldno_realm = fld_find_long_by_name("realm", 0);
413 if (fldno_cnum < 0 || fldno_realm < 0)
416 realm = (long)fldval[fldno_realm].val_as.dbl;
417 cnum = (long)fldval[fldno_cnum].val_as.dbl;
418 if (cnum < 0 || cnum >= MAXNOC)
419 return gripe("Field %d must be between 0 and %d",
421 if (realm < 0 || realm >= MAXNOR)
422 return gripe("Field %d must be between 0 and %d",
423 fldno_realm, MAXNOR);
424 id = realm + cnum * MAXNOR;
426 return gripe("Fields %d,%d must be > (%d,%d)",
427 fldno_cnum + 1, fldno_realm + 1,
428 cur_id / MAXNOR, cur_id % MAXNOR);
432 * Get the current row's object.
433 * Extend the table if necessary.
435 * Return the object on success, NULL on failure.
440 int last_id = cur_id;
445 id = expected_id(cur_id + 1, empfile[cur_type].fids);
447 gripe("Table's first part doesn't have this row");
450 } else if (ca0_is_id(cur_type)) {
454 } else if (cur_type == EF_SECTOR) {
458 } else if (cur_type == EF_REALM) {
464 if (id > ef_id_limit(cur_type)) {
465 gripe("Too many rows");
471 omit_ids(last_id + 1, id);
476 * Is a new value for field FLDNO required to match the old one?
479 fldval_must_match(int fldno)
481 struct castr *ca = ef_cadef(cur_type);
482 int i = fldca[fldno] - ca;
485 * Value must match if:
486 * it's for a const selector, unless the object is still blank, or
487 * it was already given in a previous part of a split table.
489 return (cur_id < old_nelem && (fldca[fldno]->ca_flags & NSC_CONST))
490 || fldidx[fldno] < cafldspp[i];
494 * Set OBJ's field FLDNO to DBL.
495 * Return 0 on success, -1 on error.
498 putnum(void *obj, int fldno, double dbl)
500 struct castr *ca = fldca[fldno];
501 int idx = fldidx[fldno];
505 memb_ptr = (char *)obj + ca->ca_off;
507 switch (ca->ca_type) {
509 old = ((signed char *)memb_ptr)[idx];
510 new = ((signed char *)memb_ptr)[idx] = (signed char)dbl;
513 old = ((unsigned char *)memb_ptr)[idx];
514 new = ((unsigned char *)memb_ptr)[idx] = (unsigned char)dbl;
517 old = ((short *)memb_ptr)[idx];
518 new = ((short *)memb_ptr)[idx] = (short)dbl;
521 old = ((unsigned short *)memb_ptr)[idx];
522 new = ((unsigned short *)memb_ptr)[idx] = (unsigned short)dbl;
525 old = ((int *)memb_ptr)[idx];
526 new = ((int *)memb_ptr)[idx] = (int)dbl;
529 old = ((long *)memb_ptr)[idx];
530 new = ((long *)memb_ptr)[idx] = (long)dbl;
533 old = ((coord *)memb_ptr)[idx];
534 /* FIXME use variant of xrel() that takes orig instead of nation */
535 if (old >= WORLD_X / 2)
537 new = ((coord *)memb_ptr)[idx] = XNORM((coord)dbl);
538 if (new >= WORLD_X / 2)
542 old = ((coord *)memb_ptr)[idx];
543 /* FIXME use variant of yrel() that takes orig instead of nation */
544 if (old >= WORLD_Y / 2)
546 new = ((coord *)memb_ptr)[idx] = YNORM((coord)dbl);
547 if (new >= WORLD_Y / 2)
551 old = ((float *)memb_ptr)[idx];
552 ((float *)memb_ptr)[idx] = (float)dbl;
553 new = dbl; /* suppress new != dbl check */
556 old = ((double *)memb_ptr)[idx];
557 ((double *)memb_ptr)[idx] = dbl;
558 new = dbl; /* suppress new != dbl check */
561 old = ((time_t *)memb_ptr)[idx];
562 new = ((time_t *)memb_ptr)[idx] = (time_t)dbl;
565 return gripe("Field %d doesn't take numbers", fldno + 1);
568 if (fldval_must_match(fldno) && old != dbl)
569 return gripe("Value for field %d must be %g", fldno + 1, old);
571 return gripe("Field %d can't hold this value", fldno + 1);
577 * Set obj's field FLDNO to STR.
578 * Return 0 on success, -1 on error.
581 putstr(void *obj, int fldno, char *str)
583 struct castr *ca = fldca[fldno];
584 int idx = fldidx[fldno];
585 int must_match, mismatch;
587 char *memb_ptr, *old;
589 memb_ptr = (char *)obj + ca->ca_off;
590 must_match = fldval_must_match(fldno);
593 switch (ca->ca_type) {
595 old = ((char **)memb_ptr)[idx];
597 mismatch = old ? !str || strcmp(old, str) : !!str;
599 /* FIXME may leak old value */
600 ((char **)memb_ptr)[idx] = str ? strdup(str) : NULL;
601 len = -1; /* unlimited */
604 if (CANT_HAPPEN(idx))
607 return gripe("Field %d doesn't take nil", fldno + 1);
608 /* Wart: if ca_len <= 1, the terminating null may be omitted */
610 len = sz > 1 ? sz - 1 : sz;
611 if (strlen(str) > len)
612 return gripe("Field %d takes at most %d characters",
613 fldno + 1, (int)len);
616 mismatch = !str || strncmp(old, str, len);
618 strncpy(memb_ptr, str, sz);
621 return gripe("Field %d doesn't take strings", fldno + 1);
626 return gripe("Value for field %d must be \"%.*s\"",
627 fldno + 1, (int)len, old);
629 return gripe("Value for field %d must be nil", fldno + 1);
636 * Save the current row's fields in its object.
637 * Return 0 on success, -1 on failure.
649 for (i = 0; i < nflds; i++) {
650 switch (fldval[i].val_type) {
652 ret |= putnum(obj, i, fldval[i].val_as.dbl);
655 ret |= putstr(obj, i, fldval[i].val_as.str.base);
656 free(fldval[i].val_as.str.base);
668 * Read and ignore field separators from FP.
669 * Return first character that is not a field separator.
678 } while (ch == ' ' || ch == '\t');
683 } while (ch != EOF && ch != '\n');
690 * Decode escape sequences in BUF.
691 * Return BUF on success, null pointer on failure.
703 if (sscanf(++src, "%3o%n", &octal_chr, &n) != 1 || n != 3)
705 *dst++ = (char)octal_chr;
715 * Read an identifier from FP into BUF.
716 * BUF must have space for 1024 characters.
717 * Return number of characters read on success, -1 on failure.
720 getid(FILE *fp, char *buf)
723 if (fscanf(fp, "%1023[^\"#()<>= \t\n]%n", buf, &n) != 1
731 * Try to read a field name from FP.
732 * I is the field number, counting from zero.
733 * If a name is read, set fldca[I] and fldidx[I] for it, and update
735 * Return 1 if a name or ... was read, 0 on end of line, -1 on error.
738 xufldname(FILE *fp, int i)
746 return gripe("Unexpected EOF");
748 nflds = i - (ellipsis != 0);
754 if (getc(fp) != '.' || getc(fp) != '.')
755 return gripe("Junk in header field %d", i + 1);
757 return gripe("Header fields expected");
760 if (ch != EOF && ch != '\n')
761 return gripe("Junk after ...");
766 if (getid(fp, buf) < 0)
767 return gripe("Junk in header field %d", i + 1);
771 return deffld(i, buf, -1);
775 if (isdigit(ch) || ch == '-' || ch == '+') {
776 if (fscanf(fp, "%d", &idx) != 1)
777 return gripe("Malformed number in index of header field %d",
780 return gripe("Index must not be negative in header field %d",
783 if (getid(fp, buf) < 0)
784 return gripe("Malformed index in header field %d", i + 1);
785 return gripe("Symbolic index in header field %d not yet implemented",
790 return gripe("Malformed index in header field %d", i + 1);
791 return deffld(i, buf, idx);
796 * Try to read a field value from FP.
797 * I is the field number, counting from zero.
798 * Return 1 if a value was read, 0 on end of line, -1 on error.
801 xufld(FILE *fp, int i)
811 return gripe("Unexpected EOF");
813 CANT_HAPPEN(i > nflds);
814 for (j = i; j < nflds; j++) {
815 if (CA_IS_ARRAY(fldca[j]))
816 gripe("Field %s(%d) missing",
817 fldca[j]->ca_name, fldidx[j]);
819 gripe("Field %s missing", fldca[j]->ca_name);
821 if (i != nflds || putrow() < 0)
824 return i < nflds ? -1 : 0;
825 case '+': case '-': case '.':
826 case '0': case '1': case '2': case '3': case '4':
827 case '5': case '6': case '7': case '8': case '9':
829 if (fscanf(fp, "%lg", &dbl) != 1)
830 return gripe("Malformed number in field %d", i + 1);
831 return setnum(i, dbl);
838 if (fscanf(fp, "%1023[^\"\n]", buf) != 1 || getc(fp) != '"')
839 return gripe("Malformed string in field %d", i + 1);
841 return gripe("Invalid escape sequence in field %d",
844 return setstr(i, buf);
846 if (mtsymset(i, &set) < 0)
850 if (ch == EOF || ch == '\n')
851 return gripe("Unmatched '(' in field %d", i + 1);
855 if (getid(fp, buf) < 0)
856 return gripe("Junk in field %d", i + 1);
857 if (add2symset(i, &set, buf) < 0)
860 return setnum(i, set);
863 if (getid(fp, buf) < 0)
864 return gripe("Junk in field %d", i + 1);
865 if (!strcmp(buf, "nil"))
866 return setstr(i, NULL);
868 return setsym(i, buf);
873 * Read fields from FP.
874 * Use PARSE() to read each field.
875 * Return number of fields read on success, -1 on error.
878 xuflds(FILE *fp, int (*parse)(FILE *, int))
891 else if (ch != ' ' && ch != '\t')
892 return gripe("Bad field separator after field %d", i + 1);
897 * Define the FLDNO-th field.
898 * If IDX is negative, define as selector NAME, else as NAME(IDX).
899 * Set fldca[FLDNO] and fldidx[FLDNO] accordingly.
901 * Return 1 on success, -1 on error.
904 deffld(int fldno, char *name, int idx)
906 struct castr *ca = ef_cadef(cur_type);
909 res = stmtch(name, ca, offsetof(struct castr, ca_name),
910 sizeof(struct castr));
912 return gripe("Header %s of field %d is %s", name, fldno + 1,
913 res == M_NOTUNIQUE ? "ambiguous" : "unknown");
914 if ((ca[res].ca_flags & NSC_EXTRA) || CANT_HAPPEN(ca[res].ca_get))
915 return gripe("Extraneous header %s in field %d", name, fldno + 1);
916 if (CA_IS_ARRAY(&ca[res])) {
918 return gripe("Header %s requires an index in field %d",
919 ca[res].ca_name, fldno + 1);
920 if (idx >= ca[res].ca_len)
921 return gripe("Header %s(%d) index out of bounds in field %d",
922 ca[res].ca_name, idx, fldno + 1);
923 if (idx < caflds[res])
924 return gripe("Duplicate header %s(%d) in field %d",
925 ca[res].ca_name, idx, fldno + 1);
926 if (idx > caflds[res])
927 return gripe("Expected header %s(%d) in field %d",
928 ca[res].ca_name, caflds[res], fldno + 1);
931 return gripe("Header %s doesn't take an index in field %d",
932 ca[res].ca_name, fldno + 1);
935 return gripe("Duplicate header %s in field %d",
936 ca[res].ca_name, fldno + 1);
938 fldca[fldno] = &ca[res];
945 * Check fields in xdump are sane.
946 * Return 0 on success, -1 on error.
951 struct castr *ca = ef_cadef(cur_type);
952 int i, len, cafldsmax, res = 0;
955 return res; /* table is split, another part expected */
957 /* Check for missing fields */
958 for (i = 0; ca[i].ca_name; i++) {
959 cafldsmax = MAX(caflds[i], cafldspp[i]);
960 if (ca[i].ca_flags & NSC_EXTRA)
962 len = CA_ARRAY_LEN(&ca[i]);
963 if (!len && !cafldsmax)
964 res = gripe("Header field %s missing", ca[i].ca_name);
965 else if (len && cafldsmax == len - 1)
966 res = gripe("Header field %s(%d) missing",
967 ca[i].ca_name, len - 1);
968 else if (len && cafldsmax < len - 1)
969 res = gripe("Header fields %s(%d) ... %s(%d) missing",
970 ca[i].ca_name, cafldsmax, ca[i].ca_name, len - 1);
977 * Set value of field FLDNO in current row to DBL.
978 * Return 1 on success, -1 on error.
981 setnum(int fldno, double dbl)
983 if (!getfld(fldno, NULL))
985 fldval[fldno].val_cat = NSC_VAL;
986 fldval[fldno].val_type = NSC_DOUBLE;
987 fldval[fldno].val_as.dbl = dbl;
992 * Set value of field FLDNO in current row to STR.
993 * Return 1 on success, -1 on error.
996 setstr(int fldno, char *str)
998 if (!getfld(fldno, NULL))
1000 fldval[fldno].val_cat = NSC_VAL;
1001 fldval[fldno].val_type = NSC_STRING;
1002 fldval[fldno].val_as.str.base = str ? strdup(str) : NULL;
1003 fldval[fldno].val_as.str.maxsz = INT_MAX;
1004 /* really SIZE_MAX, but that's C99 */
1009 * Resolve symbol name ID in table referred to by CA.
1010 * Use field number N for error messages.
1011 * Return index in referred table on success, -1 on failure.
1014 xunsymbol(char *id, struct castr *ca, int n)
1016 int i = ef_elt_byname(ca->ca_table, id);
1018 return gripe("%s %s symbol `%s' in field %d",
1019 i == M_NOTUNIQUE ? "Ambiguous" : "Unknown",
1020 ca->ca_name, id, n + 1);
1025 * Map symbol index to symbol value.
1026 * CA is the table, and I is the index in it.
1029 symval(struct castr *ca, int i)
1031 int type = ca->ca_table;
1033 if (type != EF_BAD && ef_cadef(type) == symbol_ca)
1034 /* symbol table, value is in the table */
1035 return ((struct symbol *)ef_ptr(type, i))->value;
1036 /* value is the table index */
1041 * Set value of field FLDNO in current object to value of symbol SYM.
1042 * Return 1 on success, -1 on error.
1045 setsym(int fldno, char *sym)
1050 ca = getfld(fldno, NULL);
1054 if (ca->ca_table == EF_BAD || (ca->ca_flags & NSC_BITS))
1055 return gripe("Field %d doesn't take symbols", fldno + 1);
1057 i = xunsymbol(sym, ca, fldno);
1060 return setnum(fldno, symval(ca, i));
1064 * Create an empty symbol set for field FLDNO in *SET.
1065 * Return 1 on success, -1 on error.
1068 mtsymset(int fldno, long *set)
1072 ca = getfld(fldno, NULL);
1076 if (ca->ca_table == EF_BAD || ef_cadef(ca->ca_table) != symbol_ca
1077 || !(ca->ca_flags & NSC_BITS))
1078 return gripe("Field %d doesn't take symbol sets", fldno + 1);
1084 * Add a symbol to a symbol set for field FLDNO in *SET.
1085 * SYM is the name of the symbol to add.
1086 * Return 1 on success, -1 on error.
1089 add2symset(int fldno, long *set, char *sym)
1094 ca = getfld(fldno, NULL);
1098 i = xunsymbol(sym, ca, fldno);
1101 *set |= symval(ca, i);
1106 * Read an xdump table header line from FP.
1107 * Expect header for EXPECTED_TABLE, unless it is EF_BAD.
1108 * Recognize header for machine- and human-readable syntax, and set
1109 * human accordingly.
1110 * Return table type on success, -2 on EOF before header, -1 on failure.
1113 xuheader(FILE *fp, int expected_table)
1119 while ((ch = skipfs(fp)) == '\n')
1121 if (ch == EOF && expected_table == EF_BAD)
1128 ? fscanf(fp, "config%*[ \t]%63[^ \t#\n]%n", name, &res) != 1
1129 : fscanf(fp, "XDUMP%*[ \t]%63[^ \t#\n]%*[ \t]%*[^ \t#\n]%n",
1130 name, &res) != 1) || res < 0)
1131 return gripe("Expected xdump header");
1133 type = ef_byname(name);
1135 return gripe("Unknown table `%s'", name);
1136 if (expected_table != EF_BAD && expected_table != type)
1137 return gripe("Expected table `%s', not `%s'",
1138 ef_nameof(expected_table), name);
1140 if (!empfile[type].file
1141 || !ef_cadef(type) || !(ef_flags(type) & EFF_MEM)) {
1142 CANT_HAPPEN(expected_table != EF_BAD);
1143 return gripe("Table `%s' is not permitted here", name);
1146 if (skipfs(fp) != '\n')
1147 return gripe("Junk after xdump header");
1154 * Find fields in this xdump.
1155 * If reading human-readable syntax, read a field header line from FP.
1156 * Else take fields from the table's selectors in CA[].
1157 * Set ellipsis, nflds, fldca[], fldidx[] and caflds[] accordingly.
1158 * Return 0 on success, -1 on failure.
1161 xufldhdr(FILE *fp, struct castr ca[])
1167 for (i = 0; ca[i].ca_name; i++)
1172 while ((ch = skipfs(fp)) == '\n')
1175 if (xuflds(fp, xufldname) < 0)
1181 for (i = 0; ca[i].ca_name; i++) {
1182 if ((ca[i].ca_flags & NSC_EXTRA))
1184 n = CA_ARRAY_LEN(&ca[i]);
1192 nflds = fidx - fldidx;
1199 * Read xdump footer from FP.
1200 * CA[] contains the table's selectors.
1201 * The body had RECS records.
1202 * Update cafldspp[] from caflds[].
1203 * Return 0 on success, -1 on failure.
1206 xufooter(FILE *fp, struct castr ca[], int recs)
1212 if (fscanf(fp, "config%n", &res) != 0 || res < 0)
1213 return gripe("Malformed table footer");
1215 if (fscanf(fp, "%d", &n) != 1)
1216 return gripe("Malformed table footer");
1218 return gripe("Read %d rows, which doesn't match footer "
1219 "%d rows", recs, n);
1221 if (skipfs(fp) != '\n')
1222 return gripe("Junk after table footer");
1223 if (tbl_part_done() < 0)
1227 for (i = 0; ca[i].ca_name; i++) {
1228 if (cafldspp[i] < caflds[i])
1229 cafldspp[i] = caflds[i];
1236 * Read an xdump table from FP.
1237 * Both machine- and human-readable xdump syntax are recognized.
1238 * Expect table EXPECTED_TABLE, unless it is EF_BAD.
1239 * Report errors to stderr.
1240 * Messages assume FP starts in the file FILE at line *PLNO.
1241 * Update *PLNO to reflect lines read from FP.
1242 * Return table type on success, -2 on EOF before header, -1 on failure.
1245 xundump(FILE *fp, char *file, int *plno, int expected_table)
1248 int type, nca, nf, i, ch;
1253 if ((type = xuheader(fp, expected_table)) < 0)
1256 ca = ef_cadef(type);
1257 if (CANT_HAPPEN(!ca))
1261 for (i = 0; ca[i].ca_name; i++) {
1263 if (!(ca[i].ca_flags & NSC_EXTRA))
1264 nf += MAX(1, CA_ARRAY_LEN(&ca[i]));
1266 fldca = malloc(nf * sizeof(*fldca));
1267 fldidx = malloc(nf * sizeof(*fldidx));
1268 fldval = malloc(nf * sizeof(*fldval));
1269 caflds = malloc(nca * sizeof(*caflds));
1270 cafldspp = calloc(nca, sizeof(*cafldspp));
1273 if (xutail(fp, ca) < 0)
1283 /* Skip empty lines so that callers can easily check for EOF */
1284 while ((ch = skipfs(fp)) == '\n')
1293 * Read the remainder of an xdump after the table header line from FP.
1294 * CA[] contains the table's selectors.
1295 * Return 0 on success, -1 on failure.
1298 xutail(FILE *fp, struct castr *ca)
1303 if (xufldhdr(fp, ca) < 0)
1305 if ((recs = xubody(fp)) < 0)
1307 if (xufooter(fp, ca, recs) < 0)
1311 if (xuheader(fp, cur_type) < 0)
1317 * Read the body of an xdump table from FP.
1318 * Return number of rows read on success, -1 on failure.
1326 while ((ch = skipfs(fp)) == '\n')
1331 if (xuflds(fp, xufld) < 0)