From b4b65f0c35408e9554192f79b569e62753d78f99 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Sun, 26 Jun 2016 08:42:57 +0200 Subject: [PATCH] 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 --- src/lib/common/filetable.c | 5 +++-- src/lib/common/nsc.c | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/lib/common/filetable.c b/src/lib/common/filetable.c index ecbf69bf..cc802afd 100644 --- a/src/lib/common/filetable.c +++ b/src/lib/common/filetable.c @@ -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), diff --git a/src/lib/common/nsc.c b/src/lib/common/nsc.c index 3d2f5fd7..1874beac 100644 --- a/src/lib/common/nsc.c +++ b/src/lib/common/nsc.c @@ -37,6 +37,7 @@ #include +#include #include #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) {