From 9cdd8ad35f1465cb10e5b517f67e9e98ab8a7518 Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Thu, 14 Oct 2010 17:41:32 +0000 Subject: initial commit --- Makefile | 32 +++++++++++++++++++ laugeas.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 Makefile create mode 100644 laugeas.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..622de73 --- /dev/null +++ b/Makefile @@ -0,0 +1,32 @@ + +LUA_LIBDIR = /usr/lib/lua/5.1 + +VERSION = 0.1 +GIT_REV := $(shell test -d .git && git describe || echo exported) +ifneq ($(GIT_REV), exported) +FULL_VERSION := $(GIT_REV) +FULL_VERSION := $(patsubst v%,%,$(FULL_VERSION)) +else +FULL_VERSION := $(VERSION) +endif + +AUGEAS_LIBS= $(shell pkg-config --libs augeas) +OBJS = laugeas.o +LIBS = $(AUGEAS_LIBS) + +CFLAGS ?= -g -Wall -Werror +CFLAGS += -fPIC +CFLAGS += -DVERSION=\"$(FULL_VERSION)\" + +LDFLAGS += -L/lib + +all: augeas.so + + +augeas.so: $(OBJS) + $(CC) $(LDFLAGS) -o $@ -fPIC -shared $^ $(LIBS) + +clean: + rm -f augeas.so $(OBJS) + + diff --git a/laugeas.c b/laugeas.c new file mode 100644 index 0000000..5195da2 --- /dev/null +++ b/laugeas.c @@ -0,0 +1,104 @@ +#include +#include +#include + +#include + +#define LIBNAME "augeas" +#define PAUG_META "augeas" + +#ifndef VERSION +#define VERSION "unknown" +#endif + +static int Paug_init(lua_State *L) +{ + augeas **a; + a = (augeas **) lua_newuserdata(L, sizeof(augeas *)); + luaL_getmetatable(L, PAUG_META); + lua_setmetatable(L, -2); + + *a = aug_init(NULL, NULL, 0); + if (*a == NULL) + luaL_error(L, "aug_init failed"); + return 1; +} + +static augeas **Paug_checkarg_index(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 augeas **Paug_checkarg(lua_State *L) +{ + return Paug_checkarg_index(L, 1); +} + +static int Paug_close(lua_State *L) +{ + augeas **a = Paug_checkarg(L); + if (*a) + aug_close(*a); + return 0; +} + +static int Paug_get(lua_State *L) +{ + augeas **a; + const char *path; + const char *value = NULL; + + a = Paug_checkarg(L); + path = luaL_checkstring(L, 2); + lua_pushinteger(L, aug_get(*a, path, &value)); + lua_pushstring(L, value); + return 2; +} + +static int Paug_print(lua_State *L) +{ + augeas **a; + const char *path; + a = Paug_checkarg(L); + path = luaL_checkstring(L, 2); + lua_pushinteger(L, aug_print(*a, stdout, path)); + return 1; +} + +static const luaL_reg Paug_methods[] = { + {"init", Paug_init}, + {"get", Paug_get}, + {"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) +{ + luaL_register(L, LIBNAME, Paug_methods); + lua_pushliteral(L, "version"); + lua_pushliteral(L, VERSION); + lua_settable(L, -3); + + 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; +} + -- cgit v1.2.3