Add support for external html docs
[maxima.git] / interfaces / xmaxima / win32 / winkill_lib.c
blob6d53043ab3569665dfcee8c79b50334eab445790
1 // Written by William Schelter.
2 // Additional work from Davind Billinghurst and Andrej Vopodivec.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 // SPDX-License-Identifier: GPL-2.0+
22 // winkill_lib is a library that offers a shared memory location a GUI can send
23 // an interrupt signal to on MS Windows.
24 //
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <windows.h>
32 #define signal_mask(n) (1 << (n))
34 static struct {
35 HANDLE handle;
36 LPVOID address;
37 DWORD length ;
38 char name[20] ;
39 } sharedMemory = {0,0,0x10000} ;
43 typedef struct {int signumber; char *name ;} sigNameStruct;
44 sigNameStruct sigNames[]=
46 #ifdef SIGHUP
47 { SIGHUP, "HUP" }, /* Hangup (POSIX). */
48 #endif
49 #ifdef SIGINT
50 { SIGINT, "INT" }, /* Interrupt (ANSI). */
51 #endif
52 #ifdef SIGQUIT
53 { SIGQUIT, "QUIT" }, /* Quit (POSIX). */
54 #endif
55 #ifdef SIGILL
56 { SIGILL, "ILL" }, /* Illegal instruction (ANSI). */
57 #endif
58 #ifdef SIGTRAP
59 { SIGTRAP, "TRAP" }, /* Trace trap (POSIX). */
60 #endif
61 #ifdef SIGABRT
62 { SIGABRT, "ABRT" }, /* Abort (ANSI). */
63 #endif
64 #ifdef SIGIOT
65 { SIGIOT, "IOT" }, /* IOT trap (4.2 BSD). */
66 #endif
67 #ifdef SIGBUS
68 { SIGBUS, "BUS" }, /* BUS error (4.2 BSD). */
69 #endif
70 #ifdef SIGFPE
71 { SIGFPE, "FPE" }, /* Floating-point exception (ANSI). */
72 #endif
73 #ifdef SIGKILL
74 { SIGKILL, "KILL" }, /* Kill, unblockable (POSIX). */
75 #endif
76 #ifdef SIGUSR1
77 { SIGUSR1, "USR1" }, /* User-defined signal 1 (POSIX). */
78 #endif
79 #ifdef SIGSEGV
80 { SIGSEGV, "SEGV" }, /* Segmentation violation (ANSI). */
81 #endif
82 #ifdef SIGUSR2
83 { SIGUSR2, "USR2" }, /* User-defined signal 2 (POSIX). */
84 #endif
85 #ifdef SIGPIPE
86 { SIGPIPE, "PIPE" }, /* Broken pipe (POSIX). */
87 #endif
88 #ifdef SIGALRM
89 { SIGALRM, "ALRM" }, /* Alarm clock (POSIX). */
90 #endif
91 #ifdef SIGTERM
92 { SIGTERM, "TERM" }, /* Termination (ANSI). */
93 #endif
94 #ifdef SIGSTKFLT
95 { SIGSTKFLT, "STKFLT" }, /* Stack fault. */
96 #endif
97 #ifdef SIGCLD
98 { SIGCLD, "CLD" }, /* Same as SIGCHLD (System V). */
99 #endif
100 #ifdef SIGCHLD
101 { SIGCHLD, "CHLD" }, /* Child status has changed (POSIX). */
102 #endif
103 #ifdef SIGCONT
104 { SIGCONT, "CONT" }, /* Continue (POSIX). */
105 #endif
106 #ifdef SIGSTOP
107 { SIGSTOP, "STOP" }, /* Stop, unblockable (POSIX). */
108 #endif
109 #ifdef SIGTSTP
110 { SIGTSTP, "TSTP" }, /* Keyboard stop (POSIX). */
111 #endif
112 #ifdef SIGTTIN
113 { SIGTTIN, "TTIN" }, /* Background read from tty (POSIX). */
114 #endif
115 #ifdef SIGTTOU
116 { SIGTTOU, "TTOU" }, /* Background write to tty (POSIX). */
117 #endif
118 #ifdef SIGURG
119 { SIGURG, "URG" }, /* Urgent condition on socket (4.2 BSD). */
120 #endif
121 #ifdef SIGXCPU
122 { SIGXCPU, "XCPU" }, /* CPU limit exceeded (4.2 BSD). */
123 #endif
124 #ifdef SIGXFSZ
125 { SIGXFSZ, "XFSZ" }, /* File size limit exceeded (4.2 BSD). */
126 #endif
127 #ifdef SIGVTALRM
128 { SIGVTALRM, "VTALRM" }, /* Virtual alarm clock (4.2 BSD). */
129 #endif
130 #ifdef SIGPROF
131 { SIGPROF, "PROF" }, /* Profiling alarm clock (4.2 BSD). */
132 #endif
133 #ifdef SIGWINCH
134 { SIGWINCH, "WINCH" }, /* Window size change (4.3 BSD, Sun). */
135 #endif
136 #ifdef SIGPOLL
137 { SIGPOLL, "POLL" }, /* Pollable event occurred (System V). */
138 #endif
139 #ifdef SIGIO
140 { SIGIO, "IO" }, /* I/O now possible (4.2 BSD). */
141 #endif
142 #ifdef SIGPWR
143 { SIGPWR, "PWR" }, /* Power failure restart (System V). */
144 #endif
145 #ifdef SIGSYS
146 { SIGSYS, "SYS" },
147 #endif
148 { 0,0}
151 void close_shared_memory()
153 if (sharedMemory.handle)
154 CloseHandle(sharedMemory.handle);
155 sharedMemory.handle = NULL;
156 if (sharedMemory.address)
157 UnmapViewOfFile(sharedMemory.address);
158 sharedMemory.address = NULL;
161 int is_shared_memory_initialised = FALSE;
163 int check_shared_memory()
165 return is_shared_memory_initialised;
168 void print_shared_memory_name()
170 if (is_shared_memory_initialised)
171 puts(sharedMemory.name);
174 void init_shared_memory (void)
176 if ( ! is_shared_memory_initialised ) {
177 sprintf ( sharedMemory.name, "maxima-%d", getpid());
179 is_shared_memory_initialised = TRUE;
181 sharedMemory.handle =
182 CreateFileMapping ( (HANDLE)-1,
183 NULL,
184 PAGE_READWRITE,
186 sharedMemory.length,
187 TEXT (sharedMemory.name) );
189 if ( sharedMemory.handle == NULL ) {
190 is_shared_memory_initialised = FALSE;
193 sharedMemory.address =
194 MapViewOfFile(sharedMemory.handle, /* Handle to mapping object. */
195 FILE_MAP_WRITE, /* Read/write permission */
196 0, /* Max. object size. */
197 0, /* Size of hFile. */
198 0); /* Map entire file. */
200 if ( sharedMemory.address == NULL ) {
201 is_shared_memory_initialised = FALSE;
204 atexit ( close_shared_memory );
208 int read_shared_memory(void)
210 int *at;
212 if (! is_shared_memory_initialised )
213 return 0;
215 at = (int *)(sharedMemory.address);
217 if (*at & signal_mask(SIGINT))
218 return 1;
219 if (*at & signal_mask(SIGTERM))
220 return 2;
221 return 0;
224 int read_sm_sigterm(void)
226 int *at;
228 if (! is_shared_memory_initialised )
229 return 0;
231 at = (int *)(sharedMemory.address);
233 if (*at & signal_mask(SIGTERM))
234 return 1;
236 return 0;
239 int read_sm_sigint(void)
241 int *at;
243 if (! is_shared_memory_initialised )
244 return 0;
246 at = (int *)(sharedMemory.address);
248 if (*at & signal_mask(SIGINT))
249 return 1;
251 return 0;
254 void reset_shared_memory(void)
256 int *at;
258 if (! is_shared_memory_initialised )
259 return ;
261 at = (int *)(sharedMemory.address);
263 *at = 0;
266 void reset_sm_sigint(void)
268 int *at;
270 if (! is_shared_memory_initialised )
271 return ;
273 at = (int *)(sharedMemory.address);
275 *at &= ~signal_mask(SIGINT);
278 void reset_sm_sigterm(void)
280 int *at;
282 if (! is_shared_memory_initialised )
283 return ;
285 at = (int *)(sharedMemory.address);
287 *at &= ~signal_mask(SIGTERM);