diff options
author | charlet <charlet@138bc75d-0d04-0410-961f-82ee72b054a4> | 2005-07-04 13:25:47 +0000 |
---|---|---|
committer | charlet <charlet@138bc75d-0d04-0410-961f-82ee72b054a4> | 2005-07-04 13:25:47 +0000 |
commit | d72ec39ed281e36cd356435bacb4736ae4ae3ad4 (patch) | |
tree | 3472e464f86f39a9585e53bad9acf1de5b6409c5 /gcc/ada/g-expect-vms.adb | |
parent | f79adf86771c2fbb0d9c3c8c15a3838119298a10 (diff) | |
download | gcc-d72ec39ed281e36cd356435bacb4736ae4ae3ad4.tar.gz |
2005-07-04 Thomas Quinot <quinot@adacore.com>
* g-expect-vms.adb, g-expect.ads, g-expect.adb
(Get_Command_Output): New subprogram to launch a process and get its
standard output as a string.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@101571 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'gcc/ada/g-expect-vms.adb')
-rw-r--r-- | gcc/ada/g-expect-vms.adb | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/gcc/ada/g-expect-vms.adb b/gcc/ada/g-expect-vms.adb index ff09173d6c6..cbf8c724302 100644 --- a/gcc/ada/g-expect-vms.adb +++ b/gcc/ada/g-expect-vms.adb @@ -761,6 +761,89 @@ package body GNAT.Expect is end Flush; + ------------------------ + -- Get_Command_Output -- + ------------------------ + + function Get_Command_Output + (Command : String; + Arguments : GNAT.OS_Lib.Argument_List; + Input : String; + Status : access Integer; + Err_To_Out : Boolean := False) return String + is + use GNAT.Expect; + + Process : Process_Descriptor; + + Output : String_Access := new String (1 .. 1024); + -- Buffer used to accumulate standard output from the launched + -- command, expanded as necessary during execution. + + Last : Integer := 0; + -- Index of the last used character within Output + + begin + Non_Blocking_Spawn + (Process, Command, Arguments, Err_To_Out => Err_To_Out); + + if Input'Length > 0 then + Send (Process, Input); + end if; + + GNAT.OS_Lib.Close (Get_Input_Fd (Process)); + + declare + Result : Expect_Match; + + begin + -- This loop runs until the call to Expect raises Process_Died + + loop + Expect (Process, Result, ".+"); + + declare + NOutput : String_Access; + S : constant String := Expect_Out (Process); + pragma Assert (S'Length > 0); + + begin + -- Expand buffer if we need more space + + if Last + S'Length > Output'Last then + NOutput := new String (1 .. 2 * Output'Last); + NOutput (Output'Range) := Output.all; + Free (Output); + + -- Here if current buffer size is OK + + else + NOutput := Output; + end if; + + NOutput (Last + 1 .. Last + S'Length) := S; + Last := Last + S'Length; + Output := NOutput; + end; + end loop; + + exception + when Process_Died => + Close (Process, Status.all); + end; + + if Last = 0 then + return ""; + end if; + + declare + S : constant String := Output (1 .. Last); + begin + Free (Output); + return S; + end; + end Get_Command_Output; + ------------------ -- Get_Error_Fd -- ------------------ |