gbasys

view src/signal.c @ 12:a6ddf338a111

line clipping
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 01 Feb 2016 04:41:27 +0200
parents 06726f0b8cd3
children
line source
1 /*
2 gbasys - a gameboy advance hardware abstraction library
3 Copyright (C) 2004-2014 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "signal.h"
19 #include "intr.h"
21 static void sig_invalid_handler(int signum) {
22 panic("signal error");
23 }
25 static sighandler_t signal_handler[SIG_MAX];
26 static sighandler_t default_sig_handler[SIG_MAX];
27 static volatile int wait_for_signal;
28 static sighandler_t saved_signal;
30 void sig_init(void) {
31 int i;
33 for(i=0; i<SIG_MAX; i++) {
34 default_sig_handler[i] = sig_invalid_handler;
35 }
37 default_sig_handler[SIGALRM] = SIG_IGN;
38 default_sig_handler[SIGUSR1] = SIG_IGN;
39 default_sig_handler[SIGUSR2] = SIG_IGN;
40 default_sig_handler[SIGIO] = SIG_IGN;
41 default_sig_handler[SIGTTIN] = SIG_IGN;
42 for(i=0; i<SIG_MAX; i++) {
43 signal_handler[i] = default_sig_handler[i];
44 }
45 }
47 sighandler_t signal(int signum, sighandler_t handler) {
48 sighandler_t prev = signal_handler[signum];
50 signal_handler[signum] = handler == SIG_IGN ? 0 : (handler == SIG_DFL ? default_sig_handler[signum] : handler);
52 return prev;
53 }
55 int raise(int signum) {
56 if(signal_handler[signum] != SIG_IGN) {
57 signal_handler[signum](signum);
58 wait_for_signal = 0;
59 }
60 return 0;
61 }
63 int pause(void) {
64 clr_int();
65 wait_for_signal = 1;
66 set_int();
68 while(wait_for_signal);
70 /*errno = EINTR;*/
71 return -1;
72 }
74 void save_signal(int signum) {
75 saved_signal = signal_handler[signum];
76 }
78 void restore_signal(int signum) {
79 signal_handler[signum] = saved_signal;
80 }
82 sighandler_t signal_func(int signum)
83 {
84 return signal_handler[signum];
85 }