]> granicus.if.org Git - nethack/commitdiff
get lua version from lua itself
authornhmall <nhmall@nethack.org>
Sat, 30 Nov 2019 22:23:14 +0000 (17:23 -0500)
committernhmall <nhmall@nethack.org>
Sat, 30 Nov 2019 22:23:14 +0000 (17:23 -0500)
include/extern.h
src/nhlua.c
src/version.c

index 9c8de1b74c87e5f184109ee404583bb61bd1dff2..7e9a62c410aabd1ec6ccdde74f9a7ec1193ad96b 100644 (file)
@@ -1692,6 +1692,7 @@ E int FDECL(get_table_boolean_opt, (lua_State *, const char *, int));
 E int FDECL(get_table_option, (lua_State *, const char *, const char *, const char *const *));
 E int FDECL(str_lines_max_width, (const char *));
 E char *FDECL(stripdigits, (char *));
+E const char *NDECL(get_lua_version);
 #endif /* !CROSSCOMPILE || CROSSCOMPILE_TARGET */
 
 /* ### nhregex.c ### */
index 7c20d9b98467a7ad6f8e95150b51b21781fa0f71..c66cd8039ce19b2d4a1348e6ecba18f31547d97a 100644 (file)
@@ -904,3 +904,25 @@ const char *name;
 
     return ret;
 }
+
+const char *
+get_lua_version()
+{
+    size_t len;
+    const char *vs = (const char *) 0;
+    lua_State *L;
+
+    if (g.lua_ver[0] == 0) {
+        L = nhl_init();
+
+        if (L) {
+            lua_getglobal(L, "_VERSION");
+            if (lua_isstring(L, -1))
+                vs = lua_tolstring (L, -1, &len);
+            if (vs && (int) len < sizeof g.lua_ver - 1)
+                Strcpy(g.lua_ver, vs);
+        }
+        lua_close(L);
+    }
+    return (const char *) g.lua_ver;
+}
index c4040c1bcf898ee2ef26590bb04ccbc183310faa..4939f9f14041468fc7e43942adc6dbbee773ebcc 100644 (file)
@@ -290,6 +290,7 @@ boolean pastebuf;
 }
 
 extern const char regex_id[];
+extern char lua_ver[];          /* nhlua.c */
 
 /*
  * makedefs should put the first token into dat/options; we'll substitute
@@ -303,7 +304,7 @@ static struct rt_opt {
     const char *token, *value;
 } rt_opts[] = {
     { ":PATMATCH:", regex_id },
-    { ":LUAVERSION:", " 5.3.5"}, /* plan is to get this directly from Lua */
+    { ":LUAVERSION:", (const char *) g.lua_ver + 3 }, /* +3 skip past "Lua" */
 };
 
 /*
@@ -318,8 +319,11 @@ char *buf;
 {
     int i;
 
+    if (!g.lua_ver[0])
+        get_lua_version();
+        
     for (i = 0; i < SIZE(rt_opts); ++i) {
-        if (strstri(buf, rt_opts[i].token))
+        if (strstri(buf, rt_opts[i].token) && *rt_opts[i].value)
             (void) strsubst(buf, rt_opts[i].token, rt_opts[i].value);
         /* we don't break out of the loop after a match; there might be
            other matches on the same line */