Skip to content

Commit

Permalink
Add support for additional signals; SIGTERM performs a clean exit.
Browse files Browse the repository at this point in the history
  • Loading branch information
SirWumpus committed Aug 13, 2024
1 parent 1a3b202 commit 9cc8990
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
29 changes: 21 additions & 8 deletions src/post4.c
Original file line number Diff line number Diff line change
Expand Up @@ -1505,9 +1505,11 @@ p4Repl(P4_Ctx *ctx)
_thrown:
switch (rc) {
case P4_THROW_ABORT:
case P4_THROW_QUIT:
/* Historically no message, simply return to REPL. */
break;
case P4_THROW_TERMINATE:
(void) printf(crlf);
goto setjmp_cleanup;
default:
#ifdef USE_EXCEPTION_STRINGS
(void) printf("%d thrown: %s", rc, P4_THROW_future <= rc && rc < 0 ? p4_exceptions[-rc] : "?");
Expand Down Expand Up @@ -2952,13 +2954,24 @@ static const char p4_build_info[] =
"POST4_PATH=\"" P4_CORE_PATH "\"\r\n"
;

static int signalmap[][2] = {
{ SIGBUS, P4_THROW_SIGBUS },
{ SIGINT, P4_THROW_SIGINT },
{ SIGFPE, P4_THROW_SIGFPE },
{ SIGQUIT, P4_THROW_QUIT },
{ SIGSEGV, P4_THROW_SIGSEGV },
{ SIGTERM, P4_THROW_TERMINATE },
{ 0, 0 }
};

static void
sig_int(int signum)
{
switch (signum) {
case SIGINT: signum = P4_THROW_SIGINT; break;
case SIGFPE: signum = P4_THROW_SIGFPE; break;
case SIGSEGV: signum = P4_THROW_SIGSEGV; break;
for (int (*map)[2] = signalmap; map[0] != 0; map++) {
if (signum == (*map)[0]) {
signum = (*map)[1];
break;
}
}
LONGJMP(signal_ctx->on_throw, signum);
}
Expand All @@ -2971,9 +2984,9 @@ main(int argc, char **argv)

p4Init();

signal(SIGINT, sig_int);
signal(SIGFPE, sig_int);
signal(SIGSEGV, sig_int);
for (int (*map)[2] = signalmap; (*map)[0] != 0; map++) {
signal((*map)[0], sig_int);
}

while ((ch = getopt(argc, argv, "b:c:d:f:i:m:r:V")) != -1) {
switch (ch) {
Expand Down
1 change: 1 addition & 0 deletions src/post4.h
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ typedef struct {

/* -4095..-256 reserved for the system (that's us). */

#define P4_THROW_TERMINATE (-256)
#define P4_THROW_GENERIC (-4095)

/***********************************************************************
Expand Down
6 changes: 0 additions & 6 deletions test/exceptions.p4
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,4 @@ test_group_end
\ https://github.com/ForthHub/discussion/discussions/116#discussioncomment-3541822
t{ :NONAME 123 [: 456 -56 THROW ;] CATCH ; EXECUTE -> 123 -56 }t

\ While QUIT leaves the data stack untouched, it does resets the return
\ stack, disrupting the test suite. Also isolate QUIT with EVALUATE,
\ which saves input source state.
\ : tw_quit_noreturn [: 123 QUIT ;] CATCH 456 THROW ;
\ t{ _rsp@ S" tw_quit_noreturn" EVALUATE SWAP _rsp! -> 123 }t

test_group_end

0 comments on commit 9cc8990

Please sign in to comment.