comp.lang.ada
 help / color / mirror / Atom feed
* execute external shell program and process the output line by line
@ 2019-10-17 12:25 devosalain71
  2019-10-17 15:34 ` Dennis Lee Bieber
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: devosalain71 @ 2019-10-17 12:25 UTC (permalink / raw)


I need the call a process like "ls",
And process the output line by line.
So it is not sufficient if i have the return code success or error.
Thanks,

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: execute external shell program and process the output line by line
  2019-10-17 12:25 execute external shell program and process the output line by line devosalain71
@ 2019-10-17 15:34 ` Dennis Lee Bieber
  2019-10-18  7:43   ` Simon Wright
  2019-10-17 16:17 ` Dmitry A. Kazakov
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 6+ messages in thread
From: Dennis Lee Bieber @ 2019-10-17 15:34 UTC (permalink / raw)


On Thu, 17 Oct 2019 05:25:26 -0700 (PDT), devosalain71@gmail.com declaimed
the following:

>I need the call a process like "ls",
>And process the output line by line.
>So it is not sufficient if i have the return code success or error.

	So... first question: which compiler suite? I'm going to presume Linux
compatible given the specification of "ls". GNAT extension libraries cover
some of this need.

	I would also mention that "ls" may not be the best example. If your
really mean that the command you need the output from IS "ls" you might
find that you don't need it at all if using GNAT:

"""
12.68. GNAT.Directory_Operations (g-dirope.ads)
Provides a set of routines for manipulating directories, including changing
the current directory, making new directories, and scanning the files in a
directory.

12.69. GNAT.Directory_Operations.Iteration (g-diopit.ads)
A child unit of GNAT.Directory_Operations providing additional operations
for iterating through directories.
"""

	However, even that library has likely been superceded in Ada 2005 and
later by ada.directories
"""
Ada.Directories (A.16)
    This package provides operations on directories.

Ada.Directories.Hierarchical_File_Names (A.16.1)
    This package provides additional directory operations handling
hiearchical file names.

Ada.Directories.Information (A.16)
    This is an implementation defined package for additional directory
operations, which is not implemented in GNAT.
"""
example:
"""
   procedure Start_Search
     (Search    : in out Search_Type;
      Directory : String;
      Pattern   : String;
      Filter    : Filter_Type := (others => True));
   --  Starts a search in the directory entry in the directory named by
   --  Directory for entries matching Pattern. Pattern represents a file
name
   --  matching pattern. If Pattern is null, all items in the directory are
   --  matched; otherwise, the interpretation of Pattern is implementation-
   --  defined. Only items which match Filter will be returned. After a
   --  successful call on Start_Search, the object Search may have entries
   --  available, but it may have no entries available if no files or
   --  directories match Pattern and Filter. The exception Name_Error is
   --  propagated if the string given by Directory does not identify an
   --  existing directory, or if Pattern does not allow the identification
of
   --  any possible external file or directory. The exception Use_Error is
   --  propagated if the external environment does not support the
searching
   --  of the directory with the given name (in the absence of Name_Error).
"""


	And for those where you must run an external command, there are things
in GNAT extensions like:

"""
12.77. GNAT.Expect (g-expect.ads)
Provides a set of subprograms similar to what is available with the
standard Tcl Expect tool. It allows you to easily spawn and communicate
with an external process. You can send commands or inputs to the process,
and compare the output with some expected regular expression. Currently
GNAT.Expect is implemented on all native GNAT ports. It is not implemented
for cross ports, and in particular is not implemented for VxWorks or
LynxOS.
"""

	The more general mode would be to have the output of the command
written to a file, and then open/read the file... From system.os_lib
(probably another GNAT extension)

"""
   procedure Spawn
     (Program_Name : String;
      Args         : Argument_List;
      Output_File  : String;
      Success      : out Boolean;
      Return_Code  : out Integer;
      Err_To_Out   : Boolean := True);
   --  Similar to the procedure above, but saves the output of the command
to
   --  a file with the name Output_File.
   --
   --  Success is set to True if the command is executed and its output
   --  successfully written to the file. If Success is True, then
Return_Code
   --  will be set to the status code returned by the operating system.
   --  Otherwise, Return_Code is undefined.
   --
   --  Spawning processes from tasking programs is not recommended. See
   --  "NOTE: Spawn in tasking programs" below.
"""
	


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed@ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: execute external shell program and process the output line by line
  2019-10-17 12:25 execute external shell program and process the output line by line devosalain71
  2019-10-17 15:34 ` Dennis Lee Bieber
@ 2019-10-17 16:17 ` Dmitry A. Kazakov
  2019-10-17 20:31 ` Per Sandberg
  2019-10-18 14:26 ` Shark8
  3 siblings, 0 replies; 6+ messages in thread
From: Dmitry A. Kazakov @ 2019-10-17 16:17 UTC (permalink / raw)


On 2019-10-17 14:25, devosalain71@gmail.com wrote:
> I need the call a process like "ls",
> And process the output line by line.
> So it is not sufficient if i have the return code success or error.

See:

    http://www.dmitry-kazakov.de/ada/gtkada_contributions.htm#10

with callbacks for each input/output lines:

    http://www.dmitry-kazakov.de/ada/gtkada_contributions.htm#10.1

with text buffers to standard input/outputs:

    http://www.dmitry-kazakov.de/ada/gtkada_contributions.htm#10.1

The sample code are Test_Gtk_Asynchronous_Spawn and Test_Gtk_Spawn.

P.S. It is a complicated issue you better avoid if you can.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: execute external shell program and process the output line by line
  2019-10-17 12:25 execute external shell program and process the output line by line devosalain71
  2019-10-17 15:34 ` Dennis Lee Bieber
  2019-10-17 16:17 ` Dmitry A. Kazakov
@ 2019-10-17 20:31 ` Per Sandberg
  2019-10-18 14:26 ` Shark8
  3 siblings, 0 replies; 6+ messages in thread
From: Per Sandberg @ 2019-10-17 20:31 UTC (permalink / raw)


Why not use GNAT.Expect.Get_Command_Output ?
Since its already there.
/P

On 2019-10-17 14:25, devosalain71@gmail.com wrote:
> I need the call a process like "ls",
> And process the output line by line.
> So it is not sufficient if i have the return code success or error.
> Thanks,
> 


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: execute external shell program and process the output line by line
  2019-10-17 15:34 ` Dennis Lee Bieber
@ 2019-10-18  7:43   ` Simon Wright
  0 siblings, 0 replies; 6+ messages in thread
From: Simon Wright @ 2019-10-18  7:43 UTC (permalink / raw)


Dennis Lee Bieber <wlfraed@ix.netcom.com> writes:

> Ada.Directories.Hierarchical_File_Names (A.16.1)
>     This package provides additional directory operations handling
>     hiearchical file names.

Not implemented in current CE or FSF releases (but ISTR seeing reference
to it on an AdaCore site)


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: execute external shell program and process the output line by line
  2019-10-17 12:25 execute external shell program and process the output line by line devosalain71
                   ` (2 preceding siblings ...)
  2019-10-17 20:31 ` Per Sandberg
@ 2019-10-18 14:26 ` Shark8
  3 siblings, 0 replies; 6+ messages in thread
From: Shark8 @ 2019-10-18 14:26 UTC (permalink / raw)


On Thursday, October 17, 2019 at 6:25:28 AM UTC-6, devosa...@gmail.com wrote:
> I need the call a process like "ls",
> And process the output line by line.
> So it is not sufficient if i have the return code success or error.

Aside from the direct-advice others have given, allow me to give some indirect advice: be sure that you really *need* this design.

In my experience, that sort of design is usually from some horribly cobbled-together Unix/Linux system which would be much better fully implemented within your actual Ada program.

A similar thing often happens with medium/large PHP applications, where the standard solution is to use a cron-job to fire periodic scripts, which is wholly unnecessary in Ada because of (a) the presence of timed, cyclic tasks, and (b) the lack of PHP's non-persistence.

In-short, it's best not to cobble together things like you're trying to do if you can avoid doing so: you'll lose a lot of important properties, like types and their constraints.


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-10-18 14:26 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-17 12:25 execute external shell program and process the output line by line devosalain71
2019-10-17 15:34 ` Dennis Lee Bieber
2019-10-18  7:43   ` Simon Wright
2019-10-17 16:17 ` Dmitry A. Kazakov
2019-10-17 20:31 ` Per Sandberg
2019-10-18 14:26 ` Shark8

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox