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

Dirk Ziegemeyer dirk at ziegemeyer.de
Tue Mar 17 20:40:45 AEDT 2020


Hi Peter,
Hi Volker,

thank you for your help. I will try that.

Dirk


> Am 16.03.2020 um 16:27 schrieb Volker Wysk <post at volker-wysk.de>:
> 
> Am Montag, den 16.03.2020, 14:50 +0100 schrieb Volker Wysk:
>> 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. 
> 
> I've forgotten: You also need to set the pipe to close-on-exec.
> 
> Bye
> Volker



More information about the users mailing list