PR c++/54038
[official-gcc.git] / gcc / ada / expect.c
bloba40ffa3d29d374f480c70543159feaab38e84676
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-2011, 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 __alpha_vxworks
33 #include "vxWorks.h"
34 #endif
36 #ifdef IN_RTS
37 #define POSIX
38 #include "tconfig.h"
39 #include "tsystem.h"
40 #else
41 #include "config.h"
42 #include "system.h"
43 #endif
45 #include <sys/types.h>
47 #ifdef __MINGW32__
48 #if OLD_MINGW
49 #include <sys/wait.h>
50 #endif
51 #elif defined (__vxworks) && defined (__RTP__)
52 #include <wait.h>
53 #elif defined (__Lynx__)
54 /* ??? See comment in adaint.c. */
55 #define GCC_RESOURCE_H
56 #include <sys/wait.h>
57 #elif defined (__nucleus__)
58 /* No wait.h available on Nucleus */
59 #else
60 #include <sys/wait.h>
61 #endif
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. */
68 #ifdef _AIX
69 /* Work around the fact that gcc/cpp does not define "__unix__" under AiX. */
70 #define __unix__
71 #endif
73 #ifdef __APPLE__
74 /* Work around the fact that gcc/cpp does not define "__unix__" on Darwin. */
75 #define __unix__
76 #endif
78 #ifdef _WIN32
80 #include <windows.h>
81 #include <process.h>
82 #include <signal.h>
83 #include <io.h>
84 #include "mingw32.h"
86 void
87 __gnat_kill (int pid, int sig, int close)
89 HANDLE h = OpenProcess (PROCESS_ALL_ACCESS, FALSE, pid);
90 if (h == NULL)
91 return;
92 if (sig == 9)
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 */
106 CloseHandle (h);
110 __gnat_waitpid (int pid)
112 HANDLE h = OpenProcess (PROCESS_ALL_ACCESS, FALSE, pid);
113 DWORD exitcode = 1;
114 DWORD res;
116 if (h != NULL)
118 res = WaitForSingleObject (h, INFINITE);
119 GetExitCodeProcess (h, &exitcode);
120 CloseHandle (h);
123 __gnat_win32_remove_handle (NULL, pid);
124 return (int) exitcode;
128 __gnat_expect_fork (void)
130 return 0;
133 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)
142 HANDLE read, write;
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;
156 DWORD avail;
157 HANDLE handles[num_fd];
159 for (i = 0; i < num_fd; i++)
160 is_set[i] = 0;
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 */
167 delay = 5;
169 if (timeout < 0)
170 infinite = 1;
172 while (1)
174 for (i = 0; i < num_fd; i++)
176 if (!PeekNamedPipe (handles [i], NULL, 0, NULL, &avail, NULL))
177 return -1;
179 if (avail > 0)
181 is_set[i] = 1;
182 return 1;
186 if (!infinite && timeout <= 0)
187 return 0;
189 Sleep (delay);
190 timeout -= delay;
192 if (delay < MAX_DELAY)
193 delay += 10;
197 #elif defined (VMS)
198 #include <unistd.h>
199 #include <stdio.h>
200 #include <unixio.h>
201 #include <stdlib.h>
202 #include <string.h>
203 #include <vms/descrip.h>
204 #include <stdio.h>
205 #include <vms/stsdef.h>
206 #include <vms/iodef.h>
207 #include <signal.h>
209 void
210 __gnat_kill (int pid, int sig, int close)
212 kill (pid, sig);
216 __gnat_waitpid (int pid)
218 int status = 0;
220 waitpid (pid, &status, 0);
221 status = WEXITSTATUS (status);
223 return status;
227 __gnat_pipe (int *fd)
229 return pipe (fd);
233 __gnat_expect_fork (void)
235 return -1;
238 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 */
243 execv (cmd, argv);
244 _exit (1);
248 __gnat_expect_poll (int *fd, int num_fd, int timeout, int *is_set)
250 int i, num, ready = 0;
251 unsigned int status;
252 int mbxchans [num_fd];
253 struct dsc$descriptor_s mbxname;
254 struct io_status_block {
255 short int condition;
256 short int count;
257 int dev;
258 } iosb;
259 char buf [256];
261 for (i = 0; i < num_fd; i++)
262 is_set[i] = 0;
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)
282 ready = -1;
283 return ready;
288 num = timeout / 100;
290 while (1)
292 for (i = 0; i < num_fd; i++)
294 if (mbxchans[i] > 0)
297 /* Peek in the mailbox to see if there's data */
298 status = SYS$QIOW
299 (0, mbxchans[i], IO$_SENSEMODE|IO$M_READERCHECK,
300 &iosb, 0, 0, 0, 0, 0, 0, 0, 0);
302 if ((status & 1) != 1)
304 ready = -1;
305 goto deassign;
308 if (iosb.count > 0)
310 is_set[i] = 1;
311 ready = 1;
312 goto deassign;
317 if (timeout > 0 && num == 0)
319 ready = 0;
320 goto deassign;
323 usleep (100000);
324 num--;
327 deassign:
329 /* Deassign channels assigned above */
330 for (i = 0; i < num_fd; i++)
332 if (mbxchans[i] > 0)
333 status = SYS$DASSGN (mbxchans[i]);
336 return ready;
338 #elif defined (__unix__) && !defined (__nucleus__)
340 #ifdef __hpux__
341 #include <sys/ptyio.h>
342 #endif
344 #include <sys/time.h>
346 #ifndef NO_FD_SET
347 #define SELECT_MASK fd_set
348 #else /* !NO_FD_SET */
349 #ifndef _AIX
350 typedef long fd_mask;
351 #endif /* _AIX */
352 #ifdef _IBMR2
353 #define SELECT_MASK void
354 #else /* !_IBMR2 */
355 #define SELECT_MASK int
356 #endif /* !_IBMR2 */
357 #endif /* !NO_FD_SET */
359 void
360 __gnat_kill (int pid, int sig, int close)
362 kill (pid, sig);
366 __gnat_waitpid (int pid)
368 int status = 0;
370 waitpid (pid, &status, 0);
371 status = WEXITSTATUS (status);
373 return status;
377 __gnat_pipe (int *fd)
379 return pipe (fd);
383 __gnat_expect_fork (void)
385 return fork ();
388 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 */
393 execv (cmd, argv);
394 _exit (1);
398 __gnat_expect_poll (int *fd, int num_fd, int timeout, int *is_set)
400 struct timeval tv;
401 SELECT_MASK rset;
402 SELECT_MASK eset;
404 int max_fd = 0;
405 int ready;
406 int i;
407 int received;
409 tv.tv_sec = timeout / 1000;
410 tv.tv_usec = (timeout % 1000) * 1000;
412 do {
413 FD_ZERO (&rset);
414 FD_ZERO (&eset);
416 for (i = 0; i < num_fd; i++)
418 FD_SET (fd[i], &rset);
419 FD_SET (fd[i], &eset);
421 if (fd[i] > max_fd)
422 max_fd = fd[i];
425 ready =
426 select (max_fd + 1, &rset, NULL, &eset, timeout == -1 ? NULL : &tv);
428 if (ready > 0)
430 received = 0;
432 for (i = 0; i < num_fd; i++)
434 if (FD_ISSET (fd[i], &rset))
436 is_set[i] = 1;
437 received = 1;
439 else
440 is_set[i] = 0;
443 #ifdef __hpux__
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 */
454 if (!received)
456 ioctl (fd[i], TIOCREQCHECK, &ei);
458 if (ei.request == TIOCCLOSE)
460 ioctl (fd[i], TIOCREQSET, &ei);
461 return -1;
464 ioctl (fd[i], TIOCREQSET, &ei);
466 ready--;
469 #endif
471 } while (timeout == -1 && ready == 0);
473 return ready;
476 #else
478 void
479 __gnat_kill (int pid, int sig, int close)
484 __gnat_waitpid (int pid, int sig)
486 return 0;
490 __gnat_pipe (int *fd)
492 return -1;
496 __gnat_expect_fork (void)
498 return -1;
501 void
502 __gnat_expect_portable_execvp (int *pid, char *cmd, char *argv[])
504 *pid = 0;
508 __gnat_expect_poll (int *fd, int num_fd, int timeout, int *is_set)
510 return -1;
512 #endif