/*
* 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
*
* Known contributors to this file:
* Ron Koenderink, 2005
- * Markus Armbruster, 2005-2014
+ * Markus Armbruster, 2005-2016
*/
/*
#include <stdlib.h>
#include <string.h>
#include <time.h>
-#include "file.h"
#include "match.h"
#include "nat.h"
#include "nsc.h"
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)
}
/*
- * Okay to truncate table TYPE?
+ * Okay to truncate table @type?
*/
static int
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)
}
/*
- * 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)
return -1;
}
-/* Make TYPE the current table. */
+/* Make TYPE the current table. */
static void
tbl_start(int type)
{
idgap_len = 0;
}
-/* End the current table. */
+/* End the current table. */
static void
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 *
}
/*
- * Omit ID1..ID2-1.
+ * Omit @id1..@id2-1.
* Reset the omitted objects to default state.
*/
static void
}
/*
- * 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)
}
/*
- * 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 *
}
/*
- * 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
}
/*
- * 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
/*
* 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 *
}
/*
- * 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)
* 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
}
/*
- * Set obj's field FLDNO to STR.
+ * Set obj's field @fldno to @str.
* Return 0 on success, -1 on error.
*/
static int
}
/*
- * 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
}
/*
- * 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;
}
/*
- * 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
}
/*
- * 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.
*/
}
/*
- * 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
}
/*
- * 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
}
/*
- * 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.
*/
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)
/* 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)
}
/*
- * 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
}
/*
- * 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
}
/*
- * 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
/*
* 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)
}
/*
- * 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
}
/*
- * 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
}
/*
- * 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
}
/*
- * 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.
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);
}
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.
*/
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;
}
/*
- * 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.
*/
}
/*
- * 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
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));
}
/*
- * 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
}
/*
- * 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