]> git.pond.sub.org Git - empserver/blobdiff - src/lib/common/xundump.c
Update copyright notice
[empserver] / src / lib / common / xundump.c
index 99ceef4bcc64108eb86eac2b06172055862a2fc2..82e08b9b43e8dd02e9f7097f410881d1d7f9841d 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2014, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2020, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                Ken Stevens, Steve McClure, Markus Armbruster
  *
  *  Empire is free software: you can redistribute it and/or modify
@@ -28,7 +28,7 @@
  *
  *  Known contributors to this file:
  *     Ron Koenderink, 2005
- *     Markus Armbruster, 2005-2014
+ *     Markus Armbruster, 2005-2016
  */
 
 /*
@@ -58,7 +58,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
-#include "file.h"
 #include "match.h"
 #include "nat.h"
 #include "nsc.h"
@@ -96,7 +95,7 @@ static int xubody(FILE *);
 static int xutail(FILE *, struct castr *);
 
 /*
- * Does the code hardcode indexes for table TYPE?
+ * Does the code hardcode indexes for table @type?
  */
 static int
 have_hardcoded_indexes(int type)
@@ -106,7 +105,7 @@ have_hardcoded_indexes(int type)
 }
 
 /*
- * Okay to truncate table TYPE?
+ * Okay to truncate table @type?
  */
 static int
 may_truncate(int type)
@@ -115,18 +114,18 @@ may_truncate(int type)
 }
 
 /*
- * Is TYPE's 0-th selector a usable ID?
+ * Is @type's 0-th selector a usable ID?
  */
 static int
 ca0_is_id(int type)
 {
     struct castr *ca = ef_cadef(type);
 
-    return ca[0].ca_table == type && !(ca[0].ca_flags & NSC_EXTRA);
+    return ca[0].ca_table == type && ca[0].ca_dump <= CA_DUMP_CONST;
 }
 
 /*
- * Can we fill in gaps in table TYPE?
+ * Can we fill in gaps in table @type?
  */
 static int
 can_fill_gaps(int type)
@@ -136,7 +135,7 @@ can_fill_gaps(int type)
 }
 
 /*
- * Is table TYPE's ID-th record OBJ redundant for xundump()
+ * Is table @type's @id-th record @obj redundant for xundump()?
  */
 int
 xundump_redundant(int type, int id, void *obj)
@@ -173,7 +172,7 @@ gripe(char *fmt, ...)
     return -1;
 }
 
-/* Make TYPE the current table.  */
+/* Make TYPE the current table. */
 static void
 tbl_start(int type)
 {
@@ -185,7 +184,7 @@ tbl_start(int type)
     idgap_len = 0;
 }
 
-/* End the current table.  */
+/* End the current table. */
 static void
 tbl_end(void)
 {
@@ -194,9 +193,9 @@ tbl_end(void)
 }
 
 /*
- * Seek to current table's ID-th object.
+ * Seek to current table's @id-th object.
  * Extend the table if necessary.
- * Save ID in cur_id.
+ * Save @id in @cur_id.
  * Return the object on success, NULL on failure.
  */
 static void *
@@ -219,7 +218,7 @@ tbl_seek(int id)
 }
 
 /*
- * Omit ID1..ID2-1.
+ * Omit @id1..@id2-1.
  * Reset the omitted objects to default state.
  */
 static void
@@ -241,7 +240,7 @@ omit_ids(int id1, int id2)
 }
 
 /*
- * Return the smallest non-omitted ID in ID1..ID2-1 if any, else -1.
+ * Return the smallest non-omitted ID in @id1..@id2-1 if any, else -1.
  */
 static int
 expected_id(int id1, int id2)
@@ -290,8 +289,8 @@ tbl_part_done(void)
 }
 
 /*
- * Get selector for field FLDNO.
- * Assign the field's selector index to *IDX, unless it is null.
+ * Get selector for field @fldno.
+ * Assign the field's selector index to *@idx, unless it is null.
  * Return the selector on success, null pointer on error.
  */
 static struct castr *
@@ -309,7 +308,7 @@ getfld(int fldno, int *idx)
 }
 
 /*
- * Find the field for selector CA with index IDX.
+ * Find the field for selector @ca with index @idx.
  * Return the field number if it exists, else -1.
  */
 static int
@@ -365,7 +364,7 @@ rowid(void)
 }
 
 /*
- * Find the field NAME with index IDX and value representable as long.
+ * Find the field @name with index @idx and value representable as long.
  * Return the field number if it exists, else -1.
  */
 static int
@@ -451,7 +450,7 @@ rowid_realm(void)
 /*
  * Get the current row's object.
  * Extend the table if necessary.
- * Save ID in cur_id.
+ * Save ID in @cur_id.
  * Return the object on success, NULL on failure.
  */
 static void *
@@ -493,7 +492,7 @@ rowobj(void)
 }
 
 /*
- * Is a new value for field FLDNO required to match the old one?
+ * Is a new value for field @fldno required to match the old one?
  */
 static int
 fldval_must_match(int fldno)
@@ -506,12 +505,12 @@ fldval_must_match(int fldno)
      * it's for a const selector, unless the object is still blank, or
      * it was already given in a previous part of a split table.
      */
-    return (cur_id < old_nelem && (fldca[fldno]->ca_flags & NSC_CONST))
+    return (cur_id < old_nelem && (fldca[fldno]->ca_dump == CA_DUMP_CONST))
        || fldidx[fldno] < cafldspp[i];
 }
 
 /*
- * Set OBJ's field FLDNO to DBL.
+ * Set @obj's field @fldno to @dbl.
  * Return 0 on success, -1 on error.
  */
 static int
@@ -594,7 +593,7 @@ putnum(void *obj, int fldno, double dbl)
 }
 
 /*
- * Set obj's field FLDNO to STR.
+ * Set obj's field @fldno to @str.
  * Return 0 on success, -1 on error.
  */
 static int
@@ -685,7 +684,7 @@ putrow(void)
 }
 
 /*
- * Read and ignore field separators from FP.
+ * Read and ignore field separators from @fp.
  * Return first character that is not a field separator.
  */
 static int
@@ -707,14 +706,15 @@ skipfs(FILE *fp)
 }
 
 /*
- * Decode escape sequences in BUF.
- * Return BUF on success, null pointer on failure.
+ * Decode escape sequences in @buf.
+ * Return @buf on success, null pointer on failure.
  */
 static char *
 xuesc(char *buf)
 {
     char *src, *dst;
-    int octal_chr, n;
+    unsigned octal_chr;
+    int n;
 
     dst = buf;
     src = buf;
@@ -732,8 +732,8 @@ xuesc(char *buf)
 }
 
 /*
- * Read an identifier from FP into BUF.
- * BUF must have space for 1024 characters.
+ * Read an identifier from @fp into @buf.
+ * @buf must have space for 1024 characters.
  * Return number of characters read on success, -1 on failure.
  */
 static int
@@ -748,9 +748,9 @@ getid(FILE *fp, char *buf)
 }
 
 /*
- * Try to read a field name from FP.
- * I is the field number, counting from zero.
- * If a name is read, set fldca[I] and fldidx[I] for it, and update
+ * Try to read a field name from @fp.
+ * @i is the field number, counting from zero.
+ * If a name is read, set fldca[@i] and fldidx[@i] for it, and update
  * caflds[].
  * Return 1 if a name or ... was read, 0 on end of line, -1 on error.
  */
@@ -813,8 +813,8 @@ xufldname(FILE *fp, int i)
 }
 
 /*
- * Try to read a field value from FP.
- * I is the field number, counting from zero.
+ * Try to read a field value from @fp.
+ * @i is the field number, counting from zero.
  * Return 1 if a value was read, 0 on end of line, -1 on error.
  */
 static int
@@ -890,8 +890,8 @@ xufld(FILE *fp, int i)
 }
 
 /*
- * Read fields from FP.
- * Use PARSE() to read each field.
+ * Read fields from @fp.
+ * Use @parse() to read each field.
  * Return number of fields read on success, -1 on error.
  */
 static int
@@ -914,9 +914,9 @@ xuflds(FILE *fp, int (*parse)(FILE *, int))
 }
 
 /*
- * Define the FLDNO-th field.
- * If IDX is negative, define as selector NAME, else as NAME(IDX).
- * Set fldca[FLDNO] and fldidx[FLDNO] accordingly.
+ * Define the @fldno-th field.
+ * If @idx is negative, define as selector @name, else as @name(@idx).
+ * Set fldca[@fldno] and fldidx[@fldno] accordingly.
  * Update caflds[].
  * Return 1 on success, -1 on error.
  */
@@ -932,7 +932,7 @@ deffld(int fldno, char *name, int idx)
        return gripe("%s header '%s' in field %d",
                     res == M_NOTUNIQUE ? "ambiguous" : "unknown",
                     name, fldno + 1);
-    if ((ca[res].ca_flags & NSC_EXTRA) || CANT_HAPPEN(ca[res].ca_get))
+    if (ca[res].ca_dump > CA_DUMP_CONST || CANT_HAPPEN(ca[res].ca_get))
        return gripe("extraneous header '%s' in field %d", name, fldno + 1);
     if (CA_IS_ARRAY(&ca[res])) {
        if (idx < 0)
@@ -975,7 +975,7 @@ chkflds(void)
     /* Check for missing fields */
     for (i = 0; ca[i].ca_name; i++) {
        cafldsmax = MAX(caflds[i], cafldspp[i]);
-       if (ca[i].ca_flags & NSC_EXTRA)
+       if (ca[i].ca_dump > CA_DUMP_CONST)
            continue;
        len = CA_ARRAY_LEN(&ca[i]);
        if (!len && !cafldsmax)
@@ -992,7 +992,7 @@ chkflds(void)
 }
 
 /*
- * Set value of field FLDNO in current row to DBL.
+ * Set value of field @fldno in current row to @dbl.
  * Return 1 on success, -1 on error.
  */
 static int
@@ -1007,7 +1007,7 @@ setnum(int fldno, double dbl)
 }
 
 /*
- * Set value of field FLDNO in current row to STR.
+ * Set value of field @fldno in current row to @str.
  * Return 1 on success, -1 on error.
  */
 static int
@@ -1024,8 +1024,8 @@ setstr(int fldno, char *str)
 }
 
 /*
- * Resolve symbol name ID in table referred to by CA.
- * Use field number N for error messages.
+ * Resolve symbol name @id in table referred to by @ca.
+ * Use field number @n for error messages.
  * Return index in referred table on success, -1 on failure.
  */
 static int
@@ -1041,7 +1041,7 @@ xunsymbol(char *id, struct castr *ca, int n)
 
 /*
  * Map symbol index to symbol value.
- * CA is the table, and I is the index in it.
+ * @ca is the table, and @i is the index in it.
  */
 static int
 symval(struct castr *ca, int i)
@@ -1056,7 +1056,7 @@ symval(struct castr *ca, int i)
 }
 
 /*
- * Set value of field FLDNO in current object to value of symbol SYM.
+ * Set value of field @fldno in current object to value of symbol @sym.
  * Return 1 on success, -1 on error.
  */
 static int
@@ -1079,7 +1079,7 @@ setsym(int fldno, char *sym)
 }
 
 /*
- * Create an empty symbol set for field FLDNO in *SET.
+ * Create an empty symbol set for field @fldno in *@set.
  * Return 1 on success, -1 on error.
  */
 static int
@@ -1099,8 +1099,8 @@ mtsymset(int fldno, long *set)
 }
 
 /*
- * Add a symbol to a symbol set for field FLDNO in *SET.
- * SYM is the name of the symbol to add.
+ * Add a symbol to a symbol set for field @fldno in *@set.
+ * @sym is the name of the symbol to add.
  * Return 1 on success, -1 on error.
  */
 static int
@@ -1121,8 +1121,8 @@ add2symset(int fldno, long *set, char *sym)
 }
 
 /*
- * Read an xdump table header line from FP.
- * Expect header for EXPECTED_TABLE, unless it is EF_BAD.
+ * Read an xdump table header line from @fp.
+ * Expect header for @expected_table, unless it is EF_BAD.
  * Recognize header for machine- and human-readable syntax, and set
  * human accordingly.
  * Return table type on success, -2 on EOF before header, -1 on failure.
@@ -1155,8 +1155,7 @@ xuheader(FILE *fp, int expected_table)
        return gripe("expected table '%s', not '%s'",
                     ef_nameof(expected_table), name);
 
-    if (!empfile[type].file
-       || !ef_cadef(type) || !(ef_flags(type) & EFF_MEM)) {
+    if (!xundumpable(type)) {
        CANT_HAPPEN(expected_table != EF_BAD);
        return gripe("table '%s' is not permitted here", name);
     }
@@ -1168,10 +1167,20 @@ xuheader(FILE *fp, int expected_table)
     return type;
 }
 
+/*
+ * Can table @type be xundumped?
+ */
+int
+xundumpable(int type)
+{
+    return empfile[type].file && ef_cadef(type)
+       && (ef_flags(type) & EFF_MEM);
+}
+
 /*
  * Find fields in this xdump.
- * If reading human-readable syntax, read a field header line from FP.
- * Else take fields from the table's selectors in CA[].
+ * If reading human-readable syntax, read a field header line from @fp.
+ * Else take fields from the table's selectors in @ca[].
  * Set ellipsis, nflds, fldca[], fldidx[] and caflds[] accordingly.
  * Return 0 on success, -1 on failure.
  */
@@ -1197,7 +1206,7 @@ xufldhdr(FILE *fp, struct castr ca[])
        fidx = fldidx;
 
        for (i = 0; ca[i].ca_name; i++) {
-           if ((ca[i].ca_flags & NSC_EXTRA))
+           if (ca[i].ca_dump > CA_DUMP_CONST)
                continue;
            n = CA_ARRAY_LEN(&ca[i]);
            j = 0;
@@ -1214,9 +1223,9 @@ xufldhdr(FILE *fp, struct castr ca[])
 }
 
 /*
- * Read xdump footer from FP.
- * CA[] contains the table's selectors.
- * The body had RECS records.
+ * Read xdump footer from @fp.
+ * @ca[] contains the table's selectors.
+ * The body had @recs records.
  * Update cafldspp[] from caflds[].
  * Return 0 on success, -1 on failure.
  */
@@ -1250,12 +1259,12 @@ xufooter(FILE *fp, struct castr ca[], int recs)
 }
 
 /*
- * Read an xdump table from FP.
+ * Read an xdump table from @fp.
  * Both machine- and human-readable xdump syntax are recognized.
- * Expect table EXPECTED_TABLE, unless it is EF_BAD.
+ * Expect table @expected_table, unless it is EF_BAD.
  * Report errors to stderr.
- * Messages assume FP starts in the file FILE at line *PLNO.
- * Update *PLNO to reflect lines read from FP.
+ * Messages assume @fp starts in the file @file at line *@plno.
+ * Update *@plno to reflect lines read from @fp.
  * Return table type on success, -2 on EOF before header, -1 on failure.
  */
 int
@@ -1277,7 +1286,7 @@ xundump(FILE *fp, char *file, int *plno, int expected_table)
     nca = nf = 0;
     for (i = 0; ca[i].ca_name; i++) {
        nca++;
-       if (!(ca[i].ca_flags & NSC_EXTRA))
+       if (ca[i].ca_dump <= CA_DUMP_CONST)
            nf += MAX(1, CA_ARRAY_LEN(&ca[i]));
     }
     fldca = malloc(nf * sizeof(*fldca));
@@ -1307,8 +1316,8 @@ xundump(FILE *fp, char *file, int *plno, int expected_table)
 }
 
 /*
- * Read the remainder of an xdump after the table header line from FP.
- * CA[] contains the table's selectors.
+ * Read the remainder of an xdump after the table header line from @fp.
+ * @ca[] contains the table's selectors.
  * Return 0 on success, -1 on failure.
  */
 static int
@@ -1331,7 +1340,7 @@ xutail(FILE *fp, struct castr *ca)
 }
 
 /*
- * Read the body of an xdump table from FP.
+ * Read the body of an xdump table from @fp.
  * Return number of rows read on success, -1 on failure.
  */
 static int