-- $Date: 2003/12/31 06:04:49 $
-- $Revision: 1.2 $
-- $Author: jcrocholl $

with System;
with Ada.Streams; use Ada.Streams;

-- Thick Ada wrapper for pipe functions in C.
package Pipes is

   -- Character to signal end of pipe.
   C_Constant_EOF : Character;
   pragma Import(C, C_Constant_EOF);

   -- A new stream type for pipes.
   type Pipe_Stream is
     new Root_Stream_Type with private;

   -- Make pipes compatible with stream functions.
   type Pipe is access Pipe_Stream;

   -- Either input or output.
   type Pipe_Mode is (Read_Only, Write_Only);

   -- Read-only pipe from standard input.
   function Std_In
     return Pipe-- A stream to read from standard input.

   -- Write-only pipe to standard output.
   function Std_Out
     return Pipe-- A stream to write to standard output.

   -- One-way pipe to external program.
   function Execute
     (Command : in String;    -- Name of the program to execute.
      Mode    : in Pipe_Mode-- Input or output?
     return Pipe;             -- The newly created pipe stream.

   -- Close a pipe.
   procedure Close
     (Stream : in out Pipe); -- Close this pipe.

   -- Read from a pipe.
   -- You can use Sometype'Read(Pipe, Value).
   procedure Read
     (Stream : in out Pipe_Stream;         -- Read from this stream.
      Item   : out Stream_Element_Array;   -- Read into this array.
      Last   : out Stream_Element_Offset); -- Last read element's index.

   -- Write to a pipe.
   -- You can use Sometype'Write(Pipe, Value).
   procedure Write
     (Stream : in out Pipe_Stream;       -- Write to this stream.
      Item   : in Stream_Element_Array); -- Write this array.

private

   -- Internal definition of C's view of a file.
   type C_File is new System.Address;

   -- Internal definition of a pipe stream.
   type Pipe_Stream is
     new Ada.Streams.Root_Stream_Type with
      record
         Mode : Pipe_Mode;
         File : C_File;
      end record;

end Pipes;