]> jfr.im git - irc/quakenet/newserv.git/blobdiff - lua/lua.c
LUA: port luadb to dbapi2 to drop postgres dependency
[irc/quakenet/newserv.git] / lua / lua.c
index 3535f2cbfec856f6cdd2c9114b1a65e534917862..6199da1674e6881868ad24255d35cbd037ce73d3 100644 (file)
--- a/lua/lua.c
+++ b/lua/lua.c
@@ -4,6 +4,9 @@
 /* ALL RIGHTS RESERVED. */
 /* Don't put this into the SVN repo. */
 
+#define _POSIX_C_SOURCE 200112L
+#include <stdlib.h>
+
 #include "../core/config.h"
 #include "../core/error.h"
 #include "../core/hooks.h"
 #include "../lib/irc_string.h"
 #include "../core/schedule.h"
 #include "../lib/version.h"
-
+#include "../lib/strlfunc.h"
 #include "lua.h"
 
-MODULE_VERSION(LUA_FULLVERSION);
+MODULE_VERSION(LUA_SMALLVERSION);
 
 #ifdef LUA_DEBUGSOCKET
 
@@ -41,11 +44,12 @@ void lua_startbot(void *arg);
 void lua_destroybot(void);
 void lua_startcontrol(void);
 void lua_destroycontrol(void);
+void lua_destroydb(void);
 
 void lua_onunload(lua_State *l);
 void lua_onload(lua_State *l);
 
-void lua_setpath(lua_State *l);
+void lua_setpath(void);
 
 void lua_setupdebugsocket(void);
 void lua_freedebugsocket(void);
@@ -54,7 +58,10 @@ void lua_deregisternicks(lua_list *l);
 void lua_registerlocalcommands(lua_State *ps);
 void lua_registerdebug(lua_State *ps);
 void lua_socket_closeall(lua_list *l);
+void lua_scheduler_freeall(lua_list *l);
 void lua_registersocketcommands(lua_State *ps);
+void lua_registercryptocommands(lua_State *ps);
+void lua_registerschedulercommands(lua_State *ps);
 
 #ifdef LUA_DEBUGSOCKET
 
@@ -114,6 +121,8 @@ void _init() {
     return;
   }
 
+  lua_setpath();
+
   loaded = 1;
 
   startsched = scheduleoneshot(time(NULL) + 1, &lua_startup, NULL);
@@ -143,6 +152,7 @@ void _fini() {
 
     lua_destroybot();
     lua_destroycontrol();
+    lua_destroydb();
   }
 
   freesstring(cpath);
@@ -150,6 +160,7 @@ void _fini() {
   freesstring(dummy.name);
 
   lua_freedebugsocket();
+  nscheckfreeall(POOL_LUA);
 }
 
 void lua_loadscripts(void) {
@@ -166,34 +177,49 @@ void lua_loadscripts(void) {
   }
 }
 
+/* taken from the lua manual, modified to use nsmalloc */
+static void *lua_nsmalloc(void *ud, void *ptr, size_t osize, size_t nsize) {
+  if(nsize == 0) {
+    if(ptr != NULL)
+      luafree(ptr);
+    return NULL;
+  }
+
+  return luarealloc(ptr, nsize);
+}
+
 lua_State *lua_loadscript(char *file) {
   char fullpath[LUA_PATHLEN];
   int top;
   lua_State *l;
   lua_list *n;
+  char buf[1024];
+  void *args[2];
 
   if(!cpath || !suffix)
     return NULL;
 
-  delchars(file, "./\\;");
+  strlcpy(buf, file, sizeof(buf));
+
+  delchars(buf, "./\\;");
 
-  if(lua_scriptloaded(file))
+  if(lua_scriptloaded(buf))
     return NULL;
 
-  l = lua_open();
+  l = lua_newstate(lua_nsmalloc, NULL);
   if(!l)
     return NULL;
 
-  n = (lua_list *)malloc(sizeof(lua_list));;
+  n = (lua_list *)luamalloc(sizeof(lua_list));;
   if(!n) {
-    Error("lua", ERR_ERROR, "Error allocing list for %s.", file);
+    Error("lua", ERR_ERROR, "Error allocing list for %s.", buf);
     return NULL;
   }
 
-  n->name = getsstring(file, LUA_PATHLEN);
+  n->name = getsstring(buf, LUA_PATHLEN);
   if(!n->name) {
-    Error("lua", ERR_ERROR, "Error allocing name item for %s.", file);
-    free(n);
+    Error("lua", ERR_ERROR, "Error allocing name item for %s.", buf);
+    luafree(n);
     return NULL;
   }
   n->calls = 0;
@@ -206,8 +232,13 @@ lua_State *lua_loadscript(char *file) {
   lua_registercommands(l);
   lua_registerlocalcommands(l);
   lua_registerdbcommands(l);
+  lua_registersocketcommands(l);
+  lua_registercryptocommands(l);
+  lua_registerschedulercommands(l);
 
-  lua_setpath(l);
+  args[0] = file;
+  args[1] = l;
+  triggerhook(HOOK_LUA_LOADSCRIPT, args);
 
 #ifdef LUA_USEJIT
   lua_require(l, "lib/jit");
@@ -220,7 +251,7 @@ lua_State *lua_loadscript(char *file) {
     Error("lua", ERR_ERROR, "Error loading %s.", file);
     lua_close(l);
     freesstring(n->name);
-    free(n);
+    luafree(n);
     return NULL;
   }
 
@@ -230,6 +261,7 @@ lua_State *lua_loadscript(char *file) {
   n->prev = lua_tail;
   n->nicks = NULL;
   n->sockets = NULL;
+  n->schedulers = NULL;
 
   if(!lua_head) { 
     lua_head = n;
@@ -245,7 +277,15 @@ lua_State *lua_loadscript(char *file) {
     Error("lua", ERR_ERROR, "Error pcalling: %s.", file);
     lua_close(l);
     freesstring(n->name);
-    free(n);
+
+    if(lua_head == n)
+      lua_head = NULL;
+
+    lua_tail = n->prev;
+    if(lua_tail)
+      lua_tail->next = NULL;
+
+    luafree(n);
     return NULL;
   }
 
@@ -258,9 +298,12 @@ lua_State *lua_loadscript(char *file) {
 }
 
 void lua_unloadscript(lua_list *l) {
+  triggerhook(HOOK_LUA_UNLOADSCRIPT, l->l);
+
   lua_onunload(l->l);
   lua_deregisternicks(l);
   lua_socket_closeall(l);
+  lua_scheduler_freeall(l);
   lua_close(l->l);
   freesstring(l->name);
 
@@ -287,46 +330,14 @@ void lua_unloadscript(lua_list *l) {
      }
   }
 
-  free(l);
+  luafree(l);
 }
 
-void lua_setpath(lua_State *l) {
-  char fullpath[LUA_PATHLEN], *prev = NULL;
-
-  int top = lua_gettop(l);
-
-  lua_getglobal(l, "package");
-  if(!lua_istable(l, 1)) {
-    Error("lua", ERR_ERROR, "Unable to set package.path (package is not a table).");
-    return;
-  }
-
-  lua_pushstring(l, "path");
-  lua_gettable(l, -2);
-
-  if(lua_isstring(l, -1))
-    prev = (char *)lua_tostring(l, -1);
-
-  if(prev) {
-    snprintf(fullpath, sizeof(fullpath), "%s;%s/?%s", prev, cpath->content, suffix->content);
-  } else {
-    snprintf(fullpath, sizeof(fullpath), "%s/?%s", cpath->content, suffix->content);
-  }
-
-  /* pop broke! */
-
-  lua_getglobal(l, "package");
-  if(!lua_istable(l, 1)) {
-    Error("lua", ERR_ERROR, "Unable to set package.path (package is not a table).");
-    return;
-  }
-
-  lua_pushstring(l, "path");
-
-  lua_pushstring(l, fullpath);
-  lua_settable(l, -3);
+void lua_setpath(void) {
+  char fullpath[LUA_PATHLEN];
 
-  lua_settop(l, top);
+  snprintf(fullpath, sizeof(fullpath), "%s/?%s", cpath->content, suffix->content);
+  setenv("LUA_PATH", fullpath, 1);
 }
 
 lua_list *lua_scriptloaded(char *name) {
@@ -441,7 +452,7 @@ int lua_lineok(const char *data) {
   return 1;
 }
 
-INLINE int lua_debugpcall(lua_State *l, char *message, int a, int b, int c) {
+int lua_debugpcall(lua_State *l, char *message, int a, int b, int c) {
   lua_list *l2 = lua_listfromstate(l);
   int ret;