OSDN Git Service

Add NIOS2 support. Code from SourceyG++.
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / signal.c
1 /* Implementation of the SIGNAL and ALARM g77 intrinsics
2    Copyright (C) 2005, 2007, 2009 Free Software Foundation, Inc.
3    Contributed by François-Xavier Coudert <coudert@clipper.ens.fr>
4
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 3 of the License, or (at your option) any later version.
11
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
20
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 <http://www.gnu.org/licenses/>.  */
25
26 #include "libgfortran.h"
27
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31
32 #ifdef HAVE_SIGNAL_H
33 #include <signal.h>
34 #endif
35
36 #ifdef HAVE_INTTYPES_H
37 #include <inttypes.h>
38 #endif
39
40 #include <errno.h>
41
42 /* SIGNAL subroutine with PROCEDURE as handler  */
43 extern void signal_sub (int *, void (*)(int), int *);
44 iexport_proto(signal_sub);
45
46 void
47 signal_sub (int *number, void (*handler)(int), int *status)
48 {
49 #ifdef HAVE_SIGNAL
50   intptr_t ret;
51
52   if (status != NULL)
53     {
54       ret = (intptr_t) signal (*number, handler);
55       *status = (int) ret;
56     }
57   else
58     signal (*number, handler);
59 #else
60   errno = ENOSYS;
61   if (status != NULL)
62     *status = -1;
63 #endif
64 }
65 iexport(signal_sub);
66
67
68 /* SIGNAL subroutine with INTEGER as handler  */
69 extern void signal_sub_int (int *, int *, int *);
70 iexport_proto(signal_sub_int);
71
72 void
73 signal_sub_int (int *number, int *handler, int *status)
74 {
75 #ifdef HAVE_SIGNAL
76   intptr_t ptr = *handler, ret;
77
78   if (status != NULL)
79     {
80       ret = (intptr_t) signal (*number, (void (*)(int)) ptr);
81       *status = (int) ret;
82     }
83   else
84     signal (*number, (void (*)(int)) ptr);
85 #else
86   errno = ENOSYS;
87   if (status != NULL)
88     *status = -1;
89 #endif
90 }
91 iexport(signal_sub_int);
92
93
94 /* SIGNAL function with PROCEDURE as handler  */
95 extern int signal_func (int *, void (*)(int));
96 iexport_proto(signal_func);
97
98 int
99 signal_func (int *number, void (*handler)(int))
100 {
101   int status;
102   signal_sub (number, handler, &status);
103   return status;
104 }
105 iexport(signal_func);
106
107
108 /* SIGNAL function with INTEGER as handler  */
109 extern int signal_func_int (int *, int *);
110 iexport_proto(signal_func_int);
111
112 int
113 signal_func_int (int *number, int *handler)
114 {
115   int status;
116   signal_sub_int (number, handler, &status);
117   return status;
118 }
119 iexport(signal_func_int);
120
121
122
123 /* ALARM intrinsic with PROCEDURE as handler  */
124 extern void alarm_sub_i4 (int *, void (*)(int), GFC_INTEGER_4 *);
125 iexport_proto(alarm_sub_i4);
126
127 void
128 alarm_sub_i4 (int * seconds __attribute__ ((unused)),
129               void (*handler)(int) __attribute__ ((unused)),
130               GFC_INTEGER_4 *status)
131 {
132 #if defined (SIGALRM) && defined (HAVE_ALARM) && defined (HAVE_SIGNAL)
133   if (status != NULL)
134     {
135       if (signal (SIGALRM, handler) == SIG_ERR)
136         *status = -1;
137       else
138         *status = alarm (*seconds);
139     }
140   else
141     {
142       signal (SIGALRM, handler);
143       alarm (*seconds);
144     }
145 #else
146   errno = ENOSYS;
147   if (status != NULL)
148     *status = -1;
149 #endif
150 }
151 iexport(alarm_sub_i4);
152
153
154 extern void alarm_sub_i8 (int *, void (*)(int), GFC_INTEGER_8 *);
155 iexport_proto(alarm_sub_i8);
156
157 void
158 alarm_sub_i8 (int *seconds __attribute__ ((unused)),
159               void (*handler)(int) __attribute__ ((unused)),
160               GFC_INTEGER_8 *status)
161 {
162 #if defined (SIGALRM) && defined (HAVE_ALARM) && defined (HAVE_SIGNAL)
163   if (status != NULL)
164     {
165       if (signal (SIGALRM, handler) == SIG_ERR)
166         *status = -1;
167       else
168         *status = alarm (*seconds);
169     }
170   else
171     {
172       signal (SIGALRM, handler);
173       alarm (*seconds);
174     }
175 #else
176   errno = ENOSYS;
177   if (status != NULL)
178     *status = -1;
179 #endif
180 }
181 iexport(alarm_sub_i8);
182
183
184 /* ALARM intrinsic with INTEGER as handler  */
185 extern void alarm_sub_int_i4 (int *, int *, GFC_INTEGER_4 *);
186 iexport_proto(alarm_sub_int_i4);
187
188 void
189 alarm_sub_int_i4 (int *seconds __attribute__ ((unused)),
190                   int *handler __attribute__ ((unused)),
191                   GFC_INTEGER_4 *status)
192 {
193 #if defined (SIGALRM) && defined (HAVE_ALARM) && defined (HAVE_SIGNAL)
194   if (status != NULL)
195     {
196       if (signal (SIGALRM, (void (*)(int)) (intptr_t) *handler) == SIG_ERR)
197         *status = -1;
198       else
199         *status = alarm (*seconds);
200     }
201   else
202     {
203       signal (SIGALRM, (void (*)(int)) (intptr_t) *handler);
204       alarm (*seconds);
205     }
206 #else
207   errno = ENOSYS;
208   if (status != NULL)
209     *status = -1;
210 #endif
211 }
212 iexport(alarm_sub_int_i4);
213
214
215 extern void alarm_sub_int_i8 (int *, int *, GFC_INTEGER_8 *);
216 iexport_proto(alarm_sub_int_i8);
217
218 void
219 alarm_sub_int_i8 (int *seconds __attribute__ ((unused)),
220                   int *handler __attribute__ ((unused)),
221                   GFC_INTEGER_8 *status)
222 {
223 #if defined (SIGALRM) && defined (HAVE_ALARM) && defined (HAVE_SIGNAL)
224   if (status != NULL)
225     {
226       if (signal (SIGALRM, (void (*)(int)) (intptr_t) *handler) == SIG_ERR)
227         *status = -1;
228       else
229         *status = alarm (*seconds);
230     }
231   else
232     {
233       signal (SIGALRM, (void (*)(int)) (intptr_t) *handler);
234       alarm (*seconds);
235     }
236 #else
237   errno = ENOSYS;
238   if (status != NULL)
239     *status = -1;
240 #endif
241 }
242 iexport(alarm_sub_int_i8);
243