made code a bit win32 friendly

This commit is contained in:
hyung-hwan 2018-03-31 07:10:43 +00:00
parent 8df5087b9d
commit 16b96e49bc
3 changed files with 95 additions and 28 deletions

View File

@ -193,7 +193,34 @@ static const hcl_bch_t* synerr_to_errstr (hcl_synerrnum_t errnum)
hcl_errnum_t hcl_syserr_to_errnum (int e) hcl_errnum_t hcl_syserr_to_errnum (int e)
{ {
#if defined(__OS2__) #if defined(_WIN32)
switch (e)
{
case ERROR_NOT_ENOUGH_MEMORY:
case ERROR_OUTOFMEMORY:
return HCL_ESYSMEM;
case ERROR_INVALID_PARAMETER:
case ERROR_INVALID_HANDLE:
case ERROR_INVALID_NAME:
return HCL_EINVAL;
case ERROR_ACCESS_DENIED:
case ERROR_SHARING_VIOLATION:
return HCL_EACCES;
case ERROR_FILE_NOT_FOUND:
case ERROR_PATH_NOT_FOUND:
return HCL_ENOENT;
case ERROR_ALREADY_EXISTS:
case ERROR_FILE_EXISTS:
return HCL_EEXIST;
case ERROR_BROKEN_PIPE:
return HCL_EPIPE;
/*TODO: add more mappings */
default:
return HCL_ESYSERR;
}
#elif defined(__OS2__)
/* APIRET e */ /* APIRET e */
switch (e) switch (e)
{ {

View File

@ -34,10 +34,13 @@
#include <errno.h> #include <errno.h>
#include <locale.h> #include <locale.h>
#if defined(_WIN32) #if defined(_WIN32)
# include <windows.h> # include <windows.h>
# include <tchar.h> # include <tchar.h>
# include <io.h>
# include <fcntl.h>
# include <time.h>
# include <signal.h>
# if defined(HCL_HAVE_CFG_H) && defined(HCL_ENABLE_LIBLTDL) # if defined(HCL_HAVE_CFG_H) && defined(HCL_ENABLE_LIBLTDL)
# include <ltdl.h> # include <ltdl.h>
# define USE_LTDL # define USE_LTDL
@ -136,6 +139,9 @@ struct bb_t
typedef struct xtn_t xtn_t; typedef struct xtn_t xtn_t;
struct xtn_t struct xtn_t
{ {
#if defined(_WIN32)
HANDLE waitable_timer;
#endif
const char* read_path; /* main source file */ const char* read_path; /* main source file */
const char* print_path; const char* print_path;
@ -264,7 +270,9 @@ static HCL_INLINE int open_input (hcl_t* hcl, hcl_ioinarg_t* arg)
if (!arg->includer) if (!arg->includer)
{ {
#if defined(HAVE_ISATTY)
xtn->reader_istty = isatty(fileno(bb->fp)); xtn->reader_istty = isatty(fileno(bb->fp));
#endif
} }
arg->handle = bb; arg->handle = bb;
@ -654,13 +662,25 @@ static void log_write (hcl_t* hcl, unsigned int mask, const hcl_ooch_t* msg, hcl
struct tm tm, *tmp; struct tm tm, *tmp;
now = time(NULL); now = time(NULL);
#if defined(__DOS__) #if defined(_WIN32)
tmp = localtime(&now);
tslen = strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S %z ", tmp);
if (tslen == 0)
{
tslen = strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S ", tmp);
if (tslen == 0)
{
strcpy (ts, "0000-00-00 00:00:00 +0000 ");
tslen = 26;
}
}
#elif defined(__DOS__)
tmp = localtime(&now); tmp = localtime(&now);
tslen = strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S ", tmp); /* no timezone info */ tslen = strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S ", tmp); /* no timezone info */
if (tslen == 0) if (tslen == 0)
{ {
strcpy (ts, "0000-00-00 00:00:00 "); strcpy (ts, "0000-00-00 00:00:00 ");
tslen = 19; tslen = 20;
} }
#else #else
tmp = localtime_r (&now, &tm); tmp = localtime_r (&now, &tm);
@ -672,7 +692,7 @@ static void log_write (hcl_t* hcl, unsigned int mask, const hcl_ooch_t* msg, hcl
if (tslen == 0) if (tslen == 0)
{ {
strcpy (ts, "0000-00-00 00:00:00 +0000 "); strcpy (ts, "0000-00-00 00:00:00 +0000 ");
tslen = 25; tslen = 26;
} }
#endif #endif
write_log (hcl, logfd, ts, tslen); write_log (hcl, logfd, ts, tslen);
@ -876,7 +896,7 @@ static void* dl_open (hcl_t* hcl, const hcl_ooch_t* name, int flags)
/* TODO: support various platforms */ /* TODO: support various platforms */
/* TODO: implemenent this */ /* TODO: implemenent this */
HCL_DEBUG1 (hcl, "Dynamic loading not implemented - cannot open %js\n", name); HCL_DEBUG1 (hcl, "Dynamic loading not implemented - cannot open %js\n", name);
hcl_seterrnum (hcl, HCL_ENOIMPL, "dynamic loading not implemented - cannot open %js", name); hcl_seterrbfmt (hcl, HCL_ENOIMPL, "dynamic loading not implemented - cannot open %js", name);
return HCL_NULL; return HCL_NULL;
#endif #endif
} }
@ -1033,8 +1053,8 @@ static void vm_sleep (hcl_t* hcl, const hcl_ntime_t* dur)
{ {
LARGE_INTEGER li; LARGE_INTEGER li;
li.QuadPart = -HCL_SECNSEC_TO_NSEC(dur->sec, dur->nsec); li.QuadPart = -HCL_SECNSEC_TO_NSEC(dur->sec, dur->nsec);
if(SetWaitableTimer(timer, &li, 0, HCL_NULL, HCL_NULL, FALSE) == FALSE) goto normal_sleep; if(SetWaitableTimer(xtn->waitable_timer, &li, 0, HCL_NULL, HCL_NULL, FALSE) == FALSE) goto normal_sleep;
WaitForSingleObject(timer, INFINITE); WaitForSingleObject(xtn->waitable_timer, INFINITE);
} }
else else
{ {
@ -1104,7 +1124,8 @@ static int vm_startup (hcl_t* hcl)
#if defined(_WIN32) #if defined(_WIN32)
xtn_t* xtn = (xtn_t*)hcl_getxtn(hcl); xtn_t* xtn = (xtn_t*)hcl_getxtn(hcl);
xtn->waitable_timer = CreateWaitableTimer(HCL_NULL, TRUE, HCL_NULL); xtn->waitable_timer = CreateWaitableTimer(HCL_NULL, TRUE, HCL_NULL);
xtn->vm_running = 1;
return 0;
#else #else
xtn_t* xtn = (xtn_t*)hcl_getxtn(hcl); xtn_t* xtn = (xtn_t*)hcl_getxtn(hcl);
@ -1216,7 +1237,7 @@ oops:
static void vm_cleanup (hcl_t* hcl) static void vm_cleanup (hcl_t* hcl)
{ {
#if defined(_WIN32) #if defined(_WIN32)
xtn_t* xtn = (xatn_t*)hcl_getxtn(hcl); xtn_t* xtn = (xtn_t*)hcl_getxtn(hcl);
if (xtn->waitable_timer) if (xtn->waitable_timer)
{ {
CloseHandle (xtn->waitable_timer); CloseHandle (xtn->waitable_timer);
@ -1376,8 +1397,11 @@ static int handle_logopt (hcl_t* hcl, const hcl_bch_t* str)
logmask = HCL_LOG_ALL_LEVELS | HCL_LOG_ALL_TYPES; logmask = HCL_LOG_ALL_LEVELS | HCL_LOG_ALL_TYPES;
} }
#if defined(_WIN32)
xtn->logfd = _open(xstr, _O_CREAT | _O_WRONLY | _O_APPEND | _O_BINARY , 0644);
#else
xtn->logfd = open(xstr, O_CREAT | O_WRONLY | O_APPEND , 0644); xtn->logfd = open(xstr, O_CREAT | O_WRONLY | O_APPEND , 0644);
#endif
if (xtn->logfd == -1) if (xtn->logfd == -1)
{ {
fprintf (stderr, "ERROR: cannot open a log file %s\n", xstr); fprintf (stderr, "ERROR: cannot open a log file %s\n", xstr);
@ -1541,19 +1565,22 @@ static void cancel_tick (void)
/* ========================================================================= */ /* ========================================================================= */
#if defined(__MSDOS__) && defined(_INTELC32_) #if defined(_WIN32) || defined(__MSDOS__) || defined(__OS2__) || defined(macintosh)
typedef void(*signal_handler_t)(int); typedef void(*signal_handler_t)(int);
#elif defined(macintosh) #elif defined(macintosh)
typedef void(*signal_handler_t)(int); typedef void(*signal_handler_t)(int); /* TODO: */
#else #else
typedef void(*signal_handler_t)(int, siginfo_t*, void*); typedef void(*signal_handler_t)(int, siginfo_t*, void*);
#endif #endif
#if defined(__MSDOS__) && defined(_INTELC32_) #if defined(_WIN32) || defined(__MSDOS__) || defined(__OS2__)
/* TODO: implement this */ static void handle_sigint (int sig)
{
if (g_hcl) hcl_abort (g_hcl);
}
#elif defined(macintosh) #elif defined(macintosh)
/* TODO: implement this */ /* TODO */
#else #else
static void handle_sigint (int sig, siginfo_t* siginfo, void* ctx) static void handle_sigint (int sig, siginfo_t* siginfo, void* ctx)
{ {
@ -1563,8 +1590,8 @@ static void handle_sigint (int sig, siginfo_t* siginfo, void* ctx)
static void set_signal (int sig, signal_handler_t handler) static void set_signal (int sig, signal_handler_t handler)
{ {
#if defined(__MSDOS__) && defined(_INTELC32_) #if defined(_WIN32) || defined(__MSDOS__) || defined(__OS2__)
/* TODO: implement this */ signal (sig, handler);
#elif defined(macintosh) #elif defined(macintosh)
/* TODO: implement this */ /* TODO: implement this */
#else #else
@ -1582,8 +1609,8 @@ static void set_signal (int sig, signal_handler_t handler)
static void set_signal_to_default (int sig) static void set_signal_to_default (int sig)
{ {
#if defined(__MSDOS__) && defined(_INTELC32_) #if defined(_WIN32) || defined(__MSDOS__) || defined(__OS2__)
/* TODO: implement this */ signal (sig, SIG_DFL);
#elif defined(macintosh) #elif defined(macintosh)
/* TODO: implement this */ /* TODO: implement this */
#else #else

View File

@ -1111,7 +1111,20 @@ hcl_uint128_t hcl_hton128 (hcl_uint128_t x)
/* --------------------------------------------------------------- */ /* --------------------------------------------------------------- */
#if defined(_WIN32)
# include <winsock2.h>
# include <ws2tcpip.h> /* sockaddr_in6 */
# include <windows.h>
#elif defined(__OS2__)
# if defined(TCPV40HDRS)
# define BSD_SELECT
# endif
# include <types.h>
# include <sys/socket.h>
# include <netinet/in.h> # include <netinet/in.h>
#else
# include <netinet/in.h>
#endif
union sockaddr_t union sockaddr_t
{ {