From 6cf12867f2d5e078d9a988607315a8257380d8ba Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Fri, 15 Oct 2010 09:49:24 +0000 Subject: implement match() and matches() --- laugeas.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/laugeas.c b/laugeas.c index f1ecb2e..1962a66 100644 --- a/laugeas.c +++ b/laugeas.c @@ -1,3 +1,5 @@ +#include + #include #include #include @@ -193,6 +195,35 @@ static int Paug_mv(lua_State *L) return pushresult(L, aug_mv(a, src, dst), a, NULL); } +static int Paug_matches(lua_State *L) +{ + augeas *a = Paug_checkarg(L, 1); + const char *path = luaL_checkstring(L, 2); + return pushresult(L, aug_match(a, path, NULL), a, path); +} + +static int Paug_match(lua_State *L) +{ + augeas *a = Paug_checkarg(L, 1); + const char *path = luaL_checkstring(L, 2); + char **match = NULL; + int i, n; + n = aug_match(a, path, &match); + if (n < 0) + return pusherror(L, a, path); + + lua_newtable(L); + for (i = 0; i < n; i++) { + lua_pushnumber(L, i+1); + lua_pushstring(L, match[i]); + lua_settable(L, -3); + free(match[i]); + } + free(match); + lua_pushinteger(L, n); + return 2; +} + static int Paug_save(lua_State *L) { augeas *a = Paug_checkarg(L, 1); @@ -228,6 +259,8 @@ static const luaL_reg Paug_methods[] = { {"insert", Paug_insert}, {"rm", Paug_rm}, {"mv", Paug_mv}, + {"matches", Paug_matches}, + {"match", Paug_match}, {"save", Paug_save}, {"load", Paug_load}, {"print", Paug_print}, -- cgit v1.2.3