2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ada / ctrl_c.c
blobe9ec88de711b611fdf28a0017ec4836fd07f03e9
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * C T R L _ C *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 2002-2008, 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, 51 Franklin Street, Fifth Floor, *
20 * Boston, MA 02110-1301, 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 ****************************************************************************/
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
42 /* Services to intercept Ctrl-C */
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));
48 /* __gnat_uninstall_int_handler will reinstall the original handler */
49 void __gnat_uninstall_int_handler (void);
51 /* POSIX implementation */
53 #if (defined (__unix__) || defined (_AIX) || defined (__APPLE__)) \
54 && !defined (__vxworks)
56 #include <signal.h>
58 void (*sigint_intercepted) (void) = 0;
60 struct sigaction original_act;
62 static void
63 __gnat_int_handler (int sig __attribute__ ((unused)))
65 if (sigint_intercepted != 0)
66 sigint_intercepted ();
69 /* Install handler and save original handler. */
71 void
72 __gnat_install_int_handler (void (*proc) (void))
74 struct sigaction act;
76 if (sigint_intercepted == 0)
78 act.sa_handler = __gnat_int_handler;
79 #if defined (__Lynx__)
80 /* LynxOS does not support SA_RESTART. */
81 act.sa_flags = 0;
82 #else
83 act.sa_flags = SA_RESTART;
84 #endif
85 sigemptyset (&act.sa_mask);
86 sigaction (SIGINT, &act, &original_act);
89 sigint_intercepted = proc;
92 /* Restore original handler */
94 void
95 __gnat_uninstall_int_handler (void)
97 if (sigint_intercepted != 0)
99 sigaction (SIGINT, &original_act, 0);
100 sigint_intercepted = 0;
104 /* Windows implementation */
106 #elif defined (__MINGW32__)
108 #include "mingw32.h"
109 #include <windows.h>
111 void (*sigint_intercepted) (void) = NULL;
113 static BOOL WINAPI
114 __gnat_int_handler (DWORD dwCtrlType)
116 switch (dwCtrlType)
118 case CTRL_C_EVENT:
119 case CTRL_BREAK_EVENT:
120 if (sigint_intercepted != 0)
122 sigint_intercepted ();
123 return TRUE;
125 break;
127 case CTRL_CLOSE_EVENT:
128 case CTRL_LOGOFF_EVENT:
129 case CTRL_SHUTDOWN_EVENT:
130 break;
133 return FALSE;
136 void
137 __gnat_install_int_handler (void (*proc) (void))
139 if (sigint_intercepted == NULL)
140 SetConsoleCtrlHandler (__gnat_int_handler, TRUE);
142 sigint_intercepted = proc;
145 void
146 __gnat_uninstall_int_handler (void)
148 if (sigint_intercepted != NULL)
149 SetConsoleCtrlHandler (__gnat_int_handler, FALSE);
151 sigint_intercepted = NULL;
154 /* Default implementation: do nothing */
156 #else
158 void
159 __gnat_install_int_handler (void (*proc) (void) __attribute__ ((unused)))
163 void
164 __gnat_uninstall_int_handler (void)
167 #endif