annotate gcc/ada/libgnat/g-expect.ads @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT LIBRARY COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- G N A T . E X P E C T --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- S p e c --
kono
parents:
diff changeset
8 -- --
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
9 -- Copyright (C) 2000-2018, AdaCore --
111
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
17 -- --
kono
parents:
diff changeset
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
19 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
20 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
21 -- --
kono
parents:
diff changeset
22 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
23 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
25 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
26 -- --
kono
parents:
diff changeset
27 -- GNAT was originally developed by the GNAT team at New York University. --
kono
parents:
diff changeset
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
kono
parents:
diff changeset
29 -- --
kono
parents:
diff changeset
30 ------------------------------------------------------------------------------
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 -- Currently this package is implemented on all native GNAT ports. It is not
kono
parents:
diff changeset
33 -- yet implemented for any of the cross-ports (e.g. it is not available for
kono
parents:
diff changeset
34 -- VxWorks or LynxOS).
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 -- -----------
kono
parents:
diff changeset
37 -- -- Usage --
kono
parents:
diff changeset
38 -- -----------
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 -- This package provides a set of subprograms similar to what is available
kono
parents:
diff changeset
41 -- with the standard Tcl Expect tool.
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 -- It allows you to easily spawn and communicate with an external process.
kono
parents:
diff changeset
44 -- You can send commands or inputs to the process, and compare the output
kono
parents:
diff changeset
45 -- with some expected regular expression.
kono
parents:
diff changeset
46
kono
parents:
diff changeset
47 -- Usage example:
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 -- Non_Blocking_Spawn
kono
parents:
diff changeset
50 -- (Fd, "ftp",
kono
parents:
diff changeset
51 -- (1 => new String' ("machine@domain")));
kono
parents:
diff changeset
52 -- Timeout := 10_000; -- 10 seconds
kono
parents:
diff changeset
53 -- Expect (Fd, Result, Regexp_Array'(+"\(user\)", +"\(passwd\)"),
kono
parents:
diff changeset
54 -- Timeout);
kono
parents:
diff changeset
55 -- case Result is
kono
parents:
diff changeset
56 -- when 1 => Send (Fd, "my_name"); -- matched "user"
kono
parents:
diff changeset
57 -- when 2 => Send (Fd, "my_passwd"); -- matched "passwd"
kono
parents:
diff changeset
58 -- when Expect_Timeout => null; -- timeout
kono
parents:
diff changeset
59 -- when others => null;
kono
parents:
diff changeset
60 -- end case;
kono
parents:
diff changeset
61 -- Close (Fd);
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 -- You can also combine multiple regular expressions together, and get the
kono
parents:
diff changeset
64 -- specific string matching a parenthesis pair by doing something like this:
kono
parents:
diff changeset
65 -- If you expect either "lang=optional ada" or "lang=ada" from the external
kono
parents:
diff changeset
66 -- process, you can group the two together, which is more efficient, and
kono
parents:
diff changeset
67 -- simply get the name of the language by doing:
kono
parents:
diff changeset
68
kono
parents:
diff changeset
69 -- declare
kono
parents:
diff changeset
70 -- Matched : Match_Array (0 .. 2);
kono
parents:
diff changeset
71 -- begin
kono
parents:
diff changeset
72 -- Expect (Fd, Result, "lang=(optional)? ([a-z]+)", Matched);
kono
parents:
diff changeset
73 -- Put_Line ("Seen: " &
kono
parents:
diff changeset
74 -- Expect_Out (Fd) (Matched (2).First .. Matched (2).Last));
kono
parents:
diff changeset
75 -- end;
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 -- Alternatively, you might choose to use a lower-level interface to the
kono
parents:
diff changeset
78 -- processes, where you can give your own input and output filters every
kono
parents:
diff changeset
79 -- time characters are read from or written to the process.
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81 -- procedure My_Filter
kono
parents:
diff changeset
82 -- (Descriptor : Process_Descriptor'Class;
kono
parents:
diff changeset
83 -- Str : String;
kono
parents:
diff changeset
84 -- User_Data : System.Address)
kono
parents:
diff changeset
85 -- is
kono
parents:
diff changeset
86 -- begin
kono
parents:
diff changeset
87 -- Put_Line (Str);
kono
parents:
diff changeset
88 -- end;
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 -- Non_Blocking_Spawn
kono
parents:
diff changeset
91 -- (Fd, "tail",
kono
parents:
diff changeset
92 -- (new String' ("-f"), new String' ("a_file")));
kono
parents:
diff changeset
93 -- Add_Filter (Fd, My_Filter'Access, Output);
kono
parents:
diff changeset
94 -- Expect (Fd, Result, "", 0); -- wait forever
kono
parents:
diff changeset
95
kono
parents:
diff changeset
96 -- The above example should probably be run in a separate task, since it is
kono
parents:
diff changeset
97 -- blocking on the call to Expect.
kono
parents:
diff changeset
98
kono
parents:
diff changeset
99 -- Both examples can be combined, for instance to systematically print the
kono
parents:
diff changeset
100 -- output seen by expect, even though you still want to let Expect do the
kono
parents:
diff changeset
101 -- filtering. You can use the Trace_Filter subprogram for such a filter.
kono
parents:
diff changeset
102
kono
parents:
diff changeset
103 -- If you want to get the output of a simple command, and ignore any previous
kono
parents:
diff changeset
104 -- existing output, it is recommended to do something like:
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 -- Expect (Fd, Result, ".*", Timeout => 0);
kono
parents:
diff changeset
107 -- -- Empty the buffer, by matching everything (after checking
kono
parents:
diff changeset
108 -- -- if there was any input).
kono
parents:
diff changeset
109
kono
parents:
diff changeset
110 -- Send (Fd, "command");
kono
parents:
diff changeset
111 -- Expect (Fd, Result, ".."); -- match only on the output of command
kono
parents:
diff changeset
112
kono
parents:
diff changeset
113 -- -----------------
kono
parents:
diff changeset
114 -- -- Task Safety --
kono
parents:
diff changeset
115 -- -----------------
kono
parents:
diff changeset
116
kono
parents:
diff changeset
117 -- This package is not task-safe: there should not be concurrent calls to the
kono
parents:
diff changeset
118 -- functions defined in this package. In other words, separate tasks must not
kono
parents:
diff changeset
119 -- access the facilities of this package without synchronization that
kono
parents:
diff changeset
120 -- serializes access.
kono
parents:
diff changeset
121
kono
parents:
diff changeset
122 with System;
kono
parents:
diff changeset
123 with GNAT.OS_Lib;
kono
parents:
diff changeset
124 with GNAT.Regpat;
kono
parents:
diff changeset
125
kono
parents:
diff changeset
126 package GNAT.Expect is
kono
parents:
diff changeset
127
kono
parents:
diff changeset
128 type Process_Id is new Integer;
kono
parents:
diff changeset
129 Invalid_Pid : constant Process_Id := -1;
kono
parents:
diff changeset
130 Null_Pid : constant Process_Id := 0;
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132 type Filter_Type is (Output, Input, Died);
kono
parents:
diff changeset
133 -- The signals that are emitted by the Process_Descriptor upon state change
kono
parents:
diff changeset
134 -- in the child. One can connect to any of these signals through the
kono
parents:
diff changeset
135 -- Add_Filter subprograms.
kono
parents:
diff changeset
136 --
kono
parents:
diff changeset
137 -- Output => Every time new characters are read from the process
kono
parents:
diff changeset
138 -- associated with Descriptor, the filter is called with
kono
parents:
diff changeset
139 -- these new characters in the argument.
kono
parents:
diff changeset
140 --
kono
parents:
diff changeset
141 -- Note that output is generated only when the program is
kono
parents:
diff changeset
142 -- blocked in a call to Expect.
kono
parents:
diff changeset
143 --
kono
parents:
diff changeset
144 -- Input => Every time new characters are written to the process
kono
parents:
diff changeset
145 -- associated with Descriptor, the filter is called with
kono
parents:
diff changeset
146 -- these new characters in the argument.
kono
parents:
diff changeset
147 -- Note that input is generated only by calls to Send.
kono
parents:
diff changeset
148 --
kono
parents:
diff changeset
149 -- Died => The child process has died, or was explicitly killed
kono
parents:
diff changeset
150
kono
parents:
diff changeset
151 type Process_Descriptor is tagged private;
kono
parents:
diff changeset
152 -- Contains all the components needed to describe a process handled
kono
parents:
diff changeset
153 -- in this package, including a process identifier, file descriptors
kono
parents:
diff changeset
154 -- associated with the standard input, output and error, and the buffer
kono
parents:
diff changeset
155 -- needed to handle the expect calls.
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 type Process_Descriptor_Access is access Process_Descriptor'Class;
kono
parents:
diff changeset
158
kono
parents:
diff changeset
159 ------------------------
kono
parents:
diff changeset
160 -- Spawning a process --
kono
parents:
diff changeset
161 ------------------------
kono
parents:
diff changeset
162
kono
parents:
diff changeset
163 procedure Non_Blocking_Spawn
kono
parents:
diff changeset
164 (Descriptor : out Process_Descriptor'Class;
kono
parents:
diff changeset
165 Command : String;
kono
parents:
diff changeset
166 Args : GNAT.OS_Lib.Argument_List;
kono
parents:
diff changeset
167 Buffer_Size : Natural := 4096;
kono
parents:
diff changeset
168 Err_To_Out : Boolean := False);
kono
parents:
diff changeset
169 -- This call spawns a new process and allows sending commands to
kono
parents:
diff changeset
170 -- the process and/or automatic parsing of the output.
kono
parents:
diff changeset
171 --
kono
parents:
diff changeset
172 -- The expect buffer associated with that process can contain at most
kono
parents:
diff changeset
173 -- Buffer_Size characters. Older characters are simply discarded when this
kono
parents:
diff changeset
174 -- buffer is full. Beware that if the buffer is too big, this could slow
kono
parents:
diff changeset
175 -- down the Expect calls if the output not is matched, since Expect has to
kono
parents:
diff changeset
176 -- match all the regexp against all the characters in the buffer. If
kono
parents:
diff changeset
177 -- Buffer_Size is 0, there is no limit (i.e. all the characters are kept
kono
parents:
diff changeset
178 -- till Expect matches), but this is slower.
kono
parents:
diff changeset
179 --
kono
parents:
diff changeset
180 -- If Err_To_Out is True, then the standard error of the spawned process is
kono
parents:
diff changeset
181 -- connected to the standard output. This is the only way to get the Expect
kono
parents:
diff changeset
182 -- subprograms to also match on output on standard error.
kono
parents:
diff changeset
183 --
kono
parents:
diff changeset
184 -- Invalid_Process is raised if the process could not be spawned.
kono
parents:
diff changeset
185 --
kono
parents:
diff changeset
186 -- For information about spawning processes from tasking programs, see the
kono
parents:
diff changeset
187 -- "NOTE: Spawn in tasking programs" in System.OS_Lib (s-os_lib.ads).
kono
parents:
diff changeset
188
kono
parents:
diff changeset
189 procedure Close (Descriptor : in out Process_Descriptor);
kono
parents:
diff changeset
190 -- Terminate the process and close the pipes to it. It implicitly does the
kono
parents:
diff changeset
191 -- 'wait' command required to clean up the process table. This also frees
kono
parents:
diff changeset
192 -- the buffer associated with the process id. Raise Invalid_Process if the
kono
parents:
diff changeset
193 -- process id is invalid.
kono
parents:
diff changeset
194
kono
parents:
diff changeset
195 procedure Close
kono
parents:
diff changeset
196 (Descriptor : in out Process_Descriptor;
kono
parents:
diff changeset
197 Status : out Integer);
kono
parents:
diff changeset
198 -- Same as above, but also returns the exit status of the process, as set
kono
parents:
diff changeset
199 -- for example by the procedure GNAT.OS_Lib.OS_Exit.
kono
parents:
diff changeset
200
kono
parents:
diff changeset
201 procedure Send_Signal
kono
parents:
diff changeset
202 (Descriptor : Process_Descriptor;
kono
parents:
diff changeset
203 Signal : Integer);
kono
parents:
diff changeset
204 -- Send a given signal to the process. Raise Invalid_Process if the process
kono
parents:
diff changeset
205 -- id is invalid.
kono
parents:
diff changeset
206
kono
parents:
diff changeset
207 procedure Interrupt (Descriptor : in out Process_Descriptor);
kono
parents:
diff changeset
208 -- Interrupt the process (the equivalent of Ctrl-C on unix and windows)
kono
parents:
diff changeset
209 -- and call close if the process dies.
kono
parents:
diff changeset
210
kono
parents:
diff changeset
211 function Get_Input_Fd
kono
parents:
diff changeset
212 (Descriptor : Process_Descriptor) return GNAT.OS_Lib.File_Descriptor;
kono
parents:
diff changeset
213 -- Return the input file descriptor associated with Descriptor
kono
parents:
diff changeset
214
kono
parents:
diff changeset
215 function Get_Output_Fd
kono
parents:
diff changeset
216 (Descriptor : Process_Descriptor) return GNAT.OS_Lib.File_Descriptor;
kono
parents:
diff changeset
217 -- Return the output file descriptor associated with Descriptor
kono
parents:
diff changeset
218
kono
parents:
diff changeset
219 function Get_Error_Fd
kono
parents:
diff changeset
220 (Descriptor : Process_Descriptor) return GNAT.OS_Lib.File_Descriptor;
kono
parents:
diff changeset
221 -- Return the error output file descriptor associated with Descriptor
kono
parents:
diff changeset
222
kono
parents:
diff changeset
223 function Get_Pid
kono
parents:
diff changeset
224 (Descriptor : Process_Descriptor) return Process_Id;
kono
parents:
diff changeset
225 -- Return the process id associated with a given process descriptor
kono
parents:
diff changeset
226
kono
parents:
diff changeset
227 function Get_Command_Output
kono
parents:
diff changeset
228 (Command : String;
kono
parents:
diff changeset
229 Arguments : GNAT.OS_Lib.Argument_List;
kono
parents:
diff changeset
230 Input : String;
kono
parents:
diff changeset
231 Status : not null access Integer;
kono
parents:
diff changeset
232 Err_To_Out : Boolean := False) return String;
kono
parents:
diff changeset
233 -- Execute Command with the specified Arguments and Input, and return the
kono
parents:
diff changeset
234 -- generated standard output data as a single string. If Err_To_Out is
kono
parents:
diff changeset
235 -- True, generated standard error output is included as well. On return,
kono
parents:
diff changeset
236 -- Status is set to the command's exit status.
kono
parents:
diff changeset
237
kono
parents:
diff changeset
238 --------------------
kono
parents:
diff changeset
239 -- Adding filters --
kono
parents:
diff changeset
240 --------------------
kono
parents:
diff changeset
241
kono
parents:
diff changeset
242 -- This is a rather low-level interface to subprocesses, since basically
kono
parents:
diff changeset
243 -- the filtering is left entirely to the user. See the Expect subprograms
kono
parents:
diff changeset
244 -- below for higher level functions.
kono
parents:
diff changeset
245
kono
parents:
diff changeset
246 type Filter_Function is access
kono
parents:
diff changeset
247 procedure
kono
parents:
diff changeset
248 (Descriptor : Process_Descriptor'Class;
kono
parents:
diff changeset
249 Str : String;
kono
parents:
diff changeset
250 User_Data : System.Address := System.Null_Address);
kono
parents:
diff changeset
251 -- Function called every time new characters are read from or written to
kono
parents:
diff changeset
252 -- the process.
kono
parents:
diff changeset
253 --
kono
parents:
diff changeset
254 -- Str is a string of all these characters.
kono
parents:
diff changeset
255 --
kono
parents:
diff changeset
256 -- User_Data, if specified, is user specific data that will be passed to
kono
parents:
diff changeset
257 -- the filter. Note that no checks are done on this parameter, so it should
kono
parents:
diff changeset
258 -- be used with caution.
kono
parents:
diff changeset
259
kono
parents:
diff changeset
260 procedure Add_Filter
kono
parents:
diff changeset
261 (Descriptor : in out Process_Descriptor;
kono
parents:
diff changeset
262 Filter : Filter_Function;
kono
parents:
diff changeset
263 Filter_On : Filter_Type := Output;
kono
parents:
diff changeset
264 User_Data : System.Address := System.Null_Address;
kono
parents:
diff changeset
265 After : Boolean := False);
kono
parents:
diff changeset
266 -- Add a new filter for one of the filter types. This filter will be run
kono
parents:
diff changeset
267 -- before all the existing filters, unless After is set True, in which case
kono
parents:
diff changeset
268 -- it will be run after existing filters. User_Data is passed as is to the
kono
parents:
diff changeset
269 -- filter procedure.
kono
parents:
diff changeset
270
kono
parents:
diff changeset
271 procedure Remove_Filter
kono
parents:
diff changeset
272 (Descriptor : in out Process_Descriptor;
kono
parents:
diff changeset
273 Filter : Filter_Function);
kono
parents:
diff changeset
274 -- Remove a filter from the list of filters (whatever the type of the
kono
parents:
diff changeset
275 -- filter).
kono
parents:
diff changeset
276
kono
parents:
diff changeset
277 procedure Trace_Filter
kono
parents:
diff changeset
278 (Descriptor : Process_Descriptor'Class;
kono
parents:
diff changeset
279 Str : String;
kono
parents:
diff changeset
280 User_Data : System.Address := System.Null_Address);
kono
parents:
diff changeset
281 -- Function that can be used as a filter and that simply outputs Str on
kono
parents:
diff changeset
282 -- Standard_Output. This is mainly used for debugging purposes.
kono
parents:
diff changeset
283 -- User_Data is ignored.
kono
parents:
diff changeset
284
kono
parents:
diff changeset
285 procedure Lock_Filters (Descriptor : in out Process_Descriptor);
kono
parents:
diff changeset
286 -- Temporarily disables all output and input filters. They will be
kono
parents:
diff changeset
287 -- reactivated only when Unlock_Filters has been called as many times as
kono
parents:
diff changeset
288 -- Lock_Filters.
kono
parents:
diff changeset
289
kono
parents:
diff changeset
290 procedure Unlock_Filters (Descriptor : in out Process_Descriptor);
kono
parents:
diff changeset
291 -- Unlocks the filters. They are reactivated only if Unlock_Filters
kono
parents:
diff changeset
292 -- has been called as many times as Lock_Filters.
kono
parents:
diff changeset
293
kono
parents:
diff changeset
294 ------------------
kono
parents:
diff changeset
295 -- Sending data --
kono
parents:
diff changeset
296 ------------------
kono
parents:
diff changeset
297
kono
parents:
diff changeset
298 procedure Send
kono
parents:
diff changeset
299 (Descriptor : in out Process_Descriptor;
kono
parents:
diff changeset
300 Str : String;
kono
parents:
diff changeset
301 Add_LF : Boolean := True;
kono
parents:
diff changeset
302 Empty_Buffer : Boolean := False);
kono
parents:
diff changeset
303 -- Send a string to the file descriptor.
kono
parents:
diff changeset
304 --
kono
parents:
diff changeset
305 -- The string is not formatted in any way, except if Add_LF is True, in
kono
parents:
diff changeset
306 -- which case an ASCII.LF is added at the end, so that Str is recognized
kono
parents:
diff changeset
307 -- as a command by the external process.
kono
parents:
diff changeset
308 --
kono
parents:
diff changeset
309 -- If Empty_Buffer is True, any input waiting from the process (or in the
kono
parents:
diff changeset
310 -- buffer) is first discarded before the command is sent. The output
kono
parents:
diff changeset
311 -- filters are of course called as usual.
kono
parents:
diff changeset
312
kono
parents:
diff changeset
313 -----------------------------------------------------------
kono
parents:
diff changeset
314 -- Working on the output (single process, simple regexp) --
kono
parents:
diff changeset
315 -----------------------------------------------------------
kono
parents:
diff changeset
316
kono
parents:
diff changeset
317 type Expect_Match is new Integer;
kono
parents:
diff changeset
318 Expect_Full_Buffer : constant Expect_Match := -1;
kono
parents:
diff changeset
319 -- If the buffer was full and some characters were discarded
kono
parents:
diff changeset
320
kono
parents:
diff changeset
321 Expect_Timeout : constant Expect_Match := -2;
kono
parents:
diff changeset
322 -- If no output matching the regexps was found before the timeout
kono
parents:
diff changeset
323
kono
parents:
diff changeset
324 function "+" (S : String) return GNAT.OS_Lib.String_Access;
kono
parents:
diff changeset
325 -- Allocate some memory for the string. This is merely a convenience
kono
parents:
diff changeset
326 -- function to help create the array of regexps in the call to Expect.
kono
parents:
diff changeset
327
kono
parents:
diff changeset
328 procedure Expect
kono
parents:
diff changeset
329 (Descriptor : in out Process_Descriptor;
kono
parents:
diff changeset
330 Result : out Expect_Match;
kono
parents:
diff changeset
331 Regexp : String;
kono
parents:
diff changeset
332 Timeout : Integer := 10_000;
kono
parents:
diff changeset
333 Full_Buffer : Boolean := False);
kono
parents:
diff changeset
334 -- Wait till a string matching Fd can be read from Fd, and return 1 if a
kono
parents:
diff changeset
335 -- match was found.
kono
parents:
diff changeset
336 --
kono
parents:
diff changeset
337 -- It consumes all the characters read from Fd until a match found, and
kono
parents:
diff changeset
338 -- then sets the return values for the subprograms Expect_Out and
kono
parents:
diff changeset
339 -- Expect_Out_Match.
kono
parents:
diff changeset
340 --
kono
parents:
diff changeset
341 -- The empty string "" will never match, and can be used if you only want
kono
parents:
diff changeset
342 -- to match after a specific timeout. Beware that if Timeout is -1 at the
kono
parents:
diff changeset
343 -- time, the current task will be blocked forever.
kono
parents:
diff changeset
344 --
kono
parents:
diff changeset
345 -- This command times out after Timeout milliseconds (or never if Timeout
kono
parents:
diff changeset
346 -- is -1). In that case, Expect_Timeout is returned. The value returned by
kono
parents:
diff changeset
347 -- Expect_Out and Expect_Out_Match are meaningless in that case.
kono
parents:
diff changeset
348 --
kono
parents:
diff changeset
349 -- Note that using a timeout of 0ms leads to unpredictable behavior, since
kono
parents:
diff changeset
350 -- the result depends on whether the process has already sent some output
kono
parents:
diff changeset
351 -- the first time Expect checks, and this depends on the operating system.
kono
parents:
diff changeset
352 --
kono
parents:
diff changeset
353 -- The regular expression must obey the syntax described in GNAT.Regpat.
kono
parents:
diff changeset
354 --
kono
parents:
diff changeset
355 -- If Full_Buffer is True, then Expect will match if the buffer was too
kono
parents:
diff changeset
356 -- small and some characters were about to be discarded. In that case,
kono
parents:
diff changeset
357 -- Expect_Full_Buffer is returned.
kono
parents:
diff changeset
358
kono
parents:
diff changeset
359 procedure Expect
kono
parents:
diff changeset
360 (Descriptor : in out Process_Descriptor;
kono
parents:
diff changeset
361 Result : out Expect_Match;
kono
parents:
diff changeset
362 Regexp : GNAT.Regpat.Pattern_Matcher;
kono
parents:
diff changeset
363 Timeout : Integer := 10_000;
kono
parents:
diff changeset
364 Full_Buffer : Boolean := False);
kono
parents:
diff changeset
365 -- Same as the previous one, but with a precompiled regular expression.
kono
parents:
diff changeset
366 -- This is more efficient however, especially if you are using this
kono
parents:
diff changeset
367 -- expression multiple times, since this package won't need to recompile
kono
parents:
diff changeset
368 -- the regexp every time.
kono
parents:
diff changeset
369
kono
parents:
diff changeset
370 procedure Expect
kono
parents:
diff changeset
371 (Descriptor : in out Process_Descriptor;
kono
parents:
diff changeset
372 Result : out Expect_Match;
kono
parents:
diff changeset
373 Regexp : String;
kono
parents:
diff changeset
374 Matched : out GNAT.Regpat.Match_Array;
kono
parents:
diff changeset
375 Timeout : Integer := 10_000;
kono
parents:
diff changeset
376 Full_Buffer : Boolean := False);
kono
parents:
diff changeset
377 -- Same as above, but it is now possible to get the indexes of the
kono
parents:
diff changeset
378 -- substrings for the parentheses in the regexp (see the example at the
kono
parents:
diff changeset
379 -- top of this package, as well as the documentation in the package
kono
parents:
diff changeset
380 -- GNAT.Regpat).
kono
parents:
diff changeset
381 --
kono
parents:
diff changeset
382 -- Matched'First should be 0, and this index will contain the indexes for
kono
parents:
diff changeset
383 -- the whole string that was matched. The index 1 will contain the indexes
kono
parents:
diff changeset
384 -- for the first parentheses-pair, and so on.
kono
parents:
diff changeset
385
kono
parents:
diff changeset
386 ------------
kono
parents:
diff changeset
387 -- Expect --
kono
parents:
diff changeset
388 ------------
kono
parents:
diff changeset
389
kono
parents:
diff changeset
390 procedure Expect
kono
parents:
diff changeset
391 (Descriptor : in out Process_Descriptor;
kono
parents:
diff changeset
392 Result : out Expect_Match;
kono
parents:
diff changeset
393 Regexp : GNAT.Regpat.Pattern_Matcher;
kono
parents:
diff changeset
394 Matched : out GNAT.Regpat.Match_Array;
kono
parents:
diff changeset
395 Timeout : Integer := 10_000;
kono
parents:
diff changeset
396 Full_Buffer : Boolean := False);
kono
parents:
diff changeset
397 -- Same as above, but with a precompiled regular expression
kono
parents:
diff changeset
398
kono
parents:
diff changeset
399 -------------------------------------------------------------
kono
parents:
diff changeset
400 -- Working on the output (single process, multiple regexp) --
kono
parents:
diff changeset
401 -------------------------------------------------------------
kono
parents:
diff changeset
402
kono
parents:
diff changeset
403 type Regexp_Array is array (Positive range <>) of GNAT.OS_Lib.String_Access;
kono
parents:
diff changeset
404
kono
parents:
diff changeset
405 type Pattern_Matcher_Access is access all GNAT.Regpat.Pattern_Matcher;
kono
parents:
diff changeset
406 type Compiled_Regexp_Array is
kono
parents:
diff changeset
407 array (Positive range <>) of Pattern_Matcher_Access;
kono
parents:
diff changeset
408
kono
parents:
diff changeset
409 function "+"
kono
parents:
diff changeset
410 (P : GNAT.Regpat.Pattern_Matcher) return Pattern_Matcher_Access;
kono
parents:
diff changeset
411 -- Allocate some memory for the pattern matcher. This is only a convenience
kono
parents:
diff changeset
412 -- function to help create the array of compiled regular expressions.
kono
parents:
diff changeset
413
kono
parents:
diff changeset
414 procedure Expect
kono
parents:
diff changeset
415 (Descriptor : in out Process_Descriptor;
kono
parents:
diff changeset
416 Result : out Expect_Match;
kono
parents:
diff changeset
417 Regexps : Regexp_Array;
kono
parents:
diff changeset
418 Timeout : Integer := 10_000;
kono
parents:
diff changeset
419 Full_Buffer : Boolean := False);
kono
parents:
diff changeset
420 -- Wait till a string matching one of the regular expressions in Regexps
kono
parents:
diff changeset
421 -- is found. This function returns the index of the regexp that matched.
kono
parents:
diff changeset
422 -- This command is blocking, but will timeout after Timeout milliseconds.
kono
parents:
diff changeset
423 -- In that case, Timeout is returned.
kono
parents:
diff changeset
424
kono
parents:
diff changeset
425 procedure Expect
kono
parents:
diff changeset
426 (Descriptor : in out Process_Descriptor;
kono
parents:
diff changeset
427 Result : out Expect_Match;
kono
parents:
diff changeset
428 Regexps : Compiled_Regexp_Array;
kono
parents:
diff changeset
429 Timeout : Integer := 10_000;
kono
parents:
diff changeset
430 Full_Buffer : Boolean := False);
kono
parents:
diff changeset
431 -- Same as the previous one, but with precompiled regular expressions.
kono
parents:
diff changeset
432 -- This can be much faster if you are using them multiple times.
kono
parents:
diff changeset
433
kono
parents:
diff changeset
434 procedure Expect
kono
parents:
diff changeset
435 (Descriptor : in out Process_Descriptor;
kono
parents:
diff changeset
436 Result : out Expect_Match;
kono
parents:
diff changeset
437 Regexps : Regexp_Array;
kono
parents:
diff changeset
438 Matched : out GNAT.Regpat.Match_Array;
kono
parents:
diff changeset
439 Timeout : Integer := 10_000;
kono
parents:
diff changeset
440 Full_Buffer : Boolean := False);
kono
parents:
diff changeset
441 -- Same as above, except that you can also access the parenthesis
kono
parents:
diff changeset
442 -- groups inside the matching regular expression.
kono
parents:
diff changeset
443 --
kono
parents:
diff changeset
444 -- The first index in Matched must be 0, or Constraint_Error will be
kono
parents:
diff changeset
445 -- raised. The index 0 contains the indexes for the whole string that was
kono
parents:
diff changeset
446 -- matched, the index 1 contains the indexes for the first parentheses
kono
parents:
diff changeset
447 -- pair, and so on.
kono
parents:
diff changeset
448
kono
parents:
diff changeset
449 procedure Expect
kono
parents:
diff changeset
450 (Descriptor : in out Process_Descriptor;
kono
parents:
diff changeset
451 Result : out Expect_Match;
kono
parents:
diff changeset
452 Regexps : Compiled_Regexp_Array;
kono
parents:
diff changeset
453 Matched : out GNAT.Regpat.Match_Array;
kono
parents:
diff changeset
454 Timeout : Integer := 10_000;
kono
parents:
diff changeset
455 Full_Buffer : Boolean := False);
kono
parents:
diff changeset
456 -- Same as above, but with precompiled regular expressions. The first index
kono
parents:
diff changeset
457 -- in Matched must be 0, or Constraint_Error will be raised.
kono
parents:
diff changeset
458
kono
parents:
diff changeset
459 -------------------------------------------
kono
parents:
diff changeset
460 -- Working on the output (multi-process) --
kono
parents:
diff changeset
461 -------------------------------------------
kono
parents:
diff changeset
462
kono
parents:
diff changeset
463 type Multiprocess_Regexp is record
kono
parents:
diff changeset
464 Descriptor : Process_Descriptor_Access;
kono
parents:
diff changeset
465 Regexp : Pattern_Matcher_Access;
kono
parents:
diff changeset
466 end record;
kono
parents:
diff changeset
467
kono
parents:
diff changeset
468 type Multiprocess_Regexp_Array is
kono
parents:
diff changeset
469 array (Positive range <>) of Multiprocess_Regexp;
kono
parents:
diff changeset
470
kono
parents:
diff changeset
471 procedure Free (Regexp : in out Multiprocess_Regexp);
kono
parents:
diff changeset
472 -- Free the memory occupied by Regexp
kono
parents:
diff changeset
473
kono
parents:
diff changeset
474 function Has_Process (Regexp : Multiprocess_Regexp_Array) return Boolean;
kono
parents:
diff changeset
475 -- Return True if at least one entry in Regexp is non-null, ie there is
kono
parents:
diff changeset
476 -- still at least one process to monitor
kono
parents:
diff changeset
477
kono
parents:
diff changeset
478 function First_Dead_Process
kono
parents:
diff changeset
479 (Regexp : Multiprocess_Regexp_Array) return Natural;
kono
parents:
diff changeset
480 -- Find the first entry in Regexp that corresponds to a dead process that
kono
parents:
diff changeset
481 -- wasn't Free-d yet. This function is called in general when Expect
kono
parents:
diff changeset
482 -- (below) raises the exception Process_Died. This returns 0 if no process
kono
parents:
diff changeset
483 -- has died yet.
kono
parents:
diff changeset
484
kono
parents:
diff changeset
485 procedure Expect
kono
parents:
diff changeset
486 (Result : out Expect_Match;
kono
parents:
diff changeset
487 Regexps : Multiprocess_Regexp_Array;
kono
parents:
diff changeset
488 Matched : out GNAT.Regpat.Match_Array;
kono
parents:
diff changeset
489 Timeout : Integer := 10_000;
kono
parents:
diff changeset
490 Full_Buffer : Boolean := False);
kono
parents:
diff changeset
491 -- Same as above, but for multi processes. Any of the entries in
kono
parents:
diff changeset
492 -- Regexps can have a null Descriptor or Regexp. Such entries will
kono
parents:
diff changeset
493 -- simply be ignored. Therefore when a process terminates, you can
kono
parents:
diff changeset
494 -- simply reset its entry.
kono
parents:
diff changeset
495 --
kono
parents:
diff changeset
496 -- The expect loop would therefore look like:
kono
parents:
diff changeset
497 --
kono
parents:
diff changeset
498 -- Processes : Multiprocess_Regexp_Array (...) := ...;
kono
parents:
diff changeset
499 -- R : Natural;
kono
parents:
diff changeset
500 --
kono
parents:
diff changeset
501 -- while Has_Process (Processes) loop
kono
parents:
diff changeset
502 -- begin
kono
parents:
diff changeset
503 -- Expect (Result, Processes, Timeout => -1);
kono
parents:
diff changeset
504 -- ... process output of process Result (output, full buffer,...)
kono
parents:
diff changeset
505 --
kono
parents:
diff changeset
506 -- exception
kono
parents:
diff changeset
507 -- when Process_Died =>
kono
parents:
diff changeset
508 -- -- Free memory
kono
parents:
diff changeset
509 -- R := First_Dead_Process (Processes);
kono
parents:
diff changeset
510 -- Close (Processes (R).Descriptor.all, Status);
kono
parents:
diff changeset
511 -- Free (Processes (R));
kono
parents:
diff changeset
512 -- end;
kono
parents:
diff changeset
513 -- end loop;
kono
parents:
diff changeset
514
kono
parents:
diff changeset
515 procedure Expect
kono
parents:
diff changeset
516 (Result : out Expect_Match;
kono
parents:
diff changeset
517 Regexps : Multiprocess_Regexp_Array;
kono
parents:
diff changeset
518 Timeout : Integer := 10_000;
kono
parents:
diff changeset
519 Full_Buffer : Boolean := False);
kono
parents:
diff changeset
520 -- Same as the previous one, but for multiple processes. This procedure
kono
parents:
diff changeset
521 -- finds the first regexp that match the associated process.
kono
parents:
diff changeset
522
kono
parents:
diff changeset
523 ------------------------
kono
parents:
diff changeset
524 -- Getting the output --
kono
parents:
diff changeset
525 ------------------------
kono
parents:
diff changeset
526
kono
parents:
diff changeset
527 procedure Flush
kono
parents:
diff changeset
528 (Descriptor : in out Process_Descriptor;
kono
parents:
diff changeset
529 Timeout : Integer := 0);
kono
parents:
diff changeset
530 -- Discard all output waiting from the process.
kono
parents:
diff changeset
531 --
kono
parents:
diff changeset
532 -- This output is simply discarded, and no filter is called. This output
kono
parents:
diff changeset
533 -- will also not be visible by the next call to Expect, nor will any output
kono
parents:
diff changeset
534 -- currently buffered.
kono
parents:
diff changeset
535 --
kono
parents:
diff changeset
536 -- Timeout is the delay for which we wait for output to be available from
kono
parents:
diff changeset
537 -- the process. If 0, we only get what is immediately available.
kono
parents:
diff changeset
538
kono
parents:
diff changeset
539 function Expect_Out (Descriptor : Process_Descriptor) return String;
kono
parents:
diff changeset
540 -- Return the string matched by the last Expect call.
kono
parents:
diff changeset
541 --
kono
parents:
diff changeset
542 -- The returned string is in fact the concatenation of all the strings read
kono
parents:
diff changeset
543 -- from the file descriptor up to, and including, the characters that
kono
parents:
diff changeset
544 -- matched the regular expression.
kono
parents:
diff changeset
545 --
kono
parents:
diff changeset
546 -- For instance, with an input "philosophic", and a regular expression "hi"
kono
parents:
diff changeset
547 -- in the call to expect, the strings returned the first and second time
kono
parents:
diff changeset
548 -- would be respectively "phi" and "losophi".
kono
parents:
diff changeset
549
kono
parents:
diff changeset
550 function Expect_Out_Match (Descriptor : Process_Descriptor) return String;
kono
parents:
diff changeset
551 -- Return the string matched by the last Expect call.
kono
parents:
diff changeset
552 --
kono
parents:
diff changeset
553 -- The returned string includes only the character that matched the
kono
parents:
diff changeset
554 -- specific regular expression. All the characters that came before are
kono
parents:
diff changeset
555 -- simply discarded.
kono
parents:
diff changeset
556 --
kono
parents:
diff changeset
557 -- For instance, with an input "philosophic", and a regular expression
kono
parents:
diff changeset
558 -- "hi" in the call to expect, the strings returned the first and second
kono
parents:
diff changeset
559 -- time would both be "hi".
kono
parents:
diff changeset
560
kono
parents:
diff changeset
561 ----------------
kono
parents:
diff changeset
562 -- Exceptions --
kono
parents:
diff changeset
563 ----------------
kono
parents:
diff changeset
564
kono
parents:
diff changeset
565 Invalid_Process : exception;
kono
parents:
diff changeset
566 -- Raised by most subprograms above when the parameter Descriptor is not a
kono
parents:
diff changeset
567 -- valid process or is a closed process.
kono
parents:
diff changeset
568
kono
parents:
diff changeset
569 Process_Died : exception;
kono
parents:
diff changeset
570 -- Raised by all the expect subprograms if Descriptor was originally a
kono
parents:
diff changeset
571 -- valid process that died while Expect was executing. It is also raised
kono
parents:
diff changeset
572 -- when Expect receives an end-of-file.
kono
parents:
diff changeset
573
kono
parents:
diff changeset
574 private
kono
parents:
diff changeset
575 type Filter_List_Elem;
kono
parents:
diff changeset
576 type Filter_List is access Filter_List_Elem;
kono
parents:
diff changeset
577 type Filter_List_Elem is record
kono
parents:
diff changeset
578 Filter : Filter_Function;
kono
parents:
diff changeset
579 User_Data : System.Address;
kono
parents:
diff changeset
580 Filter_On : Filter_Type;
kono
parents:
diff changeset
581 Next : Filter_List;
kono
parents:
diff changeset
582 end record;
kono
parents:
diff changeset
583
kono
parents:
diff changeset
584 type Pipe_Type is record
kono
parents:
diff changeset
585 Input, Output : GNAT.OS_Lib.File_Descriptor;
kono
parents:
diff changeset
586 end record;
kono
parents:
diff changeset
587 -- This type represents a pipe, used to communicate between two processes
kono
parents:
diff changeset
588
kono
parents:
diff changeset
589 procedure Set_Up_Communications
kono
parents:
diff changeset
590 (Pid : in out Process_Descriptor;
kono
parents:
diff changeset
591 Err_To_Out : Boolean;
kono
parents:
diff changeset
592 Pipe1 : not null access Pipe_Type;
kono
parents:
diff changeset
593 Pipe2 : not null access Pipe_Type;
kono
parents:
diff changeset
594 Pipe3 : not null access Pipe_Type);
kono
parents:
diff changeset
595 -- Set up all the communication pipes and file descriptors prior to
kono
parents:
diff changeset
596 -- spawning the child process.
kono
parents:
diff changeset
597
kono
parents:
diff changeset
598 procedure Set_Up_Parent_Communications
kono
parents:
diff changeset
599 (Pid : in out Process_Descriptor;
kono
parents:
diff changeset
600 Pipe1 : in out Pipe_Type;
kono
parents:
diff changeset
601 Pipe2 : in out Pipe_Type;
kono
parents:
diff changeset
602 Pipe3 : in out Pipe_Type);
kono
parents:
diff changeset
603 -- Finish the set up of the pipes while in the parent process
kono
parents:
diff changeset
604
kono
parents:
diff changeset
605 procedure Set_Up_Child_Communications
kono
parents:
diff changeset
606 (Pid : in out Process_Descriptor;
kono
parents:
diff changeset
607 Pipe1 : in out Pipe_Type;
kono
parents:
diff changeset
608 Pipe2 : in out Pipe_Type;
kono
parents:
diff changeset
609 Pipe3 : in out Pipe_Type;
kono
parents:
diff changeset
610 Cmd : String;
kono
parents:
diff changeset
611 Args : System.Address);
kono
parents:
diff changeset
612 -- Finish the set up of the pipes while in the child process This also
kono
parents:
diff changeset
613 -- spawns the child process (based on Cmd). On systems that support fork,
kono
parents:
diff changeset
614 -- this procedure is executed inside the newly created process.
kono
parents:
diff changeset
615
kono
parents:
diff changeset
616 type Process_Descriptor is tagged record
kono
parents:
diff changeset
617 Pid : aliased Process_Id := Invalid_Pid;
kono
parents:
diff changeset
618 Input_Fd : GNAT.OS_Lib.File_Descriptor := GNAT.OS_Lib.Invalid_FD;
kono
parents:
diff changeset
619 Output_Fd : GNAT.OS_Lib.File_Descriptor := GNAT.OS_Lib.Invalid_FD;
kono
parents:
diff changeset
620 Error_Fd : GNAT.OS_Lib.File_Descriptor := GNAT.OS_Lib.Invalid_FD;
kono
parents:
diff changeset
621 Filters_Lock : Integer := 0;
kono
parents:
diff changeset
622
kono
parents:
diff changeset
623 Filters : Filter_List := null;
kono
parents:
diff changeset
624
kono
parents:
diff changeset
625 Buffer : GNAT.OS_Lib.String_Access := null;
kono
parents:
diff changeset
626 Buffer_Size : Natural := 0;
kono
parents:
diff changeset
627 Buffer_Index : Natural := 0;
kono
parents:
diff changeset
628
kono
parents:
diff changeset
629 Last_Match_Start : Natural := 0;
kono
parents:
diff changeset
630 Last_Match_End : Natural := 0;
kono
parents:
diff changeset
631 end record;
kono
parents:
diff changeset
632
kono
parents:
diff changeset
633 -- The following subprogram is provided for use in the body, and also
kono
parents:
diff changeset
634 -- possibly in future child units providing extensions to this package.
kono
parents:
diff changeset
635
kono
parents:
diff changeset
636 procedure Portable_Execvp
kono
parents:
diff changeset
637 (Pid : not null access Process_Id;
kono
parents:
diff changeset
638 Cmd : String;
kono
parents:
diff changeset
639 Args : System.Address);
kono
parents:
diff changeset
640 pragma Import (C, Portable_Execvp, "__gnat_expect_portable_execvp");
kono
parents:
diff changeset
641 -- Executes, in a portable way, the command Cmd (full path must be
kono
parents:
diff changeset
642 -- specified), with the given Args, which must be an array of string
kono
parents:
diff changeset
643 -- pointers. Note that the first element in Args must be the executable
kono
parents:
diff changeset
644 -- name, and the last element must be a null pointer. The returned value
kono
parents:
diff changeset
645 -- in Pid is the process ID, or zero if not supported on the platform.
kono
parents:
diff changeset
646
kono
parents:
diff changeset
647 end GNAT.Expect;