From 8a901ee2447e2a2f4f3abdd57bd67dbe5c575812 Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Thu, 22 Sep 2011 13:49:53 +0200 Subject: lua: added initial lua client --- lua-client.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 lua-client.c (limited to 'lua-client.c') diff --git a/lua-client.c b/lua-client.c new file mode 100644 index 0000000..9004dbf --- /dev/null +++ b/lua-client.c @@ -0,0 +1,77 @@ + +#include +#include + +#include +#include +#include + +#include +#include +#include + +#include "pingu_adm.h" + +#define LIBNAME "pingu.client" + +static int pusherror(lua_State *L, const char *info) +{ + lua_pushnil(L); + if (info == NULL) + lua_pushstring(L, strerror(errno)); + else + lua_pushfstring(L, "%s: %s", info, strerror(errno)); + lua_pushinteger(L, errno); + return 3; +} + +static int pushfile(lua_State *L, int fd, const char *mode) +{ + FILE **f = (FILE **)lua_newuserdata(L, sizeof(FILE *)); + *f = NULL; + luaL_getmetatable(L, "FILE*"); + lua_setmetatable(L, -2); + *f = fdopen(fd, mode); + return (*f != NULL); +} + +static int Padm_open(lua_State *L) +{ + const char *socket_path = luaL_optstring(L, 1, DEFAULT_ADM_SOCKET); + struct sockaddr_un sun; + int fd, ret; + + memset(&sun, 0, sizeof(sun)); + sun.sun_family = AF_UNIX; + strncpy(sun.sun_path, socket_path, sizeof(sun.sun_path)); + + fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (fd < 0) + return pusherror(L, "socket"); + + if (connect(fd, (struct sockaddr *) &sun, sizeof(sun)) < 0) { + ret = pusherror(L, socket_path); + goto close_err; + } + + return pushfile(L, fd, "r+"); + +close_err: + close(fd); + return ret; +} + +static const luaL_reg reg_pingu_methods[] = { + {"open", Padm_open}, + {NULL, NULL}, +}; + + +LUALIB_API int luaopen_pingu_client(lua_State *L) +{ + luaL_register(L, LIBNAME, reg_pingu_methods); + lua_pushliteral(L, "version"); + lua_pushliteral(L, PINGU_VERSION); + lua_settable(L, -3); + return 1; +} -- cgit v1.2.3