From a3241d10979b75b252d126d46fcee1df7cd9d255 Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Thu, 25 Aug 2011 20:20:07 +0200 Subject: initial commit This is a proof of concept --- runlscript.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 runlscript.c (limited to 'runlscript.c') diff --git a/runlscript.c b/runlscript.c new file mode 100644 index 0000000..5eaef8a --- /dev/null +++ b/runlscript.c @@ -0,0 +1,70 @@ + +#include +#include + +#include +#include +#include + +#include +#include +#include + +#define RCLUAPATH "/lib/rc/lua" + +int load_rclibs(lua_State *L) +{ + int err = luaL_loadstring(L, "package.path = package.path..\";" RCLUAPATH "/?.lua\"\n" + "require(\"e\")\n") + || lua_pcall(L, 0, 0, 0); + if (err) { + fprintf(stderr, "error loading libs: %s\n", + lua_tostring(L, -1)); + lua_pop(L, 1); + return -1; + } +} + +int runfile(lua_State *L, const char *filename) +{ + int err; + err = luaL_loadfile(L, filename) + || lua_pcall(L, 0, 0, 0); + if (err) { + fprintf(stderr, "%s", lua_tostring(L, -1)); + lua_pop(L, 1); + } + return(err); +} + +int main(int argc, char *argv[]) +{ + int err; + lua_State *L = lua_open(); + char *filename = NULL; + char *func = NULL; + + luaL_openlibs(L); + if (argc > 1) + filename = argv[1]; + if (argc > 2) + func = argv[2]; + + if (load_rclibs(L) != 0) + return 1; + + err = runfile(L, filename); + if (err || func == NULL) + return err; + + lua_getglobal(L, func); + if (lua_pcall(L, 0, 1, 0) != 0) { + fprintf(stderr, "error running function '%s': %s\n", func, + lua_tostring(L, -1)); + return -1; + } + err = lua_toboolean(L, -1); + lua_close(L); + return !err; +} + -- cgit v1.2.3