Mark ChangeLog
[official-gcc.git] / gcc / ada / g-expect.ads
blob1bd58a639477a211e5e5d35968184cb3994bbc55
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- G N A T . E X P E C T --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 2000-2004 Ada Core Technologies, Inc. --
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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 -- Currently this package is implemented on all native GNAT ports except
35 -- for VMS. It is not yet implemented for any of the cross-ports (e.g. it
36 -- is not available for VxWorks or LynxOS).
38 -- Usage
39 -- =====
41 -- This package provides a set of subprograms similar to what is available
42 -- with the standard Tcl Expect tool.
44 -- It allows you to easily spawn and communicate with an external process.
45 -- You can send commands or inputs to the process, and compare the output
46 -- with some expected regular expression.
48 -- Usage example:
50 -- Non_Blocking_Spawn
51 -- (Fd, "ftp",
52 -- (1 => new String' ("machine@domain")));
53 -- Timeout := 10000; -- 10 seconds
54 -- Expect (Fd, Result, Regexp_Array'(+"\(user\)", +"\(passwd\)"),
55 -- Timeout);
56 -- case Result is
57 -- when 1 => Send (Fd, "my_name"); -- matched "user"
58 -- when 2 => Send (Fd, "my_passwd"); -- matched "passwd"
59 -- when Expect_Timeout => null; -- timeout
60 -- when others => null;
61 -- end case;
62 -- Close (Fd);
64 -- You can also combine multiple regular expressions together, and get the
65 -- specific string matching a parenthesis pair by doing something like. If you
66 -- expect either "lang=optional ada" or "lang=ada" from the external process,
67 -- you can group the two together, which is more efficient, and simply get the
68 -- name of the language by doing:
70 -- declare
71 -- Matched : Match_Array (0 .. 2);
72 -- begin
73 -- Expect (Fd, Result, "lang=(optional)? ([a-z]+)", Matched);
74 -- Put_Line ("Seen: " &
75 -- Expect_Out (Fd) (Matched (2).First .. Matched (2).Last));
76 -- end;
78 -- Alternatively, you might choose to use a lower-level interface to the
79 -- processes, where you can give your own input and output filters every
80 -- time characters are read from or written to the process.
82 -- procedure My_Filter
83 -- (Descriptor : Process_Descriptor'Class;
84 -- Str : String;
85 -- User_Data : System.Address)
86 -- is
87 -- begin
88 -- Put_Line (Str);
89 -- end;
91 -- Non_Blocking_Spawn
92 -- (Fd, "tail",
93 -- (new String' ("-f"), new String' ("a_file")));
94 -- Add_Filter (Fd, My_Filter'Access, Output);
95 -- Expect (Fd, Result, "", 0); -- wait forever
97 -- The above example should probably be run in a separate task, since it is
98 -- blocking on the call to Expect.
100 -- Both examples can be combined, for instance to systematically print the
101 -- output seen by expect, even though you still want to let Expect do the
102 -- filtering. You can use the Trace_Filter subprogram for such a filter.
104 -- If you want to get the output of a simple command, and ignore any previous
105 -- existing output, it is recommended to do something like:
107 -- Expect (Fd, Result, ".*", Timeout => 0);
108 -- -- Empty the buffer, by matching everything (after checking
109 -- -- if there was any input).
111 -- Send (Fd, "command");
112 -- Expect (Fd, Result, ".."); -- match only on the output of command
114 -- Task Safety
115 -- ===========
117 -- This package is not task-safe: there should be not concurrent calls to
118 -- the functions defined in this package.
120 with System;
121 with GNAT.OS_Lib;
122 with GNAT.Regpat;
124 package GNAT.Expect is
126 type Process_Id is new Integer;
127 Invalid_Pid : constant Process_Id := -1;
128 Null_Pid : constant Process_Id := 0;
130 type Filter_Type is (Output, Input, Died);
131 -- The signals that are emitted by the Process_Descriptor upon state
132 -- changed in the child. One can connect to any of this signal through
133 -- the Add_Filter subprograms.
135 -- Output => Every time new characters are read from the process
136 -- associated with Descriptor, the filter is called with
137 -- these new characters in argument.
139 -- Note that output is only generated when the program is
140 -- blocked in a call to Expect.
142 -- Input => Every time new characters are written to the process
143 -- associated with Descriptor, the filter is called with
144 -- these new characters in argument.
145 -- Note that input is only generated by calls to Send.
147 -- Died => The child process has died, or was explicitly killed
149 type Process_Descriptor is tagged private;
150 -- Contains all the components needed to describe a process handled
151 -- in this package, including a process identifier, file descriptors
152 -- associated with the standard input, output and error, and the buffer
153 -- needed to handle the expect calls.
155 type Process_Descriptor_Access is access Process_Descriptor'Class;
157 ------------------------
158 -- Spawning a process --
159 ------------------------
161 procedure Non_Blocking_Spawn
162 (Descriptor : out Process_Descriptor'Class;
163 Command : String;
164 Args : GNAT.OS_Lib.Argument_List;
165 Buffer_Size : Natural := 4096;
166 Err_To_Out : Boolean := False);
167 -- This call spawns a new process and allows sending commands to
168 -- the process and/or automatic parsing of the output.
170 -- The expect buffer associated with that process can contain at most
171 -- Buffer_Size characters. Older characters are simply discarded when
172 -- this buffer is full. Beware that if the buffer is too big, this could
173 -- slow down the Expect calls if not output is matched, since Expect has
174 -- to match all the regexp against all the characters in the buffer.
175 -- If Buffer_Size is 0, there is no limit (ie all the characters are kept
176 -- till Expect matches), but this is slower.
178 -- If Err_To_Out is True, then the standard error of the spawned process is
179 -- connected to the standard output. This is the only way to get the
180 -- Except subprograms also match on output on standard error.
182 -- Invalid_Process is raised if the process could not be spawned.
184 procedure Close (Descriptor : in out Process_Descriptor);
185 -- Terminate the process and close the pipes to it. It implicitly
186 -- does the 'wait' command required to clean up the process table.
187 -- This also frees the buffer associated with the process id.
189 procedure Close
190 (Descriptor : in out Process_Descriptor;
191 Status : out Integer);
192 -- Same as above, but also returns the exit status of the process,
193 -- as set for example by the procedure GNAT.OS_Lib.OS_Exit.
195 procedure Send_Signal
196 (Descriptor : Process_Descriptor;
197 Signal : Integer);
198 -- Send a given signal to the process.
200 procedure Interrupt (Descriptor : in out Process_Descriptor);
201 -- Interrupt the process (the equivalent of Ctrl-C on unix and windows)
202 -- and call close if the process dies.
204 function Get_Input_Fd
205 (Descriptor : Process_Descriptor)
206 return GNAT.OS_Lib.File_Descriptor;
207 -- Return the input file descriptor associated with Descriptor.
209 function Get_Output_Fd
210 (Descriptor : Process_Descriptor)
211 return GNAT.OS_Lib.File_Descriptor;
212 -- Return the output file descriptor associated with Descriptor.
214 function Get_Error_Fd
215 (Descriptor : Process_Descriptor)
216 return GNAT.OS_Lib.File_Descriptor;
217 -- Return the error output file descriptor associated with Descriptor.
219 function Get_Pid
220 (Descriptor : Process_Descriptor)
221 return Process_Id;
222 -- Return the process id assocated with a given process descriptor.
224 --------------------
225 -- Adding filters --
226 --------------------
228 -- This is a rather low-level interface to subprocesses, since basically
229 -- the filtering is left entirely to the user. See the Expect subprograms
230 -- below for higher level functions.
232 type Filter_Function is access
233 procedure
234 (Descriptor : Process_Descriptor'Class;
235 Str : String;
236 User_Data : System.Address := System.Null_Address);
237 -- Function called every time new characters are read from or written
238 -- to the process.
240 -- Str is a string of all these characters.
242 -- User_Data, if specified, is a user specific data that will be passed to
243 -- the filter. Note that no checks are done on this parameter that should
244 -- be used with cautiousness.
246 procedure Add_Filter
247 (Descriptor : in out Process_Descriptor;
248 Filter : Filter_Function;
249 Filter_On : Filter_Type := Output;
250 User_Data : System.Address := System.Null_Address;
251 After : Boolean := False);
252 -- Add a new filter for one of the filter type. This filter will be
253 -- run before all the existing filters, unless After is set True,
254 -- in which case it will be run after existing filters. User_Data
255 -- is passed as is to the filter procedure.
257 procedure Remove_Filter
258 (Descriptor : in out Process_Descriptor;
259 Filter : Filter_Function);
260 -- Remove a filter from the list of filters (whatever the type of the
261 -- filter).
263 procedure Trace_Filter
264 (Descriptor : Process_Descriptor'Class;
265 Str : String;
266 User_Data : System.Address := System.Null_Address);
267 -- Function that can be used a filter and that simply outputs Str on
268 -- Standard_Output. This is mainly used for debugging purposes.
269 -- User_Data is ignored.
271 procedure Lock_Filters (Descriptor : in out Process_Descriptor);
272 -- Temporarily disables all output and input filters. They will be
273 -- reactivated only when Unlock_Filters has been called as many times as
274 -- Lock_Filters;
276 procedure Unlock_Filters (Descriptor : in out Process_Descriptor);
277 -- Unlocks the filters. They are reactivated only if Unlock_Filters
278 -- has been called as many times as Lock_Filters.
280 ------------------
281 -- Sending data --
282 ------------------
284 procedure Send
285 (Descriptor : in out Process_Descriptor;
286 Str : String;
287 Add_LF : Boolean := True;
288 Empty_Buffer : Boolean := False);
289 -- Send a string to the file descriptor.
291 -- The string is not formatted in any way, except if Add_LF is True,
292 -- in which case an ASCII.LF is added at the end, so that Str is
293 -- recognized as a command by the external process.
295 -- If Empty_Buffer is True, any input waiting from the process (or in the
296 -- buffer) is first discarded before the command is sent. The output
297 -- filters are of course called as usual.
299 -----------------------------------------------------------
300 -- Working on the output (single process, simple regexp) --
301 -----------------------------------------------------------
303 type Expect_Match is new Integer;
304 Expect_Full_Buffer : constant Expect_Match := -1;
305 -- If the buffer was full and some characters were discarded.
307 Expect_Timeout : constant Expect_Match := -2;
308 -- If not output matching the regexps was found before the timeout.
310 function "+" (S : String) return GNAT.OS_Lib.String_Access;
311 -- Allocate some memory for the string. This is merely a convenience
312 -- function to help create the array of regexps in the call to Expect.
314 procedure Expect
315 (Descriptor : in out Process_Descriptor;
316 Result : out Expect_Match;
317 Regexp : String;
318 Timeout : Integer := 10000;
319 Full_Buffer : Boolean := False);
320 -- Wait till a string matching Fd can be read from Fd, and return 1
321 -- if a match was found.
323 -- It consumes all the characters read from Fd until a match found, and
324 -- then sets the return values for the subprograms Expect_Out and
325 -- Expect_Out_Match.
327 -- The empty string "" will never match, and can be used if you only want
328 -- to match after a specific timeout. Beware that if Timeout is -1 at the
329 -- time, the current task will be blocked forever.
331 -- This command times out after Timeout milliseconds (or never if Timeout
332 -- is -1). In that case, Expect_Timeout is returned. The value returned by
333 -- Expect_Out and Expect_Out_Match are meaningless in that case.
335 -- Note that using a timeout of 0ms leads to unpredictable behavior, since
336 -- the result depends on whether the process has already sent some output
337 -- the first time Expect checks, and this depends on the operating system.
339 -- The regular expression must obey the syntax described in GNAT.Regpat.
341 -- If Full_Buffer is True, then Expect will match if the buffer was too
342 -- small and some characters were about to be discarded. In that case,
343 -- Expect_Full_Buffer is returned.
345 procedure Expect
346 (Descriptor : in out Process_Descriptor;
347 Result : out Expect_Match;
348 Regexp : GNAT.Regpat.Pattern_Matcher;
349 Timeout : Integer := 10000;
350 Full_Buffer : Boolean := False);
351 -- Same as the previous one, but with a precompiled regular expression.
352 -- This is more efficient however, especially if you are using this
353 -- expression multiple times, since this package won't need to recompile
354 -- the regexp every time.
356 procedure Expect
357 (Descriptor : in out Process_Descriptor;
358 Result : out Expect_Match;
359 Regexp : String;
360 Matched : out GNAT.Regpat.Match_Array;
361 Timeout : Integer := 10000;
362 Full_Buffer : Boolean := False);
363 -- Same as above, but it is now possible to get the indexes of the
364 -- substrings for the parentheses in the regexp (see the example at the
365 -- top of this package, as well as the documentation in the package
366 -- GNAT.Regpat).
368 -- Matched'First should be 0, and this index will contain the indexes for
369 -- the whole string that was matched. The index 1 will contain the indexes
370 -- for the first parentheses-pair, and so on.
372 ------------
373 -- Expect --
374 ------------
376 procedure Expect
377 (Descriptor : in out Process_Descriptor;
378 Result : out Expect_Match;
379 Regexp : GNAT.Regpat.Pattern_Matcher;
380 Matched : out GNAT.Regpat.Match_Array;
381 Timeout : Integer := 10000;
382 Full_Buffer : Boolean := False);
383 -- Same as above, but with a precompiled regular expression.
385 -------------------------------------------------------------
386 -- Working on the output (single process, multiple regexp) --
387 -------------------------------------------------------------
389 type Regexp_Array is array (Positive range <>) of GNAT.OS_Lib.String_Access;
391 type Pattern_Matcher_Access is access GNAT.Regpat.Pattern_Matcher;
392 type Compiled_Regexp_Array is array (Positive range <>)
393 of Pattern_Matcher_Access;
395 function "+"
396 (P : GNAT.Regpat.Pattern_Matcher)
397 return Pattern_Matcher_Access;
398 -- Allocate some memory for the pattern matcher.
399 -- This is only a convenience function to help create the array of
400 -- compiled regular expressoins.
402 procedure Expect
403 (Descriptor : in out Process_Descriptor;
404 Result : out Expect_Match;
405 Regexps : Regexp_Array;
406 Timeout : Integer := 10000;
407 Full_Buffer : Boolean := False);
408 -- Wait till a string matching one of the regular expressions in Regexps
409 -- is found. This function returns the index of the regexp that matched.
410 -- This command is blocking, but will timeout after Timeout milliseconds.
411 -- In that case, Timeout is returned.
413 procedure Expect
414 (Descriptor : in out Process_Descriptor;
415 Result : out Expect_Match;
416 Regexps : Compiled_Regexp_Array;
417 Timeout : Integer := 10000;
418 Full_Buffer : Boolean := False);
419 -- Same as the previous one, but with precompiled regular expressions.
420 -- This can be much faster if you are using them multiple times.
422 procedure Expect
423 (Descriptor : in out Process_Descriptor;
424 Result : out Expect_Match;
425 Regexps : Regexp_Array;
426 Matched : out GNAT.Regpat.Match_Array;
427 Timeout : Integer := 10000;
428 Full_Buffer : Boolean := False);
429 -- Same as above, except that you can also access the parenthesis
430 -- groups inside the matching regular expression.
431 -- The first index in Matched must be 0, or Constraint_Error will be
432 -- raised. The index 0 contains the indexes for the whole string that was
433 -- matched, the index 1 contains the indexes for the first parentheses
434 -- pair, and so on.
436 procedure Expect
437 (Descriptor : in out Process_Descriptor;
438 Result : out Expect_Match;
439 Regexps : Compiled_Regexp_Array;
440 Matched : out GNAT.Regpat.Match_Array;
441 Timeout : Integer := 10000;
442 Full_Buffer : Boolean := False);
443 -- Same as above, but with precompiled regular expressions.
444 -- The first index in Matched must be 0, or Constraint_Error will be
445 -- raised.
447 -------------------------------------------
448 -- Working on the output (multi-process) --
449 -------------------------------------------
451 type Multiprocess_Regexp is record
452 Descriptor : Process_Descriptor_Access;
453 Regexp : Pattern_Matcher_Access;
454 end record;
455 type Multiprocess_Regexp_Array is array (Positive range <>)
456 of Multiprocess_Regexp;
458 procedure Expect
459 (Result : out Expect_Match;
460 Regexps : Multiprocess_Regexp_Array;
461 Matched : out GNAT.Regpat.Match_Array;
462 Timeout : Integer := 10000;
463 Full_Buffer : Boolean := False);
464 -- Same as above, but for multi processes.
466 procedure Expect
467 (Result : out Expect_Match;
468 Regexps : Multiprocess_Regexp_Array;
469 Timeout : Integer := 10000;
470 Full_Buffer : Boolean := False);
471 -- Same as the previous one, but for multiple processes.
472 -- This procedure finds the first regexp that match the associated process.
474 ------------------------
475 -- Getting the output --
476 ------------------------
478 procedure Flush
479 (Descriptor : in out Process_Descriptor;
480 Timeout : Integer := 0);
481 -- Discard all output waiting from the process.
483 -- This output is simply discarded, and no filter is called. This output
484 -- will also not be visible by the next call to Expect, nor will any
485 -- output currently buffered.
487 -- Timeout is the delay for which we wait for output to be available from
488 -- the process. If 0, we only get what is immediately available.
490 function Expect_Out (Descriptor : Process_Descriptor) return String;
491 -- Return the string matched by the last Expect call.
493 -- The returned string is in fact the concatenation of all the strings
494 -- read from the file descriptor up to, and including, the characters
495 -- that matched the regular expression.
497 -- For instance, with an input "philosophic", and a regular expression
498 -- "hi" in the call to expect, the strings returned the first and second
499 -- time would be respectively "phi" and "losophi".
501 function Expect_Out_Match (Descriptor : Process_Descriptor) return String;
502 -- Return the string matched by the last Expect call.
504 -- The returned string includes only the character that matched the
505 -- specific regular expression. All the characters that came before are
506 -- simply discarded.
508 -- For instance, with an input "philosophic", and a regular expression
509 -- "hi" in the call to expect, the strings returned the first and second
510 -- time would both be "hi".
512 ----------------
513 -- Exceptions --
514 ----------------
516 Invalid_Process : exception;
517 -- Raised by most subprograms above when the parameter Descriptor is not a
518 -- valid process or is a closed process.
520 Process_Died : exception;
521 -- Raised by all the expect subprograms if Descriptor was originally a
522 -- valid process that died while Expect was executing. It is also raised
523 -- when Expect receives an end-of-file.
525 private
526 type Filter_List_Elem;
527 type Filter_List is access Filter_List_Elem;
528 type Filter_List_Elem is record
529 Filter : Filter_Function;
530 User_Data : System.Address;
531 Filter_On : Filter_Type;
532 Next : Filter_List;
533 end record;
535 type Pipe_Type is record
536 Input, Output : GNAT.OS_Lib.File_Descriptor;
537 end record;
538 -- This type represents a pipe, used to communicate between two processes.
540 procedure Set_Up_Communications
541 (Pid : in out Process_Descriptor;
542 Err_To_Out : Boolean;
543 Pipe1 : access Pipe_Type;
544 Pipe2 : access Pipe_Type;
545 Pipe3 : access Pipe_Type);
546 -- Set up all the communication pipes and file descriptors prior to
547 -- spawning the child process.
549 procedure Set_Up_Parent_Communications
550 (Pid : in out Process_Descriptor;
551 Pipe1 : in out Pipe_Type;
552 Pipe2 : in out Pipe_Type;
553 Pipe3 : in out Pipe_Type);
554 -- Finish the set up of the pipes while in the parent process
556 procedure Set_Up_Child_Communications
557 (Pid : in out Process_Descriptor;
558 Pipe1 : in out Pipe_Type;
559 Pipe2 : in out Pipe_Type;
560 Pipe3 : in out Pipe_Type;
561 Cmd : String;
562 Args : System.Address);
563 -- Finish the set up of the pipes while in the child process
564 -- This also spawns the child process (based on Cmd).
565 -- On systems that support fork, this procedure is executed inside the
566 -- newly created process.
568 type Process_Descriptor is tagged record
569 Pid : aliased Process_Id := Invalid_Pid;
570 Input_Fd : GNAT.OS_Lib.File_Descriptor := GNAT.OS_Lib.Invalid_FD;
571 Output_Fd : GNAT.OS_Lib.File_Descriptor := GNAT.OS_Lib.Invalid_FD;
572 Error_Fd : GNAT.OS_Lib.File_Descriptor := GNAT.OS_Lib.Invalid_FD;
573 Filters_Lock : Integer := 0;
575 Filters : Filter_List := null;
577 Buffer : GNAT.OS_Lib.String_Access := null;
578 Buffer_Size : Natural := 0;
579 Buffer_Index : Natural := 0;
581 Last_Match_Start : Natural := 0;
582 Last_Match_End : Natural := 0;
583 end record;
585 -- The following subprogram is provided for use in the body, and also
586 -- possibly in future child units providing extensions to this package.
588 procedure Portable_Execvp
589 (Pid : access Process_Id;
590 Cmd : String;
591 Args : System.Address);
592 pragma Import (C, Portable_Execvp, "__gnat_expect_portable_execvp");
593 -- Executes, in a portable way, the command Cmd (full path must be
594 -- specified), with the given Args. Args must be an array of string
595 -- pointers. Note that the first element in Args must be the executable
596 -- name, and the last element must be a null pointer. The returned value
597 -- in Pid is the process ID, or zero if not supported on the platform.
599 end GNAT.Expect;