added moo_catch_termreq(), moo_uncatch_termreq(), moo_ignore_termreq() to relocated some signal handling functions from main.c to std.c

This commit is contained in:
hyunghwan.chung 2018-11-26 09:49:03 +00:00
parent 55b259cc4f
commit 45633570cd
3 changed files with 171 additions and 190 deletions

View File

@ -33,166 +33,35 @@
#include <stdlib.h> #include <stdlib.h>
#include <locale.h> #include <locale.h>
#if defined(_WIN32) static void print_syntax_error (moo_t* moo, const char* main_src_file)
# include <windows.h>
#elif defined(__OS2__)
# define INCL_DOSMODULEMGR
# define INCL_DOSPROCESS
# define INCL_DOSSEMAPHORES
# define INCL_DOSEXCEPTIONS
# define INCL_DOSMISC
# define INCL_DOSDATETIME
# define INCL_DOSFILEMGR
# define INCL_DOSERRORS
# include <os2.h>
# include <time.h>
# include <fcntl.h>
# include <io.h>
# include <errno.h>
#elif defined(__DOS__)
# include <dos.h>
# include <time.h>
# include <io.h>
# include <signal.h>
# include <errno.h>
#elif defined(macintosh)
# include <Types.h>
# include <OSUtils.h>
# include <Timer.h>
# include <MacErrors.h>
# include <Process.h>
# include <Dialogs.h>
# include <TextUtils.h>
/* TODO: a lot to do */
#elif defined(vms) || defined(__vms)
# define __NEW_STARLET 1
# include <starlet.h> /* (SYS$...) */
# include <ssdef.h> /* (SS$...) */
# include <lib$routines.h> /* (lib$...) */
/* TODO: a lot to do */
#else
# include <sys/types.h>
# include <unistd.h>
# include <fcntl.h>
# include <errno.h>
# 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
# if defined(HAVE_SYS_MMAN_H)
# include <sys/mman.h>
# endif
# if defined(USE_THREAD)
# include <pthread.h>
# include <sched.h>
# endif
#endif
/* ========================================================================= */
static moo_t* g_moo = MOO_NULL;
static MOO_INLINE void abort_moo (void)
{ {
if (g_moo) moo_abortstd (g_moo); moo_synerr_t synerr;
}
/* ========================================================================= */ moo_getsynerr (moo, &synerr);
#if defined(_WIN32) moo_logbfmt (moo, MOO_LOG_STDERR, "ERROR: ");
static BOOL WINAPI handle_term (DWORD ctrl_type) if (synerr.loc.file)
{
if (ctrl_type == CTRL_C_EVENT || ctrl_type == CTRL_CLOSE_EVENT)
{ {
abort_moo (); moo_logbfmt (moo, MOO_LOG_STDERR, "%js", synerr.loc.file);
return TRUE; }
else
{
moo_logbfmt (moo, MOO_LOG_STDERR, "%s", main_src_file);
} }
return FALSE; moo_logbfmt (moo, MOO_LOG_STDERR, "[%zu,%zu] %js",
} synerr.loc.line, synerr.loc.colm,
(moo_geterrmsg(moo) != moo_geterrstr(moo)? moo_geterrmsg(moo): moo_geterrstr(moo))
);
#elif defined(__OS2__) if (synerr.tgt.len > 0)
static EXCEPTIONREGISTRATIONRECORD os2_excrr = { 0 };
static ULONG _System handle_term (
PEXCEPTIONREPORTRECORD p1,
PEXCEPTIONREGISTRATIONRECORD p2,
PCONTEXTRECORD p3,
PVOID pv)
{
if (p1->ExceptionNum == XCPT_SIGNAL)
{ {
if (p1->ExceptionInfo[0] == XCPT_SIGNAL_INTR || moo_logbfmt (moo, MOO_LOG_STDERR, " - %.*js", synerr.tgt.len, synerr.tgt.ptr);
p1->ExceptionInfo[0] == XCPT_SIGNAL_KILLPROC ||
p1->ExceptionInfo[0] == XCPT_SIGNAL_BREAK)
{
abort_moo ();
return (DosAcknowledgeSignalException(p1->ExceptionInfo[0]) != NO_ERROR)? 1: XCPT_CONTINUE_EXECUTION;
}
} }
return XCPT_CONTINUE_SEARCH; /* exception not resolved */ moo_logbfmt (moo, MOO_LOG_STDERR, "\n");
}
#else
static void handle_term (int sig)
{
abort_moo ();
}
#endif
static void setup_sigterm (void)
{
#if defined(_WIN32)
SetConsoleCtrlHandler (handle_term, TRUE);
#elif defined(__OS2__)
os2_excrr.ExceptionHandler = (ERR)handle_term;
DosSetExceptionHandler (&os2_excrr); /* TODO: check if NO_ERROR is returned */
#elif defined(__DOS__)
signal (SIGINT, handle_term);
#else
struct sigaction sa;
memset (&sa, 0, MOO_SIZEOF(sa));
sa.sa_handler = handle_term;
sigaction (SIGINT, &sa, MOO_NULL);
#endif
} }
static void clear_sigterm (void)
{
#if defined(_WIN32)
SetConsoleCtrlHandler (handle_term, FALSE);
#elif defined(__OS2__)
DosUnsetExceptionHandler (&os2_excrr);
#elif defined(__DOS__)
signal (SIGINT, SIG_DFL);
#else
struct sigaction sa;
memset (&sa, 0, MOO_SIZEOF(sa));
sa.sa_handler = SIG_DFL;
sigaction (SIGINT, &sa, MOO_NULL);
#endif
}
/* ========================================================================= */ /* ========================================================================= */
#define MIN_MEMSIZE 2048000ul #define MIN_MEMSIZE 2048000ul
@ -371,7 +240,7 @@ int main (int argc, char* argv[])
{ {
moo_iostd_t in; moo_iostd_t in;
#if 0 #if 1
in.type = MOO_IOSTD_FILEB; in.type = MOO_IOSTD_FILEB;
in.u.fileb.path = argv[i]; in.u.fileb.path = argv[i];
#else #else
@ -388,31 +257,7 @@ int main (int argc, char* argv[])
{ {
if (moo->errnum == MOO_ESYNERR) if (moo->errnum == MOO_ESYNERR)
{ {
moo_synerr_t synerr; print_syntax_error (moo, argv[i]);
moo_getsynerr (moo, &synerr);
moo_logbfmt (moo, MOO_LOG_STDERR, "ERROR: ");
if (synerr.loc.file)
{
moo_logbfmt (moo, MOO_LOG_STDERR, "%js", synerr.loc.file);
}
else
{
moo_logbfmt (moo, MOO_LOG_STDERR, "%s", argv[i]);
}
moo_logbfmt (moo, MOO_LOG_STDERR, "[%zu,%zu] %js",
synerr.loc.line, synerr.loc.colm,
(moo_geterrmsg(moo) != moo_geterrstr(moo)? moo_geterrmsg(moo): moo_geterrstr(moo))
);
if (synerr.tgt.len > 0)
{
moo_logbfmt (moo, MOO_LOG_STDERR, " - %.*js", synerr.tgt.len, synerr.tgt.ptr);
}
moo_logbfmt (moo, MOO_LOG_STDERR, "\n");
} }
else else
{ {
@ -426,9 +271,9 @@ int main (int argc, char* argv[])
MOO_DEBUG0 (moo, "COMPILE OK. STARTING EXECUTION...\n"); MOO_DEBUG0 (moo, "COMPILE OK. STARTING EXECUTION...\n");
xret = 0; xret = 0;
g_moo = moo;
moo_catch_termreq ();
moo_start_ticker (); moo_start_ticker ();
setup_sigterm ();
moo_rcvtickstd (moo, 1); moo_rcvtickstd (moo, 1);
@ -443,8 +288,7 @@ int main (int argc, char* argv[])
} }
moo_stop_ticker (); moo_stop_ticker ();
clear_sigterm (); moo_uncatch_termreq ();
g_moo = MOO_NULL;
/*moo_dumpsymtab(moo); /*moo_dumpsymtab(moo);
*moo_dumpdic(moo, moo->sysdic, "System dictionary");*/ *moo_dumpdic(moo, moo->sysdic, "System dictionary");*/

View File

@ -86,6 +86,19 @@ MOO_EXPORT void moo_stop_ticker (
void void
); );
MOO_EXPORT void moo_catch_termreq (
void
);
MOO_EXPORT void moo_uncatch_termreq (
void
);
MOO_EXPORT void moo_ignore_termreq (
void
);
/* ----------------------------------------------------------------------- */
MOO_EXPORT moo_t* moo_openstd ( MOO_EXPORT moo_t* moo_openstd (
moo_oow_t xtnsize, moo_oow_t xtnsize,
const moo_cfgstd_t* cfg, const moo_cfgstd_t* cfg,
@ -111,6 +124,9 @@ MOO_EXPORT void moo_rcvtickstd (
int v int v
); );
#if defined(__cplusplus) #if defined(__cplusplus)
} }
#endif #endif

View File

@ -2728,10 +2728,28 @@ static int unset_signal_handler (int sig)
/* ========================================================================= */ /* ========================================================================= */
static MOO_INLINE void abort_all_moos (int unused)
{
/* TODO: make this atomic */
if (g_moo)
{
moo_t* moo = g_moo;
do
{
xtn_t* xtn = GET_XTN(moo);
moo_abortstd (moo);
moo = xtn->next;
}
while (moo);
}
/* TODO: make this atomic */
}
/*#define MOO_TICKER_INTERVAL_USECS 10000*/ /* microseconds. 0.01 seconds */ /*#define MOO_TICKER_INTERVAL_USECS 10000*/ /* microseconds. 0.01 seconds */
#define MOO_TICKER_INTERVAL_USECS 20000 /* microseconds. 0.02 seconds. */ #define MOO_TICKER_INTERVAL_USECS 20000 /* microseconds. 0.02 seconds. */
static MOO_INLINE void swproc_all_moos (void) static MOO_INLINE void swproc_all_moos (int unused)
{ {
/* TODO: make this atomic */ /* TODO: make this atomic */
if (g_moo) if (g_moo)
@ -2790,7 +2808,7 @@ static DWORD WINAPI msw_wait_for_timer_event (LPVOID ctx)
{ {
if (WaitForSingleObject(msw_tick_timer, 100000) == WAIT_OBJECT_0) if (WaitForSingleObject(msw_tick_timer, 100000) == WAIT_OBJECT_0)
{ {
swproc_all_moos (); swproc_all_moos (0);
#if defined(MSW_TICKER_MANUAL_RESET) #if defined(MSW_TICKER_MANUAL_RESET)
SetWaitableTimer (msw_tick_timer, &li, 0, MOO_NULL, MOO_NULL, FALSE); SetWaitableTimer (msw_tick_timer, &li, 0, MOO_NULL, MOO_NULL, FALSE);
#endif #endif
@ -2860,11 +2878,11 @@ static void EXPENTRY os2_wait_for_timer_event (ULONG x)
{ {
rc = DosWaitEventSem((HSEM)os2_tick_sem, 5000L); rc = DosWaitEventSem((HSEM)os2_tick_sem, 5000L);
#if 0 #if 0
swproc_all_moos (); swproc_all_moos (0);
DosResetEventSem ((HSEM)os2_tick_sem, &count); DosResetEventSem ((HSEM)os2_tick_sem, &count);
#else #else
DosResetEventSem ((HSEM)os2_tick_sem, &count); DosResetEventSem ((HSEM)os2_tick_sem, &count);
swproc_all_moos (); swproc_all_moos (0);
#endif #endif
} }
@ -2915,7 +2933,7 @@ static void __interrupt dos_timer_intr_handler (void)
*/ */
/* The timer interrupt (normally) occurs 18.2 times per second. */ /* The timer interrupt (normally) occurs 18.2 times per second. */
swproc_all_moos (); swproc_all_moos (0);
_chain_intr (dos_prev_timer_intr_handler); _chain_intr (dos_prev_timer_intr_handler);
} }
@ -2940,7 +2958,7 @@ static ProcessSerialNumber mac_psn;
static pascal void timer_intr_handler (TMTask* task) static pascal void timer_intr_handler (TMTask* task)
{ {
swproc_all_moos (); swproc_all_moos (0);
WakeUpProcess (&mac_psn); WakeUpProcess (&mac_psn);
PrimeTime ((QElem*)&mac_tmtask, TMTASK_DELAY); PrimeTime ((QElem*)&mac_tmtask, TMTASK_DELAY);
} }
@ -2962,14 +2980,9 @@ static MOO_INLINE void stop_ticker (void)
#elif defined(HAVE_SETITIMER) && defined(SIGVTALRM) && defined(ITIMER_VIRTUAL) #elif defined(HAVE_SETITIMER) && defined(SIGVTALRM) && defined(ITIMER_VIRTUAL)
static void arrange_process_switching (int sig)
{
swproc_all_moos ();
}
static MOO_INLINE void start_ticker (void) static MOO_INLINE void start_ticker (void)
{ {
if (set_signal_handler(SIGVTALRM, arrange_process_switching, SA_RESTART) >= 0) if (set_signal_handler(SIGVTALRM, swproc_all_moos, SA_RESTART) >= 0)
{ {
struct itimerval itv; struct itimerval itv;
itv.it_interval.tv_sec = 0; itv.it_interval.tv_sec = 0;
@ -3561,3 +3574,111 @@ void moo_stop_ticker (void)
stop_ticker (); stop_ticker ();
} }
} }
/* ========================================================================== */
#if defined(_WIN32)
static BOOL WINAPI handle_term (DWORD ctrl_type)
{
if (ctrl_type == CTRL_C_EVENT || ctrl_type == CTRL_CLOSE_EVENT)
{
abort_all_moos (0);
return TRUE;
}
return FALSE;
}
void moo_catch_termreq (void)
{
SetConsoleCtrlHandler (handle_term, TRUE);
}
void moo_uncatch_termreq (void)
{
SetConsoleCtrlHandler (handle_term, FALSE);
}
void moo_ignore_termreq (void)
{
}
#elif defined(__OS2__)
static EXCEPTIONREGISTRATIONRECORD os2_excrr = { 0 };
static ULONG _System handle_term (
PEXCEPTIONREPORTRECORD p1,
PEXCEPTIONREGISTRATIONRECORD p2,
PCONTEXTRECORD p3,
PVOID pv)
{
if (p1->ExceptionNum == XCPT_SIGNAL)
{
if (p1->ExceptionInfo[0] == XCPT_SIGNAL_INTR ||
p1->ExceptionInfo[0] == XCPT_SIGNAL_KILLPROC ||
p1->ExceptionInfo[0] == XCPT_SIGNAL_BREAK)
{
abort_all_moos (0);
return (DosAcknowledgeSignalException(p1->ExceptionInfo[0]) != NO_ERROR)? 1: XCPT_CONTINUE_EXECUTION;
}
}
return XCPT_CONTINUE_SEARCH; /* exception not resolved */
}
void moo_catch_termreq (void)
{
os2_excrr.ExceptionHandler = (ERR)handle_term;
DosSetExceptionHandler (&os2_excrr); /* TODO: check if NO_ERROR is returned */
}
void moo_uncatch_termreq (void)
{
DosUnsetExceptionHandler (&os2_excrr);
}
void moo_ignore_termreq (void)
{
}
#elif defined(__DOS__)
void moo_catch_termreq (void)
{
signal (SIGINT, abort_all_moos);
signal (SIGTERM, abort_all_moos);
}
void moo_uncatch_termreq (void)
{
signal (SIGINT, SIG_DFL);
signal (SIGTERM, SGI_DFL);
}
void moo_ignore_termreq (void)
{
signal (SIGINT, SIG_IGN);
signal (SIGTERM, SIG_IGN);
}
#else
void moo_catch_termreq (void)
{
set_signal_handler(SIGINT, abort_all_moos, 0);
}
void moo_uncatch_termreq (void)
{
unset_signal_handler(SIGTERM);
}
void moo_ignore_termreq (void)
{
set_signal_handler(SIGINT, SIG_IGN, 0);
}
#endif