comp.lang.ada
 help / color / mirror / Atom feed
From: Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject: Re: execute external shell program and process the output line by line
Date: Thu, 17 Oct 2019 11:34:24 -0400
Date: 2019-10-17T11:34:24-04:00	[thread overview]
Message-ID: <7dvgqepj9eljll1mnt9kcdbcvi69bmc3ue@4ax.com> (raw)
In-Reply-To: 69956c86-273a-40c4-b1d8-dc5c7a8e960f@googlegroups.com

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/

  reply	other threads:[~2019-10-17 15:34 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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
replies disabled

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