[comp.os.msdos.programmer] Spawning processes in DOS

dougs@claude1.rchland.ibm.com (J Doug Smith) (04/03/91)

____

I'm trying to write an application that will run another program and get that program's output so it can be displayed to the user (much like an editor runs a compiler and sends the compiler's output to the user).  Does anyone have any Ideas on how to accomplish this?  Execing another programs certainly is no problem, I just don't see how to get the output from that program.



Doug Smith
IBM Rochester, Dept. 43K
dougs@rchland.iinus1.ibm.com

dj@balrog.ctron.com (DJ Delorie) (04/04/91)

In article <1991Apr3.133226.22304@ibmpa.awdpa.ibm.com> dougs@claude1.rchland.ibm.com (J Doug Smith) writes:
> I'm trying to write an application that will run another program and
> get that program's output so it can be displayed to the user (much
> like an editor runs a compiler and sends the compiler's output to the
> user).  Does anyone have any Ideas on how to accomplish this?  Execing
> another programs certainly is no problem, I just don't see how to get
> the output from that program.

The only safe way (ie: compatible) is:

  . . .
  rc = system("cc -c foo.c > /tmp/blat 2>&1");
  rf = fopen("/tmp/blat", "r");
  . . .

but watch out for the 2>&1 on DOS :-)

DJ
dj@ctron.com

profesor@wpi.WPI.EDU (Matthew E Cross) (04/04/91)

In article <1991Apr3.133226.22304@ibmpa.awdpa.ibm.com> dougs@claude1.rchland.ibm.com (J Doug Smith) writes:
->
->____
->
->I'm trying to write an application that will run another program and
->get that program's output so it can be displayed to the user (much
->like an editor runs a compiler and sends the compiler's output to the
->user).  Does anyone have any Ideas on how to accomplish this?  Execing
->another programs certainly is no problem, I just don't see how to get
->the output from that program.

Well, one cheap wayto do it is to redirect the output to a file, then
read in the file.  This won't do if you want to do some interactive
stuff, though.  Does your compiler/library support popen()?
-- 
+-------------------------------------+---------------------------------------+
| "The letter U has a lot of uses ... |       profesor@wpi.wpi.edu            |
|  I like to play it like a guitar!"  +---------------------------------------+
|          -Sesame Street             |       Make love, not War..            |

brandis@inf.ethz.ch (Marc Brandis) (04/04/91)

In article <1991Apr3.133226.22304@ibmpa.awdpa.ibm.com> dougs@claude1.rchland.ibm.com (J Doug Smith) writes:
>I'm trying to write an application that will run another program and get that
>program's output so it can be displayed to the user (much like an editor runs
>a compiler and sends the compiler's output to the user).  Does anyone have any
>Ideas on how to accomplish this?  
>Execing another programs certainly is no problem, I just don't see how to 
>get the output from that program.


Redirect your stdout handle to a temporary file. The process your are
exec'ing inherits the handles from the parent process, which is your program.
After the program terminates, restore your stdout handle and read the output
from the temporary file.

The steps to redirect are about as follows (I never did it myself, but this is
the way it should work):

	- duplicate stdout handle to temp (required for later restore)
	- close stdout
	- open temporary file, it will get the stdout handle 1 assigned
	- exec the program
	- close temporary file (which is stdout)
	- duplicate temp handle to stdout
	- close temp handle
	- open temporary file in order to read the programs output


Marc-Michael Brandis
Computer Systems Laboratory, ETH-Zentrum (Swiss Federal Institute of Technology)
CH-8092 Zurich, Switzerland
email: brandis@inf.ethz.ch


------------------------------ End of forwarded message 1

jdb@reef.cis.ufl.edu (Brian K. W. Hook) (04/04/91)

In article <1374@bragi.ctron.com> dj@balrog.ctron.com (DJ Delorie) writes:
|>In article <1991Apr3.133226.22304@ibmpa.awdpa.ibm.com> dougs@claude1.rchland.ibm.com (J Doug Smith) writes:
|>> I'm trying to write an application that will run another program and
|>> get that program's output so it can be displayed to the user (much
|>> like an editor runs a compiler and sends the compiler's output to the
|>> user).  Does anyone have any Ideas on how to accomplish this?  Execing
|>> another programs certainly is no problem, I just don't see how to get
|>> the output from that program.
|>
|>The only safe way (ie: compatible) is:
|>
|>  . . .
|>  rc = system("cc -c foo.c > /tmp/blat 2>&1");
|>  rf = fopen("/tmp/blat", "r");
|>  . . .
|>
|>but watch out for the 2>&1 on DOS :-)
|>
|>DJ
|>dj@ctron.com

Unfortunately, this assumes that you can trap the program's output using
standard redirection.  What about programs that write direct to video
memory?  Most of my programs do, and when I redirect them I get blank
files.  Same with redirecting input...I don't use the standard DOS int to
get a character, I use bioskey() under Turbo C.

Any way around this?

Brian

dj@bragi.ctron.com (DJ Delorie) (04/04/91)

In article <27813@uflorida.cis.ufl.EDU> jdb@reef.cis.ufl.edu (Brian K. W. Hook) writes:
>Unfortunately, this assumes that you can trap the program's output using
>standard redirection.  What about programs that write direct to video
>memory?  Most of my programs do, and when I redirect them I get blank
>files.  Same with redirecting input...I don't use the standard DOS int to
>get a character, I use bioskey() under Turbo C.

This is basically like asking the question, "How do I redirect the output
of my video game to a file?"  Unless the program expects to have it's
output sent somewhere, You'd need the equivalent of Windows or VP/ix to
trap the output.

DJ
dj@ctron.com

sdawalt@valhalla.wright.edu (Shane Dawalt) (04/05/91)

From article <1991Apr3.133226.22304@ibmpa.awdpa.ibm.com>, by dougs@claude1.rchland.ibm.com (J Doug Smith):
> 
> ____
> 
> I'm trying to write an application that will run another program and get
> that program's output so it can be displayed to the user (much like an
> editor runs a compiler and sends the compiler's output to the user).  Does
> anyone have any Ideas on how to accomplish this?  Execing another programs
> certainly is no problem, I just don't see how to get the output from that
> program.

  One idea springs to mind.  Of course, it requires that the exec'd program
BIOS calls to write to the screen (none of this direct write stuff).

  It is known that a child inherits, at the very least, the parent's file
handles.  (If you tell the EXEC function, the child will also inherit the
parent's environment.)  If you redirect stderr and/or stdout of the parent then
spawn the child, the child's stderr/stdout will also be redirected.  You can
redirect these output streams to a temporary file.  After exec terminates,
clean up the parents output streams by "un-"redirecting them then process the
temporary file as you wish.

  Shane();

--------------------------------------------------------------------------
From the keyboard of:			     email: sdawalt@cs.wright.edu
	Shane A. Dawalt
--------------------------------------------------------------------------

dcc@hpopd.pwd.hp.com (Daniel Creswell) (04/05/91)

Why don't you add a re-direction to then end of a system() command and then
you can look in the file afterwards. Don't think you can do it with pipes..

Cheers, Dan.