1 /****************************************************************************
3 * GNAT RUN-TIME COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 2001-2018, AdaCore *
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. *
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. *
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/>. *
27 * GNAT was originally developed by the GNAT team at New York University. *
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
30 ****************************************************************************/
32 #ifdef __alpha_vxworks
45 #include <sys/types.h>
49 # include <sys/wait.h>
51 #elif defined (__vxworks) && defined (__RTP__)
53 #elif defined (__Lynx__)
54 /* ??? See comment in adaint.c. */
55 # define GCC_RESOURCE_H
56 # include <sys/wait.h>
57 #elif defined (__PikeOS__)
58 /* No wait.h available */
63 /* This file provides the low level functionalities needed to implement Expect
64 capabilities in GNAT.Expect.
65 Implementations for unix and windows systems is provided.
66 Dummy stubs are also provided for other systems. */
69 /* Work around the fact that gcc/cpp does not define "__unix__" under AiX. */
74 /* Work around the fact that gcc/cpp does not define "__unix__" on Darwin. */
87 __gnat_waitpid (int pid
)
89 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
95 res
= WaitForSingleObject (h
, INFINITE
);
96 GetExitCodeProcess (h
, &exitcode
);
100 __gnat_win32_remove_handle (NULL
, pid
);
101 return (int) exitcode
;
105 __gnat_expect_fork (void)
111 __gnat_expect_portable_execvp (int *pid
, char *cmd
, char *argv
[])
113 *pid
= __gnat_portable_no_block_spawn (argv
);
117 __gnat_pipe (int *fd
)
121 CreatePipe (&read
, &write
, NULL
, 0);
122 fd
[0]=_open_osfhandle ((intptr_t)read
, 0);
123 fd
[1]=_open_osfhandle ((intptr_t)write
, 0);
124 return 0; /* always success */
128 __gnat_expect_poll (int *fd
,
134 #define MAX_DELAY 100
136 int i
, delay
, infinite
= 0;
138 HANDLE handles
[num_fd
];
142 for (i
= 0; i
< num_fd
; i
++)
145 for (i
= 0; i
< num_fd
; i
++)
146 handles
[i
] = (HANDLE
) _get_osfhandle (fd
[i
]);
148 /* Start with small delays, and then increase them, to avoid polling too
149 much when waiting a long time */
157 for (i
= 0; i
< num_fd
; i
++)
159 if (!PeekNamedPipe (handles
[i
], NULL
, 0, NULL
, &avail
, NULL
))
161 *dead_process
= i
+ 1;
171 if (!infinite
&& timeout
<= 0)
177 if (delay
< MAX_DELAY
)
188 #include <vms/descrip.h>
190 #include <vms/stsdef.h>
191 #include <vms/iodef.h>
195 __gnat_waitpid (int pid
)
199 waitpid (pid
, &status
, 0);
200 status
= WEXITSTATUS (status
);
206 __gnat_pipe (int *fd
)
212 __gnat_expect_fork (void)
218 __gnat_expect_portable_execvp (int *pid
, char *cmd
, char *argv
[])
220 *pid
= (int) getpid ();
221 /* Since cmd is fully qualified, it is incorrect to call execvp */
227 __gnat_expect_poll (int *fd
,
233 int i
, num
, ready
= 0;
235 int mbxchans
[num_fd
];
236 struct dsc$descriptor_s mbxname
;
237 struct io_status_block
{
246 for (i
= 0; i
< num_fd
; i
++)
249 for (i
= 0; i
< num_fd
; i
++)
252 /* Get name of the mailbox used in the pipe */
253 getname (fd
[i
], buf
);
255 /* Assign a channel to the mailbox */
256 if (strlen (buf
) > 0)
258 mbxname
.dsc$w_length
= strlen (buf
);
259 mbxname
.dsc$b_dtype
= DSC$K_DTYPE_T
;
260 mbxname
.dsc$b_class
= DSC$K_CLASS_S
;
261 mbxname
.dsc$a_pointer
= buf
;
263 status
= SYS$
ASSIGN (&mbxname
, &mbxchans
[i
], 0, 0, 0);
265 if ((status
& 1) != 1)
268 dead_process
= i
+ 1;
278 for (i
= 0; i
< num_fd
; i
++)
283 /* Peek in the mailbox to see if there's data */
285 (0, mbxchans
[i
], IO$_SENSEMODE
|IO$M_READERCHECK
,
286 &iosb
, 0, 0, 0, 0, 0, 0, 0, 0);
288 if ((status
& 1) != 1)
303 if (timeout
> 0 && num
== 0)
315 /* Deassign channels assigned above */
316 for (i
= 0; i
< num_fd
; i
++)
319 status
= SYS$
DASSGN (mbxchans
[i
]);
324 #elif defined (__unix__)
327 #include <sys/ptyio.h>
330 #include <sys/time.h>
333 #define SELECT_MASK fd_set
334 #else /* !NO_FD_SET */
336 typedef long fd_mask
;
339 #define SELECT_MASK void
341 #define SELECT_MASK int
343 #endif /* !NO_FD_SET */
346 __gnat_waitpid (int pid
)
350 waitpid (pid
, &status
, 0);
351 status
= WEXITSTATUS (status
);
357 __gnat_pipe (int *fd
)
363 __gnat_expect_fork (void)
369 __gnat_expect_portable_execvp (int *pid
, char *cmd
, char *argv
[])
371 *pid
= (int) getpid ();
372 /* Since cmd is fully qualified, it is incorrect to call execvp */
378 __gnat_expect_poll (int *fd
,
397 tv
.tv_sec
= timeout
/ 1000;
398 tv
.tv_usec
= (timeout
% 1000) * 1000;
404 for (i
= 0; i
< num_fd
; i
++)
406 FD_SET (fd
[i
], &rset
);
407 FD_SET (fd
[i
], &eset
);
414 select (max_fd
+ 1, &rset
, NULL
, &eset
, timeout
== -1 ? NULL
: &tv
);
422 for (i
= 0; i
< num_fd
; i
++)
424 if (FD_ISSET (fd
[i
], &rset
))
436 for (i
= 0; i
< num_fd
; i
++)
438 if (FD_ISSET (fd
[i
], &eset
))
440 struct request_info ei
;
442 /* Only query and reset error state if no file descriptor
443 is ready to be read, otherwise we will be signalling a
444 died process too early */
448 ioctl (fd
[i
], TIOCREQCHECK
, &ei
);
450 if (ei
.request
== TIOCCLOSE
)
452 ioctl (fd
[i
], TIOCREQSET
, &ei
);
453 dead_process
= i
+ 1;
457 ioctl (fd
[i
], TIOCREQSET
, &ei
);
464 } while (timeout
== -1 && ready
== 0);
472 __gnat_waitpid (int pid ATTRIBUTE_UNUSED
, int sig ATTRIBUTE_UNUSED
)
478 __gnat_pipe (int *fd ATTRIBUTE_UNUSED
)
484 __gnat_expect_fork (void)
490 __gnat_expect_portable_execvp (int *pid ATTRIBUTE_UNUSED
,
491 char *cmd ATTRIBUTE_UNUSED
,
492 char *argv
[] ATTRIBUTE_UNUSED
)
498 __gnat_expect_poll (int *fd ATTRIBUTE_UNUSED
,
499 int num_fd ATTRIBUTE_UNUSED
,
500 int timeout ATTRIBUTE_UNUSED
,
501 int *dead_process ATTRIBUTE_UNUSED
,
502 int *is_set ATTRIBUTE_UNUSED
)