Daily bump.
[official-gcc.git] / gcc / ada / expect.c
blob591401cf1dc594e7afa118847bd40025e18c1033
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * E X P E C T *
6 * *
7 * C Implementation File *
8 * *
9 * $Revision: 1.1 $
10 * *
11 * Copyright (C) 2001 Ada Core Technologies, Inc. *
12 * *
13 * GNAT is free software; you can redistribute it and/or modify it under *
14 * terms of the GNU General Public License as published by the Free Soft- *
15 * ware Foundation; either version 2, or (at your option) any later ver- *
16 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
17 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
19 * for more details. You should have received a copy of the GNU General *
20 * Public License distributed with GNAT; see file COPYING. If not, write *
21 * to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, *
22 * MA 02111-1307, USA. *
23 * *
24 * As a special exception, if you link this file with other files to *
25 * produce an executable, this file does not by itself cause the resulting *
26 * executable to be covered by the GNU General Public License. This except- *
27 * ion does not however invalidate any other reasons why the executable *
28 * file might be covered by the GNU Public License. *
29 * *
30 * GNAT was originally developed by the GNAT team at New York University. *
31 * It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). *
32 * *
33 ****************************************************************************/
35 #ifdef __alpha_vxworks
36 #include "vxWorks.h"
37 #endif
39 #ifdef IN_RTS
40 #define POSIX
41 #include "tconfig.h"
42 #include "tsystem.h"
43 #else
44 #include "config.h"
45 #include "system.h"
46 #endif
48 /* This file provides the low level functionalities needed to implement Expect
49 capabilities in GNAT.Expect.
50 Implementations for unix and windows systems is provided.
51 Dummy stubs are also provided for other systems. */
53 #ifdef _AIX
54 /* Work around the fact that gcc/cpp does not define "unix" under AiX. */
55 #define unix
56 #endif
58 #ifdef _WIN32
60 #include <windows.h>
61 #include <process.h>
63 /* ??? Provide a no-op for now */
65 void
66 kill ()
70 int
71 __gnat_expect_fork ()
73 return 0;
76 void
77 __gnat_expect_portable_execvp (cmd, argv)
78 char *cmd;
79 char *argv[];
81 (void) spawnve (_P_NOWAIT, cmd, argv, NULL);
84 int
85 __gnat_pipe (fd)
86 int *fd;
88 HANDLE read, write;
90 CreatePipe (&read, &write, NULL, 0);
91 fd[0]=_open_osfhandle (read, 0);
92 fd[1]=_open_osfhandle (write, 0);
93 return 0; /* always success */
96 int
97 __gnat_expect_poll (fd, num_fd, timeout, is_set)
98 int *fd;
99 int num_fd;
100 int timeout;
101 int *is_set;
103 int i, num;
104 DWORD avail;
105 HANDLE handles[num_fd];
107 for (i = 0; i < num_fd; i++)
108 is_set[i] = 0;
110 for (i = 0; i < num_fd; i++)
111 handles[i] = (HANDLE) _get_osfhandle (fd [i]);
113 num = timeout / 10;
115 while (1)
117 for (i = 0; i < num_fd; i++)
119 if (!PeekNamedPipe (handles [i], NULL, 0, NULL, &avail, NULL))
120 return -1;
122 if (avail > 0)
124 is_set[i] = 1;
125 return 1;
129 if (timeout >= 0 && num == 0)
130 return 0;
132 Sleep (10);
133 num--;
137 #elif defined (unix)
139 #include <sys/time.h>
141 #ifndef NO_FD_SET
142 #define SELECT_MASK fd_set
143 #else /* !NO_FD_SET */
144 #ifndef _AIX
145 typedef long fd_mask;
146 #endif /* _AIX */
147 #ifdef _IBMR2
148 #define SELECT_MASK void
149 #else /* !_IBMR2 */
150 #define SELECT_MASK int
151 #endif /* !_IBMR2 */
152 #endif /* !NO_FD_SET */
155 __gnat_pipe (fd)
156 int *fd;
158 return pipe (fd);
162 __gnat_expect_fork ()
164 return fork ();
167 void
168 __gnat_expect_portable_execvp (cmd, argv)
169 char *cmd;
170 char *argv[];
172 execvp (cmd, argv);
176 __gnat_expect_poll (fd, num_fd, timeout, is_set)
177 int *fd;
178 int num_fd;
179 int timeout;
180 int *is_set;
182 struct timeval tv;
183 SELECT_MASK rset;
184 int max_fd = 0;
185 int ready;
186 int i;
188 FD_ZERO (&rset);
190 for (i = 0; i < num_fd; i++)
192 FD_SET (fd [i], &rset);
193 if (fd [i] > max_fd)
194 max_fd = fd [i];
197 tv.tv_sec = timeout / 1000;
198 tv.tv_usec = (timeout % 1000) * 1000;
200 ready = select (max_fd + 1, &rset, NULL, NULL, timeout == -1 ? NULL : &tv);
202 if (ready > 0)
203 for (i = 0; i < num_fd; i++)
204 is_set [i] = (FD_ISSET (fd [i], &rset) ? 1 : 0);
206 return ready;
209 #else
212 __gnat_pipe (fd)
213 int *fd;
215 return -1;
219 __gnat_expect_fork ()
221 return -1;
224 void
225 __gnat_expect_portable_execvp (cmd, argv)
226 char *cmd;
227 char *argv[];
232 __gnat_expect_poll (fd, num_fd, timeout, is_set)
233 int *fd;
234 int num_fd;
235 int timeout;
236 int *is_set;
238 return -1;
240 #endif