]> git.pond.sub.org Git - empserver/commitdiff
nsc filetable: Get rid of untidy use of PACKAGE_STRING
authorMarkus Armbruster <armbru@pond.sub.org>
Sun, 26 Jun 2016 06:42:57 +0000 (08:42 +0200)
committerMarkus Armbruster <armbru@pond.sub.org>
Sun, 6 Aug 2017 18:09:17 +0000 (20:09 +0200)
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>
src/lib/common/filetable.c
src/lib/common/nsc.c

index ecbf69bf4b9f0d75f6207ea07145c53fcc2d3ecd..cc802afd3f51e5d2dff8cc97b192acd8a4075493 100644 (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),
index 3d2f5fd7f57d14940d89015889ee5aa6f9637cbe..1874beac559a408701355c13c3a0986b4dc62f48 100644 (file)
@@ -37,6 +37,7 @@
 
 #include <config.h>
 
+#include <limits.h>
 #include <stdlib.h>
 #include "empobj.h"
 #include "optlist.h"
 #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)
 {