Quoting from the ARM...
The library package Text_IO has the following declaration:
...
package Ada.Text_IO is
...
-- Control of default input and output files
procedure Set_Input (File : in File_Type);
procedure Set_Output(File : in File_Type);
procedure Set_Error (File : in File_Type);
function Standard_Input return File_Type;
function Standard_Output return File_Type;
function Standard_Error return File_Type;
function Current_Input return File_Type;
function Current_Output return File_Type;
function Current_Error return File_Type;
which allow you to manipulate the default pipes stdin, stdout, stderr
as files; either as pre-opened files (input and output pipes) or allow you to redirect stdout to your own file, etc.
This example shows redirecting one of the standard pipes to a file and restoring it to the system provided file. Alternatively, File I/O subprograms may be called with the File argument set to e.g. Ada.Text_IO.Standard_Output
and should simply work as expected - outputting to Terminal or to whatever you piped stdout
to on the commandline.
Similar facilities should be available in Direct_IO, Sequential_IO, Stream_IO
etc if the data you're reading and writing isn't text.
Timur's answer shows you can read and write directly to these pipes; this answer's approach allows you to treat the standard pipes uniformly with other files, so that you can I/O either via file or pipe with the same code. For example, if a commandline filename is supplied, use that file, otherwise point your File at Standard_Output
.
And if you're asking what happens in the command line cmd1|main|grep "hello"
, yes, the output of cmd1 is on a pipe called stdout
(in C) or Standard_Output
(Ada) which is connected via the (Unix-specific pipe command) | to the Standard_Input
of your main
program written in Ada. In turn, its Standard_Output
is piped into grep's stdin
which it searches for "hello".
If you are asking how you open and access named pipes, I suspect that is OS specific, and may be a more difficult question.
(In which case, this Stack Exchange Q&A may help).