diff options
-rw-r--r-- | Makefile | 36 | ||||
-rw-r--r-- | README | 7 | ||||
-rw-r--r-- | lua-uniso.c | 60 |
3 files changed, 93 insertions, 10 deletions
@@ -12,6 +12,7 @@ CFLAGS ?= -g -Wall -Werror CFLAGS += -fPIC CFLAGS += -I. +LDFLAGS += -L. prefix ?= /usr libdir = $(prefix)/lib @@ -20,6 +21,10 @@ includedir= $(prefix)/include INSTALLDIR := install -d INSTALL := install +PKG_CONFIG ?= pkg-config + +LUA_LIBS ?= $(shell $(PKG_CONFIG) --libs lua) +LUA_CFLAGS ?= $(shell $(PKG_CONFIG) --cflags lua) install-progs-y := $(INSTALLDIR) $(DESTDIR)$(bindir) && \ $(INSTALL) $(progs-y) $(DESTDIR)$(bindir) @@ -32,26 +37,37 @@ $(SONAME)_OBJS = $(libuniso.a_OBJS) $(SONAME)_LDFLAGS = -shared -Wl,-soname,$(SONAME) uniso_OBJS := uniso.o -uniso_LDFLAGS += -L. +uniso_LIBS = $(LIBS_UNISO) -all: $(TARGETS) +lua-uniso.o_CFLAGS = $(LUA_CFLAGS) +uniso.so_OBJS = lua-uniso.o +uniso.so_LIBS = $(LIBS_UNISO) $(LUA_LIBS) +uniso.so_LDFLAGS = -shared ifneq ($(ENABLE_SHARED),) shlibs-y += $(SONAME) libuniso.so install-shlibs-y := $(INSTALLDIR) $(DESTDIR)$(libdir) && \ $(INSTALL) $(SONAME) $(DESTDIR)$(libdir) && \ ln -sf $(SONAME) $(DESTDIR)$(libdir)/libuniso.so -uniso_LIBS := -luniso +LIBS_UNISO = -luniso else -uniso_LIBS := libuniso.a +LIBS_UNISO = libuniso.a endif TARGETS += $(shlibs-y) -$(SONAME): $($(SONAME)_OBJS) -libuniso.so: $(SONAME) - ln -s $< $@ +ifneq ($(ENABLE_LUA),) +lualibs-y += uniso.so +endif +TARGETS += $(lualibs-y) +all: $(TARGETS) + +libuniso.so: + ln -s $(SONAME) $@ + +$(SONAME): $($(SONAME)_OBJS) libuniso.so uniso: $(shlib-y) +uniso.so: $(uniso.so_OBJS) $(shlibs-y) libuniso.a libuniso.a: $(libuniso.a_OBJS) $(AR) rcs $@ $^ @@ -59,9 +75,9 @@ libuniso.a: $(libuniso.a_OBJS) %.o: %.c $(CC) $(CFLAGS) $($@_CFLAGS) -c $^ -uniso: $(uniso_OBJS) $(shlibs-y) +uniso: $(uniso_OBJS) $(shlibs-y) libuniso.a -uniso $(SONAME): +uniso $(SONAME) uniso.so: $(CC) $(LDFLAGS) $($@_LDFLAGS) -o $@ $($@_OBJS) $($@_LIBS) clean: @@ -69,6 +85,8 @@ clean: shared: $(SONAME) +lua: uniso.so + install: $(TARGETS) $(INCLUDES) $(INSTALLDIR) $(DESTDIR)$(includedir) $(INSTALL) $(INCLUDES) $(DESTDIR)$(includedir) @@ -6,9 +6,14 @@ To build: To build with shared library: - echo "ENABLE_SHARED=yes" > config.mk + echo "ENABLE_SHARED=yes" >> config.mk make +To build Lua module: + + echo "ENABLE_SHARED=yes" >> config.mk + make + To install: make install diff --git a/lua-uniso.c b/lua-uniso.c new file mode 100644 index 0000000..97a1e8c --- /dev/null +++ b/lua-uniso.c @@ -0,0 +1,60 @@ + +#include <lua.h> +#include <lualib.h> +#include <lauxlib.h> + +#include <uniso.h> + +#define MODULE_NAME "uniso" + +struct l_uniso_context { + lua_State *L; + int callback_ref; +}; + +static void l_callback(size_t current, size_t total, const char *filename, + void *userdata) +{ + struct l_uniso_context *ctx = (struct l_uniso_context *)userdata; + lua_State *L = ctx->L; + + lua_rawgeti(L, LUA_REGISTRYINDEX, ctx->callback_ref); + + if (lua_isnil(L, -1)) { + lua_pop(L, 1); + return; + } + + lua_pushnumber(L, current); + lua_pushnumber(L, total); + lua_pushstring(L, filename); + lua_call(L, 3, 0); +} + +static int l_uniso(lua_State *L) +{ + int fd = luaL_checkinteger(L, 1); + int result; + struct l_uniso_context ctx; + + if (!lua_isnil(L, 2)) + luaL_checktype(L, 2, LUA_TFUNCTION); + ctx.callback_ref = luaL_ref(L, LUA_REGISTRYINDEX); + ctx.L = L; + + result = uniso(fd, &l_callback, &ctx); + lua_pushinteger(L, result); + return 1; +} + +static const luaL_reg methods[] = { + {"uniso", l_uniso}, + {NULL, NULL}, +}; + +LUALIB_API int luaopen_uniso(lua_State *L) +{ + luaL_openlib(L, MODULE_NAME, methods, 0); + return 1; +} + |