2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2015, 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 * Is table TYPE's ID-th record OBJ redundant for xundump()
142 xundump_redundant(int type, int id, void *obj)
144 char buf[EF_WITH_CADEF_MAX_ENTRY_SIZE];
146 if (!can_fill_gaps(type))
149 if (may_truncate(type) && id == ef_nelem(type) - 1)
152 ef_blank(type, id, buf);
153 if (ef_flags(type) & EFF_TYPED)
154 return ef_typedstr_eq((struct ef_typedstr *)buf,
155 (struct ef_typedstr *)obj);
156 return !memcmp(obj, buf, empfile[type].size);
160 * Gripe about the current line to stderr, return -1.
163 gripe(char *fmt, ...)
167 fprintf(stderr, "%s:%d: ", fname, lineno);
169 vfprintf(stderr, fmt, ap);
176 /* Make TYPE the current table. */
183 old_nelem = type == EF_BAD ? 0 : ef_nelem(type);
188 /* End the current table. */
197 * Seek to current table's ID-th object.
198 * Extend the table if necessary.
200 * Return the object on success, NULL on failure.
207 if (id >= ef_nelem(cur_type)) {
208 if (!ef_ensure_space(cur_type, id, 1)) {
209 gripe("can't grow table to hold this row");
214 obj = ef_ptr(cur_type, id);
215 if (CANT_HAPPEN(!obj))
223 * Reset the omitted objects to default state.
226 omit_ids(int id1, int id2)
233 idgap = realloc(idgap, id2 * sizeof(*idgap));
234 for (i = idgap_len; i < id1; i++)
236 for (i = id1; i < id2; i++) {
237 ef_blank(cur_type, i, ef_ptr(cur_type, i));
244 * Return the smallest non-omitted ID in ID1..ID2-1 if any, else -1.
247 expected_id(int id1, int id2)
251 for (i = id1; i < id2; i++) {
252 if (i >= idgap_len || !idgap[i])
260 * If the table has variable length, truncate it.
261 * Else ensure we're omitting the same objects as the previous parts.
262 * Reset any omitted objects to default state.
263 * Return 0 on success, -1 on failure.
268 struct empfile *ep = &empfile[cur_type];
270 if (cur_id + 1 < ep->fids) {
272 if (may_truncate(cur_type)) {
273 if (!ef_truncate(cur_type, cur_id + 1))
276 if (!can_fill_gaps(cur_type))
277 return gripe("expected %d more rows",
278 ep->fids - (cur_id + 1));
279 omit_ids(cur_id + 1, ep->fids);
282 if (expected_id(cur_id + 1, ep->fids) >= 0)
283 return gripe("table's first part has more rows");
293 * Get selector for field FLDNO.
294 * Assign the field's selector index to *IDX, unless it is null.
295 * Return the selector on success, null pointer on error.
297 static struct castr *
298 getfld(int fldno, int *idx)
300 if (fldno >= nflds) {
301 gripe("too many fields, expected only %d", nflds);
304 if (CANT_HAPPEN(fldno < 0))
307 *idx = fldidx[fldno];
312 * Find the field for selector CA with index IDX.
313 * Return the field number if it exists, else -1.
316 fld_find(struct castr *ca, int idx)
320 for (i = 0; i < nflds; i++) {
321 if (fldca[i] == ca && fldidx[i] == idx)
328 * Get the current row's ID.
329 * Current table's 0-th selector must be a usable ID.
330 * Return ID on success, -1 on failure.
335 struct castr *ca = ef_cadef(cur_type);
336 int fldno, id, max_id;
338 if (CANT_HAPPEN(partno != 0 || !ca0_is_id(cur_type)))
341 fldno = fld_find(ca, 0);
343 return cur_id + 1; /* ID not specified */
345 * Field values not representable as int will be rejected by
346 * putnum() or putstr(). Leave the error reporting to them, and
347 * simply pick the next ID here.
349 if (fldval[fldno].val_type != NSC_DOUBLE)
351 id = fldval[fldno].val_as.dbl;
352 if (id != fldval[fldno].val_as.dbl)
355 if (id != cur_id + 1 && !can_fill_gaps(cur_type))
356 return gripe("expected %d in field %d",
357 cur_id + 1, fldno + 1);
359 return gripe("field %d must be > %d", fldno + 1, cur_id);
360 max_id = ef_id_limit(cur_type);
362 return gripe("field %d must be <= %d", fldno + 1, max_id);
368 * Find the field NAME with index IDX and value representable as long.
369 * Return the field number if it exists, else -1.
372 fld_find_long_by_name(char *name, int idx)
376 for (i = 0; i < nflds; i++) {
377 if (!strcmp(fldca[i]->ca_name, name) && fldidx[i] == idx)
381 if (i == nflds || fldval[i].val_type != NSC_DOUBLE
382 || (long)fldval[i].val_as.dbl != fldval[i].val_as.dbl)
388 * Get the current row's ID.
389 * Current table's type must be EF_SECTOR.
390 * Return ID on success, -1 on failure.
395 int fldno_x, fldno_y, id;
398 if (CANT_HAPPEN(partno != 0 || cur_type != EF_SECTOR))
401 fldno_x = fld_find_long_by_name("xloc", 0);
402 fldno_y = fld_find_long_by_name("yloc", 0);
403 if (fldno_x < 0 || fldno_y < 0)
406 id = sctoff((long)fldval[fldno_x].val_as.dbl,
407 (long)fldval[fldno_y].val_as.dbl);
408 /* Note: reporting values out of range left to putnum() */
410 sctoff2xy(&x, &y, cur_id);
411 return gripe("coordinates in fields %d,%d must be > %d,%d",
412 fldno_x + 1, fldno_y + 1, x, y);
418 * Get the current row's ID.
419 * Current table's type must be EF_REALM.
420 * Return ID on success, -1 on failure.
425 int fldno_cnum, fldno_realm, id;
428 if (CANT_HAPPEN(partno != 0 || cur_type != EF_REALM))
431 fldno_cnum = fld_find_long_by_name("cnum", 0);
432 fldno_realm = fld_find_long_by_name("realm", 0);
433 if (fldno_cnum < 0 || fldno_realm < 0)
436 realm = (long)fldval[fldno_realm].val_as.dbl;
437 cnum = (long)fldval[fldno_cnum].val_as.dbl;
438 if (cnum < 0 || cnum >= MAXNOC)
439 return gripe("field %d must be between 0 and %d",
441 if (realm < 0 || realm >= MAXNOR)
442 return gripe("field %d must be between 0 and %d",
443 fldno_realm, MAXNOR);
444 id = realm + cnum * MAXNOR;
446 return gripe("fields %d,%d must be > (%d,%d)",
447 fldno_cnum + 1, fldno_realm + 1,
448 cur_id / MAXNOR, cur_id % MAXNOR);
452 * Get the current row's object.
453 * Extend the table if necessary.
455 * Return the object on success, NULL on failure.
460 int last_id = cur_id;
465 id = expected_id(cur_id + 1, empfile[cur_type].fids);
467 gripe("table's first part doesn't have this row");
470 } else if (ca0_is_id(cur_type)) {
474 } else if (cur_type == EF_SECTOR) {
478 } else if (cur_type == EF_REALM) {
484 if (id > ef_id_limit(cur_type)) {
485 gripe("too many rows");
491 omit_ids(last_id + 1, id);
496 * Is a new value for field FLDNO required to match the old one?
499 fldval_must_match(int fldno)
501 struct castr *ca = ef_cadef(cur_type);
502 int i = fldca[fldno] - ca;
505 * Value must match if:
506 * it's for a const selector, unless the object is still blank, or
507 * it was already given in a previous part of a split table.
509 return (cur_id < old_nelem && (fldca[fldno]->ca_flags & NSC_CONST))
510 || fldidx[fldno] < cafldspp[i];
514 * Set OBJ's field FLDNO to DBL.
515 * Return 0 on success, -1 on error.
518 putnum(void *obj, int fldno, double dbl)
520 struct castr *ca = fldca[fldno];
521 int idx = fldidx[fldno];
525 memb_ptr = (char *)obj + ca->ca_off;
527 switch (ca->ca_type) {
529 old = ((signed char *)memb_ptr)[idx];
530 new = ((signed char *)memb_ptr)[idx] = (signed char)dbl;
533 old = ((unsigned char *)memb_ptr)[idx];
534 new = ((unsigned char *)memb_ptr)[idx] = (unsigned char)dbl;
537 old = ((short *)memb_ptr)[idx];
538 new = ((short *)memb_ptr)[idx] = (short)dbl;
541 old = ((unsigned short *)memb_ptr)[idx];
542 new = ((unsigned short *)memb_ptr)[idx] = (unsigned short)dbl;
545 old = ((int *)memb_ptr)[idx];
546 new = ((int *)memb_ptr)[idx] = (int)dbl;
549 old = ((long *)memb_ptr)[idx];
550 new = ((long *)memb_ptr)[idx] = (long)dbl;
553 old = ((coord *)memb_ptr)[idx];
554 /* FIXME use variant of xrel() that takes orig instead of nation */
555 if (old >= WORLD_X / 2)
557 new = ((coord *)memb_ptr)[idx] = XNORM((coord)dbl);
558 if (new >= WORLD_X / 2)
562 old = ((coord *)memb_ptr)[idx];
563 /* FIXME use variant of yrel() that takes orig instead of nation */
564 if (old >= WORLD_Y / 2)
566 new = ((coord *)memb_ptr)[idx] = YNORM((coord)dbl);
567 if (new >= WORLD_Y / 2)
571 old = ((float *)memb_ptr)[idx];
572 ((float *)memb_ptr)[idx] = (float)dbl;
573 new = dbl; /* suppress new != dbl check */
576 old = ((double *)memb_ptr)[idx];
577 ((double *)memb_ptr)[idx] = dbl;
578 new = dbl; /* suppress new != dbl check */
581 old = ((time_t *)memb_ptr)[idx];
582 new = ((time_t *)memb_ptr)[idx] = (time_t)dbl;
585 return gripe("field %d doesn't take numbers", fldno + 1);
588 if (fldval_must_match(fldno) && old != dbl)
589 return gripe("value for field %d must be %g", fldno + 1, old);
591 return gripe("field %d can't hold this value", fldno + 1);
597 * Set obj's field FLDNO to STR.
598 * Return 0 on success, -1 on error.
601 putstr(void *obj, int fldno, char *str)
603 struct castr *ca = fldca[fldno];
604 int idx = fldidx[fldno];
605 int must_match, mismatch;
607 char *memb_ptr, *old;
609 memb_ptr = (char *)obj + ca->ca_off;
610 must_match = fldval_must_match(fldno);
613 switch (ca->ca_type) {
615 old = ((char **)memb_ptr)[idx];
617 mismatch = old ? !str || strcmp(old, str) : !!str;
619 /* FIXME may leak old value */
620 ((char **)memb_ptr)[idx] = str ? strdup(str) : NULL;
621 len = -1; /* unlimited */
624 if (CANT_HAPPEN(idx))
627 return gripe("field %d doesn't take nil", fldno + 1);
628 /* Wart: if ca_len <= 1, the terminating null may be omitted */
630 len = sz > 1 ? sz - 1 : sz;
631 if (strlen(str) > len)
632 return gripe("field %d takes at most %d characters",
633 fldno + 1, (int)len);
636 mismatch = !str || strncmp(old, str, len);
638 strncpy(memb_ptr, str, sz);
641 return gripe("field %d doesn't take strings", fldno + 1);
646 return gripe("value for field %d must be \"%.*s\"",
647 fldno + 1, (int)len, old);
649 return gripe("value for field %d must be nil", fldno + 1);
656 * Save the current row's fields in its object.
657 * Return 0 on success, -1 on failure.
669 for (i = 0; i < nflds; i++) {
670 switch (fldval[i].val_type) {
672 ret |= putnum(obj, i, fldval[i].val_as.dbl);
675 ret |= putstr(obj, i, fldval[i].val_as.str.base);
676 free(fldval[i].val_as.str.base);
688 * Read and ignore field separators from FP.
689 * Return first character that is not a field separator.
698 } while (ch == ' ' || ch == '\t');
703 } while (ch != EOF && ch != '\n');
710 * Decode escape sequences in BUF.
711 * Return BUF on success, null pointer on failure.
723 if (sscanf(++src, "%3o%n", &octal_chr, &n) != 1 || n != 3)
725 *dst++ = (char)octal_chr;
735 * Read an identifier from FP into BUF.
736 * BUF must have space for 1024 characters.
737 * Return number of characters read on success, -1 on failure.
740 getid(FILE *fp, char *buf)
743 if (fscanf(fp, "%1023[^\"#()<>= \t\n]%n", buf, &n) != 1
751 * Try to read a field name from FP.
752 * I is the field number, counting from zero.
753 * If a name is read, set fldca[I] and fldidx[I] for it, and update
755 * Return 1 if a name or ... was read, 0 on end of line, -1 on error.
758 xufldname(FILE *fp, int i)
766 return gripe("unexpected EOF");
768 nflds = i - (ellipsis != 0);
774 if (getc(fp) != '.' || getc(fp) != '.')
775 return gripe("junk in header field %d", i + 1);
777 return gripe("header fields expected");
780 if (ch != EOF && ch != '\n')
781 return gripe("junk after ...");
786 if (getid(fp, buf) < 0)
787 return gripe("junk in header field %d", i + 1);
791 return deffld(i, buf, -1);
795 if (isdigit(ch) || ch == '-' || ch == '+') {
796 if (fscanf(fp, "%d", &idx) != 1)
797 return gripe("malformed number in index of header field %d",
800 return gripe("index must not be negative in header field %d",
803 if (getid(fp, buf) < 0)
804 return gripe("malformed index in header field %d", i + 1);
805 return gripe("symbolic index in header field %d not yet implemented",
810 return gripe("malformed index in header field %d", i + 1);
811 return deffld(i, buf, idx);
816 * Try to read a field value from FP.
817 * I is the field number, counting from zero.
818 * Return 1 if a value was read, 0 on end of line, -1 on error.
821 xufld(FILE *fp, int i)
831 return gripe("unexpected EOF");
833 CANT_HAPPEN(i > nflds);
834 for (j = i; j < nflds; j++) {
835 if (CA_IS_ARRAY(fldca[j]))
836 gripe("field '%s(%d)' missing",
837 fldca[j]->ca_name, fldidx[j]);
839 gripe("field '%s' missing", fldca[j]->ca_name);
841 if (i != nflds || putrow() < 0)
844 return i < nflds ? -1 : 0;
845 case '+': case '-': case '.':
846 case '0': case '1': case '2': case '3': case '4':
847 case '5': case '6': case '7': case '8': case '9':
849 if (fscanf(fp, "%lg", &dbl) != 1)
850 return gripe("malformed number in field %d", i + 1);
851 return setnum(i, dbl);
858 if (fscanf(fp, "%1023[^\"\n]", buf) != 1 || getc(fp) != '"')
859 return gripe("malformed string in field %d", i + 1);
861 return gripe("invalid escape sequence in field %d",
864 return setstr(i, buf);
866 if (mtsymset(i, &set) < 0)
870 if (ch == EOF || ch == '\n')
871 return gripe("unmatched '(' in field %d", i + 1);
875 if (getid(fp, buf) < 0)
876 return gripe("junk in field %d", i + 1);
877 if (add2symset(i, &set, buf) < 0)
880 return setnum(i, set);
883 if (getid(fp, buf) < 0)
884 return gripe("junk in field %d", i + 1);
885 if (!strcmp(buf, "nil"))
886 return setstr(i, NULL);
888 return setsym(i, buf);
893 * Read fields from FP.
894 * Use PARSE() to read each field.
895 * Return number of fields read on success, -1 on error.
898 xuflds(FILE *fp, int (*parse)(FILE *, int))
911 else if (ch != ' ' && ch != '\t')
912 return gripe("bad field separator after field %d", i + 1);
917 * Define the FLDNO-th field.
918 * If IDX is negative, define as selector NAME, else as NAME(IDX).
919 * Set fldca[FLDNO] and fldidx[FLDNO] accordingly.
921 * Return 1 on success, -1 on error.
924 deffld(int fldno, char *name, int idx)
926 struct castr *ca = ef_cadef(cur_type);
929 res = stmtch(name, ca, offsetof(struct castr, ca_name),
930 sizeof(struct castr));
932 return gripe("%s header '%s' in field %d",
933 res == M_NOTUNIQUE ? "ambiguous" : "unknown",
935 if ((ca[res].ca_flags & NSC_EXTRA) || CANT_HAPPEN(ca[res].ca_get))
936 return gripe("extraneous header '%s' in field %d", name, fldno + 1);
937 if (CA_IS_ARRAY(&ca[res])) {
939 return gripe("header '%s' requires an index in field %d",
940 ca[res].ca_name, fldno + 1);
941 if (idx != caflds[res] && idx < ca[res].ca_len)
942 return gripe("expected header '%s(%d)' in field %d",
943 ca[res].ca_name, caflds[res], fldno + 1);
944 if (idx >= ca[res].ca_len)
945 return gripe("unexpected header '%s(%d)' in field %d",
946 ca[res].ca_name, idx, fldno + 1);
949 return gripe("header '%s' doesn't take an index in field %d",
950 ca[res].ca_name, fldno + 1);
953 return gripe("duplicate header '%s' in field %d",
954 ca[res].ca_name, fldno + 1);
956 fldca[fldno] = &ca[res];
963 * Check fields in xdump are sane.
964 * Return 0 on success, -1 on error.
969 struct castr *ca = ef_cadef(cur_type);
970 int i, len, cafldsmax, res = 0;
973 return res; /* table is split, another part expected */
975 /* Check for missing fields */
976 for (i = 0; ca[i].ca_name; i++) {
977 cafldsmax = MAX(caflds[i], cafldspp[i]);
978 if (ca[i].ca_flags & NSC_EXTRA)
980 len = CA_ARRAY_LEN(&ca[i]);
981 if (!len && !cafldsmax)
982 res = gripe("header '%s' missing", ca[i].ca_name);
983 else if (len && cafldsmax == len - 1)
984 res = gripe("header '%s(%d)' missing",
985 ca[i].ca_name, len - 1);
986 else if (len && cafldsmax < len - 1)
987 res = gripe("header '%s(%d)' ... '%s(%d)' missing",
988 ca[i].ca_name, cafldsmax, ca[i].ca_name, len - 1);
995 * Set value of field FLDNO in current row to DBL.
996 * Return 1 on success, -1 on error.
999 setnum(int fldno, double dbl)
1001 if (!getfld(fldno, NULL))
1003 fldval[fldno].val_cat = NSC_VAL;
1004 fldval[fldno].val_type = NSC_DOUBLE;
1005 fldval[fldno].val_as.dbl = dbl;
1010 * Set value of field FLDNO in current row to STR.
1011 * Return 1 on success, -1 on error.
1014 setstr(int fldno, char *str)
1016 if (!getfld(fldno, NULL))
1018 fldval[fldno].val_cat = NSC_VAL;
1019 fldval[fldno].val_type = NSC_STRING;
1020 fldval[fldno].val_as.str.base = str ? strdup(str) : NULL;
1021 fldval[fldno].val_as.str.maxsz = INT_MAX;
1022 /* really SIZE_MAX, but that's C99 */
1027 * Resolve symbol name ID in table referred to by CA.
1028 * Use field number N for error messages.
1029 * Return index in referred table on success, -1 on failure.
1032 xunsymbol(char *id, struct castr *ca, int n)
1034 int i = ef_elt_byname(ca->ca_table, id);
1036 return gripe("%s %s symbol '%s' in field %d",
1037 i == M_NOTUNIQUE ? "ambiguous" : "unknown",
1038 ca->ca_name, id, n + 1);
1043 * Map symbol index to symbol value.
1044 * CA is the table, and I is the index in it.
1047 symval(struct castr *ca, int i)
1049 int type = ca->ca_table;
1051 if (type != EF_BAD && ef_cadef(type) == symbol_ca)
1052 /* symbol table, value is in the table */
1053 return ((struct symbol *)ef_ptr(type, i))->value;
1054 /* value is the table index */
1059 * Set value of field FLDNO in current object to value of symbol SYM.
1060 * Return 1 on success, -1 on error.
1063 setsym(int fldno, char *sym)
1068 ca = getfld(fldno, NULL);
1072 if (ca->ca_table == EF_BAD || (ca->ca_flags & NSC_BITS))
1073 return gripe("field %d doesn't take symbols", fldno + 1);
1075 i = xunsymbol(sym, ca, fldno);
1078 return setnum(fldno, symval(ca, i));
1082 * Create an empty symbol set for field FLDNO in *SET.
1083 * Return 1 on success, -1 on error.
1086 mtsymset(int fldno, long *set)
1090 ca = getfld(fldno, NULL);
1094 if (ca->ca_table == EF_BAD || ef_cadef(ca->ca_table) != symbol_ca
1095 || !(ca->ca_flags & NSC_BITS))
1096 return gripe("field %d doesn't take symbol sets", fldno + 1);
1102 * Add a symbol to a symbol set for field FLDNO in *SET.
1103 * SYM is the name of the symbol to add.
1104 * Return 1 on success, -1 on error.
1107 add2symset(int fldno, long *set, char *sym)
1112 ca = getfld(fldno, NULL);
1116 i = xunsymbol(sym, ca, fldno);
1119 *set |= symval(ca, i);
1124 * Read an xdump table header line from FP.
1125 * Expect header for EXPECTED_TABLE, unless it is EF_BAD.
1126 * Recognize header for machine- and human-readable syntax, and set
1127 * human accordingly.
1128 * Return table type on success, -2 on EOF before header, -1 on failure.
1131 xuheader(FILE *fp, int expected_table)
1137 while ((ch = skipfs(fp)) == '\n')
1139 if (ch == EOF && expected_table == EF_BAD)
1146 ? fscanf(fp, "config%*[ \t]%63[^ \t#\n]%n", name, &res) != 1
1147 : fscanf(fp, "XDUMP%*[ \t]%63[^ \t#\n]%*[ \t]%*[^ \t#\n]%n",
1148 name, &res) != 1) || res < 0)
1149 return gripe("expected xdump header");
1151 type = ef_byname(name);
1153 return gripe("unknown table '%s'", name);
1154 if (expected_table != EF_BAD && expected_table != type)
1155 return gripe("expected table '%s', not '%s'",
1156 ef_nameof(expected_table), name);
1158 if (!empfile[type].file
1159 || !ef_cadef(type) || !(ef_flags(type) & EFF_MEM)) {
1160 CANT_HAPPEN(expected_table != EF_BAD);
1161 return gripe("table '%s' is not permitted here", name);
1164 if (skipfs(fp) != '\n')
1165 return gripe("junk after xdump header");
1172 * Find fields in this xdump.
1173 * If reading human-readable syntax, read a field header line from FP.
1174 * Else take fields from the table's selectors in CA[].
1175 * Set ellipsis, nflds, fldca[], fldidx[] and caflds[] accordingly.
1176 * Return 0 on success, -1 on failure.
1179 xufldhdr(FILE *fp, struct castr ca[])
1185 for (i = 0; ca[i].ca_name; i++)
1190 while ((ch = skipfs(fp)) == '\n')
1193 if (xuflds(fp, xufldname) < 0)
1199 for (i = 0; ca[i].ca_name; i++) {
1200 if ((ca[i].ca_flags & NSC_EXTRA))
1202 n = CA_ARRAY_LEN(&ca[i]);
1210 nflds = fidx - fldidx;
1217 * Read xdump footer from FP.
1218 * CA[] contains the table's selectors.
1219 * The body had RECS records.
1220 * Update cafldspp[] from caflds[].
1221 * Return 0 on success, -1 on failure.
1224 xufooter(FILE *fp, struct castr ca[], int recs)
1230 if (fscanf(fp, "config%n", &res) != 0 || res < 0)
1231 return gripe("malformed table footer");
1233 if (fscanf(fp, "%d", &n) != 1)
1234 return gripe("malformed table footer");
1236 return gripe("expected footer /%d", recs);
1238 if (skipfs(fp) != '\n')
1239 return gripe("junk after table footer");
1240 if (tbl_part_done() < 0)
1244 for (i = 0; ca[i].ca_name; i++) {
1245 if (cafldspp[i] < caflds[i])
1246 cafldspp[i] = caflds[i];
1253 * Read an xdump table from FP.
1254 * Both machine- and human-readable xdump syntax are recognized.
1255 * Expect table EXPECTED_TABLE, unless it is EF_BAD.
1256 * Report errors to stderr.
1257 * Messages assume FP starts in the file FILE at line *PLNO.
1258 * Update *PLNO to reflect lines read from FP.
1259 * Return table type on success, -2 on EOF before header, -1 on failure.
1262 xundump(FILE *fp, char *file, int *plno, int expected_table)
1265 int type, nca, nf, i, ch;
1270 if ((type = xuheader(fp, expected_table)) < 0)
1273 ca = ef_cadef(type);
1274 if (CANT_HAPPEN(!ca))
1278 for (i = 0; ca[i].ca_name; i++) {
1280 if (!(ca[i].ca_flags & NSC_EXTRA))
1281 nf += MAX(1, CA_ARRAY_LEN(&ca[i]));
1283 fldca = malloc(nf * sizeof(*fldca));
1284 fldidx = malloc(nf * sizeof(*fldidx));
1285 fldval = malloc(nf * sizeof(*fldval));
1286 caflds = malloc(nca * sizeof(*caflds));
1287 cafldspp = calloc(nca, sizeof(*cafldspp));
1290 if (xutail(fp, ca) < 0)
1300 /* Skip empty lines so that callers can easily check for EOF */
1301 while ((ch = skipfs(fp)) == '\n')
1310 * Read the remainder of an xdump after the table header line from FP.
1311 * CA[] contains the table's selectors.
1312 * Return 0 on success, -1 on failure.
1315 xutail(FILE *fp, struct castr *ca)
1320 if (xufldhdr(fp, ca) < 0)
1322 if ((recs = xubody(fp)) < 0)
1324 if (xufooter(fp, ca, recs) < 0)
1328 if (xuheader(fp, cur_type) < 0)
1334 * Read the body of an xdump table from FP.
1335 * Return number of rows read on success, -1 on failure.
1343 while ((ch = skipfs(fp)) == '\n')
1348 if (xuflds(fp, xufld) < 0)