Fix internal error in seh_cfa_offset with -O2 -fno-omit-frame-pointer
[official-gcc.git] / gcc / ada / expect.c
blobff02d49b76dbeab2e53b740007a0ee1be2fe6730
1 /****************************************************************************
2 * *
3 * GNAT RUN-TIME COMPONENTS *
4 * *
5 * E X P E C T *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 2001-2024, AdaCore *
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 ****************************************************************************/
32 #ifdef IN_RTS
33 #define POSIX
34 #include "runtime.h"
35 #include <unistd.h>
37 #else
38 #include "config.h"
39 #include "system.h"
40 #endif
42 #include "adaint.h"
43 #include <sys/types.h>
44 #include <string.h>
46 #if defined (__vxworks) && defined (__RTP__)
47 # include <wait.h>
48 #elif defined (__Lynx__)
49 /* ??? See comment in adaint.c. */
50 # define GCC_RESOURCE_H
51 # include <sys/wait.h>
52 #elif defined (__PikeOS__) || defined (__MINGW32__)
53 /* No wait.h available */
54 #else
55 #include <sys/wait.h>
56 #endif
58 /* This file provides the low level functionalities needed to implement Expect
59 capabilities in GNAT.Expect.
60 Implementations for unix and windows systems is provided.
61 Dummy stubs are also provided for other systems. */
63 #ifdef _AIX
64 /* Work around the fact that gcc/cpp does not define "__unix__" under AiX. */
65 #define __unix__
66 #endif
68 #ifdef __APPLE__
69 /* Work around the fact that gcc/cpp does not define "__unix__" on Darwin. */
70 #define __unix__
71 #endif
73 #ifdef _WIN32
75 #define WIN32_LEAN_AND_MEAN
76 #include <windows.h>
77 #include <process.h>
78 #include <signal.h>
79 #include <io.h>
80 #include "mingw32.h"
82 int
83 __gnat_waitpid (int pid)
85 HANDLE h = OpenProcess (PROCESS_ALL_ACCESS, FALSE, pid);
86 DWORD exitcode = 1;
88 if (h != NULL)
90 (void) WaitForSingleObject (h, INFINITE);
91 GetExitCodeProcess (h, &exitcode);
92 CloseHandle (h);
95 __gnat_win32_remove_handle (NULL, pid);
96 return (int) exitcode;
99 int
100 __gnat_expect_fork (void)
102 return 0;
105 void
106 __gnat_expect_portable_execvp (int *pid, char *cmd ATTRIBUTE_UNUSED,
107 char *argv[])
109 *pid = __gnat_portable_no_block_spawn (argv);
113 __gnat_pipe (int *fd)
115 HANDLE read, write;
117 CreatePipe (&read, &write, NULL, 0);
118 fd[0]=_open_osfhandle ((intptr_t)read, 0);
119 fd[1]=_open_osfhandle ((intptr_t)write, 0);
120 return 0; /* always success */
124 __gnat_expect_poll (int *fd,
125 int num_fd,
126 int timeout,
127 int *dead_process,
128 int *is_set)
130 #define MAX_DELAY 100
132 int i, delay, infinite = 0;
133 DWORD avail;
134 HANDLE handles[num_fd];
136 *dead_process = 0;
138 for (i = 0; i < num_fd; i++)
139 is_set[i] = 0;
141 for (i = 0; i < num_fd; i++)
142 handles[i] = (HANDLE) _get_osfhandle (fd [i]);
144 /* Start with small delays, and then increase them, to avoid polling too
145 much when waiting a long time */
146 delay = 5;
148 if (timeout < 0)
149 infinite = 1;
151 while (1)
153 for (i = 0; i < num_fd; i++)
155 if (!PeekNamedPipe (handles [i], NULL, 0, NULL, &avail, NULL))
157 *dead_process = i + 1;
158 return -1;
160 if (avail > 0)
162 is_set[i] = 1;
163 return 1;
167 if (!infinite && timeout <= 0)
168 return 0;
170 Sleep (delay);
171 timeout -= delay;
173 if (delay < MAX_DELAY)
174 delay += 10;
178 #elif defined (VMS)
179 #include <unistd.h>
180 #include <stdio.h>
181 #include <unixio.h>
182 #include <stdlib.h>
183 #include <string.h>
184 #include <vms/descrip.h>
185 #include <stdio.h>
186 #include <vms/stsdef.h>
187 #include <vms/iodef.h>
188 #include <signal.h>
191 __gnat_waitpid (int pid)
193 int status = 0;
195 waitpid (pid, &status, 0);
196 status = WEXITSTATUS (status);
198 return status;
202 __gnat_pipe (int *fd)
204 return pipe (fd);
208 __gnat_expect_fork (void)
210 return -1;
213 void
214 __gnat_expect_portable_execvp (int *pid, char *cmd, char *argv[])
216 *pid = (int) getpid ();
217 /* Since cmd is fully qualified, it is incorrect to call execvp */
218 execv (cmd, argv);
219 _exit (1);
223 __gnat_expect_poll (int *fd,
224 int num_fd,
225 int timeout,
226 int *dead_process,
227 int *is_set)
229 int i, num, ready = 0;
230 unsigned int status;
231 int mbxchans [num_fd];
232 struct dsc$descriptor_s mbxname;
233 struct io_status_block {
234 short int condition;
235 short int count;
236 int dev;
237 } iosb;
238 char buf [256];
240 *dead_process = 0;
242 for (i = 0; i < num_fd; i++)
243 is_set[i] = 0;
245 for (i = 0; i < num_fd; i++)
248 /* Get name of the mailbox used in the pipe */
249 getname (fd [i], buf);
251 /* Assign a channel to the mailbox */
252 if (strlen (buf) > 0)
254 mbxname.dsc$w_length = strlen (buf);
255 mbxname.dsc$b_dtype = DSC$K_DTYPE_T;
256 mbxname.dsc$b_class = DSC$K_CLASS_S;
257 mbxname.dsc$a_pointer = buf;
259 status = SYS$ASSIGN (&mbxname, &mbxchans[i], 0, 0, 0);
261 if ((status & 1) != 1)
263 ready = -1;
264 *dead_process = i + 1;
265 return ready;
270 num = timeout / 100;
272 while (1)
274 for (i = 0; i < num_fd; i++)
276 if (mbxchans[i] > 0)
279 /* Peek in the mailbox to see if there's data */
280 status = SYS$QIOW
281 (0, mbxchans[i], IO$_SENSEMODE|IO$M_READERCHECK,
282 &iosb, 0, 0, 0, 0, 0, 0, 0, 0);
284 if ((status & 1) != 1)
286 ready = -1;
287 goto deassign;
290 if (iosb.count > 0)
292 is_set[i] = 1;
293 ready = 1;
294 goto deassign;
299 if (timeout > 0 && num == 0)
301 ready = 0;
302 goto deassign;
305 usleep (100000);
306 num--;
309 deassign:
311 /* Deassign channels assigned above */
312 for (i = 0; i < num_fd; i++)
314 if (mbxchans[i] > 0)
315 status = SYS$DASSGN (mbxchans[i]);
318 return ready;
320 #elif defined (__unix__)
322 #ifdef __hpux__
323 #include <sys/ptyio.h>
324 #endif
326 #include <sys/time.h>
328 #ifndef NO_FD_SET
329 #define SELECT_MASK fd_set
330 #else /* !NO_FD_SET */
331 #ifndef _AIX
332 typedef long fd_mask;
333 #endif /* _AIX */
334 #ifdef _IBMR2
335 #define SELECT_MASK void
336 #else /* !_IBMR2 */
337 #define SELECT_MASK int
338 #endif /* !_IBMR2 */
339 #endif /* !NO_FD_SET */
342 __gnat_waitpid (int pid)
344 int status = 0;
346 if (waitpid (pid, &status, 0) == -1) {
347 return -1;
350 if (WIFEXITED (status)) {
351 status = WEXITSTATUS (status);
352 } else if (WIFSIGNALED (status)) {
353 status = WTERMSIG (status);
354 } else if (WIFSTOPPED (status)) {
355 status = WSTOPSIG (status);
358 return status;
362 __gnat_pipe (int *fd)
364 return pipe (fd);
368 __gnat_expect_fork (void)
370 int pid = fork();
371 if (pid == 0) {
372 __gnat_in_child_after_fork = 1;
374 return pid;
377 void
378 __gnat_expect_portable_execvp (int *pid, char *cmd, char *argv[])
380 *pid = (int) getpid ();
381 /* Since cmd is fully qualified, it is incorrect to call execvp */
382 execv (cmd, argv);
383 _exit (1);
387 __gnat_expect_poll (int *fd,
388 int num_fd,
389 int timeout,
390 int *dead_process,
391 int *is_set)
393 struct timeval tv;
394 SELECT_MASK rset;
395 SELECT_MASK eset;
397 int max_fd = 0;
398 int ready;
399 int i;
400 #ifdef __hpux__
401 int received;
402 #endif
404 *dead_process = 0;
406 tv.tv_sec = timeout / 1000;
407 tv.tv_usec = (timeout % 1000) * 1000;
409 do {
410 FD_ZERO (&rset);
411 FD_ZERO (&eset);
413 for (i = 0; i < num_fd; i++)
415 FD_SET (fd[i], &rset);
416 FD_SET (fd[i], &eset);
418 if (fd[i] > max_fd)
419 max_fd = fd[i];
422 ready =
423 select (max_fd + 1, &rset, NULL, &eset, timeout == -1 ? NULL : &tv);
425 if (ready > 0)
427 #ifdef __hpux__
428 received = 0;
429 #endif
431 for (i = 0; i < num_fd; i++)
433 if (FD_ISSET (fd[i], &rset))
435 is_set[i] = 1;
436 #ifdef __hpux__
437 received = 1;
438 #endif
440 else
441 is_set[i] = 0;
444 #ifdef __hpux__
445 for (i = 0; i < num_fd; i++)
447 if (FD_ISSET (fd[i], &eset))
449 struct request_info ei;
451 /* Only query and reset error state if no file descriptor
452 is ready to be read, otherwise we will be signalling a
453 died process too early */
455 if (!received)
457 ioctl (fd[i], TIOCREQCHECK, &ei);
459 if (ei.request == TIOCCLOSE)
461 ioctl (fd[i], TIOCREQSET, &ei);
462 *dead_process = i + 1;
463 return -1;
466 ioctl (fd[i], TIOCREQSET, &ei);
468 ready--;
471 #endif
473 } while (timeout == -1 && ready == 0);
475 return ready;
478 #else
481 __gnat_waitpid (int pid ATTRIBUTE_UNUSED, int sig ATTRIBUTE_UNUSED)
483 return 0;
487 __gnat_pipe (int *fd ATTRIBUTE_UNUSED)
489 return -1;
493 __gnat_expect_fork (void)
495 return -1;
498 void
499 __gnat_expect_portable_execvp (int *pid ATTRIBUTE_UNUSED,
500 char *cmd ATTRIBUTE_UNUSED,
501 char *argv[] ATTRIBUTE_UNUSED)
503 *pid = 0;
507 __gnat_expect_poll (int *fd ATTRIBUTE_UNUSED,
508 int num_fd ATTRIBUTE_UNUSED,
509 int timeout ATTRIBUTE_UNUSED,
510 int *dead_process ATTRIBUTE_UNUSED,
511 int *is_set ATTRIBUTE_UNUSED)
513 *dead_process = 0;
514 return -1;
516 #endif