Popen

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Popen

Carsten Bager
I am trying to use the "Popen" function in Read mode.
I first tried the example (---Write---) from the manual (see beneath).
The POpen gives an error 25 (Not a typewriter) but it looks like it works.
I try to do almost the same (---Read---)to read the output, but I cannot get this to read
anything. As far as I can tell, the script is not executed.
Dos anybody have a hint.

Carsten

Program output is beneath ">>>>>>>>>"

------------------Write--------------------------
Program Example37 ;
{Program to demonstrate the Popen function.}
uses
  BaseUnix,Unix;
var
  f:text;
  i:longint;
begin
  writeln('Creating a shell script to which echoes its arguments ');
  writeln('and input back to stdout');
  assign (f,'test21a.sh');
  rewrite(f);
  writeln(f,'#!/bin/sh');
  writeln(f,'echo this is the child speaking . . . . ');
  writeln(f,'echo got arguments\*"$*"\*');
  writeln(f,'cat') ;
  writeln(f,'exit 2');
  writeln(f);
  close(f);
  fpchmod('test21a.sh',&755);
  popen(f,'./test21a.sh arg1 arg2','W') ;
  if fpgeterrno <>0 then
    writeln('error from POpen: errno: ',fpgeterrno);
  for i :=1 to 10 do
  writeln(f,'This is written to the pipe , and should appear on stdout.' ) ;
  Flush(f);
  Writeln('The script exited with status :',PClose(f));
  writeln;
  writeln('Press < return > to remove shell script. ');
  readln;
  assign(f,'test21a.sh');
  erase(f);
end.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Creating a shell script to which echoes its arguments
and input back to stdout
error from POpen: errno: 25
this is the child speaking . . . .
got arguments*arg1 arg2*
This is written to the pipe , and should appear on stdout.
This is written to the pipe , and should appear on stdout.
This is written to the pipe , and should appear on stdout.
This is written to the pipe , and should appear on stdout.
This is written to the pipe , and should appear on stdout.
This is written to the pipe , and should appear on stdout.
This is written to the pipe , and should appear on stdout.
This is written to the pipe , and should appear on stdout.
This is written to the pipe , and should appear on stdout.
This is written to the pipe , and should appear on stdout.
The script exited with status :2

Press < return > to remove shell script.
--------------------------

----- Read---------------------------------
Program Example37 ;
{Program to demonstrate the Popen function.}
uses BaseUnix ,
     Unix,
     errors;      {System}
var
  f:text;
  i:longint;
  s:string[255];
begin
  writeln('Creating a shell script to which echoes its arguments ');
  writeln('and input back to stdout');
  assign (f,'test21a.sh');
  rewrite(f);
  writeln(f,'#!/bin/sh');
  writeln(f,'echo this is the child speaking . . . . ' ) ;
  writeln(f,'echo got arguments\*"$*"\*');
  writeln(f,'echo got arguments\*"$*"\* >m');
  writeln(f,'exit 3');
  writeln(f);
  close(f);
  fpchmod('test21a.sh',&755);
  popen(f,'./test21a.sh arg1 arg2','R') ;
  if fpgeterrno <>0 then
    writeln('error from POpen: errno: ',StrError(fpgeterrno));
  for i :=1 to 10 do
  begin
    Readln(f,s);
    writeln(s);
  end;
  Writeln('The script exited with status :' ,PClose(f));
  writeln;
  writeln('Press < return > to remove shell script. ');
  readln ;
  assign (f,'test21a.sh');
  erase(f);
end.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Creating a shell script to which echoes its arguments
and input back to stdout
error from POpen: errno: Not a typewriter










The script exited with status :127

Press < return > to remove shell script.
---------------------------------------------------


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Popen

Marco van de Voort
> I am trying to use the "Popen" function in Read mode.
> I first tried the example (---Write---) from the manual (see beneath).
> The POpen gives an error 25 (Not a typewriter) but it looks like it works.
> I try to do almost the same (---Read---)to read the output, but I cannot get this to read
> anything. As far as I can tell, the script is not executed.
> Dos anybody have a hint.

I don't have time to test your program atm, but I can see directly that the
program is wrong. You are only allowed to check errno if popen returns -1
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Popen

Carsten Bager
> > I am trying to use the "Popen" function in Read mode.
> > I first tried the example (---Write---) from the manual (see beneath).
> > The POpen gives an error 25 (Not a typewriter) but it looks like it works.
> > I try to do almost the same (---Read---)to read the output, but I cannot get this to read
> > anything. As far as I can tell, the script is not executed.
> > Dos anybody have a hint.
>
> I don't have time to test your program atm, but I can see directly that the
> program is wrong. You are only allowed to check errno if popen returns -1

The FPC manual (REFERENCE FOR UNIT ´UNIX´) 32.3.20 dos not
indicate this, (The code is a direct copy from the manual).

I have corrected it, but it did not change anything.

Carsten

Med venlig hilsen
Carsten Bager

BEAS A/S
Brørupvænget 10
DK-7650 Bøvlingbjerg
Tlf. : +45 9788 5222 Fax : +45 9788 5434
www.beas.dk


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal