annotate gcc/ada/ctrl_c.c @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents 84e7813d76e9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /****************************************************************************
kono
parents:
diff changeset
2 * *
kono
parents:
diff changeset
3 * GNAT COMPILER COMPONENTS *
kono
parents:
diff changeset
4 * *
kono
parents:
diff changeset
5 * C T R L _ C *
kono
parents:
diff changeset
6 * *
kono
parents:
diff changeset
7 * C Implementation File *
kono
parents:
diff changeset
8 * *
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
9 * Copyright (C) 2002-2019, Free Software Foundation, Inc. *
111
kono
parents:
diff changeset
10 * *
kono
parents:
diff changeset
11 * GNAT is free software; you can redistribute it and/or modify it under *
kono
parents:
diff changeset
12 * terms of the GNU General Public License as published by the Free Soft- *
kono
parents:
diff changeset
13 * ware Foundation; either version 3, or (at your option) any later ver- *
kono
parents:
diff changeset
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
kono
parents:
diff changeset
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
kono
parents:
diff changeset
16 * or FITNESS FOR A PARTICULAR PURPOSE. *
kono
parents:
diff changeset
17 * *
kono
parents:
diff changeset
18 * As a special exception under Section 7 of GPL version 3, you are granted *
kono
parents:
diff changeset
19 * additional permissions described in the GCC Runtime Library Exception, *
kono
parents:
diff changeset
20 * version 3.1, as published by the Free Software Foundation. *
kono
parents:
diff changeset
21 * *
kono
parents:
diff changeset
22 * You should have received a copy of the GNU General Public License and *
kono
parents:
diff changeset
23 * a copy of the GCC Runtime Library Exception along with this program; *
kono
parents:
diff changeset
24 * see the files COPYING3 and COPYING.RUNTIME respectively. If not, see *
kono
parents:
diff changeset
25 * <http://www.gnu.org/licenses/>. *
kono
parents:
diff changeset
26 * *
kono
parents:
diff changeset
27 * GNAT was originally developed by the GNAT team at New York University. *
kono
parents:
diff changeset
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
kono
parents:
diff changeset
29 * *
kono
parents:
diff changeset
30 ****************************************************************************/
kono
parents:
diff changeset
31
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
32 #ifndef IN_RTS
111
kono
parents:
diff changeset
33 #include "config.h"
kono
parents:
diff changeset
34 #include "system.h"
kono
parents:
diff changeset
35 #endif
kono
parents:
diff changeset
36
kono
parents:
diff changeset
37 /* Services to intercept Ctrl-C */
kono
parents:
diff changeset
38
kono
parents:
diff changeset
39 /* __gnat_install_int_handler will install the specified handler.
kono
parents:
diff changeset
40 If called for the first time, it will also save the original handler */
kono
parents:
diff changeset
41 void __gnat_install_int_handler (void (*) (void));
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 /* __gnat_uninstall_int_handler will reinstall the original handler */
kono
parents:
diff changeset
44 void __gnat_uninstall_int_handler (void);
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 /* POSIX implementation */
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 #if (defined (__unix__) || defined (_AIX) || defined (__APPLE__)) \
kono
parents:
diff changeset
49 || defined (VMS) && !defined (__vxworks)
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 #ifdef VMS
kono
parents:
diff changeset
52 /* On VMS _gnat_handle_vms_condition gets control first, and it has to
kono
parents:
diff changeset
53 resignal the Ctrl/C in order for sigaction to gain control and execute
kono
parents:
diff changeset
54 the user handler routine, but in doing so propagates the condition
kono
parents:
diff changeset
55 causing the program to terminate. So instead we install a dummy handler
kono
parents:
diff changeset
56 routine and put the real user handler in a special global variable so
kono
parents:
diff changeset
57 that __gnat_handle_vms_condition can declare an AST to asynchronously
kono
parents:
diff changeset
58 execute the Ctrl/C user handler at some future time and allow
kono
parents:
diff changeset
59 __gnat_handle_vms_condition to return and not be held up waiting for
kono
parents:
diff changeset
60 the potentially unbounded time required to execute the Crtl/C handler. */
kono
parents:
diff changeset
61 void
kono
parents:
diff changeset
62 dummy_handler () {}
kono
parents:
diff changeset
63
kono
parents:
diff changeset
64 /* Lives in init.c. */
kono
parents:
diff changeset
65 extern void (*__gnat_ctrl_c_handler) (void);
kono
parents:
diff changeset
66 #endif
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 #include <signal.h>
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70 void (*sigint_intercepted) (void) = 0;
kono
parents:
diff changeset
71
kono
parents:
diff changeset
72 struct sigaction original_act;
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 static void
kono
parents:
diff changeset
75 __gnat_int_handler (int sig __attribute__ ((unused)))
kono
parents:
diff changeset
76 {
kono
parents:
diff changeset
77 if (sigint_intercepted != 0)
kono
parents:
diff changeset
78 sigint_intercepted ();
kono
parents:
diff changeset
79 }
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81 /* Install handler and save original handler. */
kono
parents:
diff changeset
82
kono
parents:
diff changeset
83 void
kono
parents:
diff changeset
84 __gnat_install_int_handler (void (*proc) (void))
kono
parents:
diff changeset
85 {
kono
parents:
diff changeset
86 struct sigaction act;
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 if (sigint_intercepted == 0)
kono
parents:
diff changeset
89 {
kono
parents:
diff changeset
90 act.sa_handler = __gnat_int_handler;
kono
parents:
diff changeset
91 #if defined (__Lynx__) || defined (VMS) || defined(__DJGPP__)
kono
parents:
diff changeset
92 /* LynxOS, VMS and DJGPP do not support SA_RESTART. */
kono
parents:
diff changeset
93 act.sa_flags = 0;
kono
parents:
diff changeset
94 #else
kono
parents:
diff changeset
95 act.sa_flags = SA_RESTART;
kono
parents:
diff changeset
96 #endif
kono
parents:
diff changeset
97 sigemptyset (&act.sa_mask);
kono
parents:
diff changeset
98 sigaction (SIGINT, &act, &original_act);
kono
parents:
diff changeset
99 }
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 #ifdef VMS
kono
parents:
diff changeset
102 sigint_intercepted = &dummy_handler;
kono
parents:
diff changeset
103 __gnat_ctrl_c_handler = proc;
kono
parents:
diff changeset
104 #else
kono
parents:
diff changeset
105 sigint_intercepted = proc;
kono
parents:
diff changeset
106 #endif
kono
parents:
diff changeset
107 }
kono
parents:
diff changeset
108
kono
parents:
diff changeset
109 /* Restore original handler */
kono
parents:
diff changeset
110
kono
parents:
diff changeset
111 void
kono
parents:
diff changeset
112 __gnat_uninstall_int_handler (void)
kono
parents:
diff changeset
113 {
kono
parents:
diff changeset
114 if (sigint_intercepted != 0)
kono
parents:
diff changeset
115 {
kono
parents:
diff changeset
116 sigaction (SIGINT, &original_act, 0);
kono
parents:
diff changeset
117 sigint_intercepted = 0;
kono
parents:
diff changeset
118 }
kono
parents:
diff changeset
119 #ifdef VMS
kono
parents:
diff changeset
120 if (__gnat_ctrl_c_handler)
kono
parents:
diff changeset
121 __gnat_ctrl_c_handler = 0;
kono
parents:
diff changeset
122 #endif
kono
parents:
diff changeset
123 }
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 /* Windows implementation */
kono
parents:
diff changeset
126
kono
parents:
diff changeset
127 #elif defined (__MINGW32__)
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 #include "mingw32.h"
kono
parents:
diff changeset
130 #include <windows.h>
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132 void (*sigint_intercepted) (void) = NULL;
kono
parents:
diff changeset
133
kono
parents:
diff changeset
134 static BOOL WINAPI
kono
parents:
diff changeset
135 __gnat_int_handler (DWORD dwCtrlType)
kono
parents:
diff changeset
136 {
kono
parents:
diff changeset
137 switch (dwCtrlType)
kono
parents:
diff changeset
138 {
kono
parents:
diff changeset
139 case CTRL_C_EVENT:
kono
parents:
diff changeset
140 case CTRL_BREAK_EVENT:
kono
parents:
diff changeset
141 if (sigint_intercepted != 0)
kono
parents:
diff changeset
142 {
kono
parents:
diff changeset
143 sigint_intercepted ();
kono
parents:
diff changeset
144 return TRUE;
kono
parents:
diff changeset
145 }
kono
parents:
diff changeset
146 break;
kono
parents:
diff changeset
147
kono
parents:
diff changeset
148 case CTRL_CLOSE_EVENT:
kono
parents:
diff changeset
149 case CTRL_LOGOFF_EVENT:
kono
parents:
diff changeset
150 case CTRL_SHUTDOWN_EVENT:
kono
parents:
diff changeset
151 break;
kono
parents:
diff changeset
152 }
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 return FALSE;
kono
parents:
diff changeset
155 }
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 void
kono
parents:
diff changeset
158 __gnat_install_int_handler (void (*proc) (void))
kono
parents:
diff changeset
159 {
kono
parents:
diff changeset
160 if (sigint_intercepted == NULL)
kono
parents:
diff changeset
161 SetConsoleCtrlHandler (__gnat_int_handler, TRUE);
kono
parents:
diff changeset
162
kono
parents:
diff changeset
163 sigint_intercepted = proc;
kono
parents:
diff changeset
164 }
kono
parents:
diff changeset
165
kono
parents:
diff changeset
166 void
kono
parents:
diff changeset
167 __gnat_uninstall_int_handler (void)
kono
parents:
diff changeset
168 {
kono
parents:
diff changeset
169 if (sigint_intercepted != NULL)
kono
parents:
diff changeset
170 SetConsoleCtrlHandler (__gnat_int_handler, FALSE);
kono
parents:
diff changeset
171
kono
parents:
diff changeset
172 sigint_intercepted = NULL;
kono
parents:
diff changeset
173 }
kono
parents:
diff changeset
174
kono
parents:
diff changeset
175 /* Default implementation: do nothing */
kono
parents:
diff changeset
176
kono
parents:
diff changeset
177 #else
kono
parents:
diff changeset
178
kono
parents:
diff changeset
179 void
kono
parents:
diff changeset
180 __gnat_install_int_handler (void (*proc) (void) __attribute__ ((unused)))
kono
parents:
diff changeset
181 {
kono
parents:
diff changeset
182 }
kono
parents:
diff changeset
183
kono
parents:
diff changeset
184 void
kono
parents:
diff changeset
185 __gnat_uninstall_int_handler (void)
kono
parents:
diff changeset
186 {
kono
parents:
diff changeset
187 }
kono
parents:
diff changeset
188 #endif