1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- G N A T . E X P E C T --
9 -- Copyright (C) 2002-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 3, 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. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 -- This is the VMS version
34 with System
; use System
;
35 with Ada
.Calendar
; use Ada
.Calendar
;
38 with GNAT
.OS_Lib
; use GNAT
.OS_Lib
;
39 with GNAT
.Regpat
; use GNAT
.Regpat
;
41 with Ada
.Unchecked_Deallocation
;
43 package body GNAT
.Expect
is
45 type Array_Of_Pd
is array (Positive range <>) of Process_Descriptor_Access
;
47 Save_Input
: File_Descriptor
;
48 Save_Output
: File_Descriptor
;
49 Save_Error
: File_Descriptor
;
51 Expect_Process_Died
: constant Expect_Match
:= -100;
52 Expect_Internal_Error
: constant Expect_Match
:= -101;
53 -- Additional possible outputs of Expect_Internal. These are not visible in
54 -- the spec because the user will never see them.
56 procedure Expect_Internal
57 (Descriptors
: in out Array_Of_Pd
;
58 Result
: out Expect_Match
;
60 Full_Buffer
: Boolean);
61 -- Internal function used to read from the process Descriptor.
63 -- Several outputs are possible:
64 -- Result=Expect_Timeout, if no output was available before the timeout
66 -- Result=Expect_Full_Buffer, if Full_Buffer is True and some characters
67 -- had to be discarded from the internal buffer of Descriptor.
68 -- Result=Express_Process_Died if one of the processes was terminated.
69 -- That process's Input_Fd is set to Invalid_FD
70 -- Result=Express_Internal_Error
71 -- Result=<integer>, indicates how many characters were added to the
72 -- internal buffer. These characters are from indexes
73 -- Descriptor.Buffer_Index - Result + 1 .. Descriptor.Buffer_Index
74 -- Process_Died is raised if the process is no longer valid.
76 procedure Reinitialize_Buffer
77 (Descriptor
: in out Process_Descriptor
'Class);
78 -- Reinitialize the internal buffer.
79 -- The buffer is deleted up to the end of the last match.
81 procedure Free
is new Ada
.Unchecked_Deallocation
82 (Pattern_Matcher
, Pattern_Matcher_Access
);
84 procedure Call_Filters
85 (Pid
: Process_Descriptor
'Class;
87 Filter_On
: Filter_Type
);
88 -- Call all the filters that have the appropriate type.
89 -- This function does nothing if the filters are locked
91 ------------------------------
92 -- Target dependent section --
93 ------------------------------
95 function Dup
(Fd
: File_Descriptor
) return File_Descriptor
;
96 pragma Import
(C
, Dup
, "decc$dup");
98 procedure Dup2
(Old_Fd
, New_Fd
: File_Descriptor
);
99 pragma Import
(C
, Dup2
, "decc$dup2");
101 procedure Kill
(Pid
: Process_Id
; Sig_Num
: Integer);
102 pragma Import
(C
, Kill
, "decc$kill");
104 function Create_Pipe
(Pipe
: not null access Pipe_Type
) return Integer;
105 pragma Import
(C
, Create_Pipe
, "__gnat_pipe");
108 (Fds
: System
.Address
;
111 Is_Set
: System
.Address
) return Integer;
112 pragma Import
(C
, Poll
, "__gnat_expect_poll");
113 -- Check whether there is any data waiting on the file descriptor
114 -- Out_fd, and wait if there is none, at most Timeout milliseconds
115 -- Returns -1 in case of error, 0 if the timeout expired before
116 -- data became available.
118 -- Out_Is_Set is set to 1 if data was available, 0 otherwise.
120 function Waitpid
(Pid
: Process_Id
) return Integer;
121 pragma Import
(C
, Waitpid
, "__gnat_waitpid");
122 -- Wait for a specific process id, and return its exit code
128 function "+" (S
: String) return GNAT
.OS_Lib
.String_Access
is
130 return new String'(S);
138 (P : GNAT.Regpat.Pattern_Matcher) return Pattern_Matcher_Access
141 return new GNAT.Regpat.Pattern_Matcher'(P
);
149 (Descriptor
: in out Process_Descriptor
;
150 Filter
: Filter_Function
;
151 Filter_On
: Filter_Type
:= Output
;
152 User_Data
: System
.Address
:= System
.Null_Address
;
153 After
: Boolean := False)
155 Current
: Filter_List
:= Descriptor
.Filters
;
159 while Current
/= null and then Current
.Next
/= null loop
160 Current
:= Current
.Next
;
163 if Current
= null then
164 Descriptor
.Filters
:=
165 new Filter_List_Elem
'
166 (Filter => Filter, Filter_On => Filter_On,
167 User_Data => User_Data, Next => null);
170 new Filter_List_Elem'
171 (Filter
=> Filter
, Filter_On
=> Filter_On
,
172 User_Data
=> User_Data
, Next
=> null);
176 Descriptor
.Filters
:=
177 new Filter_List_Elem
'
178 (Filter => Filter, Filter_On => Filter_On,
179 User_Data => User_Data, Next => Descriptor.Filters);
187 procedure Call_Filters
188 (Pid : Process_Descriptor'Class;
190 Filter_On : Filter_Type)
192 Current_Filter : Filter_List;
195 if Pid.Filters_Lock = 0 then
196 Current_Filter := Pid.Filters;
198 while Current_Filter /= null loop
199 if Current_Filter.Filter_On = Filter_On then
200 Current_Filter.Filter
201 (Pid, Str, Current_Filter.User_Data);
204 Current_Filter := Current_Filter.Next;
214 (Descriptor : in out Process_Descriptor;
215 Status : out Integer)
218 if Descriptor.Input_Fd /= Invalid_FD then
219 Close (Descriptor.Input_Fd);
222 if Descriptor.Error_Fd /= Descriptor.Output_Fd then
223 Close (Descriptor.Error_Fd);
226 Close (Descriptor.Output_Fd);
228 -- ??? Should have timeouts for different signals
230 if Descriptor.Pid > 0 then -- see comment in Send_Signal
231 Kill (Descriptor.Pid, Sig_Num => 9);
234 GNAT.OS_Lib.Free (Descriptor.Buffer);
235 Descriptor.Buffer_Size := 0;
237 -- Check process id (see comment in Send_Signal)
239 if Descriptor.Pid > 0 then
240 Status := Waitpid (Descriptor.Pid);
242 raise Invalid_Process;
246 procedure Close (Descriptor : in out Process_Descriptor) is
249 Close (Descriptor, Status);
257 (Descriptor : in out Process_Descriptor;
258 Result : out Expect_Match;
260 Timeout : Integer := 10_000;
261 Full_Buffer : Boolean := False)
265 Expect (Descriptor, Result, Never_Match, Timeout, Full_Buffer);
267 Expect (Descriptor, Result, Compile (Regexp), Timeout, Full_Buffer);
272 (Descriptor : in out Process_Descriptor;
273 Result : out Expect_Match;
275 Matched : out GNAT.Regpat.Match_Array;
276 Timeout : Integer := 10_000;
277 Full_Buffer : Boolean := False)
280 pragma Assert (Matched'First = 0);
283 (Descriptor, Result, Never_Match, Matched, Timeout, Full_Buffer);
286 (Descriptor, Result, Compile (Regexp), Matched, Timeout,
292 (Descriptor : in out Process_Descriptor;
293 Result : out Expect_Match;
294 Regexp : GNAT.Regpat.Pattern_Matcher;
295 Timeout : Integer := 10_000;
296 Full_Buffer : Boolean := False)
298 Matched : GNAT.Regpat.Match_Array (0 .. 0);
301 Expect (Descriptor, Result, Regexp, Matched, Timeout, Full_Buffer);
305 (Descriptor : in out Process_Descriptor;
306 Result : out Expect_Match;
307 Regexp : GNAT.Regpat.Pattern_Matcher;
308 Matched : out GNAT.Regpat.Match_Array;
309 Timeout : Integer := 10_000;
310 Full_Buffer : Boolean := False)
313 Descriptors : Array_Of_Pd := (1 => Descriptor'Unrestricted_Access);
314 Try_Until : constant Time := Clock + Duration (Timeout) / 1000.0;
315 Timeout_Tmp : Integer := Timeout;
318 pragma Assert (Matched'First = 0);
319 Reinitialize_Buffer (Descriptor);
322 -- First, test if what is already in the buffer matches (This is
323 -- required if this package is used in multi-task mode, since one of
324 -- the tasks might have added something in the buffer, and we don't
325 -- want other tasks to wait for new input to be available before
326 -- checking the regexps).
329 (Regexp, Descriptor.Buffer (1 .. Descriptor.Buffer_Index), Matched);
331 if Descriptor.Buffer_Index >= 1 and then Matched (0).First /= 0 then
333 Descriptor.Last_Match_Start := Matched (0).First;
334 Descriptor.Last_Match_End := Matched (0).Last;
338 -- Else try to read new input
340 Expect_Internal (Descriptors, N, Timeout_Tmp, Full_Buffer);
343 when Expect_Internal_Error | Expect_Process_Died =>
346 when Expect_Timeout | Expect_Full_Buffer =>
354 -- Calculate the timeout for the next turn
356 -- Note that Timeout is, from the caller's perspective, the maximum
357 -- time until a match, not the maximum time until some output is
358 -- read, and thus cannot be reused as is for Expect_Internal.
360 if Timeout /= -1 then
361 Timeout_Tmp := Integer (Try_Until - Clock) * 1000;
363 if Timeout_Tmp < 0 then
364 Result := Expect_Timeout;
370 -- Even if we had the general timeout above, we have to test that the
371 -- last test we read from the external process didn't match.
374 (Regexp, Descriptor.Buffer (1 .. Descriptor.Buffer_Index), Matched);
376 if Matched (0).First /= 0 then
378 Descriptor.Last_Match_Start := Matched (0).First;
379 Descriptor.Last_Match_End := Matched (0).Last;
385 (Descriptor : in out Process_Descriptor;
386 Result : out Expect_Match;
387 Regexps : Regexp_Array;
388 Timeout : Integer := 10_000;
389 Full_Buffer : Boolean := False)
391 Patterns : Compiled_Regexp_Array (Regexps'Range);
392 Matched : GNAT.Regpat.Match_Array (0 .. 0);
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);
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);
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);
497 when Expect_Internal_Error | Expect_Process_Died =>
500 when Expect_Timeout | Expect_Full_Buffer =>
511 (Result : out Expect_Match;
512 Regexps : Multiprocess_Regexp_Array;
513 Matched : out GNAT.Regpat.Match_Array;
514 Timeout : Integer := 10_000;
515 Full_Buffer : Boolean := False)
518 Descriptors : Array_Of_Pd (Regexps'Range);
521 pragma Assert (Matched'First = 0);
523 for J in Descriptors'Range loop
524 Descriptors (J) := Regexps (J).Descriptor;
526 if Descriptors (J) /= null then
527 Reinitialize_Buffer (Regexps (J).Descriptor.all);
532 -- First, test if what is already in the buffer matches (This is
533 -- required if this package is used in multi-task mode, since one of
534 -- the tasks might have added something in the buffer, and we don't
535 -- want other tasks to wait for new input to be available before
536 -- checking the regexps).
538 for J in Regexps'Range loop
539 if Regexps (J).Regexp /= null
540 and then Regexps (J).Descriptor /= null
542 Match (Regexps (J).Regexp.all,
543 Regexps (J).Descriptor.Buffer
544 (1 .. Regexps (J).Descriptor.Buffer_Index),
547 if Matched (0) /= No_Match then
548 Result := Expect_Match (J);
549 Regexps (J).Descriptor.Last_Match_Start := Matched (0).First;
550 Regexps (J).Descriptor.Last_Match_End := Matched (0).Last;
556 Expect_Internal (Descriptors, N, Timeout, Full_Buffer);
559 when Expect_Internal_Error | Expect_Process_Died =>
562 when Expect_Timeout | Expect_Full_Buffer =>
572 ---------------------
573 -- Expect_Internal --
574 ---------------------
576 procedure Expect_Internal
577 (Descriptors : in out Array_Of_Pd;
578 Result : out Expect_Match;
580 Full_Buffer : Boolean)
582 Num_Descriptors : Integer;
583 Buffer_Size : Integer := 0;
587 type File_Descriptor_Array is
588 array (0 .. Descriptors'Length - 1) of File_Descriptor;
589 Fds : aliased File_Descriptor_Array;
590 Fds_Count : Natural := 0;
592 Fds_To_Descriptor : array (Fds'Range) of Integer;
593 -- Maps file descriptor entries from Fds to entries in Descriptors.
594 -- They do not have the same index when entries in Descriptors are null.
596 type Integer_Array is array (Fds'Range) of Integer;
597 Is_Set : aliased Integer_Array;
600 for J in Descriptors'Range loop
601 if Descriptors (J) /= null then
602 Fds (Fds'First + Fds_Count) := Descriptors (J).Output_Fd;
603 Fds_To_Descriptor (Fds'First + Fds_Count) := J;
604 Fds_Count := Fds_Count + 1;
606 if Descriptors (J).Buffer_Size = 0 then
607 Buffer_Size := Integer'Max (Buffer_Size, 4096);
610 Integer'Max (Buffer_Size, Descriptors (J).Buffer_Size);
616 Buffer : aliased String (1 .. Buffer_Size);
617 -- Buffer used for input. This is allocated only once, not for
618 -- every iteration of the loop
621 -- Index in Descriptors
624 -- Loop until we match or we have a timeout
628 Poll (Fds'Address, Fds_Count, Timeout, Is_Set'Address);
630 case Num_Descriptors is
635 Result := Expect_Internal_Error;
641 Result := Expect_Timeout;
647 for F in Fds'Range loop
648 if Is_Set (F) = 1 then
649 D := Fds_To_Descriptor (F);
651 Buffer_Size := Descriptors (D).Buffer_Size;
653 if Buffer_Size = 0 then
657 N := Read (Descriptors (D).Output_Fd, Buffer'Address,
660 -- Error or End of file
663 -- ??? Note that ddd tries again up to three times
664 -- in that case. See LiterateA.C:174
666 Descriptors (D).Input_Fd := Invalid_FD;
667 Result := Expect_Process_Died;
671 -- If there is no limit to the buffer size
673 if Descriptors (D).Buffer_Size = 0 then
676 Tmp : String_Access := Descriptors (D).Buffer;
680 Descriptors (D).Buffer :=
681 new String (1 .. Tmp'Length + N);
682 Descriptors (D).Buffer (1 .. Tmp'Length) :=
684 Descriptors (D).Buffer
685 (Tmp'Length + 1 .. Tmp'Length + N) :=
688 Descriptors (D).Buffer_Index :=
689 Descriptors (D).Buffer'Last;
692 Descriptors (D).Buffer :=
694 Descriptors (D).Buffer.all :=
696 Descriptors (D).Buffer_Index := N;
701 -- Add what we read to the buffer
703 if Descriptors (D).Buffer_Index + N >
704 Descriptors (D).Buffer_Size
706 -- If the user wants to know when we have
707 -- read more than the buffer can contain.
710 Result := Expect_Full_Buffer;
714 -- Keep as much as possible from the buffer,
715 -- and forget old characters.
717 Descriptors (D).Buffer
718 (1 .. Descriptors (D).Buffer_Size - N) :=
719 Descriptors (D).Buffer
720 (N - Descriptors (D).Buffer_Size +
721 Descriptors (D).Buffer_Index + 1 ..
722 Descriptors (D).Buffer_Index);
723 Descriptors (D).Buffer_Index :=
724 Descriptors (D).Buffer_Size - N;
727 -- Keep what we read in the buffer
729 Descriptors (D).Buffer
730 (Descriptors (D).Buffer_Index + 1 ..
731 Descriptors (D).Buffer_Index + N) :=
733 Descriptors (D).Buffer_Index :=
734 Descriptors (D).Buffer_Index + N;
737 -- Call each of the output filter with what we
741 (Descriptors (D).all, Buffer (1 .. N), Output);
743 Result := Expect_Match (D);
757 function Expect_Out (Descriptor : Process_Descriptor) return String is
759 return Descriptor.Buffer (1 .. Descriptor.Last_Match_End);
762 ----------------------
763 -- Expect_Out_Match --
764 ----------------------
766 function Expect_Out_Match (Descriptor : Process_Descriptor) return String is
768 return Descriptor.Buffer
769 (Descriptor.Last_Match_Start .. Descriptor.Last_Match_End);
770 end Expect_Out_Match;
772 ------------------------
773 -- First_Dead_Process --
774 ------------------------
776 function First_Dead_Process
777 (Regexp : Multiprocess_Regexp_Array) return Natural
780 for R in Regexp'Range loop
781 if Regexp (R).Descriptor /= null
782 and then Regexp (R).Descriptor.Input_Fd = GNAT.OS_Lib.Invalid_FD
789 end First_Dead_Process;
796 (Descriptor : in out Process_Descriptor;
797 Timeout : Integer := 0)
799 Buffer_Size : constant Integer := 8192;
800 Num_Descriptors : Integer;
802 Is_Set : aliased Integer;
803 Buffer : aliased String (1 .. Buffer_Size);
806 -- Empty the current buffer
808 Descriptor.Last_Match_End := Descriptor.Buffer_Index;
809 Reinitialize_Buffer (Descriptor);
811 -- Read everything from the process to flush its output
815 Poll (Descriptor.Output_Fd'Address, 1, Timeout, Is_Set'Address);
817 case Num_Descriptors is
824 -- Timeout => End of flush
833 N := Read (Descriptor.Output_Fd, Buffer'Address,
850 procedure Free (Regexp : in out Multiprocess_Regexp) is
851 procedure Unchecked_Free is new Ada.Unchecked_Deallocation
852 (Process_Descriptor'Class, Process_Descriptor_Access);
854 Unchecked_Free (Regexp.Descriptor);
855 Free (Regexp.Regexp);
858 ------------------------
859 -- Get_Command_Output --
860 ------------------------
862 function Get_Command_Output
864 Arguments : GNAT.OS_Lib.Argument_List;
866 Status : not null access Integer;
867 Err_To_Out : Boolean := False) return String
871 Process : Process_Descriptor;
873 Output : String_Access := new String (1 .. 1024);
874 -- Buffer used to accumulate standard output from the launched
875 -- command, expanded as necessary during execution.
878 -- Index of the last used character within Output
882 (Process, Command, Arguments, Err_To_Out => Err_To_Out);
884 if Input'Length > 0 then
885 Send (Process, Input);
888 GNAT.OS_Lib.Close (Get_Input_Fd (Process));
891 Result : Expect_Match;
894 -- This loop runs until the call to Expect raises Process_Died
897 Expect (Process, Result, ".+");
900 NOutput : String_Access;
901 S : constant String := Expect_Out (Process);
902 pragma Assert (S'Length > 0);
905 -- Expand buffer if we need more space
907 if Last + S'Length > Output'Last then
908 NOutput := new String (1 .. 2 * Output'Last);
909 NOutput (Output'Range) := Output.all;
912 -- Here if current buffer size is OK
918 NOutput (Last + 1 .. Last + S'Length) := S;
919 Last := Last + S'Length;
926 Close (Process, Status.all);
934 S : constant String := Output (1 .. Last);
939 end Get_Command_Output;
945 function Get_Error_Fd
946 (Descriptor : Process_Descriptor) return GNAT.OS_Lib.File_Descriptor
949 return Descriptor.Error_Fd;
956 function Get_Input_Fd
957 (Descriptor : Process_Descriptor) return GNAT.OS_Lib.File_Descriptor
960 return Descriptor.Input_Fd;
967 function Get_Output_Fd
968 (Descriptor : Process_Descriptor) return GNAT.OS_Lib.File_Descriptor
971 return Descriptor.Output_Fd;
979 (Descriptor : Process_Descriptor) return Process_Id
982 return Descriptor.Pid;
989 function Has_Process (Regexp : Multiprocess_Regexp_Array) return Boolean is
991 return Regexp /= (Regexp'Range => (null, null));
998 procedure Interrupt (Descriptor : in out Process_Descriptor) is
999 SIGINT : constant := 2;
1001 Send_Signal (Descriptor, SIGINT);
1008 procedure Lock_Filters (Descriptor : in out Process_Descriptor) is
1010 Descriptor.Filters_Lock := Descriptor.Filters_Lock + 1;
1013 ------------------------
1014 -- Non_Blocking_Spawn --
1015 ------------------------
1017 procedure Non_Blocking_Spawn
1018 (Descriptor : out Process_Descriptor'Class;
1020 Args : GNAT.OS_Lib.Argument_List;
1021 Buffer_Size : Natural := 4096;
1022 Err_To_Out : Boolean := False)
1025 -------------------------
1026 -- Reinitialize_Buffer --
1027 -------------------------
1029 procedure Reinitialize_Buffer
1030 (Descriptor : in out Process_Descriptor'Class)
1033 if Descriptor.Buffer_Size = 0 then
1035 Tmp : String_Access := Descriptor.Buffer;
1038 Descriptor.Buffer :=
1040 (1 .. Descriptor.Buffer_Index - Descriptor.Last_Match_End);
1043 Descriptor.Buffer.all := Tmp
1044 (Descriptor.Last_Match_End + 1 .. Descriptor.Buffer_Index);
1049 Descriptor.Buffer_Index := Descriptor.Buffer'Last;
1053 (1 .. Descriptor.Buffer_Index - Descriptor.Last_Match_End) :=
1055 (Descriptor.Last_Match_End + 1 .. Descriptor.Buffer_Index);
1057 if Descriptor.Buffer_Index > Descriptor.Last_Match_End then
1058 Descriptor.Buffer_Index :=
1059 Descriptor.Buffer_Index - Descriptor.Last_Match_End;
1061 Descriptor.Buffer_Index := 0;
1065 Descriptor.Last_Match_Start := 0;
1066 Descriptor.Last_Match_End := 0;
1067 end Reinitialize_Buffer;
1073 procedure Remove_Filter
1074 (Descriptor : in out Process_Descriptor;
1075 Filter : Filter_Function)
1077 Previous : Filter_List := null;
1078 Current : Filter_List := Descriptor.Filters;
1081 while Current /= null loop
1082 if Current.Filter = Filter then
1083 if Previous = null then
1084 Descriptor.Filters := Current.Next;
1086 Previous.Next := Current.Next;
1090 Previous := Current;
1091 Current := Current.Next;
1100 (Descriptor : in out Process_Descriptor;
1102 Add_LF : Boolean := True;
1103 Empty_Buffer : Boolean := False)
1105 Full_Str : constant String := Str & ASCII.LF;
1107 Result : Expect_Match;
1108 Descriptors : Array_Of_Pd := (1 => Descriptor'Unrestricted_Access);
1111 pragma Unreferenced (Discard);
1114 if Empty_Buffer then
1116 -- Force a read on the process if there is anything waiting
1118 Expect_Internal (Descriptors, Result,
1119 Timeout => 0, Full_Buffer => False);
1121 if Result = Expect_Internal_Error
1122 or else Result = Expect_Process_Died
1127 Descriptor.Last_Match_End := Descriptor.Buffer_Index;
1131 Reinitialize_Buffer (Descriptor);
1134 Last := (if Add_LF then Full_Str'Last else Full_Str'Last - 1);
1136 Call_Filters (Descriptor, Full_Str (Full_Str'First .. Last), Input);
1139 Write (Descriptor.Input_Fd,
1141 Last - Full_Str'First + 1);
1142 -- Shouldn't we at least have a pragma Assert on the result ???
1149 procedure Send_Signal
1150 (Descriptor : Process_Descriptor;
1154 -- A nonpositive process id passed to kill has special meanings. For
1155 -- example, -1 means kill all processes in sight, including self, in
1156 -- POSIX and Windows (and something slightly different in Linux). See
1157 -- man pages for details. In any case, we don't want to do that. Note
1158 -- that Descriptor.Pid will be -1 if the process was not successfully
1159 -- started; we don't want to kill ourself in that case.
1161 if Descriptor.Pid > 0 then
1162 Kill (Descriptor.Pid, Signal);
1163 -- ??? Need to check process status here
1165 raise Invalid_Process;
1169 ---------------------------------
1170 -- Set_Up_Child_Communications --
1171 ---------------------------------
1173 procedure Set_Up_Child_Communications
1174 (Pid : in out Process_Descriptor;
1175 Pipe1 : in out Pipe_Type;
1176 Pipe2 : in out Pipe_Type;
1177 Pipe3 : in out Pipe_Type;
1179 Args : System.Address)
1181 pragma Warnings (Off, Pid);
1182 pragma Warnings (Off, Pipe1);
1183 pragma Warnings (Off, Pipe2);
1184 pragma Warnings (Off, Pipe3);
1187 -- Since the code between fork and exec on VMS executes
1188 -- in the context of the parent process, we need to
1189 -- perform the following actions:
1190 -- - save stdin, stdout, stderr
1191 -- - replace them by our pipes
1192 -- - create the child with process handle inheritance
1193 -- - revert to the previous stdin, stdout and stderr.
1195 Save_Input := Dup (GNAT.OS_Lib.Standin);
1196 Save_Output := Dup (GNAT.OS_Lib.Standout);
1197 Save_Error := Dup (GNAT.OS_Lib.Standerr);
1199 -- Since we are still called from the parent process, there is no way
1200 -- currently we can cleanly close the unneeded ends of the pipes, but
1201 -- this doesn't really matter.
1203 -- We could close Pipe1.Output, Pipe2.Input, Pipe3.Input
1205 Dup2 (Pipe1.Input, GNAT.OS_Lib.Standin);
1206 Dup2 (Pipe2.Output, GNAT.OS_Lib.Standout);
1207 Dup2 (Pipe3.Output, GNAT.OS_Lib.Standerr);
1209 Portable_Execvp (Pid.Pid'Access, Cmd & ASCII.NUL, Args);
1210 end Set_Up_Child_Communications;
1212 ---------------------------
1213 -- Set_Up_Communications --
1214 ---------------------------
1216 procedure Set_Up_Communications
1217 (Pid : in out Process_Descriptor;
1218 Err_To_Out : Boolean;
1219 Pipe1 : not null access Pipe_Type;
1220 Pipe2 : not null access Pipe_Type;
1221 Pipe3 : not null access Pipe_Type)
1226 if Create_Pipe (Pipe1) /= 0 then
1230 if Create_Pipe (Pipe2) /= 0 then
1234 Pid.Input_Fd := Pipe1.Output;
1235 Pid.Output_Fd := Pipe2.Input;
1238 Pipe3.all := Pipe2.all;
1240 if Create_Pipe (Pipe3) /= 0 then
1245 Pid.Error_Fd := Pipe3.Input;
1246 end Set_Up_Communications;
1248 ----------------------------------
1249 -- Set_Up_Parent_Communications --
1250 ----------------------------------
1252 procedure Set_Up_Parent_Communications
1253 (Pid : in out Process_Descriptor;
1254 Pipe1 : in out Pipe_Type;
1255 Pipe2 : in out Pipe_Type;
1256 Pipe3 : in out Pipe_Type)
1258 pragma Warnings (Off, Pid);
1259 pragma Warnings (Off, Pipe1);
1260 pragma Warnings (Off, Pipe2);
1261 pragma Warnings (Off, Pipe3);
1265 Dup2 (Save_Input, GNAT.OS_Lib.Standin);
1266 Dup2 (Save_Output, GNAT.OS_Lib.Standout);
1267 Dup2 (Save_Error, GNAT.OS_Lib.Standerr);
1270 Close (Save_Output);
1273 Close (Pipe1.Input);
1274 Close (Pipe2.Output);
1275 Close (Pipe3.Output);
1276 end Set_Up_Parent_Communications;
1282 procedure Trace_Filter
1283 (Descriptor : Process_Descriptor'Class;
1285 User_Data : System.Address := System.Null_Address)
1287 pragma Warnings (Off, Descriptor);
1288 pragma Warnings (Off, User_Data);
1293 --------------------
1294 -- Unlock_Filters --
1295 --------------------
1297 procedure Unlock_Filters (Descriptor : in out Process_Descriptor) is
1299 if Descriptor.Filters_Lock > 0 then
1300 Descriptor.Filters_Lock := Descriptor.Filters_Lock - 1;