[mercury-users] Interacting with other processes

Fergus Henderson fjh at cs.mu.OZ.AU
Wed Dec 16 02:00:26 AEDT 1998

On 15-Dec-1998, Gregory Daniel Denehy <gdenehy at cs.monash.edu.au> wrote:
> I'm currently working on a graphical debugger for mercury using the
> existing debugger.  I am writing this in mercury using the tcl/tk
> interface.  The problem I am faced with is that I can't find a means to
> call the debugger from within my program that uses mercury input/output
> streams to communicate with the debugger.  I am aware that it may be
> possible to implement this using tcl, but I want to avoid this if at
> all possible.  Can this be done?

Doing this is inherently system-dependent -- it won't work on
single-tasking operating systems, for example.  But if the OS supports
multi-tasking and an appropriate method of IPC, then yes, it can be done.

On Unix systems, one simple way to do it would be to create a couple of
named pipes, e.g. by using `io__call_system_command("mkfifo ...")'
or by using `pragma c_code' to interface directly to the mknod()
system call.  Then you can use `io__call_system_command' to invoke
the debugger with its input and output redirected to the named pipes,
and you can use `io__open_input' and `io__open_output' to open
the named pipes as Mercury files.

Another approach -- this one is a bit more complicated and low level --
would be to use a couple of calls to the pipe() function to create an
pair of unnamed pipes, and then fork(), dup2() and exec() to invoke the
debugger with its input and output redirected to those pipes.  The fds
(file descriptors) returned by pipe() can be converted into standard
C's FILE pointers using fdopen().  Unfortunately as yet there is no
standard Posix binding for Mercury, so you would need to use `pragma
c_code' and/or `pragma import' to interface to these system calls.  But
the details are pretty similar no matter what language you use; for
more details about this sort of thing, see e.g. the book "Advanced Unix
Programming" (did I get that title right?) by Stevens.

One remaining issue with this approach is that there is not yet any
officially documented way of converting the resulting FILE pointers
into Mercury streams.  However, this is actually quite easy to do in
the current implementation: Mercury streams are represented as
pointers to the C type `MercuryFile' defined in the file
runtime/mercury_library_types.h, which is just a struct containing a
FILE pointer and a line number.  For an example of how to convert a
FILE pointer to a Mercury file, see the mercury_open_file() function
defined in library/io.m. 

To make that last issue easier to solve, and more importantly to make
the solution less implementation-dependent, we probably ought to define
and document some C functions and/or Mercury procedures for converting
between C files and Mercury files (in both directions). 
Volunteers for this task would be very welcome. ;-)

Fergus Henderson <fjh at cs.mu.oz.au>  |  "Binaries may die
WWW: <http://www.cs.mu.oz.au/~fjh>  |   but source code lives forever"
PGP: finger fjh at        |     -- leaked Microsoft memo.

More information about the users mailing list