1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- G N A T . E X P E C T --
9 -- Copyright (C) 2000-2010, AdaCore --
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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
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. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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).
42 -- This package provides a set of subprograms similar to what is available
43 -- with the standard Tcl Expect tool.
45 -- It allows you to easily spawn and communicate with an external process.
46 -- You can send commands or inputs to the process, and compare the output
47 -- with some expected regular expression.
53 -- (1 => new String' ("machine@domain")));
54 -- Timeout := 10_000; -- 10 seconds
55 -- Expect (Fd, Result, Regexp_Array'(+"\(user\)", +"\(passwd\)"),
58 -- when 1 => Send (Fd, "my_name"); -- matched "user"
59 -- when 2 => Send (Fd, "my_passwd"); -- matched "passwd"
60 -- when Expect_Timeout => null; -- timeout
61 -- when others => null;
65 -- You can also combine multiple regular expressions together, and get the
66 -- specific string matching a parenthesis pair by doing something like this:
67 -- If you expect either "lang=optional ada" or "lang=ada" from the external
68 -- process, you can group the two together, which is more efficient, and
69 -- simply get the name of the language by doing:
72 -- Matched : Match_Array (0 .. 2);
74 -- Expect (Fd, Result, "lang=(optional)? ([a-z]+)", Matched);
75 -- Put_Line ("Seen: " &
76 -- Expect_Out (Fd) (Matched (2).First .. Matched (2).Last));
79 -- Alternatively, you might choose to use a lower-level interface to the
80 -- processes, where you can give your own input and output filters every
81 -- time characters are read from or written to the process.
83 -- procedure My_Filter
84 -- (Descriptor : Process_Descriptor'Class;
86 -- User_Data : System.Address)
94 -- (new String' ("-f"), new String' ("a_file")));
95 -- Add_Filter (Fd, My_Filter'Access, Output);
96 -- Expect (Fd, Result, "", 0); -- wait forever
98 -- The above example should probably be run in a separate task, since it is
99 -- blocking on the call to Expect.
101 -- Both examples can be combined, for instance to systematically print the
102 -- output seen by expect, even though you still want to let Expect do the
103 -- filtering. You can use the Trace_Filter subprogram for such a filter.
105 -- If you want to get the output of a simple command, and ignore any previous
106 -- existing output, it is recommended to do something like:
108 -- Expect (Fd, Result, ".*", Timeout => 0);
109 -- -- Empty the buffer, by matching everything (after checking
110 -- -- if there was any input).
112 -- Send (Fd, "command");
113 -- Expect (Fd, Result, ".."); -- match only on the output of command
119 -- This package is not task-safe: there should not be concurrent calls to the
120 -- functions defined in this package. In other words, separate tasks must not
121 -- access the facilities of this package without synchronization that
122 -- serializes access.
128 package GNAT.Expect is
130 type Process_Id is new Integer;
131 Invalid_Pid : constant Process_Id := -1;
132 Null_Pid : constant Process_Id := 0;
134 type Filter_Type is (Output, Input, Died);
135 -- The signals that are emitted by the Process_Descriptor upon state change
136 -- in the child. One can connect to any of these signals through the
137 -- Add_Filter subprograms.
139 -- Output => Every time new characters are read from the process
140 -- associated with Descriptor, the filter is called with
141 -- these new characters in the argument.
143 -- Note that output is generated only when the program is
144 -- blocked in a call to Expect.
146 -- Input => Every time new characters are written to the process
147 -- associated with Descriptor, the filter is called with
148 -- these new characters in the argument.
149 -- Note that input is generated only by calls to Send.
151 -- Died => The child process has died, or was explicitly killed
153 type Process_Descriptor is tagged private;
154 -- Contains all the components needed to describe a process handled
155 -- in this package, including a process identifier, file descriptors
156 -- associated with the standard input, output and error, and the buffer
157 -- needed to handle the expect calls.
159 type Process_Descriptor_Access is access Process_Descriptor'Class;
161 ------------------------
162 -- Spawning a process --
163 ------------------------
165 procedure Non_Blocking_Spawn
166 (Descriptor : out Process_Descriptor'Class;
168 Args : GNAT.OS_Lib.Argument_List;
169 Buffer_Size : Natural := 4096;
170 Err_To_Out : Boolean := False);
171 -- This call spawns a new process and allows sending commands to
172 -- the process and/or automatic parsing of the output.
174 -- The expect buffer associated with that process can contain at most
175 -- Buffer_Size characters. Older characters are simply discarded when this
176 -- buffer is full. Beware that if the buffer is too big, this could slow
177 -- down the Expect calls if the output not is matched, since Expect has to
178 -- match all the regexp against all the characters in the buffer. If
179 -- Buffer_Size is 0, there is no limit (i.e. all the characters are kept
180 -- till Expect matches), but this is slower.
182 -- If Err_To_Out is True, then the standard error of the spawned process is
183 -- connected to the standard output. This is the only way to get the Except
184 -- subprograms to also match on output on standard error.
186 -- Invalid_Process is raised if the process could not be spawned.
188 procedure Close (Descriptor : in out Process_Descriptor);
189 -- Terminate the process and close the pipes to it. It implicitly does the
190 -- 'wait' command required to clean up the process table. This also frees
191 -- the buffer associated with the process id. Raise Invalid_Process if the
192 -- process id is invalid.
195 (Descriptor : in out Process_Descriptor;
196 Status : out Integer);
197 -- Same as above, but also returns the exit status of the process, as set
198 -- for example by the procedure GNAT.OS_Lib.OS_Exit.
200 procedure Send_Signal
201 (Descriptor : Process_Descriptor;
203 -- Send a given signal to the process. Raise Invalid_Process if the process
206 procedure Interrupt (Descriptor : in out Process_Descriptor);
207 -- Interrupt the process (the equivalent of Ctrl-C on unix and windows)
208 -- and call close if the process dies.
210 function Get_Input_Fd
211 (Descriptor : Process_Descriptor) return GNAT.OS_Lib.File_Descriptor;
212 -- Return the input file descriptor associated with Descriptor
214 function Get_Output_Fd
215 (Descriptor : Process_Descriptor) return GNAT.OS_Lib.File_Descriptor;
216 -- Return the output file descriptor associated with Descriptor
218 function Get_Error_Fd
219 (Descriptor : Process_Descriptor) return GNAT.OS_Lib.File_Descriptor;
220 -- Return the error output file descriptor associated with Descriptor
223 (Descriptor : Process_Descriptor) return Process_Id;
224 -- Return the process id associated with a given process descriptor
226 function Get_Command_Output
228 Arguments : GNAT.OS_Lib.Argument_List;
230 Status : not null access Integer;
231 Err_To_Out : Boolean := False) return String;
232 -- Execute Command with the specified Arguments and Input, and return the
233 -- generated standard output data as a single string. If Err_To_Out is
234 -- True, generated standard error output is included as well. On return,
235 -- Status is set to the command's exit status.
241 -- This is a rather low-level interface to subprocesses, since basically
242 -- the filtering is left entirely to the user. See the Expect subprograms
243 -- below for higher level functions.
245 type Filter_Function is access
247 (Descriptor : Process_Descriptor'Class;
249 User_Data : System.Address := System.Null_Address);
250 -- Function called every time new characters are read from or written to
253 -- Str is a string of all these characters.
255 -- User_Data, if specified, is user specific data that will be passed to
256 -- the filter. Note that no checks are done on this parameter, so it should
257 -- be used with caution.
260 (Descriptor : in out Process_Descriptor;
261 Filter : Filter_Function;
262 Filter_On : Filter_Type := Output;
263 User_Data : System.Address := System.Null_Address;
264 After : Boolean := False);
265 -- Add a new filter for one of the filter types. This filter will be run
266 -- before all the existing filters, unless After is set True, in which case
267 -- it will be run after existing filters. User_Data is passed as is to the
270 procedure Remove_Filter
271 (Descriptor : in out Process_Descriptor;
272 Filter : Filter_Function);
273 -- Remove a filter from the list of filters (whatever the type of the
276 procedure Trace_Filter
277 (Descriptor : Process_Descriptor'Class;
279 User_Data : System.Address := System.Null_Address);
280 -- Function that can be used as a filter and that simply outputs Str on
281 -- Standard_Output. This is mainly used for debugging purposes.
282 -- User_Data is ignored.
284 procedure Lock_Filters (Descriptor : in out Process_Descriptor);
285 -- Temporarily disables all output and input filters. They will be
286 -- reactivated only when Unlock_Filters has been called as many times as
289 procedure Unlock_Filters (Descriptor : in out Process_Descriptor);
290 -- Unlocks the filters. They are reactivated only if Unlock_Filters
291 -- has been called as many times as Lock_Filters.
298 (Descriptor : in out Process_Descriptor;
300 Add_LF : Boolean := True;
301 Empty_Buffer : Boolean := False);
302 -- Send a string to the file descriptor.
304 -- The string is not formatted in any way, except if Add_LF is True, in
305 -- which case an ASCII.LF is added at the end, so that Str is recognized
306 -- as a command by the external process.
308 -- If Empty_Buffer is True, any input waiting from the process (or in the
309 -- buffer) is first discarded before the command is sent. The output
310 -- filters are of course called as usual.
312 -----------------------------------------------------------
313 -- Working on the output (single process, simple regexp) --
314 -----------------------------------------------------------
316 type Expect_Match is new Integer;
317 Expect_Full_Buffer : constant Expect_Match := -1;
318 -- If the buffer was full and some characters were discarded
320 Expect_Timeout : constant Expect_Match := -2;
321 -- If no output matching the regexps was found before the timeout
323 function "+" (S : String) return GNAT.OS_Lib.String_Access;
324 -- Allocate some memory for the string. This is merely a convenience
325 -- function to help create the array of regexps in the call to Expect.
328 (Descriptor : in out Process_Descriptor;
329 Result : out Expect_Match;
331 Timeout : Integer := 10_000;
332 Full_Buffer : Boolean := False);
333 -- Wait till a string matching Fd can be read from Fd, and return 1 if a
336 -- It consumes all the characters read from Fd until a match found, and
337 -- then sets the return values for the subprograms Expect_Out and
340 -- The empty string "" will never match, and can be used if you only want
341 -- to match after a specific timeout. Beware that if Timeout is -1 at the
342 -- time, the current task will be blocked forever.
344 -- This command times out after Timeout milliseconds (or never if Timeout
345 -- is -1). In that case, Expect_Timeout is returned. The value returned by
346 -- Expect_Out and Expect_Out_Match are meaningless in that case.
348 -- Note that using a timeout of 0ms leads to unpredictable behavior, since
349 -- the result depends on whether the process has already sent some output
350 -- the first time Expect checks, and this depends on the operating system.
352 -- The regular expression must obey the syntax described in GNAT.Regpat.
354 -- If Full_Buffer is True, then Expect will match if the buffer was too
355 -- small and some characters were about to be discarded. In that case,
356 -- Expect_Full_Buffer is returned.
359 (Descriptor : in out Process_Descriptor;
360 Result : out Expect_Match;
361 Regexp : GNAT.Regpat.Pattern_Matcher;
362 Timeout : Integer := 10_000;
363 Full_Buffer : Boolean := False);
364 -- Same as the previous one, but with a precompiled regular expression.
365 -- This is more efficient however, especially if you are using this
366 -- expression multiple times, since this package won't need to recompile
367 -- the regexp every time.
370 (Descriptor : in out Process_Descriptor;
371 Result : out Expect_Match;
373 Matched : out GNAT.Regpat.Match_Array;
374 Timeout : Integer := 10_000;
375 Full_Buffer : Boolean := False);
376 -- Same as above, but it is now possible to get the indexes of the
377 -- substrings for the parentheses in the regexp (see the example at the
378 -- top of this package, as well as the documentation in the package
381 -- Matched'First should be 0, and this index will contain the indexes for
382 -- the whole string that was matched. The index 1 will contain the indexes
383 -- for the first parentheses-pair, and so on.
390 (Descriptor : in out Process_Descriptor;
391 Result : out Expect_Match;
392 Regexp : GNAT.Regpat.Pattern_Matcher;
393 Matched : out GNAT.Regpat.Match_Array;
394 Timeout : Integer := 10_000;
395 Full_Buffer : Boolean := False);
396 -- Same as above, but with a precompiled regular expression
398 -------------------------------------------------------------
399 -- Working on the output (single process, multiple regexp) --
400 -------------------------------------------------------------
402 type Regexp_Array is array (Positive range <>) of GNAT.OS_Lib.String_Access;
404 type Pattern_Matcher_Access is access all GNAT.Regpat.Pattern_Matcher;
405 type Compiled_Regexp_Array is
406 array (Positive range <>) of Pattern_Matcher_Access;
409 (P : GNAT.Regpat.Pattern_Matcher) return Pattern_Matcher_Access;
410 -- Allocate some memory for the pattern matcher. This is only a convenience
411 -- function to help create the array of compiled regular expressions.
414 (Descriptor : in out Process_Descriptor;
415 Result : out Expect_Match;
416 Regexps : Regexp_Array;
417 Timeout : Integer := 10_000;
418 Full_Buffer : Boolean := False);
419 -- Wait till a string matching one of the regular expressions in Regexps
420 -- is found. This function returns the index of the regexp that matched.
421 -- This command is blocking, but will timeout after Timeout milliseconds.
422 -- In that case, Timeout is returned.
425 (Descriptor : in out Process_Descriptor;
426 Result : out Expect_Match;
427 Regexps : Compiled_Regexp_Array;
428 Timeout : Integer := 10_000;
429 Full_Buffer : Boolean := False);
430 -- Same as the previous one, but with precompiled regular expressions.
431 -- This can be much faster if you are using them multiple times.
434 (Descriptor : in out Process_Descriptor;
435 Result : out Expect_Match;
436 Regexps : Regexp_Array;
437 Matched : out GNAT.Regpat.Match_Array;
438 Timeout : Integer := 10_000;
439 Full_Buffer : Boolean := False);
440 -- Same as above, except that you can also access the parenthesis
441 -- groups inside the matching regular expression.
443 -- The first index in Matched must be 0, or Constraint_Error will be
444 -- raised. The index 0 contains the indexes for the whole string that was
445 -- matched, the index 1 contains the indexes for the first parentheses
449 (Descriptor : in out Process_Descriptor;
450 Result : out Expect_Match;
451 Regexps : Compiled_Regexp_Array;
452 Matched : out GNAT.Regpat.Match_Array;
453 Timeout : Integer := 10_000;
454 Full_Buffer : Boolean := False);
455 -- Same as above, but with precompiled regular expressions. The first index
456 -- in Matched must be 0, or Constraint_Error will be raised.
458 -------------------------------------------
459 -- Working on the output (multi-process) --
460 -------------------------------------------
462 type Multiprocess_Regexp is record
463 Descriptor : Process_Descriptor_Access;
464 Regexp : Pattern_Matcher_Access;
467 type Multiprocess_Regexp_Array is
468 array (Positive range <>) of Multiprocess_Regexp;
470 procedure Free (Regexp : in out Multiprocess_Regexp);
471 -- Free the memory occupied by Regexp
473 function Has_Process (Regexp : Multiprocess_Regexp_Array) return Boolean;
474 -- Return True if at least one entry in Regexp is non-null, ie there is
475 -- still at least one process to monitor
477 function First_Dead_Process
478 (Regexp : Multiprocess_Regexp_Array) return Natural;
479 -- Find the first entry in Regexp that corresponds to a dead process that
480 -- wasn't Free-d yet. This function is called in general when Expect
481 -- (below) raises the exception Process_Died. This returns 0 if no process
485 (Result : out Expect_Match;
486 Regexps : Multiprocess_Regexp_Array;
487 Matched : out GNAT.Regpat.Match_Array;
488 Timeout : Integer := 10_000;
489 Full_Buffer : Boolean := False);
490 -- Same as above, but for multi processes. Any of the entries in
491 -- Regexps can have a null Descriptor or Regexp. Such entries will
492 -- simply be ignored. Therefore when a process terminates, you can
493 -- simply reset its entry.
495 -- The expect loop would therefore look like:
497 -- Processes : Multiprocess_Regexp_Array (...) := ...;
500 -- while Has_Process (Processes) loop
502 -- Expect (Result, Processes, Timeout => -1);
503 -- ... process output of process Result (output, full buffer,...)
506 -- when Process_Died =>
508 -- R := First_Dead_Process (Processes);
509 -- Close (Processes (R).Descriptor.all, Status);
510 -- Free (Processes (R));
515 (Result : out Expect_Match;
516 Regexps : Multiprocess_Regexp_Array;
517 Timeout : Integer := 10_000;
518 Full_Buffer : Boolean := False);
519 -- Same as the previous one, but for multiple processes. This procedure
520 -- finds the first regexp that match the associated process.
522 ------------------------
523 -- Getting the output --
524 ------------------------
527 (Descriptor : in out Process_Descriptor;
528 Timeout : Integer := 0);
529 -- Discard all output waiting from the process.
531 -- This output is simply discarded, and no filter is called. This output
532 -- will also not be visible by the next call to Expect, nor will any output
533 -- currently buffered.
535 -- Timeout is the delay for which we wait for output to be available from
536 -- the process. If 0, we only get what is immediately available.
538 function Expect_Out (Descriptor : Process_Descriptor) return String;
539 -- Return the string matched by the last Expect call.
541 -- The returned string is in fact the concatenation of all the strings read
542 -- from the file descriptor up to, and including, the characters that
543 -- matched the regular expression.
545 -- For instance, with an input "philosophic", and a regular expression "hi"
546 -- in the call to expect, the strings returned the first and second time
547 -- would be respectively "phi" and "losophi".
549 function Expect_Out_Match (Descriptor : Process_Descriptor) return String;
550 -- Return the string matched by the last Expect call.
552 -- The returned string includes only the character that matched the
553 -- specific regular expression. All the characters that came before are
556 -- For instance, with an input "philosophic", and a regular expression
557 -- "hi" in the call to expect, the strings returned the first and second
558 -- time would both be "hi".
564 Invalid_Process : exception;
565 -- Raised by most subprograms above when the parameter Descriptor is not a
566 -- valid process or is a closed process.
568 Process_Died : exception;
569 -- Raised by all the expect subprograms if Descriptor was originally a
570 -- valid process that died while Expect was executing. It is also raised
571 -- when Expect receives an end-of-file.
574 type Filter_List_Elem;
575 type Filter_List is access Filter_List_Elem;
576 type Filter_List_Elem is record
577 Filter : Filter_Function;
578 User_Data : System.Address;
579 Filter_On : Filter_Type;
583 type Pipe_Type is record
584 Input, Output : GNAT.OS_Lib.File_Descriptor;
586 -- This type represents a pipe, used to communicate between two processes
588 procedure Set_Up_Communications
589 (Pid : in out Process_Descriptor;
590 Err_To_Out : Boolean;
591 Pipe1 : not null access Pipe_Type;
592 Pipe2 : not null access Pipe_Type;
593 Pipe3 : not null access Pipe_Type);
594 -- Set up all the communication pipes and file descriptors prior to
595 -- spawning the child process.
597 procedure Set_Up_Parent_Communications
598 (Pid : in out Process_Descriptor;
599 Pipe1 : in out Pipe_Type;
600 Pipe2 : in out Pipe_Type;
601 Pipe3 : in out Pipe_Type);
602 -- Finish the set up of the pipes while in the parent process
604 procedure Set_Up_Child_Communications
605 (Pid : in out Process_Descriptor;
606 Pipe1 : in out Pipe_Type;
607 Pipe2 : in out Pipe_Type;
608 Pipe3 : in out Pipe_Type;
610 Args : System.Address);
611 -- Finish the set up of the pipes while in the child process This also
612 -- spawns the child process (based on Cmd). On systems that support fork,
613 -- this procedure is executed inside the newly created process.
615 type Process_Descriptor is tagged record
616 Pid : aliased Process_Id := Invalid_Pid;
617 Input_Fd : GNAT.OS_Lib.File_Descriptor := GNAT.OS_Lib.Invalid_FD;
618 Output_Fd : GNAT.OS_Lib.File_Descriptor := GNAT.OS_Lib.Invalid_FD;
619 Error_Fd : GNAT.OS_Lib.File_Descriptor := GNAT.OS_Lib.Invalid_FD;
620 Filters_Lock : Integer := 0;
622 Filters : Filter_List := null;
624 Buffer : GNAT.OS_Lib.String_Access := null;
625 Buffer_Size : Natural := 0;
626 Buffer_Index : Natural := 0;
628 Last_Match_Start : Natural := 0;
629 Last_Match_End : Natural := 0;
632 -- The following subprogram is provided for use in the body, and also
633 -- possibly in future child units providing extensions to this package.
635 procedure Portable_Execvp
636 (Pid : not null access Process_Id;
638 Args : System.Address);
639 pragma Import (C, Portable_Execvp, "__gnat_expect_portable_execvp");
640 -- Executes, in a portable way, the command Cmd (full path must be
641 -- specified), with the given Args, which must be an array of string
642 -- pointers. Note that the first element in Args must be the executable
643 -- name, and the last element must be a null pointer. The returned value
644 -- in Pid is the process ID, or zero if not supported on the platform.