Paul Massey's changes to io.m

Fergus Henderson fjh at cs.mu.OZ.AU
Mon Feb 9 23:53:03 AEDT 1998


Hi,

Paul Massey sent me his socket interface today.  It included some changes
to library/io.m to support bidirectional streams and to support
creating a stream from an fd or getting the fd for a stream.
Whether or not we want to use Paul's socket interface, we're going to
need to include his changes or something along those lines in order
to do network programming.  So I'm posting them here for review.
(I'll post my own comments as a followup to this message.)

Index: io.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/io.m,v
retrieving revision 1.141
diff -u -u -r1.141 io.m
--- io.m	1997/10/08 13:27:47	1.141
+++ io.m	1998/02/09 11:44:27
@@ -16,6 +16,10 @@
 % of the world argument is properly single-threaded, and will also check
 % to ensure that you don't attempt to backtrack over any I/O.
 %
+% Modifications: P.A.Massey (MC Jul/23/997)
+%                io__bi_stream type defined and one or two predicates make
+%                externally visible.
+%
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
 
@@ -46,6 +50,22 @@
 
 :- type io__output_stream.
 
+:- type io__stream.				% Paul
+
+:- type io__bi_stream.				% Paul
+
+	% Moved here (Paul) because equivalence is needed
+	% for sockets to use I/O routines without stream
+	% type conversions.
+
+:- type io__input_stream ==	io__stream.
+
+:- type io__output_stream ==	io__stream.
+
+:- type io__bi_stream ==	io__stream.   	% Paul.
+
+:- type io__binary_stream ==	io__stream.
+
 	% Opaque handles for binary I/O streams.
 
 :- type io__binary_input_stream		==	io__binary_stream.
@@ -883,10 +903,30 @@
 
 :- pred io__error_message(io__error, string).
 :- mode io__error_message(in, out) is det.
+:- mode io__error_message(out, in) is det. 	% Paul (sockets needed it)
 %	io__error_message(ErrorCode, ErrorMessage).
 %		Look up the error message corresponding to a particular error
 %		code.
 
+% moved here by Paul.
+
+:- pred io__delete_stream_name(io__stream, io__state, io__state).
+:- mode io__delete_stream_name(in, di, uo) is det.
+
+:- pred io__insert_stream_name(io__stream, string, io__state, io__state).
+:- mode io__insert_stream_name(in, in, di, uo) is det.
+
+:- pred io__stream_name(io__stream, string, io__state, io__state).
+:- mode io__stream_name(in, out, di, uo) is det.
+
+% added by paul.
+
+:- pred io__fd_to_stream(int, io__stream, io__state, io__state).
+:- mode io__fd_to_stream(in, out, di, uo) is det.
+
+:- pred io__stream_to_fd(io__stream, int, io__state, io__state).
+:- mode io__stream_to_fd(in, out, di, uo) is det.
+
 %-----------------------------------------------------------------------------%
 :- implementation.
 
@@ -962,11 +1002,13 @@
 :- type io__stream_names ==	map(io__stream, string).
 :- type io__stream_putback ==	map(io__stream, list(char)).
 
+/* moved to interface by paul.
 :- type io__input_stream ==	io__stream.
+
 :- type io__output_stream ==	io__stream.
 
 :- type io__binary_stream ==	io__stream.
-
+ */
 :- type io__stream == c_pointer.
 
 /*
@@ -1797,8 +1839,10 @@
 io__binary_output_stream_name(Stream, Name) -->
 	io__stream_name(Stream, Name).
 
+/*			% made visible by paul.
 :- pred io__stream_name(io__stream, string, io__state, io__state).
 :- mode io__stream_name(in, out, di, uo) is det.
+*/
 
 	% XXX major design flaw with regard to unique modes
 	% means that this is very inefficient.
@@ -1829,16 +1873,20 @@
 	update_io(IO0, IO);
 ").
 
+/*		into interface by Paul.
 :- pred io__delete_stream_name(io__stream, io__state, io__state).
 :- mode io__delete_stream_name(in, di, uo) is det.
+*/
 
 io__delete_stream_name(Stream) -->
 	io__get_stream_names(StreamNames0),
 	{ map__delete(StreamNames0, Stream, StreamNames) },
 	io__set_stream_names(StreamNames).
 
+/* into interface by paul.
 :- pred io__insert_stream_name(io__stream, string, io__state, io__state).
 :- mode io__insert_stream_name(in, in, di, uo) is det.
+ */
 
 io__insert_stream_name(Stream, Name) -->
 	io__get_stream_names(StreamNames0),
@@ -2031,7 +2079,6 @@
 #define initial_io_state()		0	/* some random number */
 #define update_io(r_src, r_dest)	((r_dest) = (r_src))
 #define final_io_state(r)		((void)0)
-
 void 		mercury_init_io(void);
 MercuryFile*	mercury_open(const char *filename, const char *type);
 int		mercury_output_error(MercuryFile* mf);
@@ -2745,5 +2792,31 @@
 	update_io(IO0, IO);
 }").
 
+/*---------------------------------------------------------------------------*/
+/* converts an integer into something which will be acceptable by the 
+ * other IO predicates */
+
+:- pragma(c_code, io__fd_to_stream(Fd::in,IOStream::out,IO0::di,IO::uo), "{
+	MercuryFile *mf;
+	FILE *f;
+	f = fdopen(Fd, ""w+"");
+	if (!f) {
+		IOStream = NULL;
+	} else {
+		mf = make(MercuryFile);
+		mf->file = f;
+		mf->line_number = 1;
+		IOStream = mf;
+	};
+	update_io(IO0, IO);
+}").
+
+
+:- pragma(c_code, io__stream_to_fd(IOStream::in,Fd::out,IO0::di,IO::uo), "{
+	MercuryFile *mf;
+	mf = (MercuryFile *) IOStream;
+	Fd = fileno(mf->file);
+	update_io(IO0, IO);
+}").
 
 /*---------------------------------------------------------------------------*/

-- 
Fergus Henderson <fjh at cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3         |     -- the last words of T. S. Garp.

--3MwIy2ne0vdjdPXF
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=mail2



More information about the users mailing list