nsc filetable: Get rid of untidy use of PACKAGE_STRING

Table version selector version shows the version string stored in
version[].  Its implementation is a bit of a hack:
empfile[EF_VERSION].cache is set to version[], so it gets passed as
context object to nstr_eval().  This permits reading version[] with an
NSC_STRINGY selector at offset 0, which is what nsc_init()'s
version_ca0 is.

Both empfile[] and version_ca need the size of version[].  Since
version.h provides only an incomplete type for version[], they use
sizeof(PACKAGE_STRING), which isn't exactly clean.

Redo version_ca0 as virtual selector.  Takes a bit more code, but it's
easier to understand.

The context object is now unused.  Setting empfile[EF_VERSION].cache
to a null pointer would trip assertions, so make it point to a dummy.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
This commit is contained in:
Markus Armbruster 2016-06-26 08:42:57 +02:00
parent 89a06ec2d4
commit b4b65f0c35
2 changed files with 15 additions and 4 deletions

View file

@ -51,7 +51,6 @@
#include "ship.h"
#include "server.h"
#include "trade.h"
#include "version.h"
#include "xy.h"
static void sct_oninit(void *);
@ -67,6 +66,8 @@ static void plchr_oninit(void *);
static void lchr_oninit(void *);
static void nchr_oninit(void *);
static char dummy_cache;
/* Number of elements in ARRAY. */
#define SZ(array) (sizeof(array) / sizeof((array)[0]))
@ -225,7 +226,7 @@ struct empfile empfile[] = {
ARRAY_TABLE(empfile, EF_MAX, EFF_CFG),
NULL, NULL, NULL, NULL},
{EF_VERSION, "version", NULL, NULL, NULL, EF_BAD,
sizeof(PACKAGE_STRING), -1, EFF_STATIC, version, 1, 0, 1, 1, -1,
0, -1, EFF_MEM | EFF_STATIC, &dummy_cache, 1, 0, 1, 1, -1,
NULL, NULL, NULL, NULL},
{EF_META, "meta", NULL, NULL, mdchr_ca, EF_BAD,
PTR_CACHE(mdchr_ca, EFF_CFG),

View file

@ -37,6 +37,7 @@
#include <config.h>
#include <limits.h>
#include <stdlib.h>
#include "empobj.h"
#include "optlist.h"
@ -44,9 +45,11 @@
#include "nsc.h"
#include "product.h"
#include "unit.h"
#include "version.h"
static void *nsc_ver(struct valstr *, struct natstr *, void *);
static void *nsc_ver_maxnoc(struct valstr *, struct natstr *, void *);
static void *nsc_ver_version(struct valstr *, struct natstr *, void *);
static void *nsc_sct_terr(struct valstr *, struct natstr *, void *);
static void *nsc_sct_track(struct valstr *, struct natstr *, void *);
static void *nsc_cargo_nplane(struct valstr *, struct natstr *, void *);
@ -755,8 +758,7 @@ void
nsc_init(void)
{
static struct castr version_ca0 = {
"version", 0, NSC_STRINGY, sizeof(PACKAGE_STRING), NULL, EF_BAD, 0,
CA_DUMP
"version", 0, NSC_STRING, 0, nsc_ver_version, EF_BAD, 0, CA_DUMP
};
static struct castr version_ca1 = {
"maxnoc", 0, NSC_LONG, 0, nsc_ver_maxnoc, EF_BAD, 0, CA_DUMP
@ -820,6 +822,14 @@ nsc_ver_maxnoc(struct valstr *val, struct natstr *np, void *ptr)
return NULL;
}
static void *
nsc_ver_version(struct valstr *val, struct natstr *np, void *ptr)
{
val->val_as.str.base = version;
val->val_as.str.maxsz = INT_MAX; /* really SIZE_MAX, but that's C99 */
return NULL;
}
static void *
nsc_sct_terr(struct valstr *val, struct natstr *np, void *ptr)
{