From 6939433ff03c40bd936177cf56d1841f1bcf5b3d Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Tue, 13 Nov 2018 06:54:30 +0000 Subject: [PATCH] added moo_seterrbfmtwithsyserr() and moo_seterrufmtwithsyserr() --- moo/lib/comp.c | 6 +- moo/lib/err.c | 114 +++++++++++++++++++++++++++++++++++ moo/lib/gc.c | 4 +- moo/lib/main.c | 21 +++++-- moo/lib/moo-std.h | 22 +++---- moo/lib/moo-utl.h | 2 +- moo/lib/moo.c | 24 ++++---- moo/lib/moo.h | 37 ++++++++---- moo/lib/std.c | 147 +++++++++++++++++++++++++++------------------- 9 files changed, 274 insertions(+), 103 deletions(-) diff --git a/moo/lib/comp.c b/moo/lib/comp.c index 753420c..9e6983c 100644 --- a/moo/lib/comp.c +++ b/moo/lib/comp.c @@ -9268,18 +9268,18 @@ static MOO_INLINE int _compile (moo_t* moo, moo_ioimpl_t io) if (!moo->c) { - moo_cb_t cb, * cbp; + moo_evtcb_t cb, * cbp; MOO_MEMSET (&cb, 0, MOO_SIZEOF(cb)); cb.gc = gc_compiler; cb.fini = fini_compiler; - cbp = moo_regcb (moo, &cb); + cbp = moo_regevtcb (moo, &cb); if (!cbp) return -1; moo->c = moo_callocmem (moo, MOO_SIZEOF(*moo->c)); if (!moo->c) { - moo_deregcb (moo, cbp); + moo_deregevtcb (moo, cbp); return -1; } diff --git a/moo/lib/err.c b/moo/lib/err.c index 6ff8168..912b975 100644 --- a/moo/lib/err.c +++ b/moo/lib/err.c @@ -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) 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, ...) diff --git a/moo/lib/gc.c b/moo/lib/gc.c index 5934acd..107b964 100644 --- a/moo/lib/gc.c +++ b/moo/lib/gc.c @@ -860,7 +860,7 @@ void moo_gc (moo_t* moo) moo_heap_t* tmp; moo_oop_t old_nil; moo_oow_t i; - moo_cb_t* cb; + moo_evtcb_t* cb; moo_oow_t gcfin_count; if (moo->active_context) @@ -944,7 +944,7 @@ void moo_gc (moo_t* 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); } diff --git a/moo/lib/main.c b/moo/lib/main.c index 5595093..bb6a586 100644 --- a/moo/lib/main.c +++ b/moo/lib/main.c @@ -406,7 +406,7 @@ int main (int argc, char* argv[]) static moo_ooch_t str_main[] = { 'm', 'a', 'i', 'n' }; moo_t* moo; - moo_cfg_t cfg; + moo_stdcfg_t cfg; moo_errinf_t errinf; moo_oocs_t objname; @@ -542,12 +542,23 @@ int main (int argc, char* argv[]) for (i = opt.ind; i < argc; i++) { - moo_iostd_t instd; - instd.type = MOO_IOSTD_FILEB; - instd.u.fileb.path = argv[i]; + moo_iostd_t in; + +#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:*/ - if (moo_compilestd(moo, &instd, 1) <= -1) + if (moo_compilestd(moo, &in, 1) <= -1) { if (moo->errnum == MOO_ESYNERR) { diff --git a/moo/lib/moo-std.h b/moo/lib/moo-std.h index 09e5707..90f62ce 100644 --- a/moo/lib/moo-std.h +++ b/moo/lib/moo-std.h @@ -3,16 +3,16 @@ #include -enum moo_cfg_type_t +enum moo_stdcfg_type_t { - MOO_CFG_TYPE_B, - MOO_CFG_TYPE_U + moo_stdcfg_tYPE_B, + 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; int large_pages; @@ -20,7 +20,7 @@ struct moo_cfg_t const void* logopt; const void* dbgopt; }; -typedef struct moo_cfg_t moo_cfg_t; +typedef struct moo_stdcfg_t moo_stdcfg_t; enum moo_iostd_type_t @@ -59,9 +59,9 @@ extern "C" { #endif MOO_EXPORT moo_t* moo_openstd ( - moo_oow_t xtnsize, - const moo_cfg_t* cfg, - moo_errinf_t* errinfo + moo_oow_t xtnsize, + const moo_stdcfg_t* cfg, + moo_errinf_t* errinfo ); MOO_EXPORT void* moo_getxtnstd ( @@ -70,7 +70,7 @@ MOO_EXPORT void* moo_getxtnstd ( MOO_EXPORT int moo_compilestd( moo_t* moo, - const moo_iostd_t* instd, + const moo_iostd_t* in, moo_oow_t count ); #if defined(__cplusplus) diff --git a/moo/lib/moo-utl.h b/moo/lib/moo-utl.h index edcf956..000e83f 100644 --- a/moo/lib/moo-utl.h +++ b/moo/lib/moo-utl.h @@ -394,7 +394,7 @@ MOO_EXPORT int moo_conv_ucs_to_bcs_with_cmgr ( moo_bch_t* bcs, moo_oow_t* bcslen, moo_cmgr_t* cmgr -); +); MOO_EXPORT int moo_conv_uchars_to_bchars_with_cmgr ( const moo_uch_t* ucs, diff --git a/moo/lib/moo.c b/moo/lib/moo.c index 2103209..ecf1912 100644 --- a/moo/lib/moo.c +++ b/moo/lib/moo.c @@ -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) { - moo_cb_t* cb; + moo_evtcb_t* cb; moo_oow_t i; 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); } - for (cb = moo->cblist; cb; cb = cb->next) + for (cb = moo->evtcb_list; cb; cb = cb->next) { /* execute callbacks for finalization */ if (cb->fini) cb->fini (moo); @@ -213,7 +213,7 @@ void moo_fini (moo_t* moo) } /* 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 * are completed */ @@ -393,27 +393,27 @@ int moo_getoption (moo_t* moo, moo_option_t id, void* value) 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; *actual = *tmpl; - actual->next = moo->cblist; + actual->next = moo->evtcb_list; actual->prev = MOO_NULL; - moo->cblist = actual; + moo->evtcb_list = 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; - if (moo->cblist) moo->cblist->prev = MOO_NULL; + moo->evtcb_list = moo->evtcb_list->next; + if (moo->evtcb_list) moo->evtcb_list->prev = MOO_NULL; } else { diff --git a/moo/lib/moo.h b/moo/lib/moo.h index 570fa06..0e82aa5 100644 --- a/moo/lib/moo.h +++ b/moo/lib/moo.h @@ -1202,15 +1202,15 @@ typedef struct moo_vmprim_t moo_vmprim_t; * ========================================================================= */ typedef void (*moo_cbimpl_t) (moo_t* moo); -typedef struct moo_cb_t moo_cb_t; -struct moo_cb_t +typedef struct moo_evtcb_t moo_evtcb_t; +struct moo_evtcb_t { moo_cbimpl_t gc; moo_cbimpl_t fini; /* private below */ - moo_cb_t* prev; - moo_cb_t* next; + moo_evtcb_t* prev; + moo_evtcb_t* next; }; @@ -1363,6 +1363,7 @@ typedef struct moo_compiler_t moo_compiler_t; #endif #define MOO_ERRMSG_CAPA (2048) +#define MOO_IOBUF_CAPA (2048) enum moo_sbuf_id_t { @@ -1415,7 +1416,7 @@ struct moo_t moo_vmprim_t vmprim; - moo_cb_t* cblist; + moo_evtcb_t* evtcb_list; moo_rbt_t modtab; /* primitive module table */ struct @@ -1706,7 +1707,7 @@ struct moo_ioarg_t /** * [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 @@ -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_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_cb_t* tmpl + moo_evtcb_t* tmpl ); -MOO_EXPORT void moo_deregcb ( +MOO_EXPORT void moo_deregevtcb ( moo_t* moo, - moo_cb_t* cb + moo_evtcb_t* cb ); /** diff --git a/moo/lib/std.c b/moo/lib/std.c index 2bd4951..20a0cab 100644 --- a/moo/lib/std.c +++ b/moo/lib/std.c @@ -235,7 +235,7 @@ typedef struct bb_t bb_t; struct bb_t { - char buf[1024]; + char buf[MOO_IOBUF_CAPA]; moo_oow_t pos; moo_oow_t len; @@ -251,10 +251,10 @@ struct select_fd_t }; #endif -enum logfd_trait_t +enum logfd_flag_t { LOGFD_TTY = (1 << 0), - LOGFD_OPENED = (1 << 1) + LOGFD_OPENED_HERE = (1 << 1) }; typedef struct xtn_t xtn_t; @@ -267,7 +267,7 @@ struct xtn_t moo_bitmask_t logmask; 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 { @@ -275,8 +275,7 @@ struct xtn_t moo_oow_t len; } logbuf; - - const moo_iostd_t* instd; + const moo_iostd_t* in; #if defined(_WIN32) HANDLE waitable_timer; @@ -439,32 +438,50 @@ static MOO_INLINE moo_ooi_t open_input (moo_t* moo, moo_ioarg_t* arg) else { /* 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: - 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; 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; + #if defined(MOO_OOCH_IS_UCH) case MOO_IOSTD_FILE: + MOO_ASSERT (moo, &xtn->in->u.fileu.path == &xtn->in->u.file.path); + #endif case MOO_IOSTD_FILEU: - moo_seterrbfmt (moo, MOO_ENOIMPL, "unimplemented standard input type fileu\n"); - goto oops; + 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; + } + + 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: - 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; } } +/* TODO: support _wfopen or the like */ #if defined(_WIN32) || defined(__OS2__) || defined(__DOS__) bb->fp = fopen(bb->fn, "rb"); #else @@ -472,7 +489,7 @@ static MOO_INLINE moo_ooi_t open_input (moo_t* moo, moo_ioarg_t* arg) #endif 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; } @@ -490,7 +507,6 @@ oops: 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 = (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) { - /*xtn_t* xtn = GET_XTN(moo);*/ bb_t* bb; moo_oow_t bcslen, ucslen, remlen; 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)) { - moo_seterrnum (moo, MOO_EIOERR); + moo_seterrbfmtwithsyserr (moo, 0, errno, "unable to read file %hs", bb->fn); return -1; } 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); } - 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); 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); #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); } @@ -1370,13 +1385,13 @@ static void* dl_open (moo_t* moo, const moo_ooch_t* name, int flags) } 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 { 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 @@ -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_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 { @@ -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_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->logfd_trait |= LOGFD_OPENED; + xtn->logfd_flag |= LOGFD_OPENED_HERE; #if defined(HAVE_ISATTY) - if (isatty(xtn->logfd)) xtn->logfd_trait |= LOGFD_TTY; + if (isatty(xtn->logfd)) xtn->logfd_flag |= LOGFD_TTY; #endif 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); - if ((xtn->logfd_trait & LOGFD_OPENED) && xtn->logfd >= 0) - { - close (xtn->logfd); - /* back to the default */ - xtn->logfd = -2; - xtn->logfd_trait = 0; - #if defined(HAVE_ISATTY) - if (isatty(xtn->logfd)) xtn->logfd_trait |= LOGFD_TTY; - #endif - } + /* 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 */ +} -/* 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; else g_moo = xtn->next; 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; } -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_vmprim_t vmprim; - moo_cb_t cb; + moo_evtcb_t evtcb; xtn_t* xtn; 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); - /* initial log goes to stderr */ - xtn->logfd = -2; - xtn->logfd_trait = 0; -#if defined(HAVE_ISATTY) - if (isatty(xtn->logfd)) xtn->logfd_trait |= LOGFD_TTY; -#endif + chain (moo); /* call chain() before moo_regevtcb() as fini_moo() calls unchain() */ + reset_log_to_default (xtn); - MOO_MEMSET (&cb, 0, MOO_SIZEOF(cb)); - cb.fini = fini_moo; - moo_regcb (moo, &cb); + MOO_MEMSET (&evtcb, 0, MOO_SIZEOF(evtcb)); + evtcb.fini = fini_moo; + moo_regevtcb (moo, &evtcb); /* TODO: cfg->logopt, dbgopt => bch uch differentation */ 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; } -/* 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; } @@ -3009,7 +3038,7 @@ void* moo_getxtnstd (moo_t* moo) 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; 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); for (i = 0; i < count; i++) { - xtn->instd = &instd[i]; + xtn->in = &in[i]; if (moo_compile(moo, input_handler) <= -1) return -1; }