[m-users.] Pass stream or string to standard input of shell command

Volker Wysk post at volker-wysk.de
Tue Mar 17 00:50:27 AEDT 2020


Am Montag, den 16.03.2020, 13:56 +0100 schrieb Dirk Ziegemeyer:
> Hello,
> 
> is it possible to pass an io.text_output_stream or string to the
> standard input of an operating system shell command called from
> within Mercury?
> 
> Instead of first writing output to a large text file on disk and then
> compressing it by calling gzip with
> 
> io.call_system("gzip FILE", Result, !IO)
> 
> I would rather pass the uncompressed stream or string directly to the
> standard input of gzip.

I don't know if this is already supported in the standard library. But
you could do it with posix calls. You'd have to create a pipe, using
pipe(), then do fork(). Then you'd close the read end in the parent
process and the write end in the child process. You'd do dup() to copy
the read end to the standard input of the child process. Then you'd do
exec() in order to start the gzip program in the child process.

Then you have a pipe to the gzip program in the parent process (your
mercury program). In order to use it, you would have to attach a stream
to that pipe (to its file descriptor). I don't know if that's possible.

I've checked: The "posix" extra library provides predicates for the
mentioned posix library calls. It's odd, however, that the close()
predicate is in the posix.open module. 

Happy hacking,
Volker



More information about the users mailing list