From fe552a1422cb40b99159947c5f8f66bd99a13d4c Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Fri, 19 Jun 2015 08:40:52 +0200 Subject: lua: add support for building multiple versions It might be handy to be able to build Lua module for multiple Lua versions. --- Makefile | 13 ++++--- lsircbot.c | 113 --------------------------------------------------------- lua/lsircbot.c | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 118 deletions(-) delete mode 100644 lsircbot.c create mode 100644 lua/lsircbot.c diff --git a/Makefile b/Makefile index 0304763..217110b 100644 --- a/Makefile +++ b/Makefile @@ -9,12 +9,11 @@ CFLAGS += -DVERSION=\"$(VERSION)$(GIT_VERSION)\" TARGETS = sircbot sircbot-send ifneq (NOLUA,) -LUAPC ?= lua +LUA_VERSION ?= 5.1 +LUAPC ?= lua$(LUA_VERSION) PKGCONFIG ?= pkg-config -TARGETS += sircbot.so +TARGETS += lua/$(LUA_VERSION)/sircbot.so LUA_CFLAGS = $(shell $(PKGCONFIG) --cflags $(LUAPC)) -LUA_LIBS = $(shell $(PKGCONFIG) --libs $(LUAPC)) -CFLAGS += $(LUA_CFLAGS) endif prefix ?= /usr/local @@ -32,12 +31,16 @@ sircbot: sircbot.o irc.o sircbot-send: sircbot-send.o $(CC) $(LDFLAGS) -o $@ $^ -sircbot.so: lsircbot.o +lua/$(LUA_VERSION)/sircbot.so: lua/$(LUA_VERSION)/lsircbot.o $(CC) -shared $(LDFLAGS) -o $@ $^ $(LUA_LIBS) .c.o: $(CC) -fPIC $(CFLAGS) -c $^ +lua/$(LUA_VERSION)/%.o: lua/%.c + mkdir -p "$(dir $@)" + $(CC) -fPIC $(CFLAGS) $(LUA_CFLAGS) -o $@ -c $^ + clean: rm -f *.o $(TARGETS) diff --git a/lsircbot.c b/lsircbot.c deleted file mode 100644 index 0b97d5a..0000000 --- a/lsircbot.c +++ /dev/null @@ -1,113 +0,0 @@ -#include -#include -#include - -#include -#include -#include - -#include -#include -#include - -#define SBCONN_META "sircbot-connection" -#ifndef VERSION -#define VERSION "unknown" -#endif - -#if LUA_VERSION_NUM < 502 -# define luaL_newlib(L,l) (lua_newtable(L), luaL_register(L,NULL,l)) -# define luaL_setfuncs(L,l,n) (assert(n==0), luaL_register(L,NULL,l)) -#endif - -static int *Lsbconn_checkarg(lua_State *L, int index) -{ - int *fdp; - luaL_checktype(L, index, LUA_TUSERDATA); - fdp = (int *) luaL_checkudata(L, index, SBCONN_META); - if (fdp == NULL) { - const char *msg = lua_pushfstring(L, "%s expected, got %s", - SBCONN_META, - luaL_typename(L, index)); - luaL_argerror(L, index, msg); - } - return fdp; -} - -static int Lsbconn_new(lua_State *L) -{ - int *fdp; - const char *channel_name = luaL_checklstring(L, 1, NULL); - struct sockaddr_un sun; - fdp = (int *) lua_newuserdata(L, sizeof(int *)); - luaL_getmetatable(L, SBCONN_META); - lua_setmetatable(L, -2); - - *fdp = socket(PF_UNIX, SOCK_STREAM, 0); - if (*fdp < 0) - luaL_error(L, "Failed to create socket for '%s'", channel_name); - - sun.sun_family = AF_UNIX; - snprintf(sun.sun_path, sizeof(sun.sun_path), "/var/run/sircbot/%s", - channel_name); - if (connect(*fdp, (struct sockaddr *) &sun, sizeof(sun)) < 0) - luaL_error(L, "%s", sun.sun_path); - - return 1; -} - -static int Lsbconn_destroy(lua_State *L) -{ - int *fdp = Lsbconn_checkarg(L, 1); - close(*fdp); - return 1; -} - -static int Lsbconn_send(lua_State *L) -{ - int *fdp; - const char *msg; - int r; - - fdp = Lsbconn_checkarg(L, 1); - msg = luaL_checklstring(L, 2, NULL); - r = write(*fdp, msg, strlen(msg)); - lua_pushinteger(L, r); - return 1; -} - -static const luaL_Reg sbconn_meta_methods[] = { - { "__gc", Lsbconn_destroy }, - { NULL, NULL } -}; - -static const luaL_Reg sircbot_methods[] = { - { "connect", Lsbconn_new }, - { "send", Lsbconn_send }, - { NULL, NULL } -}; - -LUALIB_API int luaopen_sircbot(lua_State *L) -{ - /* register sircbot library */ - luaL_newlib(L, sircbot_methods); - - /* version */ - lua_pushliteral(L, "version"); - lua_pushliteral(L, VERSION); - lua_settable(L, -3); - - /* register metatable for it */ - luaL_newmetatable(L, SBCONN_META); - luaL_setfuncs(L, sbconn_meta_methods, 0); - 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; -} - diff --git a/lua/lsircbot.c b/lua/lsircbot.c new file mode 100644 index 0000000..0b97d5a --- /dev/null +++ b/lua/lsircbot.c @@ -0,0 +1,113 @@ +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +#define SBCONN_META "sircbot-connection" +#ifndef VERSION +#define VERSION "unknown" +#endif + +#if LUA_VERSION_NUM < 502 +# define luaL_newlib(L,l) (lua_newtable(L), luaL_register(L,NULL,l)) +# define luaL_setfuncs(L,l,n) (assert(n==0), luaL_register(L,NULL,l)) +#endif + +static int *Lsbconn_checkarg(lua_State *L, int index) +{ + int *fdp; + luaL_checktype(L, index, LUA_TUSERDATA); + fdp = (int *) luaL_checkudata(L, index, SBCONN_META); + if (fdp == NULL) { + const char *msg = lua_pushfstring(L, "%s expected, got %s", + SBCONN_META, + luaL_typename(L, index)); + luaL_argerror(L, index, msg); + } + return fdp; +} + +static int Lsbconn_new(lua_State *L) +{ + int *fdp; + const char *channel_name = luaL_checklstring(L, 1, NULL); + struct sockaddr_un sun; + fdp = (int *) lua_newuserdata(L, sizeof(int *)); + luaL_getmetatable(L, SBCONN_META); + lua_setmetatable(L, -2); + + *fdp = socket(PF_UNIX, SOCK_STREAM, 0); + if (*fdp < 0) + luaL_error(L, "Failed to create socket for '%s'", channel_name); + + sun.sun_family = AF_UNIX; + snprintf(sun.sun_path, sizeof(sun.sun_path), "/var/run/sircbot/%s", + channel_name); + if (connect(*fdp, (struct sockaddr *) &sun, sizeof(sun)) < 0) + luaL_error(L, "%s", sun.sun_path); + + return 1; +} + +static int Lsbconn_destroy(lua_State *L) +{ + int *fdp = Lsbconn_checkarg(L, 1); + close(*fdp); + return 1; +} + +static int Lsbconn_send(lua_State *L) +{ + int *fdp; + const char *msg; + int r; + + fdp = Lsbconn_checkarg(L, 1); + msg = luaL_checklstring(L, 2, NULL); + r = write(*fdp, msg, strlen(msg)); + lua_pushinteger(L, r); + return 1; +} + +static const luaL_Reg sbconn_meta_methods[] = { + { "__gc", Lsbconn_destroy }, + { NULL, NULL } +}; + +static const luaL_Reg sircbot_methods[] = { + { "connect", Lsbconn_new }, + { "send", Lsbconn_send }, + { NULL, NULL } +}; + +LUALIB_API int luaopen_sircbot(lua_State *L) +{ + /* register sircbot library */ + luaL_newlib(L, sircbot_methods); + + /* version */ + lua_pushliteral(L, "version"); + lua_pushliteral(L, VERSION); + lua_settable(L, -3); + + /* register metatable for it */ + luaL_newmetatable(L, SBCONN_META); + luaL_setfuncs(L, sbconn_meta_methods, 0); + 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