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.

Mailing lists / fpc-pascal / 2001-09
18 messages · 11 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
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.
Andreas K. Foerster wrotehow 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
how can I start an external program and redirect its standard output so that I can use it…
have a look at functions such as AssignStream or popen (in the Linux unit), or the FCL Process unit...
- Sebastian
how can I start an external program and redirect its standard output so that I can use it…
Andreas K. Foerster wroteHello, 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.
Michael Van Canneyt wroteOn 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
Or Interrupt 0x21 :)
But that one does its screen writing by delegating to interrupt 0x29, so catching that one is enough.
Jonas
On Linux, you can use the POpen() call. This will run a program and redirect the output…
Andreas K. Foerster wrotehow can I start an external program and redirect its standard output so that I can use it from within my program.
Michael Van Canneyt wroteOn 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
On Linux, you can use the POpen() call. This will run a program and redirect the output…
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. :-(
did I miss something here...if it's windoze thenrun 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
John wrotedid 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?
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.
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:
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
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.
It works while meeting the given requirements. Isn't this most important in the first case ?
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
Thomas Schatzl wroteFrom: "Andreas K. Foerster" Subject: Re: [fpc-pascal]Filter
[cut]
... 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.
how can I start an external program and redirect its standard output so that I can use it…
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
Peter Vreman wroteThere 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
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.
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