c++: remove some xfails
[official-gcc.git] / gcc / ada / expect.c
blobb1889feff374c03a11b9ce17ce9043a9f6b5d0b0
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-2022, 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>
45 #ifdef __MINGW32__
46 # if OLD_MINGW
47 # include <sys/wait.h>
48 # endif
49 #elif defined (__vxworks) && defined (__RTP__)
50 # include <wait.h>
51 #elif defined (__Lynx__)
52 /* ??? See comment in adaint.c. */
53 # define GCC_RESOURCE_H
54 # include <sys/wait.h>
55 #elif defined (__PikeOS__)
56 /* No wait.h available */
57 #else
58 #include <sys/wait.h>
59 #endif
61 /* This file provides the low level functionalities needed to implement Expect
62 capabilities in GNAT.Expect.
63 Implementations for unix and windows systems is provided.
64 Dummy stubs are also provided for other systems. */
66 #ifdef _AIX
67 /* Work around the fact that gcc/cpp does not define "__unix__" under AiX. */
68 #define __unix__
69 #endif
71 #ifdef __APPLE__
72 /* Work around the fact that gcc/cpp does not define "__unix__" on Darwin. */
73 #define __unix__
74 #endif
76 #ifdef _WIN32
78 #include <windows.h>
79 #include <process.h>
80 #include <signal.h>
81 #include <io.h>
82 #include "mingw32.h"
84 int
85 __gnat_waitpid (int pid)
87 HANDLE h = OpenProcess (PROCESS_ALL_ACCESS, FALSE, pid);
88 DWORD exitcode = 1;
90 if (h != NULL)
92 (void) WaitForSingleObject (h, INFINITE);
93 GetExitCodeProcess (h, &exitcode);
94 CloseHandle (h);
97 __gnat_win32_remove_handle (NULL, pid);
98 return (int) exitcode;
102 __gnat_expect_fork (void)
104 return 0;
107 void
108 __gnat_expect_portable_execvp (int *pid, char *cmd ATTRIBUTE_UNUSED,
109 char *argv[])
111 *pid = __gnat_portable_no_block_spawn (argv);
115 __gnat_pipe (int *fd)
117 HANDLE read, write;
119 CreatePipe (&read, &write, NULL, 0);
120 fd[0]=_open_osfhandle ((intptr_t)read, 0);
121 fd[1]=_open_osfhandle ((intptr_t)write, 0);
122 return 0; /* always success */
126 __gnat_expect_poll (int *fd,
127 int num_fd,
128 int timeout,
129 int *dead_process,
130 int *is_set)
132 #define MAX_DELAY 100
134 int i, delay, infinite = 0;
135 DWORD avail;
136 HANDLE handles[num_fd];
138 *dead_process = 0;
140 for (i = 0; i < num_fd; i++)
141 is_set[i] = 0;
143 for (i = 0; i < num_fd; i++)
144 handles[i] = (HANDLE) _get_osfhandle (fd [i]);
146 /* Start with small delays, and then increase them, to avoid polling too
147 much when waiting a long time */
148 delay = 5;
150 if (timeout < 0)
151 infinite = 1;
153 while (1)
155 for (i = 0; i < num_fd; i++)
157 if (!PeekNamedPipe (handles [i], NULL, 0, NULL, &avail, NULL))
159 *dead_process = i + 1;
160 return -1;
162 if (avail > 0)
164 is_set[i] = 1;
165 return 1;
169 if (!infinite && timeout <= 0)
170 return 0;
172 Sleep (delay);
173 timeout -= delay;
175 if (delay < MAX_DELAY)
176 delay += 10;
180 #elif defined (VMS)
181 #include <unistd.h>
182 #include <stdio.h>
183 #include <unixio.h>
184 #include <stdlib.h>
185 #include <string.h>
186 #include <vms/descrip.h>
187 #include <stdio.h>
188 #include <vms/stsdef.h>
189 #include <vms/iodef.h>
190 #include <signal.h>
193 __gnat_waitpid (int pid)
195 int status = 0;
197 waitpid (pid, &status, 0);
198 status = WEXITSTATUS (status);
200 return status;
204 __gnat_pipe (int *fd)
206 return pipe (fd);
210 __gnat_expect_fork (void)
212 return -1;
215 void
216 __gnat_expect_portable_execvp (int *pid, char *cmd, char *argv[])
218 *pid = (int) getpid ();
219 /* Since cmd is fully qualified, it is incorrect to call execvp */
220 execv (cmd, argv);
221 _exit (1);
225 __gnat_expect_poll (int *fd,
226 int num_fd,
227 int timeout,
228 int *dead_process,
229 int *is_set)
231 int i, num, ready = 0;
232 unsigned int status;
233 int mbxchans [num_fd];
234 struct dsc$descriptor_s mbxname;
235 struct io_status_block {
236 short int condition;
237 short int count;
238 int dev;
239 } iosb;
240 char buf [256];
242 *dead_process = 0;
244 for (i = 0; i < num_fd; i++)
245 is_set[i] = 0;
247 for (i = 0; i < num_fd; i++)
250 /* Get name of the mailbox used in the pipe */
251 getname (fd [i], buf);
253 /* Assign a channel to the mailbox */
254 if (strlen (buf) > 0)
256 mbxname.dsc$w_length = strlen (buf);
257 mbxname.dsc$b_dtype = DSC$K_DTYPE_T;
258 mbxname.dsc$b_class = DSC$K_CLASS_S;
259 mbxname.dsc$a_pointer = buf;
261 status = SYS$ASSIGN (&mbxname, &mbxchans[i], 0, 0, 0);
263 if ((status & 1) != 1)
265 ready = -1;
266 *dead_process = i + 1;
267 return ready;
272 num = timeout / 100;
274 while (1)
276 for (i = 0; i < num_fd; i++)
278 if (mbxchans[i] > 0)
281 /* Peek in the mailbox to see if there's data */
282 status = SYS$QIOW
283 (0, mbxchans[i], IO$_SENSEMODE|IO$M_READERCHECK,
284 &iosb, 0, 0, 0, 0, 0, 0, 0, 0);
286 if ((status & 1) != 1)
288 ready = -1;
289 goto deassign;
292 if (iosb.count > 0)
294 is_set[i] = 1;
295 ready = 1;
296 goto deassign;
301 if (timeout > 0 && num == 0)
303 ready = 0;
304 goto deassign;
307 usleep (100000);
308 num--;
311 deassign:
313 /* Deassign channels assigned above */
314 for (i = 0; i < num_fd; i++)
316 if (mbxchans[i] > 0)
317 status = SYS$DASSGN (mbxchans[i]);
320 return ready;
322 #elif defined (__unix__)
324 #ifdef __hpux__
325 #include <sys/ptyio.h>
326 #endif
328 #include <sys/time.h>
330 #ifndef NO_FD_SET
331 #define SELECT_MASK fd_set
332 #else /* !NO_FD_SET */
333 #ifndef _AIX
334 typedef long fd_mask;
335 #endif /* _AIX */
336 #ifdef _IBMR2
337 #define SELECT_MASK void
338 #else /* !_IBMR2 */
339 #define SELECT_MASK int
340 #endif /* !_IBMR2 */
341 #endif /* !NO_FD_SET */
344 __gnat_waitpid (int pid)
346 int status = 0;
348 if (waitpid (pid, &status, 0) == -1) {
349 return -1;
352 if WIFEXITED (status) {
353 status = WEXITSTATUS (status);
354 } else if WIFSIGNALED (status) {
355 status = WTERMSIG (status);
356 } else if WIFSTOPPED (status) {
357 status = WSTOPSIG (status);
360 return status;
364 __gnat_pipe (int *fd)
366 return pipe (fd);
370 __gnat_expect_fork (void)
372 int pid = fork();
373 if (pid == 0) {
374 __gnat_in_child_after_fork = 1;
376 return pid;
379 void
380 __gnat_expect_portable_execvp (int *pid, char *cmd, char *argv[])
382 *pid = (int) getpid ();
383 /* Since cmd is fully qualified, it is incorrect to call execvp */
384 execv (cmd, argv);
385 _exit (1);
389 __gnat_expect_poll (int *fd,
390 int num_fd,
391 int timeout,
392 int *dead_process,
393 int *is_set)
395 struct timeval tv;
396 SELECT_MASK rset;
397 SELECT_MASK eset;
399 int max_fd = 0;
400 int ready;
401 int i;
402 #ifdef __hpux__
403 int received;
404 #endif
406 *dead_process = 0;
408 tv.tv_sec = timeout / 1000;
409 tv.tv_usec = (timeout % 1000) * 1000;
411 do {
412 FD_ZERO (&rset);
413 FD_ZERO (&eset);
415 for (i = 0; i < num_fd; i++)
417 FD_SET (fd[i], &rset);
418 FD_SET (fd[i], &eset);
420 if (fd[i] > max_fd)
421 max_fd = fd[i];
424 ready =
425 select (max_fd + 1, &rset, NULL, &eset, timeout == -1 ? NULL : &tv);
427 if (ready > 0)
429 #ifdef __hpux__
430 received = 0;
431 #endif
433 for (i = 0; i < num_fd; i++)
435 if (FD_ISSET (fd[i], &rset))
437 is_set[i] = 1;
438 #ifdef __hpux__
439 received = 1;
440 #endif
442 else
443 is_set[i] = 0;
446 #ifdef __hpux__
447 for (i = 0; i < num_fd; i++)
449 if (FD_ISSET (fd[i], &eset))
451 struct request_info ei;
453 /* Only query and reset error state if no file descriptor
454 is ready to be read, otherwise we will be signalling a
455 died process too early */
457 if (!received)
459 ioctl (fd[i], TIOCREQCHECK, &ei);
461 if (ei.request == TIOCCLOSE)
463 ioctl (fd[i], TIOCREQSET, &ei);
464 *dead_process = i + 1;
465 return -1;
468 ioctl (fd[i], TIOCREQSET, &ei);
470 ready--;
473 #endif
475 } while (timeout == -1 && ready == 0);
477 return ready;
480 #else
483 __gnat_waitpid (int pid ATTRIBUTE_UNUSED, int sig ATTRIBUTE_UNUSED)
485 return 0;
489 __gnat_pipe (int *fd ATTRIBUTE_UNUSED)
491 return -1;
495 __gnat_expect_fork (void)
497 return -1;
500 void
501 __gnat_expect_portable_execvp (int *pid ATTRIBUTE_UNUSED,
502 char *cmd ATTRIBUTE_UNUSED,
503 char *argv[] ATTRIBUTE_UNUSED)
505 *pid = 0;
509 __gnat_expect_poll (int *fd ATTRIBUTE_UNUSED,
510 int num_fd ATTRIBUTE_UNUSED,
511 int timeout ATTRIBUTE_UNUSED,
512 int *dead_process ATTRIBUTE_UNUSED,
513 int *is_set ATTRIBUTE_UNUSED)
515 *dead_process = 0;
516 return -1;
518 #endif