moo/moo/lib/main.c

785 lines
17 KiB
C
Raw Normal View History

/*
* $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.
*/
2017-01-09 09:54:49 +00:00
#include "moo-prv.h"
#include <stdio.h>
#include <stdlib.h>
2015-05-17 05:02:30 +00:00
#include <string.h>
2015-06-04 18:34:37 +00:00
#include <limits.h>
#include <errno.h>
2015-10-14 13:25:36 +00:00
#if defined(_WIN32)
# include <windows.h>
# include <tchar.h>
2017-01-09 09:54:49 +00:00
# if defined(MOO_HAVE_CFG_H)
2015-10-14 13:25:36 +00:00
# include <ltdl.h>
# define USE_LTDL
# endif
#elif defined(__OS2__)
# define INCL_DOSMODULEMGR
# define INCL_DOSPROCESS
# define INCL_DOSERRORS
# include <os2.h>
2017-01-09 14:52:15 +00:00
#elif defined(__DOS__)
2016-05-18 14:53:20 +00:00
# include <dos.h>
2017-01-09 14:52:15 +00:00
# include <time.h>
#elif defined(macintosh)
# include <Timer.h>
2015-10-14 13:25:36 +00:00
#else
# include <unistd.h>
# include <ltdl.h>
# define USE_LTDL
# 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
2015-10-14 13:25:36 +00:00
#endif
#if !defined(MOO_DEFAULT_PFMODPREFIX)
2015-10-14 09:06:44 +00:00
# if defined(_WIN32)
# define MOO_DEFAULT_PFMODPREFIX "moo-"
2015-10-14 09:06:44 +00:00
# elif defined(__OS2__)
# define MOO_DEFAULT_PFMODPREFIX "moo"
2017-01-09 14:52:15 +00:00
# elif defined(__DOS__)
# define MOO_DEFAULT_PFMODPREFIX "moo"
2015-10-14 09:06:44 +00:00
# else
# define MOO_DEFAULT_PFMODPREFIX "libmoo-"
2015-10-14 09:06:44 +00:00
# endif
#endif
#if !defined(MOO_DEFAULT_PFMODPOSTFIX)
2015-10-14 09:06:44 +00:00
# if defined(_WIN32)
# define MOO_DEFAULT_PFMODPOSTFIX ""
2015-10-14 09:06:44 +00:00
# elif defined(__OS2__)
# define MOO_DEFAULT_PFMODPOSTFIX ""
2017-01-09 14:52:15 +00:00
# elif defined(__DOS__)
# define MOO_DEFAULT_PFMODPOSTFIX ""
2015-10-14 09:06:44 +00:00
# else
# define MOO_DEFAULT_PFMODPOSTFIX ""
2015-10-14 09:06:44 +00:00
# endif
#endif
2016-08-15 12:19:04 +00:00
typedef struct bb_t bb_t;
struct bb_t
{
char buf[1024];
2017-01-09 09:54:49 +00:00
moo_oow_t pos;
moo_oow_t len;
2016-08-15 12:19:04 +00:00
FILE* fp;
};
typedef struct xtn_t xtn_t;
struct xtn_t
{
2016-08-15 12:19:04 +00:00
const char* source_path; /* main source file */
2015-05-16 07:31:16 +00:00
};
2016-05-27 15:01:54 +00:00
/* ========================================================================= */
2017-01-09 09:54:49 +00:00
static void* sys_alloc (moo_mmgr_t* mmgr, moo_oow_t size)
{
return malloc (size);
}
2017-01-09 09:54:49 +00:00
static void* sys_realloc (moo_mmgr_t* mmgr, void* ptr, moo_oow_t size)
{
return realloc (ptr, size);
}
2017-01-09 09:54:49 +00:00
static void sys_free (moo_mmgr_t* mmgr, void* ptr)
{
free (ptr);
}
2017-01-09 09:54:49 +00:00
static moo_mmgr_t sys_mmgr =
{
sys_alloc,
sys_realloc,
sys_free,
2017-01-09 09:54:49 +00:00
MOO_NULL
};
2016-05-27 15:01:54 +00:00
/* ========================================================================= */
#if defined(_WIN32) || defined(__OS2__) || defined(__DOS__)
# define IS_PATH_SEP(c) ((c) == '/' || (c) == '\\')
#else
# define IS_PATH_SEP(c) ((c) == '/')
#endif
2017-01-09 09:54:49 +00:00
static const moo_bch_t* get_base_name (const moo_bch_t* path)
{
2017-01-09 09:54:49 +00:00
const moo_bch_t* p, * last = MOO_NULL;
for (p = path; *p != '\0'; p++)
{
if (IS_PATH_SEP(*p)) last = p;
}
2017-01-09 09:54:49 +00:00
return (last == MOO_NULL)? path: (last + 1);
}
2017-01-09 09:54:49 +00:00
static MOO_INLINE moo_ooi_t open_input (moo_t* moo, moo_ioarg_t* arg)
{
2017-01-09 09:54:49 +00:00
xtn_t* xtn = moo_getxtn(moo);
2016-08-15 12:19:04 +00:00
bb_t* bb;
FILE* fp;
if (arg->includer)
{
/* includee */
2017-01-09 09:54:49 +00:00
moo_bch_t bcs[1024]; /* TODO: right buffer size */
moo_oow_t bcslen = MOO_COUNTOF(bcs);
moo_oow_t ucslen;
if (moo_convootobcstr (moo, arg->name, &ucslen, bcs, &bcslen) <= -1) return -1;
2015-05-20 14:27:47 +00:00
2015-07-03 14:38:37 +00:00
/* TODO: make bcs relative to the includer */
2017-01-09 14:52:15 +00:00
#if defined(__DOS__) || defined(_WIN32) || defined(__OS2__)
2016-08-15 12:19:04 +00:00
fp = fopen (bcs, "rb");
2015-06-11 13:10:33 +00:00
#else
2016-08-15 12:19:04 +00:00
fp = fopen (bcs, "r");
2015-06-11 13:10:33 +00:00
#endif
}
else
{
/* main stream */
2017-01-09 14:52:15 +00:00
#if defined(__DOS__) || defined(_WIN32) || defined(__OS2__)
2016-08-15 12:19:04 +00:00
fp = fopen (xtn->source_path, "rb");
2015-06-11 13:10:33 +00:00
#else
2016-08-15 12:19:04 +00:00
fp = fopen (xtn->source_path, "r");
2015-06-11 13:10:33 +00:00
#endif
}
2015-05-17 05:02:30 +00:00
2016-08-15 12:19:04 +00:00
if (!fp)
2015-05-17 05:02:30 +00:00
{
2017-01-09 09:54:49 +00:00
moo_seterrnum (moo, MOO_EIOERR);
2015-05-17 05:02:30 +00:00
return -1;
}
2017-01-09 09:54:49 +00:00
bb = moo_callocmem (moo, MOO_SIZEOF(*bb));
2016-08-15 12:19:04 +00:00
if (!bb)
{
fclose (fp);
return -1;
}
bb->fp = fp;
arg->handle = bb;
return 0;
}
2017-01-09 09:54:49 +00:00
static MOO_INLINE moo_ooi_t close_input (moo_t* moo, moo_ioarg_t* arg)
2016-08-15 12:19:04 +00:00
{
2017-01-09 09:54:49 +00:00
xtn_t* xtn = moo_getxtn(moo);
2016-08-15 12:19:04 +00:00
bb_t* bb;
bb = (bb_t*)arg->handle;
2017-01-09 09:54:49 +00:00
MOO_ASSERT (moo, bb != MOO_NULL && bb->fp != MOO_NULL);
2016-08-15 12:19:04 +00:00
fclose (bb->fp);
2017-01-09 09:54:49 +00:00
moo_freemem (moo, bb);
2016-08-15 12:19:04 +00:00
2017-01-09 09:54:49 +00:00
arg->handle = MOO_NULL;
2015-05-17 05:02:30 +00:00
return 0;
}
2016-08-15 12:19:04 +00:00
2017-01-09 09:54:49 +00:00
static MOO_INLINE moo_ooi_t read_input (moo_t* moo, moo_ioarg_t* arg)
{
/*xtn_t* xtn = hcl_getxtn(hcl);*/
2016-08-15 12:19:04 +00:00
bb_t* bb;
2017-01-09 09:54:49 +00:00
moo_oow_t bcslen, ucslen, remlen;
2015-05-16 07:31:16 +00:00
int x;
2016-08-15 12:19:04 +00:00
bb = (bb_t*)arg->handle;
2017-01-09 09:54:49 +00:00
MOO_ASSERT (moo, bb != MOO_NULL && bb->fp != MOO_NULL);
do
{
x = fgetc (bb->fp);
if (x == EOF)
{
if (ferror((FILE*)bb->fp))
{
2017-01-09 09:54:49 +00:00
moo_seterrnum (moo, MOO_EIOERR);
return -1;
}
break;
}
bb->buf[bb->len++] = x;
}
2017-01-09 09:54:49 +00:00
while (bb->len < MOO_COUNTOF(bb->buf) && x != '\r' && x != '\n');
2016-08-15 12:19:04 +00:00
bcslen = bb->len;
2017-01-09 09:54:49 +00:00
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 */
2015-05-17 05:02:30 +00:00
2016-08-15 12:19:04 +00:00
remlen = bb->len - bcslen;
if (remlen > 0) memmove (bb->buf, &bb->buf[bcslen], remlen);
bb->len = remlen;
2015-05-17 05:02:30 +00:00
return ucslen;
}
2017-01-09 09:54:49 +00:00
static moo_ooi_t input_handler (moo_t* moo, moo_iocmd_t cmd, moo_ioarg_t* arg)
{
switch (cmd)
{
2017-01-09 09:54:49 +00:00
case MOO_IO_OPEN:
return open_input (moo, arg);
2017-01-09 09:54:49 +00:00
case MOO_IO_CLOSE:
return close_input (moo, arg);
2017-01-09 09:54:49 +00:00
case MOO_IO_READ:
return read_input (moo, arg);
default:
2017-01-09 09:54:49 +00:00
moo->errnum = MOO_EINTERN;
return -1;
}
}
2016-05-27 15:01:54 +00:00
/* ========================================================================= */
static void* dl_open (moo_t* moo, const moo_ooch_t* name, int flags)
{
2015-10-14 13:25:36 +00:00
#if defined(USE_LTDL)
/* TODO: support various platforms */
2017-01-09 09:54:49 +00:00
moo_bch_t buf[1024]; /* TODO: use a proper path buffer */
moo_oow_t ucslen, bcslen;
2015-10-14 13:25:36 +00:00
void* handle;
if (flags & MOO_VMPRIM_OPENDL_PFMOD)
{
moo_oow_t len;
/* opening a primitive function module */
len = moo_copybcstr (buf, MOO_COUNTOF(buf), MOO_DEFAULT_PFMODPREFIX);
bcslen = MOO_COUNTOF(buf) - len;
if (moo_convootobcstr (moo, name, &ucslen, &buf[len], &bcslen) <= -1) return MOO_NULL;
moo_copybcstr (&buf[bcslen + len], MOO_COUNTOF(buf) - bcslen - len, MOO_DEFAULT_PFMODPOSTFIX);
2015-10-14 13:25:36 +00:00
handle = lt_dlopenext (buf);
if (!handle)
{
MOO_DEBUG3 (moo, "Failed to open(ext) DL %hs[%js] - %hs\n", buf, name, lt_dlerror());
buf[bcslen + len] = '\0';
handle = lt_dlopenext (&buf[len]);
if (!handle) MOO_DEBUG3 (moo, "Failed to open(ext) DL %hs[%js] - %s\n", &buf[len], name, lt_dlerror());
else MOO_DEBUG3 (moo, "Opened(ext) DL %hs[%js] handle %p\n", &buf[len], name, handle);
}
else
{
MOO_DEBUG3 (moo, "Opened(ext) DL %hs[%js] handle %p\n", buf, name, handle);
}
2016-08-23 00:37:22 +00:00
}
else
{
/* opening a raw shared object */
bcslen = MOO_COUNTOF(buf);
if (moo_convootobcstr (moo, name, &ucslen, buf, &bcslen) <= -1) return MOO_NULL;
if (moo_findbchar (buf, bcslen, '.'))
{
handle = lt_dlopen (buf);
if (!handle) MOO_DEBUG2 (moo, "Failed to open DL %hs - %s\n", buf, lt_dlerror());
else MOO_DEBUG2 (moo, "Opened DL %hs handle %p\n", buf, handle);
}
else
{
handle = lt_dlopenext (buf);
if (!handle) MOO_DEBUG2 (moo, "Failed to open(ext) DL %hs - %s\n", buf, lt_dlerror());
else MOO_DEBUG2 (moo, "Opened(ext) DL %hs handle %p\n", buf, handle);
}
2015-10-14 13:25:36 +00:00
}
return handle;
#else
/* TODO: implemenent this */
2017-01-09 09:54:49 +00:00
return MOO_NULL;
2015-10-14 13:25:36 +00:00
#endif
}
2017-01-09 09:54:49 +00:00
static void dl_close (moo_t* moo, void* handle)
{
MOO_DEBUG1 (moo, "Closed DL handle %p\n", handle);
2015-10-14 13:25:36 +00:00
#if defined(USE_LTDL)
lt_dlclose (handle);
#elif defined(_WIN32)
FreeLibrary ((HMODULE)handle);
#elif defined(__OS2__)
DosFreeModule ((HMODULE)handle);
2017-01-09 14:52:15 +00:00
#elif defined(__DOS__) && defined(QSE_ENABLE_DOS_DYNAMIC_MODULE)
2015-10-14 13:25:36 +00:00
FreeModule (handle);
#else
/* nothing to do */
2015-10-14 13:25:36 +00:00
#endif
}
2017-01-09 09:54:49 +00:00
static void* dl_getsym (moo_t* moo, void* handle, const moo_ooch_t* name)
{
2015-10-14 13:25:36 +00:00
#if defined(USE_LTDL)
2017-01-09 09:54:49 +00:00
moo_bch_t buf[1024]; /* TODO: use a proper buffer. dynamically allocated if conversion result in too a large value */
moo_oow_t ucslen, bcslen;
void* sym;
2016-08-23 00:37:22 +00:00
const char* symname;
buf[0] = '_';
2017-01-09 09:54:49 +00:00
bcslen = MOO_COUNTOF(buf) - 2;
moo_convootobcstr (moo, name, &ucslen, &buf[1], &bcslen); /* TODO: error check */
2016-08-23 00:37:22 +00:00
symname = &buf[1];
sym = lt_dlsym (handle, symname);
if (!sym)
{
2016-08-23 00:37:22 +00:00
symname = &buf[0];
sym = lt_dlsym (handle, symname);
if (!sym)
{
buf[bcslen + 1] = '_';
buf[bcslen + 2] = '\0';
2016-08-23 00:37:22 +00:00
symname = &buf[1];
sym = lt_dlsym (handle, symname);
if (!sym)
{
2016-08-23 00:37:22 +00:00
symname = &buf[0];
sym = lt_dlsym (handle, symname);
}
}
}
2017-01-09 09:54:49 +00:00
if (sym) MOO_DEBUG2 (moo, "Loaded module symbol %s from handle %p\n", symname, handle);
return sym;
2015-10-14 13:25:36 +00:00
#else
/* TODO: IMPLEMENT THIS */
2017-01-09 09:54:49 +00:00
return MOO_NULL;
2015-10-14 13:25:36 +00:00
#endif
}
2016-05-27 15:01:54 +00:00
/* ========================================================================= */
2016-06-03 15:46:01 +00:00
2017-01-09 09:54:49 +00:00
static int write_all (int fd, const char* ptr, moo_oow_t len)
2016-10-06 14:17:24 +00:00
{
while (len > 0)
{
2017-01-09 09:54:49 +00:00
moo_ooi_t wr;
2016-10-06 14:17:24 +00:00
wr = write (1, ptr, len);
if (wr <= -1)
{
2017-01-09 13:48:33 +00:00
#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
2016-10-06 14:17:24 +00:00
return -1;
}
ptr += wr;
len -= wr;
}
return 0;
}
2017-01-09 09:54:49 +00:00
static void log_write (moo_t* moo, moo_oow_t mask, const moo_ooch_t* msg, moo_oow_t len)
2016-05-27 15:01:54 +00:00
{
#if defined(_WIN32)
# error NOT IMPLEMENTED
#else
2017-01-09 09:54:49 +00:00
moo_bch_t buf[256];
moo_oow_t ucslen, bcslen, msgidx;
2016-05-27 15:01:54 +00:00
int n;
2016-10-06 14:17:24 +00:00
char ts[32];
size_t tslen;
2016-10-06 14:17:24 +00:00
struct tm tm, *tmp;
time_t now;
2016-05-27 15:01:54 +00:00
2017-01-09 14:52:15 +00:00
2017-01-09 09:54:49 +00:00
if (mask & MOO_LOG_GC) return; /* don't show gc logs */
/* TODO: beautify the log message.
* do classification based on mask. */
now = time(NULL);
2017-01-09 14:52:15 +00:00
#if defined(__DOS__)
2016-10-06 14:17:24 +00:00
tmp = localtime (&now);
#else
tmp = localtime_r (&now, &tm);
#endif
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;
}
write_all (1, ts, tslen);
2016-10-06 14:17:24 +00:00
msgidx = 0;
while (len > 0)
2016-05-27 15:01:54 +00:00
{
ucslen = len;
2017-01-09 09:54:49 +00:00
bcslen = MOO_COUNTOF(buf);
2016-05-27 15:01:54 +00:00
2017-01-09 09:54:49 +00:00
n = moo_convootobchars (moo, &msg[msgidx], &ucslen, buf, &bcslen);
if (n == 0 || n == -2)
2016-05-27 15:01:54 +00:00
{
/* n = 0:
* converted all successfully
* n == -2:
* buffer not sufficient. not all got converted yet.
* write what have been converted this round. */
2017-01-09 09:54:49 +00:00
MOO_ASSERT (moo, ucslen > 0); /* if this fails, the buffer size must be increased */
/* attempt to write all converted characters */
2016-10-06 14:17:24 +00:00
if (write_all (1, buf, bcslen) <= -1) break;
if (n == 0) break;
else
{
msgidx += ucslen;
len -= ucslen;
}
2016-05-27 15:01:54 +00:00
}
else if (n <= -1)
{
/* conversion error */
break;
}
}
#endif
}
/* ========================================================================= */
2017-01-09 09:54:49 +00:00
static moo_ooch_t str_my_object[] = { 'M', 'y', 'O', 'b','j','e','c','t' };
static moo_ooch_t str_main[] = { 'm', 'a', 'i', 'n' };
static moo_t* g_moo = MOO_NULL;
2016-05-27 15:01:54 +00:00
/* ========================================================================= */
2017-01-09 14:52:15 +00:00
#if defined(__DOS__) && defined(_INTELC32_)
2016-05-18 14:53:20 +00:00
static void (*prev_timer_intr_handler) (void);
#pragma interrupt(timer_intr_handler)
static void timer_intr_handler (void)
{
/*
_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. */
2017-01-09 09:54:49 +00:00
if (g_moo) moo_switchprocess (g_moo);
2016-05-18 14:53:20 +00:00
_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)
{
2017-01-09 09:54:49 +00:00
if (g_moo) moo_switchprocess (g_moo);
WakeUpProcess (&g_psn);
PrimeTime ((QElem*)&g_tmtask, TMTASK_DELAY);
}
2016-05-18 14:53:20 +00:00
#else
static void arrange_process_switching (int sig)
{
2017-01-09 09:54:49 +00:00
if (g_moo) moo_switchprocess (g_moo);
}
2016-05-18 14:53:20 +00:00
#endif
static void setup_tick (void)
{
2017-01-09 14:52:15 +00:00
#if defined(__DOS__) && defined(_INTELC32_)
2016-05-18 14:53:20 +00:00
prev_timer_intr_handler = _dos_getvect (0x1C);
_dos_setvect (0x1C, timer_intr_handler);
#elif defined(macintosh)
GetCurrentProcess (&g_psn);
2017-01-09 09:54:49 +00:00
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);
2016-05-18 14:53:20 +00:00
#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 = 0;
2017-01-09 09:54:49 +00:00
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;
2017-01-09 09:54:49 +00:00
setitimer (ITIMER_VIRTUAL, &itv, MOO_NULL);
2016-05-18 14:53:20 +00:00
#else
# error UNSUPPORTED
#endif
}
static void cancel_tick (void)
{
2017-01-09 14:52:15 +00:00
#if defined(__DOS__) && defined(_INTELC32_)
2016-05-18 14:53:20 +00:00
_dos_setvect (0x1C, prev_timer_intr_handler);
#elif defined(macintosh)
RmvTime ((QElem*)&g_tmtask);
/*DisposeTimerProc (g_tmtask.tmAddr);*/
2016-05-18 14:53:20 +00:00
#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;
2017-01-09 09:54:49 +00:00
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;
2017-01-09 09:54:49 +00:00
sigaction (SIGVTALRM, &act, MOO_NULL);
2016-05-18 14:53:20 +00:00
#else
# error UNSUPPORTED
#endif
}
2016-05-27 15:01:54 +00:00
/* ========================================================================= */
int main (int argc, char* argv[])
{
2017-01-09 09:54:49 +00:00
moo_t* moo;
2015-05-16 07:31:16 +00:00
xtn_t* xtn;
2017-01-09 09:54:49 +00:00
moo_oocs_t objname;
moo_oocs_t mthname;
moo_vmprim_t vmprim;
int i, xret;
2015-05-16 07:31:16 +00:00
#if !defined(macintosh)
2015-06-11 12:09:10 +00:00
if (argc < 2)
2015-05-16 07:31:16 +00:00
{
2015-06-11 12:09:10 +00:00
fprintf (stderr, "Usage: %s filename ...\n", argv[0]);
2015-05-16 07:31:16 +00:00
return -1;
}
#endif
2015-05-16 07:31:16 +00:00
vmprim.dl_open = dl_open;
vmprim.dl_close = dl_close;
vmprim.dl_getsym = dl_getsym;
2016-05-27 15:01:54 +00:00
vmprim.log_write = log_write;
2015-10-14 13:25:36 +00:00
#if defined(USE_LTDL)
lt_dlinit ();
#endif
2017-01-09 09:54:49 +00:00
moo = moo_open (&sys_mmgr, MOO_SIZEOF(xtn_t), 2048000lu, &vmprim, MOO_NULL);
if (!moo)
{
2017-01-09 09:54:49 +00:00
printf ("cannot open moo\n");
return -1;
}
{
2017-01-09 09:54:49 +00:00
moo_oow_t tab_size;
tab_size = 5000;
2017-01-09 09:54:49 +00:00
moo_setoption (moo, MOO_SYMTAB_SIZE, &tab_size);
tab_size = 5000;
2017-01-09 09:54:49 +00:00
moo_setoption (moo, MOO_SYSDIC_SIZE, &tab_size);
2016-02-13 17:39:11 +00:00
tab_size = 600;
2017-01-09 09:54:49 +00:00
moo_setoption (moo, MOO_PROCSTK_SIZE, &tab_size);
}
2015-06-28 14:20:37 +00:00
{
int trait = 0;
2017-01-09 09:54:49 +00:00
/*trait |= MOO_NOGC;*/
trait |= MOO_AWAIT_PROCS;
moo_setoption (moo, MOO_TRAIT, &trait);
2015-06-28 14:20:37 +00:00
}
2017-01-09 09:54:49 +00:00
if (moo_ignite(moo) <= -1)
{
moo_logbfmt (moo, MOO_LOG_ERROR, "cannot ignite moo - [%d] %js\n", moo_geterrnum(moo), moo_geterrstr(moo));
2017-01-09 09:54:49 +00:00
moo_close (moo);
return -1;
}
2017-01-09 09:54:49 +00:00
xtn = moo_getxtn (moo);
2015-06-11 12:09:10 +00:00
#if defined(macintosh)
i = 20;
xtn->source_path = "test.st";
2015-06-11 12:09:10 +00:00
goto compile;
#endif
2015-06-11 12:09:10 +00:00
for (i = 1; i < argc; i++)
{
2015-06-11 12:09:10 +00:00
xtn->source_path = argv[i];
2015-05-20 15:52:45 +00:00
2015-06-11 12:09:10 +00:00
compile:
2015-05-20 15:52:45 +00:00
2017-01-09 09:54:49 +00:00
if (moo_compile (moo, input_handler) <= -1)
2015-06-11 12:09:10 +00:00
{
2017-01-09 09:54:49 +00:00
if (moo->errnum == MOO_ESYNTAX)
2015-05-20 15:52:45 +00:00
{
2017-01-09 09:54:49 +00:00
moo_synerr_t synerr;
moo_bch_t bcs[1024]; /* TODO: right buffer size */
moo_oow_t bcslen, ucslen;
2015-06-11 12:09:10 +00:00
2017-01-09 09:54:49 +00:00
moo_getsynerr (moo, &synerr);
2015-06-11 12:09:10 +00:00
printf ("ERROR: ");
if (synerr.loc.file)
2015-05-20 15:52:45 +00:00
{
2017-01-09 09:54:49 +00:00
bcslen = MOO_COUNTOF(bcs);
if (moo_convootobcstr (moo, synerr.loc.file, &ucslen, bcs, &bcslen) >= 0)
2015-06-11 12:09:10 +00:00
{
printf ("%.*s ", (int)bcslen, bcs);
}
2015-05-20 15:52:45 +00:00
}
else
{
printf ("%s ", xtn->source_path);
}
2015-05-20 15:52:45 +00:00
printf ("syntax error at line %lu column %lu - ",
(unsigned long int)synerr.loc.line, (unsigned long int)synerr.loc.colm);
2017-01-09 09:54:49 +00:00
bcslen = MOO_COUNTOF(bcs);
if (moo_convootobcstr (moo, moo_synerrnumtoerrstr(synerr.num), &ucslen, bcs, &bcslen) >= 0)
{
printf (" [%.*s]", (int)bcslen, bcs);
}
2015-06-11 12:09:10 +00:00
if (synerr.tgt.len > 0)
2015-05-19 15:16:18 +00:00
{
2017-01-09 09:54:49 +00:00
bcslen = MOO_COUNTOF(bcs);
2015-06-11 12:09:10 +00:00
ucslen = synerr.tgt.len;
2017-01-09 09:54:49 +00:00
if (moo_convootobchars (moo, synerr.tgt.ptr, &ucslen, bcs, &bcslen) >= 0)
2015-06-11 12:09:10 +00:00
{
printf (" [%.*s]", (int)bcslen, bcs);
}
2015-05-19 15:16:18 +00:00
2015-06-11 12:09:10 +00:00
}
printf ("\n");
2015-05-19 15:16:18 +00:00
}
2015-06-11 12:09:10 +00:00
else
{
moo_logbfmt (moo, MOO_LOG_ERROR, "ERROR: cannot compile code - [%d] %js\n", moo_geterrnum(moo), moo_geterrstr(moo));
2015-06-11 12:09:10 +00:00
}
2017-01-09 09:54:49 +00:00
moo_close (moo);
#if defined(USE_LTDL)
lt_dlexit ();
#endif
2015-06-11 12:09:10 +00:00
return -1;
2015-05-19 15:16:18 +00:00
}
}
MOO_DEBUG0 (moo, "COMPILE OK. STARTING EXECUTION...\n");
xret = 0;
2017-01-09 09:54:49 +00:00
g_moo = moo;
setup_tick ();
objname.ptr = str_my_object;
objname.len = 8;
2015-06-06 07:24:35 +00:00
mthname.ptr = str_main;
mthname.len = 4;
2017-01-09 09:54:49 +00:00
if (moo_invoke (moo, &objname, &mthname) <= -1)
2015-06-04 18:34:37 +00:00
{
moo_logbfmt (moo, MOO_LOG_ERROR, "ERROR: cannot execute code - [%d] %js\n", moo_geterrnum(moo), moo_geterrstr(moo));
xret = -1;
2015-06-04 18:34:37 +00:00
}
2015-06-06 07:24:35 +00:00
cancel_tick ();
2017-01-09 09:54:49 +00:00
g_moo = MOO_NULL;
2015-06-06 07:24:35 +00:00
2017-01-09 09:54:49 +00:00
/*moo_dumpsymtab(moo);
*moo_dumpdic(moo, moo->sysdic, "System dictionary");*/
2017-01-09 09:54:49 +00:00
moo_close (moo);
2015-10-14 13:25:36 +00:00
#if defined(USE_LTDL)
lt_dlexit ();
2015-11-22 13:32:06 +00:00
#endif
#if defined(_WIN32) && defined(_DEBUG)
getchar();
2015-10-14 13:25:36 +00:00
#endif
return xret;
}