Implement -mmemcpy-strategy= and -mmemset-strategy= options
[official-gcc.git] / gcc / ada / g-expect-vms.adb
blob4899682ba6bce4ee01c0875773c134e951171ab5
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- G N A T . E X P E C T --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2002-2010, AdaCore --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 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. --
17 -- --
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. --
21 -- --
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/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
32 -- This is the VMS version
34 with System; use System;
35 with Ada.Calendar; use Ada.Calendar;
37 with GNAT.IO;
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;
59 Timeout : Integer;
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
65 -- expired.
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;
86 Str : String;
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");
107 function Poll
108 (Fds : System.Address;
109 Num_Fds : Integer;
110 Timeout : Integer;
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
124 ---------
125 -- "+" --
126 ---------
128 function "+" (S : String) return GNAT.OS_Lib.String_Access is
129 begin
130 return new String'(S);
131 end "+";
133 ---------
134 -- "+" --
135 ---------
137 function "+"
138 (P : GNAT.Regpat.Pattern_Matcher) return Pattern_Matcher_Access
140 begin
141 return new GNAT.Regpat.Pattern_Matcher'(P);
142 end "+";
144 ----------------
145 -- Add_Filter --
146 ----------------
148 procedure Add_Filter
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;
157 begin
158 if After then
159 while Current /= null and then Current.Next /= null loop
160 Current := Current.Next;
161 end loop;
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);
168 else
169 Current.Next :=
170 new Filter_List_Elem'
171 (Filter => Filter, Filter_On => Filter_On,
172 User_Data => User_Data, Next => null);
173 end if;
175 else
176 Descriptor.Filters :=
177 new Filter_List_Elem'
178 (Filter => Filter, Filter_On => Filter_On,
179 User_Data => User_Data, Next => Descriptor.Filters);
180 end if;
181 end Add_Filter;
183 ------------------
184 -- Call_Filters --
185 ------------------
187 procedure Call_Filters
188 (Pid : Process_Descriptor'Class;
189 Str : String;
190 Filter_On : Filter_Type)
192 Current_Filter : Filter_List;
194 begin
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);
202 end if;
204 Current_Filter := Current_Filter.Next;
205 end loop;
206 end if;
207 end Call_Filters;
209 -----------
210 -- Close --
211 -----------
213 procedure Close
214 (Descriptor : in out Process_Descriptor;
215 Status : out Integer)
217 begin
218 if Descriptor.Input_Fd /= Invalid_FD then
219 Close (Descriptor.Input_Fd);
220 end if;
222 if Descriptor.Error_Fd /= Descriptor.Output_Fd then
223 Close (Descriptor.Error_Fd);
224 end if;
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);
232 end if;
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);
241 else
242 raise Invalid_Process;
243 end if;
244 end Close;
246 procedure Close (Descriptor : in out Process_Descriptor) is
247 Status : Integer;
248 begin
249 Close (Descriptor, Status);
250 end Close;
252 ------------
253 -- Expect --
254 ------------
256 procedure Expect
257 (Descriptor : in out Process_Descriptor;
258 Result : out Expect_Match;
259 Regexp : String;
260 Timeout : Integer := 10_000;
261 Full_Buffer : Boolean := False)
263 begin
264 if Regexp = "" then
265 Expect (Descriptor, Result, Never_Match, Timeout, Full_Buffer);
266 else
267 Expect (Descriptor, Result, Compile (Regexp), Timeout, Full_Buffer);
268 end if;
269 end Expect;
271 procedure Expect
272 (Descriptor : in out Process_Descriptor;
273 Result : out Expect_Match;
274 Regexp : String;
275 Matched : out GNAT.Regpat.Match_Array;
276 Timeout : Integer := 10_000;
277 Full_Buffer : Boolean := False)
279 begin
280 pragma Assert (Matched'First = 0);
281 if Regexp = "" then
282 Expect
283 (Descriptor, Result, Never_Match, Matched, Timeout, Full_Buffer);
284 else
285 Expect
286 (Descriptor, Result, Compile (Regexp), Matched, Timeout,
287 Full_Buffer);
288 end if;
289 end Expect;
291 procedure Expect
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);
300 begin
301 Expect (Descriptor, Result, Regexp, Matched, Timeout, Full_Buffer);
302 end Expect;
304 procedure Expect
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)
312 N : Expect_Match;
313 Descriptors : Array_Of_Pd := (1 => Descriptor'Unrestricted_Access);
314 Try_Until : constant Time := Clock + Duration (Timeout) / 1000.0;
315 Timeout_Tmp : Integer := Timeout;
317 begin
318 pragma Assert (Matched'First = 0);
319 Reinitialize_Buffer (Descriptor);
321 loop
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).
328 Match
329 (Regexp, Descriptor.Buffer (1 .. Descriptor.Buffer_Index), Matched);
331 if Descriptor.Buffer_Index >= 1 and then Matched (0).First /= 0 then
332 Result := 1;
333 Descriptor.Last_Match_Start := Matched (0).First;
334 Descriptor.Last_Match_End := Matched (0).Last;
335 return;
336 end if;
338 -- Else try to read new input
340 Expect_Internal (Descriptors, N, Timeout_Tmp, Full_Buffer);
342 case N is
343 when Expect_Internal_Error | Expect_Process_Died =>
344 raise Process_Died;
346 when Expect_Timeout | Expect_Full_Buffer =>
347 Result := N;
348 return;
350 when others =>
351 null; -- See below
352 end case;
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;
365 exit;
366 end if;
367 end if;
368 end loop;
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.
373 Match
374 (Regexp, Descriptor.Buffer (1 .. Descriptor.Buffer_Index), Matched);
376 if Matched (0).First /= 0 then
377 Result := 1;
378 Descriptor.Last_Match_Start := Matched (0).First;
379 Descriptor.Last_Match_End := Matched (0).Last;
380 return;
381 end if;
382 end Expect;
384 procedure Expect
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);
394 begin
395 for J in Regexps'Range loop
396 Patterns (J) := new Pattern_Matcher'(Compile (Regexps (J).all));
397 end loop;
399 Expect (Descriptor, Result, Patterns, Matched, Timeout, Full_Buffer);
401 for J in Regexps'Range loop
402 Free (Patterns (J));
403 end loop;
404 end Expect;
406 procedure Expect
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);
415 begin
416 Expect (Descriptor, Result, Regexps, Matched, Timeout, Full_Buffer);
417 end Expect;
419 procedure Expect
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);
427 begin
428 Expect (Result, Regexps, Matched, Timeout, Full_Buffer);
429 end Expect;
431 procedure Expect
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);
441 begin
442 pragma Assert (Matched'First = 0);
444 for J in Regexps'Range loop
445 Patterns (J) := new Pattern_Matcher'(Compile (Regexps (J).all));
446 end loop;
448 Expect (Descriptor, Result, Patterns, Matched, Timeout, Full_Buffer);
450 for J in Regexps'Range loop
451 Free (Patterns (J));
452 end loop;
453 end Expect;
455 procedure Expect
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)
463 N : Expect_Match;
464 Descriptors : Array_Of_Pd := (1 => Descriptor'Unrestricted_Access);
466 begin
467 pragma Assert (Matched'First = 0);
469 Reinitialize_Buffer (Descriptor);
471 loop
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
480 Match
481 (Regexps (J).all,
482 Descriptor.Buffer (1 .. Descriptor.Buffer_Index),
483 Matched);
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;
489 return;
490 end if;
491 end loop;
492 end if;
494 Expect_Internal (Descriptors, N, Timeout, Full_Buffer);
496 case N is
497 when Expect_Internal_Error | Expect_Process_Died =>
498 raise Process_Died;
500 when Expect_Timeout | Expect_Full_Buffer =>
501 Result := N;
502 return;
504 when others =>
505 null; -- Continue
506 end case;
507 end loop;
508 end Expect;
510 procedure Expect
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)
517 N : Expect_Match;
518 Descriptors : Array_Of_Pd (Regexps'Range);
520 begin
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);
528 end if;
529 end loop;
531 loop
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
541 then
542 Match (Regexps (J).Regexp.all,
543 Regexps (J).Descriptor.Buffer
544 (1 .. Regexps (J).Descriptor.Buffer_Index),
545 Matched);
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;
551 return;
552 end if;
553 end if;
554 end loop;
556 Expect_Internal (Descriptors, N, Timeout, Full_Buffer);
558 case N is
559 when Expect_Internal_Error | Expect_Process_Died =>
560 raise Process_Died;
562 when Expect_Timeout | Expect_Full_Buffer =>
563 Result := N;
564 return;
566 when others =>
567 null; -- Continue
568 end case;
569 end loop;
570 end Expect;
572 ---------------------
573 -- Expect_Internal --
574 ---------------------
576 procedure Expect_Internal
577 (Descriptors : in out Array_Of_Pd;
578 Result : out Expect_Match;
579 Timeout : Integer;
580 Full_Buffer : Boolean)
582 Num_Descriptors : Integer;
583 Buffer_Size : Integer := 0;
585 N : Integer;
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;
599 begin
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);
608 else
609 Buffer_Size :=
610 Integer'Max (Buffer_Size, Descriptors (J).Buffer_Size);
611 end if;
612 end if;
613 end loop;
615 declare
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
620 D : Integer;
621 -- Index in Descriptors
623 begin
624 -- Loop until we match or we have a timeout
626 loop
627 Num_Descriptors :=
628 Poll (Fds'Address, Fds_Count, Timeout, Is_Set'Address);
630 case Num_Descriptors is
632 -- Error?
634 when -1 =>
635 Result := Expect_Internal_Error;
636 return;
638 -- Timeout?
640 when 0 =>
641 Result := Expect_Timeout;
642 return;
644 -- Some input
646 when others =>
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
654 Buffer_Size := 4096;
655 end if;
657 N := Read (Descriptors (D).Output_Fd, Buffer'Address,
658 Buffer_Size);
660 -- Error or End of file
662 if N <= 0 then
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;
668 return;
670 else
671 -- If there is no limit to the buffer size
673 if Descriptors (D).Buffer_Size = 0 then
675 declare
676 Tmp : String_Access := Descriptors (D).Buffer;
678 begin
679 if Tmp /= null then
680 Descriptors (D).Buffer :=
681 new String (1 .. Tmp'Length + N);
682 Descriptors (D).Buffer (1 .. Tmp'Length) :=
683 Tmp.all;
684 Descriptors (D).Buffer
685 (Tmp'Length + 1 .. Tmp'Length + N) :=
686 Buffer (1 .. N);
687 Free (Tmp);
688 Descriptors (D).Buffer_Index :=
689 Descriptors (D).Buffer'Last;
691 else
692 Descriptors (D).Buffer :=
693 new String (1 .. N);
694 Descriptors (D).Buffer.all :=
695 Buffer (1 .. N);
696 Descriptors (D).Buffer_Index := N;
697 end if;
698 end;
700 else
701 -- Add what we read to the buffer
703 if Descriptors (D).Buffer_Index + N >
704 Descriptors (D).Buffer_Size
705 then
706 -- If the user wants to know when we have
707 -- read more than the buffer can contain.
709 if Full_Buffer then
710 Result := Expect_Full_Buffer;
711 return;
712 end if;
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;
725 end if;
727 -- Keep what we read in the buffer
729 Descriptors (D).Buffer
730 (Descriptors (D).Buffer_Index + 1 ..
731 Descriptors (D).Buffer_Index + N) :=
732 Buffer (1 .. N);
733 Descriptors (D).Buffer_Index :=
734 Descriptors (D).Buffer_Index + N;
735 end if;
737 -- Call each of the output filter with what we
738 -- read.
740 Call_Filters
741 (Descriptors (D).all, Buffer (1 .. N), Output);
743 Result := Expect_Match (D);
744 return;
745 end if;
746 end if;
747 end loop;
748 end case;
749 end loop;
750 end;
751 end Expect_Internal;
753 ----------------
754 -- Expect_Out --
755 ----------------
757 function Expect_Out (Descriptor : Process_Descriptor) return String is
758 begin
759 return Descriptor.Buffer (1 .. Descriptor.Last_Match_End);
760 end Expect_Out;
762 ----------------------
763 -- Expect_Out_Match --
764 ----------------------
766 function Expect_Out_Match (Descriptor : Process_Descriptor) return String is
767 begin
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
779 begin
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
783 then
784 return R;
785 end if;
786 end loop;
788 return 0;
789 end First_Dead_Process;
791 -----------
792 -- Flush --
793 -----------
795 procedure Flush
796 (Descriptor : in out Process_Descriptor;
797 Timeout : Integer := 0)
799 Buffer_Size : constant Integer := 8192;
800 Num_Descriptors : Integer;
801 N : Integer;
802 Is_Set : aliased Integer;
803 Buffer : aliased String (1 .. Buffer_Size);
805 begin
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
813 loop
814 Num_Descriptors :=
815 Poll (Descriptor.Output_Fd'Address, 1, Timeout, Is_Set'Address);
817 case Num_Descriptors is
819 -- Error ?
821 when -1 =>
822 raise Process_Died;
824 -- Timeout => End of flush
826 when 0 =>
827 return;
829 -- Some input
831 when others =>
832 if Is_Set = 1 then
833 N := Read (Descriptor.Output_Fd, Buffer'Address,
834 Buffer_Size);
836 if N = -1 then
837 raise Process_Died;
838 elsif N = 0 then
839 return;
840 end if;
841 end if;
842 end case;
843 end loop;
844 end Flush;
846 ----------
847 -- Free --
848 ----------
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);
853 begin
854 Unchecked_Free (Regexp.Descriptor);
855 Free (Regexp.Regexp);
856 end Free;
858 ------------------------
859 -- Get_Command_Output --
860 ------------------------
862 function Get_Command_Output
863 (Command : String;
864 Arguments : GNAT.OS_Lib.Argument_List;
865 Input : String;
866 Status : not null access Integer;
867 Err_To_Out : Boolean := False) return String
869 use GNAT.Expect;
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.
877 Last : Integer := 0;
878 -- Index of the last used character within Output
880 begin
881 Non_Blocking_Spawn
882 (Process, Command, Arguments, Err_To_Out => Err_To_Out);
884 if Input'Length > 0 then
885 Send (Process, Input);
886 end if;
888 GNAT.OS_Lib.Close (Get_Input_Fd (Process));
890 declare
891 Result : Expect_Match;
893 begin
894 -- This loop runs until the call to Expect raises Process_Died
896 loop
897 Expect (Process, Result, ".+");
899 declare
900 NOutput : String_Access;
901 S : constant String := Expect_Out (Process);
902 pragma Assert (S'Length > 0);
904 begin
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;
910 Free (Output);
912 -- Here if current buffer size is OK
914 else
915 NOutput := Output;
916 end if;
918 NOutput (Last + 1 .. Last + S'Length) := S;
919 Last := Last + S'Length;
920 Output := NOutput;
921 end;
922 end loop;
924 exception
925 when Process_Died =>
926 Close (Process, Status.all);
927 end;
929 if Last = 0 then
930 return "";
931 end if;
933 declare
934 S : constant String := Output (1 .. Last);
935 begin
936 Free (Output);
937 return S;
938 end;
939 end Get_Command_Output;
941 ------------------
942 -- Get_Error_Fd --
943 ------------------
945 function Get_Error_Fd
946 (Descriptor : Process_Descriptor) return GNAT.OS_Lib.File_Descriptor
948 begin
949 return Descriptor.Error_Fd;
950 end Get_Error_Fd;
952 ------------------
953 -- Get_Input_Fd --
954 ------------------
956 function Get_Input_Fd
957 (Descriptor : Process_Descriptor) return GNAT.OS_Lib.File_Descriptor
959 begin
960 return Descriptor.Input_Fd;
961 end Get_Input_Fd;
963 -------------------
964 -- Get_Output_Fd --
965 -------------------
967 function Get_Output_Fd
968 (Descriptor : Process_Descriptor) return GNAT.OS_Lib.File_Descriptor
970 begin
971 return Descriptor.Output_Fd;
972 end Get_Output_Fd;
974 -------------
975 -- Get_Pid --
976 -------------
978 function Get_Pid
979 (Descriptor : Process_Descriptor) return Process_Id
981 begin
982 return Descriptor.Pid;
983 end Get_Pid;
985 -----------------
986 -- Has_Process --
987 -----------------
989 function Has_Process (Regexp : Multiprocess_Regexp_Array) return Boolean is
990 begin
991 return Regexp /= (Regexp'Range => (null, null));
992 end Has_Process;
994 ---------------
995 -- Interrupt --
996 ---------------
998 procedure Interrupt (Descriptor : in out Process_Descriptor) is
999 SIGINT : constant := 2;
1000 begin
1001 Send_Signal (Descriptor, SIGINT);
1002 end Interrupt;
1004 ------------------
1005 -- Lock_Filters --
1006 ------------------
1008 procedure Lock_Filters (Descriptor : in out Process_Descriptor) is
1009 begin
1010 Descriptor.Filters_Lock := Descriptor.Filters_Lock + 1;
1011 end Lock_Filters;
1013 ------------------------
1014 -- Non_Blocking_Spawn --
1015 ------------------------
1017 procedure Non_Blocking_Spawn
1018 (Descriptor : out Process_Descriptor'Class;
1019 Command : String;
1020 Args : GNAT.OS_Lib.Argument_List;
1021 Buffer_Size : Natural := 4096;
1022 Err_To_Out : Boolean := False)
1023 is separate;
1025 -------------------------
1026 -- Reinitialize_Buffer --
1027 -------------------------
1029 procedure Reinitialize_Buffer
1030 (Descriptor : in out Process_Descriptor'Class)
1032 begin
1033 if Descriptor.Buffer_Size = 0 then
1034 declare
1035 Tmp : String_Access := Descriptor.Buffer;
1037 begin
1038 Descriptor.Buffer :=
1039 new String
1040 (1 .. Descriptor.Buffer_Index - Descriptor.Last_Match_End);
1042 if Tmp /= null then
1043 Descriptor.Buffer.all := Tmp
1044 (Descriptor.Last_Match_End + 1 .. Descriptor.Buffer_Index);
1045 Free (Tmp);
1046 end if;
1047 end;
1049 Descriptor.Buffer_Index := Descriptor.Buffer'Last;
1051 else
1052 Descriptor.Buffer
1053 (1 .. Descriptor.Buffer_Index - Descriptor.Last_Match_End) :=
1054 Descriptor.Buffer
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;
1060 else
1061 Descriptor.Buffer_Index := 0;
1062 end if;
1063 end if;
1065 Descriptor.Last_Match_Start := 0;
1066 Descriptor.Last_Match_End := 0;
1067 end Reinitialize_Buffer;
1069 -------------------
1070 -- Remove_Filter --
1071 -------------------
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;
1080 begin
1081 while Current /= null loop
1082 if Current.Filter = Filter then
1083 if Previous = null then
1084 Descriptor.Filters := Current.Next;
1085 else
1086 Previous.Next := Current.Next;
1087 end if;
1088 end if;
1090 Previous := Current;
1091 Current := Current.Next;
1092 end loop;
1093 end Remove_Filter;
1095 ----------
1096 -- Send --
1097 ----------
1099 procedure Send
1100 (Descriptor : in out Process_Descriptor;
1101 Str : String;
1102 Add_LF : Boolean := True;
1103 Empty_Buffer : Boolean := False)
1105 Full_Str : constant String := Str & ASCII.LF;
1106 Last : Natural;
1107 Result : Expect_Match;
1108 Descriptors : Array_Of_Pd := (1 => Descriptor'Unrestricted_Access);
1110 Discard : Natural;
1111 pragma Unreferenced (Discard);
1113 begin
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
1123 then
1124 raise Process_Died;
1125 end if;
1127 Descriptor.Last_Match_End := Descriptor.Buffer_Index;
1129 -- Empty the buffer
1131 Reinitialize_Buffer (Descriptor);
1132 end if;
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);
1138 Discard :=
1139 Write (Descriptor.Input_Fd,
1140 Full_Str'Address,
1141 Last - Full_Str'First + 1);
1142 -- Shouldn't we at least have a pragma Assert on the result ???
1143 end Send;
1145 -----------------
1146 -- Send_Signal --
1147 -----------------
1149 procedure Send_Signal
1150 (Descriptor : Process_Descriptor;
1151 Signal : Integer)
1153 begin
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
1164 else
1165 raise Invalid_Process;
1166 end if;
1167 end Send_Signal;
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;
1178 Cmd : String;
1179 Args : System.Address)
1181 pragma Warnings (Off, Pid);
1182 pragma Warnings (Off, Pipe1);
1183 pragma Warnings (Off, Pipe2);
1184 pragma Warnings (Off, Pipe3);
1186 begin
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)
1223 begin
1224 -- Create the pipes
1226 if Create_Pipe (Pipe1) /= 0 then
1227 return;
1228 end if;
1230 if Create_Pipe (Pipe2) /= 0 then
1231 return;
1232 end if;
1234 Pid.Input_Fd := Pipe1.Output;
1235 Pid.Output_Fd := Pipe2.Input;
1237 if Err_To_Out then
1238 Pipe3.all := Pipe2.all;
1239 else
1240 if Create_Pipe (Pipe3) /= 0 then
1241 return;
1242 end if;
1243 end if;
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);
1263 begin
1265 Dup2 (Save_Input, GNAT.OS_Lib.Standin);
1266 Dup2 (Save_Output, GNAT.OS_Lib.Standout);
1267 Dup2 (Save_Error, GNAT.OS_Lib.Standerr);
1269 Close (Save_Input);
1270 Close (Save_Output);
1271 Close (Save_Error);
1273 Close (Pipe1.Input);
1274 Close (Pipe2.Output);
1275 Close (Pipe3.Output);
1276 end Set_Up_Parent_Communications;
1278 ------------------
1279 -- Trace_Filter --
1280 ------------------
1282 procedure Trace_Filter
1283 (Descriptor : Process_Descriptor'Class;
1284 Str : String;
1285 User_Data : System.Address := System.Null_Address)
1287 pragma Warnings (Off, Descriptor);
1288 pragma Warnings (Off, User_Data);
1289 begin
1290 GNAT.IO.Put (Str);
1291 end Trace_Filter;
1293 --------------------
1294 -- Unlock_Filters --
1295 --------------------
1297 procedure Unlock_Filters (Descriptor : in out Process_Descriptor) is
1298 begin
1299 if Descriptor.Filters_Lock > 0 then
1300 Descriptor.Filters_Lock := Descriptor.Filters_Lock - 1;
1301 end if;
1302 end Unlock_Filters;
1304 end GNAT.Expect;