summaryrefslogtreecommitdiffstats
path: root/laugeas.c
blob: f7bef35362155096724fdf313ca46d9aa5123e89 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

#include <augeas.h>

#define LIBNAME "augeas"
#define PAUG_META "augeas"

#ifndef VERSION
#define VERSION "unknown"
#endif

#define LUA_FILEHANDLE	"FILE*"

struct aug_flagmap {
	const char *name;
	int value;
};

struct aug_flagmap Taug_flagmap[] = {
	{ "none",		AUG_NONE },
	{ "save_backup",	AUG_SAVE_BACKUP },
	{ "save_newfile",	AUG_SAVE_NEWFILE },
	{ "typecheck",		AUG_TYPE_CHECK },
	{ "no_stdinc",		AUG_NO_STDINC },
	{ "save_noop",		AUG_SAVE_NOOP },
	{ "no_load",		AUG_NO_LOAD },
	{ "no_modl_autoload",	AUG_NO_MODL_AUTOLOAD },
	{ NULL, 0 }
};

static const char *get_opt_string_field(lua_State *L, int index,
				        const char *key, const char *def)
{
	const char *value;
	lua_getfield(L, index, key);
	value = luaL_optstring(L, -1, def);
	lua_pop(L, 1);
	return value;
}

static int get_boolean_field(lua_State *L, int index, const char *key)
{
	int value;
	lua_getfield(L, index, key);
	value = lua_toboolean(L, -1);
	lua_pop(L, 1);
	return value;
}

static int pusherror(lua_State *L, augeas *aug, const char *info)
{
	lua_pushnil(L);
	if (info==NULL)
		lua_pushstring(L, aug_error_message(aug));
	else
		lua_pushfstring(L, "%s: %s", info, aug_error_message(aug));
	lua_pushinteger(L, aug_error(aug));
	return 3;
}

static int pushresult(lua_State *L, int i, augeas *aug, const char *info)
{
	if (i < 0)
		return pusherror(L, aug, info);
	lua_pushinteger(L, i);
	return 1;
}

static int Paug_init(lua_State *L)
{
	augeas **a;
	struct aug_flagmap *f;
	const char *root = NULL, *loadpath = NULL;
	int flags = 0;

	if (lua_istable(L, 1)) {
		root = get_opt_string_field(L, 1, "root", NULL);
		loadpath = get_opt_string_field(L, 1, "loadpath", NULL);
		for (f = Taug_flagmap; f->name != NULL; f++)
			if (get_boolean_field(L, 1, f->name))
				flags |= f->value;
	} else {
		root = luaL_optstring(L, 1, NULL);
		loadpath = luaL_optstring(L, 2, NULL);
		flags = luaL_optinteger(L, 3, AUG_NONE);
	}

	a = (augeas **) lua_newuserdata(L, sizeof(augeas *));
	luaL_getmetatable(L, PAUG_META);
	lua_setmetatable(L, -2);

	*a = aug_init(root, loadpath, flags);
	if (*a == NULL)
		luaL_error(L, "aug_init failed");
	return 1;
}

static augeas *Paug_checkarg(lua_State *L, int index)
{
	augeas **a;
	luaL_checktype(L, index, LUA_TUSERDATA);
	a = (augeas **) luaL_checkudata(L, index, PAUG_META);
	if (a == NULL)
		luaL_typerror(L, index, PAUG_META);
	return *a;
}

static int Paug_close(lua_State *L)
{
	augeas *a = Paug_checkarg(L, 1);
	if (a)
		aug_close(a);
	a = NULL;
	return 0;
}

static int Paug_get(lua_State *L)
{
	augeas *a;
	const char *path;
	const char *value = NULL;
	int r;

	a = Paug_checkarg(L, 1);
	path = luaL_checkstring(L, 2);
	r = aug_get(a, path, &value);
	if (r < 0)
		return pusherror(L, a, path);
	lua_pushstring(L, value);
	return 1;
}

static int Paug_set(lua_State *L)
{
	augeas *a;
	const char *path, *value;

	a = Paug_checkarg(L, 1);
	path = luaL_checkstring(L, 2);
	value = luaL_checkstring(L, 3);
	return pushresult(L, aug_set(a, path, value), a, path);
}

static int Paug_rm(lua_State *L)
{
	augeas *a = Paug_checkarg(L, 1);
	const char *path = luaL_checkstring(L, 2);
	return pushresult(L, aug_rm(a, path), a, NULL);
}

static int Paug_save(lua_State *L)
{
	augeas *a = Paug_checkarg(L, 1);
	return pushresult(L, aug_save(a), a, NULL);
}

static int Paug_load(lua_State *L)
{
	augeas *a = Paug_checkarg(L, 1);
	return pushresult(L, aug_load(a), a, NULL);
}

static int Paug_print(lua_State *L)
{
	augeas *a;
	const char *path;
	FILE *f = stdout;
	a = Paug_checkarg(L, 1);
	path = luaL_checkstring(L, 2);
	if (lua_isuserdata(L, 3))
		f = *(FILE**) luaL_checkudata(L, 3, LUA_FILEHANDLE);
	return pushresult(L, aug_print(a, f, path), a, path);
}

static const luaL_reg Paug_methods[] = {
	{"init",	Paug_init},
	{"close",	Paug_close},
	{"get",		Paug_get},
	{"set",		Paug_set},
	{"rm",		Paug_rm},
	{"save",	Paug_save},
	{"load",	Paug_load},
	{"print",	Paug_print},
	{NULL,		NULL}
};

static const luaL_reg Luag_meta_methods[] = {
	{"__gc",	Paug_close},
	{NULL,		NULL}
};


LUALIB_API int luaopen_augeas(lua_State *L)
{
	struct aug_flagmap *f = Taug_flagmap;
	luaL_register(L, LIBNAME, Paug_methods);
	lua_pushliteral(L, "version");
	lua_pushliteral(L, VERSION);
	lua_settable(L, -3);

	while (f->name != NULL) {
		lua_pushstring(L, f->name);
		lua_pushinteger(L, f->value);
		lua_settable(L, -3);
		f++;
	}

	luaL_newmetatable(L, PAUG_META);
	luaL_register(L, NULL, Luag_meta_methods);
	lua_pushliteral(L, "__index");
	lua_pushvalue(L, -3);
	lua_rawset(L, -3);
	lua_pushliteral(L, "__metatable");
	lua_pushvalue(L, -3);
	lua_rawset(L, -3);
	lua_pop(L, 1);

	return 1;
}