diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-01-24 02:47:24 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-01-24 02:47:24 +0000 |
| commit | 62a4c505e488c714e8795ea85564504562d30301 (patch) | |
| tree | f270f53f26905b7275cea172e52ebc269a41c695 /src | |
| parent | 0c9f420a3549df3fb331bb24157b65a3301641d4 (diff) | |
| download | luasocket-62a4c505e488c714e8795ea85564504562d30301.tar.gz luasocket-62a4c505e488c714e8795ea85564504562d30301.tar.bz2 luasocket-62a4c505e488c714e8795ea85564504562d30301.zip | |
Working on the manual...
Making better tests for error messages.
Changed a few names.
Moved gethostname to inet.c.
Diffstat (limited to 'src')
| -rw-r--r-- | src/inet.c | 21 | ||||
| -rw-r--r-- | src/luasocket.c | 27 | ||||
| -rw-r--r-- | src/mime.c | 16 | ||||
| -rw-r--r-- | src/mime.lua | 19 |
4 files changed, 42 insertions, 41 deletions
| @@ -19,11 +19,13 @@ | |||
| 19 | static int inet_global_toip(lua_State *L); | 19 | static int inet_global_toip(lua_State *L); |
| 20 | static int inet_global_tohostname(lua_State *L); | 20 | static int inet_global_tohostname(lua_State *L); |
| 21 | static void inet_pushresolved(lua_State *L, struct hostent *hp); | 21 | static void inet_pushresolved(lua_State *L, struct hostent *hp); |
| 22 | static int inet_global_gethostname(lua_State *L); | ||
| 22 | 23 | ||
| 23 | /* DNS functions */ | 24 | /* DNS functions */ |
| 24 | static luaL_reg func[] = { | 25 | static luaL_reg func[] = { |
| 25 | { "toip", inet_global_toip }, | 26 | { "toip", inet_global_toip }, |
| 26 | { "tohostname", inet_global_tohostname }, | 27 | { "tohostname", inet_global_tohostname }, |
| 28 | { "gethostname", inet_global_gethostname}, | ||
| 27 | { NULL, NULL} | 29 | { NULL, NULL} |
| 28 | }; | 30 | }; |
| 29 | 31 | ||
| @@ -101,6 +103,25 @@ static int inet_global_tohostname(lua_State *L) | |||
| 101 | return 2; | 103 | return 2; |
| 102 | } | 104 | } |
| 103 | 105 | ||
| 106 | /*-------------------------------------------------------------------------*\ | ||
| 107 | * Gets the host name | ||
| 108 | \*-------------------------------------------------------------------------*/ | ||
| 109 | static int inet_global_gethostname(lua_State *L) | ||
| 110 | { | ||
| 111 | char name[257]; | ||
| 112 | name[256] = '\0'; | ||
| 113 | if (gethostname(name, 256) < 0) { | ||
| 114 | lua_pushnil(L); | ||
| 115 | lua_pushstring(L, "gethostname failed"); | ||
| 116 | return 2; | ||
| 117 | } else { | ||
| 118 | lua_pushstring(L, name); | ||
| 119 | return 1; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | |||
| 124 | |||
| 104 | /*=========================================================================*\ | 125 | /*=========================================================================*\ |
| 105 | * Lua methods | 126 | * Lua methods |
| 106 | \*=========================================================================*/ | 127 | \*=========================================================================*/ |
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 @@ | |||
| 38 | /*=========================================================================*\ | 38 | /*=========================================================================*\ |
| 39 | * Declarations | 39 | * Declarations |
| 40 | \*=========================================================================*/ | 40 | \*=========================================================================*/ |
| 41 | static int global_gethostname(lua_State *L); | ||
| 42 | static int base_open(lua_State *L); | 41 | static int base_open(lua_State *L); |
| 43 | 42 | ||
| 44 | /* functions in library namespace */ | ||
| 45 | static luaL_reg func[] = { | ||
| 46 | {"gethostname", global_gethostname}, | ||
| 47 | {NULL, NULL} | ||
| 48 | }; | ||
| 49 | |||
| 50 | /*-------------------------------------------------------------------------*\ | 43 | /*-------------------------------------------------------------------------*\ |
| 51 | * Setup basic stuff. | 44 | * Setup basic stuff. |
| 52 | \*-------------------------------------------------------------------------*/ | 45 | \*-------------------------------------------------------------------------*/ |
| @@ -70,30 +63,10 @@ static int base_open(lua_State *L) | |||
| 70 | lua_pushstring(L, "LUASOCKET_LIBNAME"); | 63 | lua_pushstring(L, "LUASOCKET_LIBNAME"); |
| 71 | lua_pushstring(L, LUASOCKET_LIBNAME); | 64 | lua_pushstring(L, LUASOCKET_LIBNAME); |
| 72 | lua_settable(L, LUA_GLOBALSINDEX); | 65 | lua_settable(L, LUA_GLOBALSINDEX); |
| 73 | /* define library functions */ | ||
| 74 | luaL_openlib(L, LUASOCKET_LIBNAME, func, 0); | ||
| 75 | lua_pop(L, 1); | ||
| 76 | return 0; | 66 | return 0; |
| 77 | } | 67 | } |
| 78 | 68 | ||
| 79 | /*-------------------------------------------------------------------------*\ | 69 | /*-------------------------------------------------------------------------*\ |
| 80 | * Gets the host name | ||
| 81 | \*-------------------------------------------------------------------------*/ | ||
| 82 | static int global_gethostname(lua_State *L) | ||
| 83 | { | ||
| 84 | char name[257]; | ||
| 85 | name[256] = '\0'; | ||
| 86 | if (gethostname(name, 256) < 0) { | ||
| 87 | lua_pushnil(L); | ||
| 88 | lua_pushstring(L, "gethostname failed"); | ||
| 89 | return 2; | ||
| 90 | } else { | ||
| 91 | lua_pushstring(L, name); | ||
| 92 | return 1; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | /*-------------------------------------------------------------------------*\ | ||
| 97 | * Initializes all library modules. | 70 | * Initializes all library modules. |
| 98 | \*-------------------------------------------------------------------------*/ | 71 | \*-------------------------------------------------------------------------*/ |
| 99 | LUASOCKET_API int luaopen_socket(lua_State *L) | 72 | LUASOCKET_API int luaopen_socket(lua_State *L) |
| @@ -27,12 +27,12 @@ static const char EQCRLF[3] = {'=', CR, LF}; | |||
| 27 | /*=========================================================================*\ | 27 | /*=========================================================================*\ |
| 28 | * Internal function prototypes. | 28 | * Internal function prototypes. |
| 29 | \*=========================================================================*/ | 29 | \*=========================================================================*/ |
| 30 | static int mime_global_fmt(lua_State *L); | 30 | static int mime_global_wrp(lua_State *L); |
| 31 | static int mime_global_b64(lua_State *L); | 31 | static int mime_global_b64(lua_State *L); |
| 32 | static int mime_global_unb64(lua_State *L); | 32 | static int mime_global_unb64(lua_State *L); |
| 33 | static int mime_global_qp(lua_State *L); | 33 | static int mime_global_qp(lua_State *L); |
| 34 | static int mime_global_unqp(lua_State *L); | 34 | static int mime_global_unqp(lua_State *L); |
| 35 | static int mime_global_qpfmt(lua_State *L); | 35 | static int mime_global_qpwrp(lua_State *L); |
| 36 | static int mime_global_eol(lua_State *L); | 36 | static int mime_global_eol(lua_State *L); |
| 37 | 37 | ||
| 38 | static void b64setup(UC *b64unbase); | 38 | static void b64setup(UC *b64unbase); |
| @@ -54,10 +54,10 @@ static luaL_reg func[] = { | |||
| 54 | { "eol", mime_global_eol }, | 54 | { "eol", mime_global_eol }, |
| 55 | { "qp", mime_global_qp }, | 55 | { "qp", mime_global_qp }, |
| 56 | { "unqp", mime_global_unqp }, | 56 | { "unqp", mime_global_unqp }, |
| 57 | { "qpfmt", mime_global_qpfmt }, | 57 | { "qpwrp", mime_global_qpwrp }, |
| 58 | { "b64", mime_global_b64 }, | 58 | { "b64", mime_global_b64 }, |
| 59 | { "unb64", mime_global_unb64 }, | 59 | { "unb64", mime_global_unb64 }, |
| 60 | { "fmt", mime_global_fmt }, | 60 | { "wrp", mime_global_wrp }, |
| 61 | { NULL, NULL } | 61 | { NULL, NULL } |
| 62 | }; | 62 | }; |
| 63 | 63 | ||
| @@ -127,13 +127,13 @@ static const char *optlstring(lua_State *L, int n, const char *v, size_t *l) | |||
| 127 | \*=========================================================================*/ | 127 | \*=========================================================================*/ |
| 128 | /*-------------------------------------------------------------------------*\ | 128 | /*-------------------------------------------------------------------------*\ |
| 129 | * Incrementaly breaks a string into lines | 129 | * Incrementaly breaks a string into lines |
| 130 | * A, n = fmt(l, B, length, marker) | 130 | * A, n = wrp(l, B, length, marker) |
| 131 | * A is a copy of B, broken into lines of at most 'length' bytes. | 131 | * A is a copy of B, broken into lines of at most 'length' bytes. |
| 132 | * 'l' is how many bytes are left for the first line of B. | 132 | * 'l' is how many bytes are left for the first line of B. |
| 133 | * 'n' is the number of bytes left in the last line of A. | 133 | * 'n' is the number of bytes left in the last line of A. |
| 134 | * Marker is the end-of-line marker. | 134 | * Marker is the end-of-line marker. |
| 135 | \*-------------------------------------------------------------------------*/ | 135 | \*-------------------------------------------------------------------------*/ |
| 136 | static int mime_global_fmt(lua_State *L) | 136 | static int mime_global_wrp(lua_State *L) |
| 137 | { | 137 | { |
| 138 | size_t size = 0; | 138 | size_t size = 0; |
| 139 | int left = (int) luaL_checknumber(L, 1); | 139 | int left = (int) luaL_checknumber(L, 1); |
| @@ -526,14 +526,14 @@ static int mime_global_unqp(lua_State *L) | |||
| 526 | 526 | ||
| 527 | /*-------------------------------------------------------------------------*\ | 527 | /*-------------------------------------------------------------------------*\ |
| 528 | * Incrementally breaks a quoted-printed string into lines | 528 | * Incrementally breaks a quoted-printed string into lines |
| 529 | * A, n = qpfmt(l, B, length) | 529 | * A, n = qpwrp(l, B, length) |
| 530 | * A is a copy of B, broken into lines of at most 'length' bytes. | 530 | * A is a copy of B, broken into lines of at most 'length' bytes. |
| 531 | * 'l' is how many bytes are left for the first line of B. | 531 | * 'l' is how many bytes are left for the first line of B. |
| 532 | * 'n' is the number of bytes left in the last line of A. | 532 | * 'n' is the number of bytes left in the last line of A. |
| 533 | * There are two complications: lines can't be broken in the middle | 533 | * There are two complications: lines can't be broken in the middle |
| 534 | * of an encoded =XX, and there might be line breaks already | 534 | * of an encoded =XX, and there might be line breaks already |
| 535 | \*-------------------------------------------------------------------------*/ | 535 | \*-------------------------------------------------------------------------*/ |
| 536 | static int mime_global_qpfmt(lua_State *L) | 536 | static int mime_global_qpwrp(lua_State *L) |
| 537 | { | 537 | { |
| 538 | size_t size = 0; | 538 | size_t size = 0; |
| 539 | int left = (int) luaL_checknumber(L, 1); | 539 | 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 = {} | |||
| 19 | local function choose(table) | 19 | local function choose(table) |
| 20 | return function(method, ...) | 20 | return function(method, ...) |
| 21 | local f = table[method or "nil"] | 21 | local f = table[method or "nil"] |
| 22 | if not f then return nil, "unknown method (" .. tostring(method) .. ")" | 22 | if not f then error("unknown method (" .. tostring(method) .. ")", 3) |
| 23 | else return f(unpack(arg)) end | 23 | else return f(unpack(arg)) end |
| 24 | end | 24 | end |
| 25 | end | 25 | end |
| @@ -37,7 +37,15 @@ end | |||
| 37 | -- function that choose the encoding, decoding or wrap algorithm | 37 | -- function that choose the encoding, decoding or wrap algorithm |
| 38 | encode = choose(et) | 38 | encode = choose(et) |
| 39 | decode = choose(dt) | 39 | decode = choose(dt) |
| 40 | wrap = choose(wt) | 40 | |
| 41 | -- the wrap filter has default parameters | ||
| 42 | local cwt = choose(wt) | ||
| 43 | function wrap(...) | ||
| 44 | if not arg[1] or type(arg[1]) ~= "string" then | ||
| 45 | table.insert(arg, 1, "base64") | ||
| 46 | end | ||
| 47 | return cwt(unpack(arg)) | ||
| 48 | end | ||
| 41 | 49 | ||
| 42 | -- define the encoding algorithms | 50 | -- define the encoding algorithms |
| 43 | et['base64'] = function() | 51 | et['base64'] = function() |
| @@ -58,15 +66,14 @@ dt['quoted-printable'] = function() | |||
| 58 | end | 66 | end |
| 59 | 67 | ||
| 60 | -- define the wrap algorithms | 68 | -- define the wrap algorithms |
| 61 | wt['character'] = function(length) | 69 | wt['base64'] = function(length, marker) |
| 62 | length = length or 76 | 70 | length = length or 76 |
| 63 | return cicle(fmt, length, length) | 71 | return cicle(wrp, length, length, marker) |
| 64 | end | 72 | end |
| 65 | wt['base64'] = wt['character'] | ||
| 66 | 73 | ||
| 67 | wt['quoted-printable'] = function(length) | 74 | wt['quoted-printable'] = function(length) |
| 68 | length = length or 76 | 75 | length = length or 76 |
| 69 | return cicle(qpfmt, length, length) | 76 | return cicle(qpwrp, length, length) |
| 70 | end | 77 | end |
| 71 | 78 | ||
| 72 | -- define the end-of-line translation function | 79 | -- define the end-of-line translation function |
