<!--
  - Authors:
  -   Christian Schulte <schulte@ps.uni-sb.de>
  -
  - Contributors (sgml translation):
  -   Daniel Simon <dansim@ps.uni-sb.de>
  -   Andy Walter <anwalt@ps.uni-sb.de>
  -
  - Copyright:
  -   Christian Schulte, 1998
  -
  - Last change:
  -   $Date: 1999/01/21 09:52:59 $ by $Author: schulte $
  -   $Revision: 1.6 $
  -
  - This file is part of Mozart, an implementation
  - of Oz 3
  -    http://www.mozart-oz.org
  -
  - See the file "LICENSE" or
  -    http://www.mozart-oz.org/LICENSE.html
  - for information on usage and redistribution
  - of this file, and for a DISCLAIMER OF ALL
  - WARRANTIES.
  -
  -->


<chapter id=chapter.pipe>
<title/Running Processes/
<p>
A frequent need in programming open applications is to start an
operating system process from Oz and to send information to and receive
information from the running process.
<p>
For this purpose we provide a class <<Open.pipe>>.

<section id="section.pipe.shell"><title/Example: A Shell in Oz/
<p>
Suppose, we want to start and control a Unix Bourne shell <file/sh/
(see <file/sh(n)/) by an Oz program.  We first have to start a
Unix process running the shell and then we need a connection to this
shell to send commands to it and receive its output.
<p>
This can be done with class <<Open.pipe>><index class=module/Open<AND>pipe/. 
Program <ptr to=prog.shell> shows the definition of a shell class.

<index/Shell/
<figure class=Program id=prog.shell>
<caption/A shell class./
<chunk class=anonymous><title/Shell/
class Shell from Open.pipe Open.text
   meth init
      Open.pipe,init(cmd:"sh" args:["-s"]) 
   end
   meth cmd(Cmd) 
      Open.text,putS(Cmd) 
   end
   meth show
      case Open.text,getS($) of false then
         {Browse 'Shell has died.'} {self close}
      elseof S then {Browse S}
      end
   end
end
</chunk>
</figure>
      
Creating a shell object by
<chunk class=anonymous><title/shell init/
S={New Shell init}
</chunk>
the command <file/sh/ is executed in a newly created process. The command
<file/sh/ gets the argument <file/-s/ to signal that the shell should read
from standard input. The forked process is connected by its
standard input and standard output to the created <<Shell>> object. 

<p>
By inheriting from the class <<Open.text>><index class=module/Open<AND>text/ we can
simply send text to the running <file/sh/ process by using the <<putS>>
method, and receive lines of text by using the <<getS>> method.

<p>
If we want to see the files in our home directory, we will first
navigate to it by <file/cd(1)/, and then issue the <file/ls(1)/
command:
<chunk class=anonymous><title/shell cmd/
{S cmd(cd)}
{S cmd(ls)}
</chunk>
Now we can incrementally request the names of the files by
<chunk class=anonymous><title/shell show/
{S show}
</chunk>
and they will appear in the Browser window. 
<p>
Closing the object and the shell is done simply by
<chunk class=anonymous><title/shell close/
{S close}
</chunk>
</section>


<![ %EXTRA; [
<section><title/NONE/
<p>
<chunk><title/EXAMPLES/
%%%
%%% Chapter: Running Processes
%%%


declare
<chunk.ref/Shell/

declare
<chunk.ref/shell init/

<chunk.ref/shell cmd/

<chunk.ref/shell show/

<chunk.ref/shell close/




</chunk>
]]>


</chapter>





