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

Volker Wysk post at volker-wysk.de
Tue Mar 17 21:34:02 AEDT 2020


Am Dienstag, den 17.03.2020, 10:40 +0100 schrieb Dirk Ziegemeyer:
> Hi Peter,
> Hi Volker,
> 
> thank you for your help. I will try that.

If you happen to know Haskell, you might want to look at 
www.volker-wysk.de/hsshellscript.

And this sets a file descriptor to close-on-exec:

  /* Set a file descriptor to "close on exec" mode. Returns the old 
  flags. */
  int c_close_on_exec(int fd)
  {
    int old_flags;
    old_flags = fcntl(fd, F_GETFL);
    fcntl(fd, F_SETFL, old_flags | FD_CLOEXEC);
    return old_flags;
  }

Good luck,
Volker

> > 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