/*
* Empire - A multi-player, client/server Internet based war game.
- * Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ * Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
*
* Known contributors to this file:
* Ron Koenderink, 2005
- * Markus Armbruster, 2005
+ * Markus Armbruster, 2005-2006
*/
-/* FIXME normalize terminology: table/rows/columns or file/records/fields */
+/*
+ * FIXME:
+ * - Normalize terminology: table/rows/columns or file/records/fields
+ * - Loading tables with NSC_STRING elements more than once leaks memory
+ * TODO:
+ * - Check each partial table supplies the same rows
+ * - Check EFF_CFG tables are dense
+ * - Symbolic references to non-symbol tables
+ * - Symbolic array indexes
+ * TODO, but hardly worth the effort:
+ * - Permit reordering of array elements
+ * - Permit repetition of array elements in split tables
+ */
#include <config.h>
-#include <stdio.h>
-#include <stdlib.h>
-
#include <ctype.h>
-#include <string.h>
#include <stdarg.h>
+#include <stdio.h>
#include <time.h>
-
#include "file.h"
#include "match.h"
#include "nsc.h"
static char *fname;
static int lineno;
static int human;
-static int ellipsis;
+static int ellipsis, is_partial;
static int cur_type, cur_id;
static void *cur_obj;
+static int cur_obj_is_blank;
static int nflds;
static struct castr **fldca;
static int *fldidx;
static int *caflds;
+static unsigned char *caseen;
static int gripe(char *, ...) ATTRIBUTE((format (printf, 1, 2)));
static int deffld(int, char *, int);
static int chkflds(void);
static int setnum(int, double);
static int setstr(int, char *);
-static int xunsymbol1(char *, struct symbol *, struct castr *, int);
+static int xunsymbol(char *, struct castr *, int);
static int setsym(int, char *);
static int mtsymset(int, long *);
static int add2symset(int, long *, char *);
-static struct symbol *get_symtab(struct castr *);
static int xundump1(FILE *, int, struct castr *);
static int xundump2(FILE *, int, struct castr *);
getid(FILE *fp, char *buf)
{
int n;
- if (fscanf(fp, "%1023[^#() \t\n]%n", buf, &n) != 1 || !isalpha(buf[0]))
+ if (fscanf(fp, "%1023[^#()<>=#\" \t\n]%n", buf, &n) != 1
+ || !isalpha(buf[0]))
return -1;
return n;
}
case EOF:
return gripe("Unexpected EOF");
case '\n':
- if (i != nflds)
+ if (i != nflds) {
+ if (fldca[i]->ca_type != NSC_STRINGY && fldca[i]->ca_len)
+ return gripe("Field %s(%d) missing",
+ fldca[i]->ca_name, fldidx[i]);
return gripe("Field %s missing", fldca[i]->ca_name);
+ }
lineno++;
return 0;
case '+': case '-': case '.':
return gripe("Header %s doesn't take an index in field %d",
ca[res].ca_name, fldno + 1);
idx = 0;
- if (caflds[res])
+ if (caflds[res] && !caseen[res])
+ /* FIXME doesn't catch dupes within table part when caseen[res] */
return gripe("Duplicate header %s in field %d",
ca[res].ca_name, fldno + 1);
}
fldca[fldno] = &ca[res];
fldidx[fldno] = idx;
- caflds[res]++;
+ if (!caseen[res])
+ caflds[res]++;
return 1;
}
if (ca[0].ca_table != cur_type)
return gripe("Table %s doesn't support ...", ef_nameof(cur_type));
ellipsis = fldno;
+ is_partial = 1;
return 0;
}
struct castr *ca = ef_cadef(cur_type);
int i, len, res = 0;
- if (ellipsis) {
+ if (is_partial) {
+ /* Require index field */
if (!caflds[0])
return gripe("Header field %s required with ...", ca[0].ca_name);
+ /* Want the index field again in continued table: */
+ caflds[0] = 0;
return 0;
}
return fldca[fldno];
}
+static int
+fldval_must_match(int fldno)
+{
+ struct castr *ca = ef_cadef(cur_type);
+ int i = fldca[fldno] - ca;
+
+ return (!cur_obj_is_blank && (fldca[fldno]->ca_flags & NSC_CONST))
+ || caseen[i];
+}
+
static void *
getobj(struct castr *ca, int altid)
{
if (!cur_obj) {
if (ca->ca_table == cur_type)
cur_id = altid;
- if (cur_id >= ep->fids) {
+ cur_obj_is_blank = cur_id >= ep->fids;
+ if (cur_obj_is_blank) {
/* TODO grow cache (and posssibly file) unless EFF_STATIC */
if (cur_id < ep->csize - !!need_sentinel)
ep->cids = ep->fids = cur_id + 1;
return -1;
memb_ptr += ca->ca_off;
+ /* FIXME check assignment preserves value */
switch (ca->ca_type) {
case NSC_CHAR:
- case NSC_TYPEID:
old = ((signed char *)memb_ptr)[idx];
((signed char *)memb_ptr)[idx] = (signed char)dbl;
break;
case NSC_UCHAR:
+ case NSC_HIDDEN:
old = ((unsigned char *)memb_ptr)[idx];
((unsigned char *)memb_ptr)[idx] = (unsigned char)dbl;
break;
return gripe("Field %d doesn't take numbers", fldno + 1);
}
- if ((ca->ca_flags & NSC_CONST) && old != dbl)
+ if (fldval_must_match(fldno) && old != dbl)
return gripe("Value for field %d must be %g", fldno + 1, old);
return 1;
setstr(int fldno, char *str)
{
struct castr *ca;
- int idx;
+ int must_match, idx;
size_t len;
char *memb_ptr, *old;
if (!memb_ptr)
return -1;
memb_ptr += ca->ca_off;
+ must_match = fldval_must_match(fldno);
switch (ca->ca_type) {
case NSC_STRING:
old = ((char **)memb_ptr)[idx];
- if (!(ca->ca_flags & NSC_CONST))
+ if (!must_match)
((char **)memb_ptr)[idx] = str ? strdup(str) : NULL;
- len = 65535; /* really SIZE_MAX, but it's C99 */
+ len = 65535; /* really SIZE_MAX, but that's C99 */
break;
case NSC_STRINGY:
if (CANT_HAPPEN(idx))
return -1;
if (!str)
- return gripe("Field doesn't take nil");
+ return gripe("Field %d doesn't take nil", fldno + 1);
len = ca->ca_len;
if (strlen(str) > len)
return gripe("Field %d takes at most %d characters",
- fldno + 1, len);
+ fldno + 1, (int)len);
old = memb_ptr;
- if (!(ca->ca_flags & NSC_CONST))
+ if (!must_match)
strncpy(memb_ptr, str, len);
break;
default:
return gripe("Field %d doesn't take strings", fldno + 1);
}
- if (ca->ca_flags & NSC_CONST) {
+ if (must_match) {
if (old && (!str || strncmp(old, str, len)))
return gripe("Value for field %d must be \"%.*s\"",
- fldno + 1, len, old);
+ fldno + 1, (int)len, old);
if (!old && str)
return gripe("Value for field %d must be nil", fldno + 1);
}
}
static int
-xunsymbol1(char *id, struct symbol *symtab, struct castr *ca, int n)
+xunsymbol(char *id, struct castr *ca, int n)
{
- int i = stmtch(id, symtab, offsetof(struct symbol, name),
- sizeof(struct symbol));
+ int i = ef_elt_byname(ca->ca_table, id);
if (i < 0)
return gripe("%s %s symbol `%s' in field %d",
i == M_NOTUNIQUE ? "Ambiguous" : "Unknown",
- ca->ca_name, id, n);
+ ca->ca_name, id, n + 1);
+ return i;
+}
+
+static int
+symval(struct castr *ca, int i)
+{
+ int type = ca->ca_table;
+
+ if (ef_check(type) < 0)
+ return -1;
+ if (ef_cadef(type) == symbol_ca)
+ /* symbol table, value is in the table */
+ return ((struct symbol *)ef_ptr(type, i))->value;
+ /* value is the table index */
return i;
}
setsym(int fldno, char *sym)
{
struct castr *ca;
- struct symbol *symtab;
int i;
ca = getfld(fldno, NULL);
if (!ca)
return -1;
- symtab = get_symtab(ca);
- if (!symtab || (ca->ca_flags & NSC_BITS))
+ if (ca->ca_table == EF_BAD || (ca->ca_flags & NSC_BITS))
return gripe("Field %d doesn't take symbols", fldno + 1);
- i = xunsymbol1(sym, symtab, ca, fldno);
+ i = xunsymbol(sym, ca, fldno);
if (i < 0)
return -1;
- return setnum(fldno, symtab[i].value);
+ return setnum(fldno, symval(ca, i));
}
static int
mtsymset(int fldno, long *set)
{
struct castr *ca;
- struct symbol *symtab;
ca = getfld(fldno, NULL);
if (!ca)
return -1;
- symtab = get_symtab(ca);
- if (!symtab || !(ca->ca_flags & NSC_BITS)) {
+ if (ca->ca_table == EF_BAD || ef_cadef(ca->ca_table) != symbol_ca
+ || !(ca->ca_flags & NSC_BITS))
return gripe("Field %d doesn't take symbol sets", fldno + 1);
- }
*set = 0;
return 0;
}
add2symset(int fldno, long *set, char *sym)
{
struct castr *ca;
- struct symbol *symtab;
int i;
ca = getfld(fldno, NULL);
if (!ca)
return -1;
- symtab = get_symtab(ca);
- i = xunsymbol1(sym, symtab, ca, fldno);
+ i = xunsymbol(sym, ca, fldno);
if (i < 0)
return -1;
- *set |= symtab[i].value;
+ *set |= symval(ca, i);
return 0;
}
-static struct symbol *
-get_symtab(struct castr *ca)
-{
- int symtype = ca->ca_table;
- struct symbol *symtab;
-
- if (symtype == EF_BAD || ef_cadef(symtype) != symbol_ca)
- return NULL;
-
- symtab = ef_ptr(symtype, 0);
- CANT_HAPPEN(!symtab);
- return symtab;
-}
-
static int
xuheader(FILE *fp, int expected_table)
{
if (!ef_cadef(type) || !(ef_flags(type) & EFF_MEM)) {
CANT_HAPPEN(expected_table != EF_BAD);
- return gripe("Undumping of table `%s' not implemented", name);
+ return gripe("Table `%s' is not permitted here", name);
}
if (skipfs(fp) != '\n')
int ch, i, j, n;
if (human) {
- /* Allow repetition of the index field in continued table: */
- caflds[0] = 0;
while ((ch = skipfs(fp)) == '\n')
lineno++;
ungetc(ch, fp);
fldca = calloc(nf, sizeof(*fldca));
fldidx = calloc(nf, sizeof(*fldidx));
caflds = calloc(nca, sizeof(*caflds));
+ caseen = calloc(nca, sizeof(*caseen));
cur_type = type;
if (xundump2(fp, type, ca) < 0)
type = EF_BAD;
+ free(caseen);
free(caflds);
free(fldidx);
free(fldca);
static int
xundump2(FILE *fp, int type, struct castr *ca)
{
+ int i;
+
+ is_partial = 0;
for (;;) {
if (xuheader1(fp, type, ca) < 0)
return -1;
return -1;
if (!ellipsis)
return 0;
+ for (i = 0; ca[i].ca_name; i++)
+ caseen[i] = caflds[i] != 0;
if (xuheader(fp, type) < 0)
return -1;
}
gripe("Warning: should resize table %s from %d to %d, not implemented",
ef_nameof(type), ep->csize, n);
else if (type >= EF_SHIP_CHR && type <= EF_NUKE_CHR)
- ; /* shrinking these is okay */
+ ep->cids = ep->fids = n;
else
return gripe("Table %s requires %d rows, got %d",
ef_nameof(type), ep->fids, n);