From 62a4c505e488c714e8795ea85564504562d30301 Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Sat, 24 Jan 2004 02:47:24 +0000 Subject: Working on the manual... Making better tests for error messages. Changed a few names. Moved gethostname to inet.c. --- src/inet.c | 21 +++++++++++++++++++++ src/luasocket.c | 27 --------------------------- src/mime.c | 16 ++++++++-------- src/mime.lua | 19 +++++++++++++------ 4 files changed, 42 insertions(+), 41 deletions(-) (limited to 'src') diff --git a/src/inet.c b/src/inet.c index dff4bf2..d626b86 100644 --- a/src/inet.c +++ b/src/inet.c @@ -19,11 +19,13 @@ static int inet_global_toip(lua_State *L); static int inet_global_tohostname(lua_State *L); static void inet_pushresolved(lua_State *L, struct hostent *hp); +static int inet_global_gethostname(lua_State *L); /* DNS functions */ static luaL_reg func[] = { { "toip", inet_global_toip }, { "tohostname", inet_global_tohostname }, + { "gethostname", inet_global_gethostname}, { NULL, NULL} }; @@ -101,6 +103,25 @@ static int inet_global_tohostname(lua_State *L) return 2; } +/*-------------------------------------------------------------------------*\ +* Gets the host name +\*-------------------------------------------------------------------------*/ +static int inet_global_gethostname(lua_State *L) +{ + char name[257]; + name[256] = '\0'; + if (gethostname(name, 256) < 0) { + lua_pushnil(L); + lua_pushstring(L, "gethostname failed"); + return 2; + } else { + lua_pushstring(L, name); + return 1; + } +} + + + /*=========================================================================*\ * Lua methods \*=========================================================================*/ diff --git a/src/luasocket.c b/src/luasocket.c index 73583a8..bfe71c2 100644 --- a/src/luasocket.c +++ b/src/luasocket.c @@ -38,15 +38,8 @@ /*=========================================================================*\ * Declarations \*=========================================================================*/ -static int global_gethostname(lua_State *L); static int base_open(lua_State *L); -/* functions in library namespace */ -static luaL_reg func[] = { - {"gethostname", global_gethostname}, - {NULL, NULL} -}; - /*-------------------------------------------------------------------------*\ * Setup basic stuff. \*-------------------------------------------------------------------------*/ @@ -70,29 +63,9 @@ static int base_open(lua_State *L) lua_pushstring(L, "LUASOCKET_LIBNAME"); lua_pushstring(L, LUASOCKET_LIBNAME); lua_settable(L, LUA_GLOBALSINDEX); - /* define library functions */ - luaL_openlib(L, LUASOCKET_LIBNAME, func, 0); - lua_pop(L, 1); return 0; } -/*-------------------------------------------------------------------------*\ -* Gets the host name -\*-------------------------------------------------------------------------*/ -static int global_gethostname(lua_State *L) -{ - char name[257]; - name[256] = '\0'; - if (gethostname(name, 256) < 0) { - lua_pushnil(L); - lua_pushstring(L, "gethostname failed"); - return 2; - } else { - lua_pushstring(L, name); - return 1; - } -} - /*-------------------------------------------------------------------------*\ * Initializes all library modules. \*-------------------------------------------------------------------------*/ diff --git a/src/mime.c b/src/mime.c index 8e97f8b..95cd400 100644 --- a/src/mime.c +++ b/src/mime.c @@ -27,12 +27,12 @@ static const char EQCRLF[3] = {'=', CR, LF}; /*=========================================================================*\ * Internal function prototypes. \*=========================================================================*/ -static int mime_global_fmt(lua_State *L); +static int mime_global_wrp(lua_State *L); static int mime_global_b64(lua_State *L); static int mime_global_unb64(lua_State *L); static int mime_global_qp(lua_State *L); static int mime_global_unqp(lua_State *L); -static int mime_global_qpfmt(lua_State *L); +static int mime_global_qpwrp(lua_State *L); static int mime_global_eol(lua_State *L); static void b64setup(UC *b64unbase); @@ -54,10 +54,10 @@ static luaL_reg func[] = { { "eol", mime_global_eol }, { "qp", mime_global_qp }, { "unqp", mime_global_unqp }, - { "qpfmt", mime_global_qpfmt }, + { "qpwrp", mime_global_qpwrp }, { "b64", mime_global_b64 }, { "unb64", mime_global_unb64 }, - { "fmt", mime_global_fmt }, + { "wrp", mime_global_wrp }, { NULL, NULL } }; @@ -127,13 +127,13 @@ static const char *optlstring(lua_State *L, int n, const char *v, size_t *l) \*=========================================================================*/ /*-------------------------------------------------------------------------*\ * Incrementaly breaks a string into lines -* A, n = fmt(l, B, length, marker) +* A, n = wrp(l, B, length, marker) * A is a copy of B, broken into lines of at most 'length' bytes. * 'l' is how many bytes are left for the first line of B. * 'n' is the number of bytes left in the last line of A. * Marker is the end-of-line marker. \*-------------------------------------------------------------------------*/ -static int mime_global_fmt(lua_State *L) +static int mime_global_wrp(lua_State *L) { size_t size = 0; int left = (int) luaL_checknumber(L, 1); @@ -526,14 +526,14 @@ static int mime_global_unqp(lua_State *L) /*-------------------------------------------------------------------------*\ * Incrementally breaks a quoted-printed string into lines -* A, n = qpfmt(l, B, length) +* A, n = qpwrp(l, B, length) * A is a copy of B, broken into lines of at most 'length' bytes. * 'l' is how many bytes are left for the first line of B. * 'n' is the number of bytes left in the last line of A. * There are two complications: lines can't be broken in the middle * of an encoded =XX, and there might be line breaks already \*-------------------------------------------------------------------------*/ -static int mime_global_qpfmt(lua_State *L) +static int mime_global_qpwrp(lua_State *L) { size_t size = 0; int left = (int) luaL_checknumber(L, 1); diff --git a/src/mime.lua b/src/mime.lua index 0251f6e..30c6b38 100644 --- a/src/mime.lua +++ b/src/mime.lua @@ -19,7 +19,7 @@ local wt = {} local function choose(table) return function(method, ...) local f = table[method or "nil"] - if not f then return nil, "unknown method (" .. tostring(method) .. ")" + if not f then error("unknown method (" .. tostring(method) .. ")", 3) else return f(unpack(arg)) end end end @@ -37,7 +37,15 @@ end -- function that choose the encoding, decoding or wrap algorithm encode = choose(et) decode = choose(dt) -wrap = choose(wt) + +-- the wrap filter has default parameters +local cwt = choose(wt) +function wrap(...) + if not arg[1] or type(arg[1]) ~= "string" then + table.insert(arg, 1, "base64") + end + return cwt(unpack(arg)) +end -- define the encoding algorithms et['base64'] = function() @@ -58,15 +66,14 @@ dt['quoted-printable'] = function() end -- define the wrap algorithms -wt['character'] = function(length) +wt['base64'] = function(length, marker) length = length or 76 - return cicle(fmt, length, length) + return cicle(wrp, length, length, marker) end -wt['base64'] = wt['character'] wt['quoted-printable'] = function(length) length = length or 76 - return cicle(qpfmt, length, length) + return cicle(qpwrp, length, length) end -- define the end-of-line translation function -- cgit v1.2.3-55-g6feb