summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNatanael Copa <ncopa@alpinelinux.org>2010-01-07 07:47:26 +0000
committerNatanael Copa <ncopa@alpinelinux.org>2010-01-07 07:51:14 +0000
commitdc41f45a9d4512970e7ef62669b64ee0e20add10 (patch)
treec54df82c82cdadc09ad7ed1ca0a34ca8c43c2b47
downloadlua-openrc-dc41f45a9d4512970e7ef62669b64ee0e20add10.tar.bz2
lua-openrc-dc41f45a9d4512970e7ef62669b64ee0e20add10.tar.xz
initial commit
-rw-r--r--Makefile17
-rw-r--r--rc.c118
2 files changed, 135 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..ffa6753
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,17 @@
+
+OBJS = rc.o
+LIBS = -lrc
+
+DEBUGFLAGS = -g -Wall -Werror
+CFLAGS += $(DEBUGFLAGS)
+
+LDFLAGS += -L/lib
+
+all: rc.so
+
+
+rc.so: $(OBJS)
+ $(CC) $(LDFLAGS) -o $@ -fPIC -shared $^ $(LIBS)
+
+clean:
+ rm -f rc.so $(OBJS)
diff --git a/rc.c b/rc.c
new file mode 100644
index 0000000..d53e35e
--- /dev/null
+++ b/rc.c
@@ -0,0 +1,118 @@
+
+#include "lua.h"
+#include "lualib.h"
+#include "lauxlib.h"
+
+#include <rc.h>
+
+#define MYNAME "rc"
+#define MYVERSION "OpenRC library for " LUA_VERSION " / Jan 2010"
+
+/** runlevel_get() - Return the current runlevel. */
+static int Prunlevel_get(lua_State *L)
+{
+ lua_pushstring(L, rc_runlevel_get());
+ return 1;
+}
+
+/** runlevel_exists(runlevel) - Checks if the runlevel exists or not */
+static int Prunlevel_exists(lua_State *L)
+{
+ lua_pushboolean(L, rc_runlevel_exists(luaL_checkstring(L, 1)));
+ return 1;
+}
+
+/** runlevel_list() - Return a list of runlevels. */
+static int Prunlevel_list(lua_State *L)
+{
+ int i = 1;
+ RC_STRING *item;
+ RC_STRINGLIST *list = rc_runlevel_list();
+ lua_newtable(L);
+ TAILQ_FOREACH(item, list, entries) {
+ lua_pushnumber(L, i++);
+ lua_pushstring(L, item->value);
+ lua_settable(L, -3);
+ }
+ rc_stringlist_free(list);
+ return 1;
+}
+
+
+/** service_add(service, [runlevel]) - Add the service to the runlevel */
+static int Pservice_add(lua_State *L)
+{
+ const char *service, *runlevel;
+ service =luaL_checkstring(L, 1);
+ runlevel = luaL_optstring(L, 2, rc_runlevel_get());
+ lua_pushboolean(L, rc_service_add(runlevel, service));
+ return 1;
+}
+
+/** service_delete(service, [runlevel]) - Remove the service from the runlevel */
+static int Pservice_delete(lua_State *L)
+{
+ const char *service, *runlevel;
+ service =luaL_checkstring(L, 1);
+ runlevel = luaL_optstring(L, 2, rc_runlevel_get());
+ lua_pushboolean(L, rc_service_delete(runlevel, service));
+ return 1;
+}
+
+/** service_exists(service) - Checks if a service exists or not.*/
+static int Pservice_exists(lua_State *L)
+{
+ const char *service = luaL_checkstring(L, 1);
+ lua_pushboolean(L, rc_service_exists(service));
+ return 1;
+}
+
+/** service_in_runlevel(service, [runlevel]) - Checks if a service is in a runlevel*/
+static int Pservice_in_runlevel(lua_State *L)
+{
+ const char *service, *runlevel;
+ service =luaL_checkstring(L, 1);
+ runlevel = luaL_optstring(L, 2, rc_runlevel_get());
+ lua_pushboolean(L, rc_service_in_runlevel(service, runlevel));
+ return 1;
+}
+
+/** service_resolve(service) - Resolves a service name to its full path. */
+static int Pservice_resolve(lua_State *L)
+{
+ const char *service;
+ service =luaL_checkstring(L, 1);
+ lua_pushstring(L, rc_service_resolve(service));
+ return 1;
+}
+
+static const luaL_reg R[] =
+{
+ {"runlevel_get", Prunlevel_get},
+ {"runlevel_exists", Prunlevel_exists},
+ {"runlevel_list", Prunlevel_list},
+
+ {"service_add", Pservice_add},
+ {"service_delete", Pservice_delete},
+ {"service_exists", Pservice_exists},
+ {"service_in_runlevel", Pservice_in_runlevel},
+ {"service_resolve", Pservice_resolve},
+ {NULL, NULL}
+};
+
+#define set_const(key, value) \
+ lua_pushliteral(L, key); \
+ lua_pushnumber(L, value); \
+ lua_settable(L, -3)
+
+LUALIB_API int luaopen_rc (lua_State *L)
+{
+ luaL_register(L, MYNAME, R);
+
+ lua_pushliteral(L,"version"); /** version */
+ lua_pushliteral(L,MYVERSION);
+ lua_settable(L,-3);
+
+ return 1;
+}
+