[Overview][Constants][Types][Procedures and functions][Variables][Index] Reference for unit 'oldlinux' (#rtl)

AssignStream

Assign stream for in and output to a program

Declaration

Source position: line 0

function AssignStream(

  var StreamIn: text;

  var Streamout: text;

  const Prog: String

):LongInt;

function AssignStream(

  var StreamIn: Text;

  var StreamOut: Text;

  var StreamErr: Text;

  const prog: String

):LongInt;

Description

AssignStream creates a 2 or 3 pipes, i.e. two (or three) file objects, one for input, one for output,(and one for standard error) the other ends of these pipes are connected to standard input and output (and standard error) of Prog. Prog is the name of a program (including path) with options, which will be executed.

What is written to StreamOut, will go to the standard input of Prog. Whatever is written by Prog to it's standard output can be read from StreamIn. Whatever is written by Prog to it's standard error read from StreamErr, if present.

Reading and writing happens through the usual Readln(StreamIn,...) and Writeln (StreamOut,...) procedures.

Remark: You should not use Reset or Rewrite on a file opened with POpen. This will close the file before re-opening it again, thereby closing the connection with the program.

The function returns the process ID of the spawned process, or -1 in case of error.

Errors

In case of error (return value -1) LinuxError is used to report errors:

sys_emfile
Too many file descriptors for this process.
sys_enfile
The system file table is full.

Other errors include the ones by the fork and exec programs

See also

AssignPipe

  

Create a set of pipe file handlers

POpen

  

Pipe file to standard input/output of program

Example

Program Example38;

{ Program to demonstrate the AssignStream function. }

Uses oldlinux;

Var Si,So : Text;
    S : String;
    i : longint;

begin
  if not (paramstr(1)='-son') then
    begin
    Writeln ('Calling son');
    Assignstream (Si,So,'./ex38 -son');
    if linuxerror<>0 then
      begin
      writeln ('AssignStream failed !');
      halt(1);
      end;
    Writeln ('Speaking to son');
    For i:=1 to 10 do
      begin
      writeln (so,'Hello son !');
      if ioresult<>0 then writeln ('Can''t speak to son...');
      end;
    For i:=1 to 3 do writeln (so,'Hello chap !');
    close (so);
    while not eof(si) do
      begin
      readln (si,s);
      writeln ('Father: Son said : ',S);
      end;
    Writeln ('Stopped conversation');
    Close (Si);
    Writeln ('Put down phone');
    end
  Else
    begin
    Writeln ('This is the son ');
    While not eof (input) do
      begin
      readln (s);
      if pos ('Hello son !',S)<>0 then
         Writeln ('Hello Dad !')
      else
         writeln ('Who are you ?');
      end;
    close (output);
    end
end.
The latest version of this document can be found at lazarus-ccr.sourceforge.net.