OSDN Git Service

2003-10-21 Arnaud Charlet <charlet@act-europe.fr>
[pf3gnuchains/gcc-fork.git] / gcc / ada / ctrl_c.c
1 /****************************************************************************
2  *                                                                          *
3  *                         GNAT COMPILER COMPONENTS                         *
4  *                                                                          *
5  *                               C T R L _ C                                *
6  *                                                                          *
7  *                          C Implementation File                           *
8  *                                                                          *
9  *        Copyright (C) 2002-2003, Free Software Foundation, Inc.           *
10  *                                                                          *
11  * GNAT is free software;  you can  redistribute it  and/or modify it under *
12  * terms of the  GNU General Public License as published  by the Free Soft- *
13  * ware  Foundation;  either version 2,  or (at your option) any later ver- *
14  * sion.  GNAT is distributed in the hope that it will be useful, but WITH- *
15  * OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY *
16  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License *
17  * for  more details.  You should have  received  a copy of the GNU General *
18  * Public License  distributed with GNAT;  see file COPYING.  If not, write *
19  * to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, *
20  * MA 02111-1307, USA.                                                      *
21  *                                                                          *
22  * As a  special  exception,  if you  link  this file  with other  files to *
23  * produce an executable,  this file does not by itself cause the resulting *
24  * executable to be covered by the GNU General Public License. This except- *
25  * ion does not  however invalidate  any other reasons  why the  executable *
26  * file might be covered by the  GNU Public License.                        *
27  *                                                                          *
28  * GNAT was originally developed  by the GNAT team at  New York University. *
29  * Extensive contributions were provided by Ada Core Technologies Inc.      *
30  *                                                                          *
31  ****************************************************************************/
32
33 #ifdef IN_RTS
34 #include "tconfig.h"
35 #include "tsystem.h"
36 #include <sys/stat.h>
37 #else
38 #include "config.h"
39 #include "system.h"
40 #endif
41
42 /* Services to intercept Ctrl-C */
43
44 /* __gnat_install_int_handler will install the specified handler.
45    If called for the first time, it will also save the original handler */
46 void __gnat_install_int_handler (void (*) (void));
47
48 /* __gnat_uninstall_int_handler will reinstall the original handler */
49 void __gnat_uninstall_int_handler (void);
50
51 static void __gnat_int_handler (int);
52
53 /* POSIX implementation */
54
55 #if (defined (_AIX) || defined (unix)) && !defined (__vxworks)
56
57 #include <signal.h>
58
59 void (*sigint_intercepted) (void) = 0;
60
61 struct sigaction original_act;
62
63 static void
64 __gnat_int_handler (int sig __attribute__ ((unused)))
65 {
66   if (sigint_intercepted != 0)
67     sigint_intercepted ();
68 }
69
70 /* Install handler and save original handler. */
71
72 void
73 __gnat_install_int_handler (void (*proc) (void))
74 {
75   struct sigaction act;
76
77   if (sigint_intercepted == 0)
78     {
79       act.sa_handler = __gnat_int_handler;
80       act.sa_flags = SA_RESTART;
81       sigemptyset (&act.sa_mask);
82       sigaction (SIGINT, &act, &original_act);
83     }
84
85   sigint_intercepted = proc;
86 }
87
88 /* Restore original handler */
89
90 void
91 __gnat_uninstall_int_handler (void)
92 {
93  if (sigint_intercepted != 0)
94    {
95      sigaction (SIGINT, &original_act, 0);
96      sigint_intercepted = 0;
97    }
98 }
99
100 /* Windows implementation */
101
102 #elif defined (__MINGW32__)
103
104 #include "mingw32.h"
105 #include <windows.h>
106
107 void (*sigint_intercepted) () = NULL;
108
109 static BOOL WINAPI
110 __gnat_int_handler  (DWORD dwCtrlType)
111 {
112   switch (dwCtrlType)
113     {
114     case CTRL_C_EVENT:
115     case CTRL_BREAK_EVENT:
116       if (sigint_intercepted != 0)
117         sigint_intercepted ();
118       break;
119
120     case CTRL_CLOSE_EVENT:
121     case CTRL_LOGOFF_EVENT:
122     case CTRL_SHUTDOWN_EVENT:
123       break;
124     }
125 }
126
127 void
128 __gnat_install_int_handler (void (*proc) (void))
129 {
130   if (sigint_intercepted == NULL)
131     SetConsoleCtrlHandler (__gnat_int_handler, TRUE);
132
133   sigint_intercepted = proc;
134 }
135
136 void
137 __gnat_uninstall_int_handler ()
138 {
139   if (sigint_intercepted != NULL)
140     SetConsoleCtrlHandler (__gnat_int_handler, FALSE);
141
142   sigint_intercepted = NULL;
143 }
144
145 /* Default implementation: do nothing */
146
147 #else
148
149 void
150 __gnat_install_int_handler (void (*proc) (void) __attribute__ ((unused)))
151 {
152 }
153
154 void
155 __gnat_uninstall_int_handler ()
156 {
157 }
158 #endif