OSDN Git Service

* doc/install.texi (xtensa-*-elf): New target.
[pf3gnuchains/gcc-fork.git] / gcc / ada / expect.c
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  ****************************************************************************/
34
35 #ifdef __alpha_vxworks
36 #include "vxWorks.h"
37 #endif
38
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
47
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. */
52
53 #ifdef _AIX
54 /* Work around the fact that gcc/cpp does not define "unix" under AiX.  */
55 #define unix
56 #endif
57
58 #ifdef _WIN32
59
60 #include <windows.h>
61 #include <process.h>
62
63 /* ??? Provide a no-op for now */
64
65 void
66 kill ()
67 {
68 }
69
70 int
71 __gnat_expect_fork ()
72 {
73   return 0;
74 }
75
76 void
77 __gnat_expect_portable_execvp (cmd, argv)
78      char *cmd;
79      char *argv[];
80 {
81   (void) spawnve (_P_NOWAIT, cmd, argv, NULL);
82 }
83
84 int
85 __gnat_pipe (fd)
86      int *fd;
87 {
88   HANDLE read, write;
89
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 */
94 }
95
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;
102 {
103   int i, num;
104   DWORD avail;
105   HANDLE handles[num_fd];
106
107   for (i = 0; i < num_fd; i++)
108     is_set[i] = 0;
109
110   for (i = 0; i < num_fd; i++)
111     handles[i] = (HANDLE) _get_osfhandle (fd [i]);
112
113   num = timeout / 10;
114
115   while (1)
116     {
117       for (i = 0; i < num_fd; i++)
118         {
119           if (!PeekNamedPipe (handles [i], NULL, 0, NULL, &avail, NULL))
120             return -1;
121
122           if (avail > 0)
123             {
124               is_set[i] = 1;
125               return 1;
126             }
127         }
128
129       if (timeout >= 0 && num == 0)
130         return 0;
131
132       Sleep (10);
133       num--;
134     }
135 }
136
137 #elif defined (unix)
138
139 #include <sys/time.h>
140
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 */
153
154 int
155 __gnat_pipe (fd)
156      int *fd;
157 {
158   return pipe (fd);
159 }
160
161 int
162 __gnat_expect_fork ()
163 {
164   return fork ();
165 }
166
167 void
168 __gnat_expect_portable_execvp (cmd, argv) 
169      char *cmd;
170      char *argv[];
171 {
172   execvp (cmd, argv);
173 }
174
175 int
176 __gnat_expect_poll (fd, num_fd, timeout, is_set)
177      int *fd;
178      int num_fd;
179      int timeout;
180      int *is_set;
181 {
182   struct timeval tv;
183   SELECT_MASK rset;
184   int max_fd = 0;
185   int ready;
186   int i;
187
188   FD_ZERO (&rset);
189
190   for (i = 0; i < num_fd; i++)
191     {
192       FD_SET (fd [i], &rset);
193       if (fd [i] > max_fd)
194         max_fd = fd [i];
195     }
196
197   tv.tv_sec  = timeout / 1000;
198   tv.tv_usec = (timeout % 1000) * 1000;
199
200   ready = select (max_fd + 1, &rset, NULL, NULL, timeout == -1 ? NULL : &tv);
201
202   if (ready > 0)
203     for (i = 0; i < num_fd; i++)
204       is_set [i] = (FD_ISSET (fd [i], &rset)  ? 1 : 0);
205
206   return ready;
207 }
208
209 #else
210
211 int
212 __gnat_pipe (fd)
213      int *fd;
214 {
215   return -1;
216 }
217
218 int
219 __gnat_expect_fork ()
220 {
221   return -1;
222 }
223
224 void
225 __gnat_expect_portable_execvp (cmd, argv)
226      char *cmd;
227      char *argv[];
228 {
229 }
230
231 int
232 __gnat_expect_poll (fd, num_fd, timeout, is_set)
233      int *fd;
234      int num_fd;
235      int timeout;
236      int *is_set;
237 {
238   return -1;
239 }
240 #endif