1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- G N A T . E X P E C T --
9 -- Copyright (C) 2002-2003 Ada Core Technologies, Inc. --
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. --
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 -- This is the VMS version.
36 with System
; use System
;
37 with Ada
.Calendar
; use Ada
.Calendar
;
40 with GNAT
.OS_Lib
; use GNAT
.OS_Lib
;
41 with GNAT
.Regpat
; use GNAT
.Regpat
;
43 with Unchecked_Deallocation
;
45 package body GNAT
.Expect
is
47 type Array_Of_Pd
is array (Positive range <>) of Process_Descriptor_Access
;
49 Save_Input
: File_Descriptor
;
50 Save_Output
: File_Descriptor
;
51 Save_Error
: File_Descriptor
;
53 procedure Expect_Internal
54 (Descriptors
: in out Array_Of_Pd
;
55 Result
: out Expect_Match
;
57 Full_Buffer
: Boolean);
58 -- Internal function used to read from the process Descriptor.
60 -- Three outputs are possible:
61 -- Result=Expect_Timeout, if no output was available before the timeout
63 -- Result=Expect_Full_Buffer, if Full_Buffer is True and some characters
64 -- had to be discarded from the internal buffer of Descriptor.
65 -- Result=<integer>, indicates how many characters were added to the
66 -- internal buffer. These characters are from indexes
67 -- Descriptor.Buffer_Index - Result + 1 .. Descriptor.Buffer_Index
68 -- Process_Died is raised if the process is no longer valid.
70 procedure Reinitialize_Buffer
71 (Descriptor
: in out Process_Descriptor
'Class);
72 -- Reinitialize the internal buffer.
73 -- The buffer is deleted up to the end of the last match.
75 procedure Free
is new Unchecked_Deallocation
76 (Pattern_Matcher
, Pattern_Matcher_Access
);
78 procedure Call_Filters
79 (Pid
: Process_Descriptor
'Class;
81 Filter_On
: Filter_Type
);
82 -- Call all the filters that have the appropriate type.
83 -- This function does nothing if the filters are locked
85 ------------------------------
86 -- Target dependent section --
87 ------------------------------
89 function Dup
(Fd
: File_Descriptor
) return File_Descriptor
;
90 pragma Import
(C
, Dup
);
92 procedure Dup2
(Old_Fd
, New_Fd
: File_Descriptor
);
93 pragma Import
(C
, Dup2
);
95 procedure Kill
(Pid
: Process_Id
; Sig_Num
: Integer);
96 pragma Import
(C
, Kill
);
98 function Create_Pipe
(Pipe
: access Pipe_Type
) return Integer;
99 pragma Import
(C
, Create_Pipe
, "__gnat_pipe");
102 (Fds
: System
.Address
;
105 Is_Set
: System
.Address
)
107 pragma Import
(C
, Poll
, "__gnat_expect_poll");
108 -- Check whether there is any data waiting on the file descriptor
109 -- Out_fd, and wait if there is none, at most Timeout milliseconds
110 -- Returns -1 in case of error, 0 if the timeout expired before
111 -- data became available.
113 -- Out_Is_Set is set to 1 if data was available, 0 otherwise.
115 function Waitpid
(Pid
: Process_Id
) return Integer;
116 pragma Import
(C
, Waitpid
, "__gnat_waitpid");
117 -- Wait for a specific process id, and return its exit code.
123 function "+" (S
: String) return GNAT
.OS_Lib
.String_Access
is
125 return new String'(S);
133 (P : GNAT.Regpat.Pattern_Matcher)
134 return Pattern_Matcher_Access
137 return new GNAT.Regpat.Pattern_Matcher'(P
);
145 (Descriptor
: in out Process_Descriptor
;
146 Filter
: Filter_Function
;
147 Filter_On
: Filter_Type
:= Output
;
148 User_Data
: System
.Address
:= System
.Null_Address
;
149 After
: Boolean := False)
151 Current
: Filter_List
:= Descriptor
.Filters
;
155 while Current
/= null and then Current
.Next
/= null loop
156 Current
:= Current
.Next
;
159 if Current
= null then
160 Descriptor
.Filters
:=
161 new Filter_List_Elem
'
162 (Filter => Filter, Filter_On => Filter_On,
163 User_Data => User_Data, Next => null);
166 new Filter_List_Elem'
167 (Filter
=> Filter
, Filter_On
=> Filter_On
,
168 User_Data
=> User_Data
, Next
=> null);
172 Descriptor
.Filters
:=
173 new Filter_List_Elem
'
174 (Filter => Filter, Filter_On => Filter_On,
175 User_Data => User_Data, Next => Descriptor.Filters);
183 procedure Call_Filters
184 (Pid : Process_Descriptor'Class;
186 Filter_On : Filter_Type)
188 Current_Filter : Filter_List;
191 if Pid.Filters_Lock = 0 then
192 Current_Filter := Pid.Filters;
194 while Current_Filter /= null loop
195 if Current_Filter.Filter_On = Filter_On then
196 Current_Filter.Filter
197 (Pid, Str, Current_Filter.User_Data);
200 Current_Filter := Current_Filter.Next;
210 (Descriptor : in out Process_Descriptor;
211 Status : out Integer)
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
223 Kill (Descriptor.Pid, 9);
225 GNAT.OS_Lib.Free (Descriptor.Buffer);
226 Descriptor.Buffer_Size := 0;
228 Status := Waitpid (Descriptor.Pid);
231 procedure Close (Descriptor : in out Process_Descriptor) is
234 Close (Descriptor, Status);
242 (Descriptor : in out Process_Descriptor;
243 Result : out Expect_Match;
245 Timeout : Integer := 10000;
246 Full_Buffer : Boolean := False)
250 Expect (Descriptor, Result, Never_Match, Timeout, Full_Buffer);
252 Expect (Descriptor, Result, Compile (Regexp), Timeout, Full_Buffer);
257 (Descriptor : in out Process_Descriptor;
258 Result : out Expect_Match;
260 Matched : out GNAT.Regpat.Match_Array;
261 Timeout : Integer := 10000;
262 Full_Buffer : Boolean := False)
265 pragma Assert (Matched'First = 0);
268 (Descriptor, Result, Never_Match, Matched, Timeout, Full_Buffer);
271 (Descriptor, Result, Compile (Regexp), Matched, Timeout,
277 (Descriptor : in out Process_Descriptor;
278 Result : out Expect_Match;
279 Regexp : GNAT.Regpat.Pattern_Matcher;
280 Timeout : Integer := 10000;
281 Full_Buffer : Boolean := False)
283 Matched : GNAT.Regpat.Match_Array (0 .. 0);
286 Expect (Descriptor, Result, Regexp, Matched, Timeout, Full_Buffer);
290 (Descriptor : in out Process_Descriptor;
291 Result : out Expect_Match;
292 Regexp : GNAT.Regpat.Pattern_Matcher;
293 Matched : out GNAT.Regpat.Match_Array;
294 Timeout : Integer := 10000;
295 Full_Buffer : Boolean := False)
298 Descriptors : Array_Of_Pd := (1 => Descriptor'Unrestricted_Access);
299 Try_Until : constant Time := Clock + Duration (Timeout) / 1000.0;
300 Timeout_Tmp : Integer := Timeout;
303 pragma Assert (Matched'First = 0);
304 Reinitialize_Buffer (Descriptor);
307 -- First, test if what is already in the buffer matches (This is
308 -- required if this package is used in multi-task mode, since one of
309 -- the tasks might have added something in the buffer, and we don't
310 -- want other tasks to wait for new input to be available before
311 -- checking the regexps).
314 (Regexp, Descriptor.Buffer (1 .. Descriptor.Buffer_Index), Matched);
316 if Descriptor.Buffer_Index >= 1 and then Matched (0).First /= 0 then
318 Descriptor.Last_Match_Start := Matched (0).First;
319 Descriptor.Last_Match_End := Matched (0).Last;
323 -- Else try to read new input
325 Expect_Internal (Descriptors, N, Timeout_Tmp, Full_Buffer);
327 if N = Expect_Timeout or else N = Expect_Full_Buffer then
332 -- Calculate the timeout for the next turn.
333 -- Note that Timeout is, from the caller's perspective, the maximum
334 -- time until a match, not the maximum time until some output is
335 -- read, and thus can not be reused as is for Expect_Internal.
337 if Timeout /= -1 then
338 Timeout_Tmp := Integer (Try_Until - Clock) * 1000;
340 if Timeout_Tmp < 0 then
341 Result := Expect_Timeout;
347 -- Even if we had the general timeout above, we have to test that the
348 -- last test we read from the external process didn't match.
351 (Regexp, Descriptor.Buffer (1 .. Descriptor.Buffer_Index), Matched);
353 if Matched (0).First /= 0 then
355 Descriptor.Last_Match_Start := Matched (0).First;
356 Descriptor.Last_Match_End := Matched (0).Last;
362 (Descriptor : in out Process_Descriptor;
363 Result : out Expect_Match;
364 Regexps : Regexp_Array;
365 Timeout : Integer := 10000;
366 Full_Buffer : Boolean := False)
368 Patterns : Compiled_Regexp_Array (Regexps'Range);
369 Matched : GNAT.Regpat.Match_Array (0 .. 0);
372 for J in Regexps'Range loop
373 Patterns (J) := new Pattern_Matcher'(Compile
(Regexps
(J
).all));
376 Expect
(Descriptor
, Result
, Patterns
, Matched
, Timeout
, Full_Buffer
);
378 for J
in Regexps
'Range loop
384 (Descriptor
: in out Process_Descriptor
;
385 Result
: out Expect_Match
;
386 Regexps
: Compiled_Regexp_Array
;
387 Timeout
: Integer := 10000;
388 Full_Buffer
: Boolean := False)
390 Matched
: GNAT
.Regpat
.Match_Array
(0 .. 0);
393 Expect
(Descriptor
, Result
, Regexps
, Matched
, Timeout
, Full_Buffer
);
397 (Result
: out Expect_Match
;
398 Regexps
: Multiprocess_Regexp_Array
;
399 Timeout
: Integer := 10000;
400 Full_Buffer
: Boolean := False)
402 Matched
: GNAT
.Regpat
.Match_Array
(0 .. 0);
405 Expect
(Result
, Regexps
, Matched
, Timeout
, Full_Buffer
);
409 (Descriptor
: in out Process_Descriptor
;
410 Result
: out Expect_Match
;
411 Regexps
: Regexp_Array
;
412 Matched
: out GNAT
.Regpat
.Match_Array
;
413 Timeout
: Integer := 10000;
414 Full_Buffer
: Boolean := False)
416 Patterns
: Compiled_Regexp_Array
(Regexps
'Range);
419 pragma Assert
(Matched
'First = 0);
421 for J
in Regexps
'Range loop
422 Patterns
(J
) := new Pattern_Matcher
'(Compile (Regexps (J).all));
425 Expect (Descriptor, Result, Patterns, Matched, Timeout, Full_Buffer);
427 for J in Regexps'Range loop
433 (Descriptor : in out Process_Descriptor;
434 Result : out Expect_Match;
435 Regexps : Compiled_Regexp_Array;
436 Matched : out GNAT.Regpat.Match_Array;
437 Timeout : Integer := 10000;
438 Full_Buffer : Boolean := False)
441 Descriptors : Array_Of_Pd := (1 => Descriptor'Unrestricted_Access);
444 pragma Assert (Matched'First = 0);
446 Reinitialize_Buffer (Descriptor);
449 -- First, test if what is already in the buffer matches (This is
450 -- required if this package is used in multi-task mode, since one of
451 -- the tasks might have added something in the buffer, and we don't
452 -- want other tasks to wait for new input to be available before
453 -- checking the regexps).
455 if Descriptor.Buffer /= null then
456 for J in Regexps'Range loop
459 Descriptor.Buffer (1 .. Descriptor.Buffer_Index),
462 if Matched (0) /= No_Match then
463 Result := Expect_Match (J);
464 Descriptor.Last_Match_Start := Matched (0).First;
465 Descriptor.Last_Match_End := Matched (0).Last;
471 Expect_Internal (Descriptors, N, Timeout, Full_Buffer);
473 if N = Expect_Timeout or else N = Expect_Full_Buffer then
481 (Result : out Expect_Match;
482 Regexps : Multiprocess_Regexp_Array;
483 Matched : out GNAT.Regpat.Match_Array;
484 Timeout : Integer := 10000;
485 Full_Buffer : Boolean := False)
488 Descriptors : Array_Of_Pd (Regexps'Range);
491 pragma Assert (Matched'First = 0);
493 for J in Descriptors'Range loop
494 Descriptors (J) := Regexps (J).Descriptor;
495 Reinitialize_Buffer (Regexps (J).Descriptor.all);
499 -- First, test if what is already in the buffer matches (This is
500 -- required if this package is used in multi-task mode, since one of
501 -- the tasks might have added something in the buffer, and we don't
502 -- want other tasks to wait for new input to be available before
503 -- checking the regexps).
505 for J in Regexps'Range loop
506 Match (Regexps (J).Regexp.all,
507 Regexps (J).Descriptor.Buffer
508 (1 .. Regexps (J).Descriptor.Buffer_Index),
511 if Matched (0) /= No_Match then
512 Result := Expect_Match (J);
513 Regexps (J).Descriptor.Last_Match_Start := Matched (0).First;
514 Regexps (J).Descriptor.Last_Match_End := Matched (0).Last;
519 Expect_Internal (Descriptors, N, Timeout, Full_Buffer);
521 if N = Expect_Timeout or else N = Expect_Full_Buffer then
528 ---------------------
529 -- Expect_Internal --
530 ---------------------
532 procedure Expect_Internal
533 (Descriptors : in out Array_Of_Pd;
534 Result : out Expect_Match;
536 Full_Buffer : Boolean)
538 Num_Descriptors : Integer;
539 Buffer_Size : Integer := 0;
543 type File_Descriptor_Array is
544 array (Descriptors'Range) of File_Descriptor;
545 Fds : aliased File_Descriptor_Array;
547 type Integer_Array is array (Descriptors'Range) of Integer;
548 Is_Set : aliased Integer_Array;
551 for J in Descriptors'Range loop
552 Fds (J) := Descriptors (J).Output_Fd;
554 if Descriptors (J).Buffer_Size = 0 then
555 Buffer_Size := Integer'Max (Buffer_Size, 4096);
558 Integer'Max (Buffer_Size, Descriptors (J).Buffer_Size);
563 Buffer : aliased String (1 .. Buffer_Size);
564 -- Buffer used for input. This is allocated only once, not for
565 -- every iteration of the loop
568 -- Loop until we match or we have a timeout
572 Poll (Fds'Address, Fds'Length, Timeout, Is_Set'Address);
574 case Num_Descriptors is
584 Result := Expect_Timeout;
590 for J in Descriptors'Range loop
591 if Is_Set (J) = 1 then
592 Buffer_Size := Descriptors (J).Buffer_Size;
594 if Buffer_Size = 0 then
598 N := Read (Descriptors (J).Output_Fd, Buffer'Address,
601 -- Error or End of file
604 -- ??? Note that ddd tries again up to three times
605 -- in that case. See LiterateA.C:174
609 -- If there is no limit to the buffer size
611 if Descriptors (J).Buffer_Size = 0 then
614 Tmp : String_Access := Descriptors (J).Buffer;
618 Descriptors (J).Buffer :=
619 new String (1 .. Tmp'Length + N);
620 Descriptors (J).Buffer (1 .. Tmp'Length) :=
622 Descriptors (J).Buffer
623 (Tmp'Length + 1 .. Tmp'Length + N) :=
626 Descriptors (J).Buffer_Index :=
627 Descriptors (J).Buffer'Last;
630 Descriptors (J).Buffer :=
632 Descriptors (J).Buffer.all :=
634 Descriptors (J).Buffer_Index := N;
639 -- Add what we read to the buffer
641 if Descriptors (J).Buffer_Index + N - 1 >
642 Descriptors (J).Buffer_Size
644 -- If the user wants to know when we have
645 -- read more than the buffer can contain.
648 Result := Expect_Full_Buffer;
652 -- Keep as much as possible from the buffer,
653 -- and forget old characters.
655 Descriptors (J).Buffer
656 (1 .. Descriptors (J).Buffer_Size - N) :=
657 Descriptors (J).Buffer
658 (N - Descriptors (J).Buffer_Size +
659 Descriptors (J).Buffer_Index + 1 ..
660 Descriptors (J).Buffer_Index);
661 Descriptors (J).Buffer_Index :=
662 Descriptors (J).Buffer_Size - N;
665 -- Keep what we read in the buffer.
667 Descriptors (J).Buffer
668 (Descriptors (J).Buffer_Index + 1 ..
669 Descriptors (J).Buffer_Index + N) :=
671 Descriptors (J).Buffer_Index :=
672 Descriptors (J).Buffer_Index + N;
675 -- Call each of the output filter with what we
679 (Descriptors (J).all, Buffer (1 .. N), Output);
681 Result := Expect_Match (N);
695 function Expect_Out (Descriptor : Process_Descriptor) return String is
697 return Descriptor.Buffer (1 .. Descriptor.Last_Match_End);
700 ----------------------
701 -- Expect_Out_Match --
702 ----------------------
704 function Expect_Out_Match (Descriptor : Process_Descriptor) return String is
706 return Descriptor.Buffer
707 (Descriptor.Last_Match_Start .. Descriptor.Last_Match_End);
708 end Expect_Out_Match;
715 (Descriptor : in out Process_Descriptor;
716 Timeout : Integer := 0)
718 Buffer_Size : constant Integer := 8192;
719 Num_Descriptors : Integer;
721 Is_Set : aliased Integer;
722 Buffer : aliased String (1 .. Buffer_Size);
725 -- Empty the current buffer
727 Descriptor.Last_Match_End := Descriptor.Buffer_Index;
728 Reinitialize_Buffer (Descriptor);
730 -- Read everything from the process to flush its output
734 Poll (Descriptor.Output_Fd'Address, 1, Timeout, Is_Set'Address);
736 case Num_Descriptors is
743 -- Timeout => End of flush
752 N := Read (Descriptor.Output_Fd, Buffer'Address,
770 function Get_Error_Fd
771 (Descriptor : Process_Descriptor)
772 return GNAT.OS_Lib.File_Descriptor
775 return Descriptor.Error_Fd;
782 function Get_Input_Fd
783 (Descriptor : Process_Descriptor)
784 return GNAT.OS_Lib.File_Descriptor
787 return Descriptor.Input_Fd;
794 function Get_Output_Fd
795 (Descriptor : Process_Descriptor)
796 return GNAT.OS_Lib.File_Descriptor
799 return Descriptor.Output_Fd;
807 (Descriptor : Process_Descriptor)
811 return Descriptor.Pid;
818 procedure Interrupt (Descriptor : in out Process_Descriptor) is
819 SIGINT : constant := 2;
822 Send_Signal (Descriptor, SIGINT);
829 procedure Lock_Filters (Descriptor : in out Process_Descriptor) is
831 Descriptor.Filters_Lock := Descriptor.Filters_Lock + 1;
834 ------------------------
835 -- Non_Blocking_Spawn --
836 ------------------------
838 procedure Non_Blocking_Spawn
839 (Descriptor : out Process_Descriptor'Class;
841 Args : GNAT.OS_Lib.Argument_List;
842 Buffer_Size : Natural := 4096;
843 Err_To_Out : Boolean := False)
845 function Alloc_Vfork_Blocks return Integer;
846 pragma Import (C, Alloc_Vfork_Blocks, "decc$$alloc_vfork_blocks");
848 function Get_Vfork_Jmpbuf return System.Address;
849 pragma Import (C, Get_Vfork_Jmpbuf, "decc$$get_vfork_jmpbuf");
851 function Get_Current_Invo_Context (Addr : System.Address)
853 pragma Import (C, Get_Current_Invo_Context,
854 "LIB$GET_CURRENT_INVO_CONTEXT");
856 Pipe1, Pipe2, Pipe3 : aliased Pipe_Type;
859 Arg_List : aliased array (1 .. Args'Length + 2) of System.Address;
861 Command_With_Path : String_Access;
864 -- Create the rest of the pipes
866 Set_Up_Communications
867 (Descriptor, Err_To_Out, Pipe1'Access, Pipe2'Access, Pipe3'Access);
869 Command_With_Path := Locate_Exec_On_Path (Command);
871 if Command_With_Path = null then
872 raise Invalid_Process;
875 -- Fork a new process. It's not possible to do this in a subprogram.
877 if Alloc_Vfork_Blocks >= 0 then
878 Descriptor.Pid := Get_Current_Invo_Context (Get_Vfork_Jmpbuf);
880 Descriptor.Pid := -1;
883 -- Are we now in the child (or, for Windows, still in the common
886 if Descriptor.Pid = Null_Pid then
887 -- Prepare an array of arguments to pass to C
889 Arg := new String (1 .. Command_With_Path'Length + 1);
890 Arg (1 .. Command_With_Path'Length) := Command_With_Path.all;
891 Arg (Arg'Last) := ASCII.Nul;
892 Arg_List (1) := Arg.all'Address;
894 for J in Args'Range loop
895 Arg := new String (1 .. Args (J)'Length + 1);
896 Arg (1 .. Args (J)'Length) := Args (J).all;
897 Arg (Arg'Last) := ASCII.Nul;
898 Arg_List (J + 2 - Args'First) := Arg.all'Address;
901 Arg_List (Arg_List'Last) := System.Null_Address;
903 -- This does not return on Unix systems
905 Set_Up_Child_Communications
906 (Descriptor, Pipe1, Pipe2, Pipe3, Command_With_Path.all,
910 Free (Command_With_Path);
912 -- Did we have an error when spawning the child ?
914 if Descriptor.Pid < Null_Pid then
915 raise Invalid_Process;
917 -- We are now in the parent process
919 Set_Up_Parent_Communications (Descriptor, Pipe1, Pipe2, Pipe3);
924 Descriptor.Buffer_Size := Buffer_Size;
926 if Buffer_Size /= 0 then
927 Descriptor.Buffer := new String (1 .. Positive (Buffer_Size));
929 end Non_Blocking_Spawn;
931 -------------------------
932 -- Reinitialize_Buffer --
933 -------------------------
935 procedure Reinitialize_Buffer
936 (Descriptor : in out Process_Descriptor'Class)
939 if Descriptor.Buffer_Size = 0 then
941 Tmp : String_Access := Descriptor.Buffer;
946 (1 .. Descriptor.Buffer_Index - Descriptor.Last_Match_End);
949 Descriptor.Buffer.all := Tmp
950 (Descriptor.Last_Match_End + 1 .. Descriptor.Buffer_Index);
955 Descriptor.Buffer_Index := Descriptor.Buffer'Last;
959 (1 .. Descriptor.Buffer_Index - Descriptor.Last_Match_End) :=
961 (Descriptor.Last_Match_End + 1 .. Descriptor.Buffer_Index);
963 if Descriptor.Buffer_Index > Descriptor.Last_Match_End then
964 Descriptor.Buffer_Index :=
965 Descriptor.Buffer_Index - Descriptor.Last_Match_End;
967 Descriptor.Buffer_Index := 0;
971 Descriptor.Last_Match_Start := 0;
972 Descriptor.Last_Match_End := 0;
973 end Reinitialize_Buffer;
979 procedure Remove_Filter
980 (Descriptor : in out Process_Descriptor;
981 Filter : Filter_Function)
983 Previous : Filter_List := null;
984 Current : Filter_List := Descriptor.Filters;
987 while Current /= null loop
988 if Current.Filter = Filter then
989 if Previous = null then
990 Descriptor.Filters := Current.Next;
992 Previous.Next := Current.Next;
997 Current := Current.Next;
1006 (Descriptor : in out Process_Descriptor;
1008 Add_LF : Boolean := True;
1009 Empty_Buffer : Boolean := False)
1012 Full_Str : constant String := Str & ASCII.LF;
1014 Result : Expect_Match;
1015 Descriptors : Array_Of_Pd := (1 => Descriptor'Unrestricted_Access);
1018 if Empty_Buffer then
1020 -- Force a read on the process if there is anything waiting.
1022 Expect_Internal (Descriptors, Result,
1023 Timeout => 0, Full_Buffer => False);
1024 Descriptor.Last_Match_End := Descriptor.Buffer_Index;
1028 Reinitialize_Buffer (Descriptor);
1032 Last := Full_Str'Last;
1034 Last := Full_Str'Last - 1;
1037 Call_Filters (Descriptor, Full_Str (Full_Str'First .. Last), Input);
1039 N := Write (Descriptor.Input_Fd,
1041 Last - Full_Str'First + 1);
1048 procedure Send_Signal
1049 (Descriptor : Process_Descriptor;
1053 Kill (Descriptor.Pid, Signal);
1054 -- ??? Need to check process status here.
1057 ---------------------------------
1058 -- Set_Up_Child_Communications --
1059 ---------------------------------
1061 procedure Set_Up_Child_Communications
1062 (Pid : in out Process_Descriptor;
1063 Pipe1 : in out Pipe_Type;
1064 Pipe2 : in out Pipe_Type;
1065 Pipe3 : in out Pipe_Type;
1067 Args : in System.Address)
1069 pragma Warnings (Off, Pid);
1072 -- Since the code between fork and exec on VMS executes
1073 -- in the context of the parent process, we need to
1074 -- perform the following actions:
1075 -- - save stdin, stdout, stderr
1076 -- - replace them by our pipes
1077 -- - create the child with process handle inheritance
1078 -- - revert to the previous stdin, stdout and stderr.
1080 Save_Input := Dup (GNAT.OS_Lib.Standin);
1081 Save_Output := Dup (GNAT.OS_Lib.Standout);
1082 Save_Error := Dup (GNAT.OS_Lib.Standerr);
1084 -- Since we are still called from the parent process, there is no way
1085 -- currently we can cleanly close the unneeded ends of the pipes, but
1086 -- this doesn't really matter.
1087 -- We could close Pipe1.Output, Pipe2.Input, Pipe3.Input.
1089 Dup2 (Pipe1.Input, GNAT.OS_Lib.Standin);
1090 Dup2 (Pipe2.Output, GNAT.OS_Lib.Standout);
1091 Dup2 (Pipe3.Output, GNAT.OS_Lib.Standerr);
1093 Portable_Execvp (Pid.Pid'Access, Cmd & ASCII.Nul, Args);
1095 end Set_Up_Child_Communications;
1097 ---------------------------
1098 -- Set_Up_Communications --
1099 ---------------------------
1101 procedure Set_Up_Communications
1102 (Pid : in out Process_Descriptor;
1103 Err_To_Out : Boolean;
1104 Pipe1 : access Pipe_Type;
1105 Pipe2 : access Pipe_Type;
1106 Pipe3 : access Pipe_Type)
1111 if Create_Pipe (Pipe1) /= 0 then
1115 if Create_Pipe (Pipe2) /= 0 then
1119 Pid.Input_Fd := Pipe1.Output;
1120 Pid.Output_Fd := Pipe2.Input;
1123 Pipe3.all := Pipe2.all;
1125 if Create_Pipe (Pipe3) /= 0 then
1130 Pid.Error_Fd := Pipe3.Input;
1131 end Set_Up_Communications;
1133 ----------------------------------
1134 -- Set_Up_Parent_Communications --
1135 ----------------------------------
1137 procedure Set_Up_Parent_Communications
1138 (Pid : in out Process_Descriptor;
1139 Pipe1 : in out Pipe_Type;
1140 Pipe2 : in out Pipe_Type;
1141 Pipe3 : in out Pipe_Type)
1143 pragma Warnings (Off, Pid);
1147 Dup2 (Save_Input, GNAT.OS_Lib.Standin);
1148 Dup2 (Save_Output, GNAT.OS_Lib.Standout);
1149 Dup2 (Save_Error, GNAT.OS_Lib.Standerr);
1152 Close (Save_Output);
1155 Close (Pipe1.Input);
1156 Close (Pipe2.Output);
1157 Close (Pipe3.Output);
1158 end Set_Up_Parent_Communications;
1164 procedure Trace_Filter
1165 (Descriptor : Process_Descriptor'Class;
1167 User_Data : System.Address := System.Null_Address)
1169 pragma Warnings (Off, Descriptor);
1170 pragma Warnings (Off, User_Data);
1176 --------------------
1177 -- Unlock_Filters --
1178 --------------------
1180 procedure Unlock_Filters (Descriptor : in out Process_Descriptor) is
1182 if Descriptor.Filters_Lock > 0 then
1183 Descriptor.Filters_Lock := Descriptor.Filters_Lock - 1;