gbasys

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