added moo_seterrbfmtwithsyserr() and moo_seterrufmtwithsyserr()

This commit is contained in:
hyunghwan.chung 2018-11-13 06:54:30 +00:00
parent 192d71c80e
commit 6939433ff0
9 changed files with 274 additions and 103 deletions

View File

@ -9268,18 +9268,18 @@ static MOO_INLINE int _compile (moo_t* moo, moo_ioimpl_t io)
if (!moo->c) if (!moo->c)
{ {
moo_cb_t cb, * cbp; moo_evtcb_t cb, * cbp;
MOO_MEMSET (&cb, 0, MOO_SIZEOF(cb)); MOO_MEMSET (&cb, 0, MOO_SIZEOF(cb));
cb.gc = gc_compiler; cb.gc = gc_compiler;
cb.fini = fini_compiler; cb.fini = fini_compiler;
cbp = moo_regcb (moo, &cb); cbp = moo_regevtcb (moo, &cb);
if (!cbp) return -1; if (!cbp) return -1;
moo->c = moo_callocmem (moo, MOO_SIZEOF(*moo->c)); moo->c = moo_callocmem (moo, MOO_SIZEOF(*moo->c));
if (!moo->c) if (!moo->c)
{ {
moo_deregcb (moo, cbp); moo_deregevtcb (moo, cbp);
return -1; return -1;
} }

View File

@ -232,6 +232,120 @@ void moo_seterrwithsyserr (moo_t* moo, int syserr_type, int syserr_code)
} }
} }
void moo_seterrbfmtwithsyserr (moo_t* moo, int syserr_type, int syserr_code, const moo_bch_t* fmt, ...)
{
moo_errnum_t errnum;
moo_oow_t ucslen, bcslen;
va_list ap;
if (moo->shuterr) return;
if (moo->vmprim.syserrstrb)
{
errnum = moo->vmprim.syserrstrb(moo, syserr_type, syserr_code, moo->errmsg.tmpbuf.bch, MOO_COUNTOF(moo->errmsg.tmpbuf.bch));
va_start (ap, fmt);
moo_seterrbfmtv (moo, errnum, fmt, ap);
va_end (ap);
if (MOO_COUNTOF(moo->errmsg.buf) - moo->errmsg.len >= 5)
{
moo->errmsg.buf[moo->errmsg.len++] = ' ';
moo->errmsg.buf[moo->errmsg.len++] = '-';
moo->errmsg.buf[moo->errmsg.len++] = ' ';
#if defined(MOO_OOCH_IS_BCH)
moo->errmsg.len += moo_copy_bcstr(moo->errmsg.buf[moo->errmsg.len], MOO_COUNTOF(moo->errmsg.buf) - moo->errmsg.len, moo->errmsg.tmpbuf.bch);
#else
ucslen = MOO_COUNTOF(moo->errmsg.buf) - moo->errmsg.len;
moo_convbtoucstr (moo, moo->errmsg.tmpbuf.bch, &bcslen, &moo->errmsg.buf[moo->errmsg.len], &ucslen);
moo->errmsg.len += ucslen;
#endif
}
}
else
{
MOO_ASSERT (moo, moo->vmprim.syserrstru != MOO_NULL);
errnum = moo->vmprim.syserrstru(moo, syserr_type, syserr_code, moo->errmsg.tmpbuf.uch, MOO_COUNTOF(moo->errmsg.tmpbuf.uch));
va_start (ap, fmt);
moo_seterrbfmtv (moo, errnum, fmt, ap);
va_end (ap);
if (MOO_COUNTOF(moo->errmsg.buf) - moo->errmsg.len >= 5)
{
moo->errmsg.buf[moo->errmsg.len++] = ' ';
moo->errmsg.buf[moo->errmsg.len++] = '-';
moo->errmsg.buf[moo->errmsg.len++] = ' ';
#if defined(MOO_OOCH_IS_BCH)
bcslen = MOO_COUNTOF(moo->errmsg.buf) - moo->errmsg.len;
moo_convutobcstr (moo, moo->errmsg.tmpbuf.uch, &ucslen, &moo->errmsg.buf[moo->errmsg.len], &bcslen);
moo->errmsg.len += bcslen;
#else
moo->errmsg.len += moo_copy_ucstr(moo->errmsg.buf[moo->errmsg.len], MOO_COUNTOF(moo->errmsg.buf) - moo->errmsg.len, moo->errmsg.tmpbuf.uch);
#endif
}
}
}
void moo_seterrufmtwithsyserr (moo_t* moo, int syserr_type, int syserr_code, const moo_uch_t* fmt, ...)
{
moo_errnum_t errnum;
moo_oow_t ucslen, bcslen;
va_list ap;
if (moo->shuterr) return;
if (moo->vmprim.syserrstrb)
{
errnum = moo->vmprim.syserrstrb(moo, syserr_type, syserr_code, moo->errmsg.tmpbuf.bch, MOO_COUNTOF(moo->errmsg.tmpbuf.bch));
va_start (ap, fmt);
moo_seterrufmtv (moo, errnum, fmt, ap);
va_end (ap);
if (MOO_COUNTOF(moo->errmsg.buf) - moo->errmsg.len >= 5)
{
moo->errmsg.buf[moo->errmsg.len++] = ' ';
moo->errmsg.buf[moo->errmsg.len++] = '-';
moo->errmsg.buf[moo->errmsg.len++] = ' ';
#if defined(MOO_OOCH_IS_BCH)
moo->errmsg.len += moo_copy_bcstr(moo->errmsg.buf[moo->errmsg.len], MOO_COUNTOF(moo->errmsg.buf) - moo->errmsg.len, moo->errmsg.tmpbuf.bch);
#else
ucslen = MOO_COUNTOF(moo->errmsg.buf) - moo->errmsg.len;
moo_convbtoucstr (moo, moo->errmsg.tmpbuf.bch, &bcslen, &moo->errmsg.buf[moo->errmsg.len], &ucslen);
moo->errmsg.len += ucslen;
#endif
}
}
else
{
MOO_ASSERT (moo, moo->vmprim.syserrstru != MOO_NULL);
errnum = moo->vmprim.syserrstru(moo, syserr_type, syserr_code, moo->errmsg.tmpbuf.uch, MOO_COUNTOF(moo->errmsg.tmpbuf.uch));
va_start (ap, fmt);
moo_seterrufmtv (moo, errnum, fmt, ap);
va_end (ap);
if (MOO_COUNTOF(moo->errmsg.buf) - moo->errmsg.len >= 5)
{
moo->errmsg.buf[moo->errmsg.len++] = ' ';
moo->errmsg.buf[moo->errmsg.len++] = '-';
moo->errmsg.buf[moo->errmsg.len++] = ' ';
#if defined(MOO_OOCH_IS_BCH)
bcslen = MOO_COUNTOF(moo->errmsg.buf) - moo->errmsg.len;
moo_convutobcstr (moo, moo->errmsg.tmpbuf.uch, &ucslen, &moo->errmsg.buf[moo->errmsg.len], &bcslen);
moo->errmsg.len += bcslen;
#else
moo->errmsg.len += moo_copy_ucstr(moo->errmsg.buf[moo->errmsg.len], MOO_COUNTOF(moo->errmsg.buf) - moo->errmsg.len, moo->errmsg.tmpbuf.uch);
#endif
}
}
}
#if defined(MOO_INCLUDE_COMPILER) #if defined(MOO_INCLUDE_COMPILER)
void moo_setsynerrbfmt (moo_t* moo, moo_synerrnum_t num, const moo_ioloc_t* loc, const moo_oocs_t* tgt, const moo_bch_t* msgfmt, ...) void moo_setsynerrbfmt (moo_t* moo, moo_synerrnum_t num, const moo_ioloc_t* loc, const moo_oocs_t* tgt, const moo_bch_t* msgfmt, ...)

View File

@ -860,7 +860,7 @@ void moo_gc (moo_t* moo)
moo_heap_t* tmp; moo_heap_t* tmp;
moo_oop_t old_nil; moo_oop_t old_nil;
moo_oow_t i; moo_oow_t i;
moo_cb_t* cb; moo_evtcb_t* cb;
moo_oow_t gcfin_count; moo_oow_t gcfin_count;
if (moo->active_context) if (moo->active_context)
@ -944,7 +944,7 @@ void moo_gc (moo_t* moo)
moo_rbt_walk (&moo->modtab, call_module_gc, moo); moo_rbt_walk (&moo->modtab, call_module_gc, moo);
for (cb = moo->cblist; cb; cb = cb->next) for (cb = moo->evtcb_list; cb; cb = cb->next)
{ {
if (cb->gc) cb->gc (moo); if (cb->gc) cb->gc (moo);
} }

View File

@ -406,7 +406,7 @@ int main (int argc, char* argv[])
static moo_ooch_t str_main[] = { 'm', 'a', 'i', 'n' }; static moo_ooch_t str_main[] = { 'm', 'a', 'i', 'n' };
moo_t* moo; moo_t* moo;
moo_cfg_t cfg; moo_stdcfg_t cfg;
moo_errinf_t errinf; moo_errinf_t errinf;
moo_oocs_t objname; moo_oocs_t objname;
@ -542,12 +542,23 @@ int main (int argc, char* argv[])
for (i = opt.ind; i < argc; i++) for (i = opt.ind; i < argc; i++)
{ {
moo_iostd_t instd; moo_iostd_t in;
instd.type = MOO_IOSTD_FILEB;
instd.u.fileb.path = argv[i]; #if 0
in.type = MOO_IOSTD_FILEB;
in.u.fileb.path = argv[i];
#else
moo_uch_t tmp[1000];
moo_oow_t bcslen, ucslen;
ucslen = MOO_COUNTOF(tmp);
moo_conv_utf8_to_ucstr(argv[i], &bcslen, tmp, &ucslen);
in.type = MOO_IOSTD_FILEU;
in.u.fileu.path = tmp;
#endif
/*compile:*/ /*compile:*/
if (moo_compilestd(moo, &instd, 1) <= -1) if (moo_compilestd(moo, &in, 1) <= -1)
{ {
if (moo->errnum == MOO_ESYNERR) if (moo->errnum == MOO_ESYNERR)
{ {

View File

@ -3,16 +3,16 @@
#include <moo.h> #include <moo.h>
enum moo_cfg_type_t enum moo_stdcfg_type_t
{ {
MOO_CFG_TYPE_B, moo_stdcfg_tYPE_B,
MOO_CFG_TYPE_U moo_stdcfg_tYPE_U
}; };
typedef enum moo_cfg_type_t moo_cfg_type_t; typedef enum moo_stdcfg_type_t moo_stdcfg_type_t;
struct moo_cfg_t struct moo_stdcfg_t
{ {
moo_cfg_type_t type; moo_stdcfg_type_t type;
moo_oow_t memsize; moo_oow_t memsize;
int large_pages; int large_pages;
@ -20,7 +20,7 @@ struct moo_cfg_t
const void* logopt; const void* logopt;
const void* dbgopt; const void* dbgopt;
}; };
typedef struct moo_cfg_t moo_cfg_t; typedef struct moo_stdcfg_t moo_stdcfg_t;
enum moo_iostd_type_t enum moo_iostd_type_t
@ -60,7 +60,7 @@ extern "C" {
MOO_EXPORT moo_t* moo_openstd ( MOO_EXPORT moo_t* moo_openstd (
moo_oow_t xtnsize, moo_oow_t xtnsize,
const moo_cfg_t* cfg, const moo_stdcfg_t* cfg,
moo_errinf_t* errinfo moo_errinf_t* errinfo
); );
@ -70,7 +70,7 @@ MOO_EXPORT void* moo_getxtnstd (
MOO_EXPORT int moo_compilestd( MOO_EXPORT int moo_compilestd(
moo_t* moo, moo_t* moo,
const moo_iostd_t* instd, const moo_iostd_t* in,
moo_oow_t count moo_oow_t count
); );
#if defined(__cplusplus) #if defined(__cplusplus)

View File

@ -183,7 +183,7 @@ static moo_rbt_walk_t unload_module (moo_rbt_t* rbt, moo_rbt_pair_t* pair, void*
void moo_fini (moo_t* moo) void moo_fini (moo_t* moo)
{ {
moo_cb_t* cb; moo_evtcb_t* cb;
moo_oow_t i; moo_oow_t i;
moo_rbt_walk (&moo->modtab, unload_module, moo); /* unload all modules */ moo_rbt_walk (&moo->modtab, unload_module, moo); /* unload all modules */
@ -196,7 +196,7 @@ void moo_fini (moo_t* moo)
vmprim_log_write (moo, moo->log.last_mask, moo->log.ptr, moo->log.len); vmprim_log_write (moo, moo->log.last_mask, moo->log.ptr, moo->log.len);
} }
for (cb = moo->cblist; cb; cb = cb->next) for (cb = moo->evtcb_list; cb; cb = cb->next)
{ {
/* execute callbacks for finalization */ /* execute callbacks for finalization */
if (cb->fini) cb->fini (moo); if (cb->fini) cb->fini (moo);
@ -213,7 +213,7 @@ void moo_fini (moo_t* moo)
} }
/* deregister all callbacks */ /* deregister all callbacks */
while (moo->cblist) moo_deregcb (moo, moo->cblist); while (moo->evtcb_list) moo_deregevtcb (moo, moo->evtcb_list);
/* free up internal data structures. this is done after all callbacks /* free up internal data structures. this is done after all callbacks
* are completed */ * are completed */
@ -393,27 +393,27 @@ int moo_getoption (moo_t* moo, moo_option_t id, void* value)
return -1; return -1;
} }
moo_cb_t* moo_regcb (moo_t* moo, moo_cb_t* tmpl) moo_evtcb_t* moo_regevtcb (moo_t* moo, moo_evtcb_t* tmpl)
{ {
moo_cb_t* actual; moo_evtcb_t* actual;
actual = (moo_cb_t*)moo_allocmem(moo, MOO_SIZEOF(*actual)); actual = (moo_evtcb_t*)moo_allocmem(moo, MOO_SIZEOF(*actual));
if (!actual) return MOO_NULL; if (!actual) return MOO_NULL;
*actual = *tmpl; *actual = *tmpl;
actual->next = moo->cblist; actual->next = moo->evtcb_list;
actual->prev = MOO_NULL; actual->prev = MOO_NULL;
moo->cblist = actual; moo->evtcb_list = actual;
return actual; return actual;
} }
void moo_deregcb (moo_t* moo, moo_cb_t* cb) void moo_deregevtcb (moo_t* moo, moo_evtcb_t* cb)
{ {
if (cb == moo->cblist) if (cb == moo->evtcb_list)
{ {
moo->cblist = moo->cblist->next; moo->evtcb_list = moo->evtcb_list->next;
if (moo->cblist) moo->cblist->prev = MOO_NULL; if (moo->evtcb_list) moo->evtcb_list->prev = MOO_NULL;
} }
else else
{ {

View File

@ -1202,15 +1202,15 @@ typedef struct moo_vmprim_t moo_vmprim_t;
* ========================================================================= */ * ========================================================================= */
typedef void (*moo_cbimpl_t) (moo_t* moo); typedef void (*moo_cbimpl_t) (moo_t* moo);
typedef struct moo_cb_t moo_cb_t; typedef struct moo_evtcb_t moo_evtcb_t;
struct moo_cb_t struct moo_evtcb_t
{ {
moo_cbimpl_t gc; moo_cbimpl_t gc;
moo_cbimpl_t fini; moo_cbimpl_t fini;
/* private below */ /* private below */
moo_cb_t* prev; moo_evtcb_t* prev;
moo_cb_t* next; moo_evtcb_t* next;
}; };
@ -1363,6 +1363,7 @@ typedef struct moo_compiler_t moo_compiler_t;
#endif #endif
#define MOO_ERRMSG_CAPA (2048) #define MOO_ERRMSG_CAPA (2048)
#define MOO_IOBUF_CAPA (2048)
enum moo_sbuf_id_t enum moo_sbuf_id_t
{ {
@ -1415,7 +1416,7 @@ struct moo_t
moo_vmprim_t vmprim; moo_vmprim_t vmprim;
moo_cb_t* cblist; moo_evtcb_t* evtcb_list;
moo_rbt_t modtab; /* primitive module table */ moo_rbt_t modtab; /* primitive module table */
struct struct
@ -1706,7 +1707,7 @@ struct moo_ioarg_t
/** /**
* [OUT] place data here * [OUT] place data here
*/ */
moo_ooch_t buf[2048]; moo_ooch_t buf[MOO_IOBUF_CAPA];
/** /**
* [IN] points to the data of the includer. It is #MOO_NULL for the * [IN] points to the data of the includer. It is #MOO_NULL for the
@ -1895,6 +1896,22 @@ MOO_EXPORT void moo_seterrufmt (
... ...
); );
MOO_EXPORT void moo_seterrbfmtwithsyserr (
moo_t* moo,
int syserr_type,
int syserr_code,
const moo_bch_t* fmt,
...
);
MOO_EXPORT void moo_seterrufmtwithsyserr (
moo_t* moo,
int syserr_type,
int syserr_code,
const moo_uch_t* fmt,
...
);
MOO_EXPORT const moo_ooch_t* moo_geterrstr ( MOO_EXPORT const moo_ooch_t* moo_geterrstr (
moo_t* moo moo_t* moo
); );
@ -1937,14 +1954,14 @@ MOO_EXPORT int moo_setoption (
); );
MOO_EXPORT moo_cb_t* moo_regcb ( MOO_EXPORT moo_evtcb_t* moo_regevtcb (
moo_t* moo, moo_t* moo,
moo_cb_t* tmpl moo_evtcb_t* tmpl
); );
MOO_EXPORT void moo_deregcb ( MOO_EXPORT void moo_deregevtcb (
moo_t* moo, moo_t* moo,
moo_cb_t* cb moo_evtcb_t* cb
); );
/** /**

View File

@ -235,7 +235,7 @@
typedef struct bb_t bb_t; typedef struct bb_t bb_t;
struct bb_t struct bb_t
{ {
char buf[1024]; char buf[MOO_IOBUF_CAPA];
moo_oow_t pos; moo_oow_t pos;
moo_oow_t len; moo_oow_t len;
@ -251,10 +251,10 @@ struct select_fd_t
}; };
#endif #endif
enum logfd_trait_t enum logfd_flag_t
{ {
LOGFD_TTY = (1 << 0), LOGFD_TTY = (1 << 0),
LOGFD_OPENED = (1 << 1) LOGFD_OPENED_HERE = (1 << 1)
}; };
typedef struct xtn_t xtn_t; typedef struct xtn_t xtn_t;
@ -267,7 +267,7 @@ struct xtn_t
moo_bitmask_t logmask; moo_bitmask_t logmask;
int logfd; int logfd;
int logfd_trait; /* bitwise OR'ed fo logfd_trait_t bits */ int logfd_flag; /* bitwise OR'ed fo logfd_flag_t bits */
struct struct
{ {
@ -275,8 +275,7 @@ struct xtn_t
moo_oow_t len; moo_oow_t len;
} logbuf; } logbuf;
const moo_iostd_t* in;
const moo_iostd_t* instd;
#if defined(_WIN32) #if defined(_WIN32)
HANDLE waitable_timer; HANDLE waitable_timer;
@ -439,32 +438,50 @@ static MOO_INLINE moo_ooi_t open_input (moo_t* moo, moo_ioarg_t* arg)
else else
{ {
/* main stream */ /* main stream */
moo_oow_t pathlen; moo_oow_t ucslen, bcslen;
switch (xtn->instd->type) switch (xtn->in->type)
{ {
//case MOO_IOSTD_FILE: // TODO: #if defined(MOO_OOCH_IS_BCH)
case MOO_IOSTD_FILE:
MOO_ASSERT (moo, &xtn->in->u.fileb.path == &xtn->in->u.file.path);
#endif
case MOO_IOSTD_FILEB: case MOO_IOSTD_FILEB:
pathlen = moo_count_bcstr(xtn->instd->u.fileb.path); bcslen = moo_count_bcstr(xtn->in->u.fileb.path);
bb = moo_callocmem(moo, MOO_SIZEOF(*bb) + (MOO_SIZEOF(moo_bch_t) * (pathlen + 1))); bb = moo_callocmem(moo, MOO_SIZEOF(*bb) + (MOO_SIZEOF(moo_bch_t) * (bcslen + 1)));
if (!bb) goto oops; if (!bb) goto oops;
bb->fn = (moo_bch_t*)(bb + 1); bb->fn = (moo_bch_t*)(bb + 1);
moo_copy_bcstr (bb->fn, pathlen + 1, xtn->instd->u.fileb.path); moo_copy_bcstr (bb->fn, bcslen + 1, xtn->in->u.fileb.path);
break; break;
#if defined(MOO_OOCH_IS_UCH)
case MOO_IOSTD_FILE: case MOO_IOSTD_FILE:
MOO_ASSERT (moo, &xtn->in->u.fileu.path == &xtn->in->u.file.path);
#endif
case MOO_IOSTD_FILEU: case MOO_IOSTD_FILEU:
moo_seterrbfmt (moo, MOO_ENOIMPL, "unimplemented standard input type fileu\n"); if (moo_convutobcstr(moo, xtn->in->u.fileu.path, &ucslen, MOO_NULL, &bcslen) <= -1)
{
moo_seterrbfmt (moo, moo_geterrnum(moo), "unable to convert encoding of path %ls", xtn->in->u.fileu.path);
goto oops; goto oops;
}
bb = moo_callocmem(moo, MOO_SIZEOF(*bb) + (MOO_SIZEOF(moo_bch_t) * (bcslen + 1)));
if (!bb) goto oops;
bb->fn = (moo_bch_t*)(bb + 1);
bcslen += 1;
moo_convutobcstr (moo, xtn->in->u.fileu.path, &ucslen, bb->fn, &bcslen);
break;
default: default:
moo_seterrbfmt (moo, MOO_EINVAL, "unsupported standard input type - %d", (int)xtn->instd->type); moo_seterrbfmt (moo, MOO_EINVAL, "unsupported standard input type - %d", (int)xtn->in->type);
goto oops; goto oops;
} }
} }
/* TODO: support _wfopen or the like */
#if defined(_WIN32) || defined(__OS2__) || defined(__DOS__) #if defined(_WIN32) || defined(__OS2__) || defined(__DOS__)
bb->fp = fopen(bb->fn, "rb"); bb->fp = fopen(bb->fn, "rb");
#else #else
@ -472,7 +489,7 @@ static MOO_INLINE moo_ooi_t open_input (moo_t* moo, moo_ioarg_t* arg)
#endif #endif
if (!bb->fp) if (!bb->fp)
{ {
moo_seterrbfmt (moo, MOO_EIOERR, "unable to open file %hs", bb->fn); moo_seterrbfmtwithsyserr (moo, 0, errno, "unable to open file %hs", bb->fn);
goto oops; goto oops;
} }
@ -490,7 +507,6 @@ oops:
static MOO_INLINE moo_ooi_t close_input (moo_t* moo, moo_ioarg_t* arg) static MOO_INLINE moo_ooi_t close_input (moo_t* moo, moo_ioarg_t* arg)
{ {
/*xtn_t* xtn = GET_XTN(moo);*/
bb_t* bb; bb_t* bb;
bb = (bb_t*)arg->handle; bb = (bb_t*)arg->handle;
@ -505,7 +521,6 @@ static MOO_INLINE moo_ooi_t close_input (moo_t* moo, moo_ioarg_t* arg)
static MOO_INLINE moo_ooi_t read_input (moo_t* moo, moo_ioarg_t* arg) static MOO_INLINE moo_ooi_t read_input (moo_t* moo, moo_ioarg_t* arg)
{ {
/*xtn_t* xtn = GET_XTN(moo);*/
bb_t* bb; bb_t* bb;
moo_oow_t bcslen, ucslen, remlen; moo_oow_t bcslen, ucslen, remlen;
int x; int x;
@ -519,7 +534,7 @@ static MOO_INLINE moo_ooi_t read_input (moo_t* moo, moo_ioarg_t* arg)
{ {
if (ferror((FILE*)bb->fp)) if (ferror((FILE*)bb->fp))
{ {
moo_seterrnum (moo, MOO_EIOERR); moo_seterrbfmtwithsyserr (moo, 0, errno, "unable to read file %hs", bb->fn);
return -1; return -1;
} }
break; break;
@ -801,7 +816,7 @@ static void log_write (moo_t* moo, moo_bitmask_t mask, const moo_ooch_t* msg, mo
write_log (moo, logfd, ts, tslen); write_log (moo, logfd, ts, tslen);
} }
if (logfd == xtn->logfd && (xtn->logfd_trait & LOGFD_TTY)) if (logfd == xtn->logfd && (xtn->logfd_flag & LOGFD_TTY))
{ {
if (mask & MOO_LOG_FATAL) write_log (moo, logfd, "\x1B[1;31m", 7); if (mask & MOO_LOG_FATAL) write_log (moo, logfd, "\x1B[1;31m", 7);
else if (mask & MOO_LOG_ERROR) write_log (moo, logfd, "\x1B[1;32m", 7); else if (mask & MOO_LOG_ERROR) write_log (moo, logfd, "\x1B[1;32m", 7);
@ -846,7 +861,7 @@ static void log_write (moo_t* moo, moo_bitmask_t mask, const moo_ooch_t* msg, mo
write_log (moo, logfd, msg, len); write_log (moo, logfd, msg, len);
#endif #endif
if (logfd == xtn->logfd && (xtn->logfd_trait & LOGFD_TTY)) if (logfd == xtn->logfd && (xtn->logfd_flag & LOGFD_TTY))
{ {
if (mask & (MOO_LOG_FATAL | MOO_LOG_ERROR | MOO_LOG_WARN)) write_log (moo, logfd, "\x1B[0m", 4); if (mask & (MOO_LOG_FATAL | MOO_LOG_ERROR | MOO_LOG_WARN)) write_log (moo, logfd, "\x1B[0m", 4);
} }
@ -1370,13 +1385,13 @@ static void* dl_open (moo_t* moo, const moo_ooch_t* name, int flags)
} }
else else
{ {
MOO_DEBUG3 (moo, "Opened(ext) PFMOD %hs[%js] handle %p\n", &bufptr[len], name, handle); MOO_DEBUG3 (moo, "OPENED_HERE(ext) PFMOD %hs[%js] handle %p\n", &bufptr[len], name, handle);
} }
} }
else else
{ {
pfmod_open_ok: pfmod_open_ok:
MOO_DEBUG3 (moo, "Opened(ext) PFMOD %hs[%js] handle %p\n", &bufptr[dlen], name, handle); MOO_DEBUG3 (moo, "OPENED_HERE(ext) PFMOD %hs[%js] handle %p\n", &bufptr[dlen], name, handle);
} }
} }
else else
@ -1399,7 +1414,7 @@ static void* dl_open (moo_t* moo, const moo_ooch_t* name, int flags)
MOO_DEBUG2 (moo, "Unable to open DL %hs - %hs\n", bufptr, dl_errstr); MOO_DEBUG2 (moo, "Unable to open DL %hs - %hs\n", bufptr, dl_errstr);
moo_seterrbfmt (moo, MOO_ESYSERR, "unable to open DL %js - %hs", name, dl_errstr); moo_seterrbfmt (moo, MOO_ESYSERR, "unable to open DL %js - %hs", name, dl_errstr);
} }
else MOO_DEBUG2 (moo, "Opened DL %hs handle %p\n", bufptr, handle); else MOO_DEBUG2 (moo, "OPENED_HERE DL %hs handle %p\n", bufptr, handle);
} }
else else
{ {
@ -1411,7 +1426,7 @@ static void* dl_open (moo_t* moo, const moo_ooch_t* name, int flags)
MOO_DEBUG2 (moo, "Unable to open(ext) DL %hs - %hs\n", bufptr, dl_errstr); MOO_DEBUG2 (moo, "Unable to open(ext) DL %hs - %hs\n", bufptr, dl_errstr);
moo_seterrbfmt (moo, MOO_ESYSERR, "unable to open(ext) DL %js - %hs", name, dl_errstr); moo_seterrbfmt (moo, MOO_ESYSERR, "unable to open(ext) DL %js - %hs", name, dl_errstr);
} }
else MOO_DEBUG2 (moo, "Opened(ext) DL %hs handle %p\n", bufptr, handle); else MOO_DEBUG2 (moo, "OPENED_HERE(ext) DL %hs handle %p\n", bufptr, handle);
} }
} }
@ -2874,9 +2889,9 @@ static int handle_logopt (moo_t* moo, const moo_bch_t* str)
} }
xtn->logmask = logmask; xtn->logmask = logmask;
xtn->logfd_trait |= LOGFD_OPENED; xtn->logfd_flag |= LOGFD_OPENED_HERE;
#if defined(HAVE_ISATTY) #if defined(HAVE_ISATTY)
if (isatty(xtn->logfd)) xtn->logfd_trait |= LOGFD_TTY; if (isatty(xtn->logfd)) xtn->logfd_flag |= LOGFD_TTY;
#endif #endif
if (str != xstr) moo_freemem (moo, xstr); if (str != xstr) moo_freemem (moo, xstr);
@ -2916,35 +2931,59 @@ static int handle_dbgopt (moo_t* moo, const moo_bch_t* str)
/* ========================================================================= */ /* ========================================================================= */
static void fini_moo (moo_t* moo) static MOO_INLINE void reset_log_to_default (xtn_t* xtn)
{
#if defined(ENABLE_LOG_INITIALLY)
xtn->logfd = 2;
xtn->logfd_flag = 0;
#if defined(HAVE_ISATTY)
if (isatty(xtn->logfd)) xtn->logfd_flag |= LOGFD_TTY;
#endif
xtn->logmask = MOO_LOG_ALL_LEVELS | MOO_LOG_ALL_TYPES;
#else
xtn->logfd = -1;
xtn->logfd_flag = 0;
xtn->logmask = 0;
#endif
}
static MOO_INLINE void chain (moo_t* moo)
{ {
xtn_t* xtn = GET_XTN(moo); xtn_t* xtn = GET_XTN(moo);
if ((xtn->logfd_trait & LOGFD_OPENED) && xtn->logfd >= 0)
{
close (xtn->logfd);
/* back to the default */ /* TODO: make this atomic */
xtn->logfd = -2; if (g_moo) GET_XTN(g_moo)->prev = moo;
xtn->logfd_trait = 0; else g_moo = moo;
#if defined(HAVE_ISATTY) xtn->next = g_moo;
if (isatty(xtn->logfd)) xtn->logfd_trait |= LOGFD_TTY; /* TODO: make this atomic */
#endif }
}
/* TODO: make this atomic */ static MOO_INLINE void unchain (moo_t* moo)
{
xtn_t* xtn = GET_XTN(moo);
/* TODO: make this atomic */
if (xtn->prev) GET_XTN(xtn->prev)->next = xtn->next; if (xtn->prev) GET_XTN(xtn->prev)->next = xtn->next;
else g_moo = xtn->next; else g_moo = xtn->next;
if (xtn->next) GET_XTN(xtn->next)->prev = xtn->prev; if (xtn->next) GET_XTN(xtn->next)->prev = xtn->prev;
/* TODO: make this atomic */ /* TODO: make this atomic */
xtn->prev = MOO_NULL; xtn->prev = MOO_NULL;
xtn->prev = MOO_NULL; xtn->prev = MOO_NULL;
} }
moo_t* moo_openstd (moo_oow_t xtnsize, const moo_cfg_t* cfg, moo_errinf_t* errinfo) static void fini_moo (moo_t* moo)
{
xtn_t* xtn = GET_XTN(moo);
if ((xtn->logfd_flag & LOGFD_OPENED_HERE) && xtn->logfd >= 0) close (xtn->logfd);
reset_log_to_default (moo);
unchain (moo);
}
moo_t* moo_openstd (moo_oow_t xtnsize, const moo_stdcfg_t* cfg, moo_errinf_t* errinfo)
{ {
moo_t* moo; moo_t* moo;
moo_vmprim_t vmprim; moo_vmprim_t vmprim;
moo_cb_t cb; moo_evtcb_t evtcb;
xtn_t* xtn; xtn_t* xtn;
MOO_MEMSET(&vmprim, 0, MOO_SIZEOF(vmprim)); MOO_MEMSET(&vmprim, 0, MOO_SIZEOF(vmprim));
@ -2975,16 +3014,12 @@ moo_t* moo_openstd (moo_oow_t xtnsize, const moo_cfg_t* cfg, moo_errinf_t* errin
xtn = GET_XTN(moo); xtn = GET_XTN(moo);
/* initial log goes to stderr */ chain (moo); /* call chain() before moo_regevtcb() as fini_moo() calls unchain() */
xtn->logfd = -2; reset_log_to_default (xtn);
xtn->logfd_trait = 0;
#if defined(HAVE_ISATTY)
if (isatty(xtn->logfd)) xtn->logfd_trait |= LOGFD_TTY;
#endif
MOO_MEMSET (&cb, 0, MOO_SIZEOF(cb)); MOO_MEMSET (&evtcb, 0, MOO_SIZEOF(evtcb));
cb.fini = fini_moo; evtcb.fini = fini_moo;
moo_regcb (moo, &cb); moo_regevtcb (moo, &evtcb);
/* TODO: cfg->logopt, dbgopt => bch uch differentation */ /* TODO: cfg->logopt, dbgopt => bch uch differentation */
if ((cfg->logopt && handle_logopt(moo, cfg->logopt) <= -1) || if ((cfg->logopt && handle_logopt(moo, cfg->logopt) <= -1) ||
@ -2995,12 +3030,6 @@ moo_t* moo_openstd (moo_oow_t xtnsize, const moo_cfg_t* cfg, moo_errinf_t* errin
return MOO_NULL; return MOO_NULL;
} }
/* TODO: make this atomic */
if (g_moo) GET_XTN(g_moo)->prev = moo;
else g_moo = moo;
xtn->next = g_moo;
/* TODO: make this atomic */
return moo; return moo;
} }
@ -3009,7 +3038,7 @@ void* moo_getxtnstd (moo_t* moo)
return (void*)((moo_uint8_t*)GET_XTN(moo) + MOO_SIZEOF(xtn_t)); return (void*)((moo_uint8_t*)GET_XTN(moo) + MOO_SIZEOF(xtn_t));
} }
int moo_compilestd(moo_t* moo, const moo_iostd_t* instd, moo_oow_t count) int moo_compilestd(moo_t* moo, const moo_iostd_t* in, moo_oow_t count)
{ {
xtn_t* xtn; xtn_t* xtn;
moo_oow_t i; moo_oow_t i;
@ -3017,7 +3046,7 @@ int moo_compilestd(moo_t* moo, const moo_iostd_t* instd, moo_oow_t count)
xtn = GET_XTN(moo); xtn = GET_XTN(moo);
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
{ {
xtn->instd = &instd[i]; xtn->in = &in[i];
if (moo_compile(moo, input_handler) <= -1) return -1; if (moo_compile(moo, input_handler) <= -1) return -1;
} }