/*
 * $Id$
 *
    Copyright (c) 2014-2017 Chung, Hyung-Hwan. All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:
    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.

    THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "moo-prv.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>


#if defined(_WIN32)
#	include <windows.h>
#	include <tchar.h>
#	if defined(MOO_HAVE_CFG_H) && defined(MOO_ENABLE_LIBLTDL)
#		include <ltdl.h>
#		define USE_LTDL
#	endif
#elif defined(__OS2__)
#	define INCL_DOSMODULEMGR
#	define INCL_DOSPROCESS
#	define INCL_DOSMISC
#	define INCL_DOSDATETIME
#	define INCL_DOSERRORS
#	include <os2.h>
#	include <time.h>
#elif defined(__DOS__)
#	include <dos.h>
#	include <time.h>
#	include <io.h>
#elif defined(macintosh)
#	include <Types.h>
#	include <OSUtils.h>
#	include <Timer.h>
#else
#	include <unistd.h>
#	if defined(MOO_ENABLE_LIBLTDL)
#		include <ltdl.h>
#		define USE_LTDL
#	else
#		include <dlfcn.h>
#		error NOT IMPLEMENTED
#	endif

#	if defined(HAVE_TIME_H)
#		include <time.h>
#	endif
#	if defined(HAVE_SYS_TIME_H)
#		include <sys/time.h>
#	endif
#	if defined(HAVE_SIGNAL_H)
#		include <signal.h>
#	endif

#	include <sys/epoll.h>
#endif

#if !defined(MOO_DEFAULT_PFMODPREFIX)
#	if defined(_WIN32)
#		define MOO_DEFAULT_PFMODPREFIX "moo-"
#	elif defined(__OS2__)
#		define MOO_DEFAULT_PFMODPREFIX "moo"
#	elif defined(__DOS__)
#		define MOO_DEFAULT_PFMODPREFIX "moo"
#	else
#		define MOO_DEFAULT_PFMODPREFIX "libmoo-"
#	endif
#endif

#if !defined(MOO_DEFAULT_PFMODPOSTFIX)
#	if defined(_WIN32)
#		define MOO_DEFAULT_PFMODPOSTFIX ""
#	elif defined(__OS2__)
#		define MOO_DEFAULT_PFMODPOSTFIX ""
#	elif defined(__DOS__)
#		define MOO_DEFAULT_PFMODPOSTFIX ""
#	else
#		define MOO_DEFAULT_PFMODPOSTFIX ""
#	endif
#endif

typedef struct bb_t bb_t;
struct bb_t
{
	char buf[1024];
	moo_oow_t pos;
	moo_oow_t len;

	FILE* fp;
	moo_bch_t* fn;
};

typedef struct xtn_t xtn_t;
struct xtn_t
{
	const char* source_path; /* main source file */
#if defined(_WIN32)
	HANDLE waitable_timer;
#else
	int ep; /* epoll */
#endif
};

/* ========================================================================= */

static void* sys_alloc (moo_mmgr_t* mmgr, moo_oow_t size)
{
	return malloc (size);
}

static void* sys_realloc (moo_mmgr_t* mmgr, void* ptr, moo_oow_t size)
{
	return realloc (ptr, size);
}

static void sys_free (moo_mmgr_t* mmgr, void* ptr)
{
	free (ptr);
}

static moo_mmgr_t sys_mmgr =
{
	sys_alloc,
	sys_realloc,
	sys_free,
	MOO_NULL
};

/* ========================================================================= */

#if defined(_WIN32) || defined(__OS2__) || defined(__DOS__)
#	define IS_PATH_SEP(c) ((c) == '/' || (c) == '\\')
#else
#	define IS_PATH_SEP(c) ((c) == '/')
#endif


static const moo_bch_t* get_base_name (const moo_bch_t* path)
{
	const moo_bch_t* p, * last = MOO_NULL;

	for (p = path; *p != '\0'; p++)
	{
		if (IS_PATH_SEP(*p)) last = p;
	}

	return (last == MOO_NULL)? path: (last + 1);
}

static MOO_INLINE moo_ooi_t open_input (moo_t* moo, moo_ioarg_t* arg)
{
	xtn_t* xtn = moo_getxtn(moo);
	bb_t* bb = MOO_NULL;

/* TOOD: support predefined include directory as well */
	if (arg->includer)
	{
		/* includee */
		moo_oow_t ucslen, bcslen, parlen;
		const moo_bch_t* fn, * fb;

	#if defined(MOO_OOCH_IS_UCH)
		if (moo_convootobcstr (moo, arg->name, &ucslen, MOO_NULL, &bcslen) <= -1) goto oops;
	#else
		bcslen = moo_countbcstr (arg->name);
	#endif

		fn = ((bb_t*)arg->includer->handle)->fn;

		fb = get_base_name (fn);
		parlen = fb - fn;

		bb = moo_callocmem (moo, MOO_SIZEOF(*bb) + (MOO_SIZEOF(moo_bch_t) * (parlen + bcslen + 1)));
		if (!bb) goto oops;

		bb->fn = (moo_bch_t*)(bb + 1);
		moo_copybchars (bb->fn, fn, parlen);
	#if defined(MOO_OOCH_IS_UCH)
		moo_convootobcstr (moo, arg->name, &ucslen, &bb->fn[parlen], &bcslen);
	#else
		moo_copybcstr (&bb->fn[parlen], bcslen + 1, arg->name);
	#endif
	}
	else
	{
		/* main stream */
		moo_oow_t pathlen;

		pathlen = moo_countbcstr (xtn->source_path);

		bb = moo_callocmem (moo, MOO_SIZEOF(*bb) + (MOO_SIZEOF(moo_bch_t) * (pathlen + 1)));
		if (!bb) goto oops;

		bb->fn = (moo_bch_t*)(bb + 1);
		moo_copybcstr (bb->fn, pathlen + 1, xtn->source_path);
	}

#if defined(__DOS__) || defined(_WIN32) || defined(__OS2__)
	bb->fp = fopen (bb->fn, "rb");
#else
	bb->fp = fopen (bb->fn, "r");
#endif
	if (!bb->fp)
	{
		moo_seterrnum (moo, MOO_EIOERR);
		goto oops;
	}

	arg->handle = bb;
	return 0;

oops:
	if (bb) 
	{
		if (bb->fp) fclose (bb->fp);
		moo_freemem (moo, bb);
	}
	return -1;
}

static MOO_INLINE moo_ooi_t close_input (moo_t* moo, moo_ioarg_t* arg)
{
	/*xtn_t* xtn = moo_getxtn(moo);*/
	bb_t* bb;

	bb = (bb_t*)arg->handle;
	MOO_ASSERT (moo, bb != MOO_NULL && bb->fp != MOO_NULL);

	fclose (bb->fp);
	moo_freemem (moo, bb);

	arg->handle = MOO_NULL;
	return 0;
}


static MOO_INLINE moo_ooi_t read_input (moo_t* moo, moo_ioarg_t* arg)
{
	/*xtn_t* xtn = hcl_getxtn(hcl);*/
	bb_t* bb;
	moo_oow_t bcslen, ucslen, remlen;
	int x;

	bb = (bb_t*)arg->handle;
	MOO_ASSERT (moo, bb != MOO_NULL && bb->fp != MOO_NULL);
	do
	{
		x = fgetc (bb->fp);
		if (x == EOF)
		{
			if (ferror((FILE*)bb->fp))
			{
				moo_seterrnum (moo, MOO_EIOERR);
				return -1;
			}
			break;
		}

		bb->buf[bb->len++] = x;
	}
	while (bb->len < MOO_COUNTOF(bb->buf) && x != '\r' && x != '\n');

#if defined(MOO_OOCH_IS_UCH)
	bcslen = bb->len;
	ucslen = MOO_COUNTOF(arg->buf);
	x = moo_convbtooochars (moo, bb->buf, &bcslen, arg->buf, &ucslen);
	if (x <= -1 && ucslen <= 0) return -1;
	/* if ucslen is greater than 0, i see that some characters have been
	 * converted properly */
#else
	bcslen = (bb->len < MOO_COUNTOF(arg->buf))? bb->len: MOO_COUNTOF(arg->buf);
	ucslen = bcslen;
	moo_copybchars (arg->buf, bb->buf, bcslen);
#endif

	remlen = bb->len - bcslen;
	if (remlen > 0) memmove (bb->buf, &bb->buf[bcslen], remlen);
	bb->len = remlen;
	return ucslen;
}

static moo_ooi_t input_handler (moo_t* moo, moo_iocmd_t cmd, moo_ioarg_t* arg)
{
	switch (cmd)
	{
		case MOO_IO_OPEN:
			return open_input (moo, arg);
			
		case MOO_IO_CLOSE:
			return close_input (moo, arg);

		case MOO_IO_READ:
			return read_input (moo, arg);

		default:
			moo->errnum = MOO_EINTERN;
			return -1;
	}
}
/* ========================================================================= */

static void* dl_open (moo_t* moo, const moo_ooch_t* name, int flags)
{
#if defined(USE_LTDL)
	moo_bch_t stabuf[128], * bufptr;
	moo_oow_t ucslen, bcslen, bufcapa;
	void* handle;

	#if defined(MOO_OOCH_IS_UCH)
	if (moo_convootobcstr (moo, name, &ucslen, MOO_NULL, &bufcapa) <= -1) return MOO_NULL;
	/* +1 for terminating null. but it's not needed because MOO_COUNTOF(MOO_DEFAULT_PFMODPREFIX)
	 * and MOO_COUNTOF(MOO_DEFAULT_PFMODPOSTIFX) include the terminating nulls. Never mind about
	 * the extra 2 characters. */
	#else
	bufcapa = moo_countbcstr (name);
	#endif
	bufcapa += MOO_COUNTOF(MOO_DEFAULT_PFMODPREFIX) + MOO_COUNTOF(MOO_DEFAULT_PFMODPOSTFIX) + 1; 

	if (bufcapa <= MOO_COUNTOF(stabuf)) bufptr = stabuf;
	else
	{
		bufptr = moo_allocmem (moo, bufcapa * MOO_SIZEOF(*bufptr));
		if (!bufptr) return MOO_NULL;
	}

	if (flags & MOO_VMPRIM_OPENDL_PFMOD)
	{
		moo_oow_t len, i, xlen;

		/* opening a primitive function module - mostly libmoo-xxxx */
		len = moo_copybcstr (bufptr, bufcapa, MOO_DEFAULT_PFMODPREFIX);

		bcslen = bufcapa - len;
	#if defined(MOO_OOCH_IS_UCH)
		moo_convootobcstr (moo, name, &ucslen, &bufptr[len], &bcslen);
	#else
		bcslen = moo_copybcstr (&bufptr[len], bcslen, name);
	#endif

		/* length including the prefix and the name. but excluding the postfix */
		xlen  = len + bcslen; 

		/* convert a period(.) to a dash(-) */
		for (i = len; i < xlen; i++) 
		{
			if (bufptr[i] == '.') bufptr[i] = '-';
		}
 
	retry:
		moo_copybcstr (&bufptr[xlen], bufcapa - xlen, MOO_DEFAULT_PFMODPOSTFIX);

		/* both prefix and postfix attached. for instance, libmoo-xxx */
		handle = lt_dlopenext (bufptr);
		if (!handle) 
		{
			MOO_DEBUG3 (moo, "Failed to open(ext) DL %hs[%js] - %hs\n", bufptr, name, lt_dlerror());

			/* try without prefix and postfix */
			bufptr[xlen] = '\0';
			handle = lt_dlopenext (&bufptr[len]);
			if (!handle) 
			{
				moo_bch_t* dash;
				MOO_DEBUG3 (moo, "Failed to open(ext) DL %hs[%js] - %s\n", &bufptr[len], name, lt_dlerror());
				dash = moo_rfindbchar (bufptr, moo_countbcstr(bufptr), '-');
				if (dash) 
				{
					/* remove a segment at the back. 
					 * [NOTE] a dash contained in the original name before
					 *        period-to-dash transformation may cause extraneous/wrong
					 *        loading reattempts. */
					xlen = dash - bufptr;
					goto retry;
				}
			}
			else MOO_DEBUG3 (moo, "Opened(ext) DL %hs[%js] handle %p\n", &bufptr[len], name, handle);
		}
		else
		{
			MOO_DEBUG3 (moo, "Opened(ext) DL %hs[%js] handle %p\n", bufptr, name, handle);
		}
	}
	else
	{
		/* opening a raw shared object without a prefix and/or a postfix */
	#if defined(MOO_OOCH_IS_UCH)
		bcslen = bufcapa;
		moo_convootobcstr (moo, name, &ucslen, bufptr, &bcslen);
	#else
		bcslen = moo_copybcstr (bufptr, bufcapa, name);
	#endif

		if (moo_findbchar (bufptr, bcslen, '.'))
		{
			handle = lt_dlopen (bufptr);
			if (!handle) MOO_DEBUG2 (moo, "Failed to open DL %hs - %s\n", bufptr, lt_dlerror());
			else MOO_DEBUG2 (moo, "Opened DL %hs handle %p\n", bufptr, handle);
		}
		else
		{
			handle = lt_dlopenext (bufptr);
			if (!handle) MOO_DEBUG2 (moo, "Failed to open(ext) DL %hs - %s\n", bufptr, lt_dlerror());
			else MOO_DEBUG2 (moo, "Opened(ext) DL %hs handle %p\n", bufptr, handle);
		}
	}

	if (bufptr != stabuf) moo_freemem (moo, bufptr);
	return handle;

#else
/* TODO: support various platforms */
	/* TODO: implemenent this */
	MOO_DEBUG1 (moo, "Dynamic loading not implemented - cannot open %js\n", name);
	moo_seterrnum (moo, MOO_ENOIMPL);
	return MOO_NULL;
#endif
}

static void dl_close (moo_t* moo, void* handle)
{
#if defined(USE_LTDL)
	MOO_DEBUG1 (moo, "Closed DL handle %p\n", handle);
	lt_dlclose (handle);

#else
	/* TODO: implemenent this */
	MOO_DEBUG1 (moo, "Dynamic loading not implemented - cannot close handle %p\n", handle);
#endif
}

static void* dl_getsym (moo_t* moo, void* handle, const moo_ooch_t* name)
{
#if defined(USE_LTDL)
	moo_bch_t stabuf[64], * bufptr;
	moo_oow_t bufcapa, ucslen, bcslen, i;
	const moo_bch_t* symname;
	void* sym;

	#if defined(MOO_OOCH_IS_UCH)
	if (moo_convootobcstr (moo, name, &ucslen, MOO_NULL, &bcslen) <= -1) return MOO_NULL;
	#else
	bcslen = moo_countbcstr (name);
	#endif

	if (bcslen >= MOO_COUNTOF(stabuf) - 2)
	{
		bufcapa = bcslen + 3;
		bufptr = moo_allocmem (moo, bufcapa * MOO_SIZEOF(*bufptr));
		if (!bufptr) return MOO_NULL;
	}
	else
	{
		bufcapa = MOO_COUNTOF(stabuf);
		bufptr = stabuf;
	}

	bcslen = bufcapa - 1;
	#if defined(MOO_OOCH_IS_UCH)
	moo_convootobcstr (moo, name, &ucslen, &bufptr[1], &bcslen);
	#else
	bcslen = moo_copybcstr (&bufptr[1], bcslen, name);
	#endif

	/* convert a period(.) to an underscore(_) */
	for (i = 1; i <= bcslen; i++) if (bufptr[i] == '.') bufptr[i] = '_';

	symname = &bufptr[1]; /* try the name as it is */
	sym = lt_dlsym (handle, symname);
	if (!sym)
	{
		bufptr[0] = '_';
		symname = &bufptr[0]; /* try _name */
		sym = lt_dlsym (handle, symname);
		if (!sym)
		{
			bufptr[bcslen + 1] = '_'; 
			bufptr[bcslen + 2] = '\0';

			symname = &bufptr[1]; /* try name_ */
			sym = lt_dlsym (handle, symname);
			if (!sym)
			{
				symname = &bufptr[0]; /* try _name_ */
				sym = lt_dlsym (handle, symname);
			}
		}
	}

	if (sym) MOO_DEBUG3 (moo, "Loaded module symbol %js from handle %p - %hs\n", name, handle, symname);
	if (bufptr != stabuf) moo_freemem (moo, bufptr);
	return sym;
#else
	/* TODO: IMPLEMENT THIS */
	MOO_DEBUG2 (moo, "Dynamic loading not implemented - Cannot load module symbol %js from handle %p\n", name, handle);
	moo_seterrnum (moo, MOO_ENOIMPL);
	return MOO_NULL;
#endif
}

/* ========================================================================= */

#if defined(_WIN32)
	/* nothing to do */

#elif defined(macintosh)
	/* nothing to do */

#else
static int write_all (int fd, const char* ptr, moo_oow_t len)
{
	while (len > 0)
	{
		moo_ooi_t wr;

		wr = write (1, ptr, len);

		if (wr <= -1)
		{
		#if defined(EAGAIN) && defined(EWOULDBLOCK) && (EAGAIN == EWOULDBLOCK)
			/* TODO: push it to internal buffers? before writing data just converted, need to write buffered data first. */
			if (errno == EAGAIN) continue;
		#else

			#if defined(EAGAIN)
			if (errno == EAGAIN) continue;
			#endif
			#if defined(EWOULDBLOCK)
			if (errno == EWOULDBLOCK) continue;
			#endif

		#endif
			return -1;
		}

		ptr += wr;
		len -= wr;
	}

	return 0;
}
#endif

static void log_write (moo_t* moo, moo_oow_t mask, const moo_ooch_t* msg, moo_oow_t len)
{
#if defined(_WIN32)
#	error NOT IMPLEMENTED 
	
#elif defined(macintosh)
#	error NOT IMPLEMENTED
#else
	moo_bch_t buf[256];
	moo_oow_t ucslen, bcslen, msgidx;
	int n;
	char ts[32];
	size_t tslen;
	struct tm tm, *tmp;
	time_t now;


if (mask & MOO_LOG_GC) return; /* don't show gc logs */

/* TODO: beautify the log message.
 *       do classification based on mask. */

	now = time(NULL);
#if defined(__DOS__)
	tmp = localtime (&now);
	tslen = strftime (ts, sizeof(ts), "%Y-%m-%d %H:%M:%S ", tmp); /* no timezone info */
	if (tslen == 0) 
	{
		strcpy (ts, "0000-00-00 00:00:00");
		tslen = 19; 
	}
#else
	tmp = localtime_r (&now, &tm);
	tslen = strftime (ts, sizeof(ts), "%Y-%m-%d %H:%M:%S %z ", tmp);
	if (tslen == 0) 
	{
		strcpy (ts, "0000-00-00 00:00:00 +0000");
		tslen = 25; 
	}
#endif
	write_all (1, ts, tslen);

#if defined(MOO_OOCH_IS_UCH)
	msgidx = 0;
	while (len > 0)
	{
		ucslen = len;
		bcslen = MOO_COUNTOF(buf);

		n = moo_convootobchars (moo, &msg[msgidx], &ucslen, buf, &bcslen);
		if (n == 0 || n == -2)
		{
			/* n = 0: 
			 *   converted all successfully 
			 * n == -2: 
			 *    buffer not sufficient. not all got converted yet.
			 *    write what have been converted this round. */

			MOO_ASSERT (moo, ucslen > 0); /* if this fails, the buffer size must be increased */

			/* attempt to write all converted characters */
			if (write_all (1, buf, bcslen) <= -1) break;

			if (n == 0) break;
			else
			{
				msgidx += ucslen;
				len -= ucslen;
			}
		}
		else if (n <= -1)
		{
			/* conversion error */
			break;
		}
	}
#else
	write_all (1, msg, len);
#endif

#endif
}


/* ========================================================================= */

static int vm_startup (moo_t* moo)
{
#if defined(_WIN32)
	xtn_t* xtn = (xtn_t*)moo_getxtn(moo);
	xtn->waitable_timer = CreateWaitableTimer(MOO_NULL, TRUE, MOO_NULL);

#else
	xtn_t* xtn = (xtn_t*)moo_getxtn(moo);

	xtn->ep = epoll_create1 (EPOLL_CLOEXEC);
	if (xtn->ep == -1) 
	{
		MOO_DEBUG0 (moo, "Cannot create epoll\n");
		moo_syserrtoerrnum (errno);
		goto oops;
	}

	return 0;

oops:
	if (xtn->ep >= 0)
	{
		close (xtn->ep);
		xtn->ep = -1;
	}

	return -1;
#endif
}

static void vm_cleanup (moo_t* moo)
{
#if defined(_WIN32)
	xtn_t* xtn = (xtn_t*)moo_getxtn(moo);
	if (xtn->waitable_timer)
	{
		CloseHandle (xtn->waitable_timer);
		xtn->waitable_timer = MOO_NULL;
	}
#else
	xtn_t* xtn = (xtn_t*)moo_getxtn(moo);

	if (xtn->ep)
	{
		close (xtn->ep);
		xtn->ep = -1;
	}
#endif
}

static void vm_gettime (moo_t* moo, moo_ntime_t* now)
{
#if defined(_WIN32)
	/* TODO: */
#elif defined(__OS2__)
	ULONG out;

/* TODO: handle overflow?? */
/* TODO: use DosTmrQueryTime() and DosTmrQueryFreq()? */
	DosQuerySysInfo (QSV_MS_COUNT, QSV_MS_COUNT, &out, MOO_SIZEOF(out)); /* milliseconds */
	/* it must return NO_ERROR */
	MOO_INITNTIME (now, MOO_MSEC_TO_SEC(out), MOO_MSEC_TO_NSEC(out));
#elif defined(__DOS__) && (defined(_INTELC32_) || defined(__WATCOMC__))
	clock_t c;

/* TODO: handle overflow?? */
	c = clock ();
	now->sec = c / CLOCKS_PER_SEC;
	#if (CLOCKS_PER_SEC == 100)
		now->nsec = MOO_MSEC_TO_NSEC((c % CLOCKS_PER_SEC) * 10);
	#elif (CLOCKS_PER_SEC == 1000)
		now->nsec = MOO_MSEC_TO_NSEC(c % CLOCKS_PER_SEC);
	#elif (CLOCKS_PER_SEC == 1000000L)
		now->nsec = MOO_USEC_TO_NSEC(c % CLOCKS_PER_SEC);
	#elif (CLOCKS_PER_SEC == 1000000000L)
		now->nsec = (c % CLOCKS_PER_SEC);
	#else
	#	error UNSUPPORTED CLOCKS_PER_SEC
	#endif
#elif defined(macintosh)
	UnsignedWide tick;
	moo_uint64_t tick64;
	Microseconds (&tick);
	tick64 = *(moo_uint64_t*)&tick;
	MOO_INITNTIME (now, MOO_USEC_TO_SEC(tick64), MOO_USEC_TO_NSEC(tick64));
#elif defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
	struct timespec ts;
	clock_gettime (CLOCK_MONOTONIC, &ts);
	MOO_INITNTIME(now, ts.tv_sec, ts.tv_nsec);
#elif defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_REALTIME)
	struct timespec ts;
	clock_gettime (CLOCK_REALTIME, &ts);
	MOO_INITNTIME(now, ts.tv_sec, ts.tv_nsec);
#else
	struct timeval tv;
	gettimeofday (&tv, MOO_NULL);
	MOO_INITNTIME(now, tv.tv_sec, MOO_USEC_TO_NSEC(tv.tv_usec));
#endif
}

#if defined(__DOS__) 
#	if defined(_INTELC32_) 
	void _halt_cpu (void);
#	elif defined(__WATCOMC__)
	void _halt_cpu (void);
#	pragma aux _halt_cpu = "hlt"
#	endif
#endif

static void vm_sleep (moo_t* moo, const moo_ntime_t* dur)
{
#if defined(_WIN32)
	xtn_t* xtn = moo_getxtn(moo);
	if (xtn->waitable_timer)
	{
		LARGE_INTEGER li;
		li.QuadPart = -MOO_SECNSEC_TO_NSEC(dur->sec, dur->nsec);
		if(SetWaitableTimer(timer, &li, 0, MOO_NULL, MOO_NULL, FALSE) == FALSE) goto normal_sleep;
		WaitForSingleObject(timer, INFINITE);
	}
	else
	{
	normal_sleep:
		/* fallback to normal Sleep() */
		Sleep (MOO_SECNSEC_TO_MSEC(dur->sec,dur->nsec));
	}
#elif defined(__OS2__)

	/* TODO: in gui mode, this is not a desirable method??? 
	 *       this must be made event-driven coupled with the main event loop */
	DosSleep (MOO_SECNSEC_TO_MSEC(dur->sec,dur->nsec));

#elif defined(macintosh)

	/* TODO: ... */

#elif defined(__DOS__) && (defined(_INTELC32_) || defined(__WATCOMC__))

	clock_t c;

	c = clock ();
	c += dur->sec * CLOCKS_PER_SEC;

	#if (CLOCKS_PER_SEC == 100)
		c += MOO_NSEC_TO_MSEC(dur->nsec) / 10;
	#elif (CLOCKS_PER_SEC == 1000)
		c += MOO_NSEC_TO_MSEC(dur->nsec);
	#elif (CLOCKS_PER_SEC == 1000000L)
		c += MOO_NSEC_TO_USEC(dur->nsec);
	#elif (CLOCKS_PER_SEC == 1000000000L)
		c += dur->nsec;
	#else
	#	error UNSUPPORTED CLOCKS_PER_SEC
	#endif

/* TODO: handle clock overvlow */
/* TODO: check if there is abortion request or interrupt */
	while (c > clock()) 
	{
		_halt_cpu();
	}

#else
	
	struct timespec ts;
	ts.tv_sec = dur->sec;
	ts.tv_nsec = dur->nsec;
	nanosleep (&ts, MOO_NULL);
#endif
}

static int mux_add (moo_t* moo, moo_oop_semaphore_t sem)
{
	xtn_t* xtn = (xtn_t*)moo_getxtn(moo);
	struct epoll_event ev;
	moo_ooi_t mask;

	MOO_ASSERT (moo, MOO_OOP_IS_SMOOI(sem->io_index));
	MOO_ASSERT (moo, MOO_OOP_IS_SMOOI(sem->io_handle));
	MOO_ASSERT (moo, MOO_OOP_IS_SMOOI(sem->io_mask));

	mask = MOO_OOP_TO_SMOOI(sem->io_mask);
	ev.events = 0;
	if (mask & 1) ev.events |= EPOLLIN; /*TODO: define io mask constants... */
	if (mask & 2) ev.events |= EPOLLOUT;
	ev.data.ptr = (void*)MOO_OOP_TO_SMOOI(sem->io_index);

	if (ev.events == 0) 
	{
		MOO_DEBUG2 (moo, "<mux_add> Invalid semaphore mask %zd on handle %zd\n", mask, MOO_OOP_TO_SMOOI(sem->io_handle));
		moo_seterrnum (moo, MOO_EINVAL);
		return -1;
	}
	
	if (epoll_ctl (xtn->ep, EPOLL_CTL_ADD, MOO_OOP_TO_SMOOI(sem->io_handle), &ev) == -1)
	{
		moo_seterrnum (moo, moo_syserrtoerrnum (errno));
		MOO_DEBUG2 (moo, "<mux_add> epoll_ctl failure on handle %zd - %hs\n", MOO_OOP_TO_SMOOI(sem->io_handle), strerror(errno));
		return -1;
	}

	return 0;
}

static void mux_del (moo_t* moo, moo_oop_semaphore_t sem)
{
	xtn_t* xtn = (xtn_t*)moo_getxtn(moo);
	struct epoll_event ev;

	MOO_ASSERT (moo, MOO_OOP_IS_SMOOI(sem->io_index));
	MOO_ASSERT (moo, MOO_OOP_IS_SMOOI(sem->io_handle));
	MOO_ASSERT (moo, MOO_OOP_IS_SMOOI(sem->io_mask));

	epoll_ctl (xtn->ep, EPOLL_CTL_DEL, MOO_OOP_TO_SMOOI(sem->io_handle), &ev);
}

static void mux_wait (moo_t* moo, const moo_ntime_t* dur, moo_vmprim_muxwait_cb_t muxwcb)
{
	xtn_t* xtn = (xtn_t*)moo_getxtn(moo);
	int tmout = 0, n;
	struct epoll_event ev[32]; /*TODO: make it into xtn->evt_ptr or somewhere else as a dynamically allocated memory block */

	if (dur) tmout = MOO_SECNSEC_TO_MSEC(dur->sec, dur->nsec);

	n = epoll_wait (xtn->ep, ev, MOO_COUNTOF(ev), tmout);
	while (n > 0)
	{
		int mask;

		--n;

		mask = 0;
		if (ev[n].events & EPOLLIN) mask |= 1; /* TODO define constants for IO Mask */
		if (ev[n].events & EPOLLOUT) mask |= 2;
		if (ev[n].events & EPOLLERR) mask |= 4;
		if (ev[n].events & EPOLLHUP) mask |= 8;

		muxwcb (moo, mask, ev[n].data.ptr);
	}
}
/* ========================================================================= */

static moo_t* g_moo = MOO_NULL;

/* ========================================================================= */

#if defined(__DOS__) && (defined(_INTELC32_) || defined(__WATCOMC__))

#if defined(_INTELC32_)
static void (*prev_timer_intr_handler) (void);
#else
static void (__interrupt *prev_timer_intr_handler) (void);
#endif

#if defined(_INTELC32_)
#pragma interrupt(timer_intr_handler)
static void timer_intr_handler (void)
#else
static void __interrupt timer_intr_handler (void)
#endif
{
	/*
	_XSTACK *stk;
	int r;
	stk = (_XSTACK *)_get_stk_frame();
	r = (unsigned short)stk_ptr->eax;   
	*/

	/* The timer interrupt (normally) occurs 18.2 times per second. */
	if (g_moo) moo_switchprocess (g_moo);
	_chain_intr (prev_timer_intr_handler);
}

#elif defined(macintosh)

static TMTask g_tmtask;
static ProcessSerialNumber g_psn;

#define TMTASK_DELAY 50 /* milliseconds if positive, microseconds(after negation) if negative */

static pascal void timer_intr_handler (TMTask* task)
{
	if (g_moo) moo_switchprocess (g_moo);
	WakeUpProcess (&g_psn);
	PrimeTime ((QElem*)&g_tmtask, TMTASK_DELAY);
}

#else
static void arrange_process_switching (int sig)
{
	if (g_moo) moo_switchprocess (g_moo);
}
#endif

static void setup_tick (void)
{
#if defined(__DOS__) && (defined(_INTELC32_) || defined(__WATCOMC__))

	prev_timer_intr_handler = _dos_getvect (0x1C);
	_dos_setvect (0x1C, timer_intr_handler);

#elif defined(macintosh)

	GetCurrentProcess (&g_psn);
	memset (&g_tmtask, 0, MOO_SIZEOF(g_tmtask));
	g_tmtask.tmAddr = NewTimerProc (timer_intr_handler);
	InsXTime ((QElem*)&g_tmtask);

	PrimeTime ((QElem*)&g_tmtask, TMTASK_DELAY);

#elif defined(HAVE_SETITIMER) && defined(SIGVTALRM) && defined(ITIMER_VIRTUAL)
	struct itimerval itv;
	struct sigaction act;

	sigemptyset (&act.sa_mask);
	act.sa_handler = arrange_process_switching;
	act.sa_flags = SA_RESTART;
	sigaction (SIGVTALRM, &act, MOO_NULL);

	itv.it_interval.tv_sec = 0;
	itv.it_interval.tv_usec = 100; /* 100 microseconds */
	itv.it_value.tv_sec = 0;
	itv.it_value.tv_usec = 100;
	setitimer (ITIMER_VIRTUAL, &itv, MOO_NULL);
#else

#	error UNSUPPORTED
#endif
}

static void cancel_tick (void)
{
#if defined(__DOS__) && (defined(_INTELC32_) || defined(__WATCOMC__))

	_dos_setvect (0x1C, prev_timer_intr_handler);

#elif defined(macintosh)
	RmvTime ((QElem*)&g_tmtask);
	/*DisposeTimerProc (g_tmtask.tmAddr);*/

#elif defined(HAVE_SETITIMER) && defined(SIGVTALRM) && defined(ITIMER_VIRTUAL)
	struct itimerval itv;
	struct sigaction act;

	itv.it_interval.tv_sec = 0;
	itv.it_interval.tv_usec = 0;
	itv.it_value.tv_sec = 0; /* make setitimer() one-shot only */
	itv.it_value.tv_usec = 0;
	setitimer (ITIMER_VIRTUAL, &itv, MOO_NULL);

	sigemptyset (&act.sa_mask); 
	act.sa_handler = SIG_IGN; /* ignore the signal potentially fired by the one-shot arrange above */
	act.sa_flags = 0;
	sigaction (SIGVTALRM, &act, MOO_NULL);

#else
#	error UNSUPPORTED
#endif
}

static void handle_term (int sig)
{
	if (g_moo) moo_abort (g_moo);
}

static void setup_term (void)
{
	struct sigaction sa;
	sa.sa_handler = handle_term;
	sigaction (SIGINT, &sa, MOO_NULL);
}

/* ========================================================================= */

int main (int argc, char* argv[])
{
	static moo_ooch_t str_my_object[] = { 'M', 'y', 'O', 'b','j','e','c','t' }; /*TODO: make this an argument */
	static moo_ooch_t str_main[] = { 'm', 'a', 'i', 'n' };

	moo_t* moo;
	xtn_t* xtn;
	moo_oocs_t objname;
	moo_oocs_t mthname;
	moo_vmprim_t vmprim;
	int i, xret;

#if !defined(macintosh)
	if (argc < 2)
	{
		fprintf (stderr, "Usage: %s filename ...\n", argv[0]);
		return -1;
	}
#endif

	vmprim.dl_open = dl_open;
	vmprim.dl_close = dl_close;
	vmprim.dl_getsym = dl_getsym;
	vmprim.log_write = log_write;

	vmprim.vm_startup = vm_startup;
	vmprim.vm_cleanup = vm_cleanup;
	vmprim.vm_gettime = vm_gettime;
	vmprim.vm_sleep = vm_sleep;
	vmprim.mux_add = mux_add;
	vmprim.mux_del = mux_del;
	vmprim.mux_wait = mux_wait;

#if defined(USE_LTDL)
	lt_dlinit ();
#endif

	moo = moo_open (&sys_mmgr, MOO_SIZEOF(xtn_t), 2048000lu, &vmprim, MOO_NULL);
	if (!moo)
	{
		printf ("cannot open moo\n");
		return -1;
	}


	{
		moo_oow_t tab_size;

		tab_size = 5000;
		moo_setoption (moo, MOO_SYMTAB_SIZE, &tab_size);
		tab_size = 5000;
		moo_setoption (moo, MOO_SYSDIC_SIZE, &tab_size);
		tab_size = 600;
		moo_setoption (moo, MOO_PROCSTK_SIZE, &tab_size);
	}

	{
		int trait = 0;

		/*trait |= MOO_NOGC;*/
		trait |= MOO_AWAIT_PROCS;
		moo_setoption (moo, MOO_TRAIT, &trait);
	}

	if (moo_ignite(moo) <= -1)
	{
		moo_logbfmt (moo, MOO_LOG_ERROR, "cannot ignite moo - [%d] %js\n", moo_geterrnum(moo), moo_geterrstr(moo));
		moo_close (moo);
		return -1;
	}

	xtn = moo_getxtn (moo);

#if defined(macintosh)
	i = 20;
	xtn->source_path = "test.st";
	goto compile;
#endif

	for (i = 1; i < argc; i++)
	{
		xtn->source_path = argv[i];

	compile:

		if (moo_compile (moo, input_handler) <= -1)
		{
			if (moo->errnum == MOO_ESYNTAX)
			{
				moo_synerr_t synerr;
				moo_bch_t bcs[1024]; /* TODO: right buffer size */
				moo_oow_t bcslen, ucslen;

				moo_getsynerr (moo, &synerr);

				printf ("ERROR: ");
				if (synerr.loc.file)
				{
				#if defined(MOO_OOCH_IS_UCH)
					bcslen = MOO_COUNTOF(bcs);
					if (moo_convootobcstr (moo, synerr.loc.file, &ucslen, bcs, &bcslen) >= 0)
					{
						printf ("%.*s ", (int)bcslen, bcs);
					}
				#else
					printf ("%s ", synerr.loc.file);
				#endif
				}
				else
				{
					printf ("%s ", xtn->source_path);
				}

				printf ("syntax error at line %lu column %lu - ", 
					(unsigned long int)synerr.loc.line, (unsigned long int)synerr.loc.colm);

				bcslen = MOO_COUNTOF(bcs);
			#if defined(MOO_OOCH_IS_UCH)
				if (moo_convootobcstr (moo, moo_synerrnumtoerrstr(synerr.num), &ucslen, bcs, &bcslen) >= 0)
				{
					printf (" [%.*s]", (int)bcslen, bcs);
				}
			#else
				printf (" [%s]", moo_synerrnumtoerrstr(synerr.num));
			#endif

				if (synerr.tgt.len > 0)
				{
					bcslen = MOO_COUNTOF(bcs);
					ucslen = synerr.tgt.len;

				#if defined(MOO_OOCH_IS_UCH)
					if (moo_convootobchars (moo, synerr.tgt.ptr, &ucslen, bcs, &bcslen) >= 0)
					{
						printf (" [%.*s]", (int)bcslen, bcs);
					}
				#else
					printf (" [%.*s]", (int)synerr.tgt.len, synerr.tgt.ptr);
				#endif

				}
				printf ("\n");
			}
			else
			{
				moo_logbfmt (moo, MOO_LOG_ERROR, "ERROR: cannot compile code - [%d] %js\n", moo_geterrnum(moo), moo_geterrstr(moo));
			}
			moo_close (moo);
#if defined(USE_LTDL)
			lt_dlexit ();
#endif
			return -1;
		}
	}

	MOO_DEBUG0 (moo, "COMPILE OK. STARTING EXECUTION...\n");
	xret = 0;
	g_moo = moo;
	setup_tick ();
	setup_term ();

	objname.ptr = str_my_object;
	objname.len = 8;
	mthname.ptr = str_main;
	mthname.len = 4;
	if (moo_invoke (moo, &objname, &mthname) <= -1)
	{
		moo_logbfmt (moo, MOO_LOG_ERROR, "ERROR: cannot execute code - [%d] %js\n", moo_geterrnum(moo), moo_geterrstr(moo));
		xret = -1;
	}

	cancel_tick ();
	g_moo = MOO_NULL;

	/*moo_dumpsymtab(moo);
	 *moo_dumpdic(moo, moo->sysdic, "System dictionary");*/

	moo_close (moo);

#if defined(USE_LTDL)
	lt_dlexit ();
#endif

#if defined(_WIN32) && defined(_DEBUG)
getchar();
#endif
	return xret;
}
