1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * C T R L _ C *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 2002-2009, 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 3, 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. *
17 * *
18 * As a special exception under Section 7 of GPL version 3, you are granted *
19 * additional permissions described in the GCC Runtime Library Exception, *
20 * version 3.1, as published by the Free Software Foundation. *
21 * *
22 * You should have received a copy of the GNU General Public License and *
23 * a copy of the GCC Runtime Library Exception along with this program; *
24 * see the files COPYING3 and COPYING.RUNTIME respectively. If not, see *
25 * <http://www.gnu.org/licenses/>. *
26 * *
27 * GNAT was originally developed by the GNAT team at New York University. *
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
29 * *
30 ****************************************************************************/
31
32 #ifdef IN_RTS
33 #include "tconfig.h"
34 #include "tsystem.h"
35 #include <sys/stat.h>
36 #else
37 #include "config.h"
38 #include "system.h"
39 #endif
40
41 /* Services to intercept Ctrl-C */
42
43 /* __gnat_install_int_handler will install the specified handler.
44 If called for the first time, it will also save the original handler */
45 void __gnat_install_int_handler (void (*) (void));
46
47 /* __gnat_uninstall_int_handler will reinstall the original handler */
48 void __gnat_uninstall_int_handler (void);
49
50 /* POSIX implementation */
51
52 #if (defined (__unix__) || defined (_AIX) || defined (__APPLE__)) \
53 && !defined (__vxworks)
54
55 #include <signal.h>
56
57 void (*sigint_intercepted) (void) = 0;
58
59 struct sigaction original_act;
60
61 static void
__gnat_int_handler(int sig)62 __gnat_int_handler (int sig __attribute__ ((unused)))
63 {
64 if (sigint_intercepted != 0)
65 sigint_intercepted ();
66 }
67
68 /* Install handler and save original handler. */
69
70 void
__gnat_install_int_handler(void (* proc)(void))71 __gnat_install_int_handler (void (*proc) (void))
72 {
73 struct sigaction act;
74
75 if (sigint_intercepted == 0)
76 {
77 act.sa_handler = __gnat_int_handler;
78 #if defined (__Lynx__)
79 /* LynxOS does not support SA_RESTART. */
80 act.sa_flags = 0;
81 #else
82 act.sa_flags = SA_RESTART;
83 #endif
84 sigemptyset (&act.sa_mask);
85 sigaction (SIGINT, &act, &original_act);
86 }
87
88 sigint_intercepted = proc;
89 }
90
91 /* Restore original handler */
92
93 void
__gnat_uninstall_int_handler(void)94 __gnat_uninstall_int_handler (void)
95 {
96 if (sigint_intercepted != 0)
97 {
98 sigaction (SIGINT, &original_act, 0);
99 sigint_intercepted = 0;
100 }
101 }
102
103 /* Windows implementation */
104
105 #elif defined (__MINGW32__)
106
107 #include "mingw32.h"
108 #include <windows.h>
109
110 void (*sigint_intercepted) (void) = NULL;
111
112 static BOOL WINAPI
__gnat_int_handler(DWORD dwCtrlType)113 __gnat_int_handler (DWORD dwCtrlType)
114 {
115 switch (dwCtrlType)
116 {
117 case CTRL_C_EVENT:
118 case CTRL_BREAK_EVENT:
119 if (sigint_intercepted != 0)
120 {
121 sigint_intercepted ();
122 return TRUE;
123 }
124 break;
125
126 case CTRL_CLOSE_EVENT:
127 case CTRL_LOGOFF_EVENT:
128 case CTRL_SHUTDOWN_EVENT:
129 break;
130 }
131
132 return FALSE;
133 }
134
135 void
__gnat_install_int_handler(void (* proc)(void))136 __gnat_install_int_handler (void (*proc) (void))
137 {
138 if (sigint_intercepted == NULL)
139 SetConsoleCtrlHandler (__gnat_int_handler, TRUE);
140
141 sigint_intercepted = proc;
142 }
143
144 void
__gnat_uninstall_int_handler(void)145 __gnat_uninstall_int_handler (void)
146 {
147 if (sigint_intercepted != NULL)
148 SetConsoleCtrlHandler (__gnat_int_handler, FALSE);
149
150 sigint_intercepted = NULL;
151 }
152
153 /* Default implementation: do nothing */
154
155 #else
156
157 void
__gnat_install_int_handler(void (* proc)(void))158 __gnat_install_int_handler (void (*proc) (void) __attribute__ ((unused)))
159 {
160 }
161
162 void
__gnat_uninstall_int_handler(void)163 __gnat_uninstall_int_handler (void)
164 {
165 }
166 #endif
167