diff options
author | Thomas Quinot <quinot@adacore.com> | 2005-07-04 15:25:47 +0200 |
---|---|---|
committer | Arnaud Charlet <charlet@gcc.gnu.org> | 2005-07-04 15:25:47 +0200 |
commit | 1a79be3c0024007e40e7b8ffb1cfcd5382358fc7 (patch) | |
tree | 3472e464f86f39a9585e53bad9acf1de5b6409c5 /gcc/ada/g-expect.adb | |
parent | fc64d83c6f8850a74d45bfcea1458eca80f4182c (diff) | |
download | gcc-1a79be3c0024007e40e7b8ffb1cfcd5382358fc7.zip gcc-1a79be3c0024007e40e7b8ffb1cfcd5382358fc7.tar.gz gcc-1a79be3c0024007e40e7b8ffb1cfcd5382358fc7.tar.bz2 |
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.
From-SVN: r101571
Diffstat (limited to 'gcc/ada/g-expect.adb')
-rw-r--r-- | gcc/ada/g-expect.adb | 92 |
1 files changed, 87 insertions, 5 deletions
diff --git a/gcc/ada/g-expect.adb b/gcc/ada/g-expect.adb index 9c148cc..6f0f7cf 100644 --- a/gcc/ada/g-expect.adb +++ b/gcc/ada/g-expect.adb @@ -108,7 +108,7 @@ package body GNAT.Expect is function Waitpid (Pid : Process_Id) return Integer; pragma Import (C, Waitpid, "__gnat_waitpid"); - -- Wait for a specific process id, and return its exit code. + -- Wait for a specific process id, and return its exit code --------- -- "+" -- @@ -656,7 +656,7 @@ package body GNAT.Expect is Descriptors (J).Buffer_Size - N; end if; - -- Keep what we read in the buffer. + -- Keep what we read in the buffer Descriptors (J).Buffer (Descriptors (J).Buffer_Index + 1 .. @@ -754,9 +754,91 @@ package body GNAT.Expect is end if; end case; end loop; - 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 -- ------------------ @@ -1012,7 +1094,7 @@ package body GNAT.Expect is begin if Empty_Buffer then - -- Force a read on the process if there is anything waiting. + -- Force a read on the process if there is anything waiting Expect_Internal (Descriptors, Result, Timeout => 0, Full_Buffer => False); @@ -1047,7 +1129,7 @@ package body GNAT.Expect is is begin Kill (Descriptor.Pid, Signal); - -- ??? Need to check process status here. + -- ??? Need to check process status here end Send_Signal; --------------------------------- |