Re: [m-users.] How to get the file descriptor of a stream

Volker Wysk <[email protected]>
Newsgroups gmane.comp.lang.mercury.general
Message-ID <[email protected]>
Am Donnerstag, dem 20.07.2023 um 17:01 +1000 schrieb Julien Fischer:
> Hi Volker,

Hi Julien!

> On Thu, 20 Jul 2023, Volker Wysk wrote:
> 
> > I would like to have a predicate which returns the file descriptor, which is
> > used to implement a stream. Such as the fileno(3) C library function. It's
> > for querying the terminal width, of streams that can be connected to a
> > terminal. I want to print (error and normal) messages which are formatted
> > for the terminal's width.
> > 
> > There's stdout_stream and stderr_stream and you can assume that they have
> > the file descriptors 1 and 2. But this isn't foolproof and other streams
> > might be connected to a terminal as well (or isn't it?).
> > 
> > It might, for instance, theoretically happen that stdout_stream is closed
> > and the file descriptor 1 gets reused later. Then we could end up with a fd
> > 1 which doesn't belong to stdout_stream.
> > 
> > I've searched the io and stream libraries, but couldn't find such a
> > predicate.
> 
> For the C backends, it's straightfoward to get the file descriptor for a
> Mercury file stream. Here's how you do it for text streams (binary
> file streams would be very similar).
> 
> :- pred get_text_input_stream_fd(io.text_input_stream::in, int::out,
>       io::di, io::uo) is det.
> 
> :- pragma foreign_proc("C",
>      get_text_input_stream_fd(Stream::in, FD::out, _IO0::di, _IO::uo),
>      [will_not_call_mercury, promise_pure, tabled_for_io],
> "
>      FD = fileno(MR_file(*MR_unwrap_input_stream(Stream)));
> ").
> 
> :- pred get_text_output_stream_fd(io.text_output_stream::in, int::out,
>        io::di, io::uo) is det.
> 
> :- pragma foreign_proc("C",
>      get_text_output_stream_fd(Stream::in, FD::out, _IO0::di, _IO::uo),
>      [will_not_call_mercury, promise_pure, tabled_for_io],
> "
>      FD = fileno(MR_file(*MR_unwrap_output_stream(Stream)));
> ").
> 
> Julien.

Thanks! The "MR_file(*MR_unwrap_input/output_stream(Stream))" part was the
missing piece.

MR_file and MR_unwrap_input/output_stream don't occur in the Language
Reference Manual or the Library Reference Manual. But they're mentioned in a
comment in io.m. 

So the documentation isn't completely complete and you have to resort to
reading the source code, sometimes.

Regards,
Volker

_______________________________________________
users mailing list
[email protected]
https://lists.mercurylang.org/listinfo/users
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEE6QXGh82Ov3+2nrxp+K4ydFOsHoUFAmS49A0ACgkQ+K4ydFOs
HoVCgA/9HS6ry1QFpuVGVxI75Qj5KJ1QnxhOlcrXfMQlG8u51rgLzKL2e4YBrUU6
VjMesf8Wz/AGPhZFPiqPb2INi9qm/H+yiS+lvvL69mCEpaXvyUa0eLrH/HnMehGI
Wq1E6e69BEk8rl1iE6Y2JseS50nstTPYaqyemnXgyq7OwlUFcAhOdgiraA7tbFd6
+xgSQ+VwfZnf0eMSLW+TH3UHFCCpYMpwViHzKMJw4eVkW4enfIFq7kTPoD3uh5Gd
Dj9pFzYJ5wvu5BapgZ6xrzeufS20JYQ1C3gjeerQX1SPOABxB4IQGOuee+3jeGaL
vp0Rp3d0bSndsTRf1fnIXCfXpY64SQ/1wdgkxbBTjUQoXxB/czy7IngG0Oz5yfIj
RRzat/RoyVt/OU3aml0Fr5PctXYGrK/HhSlhsNhsZrZGQWlz1zA5EktRu30GH2Sn
k2+amICL1Jpn9kXA6k3CtDI5x02QHtwyDLSN7+35xMrNi7gvdP6ynMJii2Jvof1F
3KJqX3JDEHUa9oin6htfiqBWsqXuBjiJO1vid2D+qN4a0fw+OMo5WK3pf1QXUS60
kEDOdxUyYX0+DZMZ8/ZUpRTRlT7vh6sYP8uetfLO1PByM5B0flWis5Dwr3bsETqf
DwpfGAkHMu/n+HeYKy9fGXtx0e2K9OkFYSVjVDKAu15gMOQHLio=
=To23
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.