From b48f6d21cf776ca4afa389a72dc37ce676624134 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 29 Mar 2011 23:28:52 +0000 Subject: moved to lua-mq (mq) --- Makefile | 4 +- config | 2 +- debian/Makefile.Debian.conf | 43 +++++++++++ debian/compat | 1 + src/lua-mq.c | 179 +++++++++++++++++++++++++++++++++++++++++++ src/luamq.c | 180 -------------------------------------------- test.lua | 2 +- 7 files changed, 227 insertions(+), 184 deletions(-) create mode 100644 debian/Makefile.Debian.conf create mode 100644 debian/compat create mode 100644 src/lua-mq.c delete mode 100644 src/luamq.c diff --git a/Makefile b/Makefile index 31ff59c..474f44c 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,8 @@ CONFIG= ./config include $(CONFIG) -OBJS= src/luamq.o -SRCS= src/luamq.h src/luamq.c +OBJS= src/lua-mq.o +SRCS= src/lua-mq.h src/lua-mq.c AR= ar rcu RANLIB= ranlib diff --git a/config b/config index 42364d3..1bc3397 100644 --- a/config +++ b/config @@ -17,7 +17,7 @@ LIB_OPTION= -lrt -shared #for Linux V= 0.1 -LIBNAME= luamq.so +LIBNAME= lua-mq.so WARN= -Wall -Wmissing-prototypes -Wmissing-declarations -pedantic -fPIC INCS= -I$(LUA_INC) diff --git a/debian/Makefile.Debian.conf b/debian/Makefile.Debian.conf new file mode 100644 index 0000000..447860f --- /dev/null +++ b/debian/Makefile.Debian.conf @@ -0,0 +1,43 @@ +### $Id: Makefile.Debian.conf.sample 1398 2009-11-13 21:10:12Z gareuselesinge $ + +### mandatory fields +PKG_NAME=mq + +### things relative to the C library part +CLIB_CFLAGS= -I src/ +CLIB_LDFLAGS= +CLIB_LDFLAGS_STATIC= +CLIB_OBJS=src/luamq.lo +VERSION_INFO=0:1:0 +LUA_MODNAME_CPART= + +### things relative to the lua library part +LUA_HEADER= +LUA_SOURCES= +LUA_MODNAME=mq +LUA_TEST= + +### this part is relative to pkg-config +PKG_VERSION=$(shell dpkg-parsechangelog|grep ^Ver|cut -d ' ' -f 2|cut -d '-' -f 1) +PKG_LIBS_PRIVATE= +PKG_URL= +PKG_REQUIRES= +PKG_CONFLICTS= + +### this part is relative to debian libs naming policy +DEB_EXTRA_DASH= + +### In addition, to gather flexibility, you can set these hooks +### (called in this order) +pre-all-hook:: +post-all-hook:: +pre-test-hook:: +pre-lua-dynamic-test-hook:: +pre-dynamic-link-hook:: +pre-app-dynamic-test-hook:: +pre-static-link-hook:: +pre-app-static-test-hook:: +pre-install-hook:: +post-install-hook:: +pre-clean-hook:: +post-clean-hook:: diff --git a/debian/compat b/debian/compat new file mode 100644 index 0000000..7ed6ff8 --- /dev/null +++ b/debian/compat @@ -0,0 +1 @@ +5 diff --git a/src/lua-mq.c b/src/lua-mq.c new file mode 100644 index 0000000..2975d16 --- /dev/null +++ b/src/lua-mq.c @@ -0,0 +1,179 @@ +#include "lua.h" +#include "lauxlib.h" + +#include +#include +#define _XOPEN_SOURCE 600 +#include +#include +#include + +#define MQD_TYPENAME "mqd_t" +#include "lua-mq.h" + +static mode_t get_mode(const char* modestr) +{ +/* TODO: implement this!!! */ + return S_IRWXU | S_IRWXG; +} + +static int get_oflags(const char* flagstr) +{ + if(!strcmp(flagstr, "ro")) + return O_RDONLY; + + if(!strcmp(flagstr, "wo")) + return O_WRONLY; + + return O_RDWR; +} + +static void push_errno(lua_State *L) +{ + char msg[255]; + int ret; + + ret = strerror_r(errno, msg, sizeof(msg)); + if(!ret) + lua_pushstring(L, msg); + else + lua_pushstring(L, "unknown error"); +} + +static int l_mq_create(lua_State *L) +{ + mqd_t id, *ptr; + int flags; + mode_t mode; + + flags = get_oflags(luaL_optstring(L, 2, "")); + mode = get_mode(luaL_optstring(L, 3, "")); + id = mq_open(luaL_checkstring(L, 1), flags | O_CREAT, mode, NULL); + if(id == (mqd_t)-1) { + lua_pushnil(L); + push_errno(L); + return 2; + } + ptr = (mqd_t*)lua_newuserdata(L, sizeof(mqd_t)); + *ptr = id; + luaL_newmetatable(L, MQD_TYPENAME); + lua_setmetatable(L, -2); + return 1; +} + +static int l_mq_open(lua_State *L) +{ + mqd_t id, *ptr; + int flags; + flags = get_oflags(luaL_optlstring(L, 2, "", NULL)); + id = mq_open(luaL_checkstring(L, 1), flags); + if(id == (mqd_t)-1) { + lua_pushnil(L); + push_errno(L); + return 2; + } + ptr = (mqd_t*)lua_newuserdata(L, sizeof(mqd_t)); + *ptr = id; + luaL_newmetatable(L, MQD_TYPENAME); + lua_setmetatable(L, -2); + return 1; +} + +static int l_mq_send(lua_State *L) +{ + mqd_t* id; + unsigned int prio; + size_t len; + const char* str; + + id = luaL_checkudata(L, 1, MQD_TYPENAME); + str = luaL_checklstring(L, 2, &len); + prio = luaL_optint(L, 3, 0); + if(mq_send(*id, str, len, prio)) { + lua_pushnil(L); + push_errno(L); + return 2; + } + + lua_pushboolean(L, 1); + return 1; +} + +static int l_mq_receive(lua_State *L) +{ + mqd_t* id; + unsigned int prio; + struct mq_attr attr; + ssize_t len; + char* str; + + id = luaL_checkudata(L, 1, MQD_TYPENAME); + if(mq_getattr(*id, &attr)) { + lua_pushnil(L); + push_errno(L); + return 2; + } + + len = attr.mq_msgsize; + str = malloc(len); + if(!str) { + lua_pushnil(L); + lua_pushstring(L, "bad alloc"); + return 2; + } + + len = mq_receive(*id, str, len, &prio); + if(len < 0) { + free(str); + lua_pushnil(L); + push_errno(L); + return 2; + } + + lua_pushlstring(L, str, len); + free(str); + lua_pushinteger(L, prio); + return 2; +} + +static int l_mq_close(lua_State *L) +{ + mqd_t* id; + id = luaL_checkudata(L, 1, MQD_TYPENAME); + if(mq_close(*id)) { + lua_pushnil(L); + push_errno(L); + return 2; + } + + lua_pushboolean(L, 1); + return 1; +} + +static int l_mq_unlink(lua_State *L) +{ + if(mq_unlink(luaL_checkstring(L, 1))) { + lua_pushnil(L); + push_errno(L); + return 2; + } + + lua_pushboolean(L, 1); + return 1; +} + +static const struct luaL_reg mq_funcs [] = { + { "create", l_mq_create }, + { "open", l_mq_open }, + { "send", l_mq_send }, + { "receive", l_mq_receive }, + { "close", l_mq_close }, + { "unlink", l_mq_unlink }, + { NULL, NULL } +}; + +LUALIB_API int luaopen_mq(lua_State *L) +{ + luaL_register(L, "mq", mq_funcs); + return 1; +} diff --git a/src/luamq.c b/src/luamq.c deleted file mode 100644 index 53677fb..0000000 --- a/src/luamq.c +++ /dev/null @@ -1,180 +0,0 @@ -#include "lua.h" -#include "lauxlib.h" - -#include -#include -#define _XOPEN_SOURCE 600 -#include -#include -#include - -#define MQD_TYPENAME "mqd_t" - -LUALIB_API int luaopen_luamq(lua_State *L); - -static mode_t get_mode(const char* modestr) -{ -/* TODO: implement this!!! */ - return S_IRWXU | S_IRWXG; -} - -static int get_oflags(const char* flagstr) -{ - if(!strcmp(flagstr, "ro")) - return O_RDONLY; - - if(!strcmp(flagstr, "wo")) - return O_WRONLY; - - return O_RDWR; -} - -static void push_errno(lua_State *L) -{ - char msg[255]; - int ret; - - ret = strerror_r(errno, msg, sizeof(msg)); - if(!ret) - lua_pushstring(L, msg); - else - lua_pushstring(L, "unknown error"); -} - -static int l_luamq_create(lua_State *L) -{ - mqd_t id, *ptr; - int flags; - mode_t mode; - - flags = get_oflags(luaL_optstring(L, 2, "")); - mode = get_mode(luaL_optstring(L, 3, "")); - id = mq_open(luaL_checkstring(L, 1), flags | O_CREAT, mode, NULL); - if(id == (mqd_t)-1) { - lua_pushnil(L); - push_errno(L); - return 2; - } - ptr = (mqd_t*)lua_newuserdata(L, sizeof(mqd_t)); - *ptr = id; - luaL_newmetatable(L, MQD_TYPENAME); - lua_setmetatable(L, -2); - return 1; -} - -static int l_luamq_open(lua_State *L) -{ - mqd_t id, *ptr; - int flags; - flags = get_oflags(luaL_optlstring(L, 2, "", NULL)); - id = mq_open(luaL_checkstring(L, 1), flags); - if(id == (mqd_t)-1) { - lua_pushnil(L); - push_errno(L); - return 2; - } - ptr = (mqd_t*)lua_newuserdata(L, sizeof(mqd_t)); - *ptr = id; - luaL_newmetatable(L, MQD_TYPENAME); - lua_setmetatable(L, -2); - return 1; -} - -static int l_luamq_send(lua_State *L) -{ - mqd_t* id; - unsigned int prio; - size_t len; - const char* str; - - id = luaL_checkudata(L, 1, MQD_TYPENAME); - str = luaL_checklstring(L, 2, &len); - prio = luaL_optint(L, 3, 0); - if(mq_send(*id, str, len, prio)) { - lua_pushnil(L); - push_errno(L); - return 2; - } - - lua_pushboolean(L, 1); - return 1; -} - -static int l_luamq_receive(lua_State *L) -{ - mqd_t* id; - unsigned int prio; - struct mq_attr attr; - ssize_t len; - char* str; - - id = luaL_checkudata(L, 1, MQD_TYPENAME); - if(mq_getattr(*id, &attr)) { - lua_pushnil(L); - push_errno(L); - return 2; - } - - len = attr.mq_msgsize; - str = malloc(len); - if(!str) { - lua_pushnil(L); - lua_pushstring(L, "bad alloc"); - return 2; - } - - len = mq_receive(*id, str, len, &prio); - if(len < 0) { - free(str); - lua_pushnil(L); - push_errno(L); - return 2; - } - - lua_pushlstring(L, str, len); - free(str); - lua_pushinteger(L, prio); - return 2; -} - -static int l_luamq_close(lua_State *L) -{ - mqd_t* id; - id = luaL_checkudata(L, 1, MQD_TYPENAME); - if(mq_close(*id)) { - lua_pushnil(L); - push_errno(L); - return 2; - } - - lua_pushboolean(L, 1); - return 1; -} - -static int l_luamq_unlink(lua_State *L) -{ - if(mq_unlink(luaL_checkstring(L, 1))) { - lua_pushnil(L); - push_errno(L); - return 2; - } - - lua_pushboolean(L, 1); - return 1; -} - -static const struct luaL_reg luamq_funcs [] = { - { "create", l_luamq_create }, - { "open", l_luamq_open }, - { "send", l_luamq_send }, - { "receive", l_luamq_receive }, - { "close", l_luamq_close }, - { "unlink", l_luamq_unlink }, - { NULL, NULL } -}; - -LUALIB_API int luaopen_luamq(lua_State *L) -{ - luaL_register(L, "luamq", luamq_funcs); - return 1; -} diff --git a/test.lua b/test.lua index 8861fea..445e303 100755 --- a/test.lua +++ b/test.lua @@ -1,6 +1,6 @@ #!/usr/bin/lua -mq = require "luamq" +mq = require "mq" q, err = mq.create("/nownext", "wo") if q == nil then -- cgit v1.2.3