1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- G N A T . E X P E C T --
9 -- Copyright (C) 2000-2009, 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 with System
; use System
;
35 with System
.OS_Constants
; use System
.OS_Constants
;
36 with Ada
.Calendar
; use Ada
.Calendar
;
39 with GNAT
.OS_Lib
; use GNAT
.OS_Lib
;
40 with GNAT
.Regpat
; use GNAT
.Regpat
;
42 with Ada
.Unchecked_Deallocation
;
44 package body GNAT
.Expect
is
46 type Array_Of_Pd
is array (Positive range <>) of Process_Descriptor_Access
;
48 procedure Expect_Internal
49 (Descriptors
: in out Array_Of_Pd
;
50 Result
: out Expect_Match
;
52 Full_Buffer
: Boolean);
53 -- Internal function used to read from the process Descriptor.
55 -- Three outputs are possible:
56 -- Result=Expect_Timeout, if no output was available before the timeout
58 -- Result=Expect_Full_Buffer, if Full_Buffer is True and some characters
59 -- had to be discarded from the internal buffer of Descriptor.
60 -- Result=<integer>, indicates how many characters were added to the
61 -- internal buffer. These characters are from indexes
62 -- Descriptor.Buffer_Index - Result + 1 .. Descriptor.Buffer_Index
63 -- Process_Died is raised if the process is no longer valid.
65 procedure Reinitialize_Buffer
66 (Descriptor
: in out Process_Descriptor
'Class);
67 -- Reinitialize the internal buffer.
68 -- The buffer is deleted up to the end of the last match.
70 procedure Free
is new Ada
.Unchecked_Deallocation
71 (Pattern_Matcher
, Pattern_Matcher_Access
);
73 procedure Free
is new Ada
.Unchecked_Deallocation
74 (Filter_List_Elem
, Filter_List
);
76 procedure Call_Filters
77 (Pid
: Process_Descriptor
'Class;
79 Filter_On
: Filter_Type
);
80 -- Call all the filters that have the appropriate type.
81 -- This function does nothing if the filters are locked
83 ------------------------------
84 -- Target dependent section --
85 ------------------------------
87 function Dup
(Fd
: File_Descriptor
) return File_Descriptor
;
88 pragma Import
(C
, Dup
);
90 procedure Dup2
(Old_Fd
, New_Fd
: File_Descriptor
);
91 pragma Import
(C
, Dup2
);
93 procedure Kill
(Pid
: Process_Id
; Sig_Num
: Integer; Close
: Integer);
94 pragma Import
(C
, Kill
, "__gnat_kill");
95 -- if Close is set to 1 all OS resources used by the Pid must be freed
97 function Create_Pipe
(Pipe
: not null access Pipe_Type
) return Integer;
98 pragma Import
(C
, Create_Pipe
, "__gnat_pipe");
101 (Fds
: System
.Address
;
104 Is_Set
: System
.Address
) return Integer;
105 pragma Import
(C
, Poll
, "__gnat_expect_poll");
106 -- Check whether there is any data waiting on the file descriptor
107 -- Out_fd, and wait if there is none, at most Timeout milliseconds
108 -- Returns -1 in case of error, 0 if the timeout expired before
109 -- data became available.
111 -- Out_Is_Set is set to 1 if data was available, 0 otherwise.
113 function Waitpid
(Pid
: Process_Id
) return Integer;
114 pragma Import
(C
, Waitpid
, "__gnat_waitpid");
115 -- Wait for a specific process id, and return its exit code
121 function "+" (S
: String) return GNAT
.OS_Lib
.String_Access
is
123 return new String'(S);
131 (P : GNAT.Regpat.Pattern_Matcher) return Pattern_Matcher_Access
134 return new GNAT.Regpat.Pattern_Matcher'(P
);
142 (Descriptor
: in out Process_Descriptor
;
143 Filter
: Filter_Function
;
144 Filter_On
: Filter_Type
:= Output
;
145 User_Data
: System
.Address
:= System
.Null_Address
;
146 After
: Boolean := False)
148 Current
: Filter_List
:= Descriptor
.Filters
;
152 while Current
/= null and then Current
.Next
/= null loop
153 Current
:= Current
.Next
;
156 if Current
= null then
157 Descriptor
.Filters
:=
158 new Filter_List_Elem
'
159 (Filter => Filter, Filter_On => Filter_On,
160 User_Data => User_Data, Next => null);
163 new Filter_List_Elem'
164 (Filter
=> Filter
, Filter_On
=> Filter_On
,
165 User_Data
=> User_Data
, Next
=> null);
169 Descriptor
.Filters
:=
170 new Filter_List_Elem
'
171 (Filter => Filter, Filter_On => Filter_On,
172 User_Data => User_Data, Next => Descriptor.Filters);
180 procedure Call_Filters
181 (Pid : Process_Descriptor'Class;
183 Filter_On : Filter_Type)
185 Current_Filter : Filter_List;
188 if Pid.Filters_Lock = 0 then
189 Current_Filter := Pid.Filters;
191 while Current_Filter /= null loop
192 if Current_Filter.Filter_On = Filter_On then
193 Current_Filter.Filter
194 (Pid, Str, Current_Filter.User_Data);
197 Current_Filter := Current_Filter.Next;
207 (Descriptor : in out Process_Descriptor;
208 Status : out Integer)
210 Current_Filter : Filter_List;
211 Next_Filter : Filter_List;
214 Close (Descriptor.Input_Fd);
216 if Descriptor.Error_Fd /= Descriptor.Output_Fd then
217 Close (Descriptor.Error_Fd);
220 Close (Descriptor.Output_Fd);
222 -- ??? Should have timeouts for different signals
224 if Descriptor.Pid > 0 then -- see comment in Send_Signal
225 Kill (Descriptor.Pid, Sig_Num => 9, Close => 0);
228 GNAT.OS_Lib.Free (Descriptor.Buffer);
229 Descriptor.Buffer_Size := 0;
231 Current_Filter := Descriptor.Filters;
233 while Current_Filter /= null loop
234 Next_Filter := Current_Filter.Next;
235 Free (Current_Filter);
236 Current_Filter := Next_Filter;
239 Descriptor.Filters := null;
241 -- Check process id (see comment in Send_Signal)
243 if Descriptor.Pid > 0 then
244 Status := Waitpid (Descriptor.Pid);
246 raise Invalid_Process;
250 procedure Close (Descriptor : in out Process_Descriptor) is
252 pragma Unreferenced (Status);
254 Close (Descriptor, Status);
262 (Descriptor : in out Process_Descriptor;
263 Result : out Expect_Match;
265 Timeout : Integer := 10_000;
266 Full_Buffer : Boolean := False)
270 Expect (Descriptor, Result, Never_Match, Timeout, Full_Buffer);
272 Expect (Descriptor, Result, Compile (Regexp), Timeout, Full_Buffer);
277 (Descriptor : in out Process_Descriptor;
278 Result : out Expect_Match;
280 Matched : out GNAT.Regpat.Match_Array;
281 Timeout : Integer := 10_000;
282 Full_Buffer : Boolean := False)
285 pragma Assert (Matched'First = 0);
288 (Descriptor, Result, Never_Match, Matched, Timeout, Full_Buffer);
291 (Descriptor, Result, Compile (Regexp), Matched, Timeout,
297 (Descriptor : in out Process_Descriptor;
298 Result : out Expect_Match;
299 Regexp : GNAT.Regpat.Pattern_Matcher;
300 Timeout : Integer := 10_000;
301 Full_Buffer : Boolean := False)
303 Matched : GNAT.Regpat.Match_Array (0 .. 0);
304 pragma Warnings (Off, Matched);
306 Expect (Descriptor, Result, Regexp, Matched, Timeout, Full_Buffer);
310 (Descriptor : in out Process_Descriptor;
311 Result : out Expect_Match;
312 Regexp : GNAT.Regpat.Pattern_Matcher;
313 Matched : out GNAT.Regpat.Match_Array;
314 Timeout : Integer := 10_000;
315 Full_Buffer : Boolean := False)
318 Descriptors : Array_Of_Pd := (1 => Descriptor'Unrestricted_Access);
319 Try_Until : constant Time := Clock + Duration (Timeout) / 1000.0;
320 Timeout_Tmp : Integer := Timeout;
323 pragma Assert (Matched'First = 0);
324 Reinitialize_Buffer (Descriptor);
327 -- First, test if what is already in the buffer matches (This is
328 -- required if this package is used in multi-task mode, since one of
329 -- the tasks might have added something in the buffer, and we don't
330 -- want other tasks to wait for new input to be available before
331 -- checking the regexps).
334 (Regexp, Descriptor.Buffer (1 .. Descriptor.Buffer_Index), Matched);
336 if Descriptor.Buffer_Index >= 1 and then Matched (0).First /= 0 then
338 Descriptor.Last_Match_Start := Matched (0).First;
339 Descriptor.Last_Match_End := Matched (0).Last;
343 -- Else try to read new input
345 Expect_Internal (Descriptors, N, Timeout_Tmp, Full_Buffer);
347 if N = Expect_Timeout or else N = Expect_Full_Buffer then
352 -- Calculate the timeout for the next turn
354 -- Note that Timeout is, from the caller's perspective, the maximum
355 -- time until a match, not the maximum time until some output is
356 -- read, and thus cannot be reused as is for Expect_Internal.
358 if Timeout /= -1 then
359 Timeout_Tmp := Integer (Try_Until - Clock) * 1000;
361 if Timeout_Tmp < 0 then
362 Result := Expect_Timeout;
368 -- Even if we had the general timeout above, we have to test that the
369 -- last test we read from the external process didn't match.
372 (Regexp, Descriptor.Buffer (1 .. Descriptor.Buffer_Index), Matched);
374 if Matched (0).First /= 0 then
376 Descriptor.Last_Match_Start := Matched (0).First;
377 Descriptor.Last_Match_End := Matched (0).Last;
383 (Descriptor : in out Process_Descriptor;
384 Result : out Expect_Match;
385 Regexps : Regexp_Array;
386 Timeout : Integer := 10_000;
387 Full_Buffer : Boolean := False)
389 Patterns : Compiled_Regexp_Array (Regexps'Range);
391 Matched : GNAT.Regpat.Match_Array (0 .. 0);
392 pragma Warnings (Off, Matched);
395 for J in Regexps'Range loop
396 Patterns (J) := new Pattern_Matcher'(Compile
(Regexps
(J
).all));
399 Expect
(Descriptor
, Result
, Patterns
, Matched
, Timeout
, Full_Buffer
);
401 for J
in Regexps
'Range loop
407 (Descriptor
: in out Process_Descriptor
;
408 Result
: out Expect_Match
;
409 Regexps
: Compiled_Regexp_Array
;
410 Timeout
: Integer := 10_000
;
411 Full_Buffer
: Boolean := False)
413 Matched
: GNAT
.Regpat
.Match_Array
(0 .. 0);
414 pragma Warnings
(Off
, Matched
);
416 Expect
(Descriptor
, Result
, Regexps
, Matched
, Timeout
, Full_Buffer
);
420 (Result
: out Expect_Match
;
421 Regexps
: Multiprocess_Regexp_Array
;
422 Timeout
: Integer := 10_000
;
423 Full_Buffer
: Boolean := False)
425 Matched
: GNAT
.Regpat
.Match_Array
(0 .. 0);
426 pragma Warnings
(Off
, Matched
);
428 Expect
(Result
, Regexps
, Matched
, Timeout
, Full_Buffer
);
432 (Descriptor
: in out Process_Descriptor
;
433 Result
: out Expect_Match
;
434 Regexps
: Regexp_Array
;
435 Matched
: out GNAT
.Regpat
.Match_Array
;
436 Timeout
: Integer := 10_000
;
437 Full_Buffer
: Boolean := False)
439 Patterns
: Compiled_Regexp_Array
(Regexps
'Range);
442 pragma Assert
(Matched
'First = 0);
444 for J
in Regexps
'Range loop
445 Patterns
(J
) := new Pattern_Matcher
'(Compile (Regexps (J).all));
448 Expect (Descriptor, Result, Patterns, Matched, Timeout, Full_Buffer);
450 for J in Regexps'Range loop
456 (Descriptor : in out Process_Descriptor;
457 Result : out Expect_Match;
458 Regexps : Compiled_Regexp_Array;
459 Matched : out GNAT.Regpat.Match_Array;
460 Timeout : Integer := 10_000;
461 Full_Buffer : Boolean := False)
464 Descriptors : Array_Of_Pd := (1 => Descriptor'Unrestricted_Access);
467 pragma Assert (Matched'First = 0);
469 Reinitialize_Buffer (Descriptor);
472 -- First, test if what is already in the buffer matches (This is
473 -- required if this package is used in multi-task mode, since one of
474 -- the tasks might have added something in the buffer, and we don't
475 -- want other tasks to wait for new input to be available before
476 -- checking the regexps).
478 if Descriptor.Buffer /= null then
479 for J in Regexps'Range loop
482 Descriptor.Buffer (1 .. Descriptor.Buffer_Index),
485 if Matched (0) /= No_Match then
486 Result := Expect_Match (J);
487 Descriptor.Last_Match_Start := Matched (0).First;
488 Descriptor.Last_Match_End := Matched (0).Last;
494 Expect_Internal (Descriptors, N, Timeout, Full_Buffer);
496 if N = Expect_Timeout or else N = Expect_Full_Buffer then
504 (Result : out Expect_Match;
505 Regexps : Multiprocess_Regexp_Array;
506 Matched : out GNAT.Regpat.Match_Array;
507 Timeout : Integer := 10_000;
508 Full_Buffer : Boolean := False)
511 Descriptors : Array_Of_Pd (Regexps'Range);
514 pragma Assert (Matched'First = 0);
516 for J in Descriptors'Range loop
517 Descriptors (J) := Regexps (J).Descriptor;
518 Reinitialize_Buffer (Regexps (J).Descriptor.all);
522 -- First, test if what is already in the buffer matches (This is
523 -- required if this package is used in multi-task mode, since one of
524 -- the tasks might have added something in the buffer, and we don't
525 -- want other tasks to wait for new input to be available before
526 -- checking the regexps).
528 for J in Regexps'Range loop
529 Match (Regexps (J).Regexp.all,
530 Regexps (J).Descriptor.Buffer
531 (1 .. Regexps (J).Descriptor.Buffer_Index),
534 if Matched (0) /= No_Match then
535 Result := Expect_Match (J);
536 Regexps (J).Descriptor.Last_Match_Start := Matched (0).First;
537 Regexps (J).Descriptor.Last_Match_End := Matched (0).Last;
542 Expect_Internal (Descriptors, N, Timeout, Full_Buffer);
544 if N = Expect_Timeout or else N = Expect_Full_Buffer then
551 ---------------------
552 -- Expect_Internal --
553 ---------------------
555 procedure Expect_Internal
556 (Descriptors : in out Array_Of_Pd;
557 Result : out Expect_Match;
559 Full_Buffer : Boolean)
561 Num_Descriptors : Integer;
562 Buffer_Size : Integer := 0;
566 type File_Descriptor_Array is
567 array (Descriptors'Range) of File_Descriptor;
568 Fds : aliased File_Descriptor_Array;
570 type Integer_Array is array (Descriptors'Range) of Integer;
571 Is_Set : aliased Integer_Array;
574 for J in Descriptors'Range loop
575 Fds (J) := Descriptors (J).Output_Fd;
577 if Descriptors (J).Buffer_Size = 0 then
578 Buffer_Size := Integer'Max (Buffer_Size, 4096);
581 Integer'Max (Buffer_Size, Descriptors (J).Buffer_Size);
586 Buffer : aliased String (1 .. Buffer_Size);
587 -- Buffer used for input. This is allocated only once, not for
588 -- every iteration of the loop
591 -- Loop until we match or we have a timeout
595 Poll (Fds'Address, Fds'Length, Timeout, Is_Set'Address);
597 case Num_Descriptors is
607 Result := Expect_Timeout;
613 for J in Descriptors'Range loop
614 if Is_Set (J) = 1 then
615 Buffer_Size := Descriptors (J).Buffer_Size;
617 if Buffer_Size = 0 then
621 N := Read (Descriptors (J).Output_Fd, Buffer'Address,
624 -- Error or End of file
627 -- ??? Note that ddd tries again up to three times
628 -- in that case. See LiterateA.C:174
632 -- If there is no limit to the buffer size
634 if Descriptors (J).Buffer_Size = 0 then
637 Tmp : String_Access := Descriptors (J).Buffer;
641 Descriptors (J).Buffer :=
642 new String (1 .. Tmp'Length + N);
643 Descriptors (J).Buffer (1 .. Tmp'Length) :=
645 Descriptors (J).Buffer
646 (Tmp'Length + 1 .. Tmp'Length + N) :=
649 Descriptors (J).Buffer_Index :=
650 Descriptors (J).Buffer'Last;
653 Descriptors (J).Buffer :=
655 Descriptors (J).Buffer.all :=
657 Descriptors (J).Buffer_Index := N;
662 -- Add what we read to the buffer
664 if Descriptors (J).Buffer_Index + N >
665 Descriptors (J).Buffer_Size
667 -- If the user wants to know when we have
668 -- read more than the buffer can contain.
671 Result := Expect_Full_Buffer;
675 -- Keep as much as possible from the buffer,
676 -- and forget old characters.
678 Descriptors (J).Buffer
679 (1 .. Descriptors (J).Buffer_Size - N) :=
680 Descriptors (J).Buffer
681 (N - Descriptors (J).Buffer_Size +
682 Descriptors (J).Buffer_Index + 1 ..
683 Descriptors (J).Buffer_Index);
684 Descriptors (J).Buffer_Index :=
685 Descriptors (J).Buffer_Size - N;
688 -- Keep what we read in the buffer
690 Descriptors (J).Buffer
691 (Descriptors (J).Buffer_Index + 1 ..
692 Descriptors (J).Buffer_Index + N) :=
694 Descriptors (J).Buffer_Index :=
695 Descriptors (J).Buffer_Index + N;
698 -- Call each of the output filter with what we
702 (Descriptors (J).all, Buffer (1 .. N), Output);
704 Result := Expect_Match (N);
718 function Expect_Out (Descriptor : Process_Descriptor) return String is
720 return Descriptor.Buffer (1 .. Descriptor.Last_Match_End);
723 ----------------------
724 -- Expect_Out_Match --
725 ----------------------
727 function Expect_Out_Match (Descriptor : Process_Descriptor) return String is
729 return Descriptor.Buffer
730 (Descriptor.Last_Match_Start .. Descriptor.Last_Match_End);
731 end Expect_Out_Match;
738 (Descriptor : in out Process_Descriptor;
739 Timeout : Integer := 0)
741 Buffer_Size : constant Integer := 8192;
742 Num_Descriptors : Integer;
744 Is_Set : aliased Integer;
745 Buffer : aliased String (1 .. Buffer_Size);
748 -- Empty the current buffer
750 Descriptor.Last_Match_End := Descriptor.Buffer_Index;
751 Reinitialize_Buffer (Descriptor);
753 -- Read everything from the process to flush its output
757 Poll (Descriptor.Output_Fd'Address, 1, Timeout, Is_Set'Address);
759 case Num_Descriptors is
766 -- Timeout => End of flush
775 N := Read (Descriptor.Output_Fd, Buffer'Address,
788 ------------------------
789 -- Get_Command_Output --
790 ------------------------
792 function Get_Command_Output
794 Arguments : GNAT.OS_Lib.Argument_List;
796 Status : not null access Integer;
797 Err_To_Out : Boolean := False) return String
801 Process : Process_Descriptor;
803 Output : String_Access := new String (1 .. 1024);
804 -- Buffer used to accumulate standard output from the launched
805 -- command, expanded as necessary during execution.
808 -- Index of the last used character within Output
812 (Process, Command, Arguments, Err_To_Out => Err_To_Out);
814 if Input'Length > 0 then
815 Send (Process, Input);
818 Close (Process.Input_Fd);
819 Process.Input_Fd := Invalid_FD;
822 Result : Expect_Match;
823 pragma Unreferenced (Result);
826 -- This loop runs until the call to Expect raises Process_Died
829 Expect (Process, Result, ".+");
832 NOutput : String_Access;
833 S : constant String := Expect_Out (Process);
834 pragma Assert (S'Length > 0);
837 -- Expand buffer if we need more space. Note here that we add
838 -- S'Length to ensure that S will fit in the new buffer size.
840 if Last + S'Length > Output'Last then
841 NOutput := new String (1 .. 2 * Output'Last + S'Length);
842 NOutput (Output'Range) := Output.all;
845 -- Here if current buffer size is OK
851 NOutput (Last + 1 .. Last + S'Length) := S;
852 Last := Last + S'Length;
859 Close (Process, Status.all);
867 S : constant String := Output (1 .. Last);
872 end Get_Command_Output;
878 function Get_Error_Fd
879 (Descriptor : Process_Descriptor) return GNAT.OS_Lib.File_Descriptor
882 return Descriptor.Error_Fd;
889 function Get_Input_Fd
890 (Descriptor : Process_Descriptor) return GNAT.OS_Lib.File_Descriptor
893 return Descriptor.Input_Fd;
900 function Get_Output_Fd
901 (Descriptor : Process_Descriptor) return GNAT.OS_Lib.File_Descriptor
904 return Descriptor.Output_Fd;
912 (Descriptor : Process_Descriptor) return Process_Id
915 return Descriptor.Pid;
922 procedure Interrupt (Descriptor : in out Process_Descriptor) is
923 SIGINT : constant := 2;
925 Send_Signal (Descriptor, SIGINT);
932 procedure Lock_Filters (Descriptor : in out Process_Descriptor) is
934 Descriptor.Filters_Lock := Descriptor.Filters_Lock + 1;
937 ------------------------
938 -- Non_Blocking_Spawn --
939 ------------------------
941 procedure Non_Blocking_Spawn
942 (Descriptor : out Process_Descriptor'Class;
944 Args : GNAT.OS_Lib.Argument_List;
945 Buffer_Size : Natural := 4096;
946 Err_To_Out : Boolean := False)
948 function Fork return Process_Id;
949 pragma Import (C, Fork, "__gnat_expect_fork");
950 -- Starts a new process if possible. See the Unix command fork for more
951 -- information. On systems that do not support this capability (such as
952 -- Windows...), this command does nothing, and Fork will return
955 Pipe1, Pipe2, Pipe3 : aliased Pipe_Type;
958 Arg_List : String_List (1 .. Args'Length + 2);
959 C_Arg_List : aliased array (1 .. Args'Length + 2) of System.Address;
961 Command_With_Path : String_Access;
964 -- Create the rest of the pipes
966 Set_Up_Communications
967 (Descriptor, Err_To_Out, Pipe1'Access, Pipe2'Access, Pipe3'Access);
969 Command_With_Path := Locate_Exec_On_Path (Command);
971 if Command_With_Path = null then
972 raise Invalid_Process;
975 -- Fork a new process
977 Descriptor.Pid := Fork;
979 -- Are we now in the child (or, for Windows, still in the common
982 if Descriptor.Pid = Null_Pid then
983 -- Prepare an array of arguments to pass to C
985 Arg := new String (1 .. Command_With_Path'Length + 1);
986 Arg (1 .. Command_With_Path'Length) := Command_With_Path.all;
987 Arg (Arg'Last) := ASCII.NUL;
990 for J in Args'Range loop
991 Arg := new String (1 .. Args (J)'Length + 1);
992 Arg (1 .. Args (J)'Length) := Args (J).all;
993 Arg (Arg'Last) := ASCII.NUL;
994 Arg_List (J + 2 - Args'First) := Arg.all'Access;
997 Arg_List (Arg_List'Last) := null;
999 -- Make sure all arguments are compatible with OS conventions
1001 Normalize_Arguments (Arg_List);
1003 -- Prepare low-level argument list from the normalized arguments
1005 for K in Arg_List'Range loop
1007 (if Arg_List (K) /= null
1008 then Arg_List (K).all'Address
1009 else System.Null_Address);
1012 -- This does not return on Unix systems
1014 Set_Up_Child_Communications
1015 (Descriptor, Pipe1, Pipe2, Pipe3, Command_With_Path.all,
1016 C_Arg_List'Address);
1019 Free (Command_With_Path);
1021 -- Did we have an error when spawning the child ?
1023 if Descriptor.Pid < Null_Pid then
1024 raise Invalid_Process;
1026 -- We are now in the parent process
1028 Set_Up_Parent_Communications (Descriptor, Pipe1, Pipe2, Pipe3);
1031 -- Create the buffer
1033 Descriptor.Buffer_Size := Buffer_Size;
1035 if Buffer_Size /= 0 then
1036 Descriptor.Buffer := new String (1 .. Positive (Buffer_Size));
1039 -- Initialize the filters
1041 Descriptor.Filters := null;
1042 end Non_Blocking_Spawn;
1044 -------------------------
1045 -- Reinitialize_Buffer --
1046 -------------------------
1048 procedure Reinitialize_Buffer
1049 (Descriptor : in out Process_Descriptor'Class)
1052 if Descriptor.Buffer_Size = 0 then
1054 Tmp : String_Access := Descriptor.Buffer;
1057 Descriptor.Buffer :=
1059 (1 .. Descriptor.Buffer_Index - Descriptor.Last_Match_End);
1062 Descriptor.Buffer.all := Tmp
1063 (Descriptor.Last_Match_End + 1 .. Descriptor.Buffer_Index);
1068 Descriptor.Buffer_Index := Descriptor.Buffer'Last;
1072 (1 .. Descriptor.Buffer_Index - Descriptor.Last_Match_End) :=
1074 (Descriptor.Last_Match_End + 1 .. Descriptor.Buffer_Index);
1076 if Descriptor.Buffer_Index > Descriptor.Last_Match_End then
1077 Descriptor.Buffer_Index :=
1078 Descriptor.Buffer_Index - Descriptor.Last_Match_End;
1080 Descriptor.Buffer_Index := 0;
1084 Descriptor.Last_Match_Start := 0;
1085 Descriptor.Last_Match_End := 0;
1086 end Reinitialize_Buffer;
1092 procedure Remove_Filter
1093 (Descriptor : in out Process_Descriptor;
1094 Filter : Filter_Function)
1096 Previous : Filter_List := null;
1097 Current : Filter_List := Descriptor.Filters;
1100 while Current /= null loop
1101 if Current.Filter = Filter then
1102 if Previous = null then
1103 Descriptor.Filters := Current.Next;
1105 Previous.Next := Current.Next;
1109 Previous := Current;
1110 Current := Current.Next;
1119 (Descriptor : in out Process_Descriptor;
1121 Add_LF : Boolean := True;
1122 Empty_Buffer : Boolean := False)
1124 Line_Feed : aliased constant String := (1 .. 1 => ASCII.LF);
1125 Descriptors : Array_Of_Pd := (1 => Descriptor'Unrestricted_Access);
1127 Result : Expect_Match;
1129 pragma Warnings (Off, Result);
1130 pragma Warnings (Off, Discard);
1133 if Empty_Buffer then
1135 -- Force a read on the process if there is anything waiting
1138 (Descriptors, Result, Timeout => 0, Full_Buffer => False);
1139 Descriptor.Last_Match_End := Descriptor.Buffer_Index;
1143 Reinitialize_Buffer (Descriptor);
1146 Call_Filters (Descriptor, Str, Input);
1148 Write (Descriptor.Input_Fd, Str'Address, Str'Last - Str'First + 1);
1151 Call_Filters (Descriptor, Line_Feed, Input);
1153 Write (Descriptor.Input_Fd, Line_Feed'Address, 1);
1161 procedure Send_Signal
1162 (Descriptor : Process_Descriptor;
1166 -- A nonpositive process id passed to kill has special meanings. For
1167 -- example, -1 means kill all processes in sight, including self, in
1168 -- POSIX and Windows (and something slightly different in Linux). See
1169 -- man pages for details. In any case, we don't want to do that. Note
1170 -- that Descriptor.Pid will be -1 if the process was not successfully
1171 -- started; we don't want to kill ourself in that case.
1173 if Descriptor.Pid > 0 then
1174 Kill (Descriptor.Pid, Signal, Close => 1);
1175 -- ??? Need to check process status here
1177 raise Invalid_Process;
1181 ---------------------------------
1182 -- Set_Up_Child_Communications --
1183 ---------------------------------
1185 procedure Set_Up_Child_Communications
1186 (Pid : in out Process_Descriptor;
1187 Pipe1 : in out Pipe_Type;
1188 Pipe2 : in out Pipe_Type;
1189 Pipe3 : in out Pipe_Type;
1191 Args : System.Address)
1193 pragma Warnings (Off, Pid);
1194 pragma Warnings (Off, Pipe1);
1195 pragma Warnings (Off, Pipe2);
1196 pragma Warnings (Off, Pipe3);
1198 Input : File_Descriptor;
1199 Output : File_Descriptor;
1200 Error : File_Descriptor;
1202 No_Fork_On_Target : constant Boolean := Target_OS = Windows;
1205 if No_Fork_On_Target then
1207 -- Since Windows does not have a separate fork/exec, we need to
1208 -- perform the following actions:
1210 -- - save stdin, stdout, stderr
1211 -- - replace them by our pipes
1212 -- - create the child with process handle inheritance
1213 -- - revert to the previous stdin, stdout and stderr.
1215 Input := Dup (GNAT.OS_Lib.Standin);
1216 Output := Dup (GNAT.OS_Lib.Standout);
1217 Error := Dup (GNAT.OS_Lib.Standerr);
1220 -- Since we are still called from the parent process, there is no way
1221 -- currently we can cleanly close the unneeded ends of the pipes, but
1222 -- this doesn't really matter.
1224 -- We could close Pipe1.Output, Pipe2.Input, Pipe3.Input
1226 Dup2 (Pipe1.Input, GNAT.OS_Lib.Standin);
1227 Dup2 (Pipe2.Output, GNAT.OS_Lib.Standout);
1228 Dup2 (Pipe3.Output, GNAT.OS_Lib.Standerr);
1230 Portable_Execvp (Pid.Pid'Access, Cmd & ASCII.NUL, Args);
1232 -- The following commands are not executed on Unix systems, and are only
1233 -- required for Windows systems. We are now in the parent process.
1235 -- Restore the old descriptors
1237 Dup2 (Input, GNAT.OS_Lib.Standin);
1238 Dup2 (Output, GNAT.OS_Lib.Standout);
1239 Dup2 (Error, GNAT.OS_Lib.Standerr);
1243 end Set_Up_Child_Communications;
1245 ---------------------------
1246 -- Set_Up_Communications --
1247 ---------------------------
1249 procedure Set_Up_Communications
1250 (Pid : in out Process_Descriptor;
1251 Err_To_Out : Boolean;
1252 Pipe1 : not null access Pipe_Type;
1253 Pipe2 : not null access Pipe_Type;
1254 Pipe3 : not null access Pipe_Type)
1257 pragma Unreferenced (Status);
1262 if Create_Pipe (Pipe1) /= 0 then
1266 if Create_Pipe (Pipe2) /= 0 then
1270 -- Record the 'parent
' end of the two pipes in Pid:
1271 -- Child stdin is connected to the 'write
' end of Pipe1;
1272 -- Child stdout is connected to the 'read
' end of Pipe2.
1273 -- We do not want these descriptors to remain open in the child
1274 -- process, so we mark them close-on-exec/non-inheritable.
1276 Pid.Input_Fd := Pipe1.Output;
1277 Set_Close_On_Exec (Pipe1.Output, True, Status);
1278 Pid.Output_Fd := Pipe2.Input;
1279 Set_Close_On_Exec (Pipe2.Input, True, Status);
1283 -- Reuse the standard output pipe for standard error
1285 Pipe3.all := Pipe2.all;
1288 -- Create a separate pipe for standard error
1290 if Create_Pipe (Pipe3) /= 0 then
1295 -- As above, record the proper fd for the child's standard error stream
1297 Pid.Error_Fd := Pipe3.Input;
1298 Set_Close_On_Exec (Pipe3.Input, True, Status);
1299 end Set_Up_Communications;
1301 ----------------------------------
1302 -- Set_Up_Parent_Communications --
1303 ----------------------------------
1305 procedure Set_Up_Parent_Communications
1306 (Pid : in out Process_Descriptor;
1307 Pipe1 : in out Pipe_Type;
1308 Pipe2 : in out Pipe_Type;
1309 Pipe3 : in out Pipe_Type)
1311 pragma Warnings (Off, Pid);
1312 pragma Warnings (Off, Pipe1);
1313 pragma Warnings (Off, Pipe2);
1314 pragma Warnings (Off, Pipe3);
1317 Close (Pipe1.Input);
1318 Close (Pipe2.Output);
1320 if Pipe3.Output /= Pipe2.Output then
1321 Close (Pipe3.Output);
1323 end Set_Up_Parent_Communications;
1329 procedure Trace_Filter
1330 (Descriptor : Process_Descriptor'Class;
1332 User_Data : System.Address := System.Null_Address)
1334 pragma Warnings (Off, Descriptor);
1335 pragma Warnings (Off, User_Data);
1340 --------------------
1341 -- Unlock_Filters --
1342 --------------------
1344 procedure Unlock_Filters (Descriptor : in out Process_Descriptor) is
1346 if Descriptor.Filters_Lock > 0 then
1347 Descriptor.Filters_Lock := Descriptor.Filters_Lock - 1;