summaryrefslogtreecommitdiffstats
path: root/rc.c
blob: d53e35e637990bffac26d704e0e35992823b67fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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;
}