From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 2002:a05:620a:20d2:: with SMTP id f18mr21330445qka.474.1572345079004; Tue, 29 Oct 2019 03:31:19 -0700 (PDT) X-Received: by 2002:a54:448b:: with SMTP id v11mr3443263oiv.155.1572345078695; Tue, 29 Oct 2019 03:31:18 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!news.uzoreto.com!feeder1.cambriumusenet.nl!feed.tweak.nl!209.85.160.216.MISMATCH!j16no73157qtl.0!news-out.google.com!x9ni437qti.1!nntp.google.com!j16no73149qtl.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Tue, 29 Oct 2019 03:31:18 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=136.163.208.2; posting-account=HFCrOQoAAABZD_f-UUbYHm3lJDIrh-UX NNTP-Posting-Host: 136.163.208.2 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: How to run external program pipeline and capture output From: joakimds@kth.se Injection-Date: Tue, 29 Oct 2019 10:31:18 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Xref: reader01.eternal-september.org comp.lang.ada:57364 Date: 2019-10-29T03:31:18-07:00 List-Id: Den m=C3=A5ndag 28 oktober 2019 kl. 18:24:41 UTC+1 skrev Alain De Vos: > I like to run a command like : > /usr/local/bin/zsh -c "/bin/ls | /usr/bin/grep a" > But the procedure below produces no ouput,=20 >=20 > To make it strange, with /usr/local/bin/zsh -c "/bin/ls " it works fine. >=20 > procedure System_Command is > Command : String :=3D "/usr/local/bin/zsh -c ""/bin/ls | /= usr/bin/grep a"""; > Args : Argument_List_Access; > Status : aliased Integer; > Separators : constant String :=3D LF & CR; > Reply_List : Slice_Set; > begin > Ada.Text_IO.Put_Line (Command); > Args :=3D Argument_String_To_List (Command); > -- execute the system command and get the output in a single string > declare > Response : String :=3D > Get_Command_Output > (Command =3D> Args (Args'First).all, > Arguments =3D> Args (Args'First + 1 .. Args'Last), > Input =3D> "", > Status =3D> Status'Access); > begin > Free (Args); > -- split the output in a slice for easier manipulation > if Status =3D 0 then > Create (S =3D> Reply_List, > From =3D> Response, > Separators =3D> Separators, > Mode =3D> Multiple); > end if; > end; > -- do something with the system output. Just print it out > for I in 1 .. Slice_Count (Reply_List) loop > Put_Line (Slice (Reply_List, I)); > end loop; > end System_Command; In 2010 Gautier De Montmollin posted the following code snippet for executi= ng terminal commands: with Ada.Text_IO; use Ada.Text_IO; with GNAT.OS_Lib; use GNAT.OS_Lib; with GNAT.Expect; use GNAT.Expect; procedure Pipe_test is Command : constant String :=3D -- a command which should be long to execute "gnatmake -f -Pbig_project.gpr"; Pd : Process_Descriptor; Args : Argument_List_Access; Result : Expect_Match; begin Args :=3D Argument_String_To_List (Command); Non_Blocking_Spawn (Pd, Command =3D> Args (Args'First).all, Args =3D> Args (Args'First + 1 .. Args'Last), Buffer_Size =3D> 0); loop begin Expect (Pd, Result, Regexp =3D> "\n", Timeout =3D> 1_000); case Result is when Expect_Timeout =3D> Put("[timeout]"); -- no output for this time slice when Expect_Full_Buffer =3D> Put("[full]"); -- doesn't happen with buffer size =3D 0 ? when 1 =3D> Put(Expect_Out(Pd)); -- regexp matched when others =3D> Put("[?]"); -- shouldn't (and doesn't) happen end case; exception when Process_died =3D> Put("[died]"); exit; end; end loop; Put(Expect_Out(Pd)); Close (Pd); end Pipe_test; Best regards, Joakim