diff options
Diffstat (limited to 'util-src')
-rw-r--r-- | util-src/encodings.c | 23 | ||||
-rw-r--r-- | util-src/hashes.c | 10 | ||||
-rw-r--r-- | util-src/net.c | 7 | ||||
-rw-r--r-- | util-src/pposix.c | 81 | ||||
-rw-r--r-- | util-src/signal.c | 9 | ||||
-rw-r--r-- | util-src/windows.c | 10 |
6 files changed, 84 insertions, 56 deletions
diff --git a/util-src/encodings.c b/util-src/encodings.c index b9b6160a..2d5d49d4 100644 --- a/util-src/encodings.c +++ b/util-src/encodings.c @@ -20,6 +20,10 @@ #include "lua.h" #include "lauxlib.h" +#if (LUA_VERSION_NUM == 502) +#define luaL_register(L, N, R) luaL_setfuncs(L, R, 0) +#endif + /***************** BASE64 *****************/ static const char code[]= @@ -361,35 +365,26 @@ static const luaL_Reg Reg_idna[] = /***************** end *****************/ -static const luaL_Reg Reg[] = -{ - { NULL, NULL } -}; - LUALIB_API int luaopen_util_encodings(lua_State *L) { #ifdef USE_STRINGPREP_ICU init_icu(); #endif - luaL_register(L, "encodings", Reg); + lua_newtable(L); - lua_pushliteral(L, "base64"); lua_newtable(L); luaL_register(L, NULL, Reg_base64); - lua_settable(L,-3); + lua_setfield(L, -2, "base64"); - lua_pushliteral(L, "stringprep"); lua_newtable(L); luaL_register(L, NULL, Reg_stringprep); - lua_settable(L,-3); + lua_setfield(L, -2, "stringprep"); - lua_pushliteral(L, "idna"); lua_newtable(L); luaL_register(L, NULL, Reg_idna); - lua_settable(L,-3); + lua_setfield(L, -2, "idna"); - lua_pushliteral(L, "version"); /** version */ lua_pushliteral(L, "-3.14"); - lua_settable(L,-3); + lua_setfield(L, -2, "version"); return 1; } diff --git a/util-src/hashes.c b/util-src/hashes.c index 33041e83..459f75ac 100644 --- a/util-src/hashes.c +++ b/util-src/hashes.c @@ -27,6 +27,10 @@ typedef unsigned __int32 uint32_t; #include <openssl/sha.h> #include <openssl/md5.h> +#if (LUA_VERSION_NUM == 502) +#define luaL_register(L, N, R) luaL_setfuncs(L, R, 0) +#endif + #define HMAC_IPAD 0x36363636 #define HMAC_OPAD 0x5c5c5c5c @@ -203,9 +207,9 @@ static const luaL_Reg Reg[] = LUALIB_API int luaopen_util_hashes(lua_State *L) { - luaL_register(L, "hashes", Reg); - lua_pushliteral(L, "version"); /** version */ + lua_newtable(L); + luaL_register(L, NULL, Reg); lua_pushliteral(L, "-3.14"); - lua_settable(L,-3); + lua_setfield(L, -2, "version"); return 1; } diff --git a/util-src/net.c b/util-src/net.c index e307c628..9e57854c 100644 --- a/util-src/net.c +++ b/util-src/net.c @@ -26,6 +26,10 @@ #include <lua.h> #include <lauxlib.h> +#if (LUA_VERSION_NUM == 502) +#define luaL_register(L, N, R) luaL_setfuncs(L, R, 0) +#endif + /* Enumerate all locally configured IP addresses */ const char * const type_strings[] = { @@ -112,6 +116,7 @@ int luaopen_util_net(lua_State* L) { NULL, NULL } }; - luaL_register(L, "net", exports); + lua_newtable(L); + luaL_register(L, NULL, exports); return 1; } diff --git a/util-src/pposix.c b/util-src/pposix.c index df814c28..49e80f71 100644 --- a/util-src/pposix.c +++ b/util-src/pposix.c @@ -35,6 +35,10 @@ #include "lualib.h" #include "lauxlib.h" +#if (LUA_VERSION_NUM == 502) +#define luaL_register(L, N, R) luaL_setfuncs(L, R, 0) +#endif + #include <fcntl.h> #if defined(__linux__) && defined(_GNU_SOURCE) #include <linux/falloc.h> @@ -491,11 +495,24 @@ int string2resource(const char *s) { return -1; } +unsigned long int arg_to_rlimit(lua_State* L, int idx, rlim_t current) { + switch(lua_type(L, idx)) { + case LUA_TSTRING: + if(strcmp(lua_tostring(L, idx), "unlimited") == 0) + return RLIM_INFINITY; + case LUA_TNUMBER: + return lua_tointeger(L, idx); + case LUA_TNONE: + case LUA_TNIL: + return current; + default: + return luaL_argerror(L, idx, "unexpected type"); + } +} + int lc_setrlimit(lua_State *L) { + struct rlimit lim; int arguments = lua_gettop(L); - int softlimit = -1; - int hardlimit = -1; - const char *resource = NULL; int rid = -1; if(arguments < 1 || arguments > 3) { lua_pushboolean(L, 0); @@ -503,37 +520,26 @@ int lc_setrlimit(lua_State *L) { return 2; } - resource = luaL_checkstring(L, 1); - softlimit = luaL_checkinteger(L, 2); - hardlimit = luaL_checkinteger(L, 3); + rid = string2resource(luaL_checkstring(L, 1)); + if (rid == -1) { + lua_pushboolean(L, 0); + lua_pushstring(L, "invalid-resource"); + return 2; + } - rid = string2resource(resource); - if (rid != -1) { - struct rlimit lim; - struct rlimit lim_current; - - if (softlimit < 0 || hardlimit < 0) { - if (getrlimit(rid, &lim_current)) { - lua_pushboolean(L, 0); - lua_pushstring(L, "getrlimit-failed"); - return 2; - } - } + /* Fetch current values to use as defaults */ + if (getrlimit(rid, &lim)) { + lua_pushboolean(L, 0); + lua_pushstring(L, "getrlimit-failed"); + return 2; + } - if (softlimit < 0) lim.rlim_cur = lim_current.rlim_cur; - else lim.rlim_cur = softlimit; - if (hardlimit < 0) lim.rlim_max = lim_current.rlim_max; - else lim.rlim_max = hardlimit; + lim.rlim_cur = arg_to_rlimit(L, 2, lim.rlim_cur); + lim.rlim_max = arg_to_rlimit(L, 3, lim.rlim_max); - if (setrlimit(rid, &lim)) { - lua_pushboolean(L, 0); - lua_pushstring(L, "setrlimit-failed"); - return 2; - } - } else { - /* Unsupported resoucrce. Sorry I'm pretty limited by POSIX standard. */ + if (setrlimit(rid, &lim)) { lua_pushboolean(L, 0); - lua_pushstring(L, "invalid-resource"); + lua_pushstring(L, "setrlimit-failed"); return 2; } lua_pushboolean(L, 1); @@ -552,6 +558,8 @@ int lc_getrlimit(lua_State *L) { return 2; } + + resource = luaL_checkstring(L, 1); rid = string2resource(resource); if (rid != -1) { @@ -567,8 +575,14 @@ int lc_getrlimit(lua_State *L) { return 2; } lua_pushboolean(L, 1); - lua_pushnumber(L, lim.rlim_cur); - lua_pushnumber(L, lim.rlim_max); + if(lim.rlim_cur == RLIM_INFINITY) + lua_pushstring(L, "unlimited"); + else + lua_pushnumber(L, lim.rlim_cur); + if(lim.rlim_max == RLIM_INFINITY) + lua_pushstring(L, "unlimited"); + else + lua_pushnumber(L, lim.rlim_max); return 3; } @@ -758,7 +772,8 @@ int luaopen_util_pposix(lua_State *L) { NULL, NULL } }; - luaL_register(L, "pposix", exports); + lua_newtable(L); + luaL_register(L, NULL, exports); lua_pushliteral(L, "pposix"); lua_setfield(L, -2, "_NAME"); diff --git a/util-src/signal.c b/util-src/signal.c index 961d2d3e..63d65570 100644 --- a/util-src/signal.c +++ b/util-src/signal.c @@ -32,6 +32,10 @@ #include "lua.h" #include "lauxlib.h" +#if (LUA_VERSION_NUM == 502) +#define luaL_register(L, N, R) luaL_setfuncs(L, R, 0) +#endif + #ifndef lsig #define lsig @@ -384,13 +388,14 @@ int luaopen_util_signal(lua_State *L) int i = 0; /* add the library */ - luaL_register(L, "signal", lsignal_lib); + lua_newtable(L); + luaL_register(L, NULL, lsignal_lib); /* push lua_signals table into the registry */ /* put the signals inside the library table too, * they are only a reference */ lua_pushstring(L, LUA_SIGNAL); - lua_createtable(L, 0, 0); + lua_newtable(L); while (lua_signals[i].name != NULL) { diff --git a/util-src/windows.c b/util-src/windows.c index 3d14ca95..37f850e3 100644 --- a/util-src/windows.c +++ b/util-src/windows.c @@ -19,6 +19,10 @@ #include "lua.h" #include "lauxlib.h" +#if (LUA_VERSION_NUM == 502) +#define luaL_register(L, N, R) luaL_setfuncs(L, R, 0) +#endif + static int Lget_nameservers(lua_State *L) { char stack_buffer[1024]; // stack allocated buffer IP4_ARRAY* ips = (IP4_ARRAY*) stack_buffer; @@ -81,9 +85,9 @@ static const luaL_Reg Reg[] = }; LUALIB_API int luaopen_util_windows(lua_State *L) { - luaL_register(L, "windows", Reg); - lua_pushliteral(L, "version"); /** version */ + lua_newtable(L); + luaL_register(L, NULL, Reg); lua_pushliteral(L, "-3.14"); - lua_settable(L,-3); + lua_setfield(L, -2, "version"); return 1; } |