From 1a3a681e8d1a5ef0d29edbd6726d712c5a9d35cd Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Fri, 2 Nov 2018 05:51:22 +0000 Subject: [PATCH] removed qse_syserr_to_errnum(). changed syserrstrb() and syserrstru() to return the converted error number from the system error code --- moo/lib/comp.c | 5 +- moo/lib/err.c | 159 ++---------------------------------------- moo/lib/main.c | 178 +++++++++++++++++++++++++++++++++++++++++++----- moo/lib/moo.h | 17 +++-- moo/mod/sck.c | 24 +++---- moo/mod/stdio.c | 6 +- 6 files changed, 194 insertions(+), 195 deletions(-) diff --git a/moo/lib/comp.c b/moo/lib/comp.c index 9a9dc39..511dfac 100644 --- a/moo/lib/comp.c +++ b/moo/lib/comp.c @@ -7740,9 +7740,8 @@ static int process_class_module_import (moo_t* moo) return -1; } - if (TOKEN_NAME_LEN(moo) <= 0 || - TOKEN_NAME_LEN(moo) > MOO_MOD_NAME_LEN_MAX || - moo_find_oochar(TOKEN_NAME_PTR(moo), TOKEN_NAME_LEN(moo), '-') ) + if (TOKEN_NAME_LEN(moo) <= 0 || TOKEN_NAME_LEN(moo) > MOO_MOD_NAME_LEN_MAX || + moo_find_oochar(TOKEN_NAME_PTR(moo), TOKEN_NAME_LEN(moo), '-') ) { /* check for a bad module name. * also disallow a dash in the name - i like converting diff --git a/moo/lib/err.c b/moo/lib/err.c index 267e2fa..7bcc014 100644 --- a/moo/lib/err.c +++ b/moo/lib/err.c @@ -226,153 +226,6 @@ static const moo_ooch_t* synerr_to_errstr (moo_synerrnum_t errnum) # include #endif -moo_errnum_t moo_syserr_to_errnum (int e) -{ -#if 0 - /* -------------------------------------- */ - /* currently 'e' is expected to be 'errno'. it doesn't support GetLastError() on win32 or APIRET on os2 yet */ - /* -------------------------------------- */ -#if defined(__OS2__) - /* APIRET e */ - switch (e) - { - case ERROR_NOT_ENOUGH_MEMORY: return MOO_ESYSMEM; - - case ERROR_INVALID_PARAMETER: - case ERROR_INVALID_HANDLE: - case ERROR_INVALID_NAME: return MOO_EINVAL; - - case ERROR_ACCESS_DENIED: - case ERROR_SHARING_VIOLATION: return MOO_EACCES; - - case ERROR_FILE_NOT_FOUND: - case ERROR_PATH_NOT_FOUND: return MOO_ENOENT; - - case ERROR_ALREADY_EXISTS: return MOO_EEXIST; - - /*TODO: add more mappings */ - default: return MOO_ESYSERR; - } -#elif defined(macintosh) - switch (e) - { - case notEnoughMemoryErr: return MOO_ESYSMEM; - case paramErr: return MOO_EINVAL; - - case qErr: /* queue element not found during deletion */ - case fnfErr: /* file not found */ - case dirNFErr: /* direcotry not found */ - case resNotFound: /* resource not found */ - case resFNotFound: /* resource file not found */ - case nbpNotFound: /* name not found on remove */ - return MOO_ENOENT; - - /*TODO: add more mappings */ - default: return MOO_ESYSERR; - } - -#else - - switch (e) - { - case ENOMEM: return MOO_ESYSMEM; - case EINVAL: return MOO_EINVAL; - - #if defined(EBUSY) - case EBUSY: return MOO_EBUSY; - #endif - case EACCES: return MOO_EACCES; - #if defined(EPERM) - case EPERM: return MOO_EPERM; - #endif - #if defined(ENOTDIR) - case ENOTDIR: return MOO_ENOTDIR; - #endif - case ENOENT: return MOO_ENOENT; - #if defined(EEXIST) - case EEXIST: return MOO_EEXIST; - #endif - #if defined(EINTR) - case EINTR: return MOO_EINTR; - #endif - - #if defined(EPIPE) - case EPIPE: return MOO_EPIPE; - #endif - - #if defined(EAGAIN) && defined(EWOULDBLOCK) && (EAGAIN != EWOULDBLOCK) - case EAGAIN: - case EWOULDBLOCK: return MOO_EAGAIN; - #elif defined(EAGAIN) - case EAGAIN: return MOO_EAGAIN; - #elif defined(EWOULDBLOCK) - case EWOULDBLOCK: return MOO_EAGAIN; - #endif - - #if defined(EBADF) - case EBADF: return MOO_EBADHND; - #endif - - #if defined(EIO) - case EIO: return MOO_EIOERR; - #endif - - default: return MOO_ESYSERR; - } -#endif - -#else - /* ------------------------------------------------------------------- */ - switch (e) - { - case ENOMEM: return MOO_ESYSMEM; - case EINVAL: return MOO_EINVAL; - - #if defined(EBUSY) - case EBUSY: return MOO_EBUSY; - #endif - case EACCES: return MOO_EACCES; - #if defined(EPERM) - case EPERM: return MOO_EPERM; - #endif - #if defined(ENOTDIR) - case ENOTDIR: return MOO_ENOTDIR; - #endif - case ENOENT: return MOO_ENOENT; - #if defined(EEXIST) - case EEXIST: return MOO_EEXIST; - #endif - #if defined(EINTR) - case EINTR: return MOO_EINTR; - #endif - - #if defined(EPIPE) - case EPIPE: return MOO_EPIPE; - #endif - - #if defined(EAGAIN) && defined(EWOULDBLOCK) && (EAGAIN != EWOULDBLOCK) - case EAGAIN: - case EWOULDBLOCK: return MOO_EAGAIN; - #elif defined(EAGAIN) - case EAGAIN: return MOO_EAGAIN; - #elif defined(EWOULDBLOCK) - case EWOULDBLOCK: return MOO_EAGAIN; - #endif - - #if defined(EBADF) - case EBADF: return MOO_EBADHND; - #endif - - #if defined(EIO) - case EIO: return MOO_EIOERR; - #endif - - default: return MOO_ESYSERR; - } - /* ------------------------------------------------------------------- */ -#endif -} - /* -------------------------------------------------------------------------- * ERROR NUMBER/MESSAGE HANDLING * -------------------------------------------------------------------------- */ @@ -401,20 +254,22 @@ void moo_seterrnum (moo_t* moo, moo_errnum_t errnum) moo->errmsg.len = 0; } -void moo_seterrwithsyserr (moo_t* moo, int syserr) +void moo_seterrwithsyserr (moo_t* moo, int syserr_type, int syserr_code) { + moo_errnum_t errnum; + if (moo->shuterr) return; if (moo->vmprim.syserrstrb) { - moo->vmprim.syserrstrb (moo, syserr, moo->errmsg.tmpbuf.bch, MOO_COUNTOF(moo->errmsg.tmpbuf.bch)); - moo_seterrbfmt (moo, moo_syserr_to_errnum(syserr), "%hs", moo->errmsg.tmpbuf.bch); + errnum = moo->vmprim.syserrstrb (moo, syserr_type, syserr_code, moo->errmsg.tmpbuf.bch, MOO_COUNTOF(moo->errmsg.tmpbuf.bch)); + moo_seterrbfmt (moo, errnum, "%hs", moo->errmsg.tmpbuf.bch); } else { MOO_ASSERT (moo, moo->vmprim.syserrstru != MOO_NULL); - moo->vmprim.syserrstru (moo, syserr, moo->errmsg.tmpbuf.uch, MOO_COUNTOF(moo->errmsg.tmpbuf.uch)); - moo_seterrbfmt (moo, moo_syserr_to_errnum(syserr), "%ls", moo->errmsg.tmpbuf.uch); + errnum = moo->vmprim.syserrstru (moo, syserr_type, syserr_code, moo->errmsg.tmpbuf.uch, MOO_COUNTOF(moo->errmsg.tmpbuf.uch)); + moo_seterrbfmt (moo, errnum, "%ls", moo->errmsg.tmpbuf.uch); } } diff --git a/moo/lib/main.c b/moo/lib/main.c index db0736f..c40af2d 100644 --- a/moo/lib/main.c +++ b/moo/lib/main.c @@ -827,16 +827,162 @@ static void log_write (moo_t* moo, moo_bitmask_t mask, const moo_ooch_t* msg, mo flush_log (moo, logfd); } -static void syserrstrb (moo_t* moo, int syserr, moo_bch_t* buf, moo_oow_t len) +static moo_errnum_t errno_to_errnum (int errcode) { -#if defined(HAVE_STRERROR_R) - strerror_r (syserr, buf, len); -#else - /* this is not thread safe */ - moo_copy_bcstr (buf, len, strerror(syserr)); -#endif + switch (errcode) + { + case ENOMEM: return MOO_ESYSMEM; + case EINVAL: return MOO_EINVAL; + + #if defined(EBUSY) + case EBUSY: return MOO_EBUSY; + #endif + case EACCES: return MOO_EACCES; + #if defined(EPERM) + case EPERM: return MOO_EPERM; + #endif + #if defined(ENOTDIR) + case ENOTDIR: return MOO_ENOTDIR; + #endif + case ENOENT: return MOO_ENOENT; + #if defined(EEXIST) + case EEXIST: return MOO_EEXIST; + #endif + #if defined(EINTR) + case EINTR: return MOO_EINTR; + #endif + + #if defined(EPIPE) + case EPIPE: return MOO_EPIPE; + #endif + + #if defined(EAGAIN) && defined(EWOULDBLOCK) && (EAGAIN != EWOULDBLOCK) + case EAGAIN: + case EWOULDBLOCK: return MOO_EAGAIN; + #elif defined(EAGAIN) + case EAGAIN: return MOO_EAGAIN; + #elif defined(EWOULDBLOCK) + case EWOULDBLOCK: return MOO_EAGAIN; + #endif + + #if defined(EBADF) + case EBADF: return MOO_EBADHND; + #endif + + #if defined(EIO) + case EIO: return MOO_EIOERR; + #endif + + default: return MOO_ESYSERR; + } } +#if defined(_WIN32) +static moo_errnum_t winerr_to_errnum (DWORD errcode) +{ + return MOO_ESYSERR; +} +#endif + +#if defined(__OS2__) +static moo_errnum_t os2err_to_errnum (APIRET errcode) +{ + /* APIRET e */ + switch (errcode) + { + case ERROR_NOT_ENOUGH_MEMORY: + return MOO_ESYSMEM; + + case ERROR_INVALID_PARAMETER: + case ERROR_INVALID_HANDLE: + case ERROR_INVALID_NAME: + return MOO_EINVAL; + + case ERROR_ACCESS_DENIED: + case ERROR_SHARING_VIOLATION: + return MOO_EACCES; + + case ERROR_FILE_NOT_FOUND: + case ERROR_PATH_NOT_FOUND: + return MOO_ENOENT; + + case ERROR_ALREADY_EXISTS: + return MOO_EEXIST; + + /*TODO: add more mappings */ + default: + return MOO_ESYSERR; + } +} +#endif + +#if defined(macintosh) +static moo_errnum_t macerr_to_errnum (int errcode) +{ + switch (e) + { + case notEnoughMemoryErr: + return MOO_ESYSMEM; + case paramErr: + return MOO_EINVAL; + + case qErr: /* queue element not found during deletion */ + case fnfErr: /* file not found */ + case dirNFErr: /* direcotry not found */ + case resNotFound: /* resource not found */ + case resFNotFound: /* resource file not found */ + case nbpNotFound: /* name not found on remove */ + return MOO_ENOENT; + + /*TODO: add more mappings */ + default: + return MOO_ESYSERR; + } +} +#endif + +static moo_errnum_t syserrstrb (moo_t* moo, int syserr_type, int syserr_code, moo_bch_t* buf, moo_oow_t len) +{ + switch (syserr_type) + { + case 1: + #if defined(_WIN32) + DWORD rc; + rc = FormatMessageA ( + FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, syserr_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + buf, len, MOO_NULL + ); + while (rc > 0 && buf[rc - 1] == '\r' || buf[rc - 1] == '\n') buf[--rc] = '\0'; + return winerr_to_errnum(syserr_code); + #elif defined(__OS2__) + /* TODO: convert code to string */ + moo_copy_bcstr (buf, len, "system error"); + return os2err_to_errnum(syserr_code); + #elif defined(macintosh) + /* TODO: convert code to string */ + moo_copy_bcstr (buf, len, "system error"); + return os2err_to_errnum(syserr_code); + #else + /* in other systems, errno is still the native system error code. + * fall thru */ + #endif + + case 0: + #if defined(HAVE_STRERROR_R) + strerror_r (syserr_code, buf, len); + #else + /* this is not thread safe */ + moo_copy_bcstr (buf, len, strerror(syserr_code)); + #endif + return errno_to_errnum(syserr_code); + } + + moo_copy_bcstr (buf, len, "system error"); + return MOO_ESYSERR; +} + + /* ========================================================================= */ #if defined(USE_WIN_DLL) @@ -1177,7 +1323,7 @@ static int _add_poll_fd (moo_t* moo, int fd, int event_mask) ev.revents = 0; if (write(xtn->ep, &ev, MOO_SIZEOF(ev)) != MOO_SIZEOF(ev)) { - moo_seterrwithsyserr (moo, errno); + moo_seterrwithsyserr (moo, 0, errno); MOO_DEBUG2 (moo, "Cannot add file descriptor %d to devpoll - %hs\n", fd, strerror(errno)); return -1; } @@ -1201,7 +1347,7 @@ static int _add_poll_fd (moo_t* moo, int fd, int event_mask) ev.data.fd = fd; if (epoll_ctl(xtn->ep, EPOLL_CTL_ADD, fd, &ev) == -1) { - moo_seterrwithsyserr (moo, errno); + moo_seterrwithsyserr (moo, 0, errno); MOO_DEBUG2 (moo, "Cannot add file descriptor %d to epoll - %hs\n", fd, strerror(errno)); return -1; } @@ -1281,7 +1427,7 @@ static int _del_poll_fd (moo_t* moo, int fd) ev.revents = 0; if (write (xtn->ep, &ev, MOO_SIZEOF(ev)) != MOO_SIZEOF(ev)) { - moo_seterrwithsyserr (moo, errno); + moo_seterrwithsyserr (moo, 0, errno); MOO_DEBUG2 (moo, "Cannot remove file descriptor %d from devpoll - %hs\n", fd, strerror(errno)); return -1; } @@ -1296,7 +1442,7 @@ static int _del_poll_fd (moo_t* moo, int fd) memset (&ev, 0, MOO_SIZEOF(ev)); if (epoll_ctl (xtn->ep, EPOLL_CTL_DEL, fd, &ev) == -1) { - moo_seterrwithsyserr (moo, errno); + moo_seterrwithsyserr (moo, 0, errno); MOO_DEBUG2 (moo, "Cannot remove file descriptor %d from epoll - %hs\n", fd, strerror(errno)); return -1; } @@ -1384,7 +1530,7 @@ static int _mod_poll_fd (moo_t* moo, int fd, int event_mask) ev.data.fd = fd; if (epoll_ctl(xtn->ep, EPOLL_CTL_MOD, fd, &ev) == -1) { - moo_seterrwithsyserr (moo, errno); + moo_seterrwithsyserr (moo, 0, errno); MOO_DEBUG2 (moo, "Cannot modify file descriptor %d in epoll - %hs\n", fd, strerror(errno)); return -1; } @@ -1457,7 +1603,7 @@ static int vm_startup (moo_t* moo) xtn->ep = open("/dev/poll", O_RDWR); if (xtn->ep == -1) { - moo_seterrwithsyserr (moo, errno); + moo_seterrwithsyserr (moo, 0, errno); MOO_DEBUG1 (moo, "Cannot create devpoll - %hs\n", strerror(errno)); goto oops; } @@ -1473,7 +1619,7 @@ static int vm_startup (moo_t* moo) #endif if (xtn->ep == -1) { - moo_seterrwithsyserr (moo, errno); + moo_seterrwithsyserr (moo, 0, errno); MOO_DEBUG1 (moo, "Cannot create epoll - %hs\n", strerror(errno)); goto oops; } @@ -1499,7 +1645,7 @@ static int vm_startup (moo_t* moo) #if defined(USE_THREAD) if (pipe(xtn->p) == -1) { - moo_seterrwithsyserr (moo, errno); + moo_seterrwithsyserr (moo, 0, errno); MOO_DEBUG1 (moo, "Cannot create pipes - %hs\n", strerror(errno)); goto oops; } @@ -2093,7 +2239,7 @@ static void vm_muxwait (moo_t* moo, const moo_ntime_t* dur, moo_vmprim_muxwait_c if (n <= -1) { - moo_seterrwithsyserr (moo, errno); + moo_seterrwithsyserr (moo, 0, errno); MOO_DEBUG2 (moo, "Warning: multiplexer wait failure - %d, %s\n", errno, moo_geterrmsg(moo)); } else diff --git a/moo/lib/moo.h b/moo/lib/moo.h index 71003b4..7fa6bfc 100644 --- a/moo/lib/moo.h +++ b/moo/lib/moo.h @@ -1069,16 +1069,18 @@ typedef void (*moo_log_write_t) ( moo_oow_t len ); -typedef void (*moo_syserrstrb_t) ( +typedef moo_errnum_t (*moo_syserrstrb_t) ( moo_t* moo, - int syserr, + int syserr_type, + int syserr_code, moo_bch_t* buf, moo_oow_t len ); -typedef void (*moo_syserrstru_t) ( +typedef moo_errnum_t (*moo_syserrstru_t) ( moo_t* moo, - int syserr, + int syserr_type, + int syserr_code, moo_uch_t* buf, moo_oow_t len ); @@ -1794,7 +1796,8 @@ MOO_EXPORT void moo_seterrnum ( MOO_EXPORT void moo_seterrwithsyserr ( moo_t* moo, - int syserr + int syserr_type, + int syserr_code ); MOO_EXPORT void moo_seterrbfmt ( @@ -2296,10 +2299,6 @@ MOO_EXPORT void moo_assertfailed ( moo_oow_t line ); -MOO_EXPORT moo_errnum_t moo_syserr_to_errnum ( - int syserr -); - MOO_EXPORT const moo_ooch_t* moo_errnum_to_errstr ( moo_errnum_t errnum ); diff --git a/moo/mod/sck.c b/moo/mod/sck.c index 28a3fc7..7d5ba17 100644 --- a/moo/mod/sck.c +++ b/moo/mod/sck.c @@ -89,7 +89,7 @@ create_socket: goto create_socket; } #endif - moo_seterrwithsyserr (moo, errno); + moo_seterrwithsyserr (moo, 0, errno); goto oops; } @@ -110,7 +110,7 @@ create_socket: if (fl == -1) { fcntl_failure: - moo_seterrwithsyserr (moo, errno); + moo_seterrwithsyserr (moo, 0, errno); goto oops; } @@ -150,7 +150,7 @@ static moo_pfrc_t pf_close_socket (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) moo_releaseiohandle (moo, MOO_OOP_TO_SMOOI(sck->handle)); if (close(MOO_OOP_TO_SMOOI(sck->handle)) == -1) { - moo_seterrwithsyserr (moo, errno); + moo_seterrwithsyserr (moo, 0, errno); return MOO_PF_FAILURE; } else @@ -191,7 +191,7 @@ static moo_pfrc_t pf_bind_socket (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable, MOO_SIZEOF(int)) == -1 || bind(fd, (struct sockaddr*)MOO_OBJ_GET_BYTE_SLOT(arg), moo_sck_addr_len((sck_addr_t*)MOO_OBJ_GET_BYTE_SLOT(arg))) == -1) { - moo_seterrwithsyserr (moo, errno); + moo_seterrwithsyserr (moo, 0, errno); return MOO_PF_FAILURE; } @@ -231,7 +231,7 @@ static moo_pfrc_t pf_accept_socket (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) if (errno != EWOULDBLOCK && errno != EAGAIN) { - moo_seterrwithsyserr (moo, errno); + moo_seterrwithsyserr (moo, 0, errno); return MOO_PF_FAILURE; } @@ -251,7 +251,7 @@ normal_accept: { if (errno != EWOULDBLOCK && errno != EAGAIN) { - moo_seterrwithsyserr (moo, errno); + moo_seterrwithsyserr (moo, 0, errno); return MOO_PF_FAILURE; } @@ -264,7 +264,7 @@ normal_accept: if (fl == -1) { fcntl_failure: - moo_seterrwithsyserr (moo, errno); + moo_seterrwithsyserr (moo, 0, errno); close (newfd); return MOO_PF_FAILURE; } @@ -339,7 +339,7 @@ static moo_pfrc_t pf_listen_socket (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) n = listen(fd, MOO_OOP_TO_SMOOI(arg)); if (n == -1) { - moo_seterrwithsyserr (moo, errno); + moo_seterrwithsyserr (moo, 0, errno); return MOO_PF_FAILURE; } @@ -381,7 +381,7 @@ static moo_pfrc_t pf_connect_socket (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs } else { - moo_seterrwithsyserr (moo, errno); + moo_seterrwithsyserr (moo, 0, errno); return MOO_PF_FAILURE; } } @@ -413,7 +413,7 @@ static moo_pfrc_t pf_get_socket_error (moo_t* moo, moo_mod_t* mod, moo_ooi_t nar len = MOO_SIZEOF(ret); if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*)&ret, &len) == -1) { - moo_seterrwithsyserr (moo, errno); + moo_seterrwithsyserr (moo, 0, errno); return MOO_PF_FAILURE; } @@ -484,7 +484,7 @@ static moo_pfrc_t pf_read_socket (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) n = recv(fd, &MOO_OBJ_GET_BYTE_SLOT(buf)[offset], length, 0); if (n <= -1 && errno != EWOULDBLOCK && errno != EAGAIN) { - moo_seterrwithsyserr (moo, errno); + moo_seterrwithsyserr (moo, 0, errno); return MOO_PF_FAILURE; } @@ -557,7 +557,7 @@ static moo_pfrc_t pf_write_socket (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) n = send(fd, &MOO_OBJ_GET_BYTE_SLOT(buf)[offset], length, 0); if (n <= -1 && errno != EWOULDBLOCK && errno != EAGAIN) { - moo_seterrwithsyserr (moo, errno); + moo_seterrwithsyserr (moo, 0, errno); return MOO_PF_FAILURE; } diff --git a/moo/mod/stdio.c b/moo/mod/stdio.c index 6ce108f..b96d973 100644 --- a/moo/mod/stdio.c +++ b/moo/mod/stdio.c @@ -75,7 +75,7 @@ static moo_pfrc_t pf_open (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) #endif if (!stdio->fp) { - moo_seterrwithsyserr (moo, errno); + moo_seterrwithsyserr (moo, 0, errno); goto softfail; } @@ -157,7 +157,7 @@ static moo_pfrc_t __pf_puts (moo_t* moo, moo_ooi_t nargs, moo_oow_t limit) if (fwrite (bcs, 1, bcslen, stdio->fp) < bcslen) { - moo_seterrwithsyserr (moo, errno); + moo_seterrwithsyserr (moo, 0, errno); goto softfail; } @@ -169,7 +169,7 @@ static moo_pfrc_t __pf_puts (moo_t* moo, moo_ooi_t nargs, moo_oow_t limit) puts_string: if (fwrite (x->slot, 1, MOO_OBJ_GET_SIZE(x), stdio->fp) < MOO_OBJ_GET_SIZE(x)) { - moo_seterrwithsyserr (moo, errno); + moo_seterrwithsyserr (moo, 0, errno); goto softfail; } #endif