summaryrefslogtreecommitdiff
path: root/degesch.c
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2015-11-19 15:45:32 +0100
committerPřemysl Janouch <p.janouch@gmail.com>2015-11-21 14:09:33 +0100
commitfbfe0ba18a1bf4400d71910869b2ab3601827606 (patch)
treeea471283d3b1e982f5111399f0882ceb15ff0b3a /degesch.c
parent5ee210a5b724dabb8580654e3d2811537ff7f6b4 (diff)
downloadxK-fbfe0ba18a1bf4400d71910869b2ab3601827606.tar.gz
xK-fbfe0ba18a1bf4400d71910869b2ab3601827606.tar.xz
xK-fbfe0ba18a1bf4400d71910869b2ab3601827606.zip
degesch: add a stubbed Lua plugin loader
Diffstat (limited to 'degesch.c')
-rw-r--r--degesch.c98
1 files changed, 98 insertions, 0 deletions
diff --git a/degesch.c b/degesch.c
index 1a2ae20..ac54ded 100644
--- a/degesch.c
+++ b/degesch.c
@@ -79,6 +79,12 @@ enum
#include <histedit.h>
#endif // HAVE_EDITLINE
+#ifdef HAVE_LUA
+#include <lua.h>
+#include <lualib.h>
+#include <lauxlib.h>
+#endif // HAVE_LUA
+
/// Some arbitrary limit for the history file
#define HISTORY_LIMIT 10000
@@ -7129,6 +7135,95 @@ server_rename (struct app_context *ctx, struct server *s, const char *new_name)
}
}
+// --- Lua ---------------------------------------------------------------------
+
+#ifdef HAVE_LUA
+
+struct lua_plugin
+{
+ struct plugin super; ///< The structure we're deriving
+ struct app_context *ctx; ///< Application context
+ lua_State *L; ///< Lua state
+};
+
+static void
+lua_plugin_free (struct plugin *self_)
+{
+ struct lua_plugin *self = (struct lua_plugin *) self_;
+ lua_close (self->L);
+}
+
+struct plugin_vtable lua_plugin_vtable =
+{
+ .free = lua_plugin_free,
+};
+
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+static void *
+lua_plugin_alloc (void *ud, void *ptr, size_t o_size, size_t n_size)
+{
+ (void) ud;
+ (void) o_size;
+
+ if (n_size)
+ return realloc (ptr, n_size);
+
+ free (ptr);
+ return NULL;
+}
+
+static int
+lua_plugin_panic (lua_State *L)
+{
+ // XXX: we might be able to do something better
+ print_fatal ("Lua panicked: %s", lua_tostring (L, -1));
+ lua_close (L);
+ exit (EXIT_FAILURE);
+ return 0;
+}
+
+static luaL_Reg lua_plugin_library[] =
+{
+ { NULL, NULL },
+};
+
+static struct plugin *
+lua_plugin_load (struct app_context *ctx, const char *filename,
+ struct error **e)
+{
+ lua_State *L = lua_newstate (lua_plugin_alloc, NULL);
+ if (!L)
+ {
+ error_set (e, "Lua initialization failed");
+ return NULL;
+ }
+
+ lua_atpanic (L, lua_plugin_panic);
+ luaL_openlibs (L);
+
+ luaL_newlib (L, lua_plugin_library);
+ lua_setglobal (L, PROGRAM_NAME);
+
+ int ret;
+ if ((ret = luaL_loadfile (L, filename))
+ || (ret = lua_pcall (L, 0, 0, 0)))
+ {
+ error_set (e, "%s: %s", "Lua", lua_tostring (L, -1));
+ lua_close (L);
+ return NULL;
+ }
+
+ struct lua_plugin *plugin = xcalloc (1, sizeof *plugin);
+ plugin->super.name = NULL;
+ plugin->super.vtable = &lua_plugin_vtable;
+ plugin->ctx = ctx;
+ plugin->L = L;
+ return &plugin->super;
+}
+
+#endif // HAVE_LUA
+
// --- Plugins -----------------------------------------------------------------
typedef struct plugin *(*plugin_load_fn)
@@ -7138,6 +7233,9 @@ typedef struct plugin *(*plugin_load_fn)
// however this possibility is just a byproduct of abstraction
static plugin_load_fn g_plugin_loaders[] =
{
+#ifdef HAVE_LUA
+ lua_plugin_load,
+#endif // HAVE_LUA
};
static struct plugin *