Skip to content

Commit

Permalink
Merge pull request luvit#95 from luvit/uniform-type-strings
Browse files Browse the repository at this point in the history
R4R: Uniform type strings
  • Loading branch information
creationix committed Dec 2, 2014
2 parents 8c10a13 + 98a35a3 commit f373d2e
Show file tree
Hide file tree
Showing 12 changed files with 768 additions and 275 deletions.
692 changes: 692 additions & 0 deletions src/constants.c

Large diffs are not rendered by default.

115 changes: 23 additions & 92 deletions src/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static void luv_getaddrinfo_cb(uv_getaddrinfo_t* req, int status, struct addrinf
addr = (char*) &((struct sockaddr_in6*) curr->ai_addr)->sin6_addr;
port = ((struct sockaddr_in6*) curr->ai_addr)->sin6_port;
}
lua_pushstring(L, luv_protocol_to_string(curr->ai_family));
lua_pushstring(L, luv_af_num_to_string(curr->ai_family));
lua_setfield(L, -2, "family");
uv_inet_ntop(curr->ai_family, addr, ip, INET6_ADDRSTRLEN);
lua_pushstring(L, ip);
Expand All @@ -56,9 +56,9 @@ static void luv_getaddrinfo_cb(uv_getaddrinfo_t* req, int status, struct addrinf
lua_pushinteger(L, ntohs(port));
lua_setfield(L, -2, "port");
}
lua_pushstring(L, luv_socktype_to_string(curr->ai_socktype));
lua_pushstring(L, luv_sock_num_to_string(curr->ai_socktype));
lua_setfield(L, -2, "socktype");
lua_pushstring(L, luv_protocol_to_string(curr->ai_protocol));
lua_pushstring(L, luv_af_num_to_string(curr->ai_protocol));
lua_setfield(L, -2, "protocol");
if (curr->ai_canonname) {
lua_pushstring(L, curr->ai_canonname);
Expand Down Expand Up @@ -96,8 +96,11 @@ static int luv_getaddrinfo(lua_State* L) {

// Process the `family` hint.
lua_getfield(L, 3, "family");
if (lua_isstring(L, -1)) {
hints->ai_family = luv_string_to_protocol(lua_tostring(L, -1));
if (lua_isnumber(L, -1)) {
hints->ai_family = lua_tointeger(L, -1);
}
else if (lua_isstring(L, -1)) {
hints->ai_family = luv_af_string_to_num(lua_tostring(L, -1));
}
else if (lua_isnil(L, -1)) {
hints->ai_family = AF_UNSPEC;
Expand All @@ -109,8 +112,11 @@ static int luv_getaddrinfo(lua_State* L) {

// Process `socktype` hint
lua_getfield(L, 3, "socktype");
if (lua_isstring(L, -1)) {
hints->ai_socktype = luv_string_to_socktype(lua_tostring(L, -1));
if (lua_isnumber(L, -1)) {
hints->ai_socktype = lua_tointeger(L, -1);
}
else if (lua_isstring(L, -1)) {
hints->ai_socktype = luv_sock_string_to_num(lua_tostring(L, -1));
}
else if (!lua_isnil(L, -1)) {
return luaL_argerror(L, 3, "socktype hint must be string if set");
Expand All @@ -119,8 +125,11 @@ static int luv_getaddrinfo(lua_State* L) {

// Process the `protocol` hint
lua_getfield(L, 3, "protocol");
if (lua_isstring(L, -1)) {
int protocol = luv_string_to_protocol(lua_tostring(L, -1));
if (lua_isnumber(L, -1)) {
hints->ai_protocol = lua_tointeger(L, -1);
}
else if (lua_isstring(L, -1)) {
int protocol = luv_af_string_to_num(lua_tostring(L, -1));
if (protocol) {
hints->ai_protocol = protocol;
}
Expand Down Expand Up @@ -243,8 +252,11 @@ static int luv_getnameinfo(lua_State* L) {
}

lua_getfield(L, 1, "family");
if (lua_isstring(L, -1)) {
addr.ss_family = luv_string_to_protocol(lua_tostring(L, -1));
if (lua_isnumber(L, -1)) {
addr.ss_family = lua_tointeger(L, -1);
}
else if (lua_isstring(L, -1)) {
addr.ss_family = luv_af_string_to_num(lua_tostring(L, -1));
}
else if (!lua_isnil(L, -1)) {
luaL_argerror(L, 1, "family must be string if set");
Expand All @@ -265,84 +277,3 @@ static int luv_getnameinfo(lua_State* L) {
return 1;
}

static int luv_string_to_socktype(const char* string) {
if (!string) return 0;
if (strcmp(string, "STREAM") == 0) return SOCK_STREAM;
if (strcmp(string, "DGRAM") == 0) return SOCK_DGRAM;
return 0;
}

static const char* luv_socktype_to_string(int socktype) {
if (socktype == SOCK_STREAM) return "STREAM";
if (socktype == SOCK_DGRAM) return "DGRAM";
return NULL;
}

static int luv_string_to_protocol(const char* protocol) {
if (!protocol) return AF_UNSPEC;
#ifdef AF_UNIX
if (strcmp(protocol, "UNIX") == 0) return AF_UNIX;
#endif
#ifdef AF_INET
if (strcmp(protocol, "INET") == 0) return AF_INET;
#endif
#ifdef AF_INET6
if (strcmp(protocol, "INET6") == 0) return AF_INET6;
#endif
#ifdef AF_IPX
if (strcmp(protocol, "IPX") == 0) return AF_IPX;
#endif
#ifdef AF_NETLINK
if (strcmp(protocol, "NETLINK") == 0) return AF_NETLINK;
#endif
#ifdef AF_X25
if (strcmp(protocol, "X25") == 0) return AF_X25;
#endif
#ifdef AF_AX25
if (strcmp(protocol, "AX25") == 0) return AF_AX25;
#endif
#ifdef AF_ATMPVC
if (strcmp(protocol, "ATMPVC") == 0) return AF_ATMPVC;
#endif
#ifdef AF_APPLETALK
if (strcmp(protocol, "APPLETALK") == 0) return AF_APPLETALK;
#endif
#ifdef AF_PACKET
if (strcmp(protocol, "PACKET") == 0) return AF_PACKET;
#endif
return 0;
}

static const char* luv_protocol_to_string(int family) {
#ifdef AF_UNIX
if (family == AF_UNIX) return "UNIX";
#endif
#ifdef AF_INET
if (family == AF_INET) return "INET";
#endif
#ifdef AF_INET6
if (family == AF_INET6) return "INET6";
#endif
#ifdef AF_IPX
if (family == AF_IPX) return "IPX";
#endif
#ifdef AF_NETLINK
if (family == AF_NETLINK) return "NETLINK";
#endif
#ifdef AF_X25
if (family == AF_X25) return "X25";
#endif
#ifdef AF_AX25
if (family == AF_AX25) return "AX25";
#endif
#ifdef AF_ATMPVC
if (family == AF_ATMPVC) return "ATMPVC";
#endif
#ifdef AF_APPLETALK
if (family == AF_APPLETALK) return "APPLETALK";
#endif
#ifdef AF_PACKET
if (family == AF_PACKET) return "PACKET";
#endif
return NULL;
}
37 changes: 18 additions & 19 deletions src/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static void luv_push_timespec_table(lua_State* L, const uv_timespec_t* t) {
}

static void luv_push_stats_table(lua_State* L, const uv_stat_t* s) {
const char* type;
const char* type = NULL;
lua_createtable(L, 0, 23);
lua_pushinteger(L, s->st_dev);
lua_setfield(L, -2, "dev");
Expand Down Expand Up @@ -67,33 +67,32 @@ static void luv_push_stats_table(lua_State* L, const uv_stat_t* s) {
luv_push_timespec_table(L, &s->st_birthtim);
lua_setfield(L, -2, "birthtim");
if (S_ISREG(s->st_mode)) {
type = "FILE";
type = "file";
}
else if (S_ISDIR(s->st_mode)) {
type = "DIRECTORY";
type = "directory";
}
else if (S_ISLNK(s->st_mode)) {
type = "LINK";
type = "link";
}
else if (S_ISFIFO(s->st_mode)) {
type = "FIFO";
type = "fifo";
}
#ifdef S_ISSOCK
else if (S_ISSOCK(s->st_mode)) {
type = "SOCKET";
type = "socket";
}
#endif
else if (S_ISCHR(s->st_mode)) {
type = "CHAR";
type = "char";
}
else if (S_ISBLK(s->st_mode)) {
type = "BLOCK";
type = "block";
}
else {
type = "UNKNOWN";
if (type) {
lua_pushstring(L, type);
lua_setfield(L, -2, "type");
}
lua_pushstring(L, type);
lua_setfield(L, -2, "type");
}

static int luv_check_flags(lua_State* L, int index) {
Expand Down Expand Up @@ -383,13 +382,13 @@ static int luv_fs_scandir_next(lua_State* L) {
lua_setfield(L, -2, "name");
switch (ent.type) {
case UV_DIRENT_UNKNOWN: type = NULL; break;
case UV_DIRENT_FILE: type = "FILE"; break;
case UV_DIRENT_DIR: type = "DIR"; break;
case UV_DIRENT_LINK: type = "LINK"; break;
case UV_DIRENT_FIFO: type = "FIFO"; break;
case UV_DIRENT_SOCKET: type = "SOCKET"; break;
case UV_DIRENT_CHAR: type = "CHAR"; break;
case UV_DIRENT_BLOCK: type = "BLOCK"; break;
case UV_DIRENT_FILE: type = "file"; break;
case UV_DIRENT_DIR: type = "directory"; break;
case UV_DIRENT_LINK: type = "link"; break;
case UV_DIRENT_FIFO: type = "fifo"; break;
case UV_DIRENT_SOCKET: type = "socket"; break;
case UV_DIRENT_CHAR: type = "char"; break;
case UV_DIRENT_BLOCK: type = "block"; break;
}
if (type) {
lua_pushstring(L, type);
Expand Down
4 changes: 4 additions & 0 deletions src/luv.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "dns.c"
// #include "thread.c"
#include "misc.c"
#include "constants.c"

static const luaL_Reg luv_functions[] = {
// loop.c
Expand Down Expand Up @@ -271,5 +272,8 @@ LUALIB_API int luaopen_luv (lua_State *L) {
luv_req_init(L);
luv_handle_init(L);
luaL_newlib(L, luv_functions);
luv_constants(L);
lua_setfield(L, -2, "constants");

return 1;
}
14 changes: 9 additions & 5 deletions src/luv.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,14 @@ static void luv_connect_cb(uv_connect_t* req, int status);
// From fs.c
static void luv_push_stats_table(lua_State* L, const uv_stat_t* s);

// From dns.c
static int luv_string_to_socktype(const char* string);
static const char* luv_socktype_to_string(int socktype);
static int luv_string_to_protocol(const char* protocol);
static const char* luv_protocol_to_string(int family);
// from constants.c
static const int luv_af_string_to_num(const char* string);
static const char* luv_af_num_to_string(const int num);
static const int luv_ai_string_to_num(const char* string);
static const char* luv_ai_num_to_string(const int num);
static const int luv_sock_string_to_num(const char* string);
static const char* luv_sock_num_to_string(const int num);
static const int luv_sig_string_to_num(const char* string);
static const char* luv_sig_num_to_string(const int num);

#endif
2 changes: 1 addition & 1 deletion src/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ static int luv_interface_addresses(lua_State* L) {
}
lua_pushstring(L, ip);
lua_setfield(L, -2, "ip");
lua_pushstring(L, luv_protocol_to_string(interfaces[i].address.address4.sin_family));
lua_pushstring(L, luv_af_num_to_string(interfaces[i].address.address4.sin_family));
lua_setfield(L, -2, "family");
lua_rawseti(L, -2, lua_rawlen (L, -2) + 1);
lua_pop(L, 1);
Expand Down
Loading

0 comments on commit f373d2e

Please sign in to comment.