Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2001-09

Filter

18 messages · 11 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Andreas K. Foerster

Hello,

how can I start an external program and redirect its standard output so that I can use it from within my program.

I want to write a frontend.

Tschuess Andreas

Reply by email

Jonas Maebe
to Andreas K. Foerster
Andreas K. Foerster wrote
how can I start an external program and redirect its standard output so that I can use it from within my program.

I don't know under which OS you are working, but for Linux have a look at the documentation for POpen() (it's part of the Linux unit)

Jonas

Reply by email

Michael Van Canneyt
to Andreas K. Foerster: how can I start an external program and redirect its standard output so that I can use it…
Andreas K. Foerster wrote
Hello, how can I start an external program and redirect its standard output so that I can use it from within my program.

On Linux, you can use the POpen() call. This will run a program and redirect the output to a standard pascal file.

On windows this is more hard, but can be done as well (see the TPRocess object in the FCL).

On DOS it is impossible.

Michael.

Reply by email

Jonas Maebe
to Michael Van Canneyt
Michael Van Canneyt wrote
On DOS it is impossible.

Nbever say something is impossible :) As long as the program doesn't write directly to the screen and is a 16bit program, you can catch its output by hooking (real mode) interrupt 29 (I don't know whether it would also work for fpc programs)

Jonas

Reply by email

Cabuzel Thierry
Show quoted text 8 lines
From: Jonas Maebe On vrijdag, september 21, 2001, at 01:57 , Michael Van Canneyt wrote: > On DOS it is impossible. output by hooking (real mode) interrupt 29 (I don't know whether it would also work for fpc programs)

Or Interrupt 0x21 :)

Reply by email

Jonas Maebe
to Cabuzel Thierry
Cabuzel Thierry wrote 6 lines
>> On DOS it is impossible. > > output by hooking (real mode) interrupt 29 (I don't know whether it > would also work for fpc programs) Or Interrupt 0x21 :)

But that one does its screen writing by delegating to interrupt 0x29, so catching that one is enough.

Jonas

Reply by email

Rich Pasco
to Michael Van Canneyt: On Linux, you can use the POpen() call. This will run a program and redirect the output…
Andreas K. Foerster wrote
how can I start an external program and redirect its standard output so that I can use it from within my program.
Michael Van Canneyt wrote
On DOS it is impossible.

Not so. A colleague and I wrote a program in Borland Pascal 7.01 for DOS that does exactly that. It uses the DOS functions $45 and $46 to duplicate a file handle...

- Rich

Reply by email

Andreas K. Foerster
to Michael Van Canneyt: On Linux, you can use the POpen() call. This will run a program and redirect the output…
Michael Van Canneyt wrote 5 lines
> how can I start an external program and redirect its standard output > so that I can use it from within my program. On windows this is more hard, but can be done as well (see the TPRocess object in the FCL).

Okay, I'll have a for now it seems to be more easy to make 2 programs and combine the 3 progs with a batch file. :-(

Tschuess Andreas

Reply by email

Lee, John
did I miss something here...if it's windoze then

run external program... eg jim > tmp.tmp to redirect o/p

open tmp.tmp for i/p eg in reset (with filemode=$40 in windoze) so you can read it whilst jim is writing it
(if open fails because tmp.tmp is locked, slweep for 100 msec & retry)

err that's it...

Suppose its same idea for lunix + it's variants...

(I must have missed something!)...

J

Earlier message 18 lines
-----Original Message----- From: Andreas K. Foerster Sent: Friday, September 21, 2001 11:03 To: Subject: [fpc-pascal]Filter Hello, how can I start an external program and redirect its standard output so that I can use it from within my program. I want to write a frontend. -- Tschuess Andreas

Reply by email

Andreas K. Foerster
to Lee, John
John wrote
did I miss something here...if it's windoze then run external program... eg jim > tmp.tmp to redirect o/p

And how do you redirect it from within Pascal?

Show quoted text 5 lines
open tmp.tmp for i/p eg in reset (with filemode=$40 in windoze) so you can read it whilst jim is writing it (if open fails because tmp.tmp is locked, slweep for 100 msec & retry) err that's it...

Okay, but creating a temporary file is a little primitive, isn't it?

Suppose its same idea for lunix + it's variants...

In Linux it's more efficient with Popen.
With this you can redirect it directly to the program.

That's exactly what I was looking for... but for Windows.

Tschuess Andreas

Reply by email

Gabor DEAK JAHN
to Andreas K. Foerster

At 9/22/01 08:14 AM, you wrote:

Andreas,

That's exactly what I was looking for... but for Windows.

If it's you who is writing both programs, it would be much easier to use some standard means of interprocess communication, named pipes for instance. Your external program calls CreateNamedPipe, the main program calls CallNamedPipe, both specifying the same pipe name (it is in the format "\\.\pipe\anynameyoulike". Both receive a handle that they can use for writing and reading, respectively. The usual way to write to or read from handles is WriteFile and ReadFile but you can use SetStdHandle to reassign your standard input and output to the pipe handles and use simple 'read' and 'write' instructions instead.

Regards,

   Gabor DEAK JAHN

------------------------------------------------------------------- Gabor DEAK JAHN -- Budapest, Hungary.
WWW: www.tramontana.co.hu
E-mail:

Reply by email

Thomas Schatzl
to Andreas K. Foerster: And how do you redirect it from within Pascal? Okay, but creating a temporary file is a…

From: "Andreas K. Foerster"
Subject: Re: [fpc-pascal]Filter

Show quoted text 7 lines
On Fri, Sep 21, 2001 at 06:56:03PM +0100, Lee, John wrote: > did I miss something here...if it's windoze then > > run external program... eg jim > tmp.tmp to redirect o/p And how do you redirect it from within Pascal?

Call the external program with an appropriate commandline.

Use the CreateProcess() API call from within your program as already suggested. You may specify (windows-) file handles for the newly created process to be used as stdin, stdout and stderr. You could try to insert application defined ones for those. Or see below.

Show quoted text 7 lines
> open tmp.tmp for i/p eg in reset (with filemode=$40 in windoze) so you > can read it whilst jim is writing it > (if open fails because tmp.tmp is locked, slweep for 100 msec & retry) > > err that's it... Okay, but creating a temporary file is a little primitive, isn't it?

It works while meeting the given requirements. Isn't this most important in the first case ?

Show quoted text 6 lines
> Suppose its same idea for lunix + it's variants... In Linux it's more efficient with Popen. With this you can redirect it directly to the program. That's exactly what I was looking for... but for Windows.

CreateProcess() in conjunction with unnamed pipes can exactly duplicate the behaviour of popen(). Popen() seems to be a helper function within Linux using these techniques too, it says so in the manpages.

The general idea is (if I am not completely mistaken, from memory):

--- popen functionality
*create unnamed pipe - you get two file handles, one for reading, one for writing
*save old stdout + stdin
*set stdout to write handle
*(maybe) change read handle to be non-inheritable (to avoid inheritage of more file handles than necessary)
*create + run the child process. It automatically inherits all inheritable handles by default afair (Win32)
*(maybe) restore stdout in main program already at this point and close write handle. You don't need it anymore in the main process, the child process automatically gets a copy. This copy will be automatically closed on process exit.
*return read handle to main program
--- popen ends

*in child program just write as usual to stdout (which is now one end of the pipe)
*read data from read handle (other end of pipe) or set stdin to the read handle for more convenient access until child program finishes and no more data available.

You may use some event synchronization functions to avoid waste of cpu time. Using threads might not prove a good idea with FPC.

*close pipe handles left open
*restore standard i/o handles in main program if applicable and not done yet

See the MS docs for more information about pipes and process creation.

Regards,
Thomas

Reply by email

Unknown author
to Thomas Schatzl
Thomas Schatzl wrote
From: "Andreas K. Foerster" Subject: Re: [fpc-pascal]Filter

[cut]

Show quoted text 31 lines
The general idea is (if I am not completely mistaken, from memory): --- popen functionality *create unnamed pipe - you get two file handles, one for reading, one for writing *save old stdout + stdin *set stdout to write handle *(maybe) change read handle to be non-inheritable (to avoid inheritage of more file handles than necessary) *create + run the child process. It automatically inherits all inheritable handles by default afair (Win32) *(maybe) restore stdout in main program already at this point and close write handle. You don't need it anymore in the main process, the child process automatically gets a copy. This copy will be automatically closed on process exit. *return read handle to main program --- popen ends *in child program just write as usual to stdout (which is now one end of the pipe) *read data from read handle (other end of pipe) or set stdin to the read handle for more convenient access until child program finishes and no more data available. You may use some event synchronization functions to avoid waste of cpu time. Using threads might not prove a good idea with FPC. *close pipe handles left open *restore standard i/o handles in main program if applicable and not done yet See the MS docs for more information about pipes and process creation.

... or look at the TProcess code in the FCL, which does all this for Windows&Linux. German users may want to buy the 5th issue of Toolbox; There the complete TProcess component is presented for process control under Windows.

Michael.

Reply by email

Peter Vreman
to Andreas K. Foerster: how can I start an external program and redirect its standard output so that I can use it…
you wrote 6 lines
Hello, how can I start an external program and redirect its standard output so that I can use it from within my program. I want to write a frontend.

There is a redir unit available in the tests/ subdir of the main fpc branch. It works under dos,linux and win32. The same unit is used in the ide for redirecting the output of tools like grep.

Peter

Reply by email

Rich Pasco
to Peter Vreman
Peter Vreman wrote
There is a redir unit available in the tests/ subdir of the main fpc branch. It works under dos,linux and win32. The same unit is used in the ide for redirecting the output of tools like grep.

Where is this?

I installed the full distribution of the last general public release (1.04) in my directory c:\pp and searched for folders named "test." I found
c:\pp\source\rtl\os2\tests
c:\pp\source\fcl
c:\pp\source\fcl\db\
None of these seem to contain a redir unit.

- Rich

Reply by email

Unknown author
to Rich Pasco
Rich Pasco wrote 15 lines
Peter Vreman wrote: > There is a redir unit available in the tests/ subdir of the main fpc > branch. It works under dos,linux and win32. The same unit is used in the > ide for redirecting the output of tools like grep. Where is this? I installed the full distribution of the last general public release (1.04) in my directory c:\pp and searched for folders named "test." I found c:\pp\source\rtl\os2\tests c:\pp\source\fcl c:\pp\source\fcl\db\ None of these seem to contain a redir unit.

The tests are not distributed with FPC. They are accessible through CVS only. Try browsing the CVS tree through our web-cvs interface at http://www.freepascal.org/cgi-bin/cvsweb/fpc/tests/

Michael.

Reply by email

Rich Pasco
to Unknown author
Show quoted text 12 lines
> Peter Vreman wrote: > > > There is a redir unit available in the tests/ subdir of the main fpc > > branch. It works under dos,linux and win32. The same unit is used in the > > ide for redirecting the output of tools like grep. > > Where is this? [snip] ... The tests are not distributed with FPC. They are accessible through CVS only. Try browsing the CVS tree through our web-cvs interface at http://www.freepascal.org/cgi-bin/cvsweb/fpc/tests/

Thanks for the link to the CVS web interface.

I found the "redir" unit, version 1.6 at
http://www.freepascal.org/cgi-bin/cvsweb/fpc/tests/utils/redir.pp

- Rich

Reply by email