This commit is contained in:
156
bin/cut.c
156
bin/cut.c
@ -313,146 +313,6 @@ static void print_exec_error (hawk_cut_t* cut)
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
typedef struct sig_state_t sig_state_t;
|
||||
struct sig_state_t
|
||||
{
|
||||
hawk_uintptr_t handler;
|
||||
hawk_uintptr_t old_handler;
|
||||
#if defined(HAVE_SIGACTION)
|
||||
sigset_t old_sa_mask;
|
||||
int old_sa_flags;
|
||||
#endif
|
||||
};
|
||||
|
||||
typedef void (*sig_handler_t) (int sig);
|
||||
|
||||
static sig_state_t g_sig_state[HAWK_NSIG];
|
||||
|
||||
static int is_signal_handler_set (int sig)
|
||||
{
|
||||
return !!g_sig_state[sig].handler;
|
||||
}
|
||||
|
||||
#if defined(HAVE_SIGACTION)
|
||||
static void dispatch_siginfo (int sig, siginfo_t* si, void* ctx)
|
||||
{
|
||||
if (g_sig_state[sig].handler != (hawk_uintptr_t)SIG_IGN &&
|
||||
g_sig_state[sig].handler != (hawk_uintptr_t)SIG_DFL)
|
||||
{
|
||||
((sig_handler_t)g_sig_state[sig].handler) (sig);
|
||||
}
|
||||
|
||||
if (g_sig_state[sig].old_handler &&
|
||||
g_sig_state[sig].old_handler != (hawk_uintptr_t)SIG_IGN &&
|
||||
g_sig_state[sig].old_handler != (hawk_uintptr_t)SIG_DFL)
|
||||
{
|
||||
((void(*)(int, siginfo_t*, void*))g_sig_state[sig].old_handler) (sig, si, ctx);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static void dispatch_signal (int sig)
|
||||
{
|
||||
if (g_sig_state[sig].handler != (hawk_uintptr_t)SIG_IGN &&
|
||||
g_sig_state[sig].handler != (hawk_uintptr_t)SIG_DFL)
|
||||
{
|
||||
((sig_handler_t)g_sig_state[sig].handler) (sig);
|
||||
}
|
||||
|
||||
if (g_sig_state[sig].old_handler &&
|
||||
g_sig_state[sig].old_handler != (hawk_uintptr_t)SIG_IGN &&
|
||||
g_sig_state[sig].old_handler != (hawk_uintptr_t)SIG_DFL)
|
||||
{
|
||||
((sig_handler_t)g_sig_state[sig].old_handler) (sig);
|
||||
}
|
||||
}
|
||||
|
||||
static int set_signal_handler (int sig, sig_handler_t handler, int extra_flags)
|
||||
{
|
||||
if (g_sig_state[sig].handler)
|
||||
{
|
||||
/* already set - allow handler change. ignore extra_flags. */
|
||||
if (g_sig_state[sig].handler == (hawk_uintptr_t)handler) return -1;
|
||||
g_sig_state[sig].handler = (hawk_uintptr_t)handler;
|
||||
}
|
||||
else
|
||||
{
|
||||
#if defined(HAVE_SIGACTION)
|
||||
struct sigaction sa, oldsa;
|
||||
|
||||
if (sigaction(sig, HAWK_NULL, &oldsa) == -1) return -1;
|
||||
|
||||
memset (&sa, 0, HAWK_SIZEOF(sa));
|
||||
if (oldsa.sa_flags & SA_SIGINFO)
|
||||
{
|
||||
sa.sa_sigaction = dispatch_siginfo;
|
||||
sa.sa_flags = SA_SIGINFO;
|
||||
}
|
||||
else
|
||||
{
|
||||
sa.sa_handler = dispatch_signal;
|
||||
sa.sa_flags = 0;
|
||||
}
|
||||
sa.sa_flags |= extra_flags;
|
||||
/*sa.sa_flags |= SA_INTERUPT;
|
||||
sa.sa_flags |= SA_RESTART;*/
|
||||
sigfillset (&sa.sa_mask); /* block all signals while the handler is being executed */
|
||||
|
||||
if (sigaction(sig, &sa, HAWK_NULL) == -1) return -1;
|
||||
|
||||
g_sig_state[sig].handler = (hawk_uintptr_t)handler;
|
||||
if (oldsa.sa_flags & SA_SIGINFO)
|
||||
g_sig_state[sig].old_handler = (hawk_uintptr_t)oldsa.sa_sigaction;
|
||||
else
|
||||
g_sig_state[sig].old_handler = (hawk_uintptr_t)oldsa.sa_handler;
|
||||
|
||||
g_sig_state[sig].old_sa_mask = oldsa.sa_mask;
|
||||
g_sig_state[sig].old_sa_flags = oldsa.sa_flags;
|
||||
#else
|
||||
g_sig_state[sig].old_handler = (hawk_uintptr_t)signal(sig, handler);
|
||||
g_sig_state[sig].handler = (hawk_uintptr_t)dispatch_signal;
|
||||
#endif
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int unset_signal_handler (int sig)
|
||||
{
|
||||
#if defined(HAVE_SIGACTION)
|
||||
struct sigaction sa;
|
||||
#endif
|
||||
|
||||
if (!g_sig_state[sig].handler) return -1; /* not set */
|
||||
|
||||
#if defined(HAVE_SIGACTION)
|
||||
memset (&sa, 0, HAWK_SIZEOF(sa));
|
||||
sa.sa_mask = g_sig_state[sig].old_sa_mask;
|
||||
sa.sa_flags = g_sig_state[sig].old_sa_flags;
|
||||
|
||||
if (sa.sa_flags & SA_SIGINFO)
|
||||
{
|
||||
sa.sa_sigaction = (void(*)(int,siginfo_t*,void*))g_sig_state[sig].old_handler;
|
||||
}
|
||||
else
|
||||
{
|
||||
sa.sa_handler = (sig_handler_t)g_sig_state[sig].old_handler;
|
||||
}
|
||||
|
||||
if (sigaction(sig, &sa, HAWK_NULL) == -1) return -1;
|
||||
#else
|
||||
signal (sig, (sig_handler_t)g_sig_state[sig].old_handler);
|
||||
#endif
|
||||
|
||||
g_sig_state[sig].handler = 0;
|
||||
/* keep other fields untouched */
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
|
||||
static void stop_run (int signo)
|
||||
{
|
||||
#if !defined(_WIN32) && !defined(__OS2__) && !defined(__DOS__)
|
||||
@ -474,32 +334,32 @@ static void do_nothing (int unused)
|
||||
static void set_intr_run (void)
|
||||
{
|
||||
#if defined(SIGTERM)
|
||||
set_signal_handler (SIGTERM, stop_run, 0);
|
||||
hawk_main_set_signal_handler (SIGTERM, stop_run, 0);
|
||||
#endif
|
||||
#if defined(SIGHUP)
|
||||
set_signal_handler (SIGHUP, stop_run, 0);
|
||||
hawk_main_set_signal_handler (SIGHUP, stop_run, 0);
|
||||
#endif
|
||||
#if defined(SIGINT)
|
||||
set_signal_handler (SIGINT, stop_run, 0);
|
||||
hawk_main_set_signal_handler (SIGINT, stop_run, 0);
|
||||
#endif
|
||||
#if !defined(_WIN32) && !defined(__OS2__) && !defined(__DOS__) && defined(SIGPIPE)
|
||||
set_signal_handler (SIGPIPE, do_nothing, 0);
|
||||
hawk_main_set_signal_handler (SIGPIPE, do_nothing, 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void unset_intr_run (void)
|
||||
{
|
||||
#if defined(SIGTERM)
|
||||
unset_signal_handler (SIGTERM);
|
||||
hawk_main_unset_signal_handler (SIGTERM);
|
||||
#endif
|
||||
#if defined(SIGHUP)
|
||||
unset_signal_handler (SIGHUP);
|
||||
hawk_main_unset_signal_handler (SIGHUP);
|
||||
#endif
|
||||
#if defined(SIGINT)
|
||||
unset_signal_handler (SIGINT);
|
||||
hawk_main_unset_signal_handler (SIGINT);
|
||||
#endif
|
||||
#if !defined(_WIN32) && !defined(__OS2__) && !defined(__DOS__) && defined(SIGPIPE)
|
||||
unset_signal_handler (SIGPIPE);
|
||||
hawk_main_unset_signal_handler (SIGPIPE);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
157
bin/hawk.c
157
bin/hawk.c
@ -127,147 +127,6 @@ struct arg_t
|
||||
};
|
||||
|
||||
|
||||
/* ========================================================================= */
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
typedef struct sig_state_t sig_state_t;
|
||||
struct sig_state_t
|
||||
{
|
||||
hawk_uintptr_t handler;
|
||||
hawk_uintptr_t old_handler;
|
||||
#if defined(HAVE_SIGACTION)
|
||||
sigset_t old_sa_mask;
|
||||
int old_sa_flags;
|
||||
#endif
|
||||
};
|
||||
|
||||
typedef void (*sig_handler_t) (int sig);
|
||||
|
||||
static sig_state_t g_sig_state[HAWK_NSIG];
|
||||
|
||||
static int is_signal_handler_set (int sig)
|
||||
{
|
||||
return !!g_sig_state[sig].handler;
|
||||
}
|
||||
|
||||
#if defined(HAVE_SIGACTION)
|
||||
static void dispatch_siginfo (int sig, siginfo_t* si, void* ctx)
|
||||
{
|
||||
if (g_sig_state[sig].handler != (hawk_uintptr_t)SIG_IGN &&
|
||||
g_sig_state[sig].handler != (hawk_uintptr_t)SIG_DFL)
|
||||
{
|
||||
((sig_handler_t)g_sig_state[sig].handler) (sig);
|
||||
}
|
||||
|
||||
if (g_sig_state[sig].old_handler &&
|
||||
g_sig_state[sig].old_handler != (hawk_uintptr_t)SIG_IGN &&
|
||||
g_sig_state[sig].old_handler != (hawk_uintptr_t)SIG_DFL)
|
||||
{
|
||||
((void(*)(int, siginfo_t*, void*))g_sig_state[sig].old_handler) (sig, si, ctx);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static void dispatch_signal (int sig)
|
||||
{
|
||||
if (g_sig_state[sig].handler != (hawk_uintptr_t)SIG_IGN &&
|
||||
g_sig_state[sig].handler != (hawk_uintptr_t)SIG_DFL)
|
||||
{
|
||||
((sig_handler_t)g_sig_state[sig].handler) (sig);
|
||||
}
|
||||
|
||||
if (g_sig_state[sig].old_handler &&
|
||||
g_sig_state[sig].old_handler != (hawk_uintptr_t)SIG_IGN &&
|
||||
g_sig_state[sig].old_handler != (hawk_uintptr_t)SIG_DFL)
|
||||
{
|
||||
((sig_handler_t)g_sig_state[sig].old_handler) (sig);
|
||||
}
|
||||
}
|
||||
|
||||
static int set_signal_handler (int sig, sig_handler_t handler, int extra_flags)
|
||||
{
|
||||
if (g_sig_state[sig].handler)
|
||||
{
|
||||
/* already set - allow handler change. ignore extra_flags. */
|
||||
if (g_sig_state[sig].handler == (hawk_uintptr_t)handler) return -1;
|
||||
g_sig_state[sig].handler = (hawk_uintptr_t)handler;
|
||||
}
|
||||
else
|
||||
{
|
||||
#if defined(HAVE_SIGACTION)
|
||||
struct sigaction sa, oldsa;
|
||||
|
||||
if (sigaction(sig, HAWK_NULL, &oldsa) == -1) return -1;
|
||||
|
||||
memset (&sa, 0, HAWK_SIZEOF(sa));
|
||||
if (oldsa.sa_flags & SA_SIGINFO)
|
||||
{
|
||||
sa.sa_sigaction = dispatch_siginfo;
|
||||
sa.sa_flags = SA_SIGINFO;
|
||||
}
|
||||
else
|
||||
{
|
||||
sa.sa_handler = dispatch_signal;
|
||||
sa.sa_flags = 0;
|
||||
}
|
||||
sa.sa_flags |= extra_flags;
|
||||
/*sa.sa_flags |= SA_INTERUPT;
|
||||
sa.sa_flags |= SA_RESTART;*/
|
||||
sigfillset (&sa.sa_mask); /* block all signals while the handler is being executed */
|
||||
|
||||
if (sigaction(sig, &sa, HAWK_NULL) == -1) return -1;
|
||||
|
||||
g_sig_state[sig].handler = (hawk_uintptr_t)handler;
|
||||
if (oldsa.sa_flags & SA_SIGINFO)
|
||||
g_sig_state[sig].old_handler = (hawk_uintptr_t)oldsa.sa_sigaction;
|
||||
else
|
||||
g_sig_state[sig].old_handler = (hawk_uintptr_t)oldsa.sa_handler;
|
||||
|
||||
g_sig_state[sig].old_sa_mask = oldsa.sa_mask;
|
||||
g_sig_state[sig].old_sa_flags = oldsa.sa_flags;
|
||||
#else
|
||||
g_sig_state[sig].old_handler = (hawk_uintptr_t)signal(sig, handler);
|
||||
g_sig_state[sig].handler = (hawk_uintptr_t)dispatch_signal;
|
||||
#endif
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int unset_signal_handler (int sig)
|
||||
{
|
||||
#if defined(HAVE_SIGACTION)
|
||||
struct sigaction sa;
|
||||
#endif
|
||||
|
||||
if (!g_sig_state[sig].handler) return -1; /* not set */
|
||||
|
||||
#if defined(HAVE_SIGACTION)
|
||||
memset (&sa, 0, HAWK_SIZEOF(sa));
|
||||
sa.sa_mask = g_sig_state[sig].old_sa_mask;
|
||||
sa.sa_flags = g_sig_state[sig].old_sa_flags;
|
||||
|
||||
if (sa.sa_flags & SA_SIGINFO)
|
||||
{
|
||||
sa.sa_sigaction = (void(*)(int,siginfo_t*,void*))g_sig_state[sig].old_handler;
|
||||
}
|
||||
else
|
||||
{
|
||||
sa.sa_handler = (sig_handler_t)g_sig_state[sig].old_handler;
|
||||
}
|
||||
|
||||
if (sigaction(sig, &sa, HAWK_NULL) == -1) return -1;
|
||||
#else
|
||||
signal (sig, (sig_handler_t)g_sig_state[sig].old_handler);
|
||||
#endif
|
||||
|
||||
g_sig_state[sig].handler = 0;
|
||||
/* keep other fields untouched */
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
static void stop_run (int signo)
|
||||
@ -291,40 +150,40 @@ static void do_nothing (int unused)
|
||||
static void set_intr_pipe (void)
|
||||
{
|
||||
#if !defined(_WIN32) && !defined(__OS2__) && !defined(__DOS__) && defined(SIGPIPE)
|
||||
set_signal_handler (SIGPIPE, do_nothing, 0);
|
||||
hawk_main_set_signal_handler (SIGPIPE, do_nothing, 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void unset_intr_pipe (void)
|
||||
{
|
||||
#if !defined(_WIN32) && !defined(__OS2__) && !defined(__DOS__) && defined(SIGPIPE)
|
||||
unset_signal_handler (SIGPIPE);
|
||||
hawk_main_unset_signal_handler (SIGPIPE);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void set_intr_run (void)
|
||||
{
|
||||
#if defined(SIGTERM)
|
||||
set_signal_handler (SIGTERM, stop_run, 0);
|
||||
hawk_main_set_signal_handler (SIGTERM, stop_run, 0);
|
||||
#endif
|
||||
#if defined(SIGHUP)
|
||||
set_signal_handler (SIGHUP, stop_run, 0);
|
||||
hawk_main_set_signal_handler (SIGHUP, stop_run, 0);
|
||||
#endif
|
||||
#if defined(SIGINT)
|
||||
set_signal_handler (SIGINT, stop_run, 0);
|
||||
hawk_main_set_signal_handler (SIGINT, stop_run, 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void unset_intr_run (void)
|
||||
{
|
||||
#if defined(SIGTERM)
|
||||
unset_signal_handler (SIGTERM);
|
||||
hawk_main_unset_signal_handler (SIGTERM);
|
||||
#endif
|
||||
#if defined(SIGHUP)
|
||||
unset_signal_handler (SIGHUP);
|
||||
hawk_main_unset_signal_handler (SIGHUP);
|
||||
#endif
|
||||
#if defined(SIGINT)
|
||||
unset_signal_handler (SIGINT);
|
||||
hawk_main_unset_signal_handler (SIGINT);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
137
bin/main.c
137
bin/main.c
@ -29,6 +29,7 @@
|
||||
#include <hawk-xma.h>
|
||||
#include <stdio.h>
|
||||
#include <locale.h>
|
||||
#include <string.h>
|
||||
|
||||
#if defined(_WIN32)
|
||||
# include <windows.h>
|
||||
@ -44,6 +45,142 @@
|
||||
# include <errno.h>
|
||||
# include <signal.h>
|
||||
#endif
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
typedef struct sig_state_t sig_state_t;
|
||||
struct sig_state_t
|
||||
{
|
||||
hawk_uintptr_t handler;
|
||||
hawk_uintptr_t old_handler;
|
||||
#if defined(HAVE_SIGACTION)
|
||||
sigset_t old_sa_mask;
|
||||
int old_sa_flags;
|
||||
#endif
|
||||
};
|
||||
|
||||
static sig_state_t g_sig_state[HAWK_NSIG];
|
||||
|
||||
static int is_signal_handler_set (int sig)
|
||||
{
|
||||
return !!g_sig_state[sig].handler;
|
||||
}
|
||||
|
||||
#if defined(HAVE_SIGACTION)
|
||||
static void dispatch_siginfo (int sig, siginfo_t* si, void* ctx)
|
||||
{
|
||||
if (g_sig_state[sig].handler != (hawk_uintptr_t)SIG_IGN &&
|
||||
g_sig_state[sig].handler != (hawk_uintptr_t)SIG_DFL)
|
||||
{
|
||||
((hawk_main_sig_handler_t)g_sig_state[sig].handler) (sig);
|
||||
}
|
||||
|
||||
if (g_sig_state[sig].old_handler &&
|
||||
g_sig_state[sig].old_handler != (hawk_uintptr_t)SIG_IGN &&
|
||||
g_sig_state[sig].old_handler != (hawk_uintptr_t)SIG_DFL)
|
||||
{
|
||||
((void(*)(int, siginfo_t*, void*))g_sig_state[sig].old_handler) (sig, si, ctx);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static void dispatch_signal (int sig)
|
||||
{
|
||||
if (g_sig_state[sig].handler != (hawk_uintptr_t)SIG_IGN &&
|
||||
g_sig_state[sig].handler != (hawk_uintptr_t)SIG_DFL)
|
||||
{
|
||||
((hawk_main_sig_handler_t)g_sig_state[sig].handler) (sig);
|
||||
}
|
||||
|
||||
if (g_sig_state[sig].old_handler &&
|
||||
g_sig_state[sig].old_handler != (hawk_uintptr_t)SIG_IGN &&
|
||||
g_sig_state[sig].old_handler != (hawk_uintptr_t)SIG_DFL)
|
||||
{
|
||||
((hawk_main_sig_handler_t)g_sig_state[sig].old_handler) (sig);
|
||||
}
|
||||
}
|
||||
|
||||
int hawk_main_set_signal_handler (int sig, hawk_main_sig_handler_t handler, int extra_flags)
|
||||
{
|
||||
if (g_sig_state[sig].handler)
|
||||
{
|
||||
/* already set - allow handler change. ignore extra_flags. */
|
||||
if (g_sig_state[sig].handler == (hawk_uintptr_t)handler) return -1;
|
||||
g_sig_state[sig].handler = (hawk_uintptr_t)handler;
|
||||
}
|
||||
else
|
||||
{
|
||||
#if defined(HAVE_SIGACTION)
|
||||
struct sigaction sa, oldsa;
|
||||
|
||||
if (sigaction(sig, HAWK_NULL, &oldsa) == -1) return -1;
|
||||
|
||||
memset(&sa, 0, HAWK_SIZEOF(sa));
|
||||
if (oldsa.sa_flags & SA_SIGINFO)
|
||||
{
|
||||
sa.sa_sigaction = dispatch_siginfo;
|
||||
sa.sa_flags = SA_SIGINFO;
|
||||
}
|
||||
else
|
||||
{
|
||||
sa.sa_handler = dispatch_signal;
|
||||
sa.sa_flags = 0;
|
||||
}
|
||||
sa.sa_flags |= extra_flags;
|
||||
/*sa.sa_flags |= SA_INTERUPT;
|
||||
sa.sa_flags |= SA_RESTART;*/
|
||||
sigfillset (&sa.sa_mask); /* block all signals while the handler is being executed */
|
||||
|
||||
if (sigaction(sig, &sa, HAWK_NULL) == -1) return -1;
|
||||
|
||||
g_sig_state[sig].handler = (hawk_uintptr_t)handler;
|
||||
if (oldsa.sa_flags & SA_SIGINFO)
|
||||
g_sig_state[sig].old_handler = (hawk_uintptr_t)oldsa.sa_sigaction;
|
||||
else
|
||||
g_sig_state[sig].old_handler = (hawk_uintptr_t)oldsa.sa_handler;
|
||||
|
||||
g_sig_state[sig].old_sa_mask = oldsa.sa_mask;
|
||||
g_sig_state[sig].old_sa_flags = oldsa.sa_flags;
|
||||
#else
|
||||
g_sig_state[sig].old_handler = (hawk_uintptr_t)signal(sig, handler);
|
||||
g_sig_state[sig].handler = (hawk_uintptr_t)dispatch_signal;
|
||||
#endif
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int hawk_main_unset_signal_handler (int sig)
|
||||
{
|
||||
#if defined(HAVE_SIGACTION)
|
||||
struct sigaction sa;
|
||||
#endif
|
||||
|
||||
if (!g_sig_state[sig].handler) return -1; /* not set */
|
||||
|
||||
#if defined(HAVE_SIGACTION)
|
||||
memset(&sa, 0, HAWK_SIZEOF(sa));
|
||||
sa.sa_mask = g_sig_state[sig].old_sa_mask;
|
||||
sa.sa_flags = g_sig_state[sig].old_sa_flags;
|
||||
|
||||
if (sa.sa_flags & SA_SIGINFO)
|
||||
{
|
||||
sa.sa_sigaction = (void(*)(int,siginfo_t*,void*))g_sig_state[sig].old_handler;
|
||||
}
|
||||
else
|
||||
{
|
||||
sa.sa_handler = (hawk_main_sig_handler_t)g_sig_state[sig].old_handler;
|
||||
}
|
||||
|
||||
if (sigaction(sig, &sa, HAWK_NULL) == -1) return -1;
|
||||
#else
|
||||
signal (sig, (hawk_main_sig_handler_t)g_sig_state[sig].old_handler);
|
||||
#endif
|
||||
|
||||
g_sig_state[sig].handler = 0;
|
||||
/* keep other fields untouched */
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------- */
|
||||
|
||||
|
@ -29,6 +29,8 @@
|
||||
|
||||
#include <hawk.h>
|
||||
|
||||
typedef void (*hawk_main_sig_handler_t) (int sig);
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
@ -41,6 +43,10 @@ void hawk_main_print_xma (void* ctx, const hawk_bch_t* fmt, ...);
|
||||
void hawk_main_print_error (const hawk_bch_t* fmt, ...);
|
||||
void hawk_main_print_warning (const hawk_bch_t* fmt, ...);
|
||||
|
||||
int hawk_main_set_signal_handler (int sig, hawk_main_sig_handler_t handler, int extra_flags);
|
||||
int hawk_main_unset_signal_handler (int sig);
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
155
bin/sed.c
155
bin/sed.c
@ -372,145 +372,6 @@ static void print_exec_error(hawk_sed_t* sed)
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
typedef struct sig_state_t sig_state_t;
|
||||
struct sig_state_t
|
||||
{
|
||||
hawk_uintptr_t handler;
|
||||
hawk_uintptr_t old_handler;
|
||||
#if defined(HAVE_SIGACTION)
|
||||
sigset_t old_sa_mask;
|
||||
int old_sa_flags;
|
||||
#endif
|
||||
};
|
||||
|
||||
typedef void (*sig_handler_t) (int sig);
|
||||
|
||||
static sig_state_t g_sig_state[HAWK_NSIG];
|
||||
|
||||
static int is_signal_handler_set (int sig)
|
||||
{
|
||||
return !!g_sig_state[sig].handler;
|
||||
}
|
||||
|
||||
#if defined(HAVE_SIGACTION)
|
||||
static void dispatch_siginfo (int sig, siginfo_t* si, void* ctx)
|
||||
{
|
||||
if (g_sig_state[sig].handler != (hawk_uintptr_t)SIG_IGN &&
|
||||
g_sig_state[sig].handler != (hawk_uintptr_t)SIG_DFL)
|
||||
{
|
||||
((sig_handler_t)g_sig_state[sig].handler) (sig);
|
||||
}
|
||||
|
||||
if (g_sig_state[sig].old_handler &&
|
||||
g_sig_state[sig].old_handler != (hawk_uintptr_t)SIG_IGN &&
|
||||
g_sig_state[sig].old_handler != (hawk_uintptr_t)SIG_DFL)
|
||||
{
|
||||
((void(*)(int, siginfo_t*, void*))g_sig_state[sig].old_handler) (sig, si, ctx);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static void dispatch_signal (int sig)
|
||||
{
|
||||
if (g_sig_state[sig].handler != (hawk_uintptr_t)SIG_IGN &&
|
||||
g_sig_state[sig].handler != (hawk_uintptr_t)SIG_DFL)
|
||||
{
|
||||
((sig_handler_t)g_sig_state[sig].handler) (sig);
|
||||
}
|
||||
|
||||
if (g_sig_state[sig].old_handler &&
|
||||
g_sig_state[sig].old_handler != (hawk_uintptr_t)SIG_IGN &&
|
||||
g_sig_state[sig].old_handler != (hawk_uintptr_t)SIG_DFL)
|
||||
{
|
||||
((sig_handler_t)g_sig_state[sig].old_handler) (sig);
|
||||
}
|
||||
}
|
||||
|
||||
static int set_signal_handler (int sig, sig_handler_t handler, int extra_flags)
|
||||
{
|
||||
if (g_sig_state[sig].handler)
|
||||
{
|
||||
/* already set - allow handler change. ignore extra_flags. */
|
||||
if (g_sig_state[sig].handler == (hawk_uintptr_t)handler) return -1;
|
||||
g_sig_state[sig].handler = (hawk_uintptr_t)handler;
|
||||
}
|
||||
else
|
||||
{
|
||||
#if defined(HAVE_SIGACTION)
|
||||
struct sigaction sa, oldsa;
|
||||
|
||||
if (sigaction(sig, HAWK_NULL, &oldsa) == -1) return -1;
|
||||
|
||||
memset (&sa, 0, HAWK_SIZEOF(sa));
|
||||
if (oldsa.sa_flags & SA_SIGINFO)
|
||||
{
|
||||
sa.sa_sigaction = dispatch_siginfo;
|
||||
sa.sa_flags = SA_SIGINFO;
|
||||
}
|
||||
else
|
||||
{
|
||||
sa.sa_handler = dispatch_signal;
|
||||
sa.sa_flags = 0;
|
||||
}
|
||||
sa.sa_flags |= extra_flags;
|
||||
/*sa.sa_flags |= SA_INTERUPT;
|
||||
sa.sa_flags |= SA_RESTART;*/
|
||||
sigfillset (&sa.sa_mask); /* block all signals while the handler is being executed */
|
||||
|
||||
if (sigaction(sig, &sa, HAWK_NULL) == -1) return -1;
|
||||
|
||||
g_sig_state[sig].handler = (hawk_uintptr_t)handler;
|
||||
if (oldsa.sa_flags & SA_SIGINFO)
|
||||
g_sig_state[sig].old_handler = (hawk_uintptr_t)oldsa.sa_sigaction;
|
||||
else
|
||||
g_sig_state[sig].old_handler = (hawk_uintptr_t)oldsa.sa_handler;
|
||||
|
||||
g_sig_state[sig].old_sa_mask = oldsa.sa_mask;
|
||||
g_sig_state[sig].old_sa_flags = oldsa.sa_flags;
|
||||
#else
|
||||
g_sig_state[sig].old_handler = (hawk_uintptr_t)signal(sig, handler);
|
||||
g_sig_state[sig].handler = (hawk_uintptr_t)dispatch_signal;
|
||||
#endif
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int unset_signal_handler (int sig)
|
||||
{
|
||||
#if defined(HAVE_SIGACTION)
|
||||
struct sigaction sa;
|
||||
#endif
|
||||
|
||||
if (!g_sig_state[sig].handler) return -1; /* not set */
|
||||
|
||||
#if defined(HAVE_SIGACTION)
|
||||
memset (&sa, 0, HAWK_SIZEOF(sa));
|
||||
sa.sa_mask = g_sig_state[sig].old_sa_mask;
|
||||
sa.sa_flags = g_sig_state[sig].old_sa_flags;
|
||||
|
||||
if (sa.sa_flags & SA_SIGINFO)
|
||||
{
|
||||
sa.sa_sigaction = (void(*)(int,siginfo_t*,void*))g_sig_state[sig].old_handler;
|
||||
}
|
||||
else
|
||||
{
|
||||
sa.sa_handler = (sig_handler_t)g_sig_state[sig].old_handler;
|
||||
}
|
||||
|
||||
if (sigaction(sig, &sa, HAWK_NULL) == -1) return -1;
|
||||
#else
|
||||
signal (sig, (sig_handler_t)g_sig_state[sig].old_handler);
|
||||
#endif
|
||||
|
||||
g_sig_state[sig].handler = 0;
|
||||
/* keep other fields untouched */
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
|
||||
static void stop_run (int signo)
|
||||
{
|
||||
@ -532,32 +393,32 @@ static void do_nothing (int unused)
|
||||
static void set_intr_run (void)
|
||||
{
|
||||
#if defined(SIGTERM)
|
||||
set_signal_handler (SIGTERM, stop_run, 0);
|
||||
hawk_main_set_signal_handler (SIGTERM, stop_run, 0);
|
||||
#endif
|
||||
#if defined(SIGHUP)
|
||||
set_signal_handler (SIGHUP, stop_run, 0);
|
||||
hawk_main_set_signal_handler (SIGHUP, stop_run, 0);
|
||||
#endif
|
||||
#if defined(SIGINT)
|
||||
set_signal_handler (SIGINT, stop_run, 0);
|
||||
hawk_main_set_signal_handler (SIGINT, stop_run, 0);
|
||||
#endif
|
||||
#if !defined(_WIN32) && !defined(__OS2__) && !defined(__DOS__) && defined(SIGPIPE)
|
||||
set_signal_handler (SIGPIPE, do_nothing, 0);
|
||||
hawk_main_set_signal_handler (SIGPIPE, do_nothing, 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void unset_intr_run (void)
|
||||
{
|
||||
#if defined(SIGTERM)
|
||||
unset_signal_handler (SIGTERM);
|
||||
hawk_main_unset_signal_handler (SIGTERM);
|
||||
#endif
|
||||
#if defined(SIGHUP)
|
||||
unset_signal_handler (SIGHUP);
|
||||
hawk_main_unset_signal_handler (SIGHUP);
|
||||
#endif
|
||||
#if defined(SIGINT)
|
||||
unset_signal_handler (SIGINT);
|
||||
hawk_main_unset_signal_handler (SIGINT);
|
||||
#endif
|
||||
#if !defined(_WIN32) && !defined(__OS2__) && !defined(__DOS__) && defined(SIGPIPE)
|
||||
unset_signal_handler (SIGPIPE);
|
||||
hawk_main_unset_signal_handler (SIGPIPE);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -370,7 +370,7 @@ static hawk_sio_t* open_sio_std (hawk_cut_t* cut, hawk_sio_std_t std, int flags)
|
||||
if (sio == HAWK_NULL)
|
||||
{
|
||||
const hawk_ooch_t* bem = hawk_cut_backuperrmsg(cut);
|
||||
hawk_cut_seterrbfmt(cut, HAWK_NULL, HAWK_CUT_EIOFIL, "unable to open %js - %js", &sio_std_names[std], bem);
|
||||
hawk_cut_seterrbfmt(cut, HAWK_NULL, HAWK_CUT_EIOFIL, "unable to open %js - %js", sio_std_names[std].ptr, bem);
|
||||
}
|
||||
return sio;
|
||||
}
|
||||
@ -407,7 +407,7 @@ static void set_eiofil_for_iostd (hawk_cut_t* cut, hawk_cut_iostd_t* io)
|
||||
}
|
||||
else
|
||||
{
|
||||
hawk_cut_seterrbfmt(cut, HAWK_NULL, HAWK_CUT_EIOFIL, "I/O error with file '%js' - %js", &sio_std_names[HAWK_SIO_STDIN], bem);
|
||||
hawk_cut_seterrbfmt(cut, HAWK_NULL, HAWK_CUT_EIOFIL, "I/O error with file '%js' - %js", sio_std_names[HAWK_SIO_STDIN].ptr, bem);
|
||||
}
|
||||
}
|
||||
|
||||
@ -932,7 +932,7 @@ static hawk_ooi_t x_in (hawk_cut_t* cut, hawk_cut_io_cmd_t cmd, hawk_cut_io_arg_
|
||||
if (n <= -1)
|
||||
{
|
||||
const hawk_ooch_t* bem = hawk_cut_backuperrmsg(cut);
|
||||
hawk_cut_seterrbfmt(cut, HAWK_NULL, HAWK_CUT_EIOFIL, "unable to read '%js' - %js", &sio_std_names[HAWK_SIO_STDIN], bem);
|
||||
hawk_cut_seterrbfmt(cut, HAWK_NULL, HAWK_CUT_EIOFIL, "unable to read '%js' - %js", sio_std_names[HAWK_SIO_STDIN].ptr, bem);
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
@ -266,7 +266,7 @@ static hawk_sio_t* open_sio_std (hawk_sed_t* sed, hawk_sio_std_t std, int flags)
|
||||
if (sio == HAWK_NULL)
|
||||
{
|
||||
const hawk_ooch_t* bem = hawk_sed_backuperrmsg(sed);
|
||||
hawk_sed_seterrbfmt(sed, HAWK_NULL, HAWK_SED_EIOFIL, "unable to open %js - %js", &sio_std_names[std], bem);
|
||||
hawk_sed_seterrbfmt(sed, HAWK_NULL, HAWK_SED_EIOFIL, "unable to open %js - %js", sio_std_names[std].ptr, bem);
|
||||
}
|
||||
return sio;
|
||||
}
|
||||
@ -303,7 +303,7 @@ static void set_eiofil_for_iostd (hawk_sed_t* sed, hawk_sed_iostd_t* io)
|
||||
}
|
||||
else
|
||||
{
|
||||
hawk_sed_seterrbfmt(sed, HAWK_NULL, HAWK_SED_EIOFIL, "I/O error with file '%js' - %js", &sio_std_names[HAWK_SIO_STDIN], bem);
|
||||
hawk_sed_seterrbfmt(sed, HAWK_NULL, HAWK_SED_EIOFIL, "I/O error with file '%js' - %js", sio_std_names[HAWK_SIO_STDIN].ptr, bem);
|
||||
}
|
||||
}
|
||||
|
||||
@ -824,7 +824,7 @@ static hawk_ooi_t x_in (hawk_sed_t* sed, hawk_sed_io_cmd_t cmd, hawk_sed_io_arg_
|
||||
if (n <= -1)
|
||||
{
|
||||
const hawk_ooch_t* bem = hawk_sed_backuperrmsg(sed);
|
||||
hawk_sed_seterrbfmt(sed, HAWK_NULL, HAWK_SED_EIOFIL, "unable to read '%js' - %js", &sio_std_names[HAWK_SIO_STDIN], bem);
|
||||
hawk_sed_seterrbfmt(sed, HAWK_NULL, HAWK_SED_EIOFIL, "unable to read '%js' - %js", sio_std_names[HAWK_SIO_STDIN].ptr, bem);
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
13
lib/std.c
13
lib/std.c
@ -250,7 +250,7 @@ static void* xma_realloc (hawk_mmgr_t* mmgr, void* ptr, hawk_oow_t size)
|
||||
|
||||
static void xma_free (hawk_mmgr_t* mmgr, void* ptr)
|
||||
{
|
||||
hawk_xma_free (mmgr->ctx, ptr);
|
||||
hawk_xma_free(mmgr->ctx, ptr);
|
||||
}
|
||||
|
||||
int hawk_init_xma_mmgr (hawk_mmgr_t* mmgr, hawk_oow_t memlimit)
|
||||
@ -277,7 +277,6 @@ void hawk_fini_xma_mmgr (hawk_mmgr_t* mmgr)
|
||||
|
||||
/* ----------------------------------------------------------------------- */
|
||||
|
||||
|
||||
hawk_flt_t hawk_stdmathpow (hawk_t* hawk, hawk_flt_t x, hawk_flt_t y)
|
||||
{
|
||||
#if defined(HAWK_USE_FLTMAX) && defined(HAVE_POWQ)
|
||||
@ -763,7 +762,7 @@ static int write_log (hawk_t* hawk, int fd, const hawk_bch_t* ptr, hawk_oow_t le
|
||||
rcapa = HAWK_COUNTOF(xtn->log.out.buf) - xtn->log.out.len;
|
||||
cplen = (len >= rcapa)? rcapa: len;
|
||||
|
||||
HAWK_MEMCPY (&xtn->log.out.buf[xtn->log.out.len], ptr, cplen);
|
||||
HAWK_MEMCPY(&xtn->log.out.buf[xtn->log.out.len], ptr, cplen);
|
||||
xtn->log.out.len += cplen;
|
||||
ptr += cplen;
|
||||
len -= cplen;
|
||||
@ -789,7 +788,7 @@ static int write_log (hawk_t* hawk, int fd, const hawk_bch_t* ptr, hawk_oow_t le
|
||||
}
|
||||
else
|
||||
{
|
||||
HAWK_MEMCPY (xtn->log.out.buf, ptr, len);
|
||||
HAWK_MEMCPY(xtn->log.out.buf, ptr, len);
|
||||
xtn->log.out.len += len;
|
||||
ptr += len;
|
||||
len -= len;
|
||||
@ -1101,7 +1100,7 @@ static hawk_sio_t* open_sio_std (hawk_t* hawk, hawk_sio_std_t std, int flags)
|
||||
if (sio == HAWK_NULL)
|
||||
{
|
||||
const hawk_ooch_t* bem = hawk_backuperrmsg(hawk);
|
||||
hawk_seterrfmt(hawk, HAWK_NULL, HAWK_EOPEN, HAWK_T("unable to open %js - %js"), &sio_std_names[std], bem);
|
||||
hawk_seterrfmt(hawk, HAWK_NULL, HAWK_EOPEN, HAWK_T("unable to open %js - %js"), sio_std_names[std].ptr, bem);
|
||||
}
|
||||
return sio;
|
||||
}
|
||||
@ -1114,7 +1113,7 @@ static hawk_sio_t* open_sio_std_rtx (hawk_rtx_t* rtx, hawk_sio_std_t std, int fl
|
||||
if (sio == HAWK_NULL)
|
||||
{
|
||||
const hawk_ooch_t* bem = hawk_rtx_backuperrmsg(rtx);
|
||||
hawk_rtx_seterrfmt (rtx, HAWK_NULL, HAWK_EOPEN, HAWK_T("unable to open %js - %js"), &sio_std_names[std], bem);
|
||||
hawk_rtx_seterrfmt (rtx, HAWK_NULL, HAWK_EOPEN, HAWK_T("unable to open %js - %js"), sio_std_names[std].ptr, bem);
|
||||
}
|
||||
return sio;
|
||||
}
|
||||
@ -1260,7 +1259,7 @@ static int fill_sio_arg_unique_id (hawk_t* hawk, hawk_sio_arg_t* arg, const hawk
|
||||
tmp.ino = st.st_ino;
|
||||
tmp.dev = st.st_dev;
|
||||
|
||||
HAWK_MEMCPY (arg->unique_id, &tmp, (HAWK_SIZEOF(tmp) > HAWK_SIZEOF(arg->unique_id)? HAWK_SIZEOF(arg->unique_id): HAWK_SIZEOF(tmp)));
|
||||
HAWK_MEMCPY(arg->unique_id, &tmp, (HAWK_SIZEOF(tmp) > HAWK_SIZEOF(arg->unique_id)? HAWK_SIZEOF(arg->unique_id): HAWK_SIZEOF(tmp)));
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
Reference in New Issue
Block a user