Fix typo in t-dimode
[official-gcc.git] / gcc / ada / expect.c
bloba92c465a473bacc5a33ca4567e728f19724d9547
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-2021, 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 waitpid (pid, &status, 0);
349 status = WEXITSTATUS (status);
351 return status;
355 __gnat_pipe (int *fd)
357 return pipe (fd);
361 __gnat_expect_fork (void)
363 int pid = fork();
364 if (pid == 0) {
365 __gnat_in_child_after_fork = 1;
367 return pid;
370 void
371 __gnat_expect_portable_execvp (int *pid, char *cmd, char *argv[])
373 *pid = (int) getpid ();
374 /* Since cmd is fully qualified, it is incorrect to call execvp */
375 execv (cmd, argv);
376 _exit (1);
380 __gnat_expect_poll (int *fd,
381 int num_fd,
382 int timeout,
383 int *dead_process,
384 int *is_set)
386 struct timeval tv;
387 SELECT_MASK rset;
388 SELECT_MASK eset;
390 int max_fd = 0;
391 int ready;
392 int i;
393 #ifdef __hpux__
394 int received;
395 #endif
397 *dead_process = 0;
399 tv.tv_sec = timeout / 1000;
400 tv.tv_usec = (timeout % 1000) * 1000;
402 do {
403 FD_ZERO (&rset);
404 FD_ZERO (&eset);
406 for (i = 0; i < num_fd; i++)
408 FD_SET (fd[i], &rset);
409 FD_SET (fd[i], &eset);
411 if (fd[i] > max_fd)
412 max_fd = fd[i];
415 ready =
416 select (max_fd + 1, &rset, NULL, &eset, timeout == -1 ? NULL : &tv);
418 if (ready > 0)
420 #ifdef __hpux__
421 received = 0;
422 #endif
424 for (i = 0; i < num_fd; i++)
426 if (FD_ISSET (fd[i], &rset))
428 is_set[i] = 1;
429 #ifdef __hpux__
430 received = 1;
431 #endif
433 else
434 is_set[i] = 0;
437 #ifdef __hpux__
438 for (i = 0; i < num_fd; i++)
440 if (FD_ISSET (fd[i], &eset))
442 struct request_info ei;
444 /* Only query and reset error state if no file descriptor
445 is ready to be read, otherwise we will be signalling a
446 died process too early */
448 if (!received)
450 ioctl (fd[i], TIOCREQCHECK, &ei);
452 if (ei.request == TIOCCLOSE)
454 ioctl (fd[i], TIOCREQSET, &ei);
455 *dead_process = i + 1;
456 return -1;
459 ioctl (fd[i], TIOCREQSET, &ei);
461 ready--;
464 #endif
466 } while (timeout == -1 && ready == 0);
468 return ready;
471 #else
474 __gnat_waitpid (int pid ATTRIBUTE_UNUSED, int sig ATTRIBUTE_UNUSED)
476 return 0;
480 __gnat_pipe (int *fd ATTRIBUTE_UNUSED)
482 return -1;
486 __gnat_expect_fork (void)
488 return -1;
491 void
492 __gnat_expect_portable_execvp (int *pid ATTRIBUTE_UNUSED,
493 char *cmd ATTRIBUTE_UNUSED,
494 char *argv[] ATTRIBUTE_UNUSED)
496 *pid = 0;
500 __gnat_expect_poll (int *fd ATTRIBUTE_UNUSED,
501 int num_fd ATTRIBUTE_UNUSED,
502 int timeout ATTRIBUTE_UNUSED,
503 int *dead_process ATTRIBUTE_UNUSED,
504 int *is_set ATTRIBUTE_UNUSED)
506 *dead_process = 0;
507 return -1;
509 #endif