1 /****************************************************************************
3 * GNAT RUN-TIME COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 2001-2011, 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>
51 #elif defined (__vxworks) && defined (__RTP__)
53 #elif defined (__Lynx__)
54 /* ??? See comment in adaint.c. */
55 #define GCC_RESOURCE_H
57 #elif defined (__nucleus__)
58 /* No wait.h available on Nucleus */
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_kill (int pid
, int sig
, int close
)
89 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
94 TerminateProcess (h
, 0);
95 __gnat_win32_remove_handle (NULL
, pid
);
97 else if (sig
== SIGINT
)
98 GenerateConsoleCtrlEvent (CTRL_C_EVENT
, pid
);
99 else if (sig
== SIGBREAK
)
100 GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, pid
);
101 /* ??? The last two alternatives don't really work. SIGBREAK requires setting
102 up process groups at start time which we don't do; treating SIGINT is just
103 not possible apparently. So we really only support signal 9. Fortunately
104 that's all we use in GNAT.Expect */
110 __gnat_waitpid (int pid
)
112 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
118 res
= WaitForSingleObject (h
, INFINITE
);
119 GetExitCodeProcess (h
, &exitcode
);
123 __gnat_win32_remove_handle (NULL
, pid
);
124 return (int) exitcode
;
128 __gnat_expect_fork (void)
134 __gnat_expect_portable_execvp (int *pid
, char *cmd
, char *argv
[])
136 *pid
= __gnat_portable_no_block_spawn (argv
);
140 __gnat_pipe (int *fd
)
144 CreatePipe (&read
, &write
, NULL
, 0);
145 fd
[0]=_open_osfhandle ((intptr_t)read
, 0);
146 fd
[1]=_open_osfhandle ((intptr_t)write
, 0);
147 return 0; /* always success */
151 __gnat_expect_poll (int *fd
, int num_fd
, int timeout
, int *is_set
)
153 #define MAX_DELAY 100
155 int i
, delay
, infinite
= 0;
157 HANDLE handles
[num_fd
];
159 for (i
= 0; i
< num_fd
; i
++)
162 for (i
= 0; i
< num_fd
; i
++)
163 handles
[i
] = (HANDLE
) _get_osfhandle (fd
[i
]);
165 /* Start with small delays, and then increase them, to avoid polling too
166 much when waiting a long time */
174 for (i
= 0; i
< num_fd
; i
++)
176 if (!PeekNamedPipe (handles
[i
], NULL
, 0, NULL
, &avail
, NULL
))
186 if (!infinite
&& timeout
<= 0)
192 if (delay
< MAX_DELAY
)
203 #include <vms/descrip.h>
205 #include <vms/stsdef.h>
206 #include <vms/iodef.h>
210 __gnat_kill (int pid
, int sig
, int close
)
216 __gnat_waitpid (int pid
)
220 waitpid (pid
, &status
, 0);
221 status
= WEXITSTATUS (status
);
227 __gnat_pipe (int *fd
)
233 __gnat_expect_fork (void)
239 __gnat_expect_portable_execvp (int *pid
, char *cmd
, char *argv
[])
241 *pid
= (int) getpid ();
242 /* Since cmd is fully qualified, it is incorrect to call execvp */
248 __gnat_expect_poll (int *fd
, int num_fd
, int timeout
, int *is_set
)
250 int i
, num
, ready
= 0;
252 int mbxchans
[num_fd
];
253 struct dsc$descriptor_s mbxname
;
254 struct io_status_block
{
261 for (i
= 0; i
< num_fd
; i
++)
264 for (i
= 0; i
< num_fd
; i
++)
267 /* Get name of the mailbox used in the pipe */
268 getname (fd
[i
], buf
);
270 /* Assign a channel to the mailbox */
271 if (strlen (buf
) > 0)
273 mbxname
.dsc$w_length
= strlen (buf
);
274 mbxname
.dsc$b_dtype
= DSC$K_DTYPE_T
;
275 mbxname
.dsc$b_class
= DSC$K_CLASS_S
;
276 mbxname
.dsc$a_pointer
= buf
;
278 status
= SYS$
ASSIGN (&mbxname
, &mbxchans
[i
], 0, 0, 0);
280 if ((status
& 1) != 1)
292 for (i
= 0; i
< num_fd
; i
++)
297 /* Peek in the mailbox to see if there's data */
299 (0, mbxchans
[i
], IO$_SENSEMODE
|IO$M_READERCHECK
,
300 &iosb
, 0, 0, 0, 0, 0, 0, 0, 0);
302 if ((status
& 1) != 1)
317 if (timeout
> 0 && num
== 0)
329 /* Deassign channels assigned above */
330 for (i
= 0; i
< num_fd
; i
++)
333 status
= SYS$
DASSGN (mbxchans
[i
]);
338 #elif defined (__unix__) && !defined (__nucleus__)
341 #include <sys/ptyio.h>
344 #include <sys/time.h>
347 #define SELECT_MASK fd_set
348 #else /* !NO_FD_SET */
350 typedef long fd_mask
;
353 #define SELECT_MASK void
355 #define SELECT_MASK int
357 #endif /* !NO_FD_SET */
360 __gnat_kill (int pid
, int sig
, int close
)
366 __gnat_waitpid (int pid
)
370 waitpid (pid
, &status
, 0);
371 status
= WEXITSTATUS (status
);
377 __gnat_pipe (int *fd
)
383 __gnat_expect_fork (void)
389 __gnat_expect_portable_execvp (int *pid
, char *cmd
, char *argv
[])
391 *pid
= (int) getpid ();
392 /* Since cmd is fully qualified, it is incorrect to call execvp */
398 __gnat_expect_poll (int *fd
, int num_fd
, int timeout
, int *is_set
)
409 tv
.tv_sec
= timeout
/ 1000;
410 tv
.tv_usec
= (timeout
% 1000) * 1000;
416 for (i
= 0; i
< num_fd
; i
++)
418 FD_SET (fd
[i
], &rset
);
419 FD_SET (fd
[i
], &eset
);
426 select (max_fd
+ 1, &rset
, NULL
, &eset
, timeout
== -1 ? NULL
: &tv
);
432 for (i
= 0; i
< num_fd
; i
++)
434 if (FD_ISSET (fd
[i
], &rset
))
444 for (i
= 0; i
< num_fd
; i
++)
446 if (FD_ISSET (fd
[i
], &eset
))
448 struct request_info ei
;
450 /* Only query and reset error state if no file descriptor
451 is ready to be read, otherwise we will be signalling a
452 died process too early */
456 ioctl (fd
[i
], TIOCREQCHECK
, &ei
);
458 if (ei
.request
== TIOCCLOSE
)
460 ioctl (fd
[i
], TIOCREQSET
, &ei
);
464 ioctl (fd
[i
], TIOCREQSET
, &ei
);
471 } while (timeout
== -1 && ready
== 0);
479 __gnat_kill (int pid
, int sig
, int close
)
484 __gnat_waitpid (int pid
, int sig
)
490 __gnat_pipe (int *fd
)
496 __gnat_expect_fork (void)
502 __gnat_expect_portable_execvp (int *pid
, char *cmd
, char *argv
[])
508 __gnat_expect_poll (int *fd
, int num_fd
, int timeout
, int *is_set
)