1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 2002-2023, Free Software Foundation, Inc. *
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. *
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. *
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/>. *
27 * GNAT was originally developed by the GNAT team at New York University. *
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
30 ****************************************************************************/
37 /* Services to intercept Ctrl-C */
39 /* __gnat_install_int_handler will install the specified handler.
40 If called for the first time, it will also save the original handler */
41 void __gnat_install_int_handler (void (*) (void));
43 /* __gnat_uninstall_int_handler will reinstall the original handler */
44 void __gnat_uninstall_int_handler (void);
46 /* POSIX implementation */
48 #if (defined (__unix__) || defined (_AIX) || defined (__APPLE__)) \
49 || defined (VMS) && !defined (__vxworks)
52 /* On VMS _gnat_handle_vms_condition gets control first, and it has to
53 resignal the Ctrl/C in order for sigaction to gain control and execute
54 the user handler routine, but in doing so propagates the condition
55 causing the program to terminate. So instead we install a dummy handler
56 routine and put the real user handler in a special global variable so
57 that __gnat_handle_vms_condition can declare an AST to asynchronously
58 execute the Ctrl/C user handler at some future time and allow
59 __gnat_handle_vms_condition to return and not be held up waiting for
60 the potentially unbounded time required to execute the Crtl/C handler. */
64 /* Lives in init.c. */
65 extern void (*__gnat_ctrl_c_handler
) (void);
70 void (*sigint_intercepted
) (void) = 0;
72 struct sigaction original_act
;
75 __gnat_int_handler (int sig
__attribute__ ((unused
)))
77 if (sigint_intercepted
!= 0)
78 sigint_intercepted ();
81 /* Install handler and save original handler. */
84 __gnat_install_int_handler (void (*proc
) (void))
88 if (sigint_intercepted
== 0)
90 act
.sa_handler
= __gnat_int_handler
;
91 #if defined (__Lynx__) || defined (VMS) || defined(__DJGPP__)
92 /* LynxOS, VMS and DJGPP do not support SA_RESTART. */
95 act
.sa_flags
= SA_RESTART
;
97 sigemptyset (&act
.sa_mask
);
98 sigaction (SIGINT
, &act
, &original_act
);
102 sigint_intercepted
= &dummy_handler
;
103 __gnat_ctrl_c_handler
= proc
;
105 sigint_intercepted
= proc
;
109 /* Restore original handler */
112 __gnat_uninstall_int_handler (void)
114 if (sigint_intercepted
!= 0)
116 sigaction (SIGINT
, &original_act
, 0);
117 sigint_intercepted
= 0;
120 if (__gnat_ctrl_c_handler
)
121 __gnat_ctrl_c_handler
= 0;
125 /* Windows implementation */
127 #elif defined (__MINGW32__)
129 #define WIN32_LEAN_AND_MEAN
133 void (*sigint_intercepted
) (void) = NULL
;
136 __gnat_int_handler (DWORD dwCtrlType
)
141 case CTRL_BREAK_EVENT
:
142 if (sigint_intercepted
!= 0)
144 sigint_intercepted ();
149 case CTRL_CLOSE_EVENT
:
150 case CTRL_LOGOFF_EVENT
:
151 case CTRL_SHUTDOWN_EVENT
:
159 __gnat_install_int_handler (void (*proc
) (void))
161 if (sigint_intercepted
== NULL
)
162 SetConsoleCtrlHandler (__gnat_int_handler
, TRUE
);
164 sigint_intercepted
= proc
;
168 __gnat_uninstall_int_handler (void)
170 if (sigint_intercepted
!= NULL
)
171 SetConsoleCtrlHandler (__gnat_int_handler
, FALSE
);
173 sigint_intercepted
= NULL
;
176 /* Default implementation: do nothing */
181 __gnat_install_int_handler (void (*proc
) (void) __attribute__ ((unused
)))
186 __gnat_uninstall_int_handler (void)