A frequent need in programming open applications is to start a Unix process within Oz and to send information to and receive information from the running process. For this purpose we provide a class Open.pipe.
Suppose, we want to start and control an ordinary Unix Bourne shell
sh (see sh(1)
) 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 the class Open.pipe .
Program contains the creation of a shell object.
create Shell from Open.pipe Open.text with init 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
Upon application of init, the Unix command sh is executed in a newly created Unix 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. The class Open.pipe has all methods of class Open.unixSocket . Thus 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:
{Shell [cmd(cd) cmd(ls)]}Now we can incrementally request the names of the files by
{Shell show}and they will appear in the Browser window. Closing the object and the shell is done simply by
{Shell close}In a very similar fashion the base of our interface to Tcl/Tk [5] has been designed, for more information see [3].
The class Open.pipe inherits from the class Open.unixSocket , thus all features and methods of this class are available. Additionally, the type of the socket must be stream.
init(cmd: +CmdV args: +ArgsVs <= nil pid: ?PidI <= _)
Forks a Unix process with process identification PidI executing the command CmdV with arguments ArgsVs. The environment of the forked process is inherited from the process which runs the Oz emulator (see [4]). The standard input of the forked process is connected to sending and writing data, the standard output and standard error to reading and receiving data. See also execv(3) , fork(2) .
close
Closes the object. If the started process is still runnning, it is killed by sending the SIGTERM signal. However, note that the inverse direction is not supported, which means the object is not automatically closed if the process terminates. See also wait(2) and kill(1) .