6.1 Example: A Shell in Oz

Suppose, we want to start and control a Unix Bourne shell sh (see 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.

This can be done with class Open.pipe. Program Program 6.1 shows the definition of a shell class.


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

Program 6.1: A shell class.


Creating a shell object by

S={New Shell init}

the command sh is executed in a newly created process. The command sh gets the argument -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.

By inheriting from the class Open.text we can simply send text to the running sh process by using the putS method, and receive lines of text by using the getS method.

If we want to see the files in our home directory, we will first navigate to it by cd(1), and then issue the ls(1) command:

{S cmd(cd)}
{S cmd(ls)}

Now we can incrementally request the names of the files by

{S show}

and they will appear in the Browser window.

Closing the object and the shell is done simply by

{S close}


Christian Schulte
Version 1.4.0 (20080702)