Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > bd5c3d824c3db63ffd9226c15941e6ad > files > 1517

mozart-1.4.0-1mdv2010.0.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>6.1 Example: A Shell in Oz</TITLE><LINK href="ozdoc.css" rel="stylesheet" type="text/css"></HEAD><BODY><TABLE align="center" border="0" cellpadding="6" cellspacing="6" class="nav"><TR bgcolor="#DDDDDD"><TD><A href="node16.html">- Up -</A></TD></TR></TABLE><DIV id="section.pipe.shell"><H2><A name="section.pipe.shell">6.1 Example: A Shell in Oz</A></H2><P>Suppose, we want to start and control a Unix Bourne shell <CODE>sh</CODE> (see <CODE>sh(n)</CODE>) 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><P>This can be done with class <CODE>Open<SPAN class="keyword">.</SPAN>pipe</CODE><A name="label152"></A>. Program <A href="node17.html#prog.shell">Program&nbsp;6.1</A> shows the definition of a shell class. <A name="label153"></A> </P><DIV class="program" id="prog.shell"><HR><P><A name="prog.shell"></A></P></DIV><DL class="anonymous"><DD class="code"><CODE><SPAN class="keyword">class</SPAN>&nbsp;<SPAN class="type">Shell</SPAN>&nbsp;<SPAN class="keyword">from</SPAN><SPAN class="type">&nbsp;Open.pipe&nbsp;Open.text</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;<SPAN class="keyword">meth</SPAN>&nbsp;<SPAN class="functionname">init</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Open<SPAN class="keyword">.</SPAN>pipe<SPAN class="keyword">,</SPAN>init(cmd:<SPAN class="string">&quot;sh&quot;</SPAN>&nbsp;args:[<SPAN class="string">&quot;-s&quot;</SPAN>])&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;<SPAN class="keyword">end</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;<SPAN class="keyword">meth</SPAN>&nbsp;<SPAN class="functionname">cmd</SPAN>(Cmd)&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Open<SPAN class="keyword">.</SPAN>text<SPAN class="keyword">,</SPAN>putS(Cmd)&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;<SPAN class="keyword">end</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;<SPAN class="keyword">meth</SPAN>&nbsp;<SPAN class="functionname">show</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">case</SPAN>&nbsp;Open<SPAN class="keyword">.</SPAN>text<SPAN class="keyword">,</SPAN>getS($)&nbsp;<SPAN class="keyword">of</SPAN>&nbsp;<SPAN class="keyword">false</SPAN>&nbsp;<SPAN class="keyword">then</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{Browse&nbsp;<SPAN class="string">'Shell&nbsp;has&nbsp;died.'</SPAN>}&nbsp;{<SPAN class="keyword">self</SPAN>&nbsp;close}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">elseof</SPAN>&nbsp;S&nbsp;<SPAN class="keyword">then</SPAN>&nbsp;{Browse&nbsp;S}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">end</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;<SPAN class="keyword">end</SPAN>&nbsp;<BR><SPAN class="keyword">end</SPAN></CODE></DD></DL><DIV class="program"><P class="caption"><STRONG>Program&nbsp;6.1:</STRONG> A shell class.</P><HR></DIV><P> Creating a shell object by </P><DL class="anonymous"><DD class="code"><CODE>S={New&nbsp;Shell&nbsp;init}</CODE></DD></DL><P> the command <CODE>sh</CODE> is executed in a newly created process. The command <CODE>sh</CODE> gets the argument <CODE>-s</CODE> 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 <CODE>Shell</CODE> object. </P><P>By inheriting from the class <CODE>Open<SPAN class="keyword">.</SPAN>text</CODE><A name="label155"></A> we can simply send text to the running <CODE>sh</CODE> process by using the <CODE>putS</CODE> method, and receive lines of text by using the <CODE>getS</CODE> method. </P><P>If we want to see the files in our home directory, we will first navigate to it by <CODE>cd(1)</CODE>, and then issue the <CODE>ls(1)</CODE> command: </P><DL class="anonymous"><DD class="code"><CODE>{S&nbsp;cmd(cd)}<BR>{S&nbsp;cmd(ls)}</CODE></DD></DL><P> Now we can incrementally request the names of the files by </P><DL class="anonymous"><DD class="code"><CODE>{S&nbsp;show}</CODE></DD></DL><P> and they will appear in the Browser window. </P><P>Closing the object and the shell is done simply by </P><DL class="anonymous"><DD class="code"><CODE>{S&nbsp;close}</CODE></DD></DL><P></P></DIV><TABLE align="center" border="0" cellpadding="6" cellspacing="6" class="nav"><TR bgcolor="#DDDDDD"><TD><A href="node16.html">- Up -</A></TD></TR></TABLE><HR><ADDRESS><A href="http://www.ps.uni-sb.de/~schulte/">Christian&nbsp;Schulte</A><BR><SPAN class="version">Version 1.4.0 (20090610)</SPAN></ADDRESS></BODY></HTML>