[m-dev.] for review: add the POSIX stuff to the extras
Thomas Conway
conway at cs.mu.OZ.AU
Mon Oct 18 10:50:00 AEST 1999
On Sat, Oct 16, 1999 at 06:16:15AM EST, Fergus Henderson wrote:
> Hi Tom,
>
> Here's some review comments on the extras/posix stuff.
>
> > Index: Mmake
Fixed.
>
> You should use `Mmakefile' rather than `Mmake', to avoid
> problems on case-insensitive file systems where "." is first
> in the PATH.
>
> > +MLLIBS = posix_workarounds.o
>
> You should use MLOBJS and MLPICOBJS rather than MLLIBS,
> so that it works if you try to build a shared library.
Fixed.
>
> > diff -N hello.m
> > +:- pred main(io:state, io:state).
> > +:- mode main(di, uo) is det.
>
> You should use `io__state' rather than `io:state',
> so that it works when `:' becomes the type qualifier.
>
> > +:- import_module posix, posix:open, posix:write, text.
>
> Likewise you should use `posix__open' rather than `posix:open'.
Fixed this in lots of places.
> > +:- pragma c_code(whence(W::in) = (V::out),
> > + [will_not_call_mercury, thread_safe], "{
> > + static int whence_flags[] = { SEEK_SET, SEEK_CUR, SEEK_END } ;
> > + V = whence_flags[W];
> > +}").
>
> s/static/static const/
>
Fixed (here and elsewhere).
> > +:- pragma c_code(oflagval(F::in) = (V::out),
> > + [will_not_call_mercury, thread_safe], "{
> > + static int oflag_values[] = {
> > + O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_EXCL, O_NOCTTY,
> > + O_TRUNC, O_APPEND, O_NDELAY, O_SYNC };
>
> Likewise here. Also it would be better IMHO to put the "};"
> on a new line, indented the same as the "static int ..." line.
>
> > +:- pragma c_code(domain(D::in) = (V::out),
> > + [will_not_call_mercury, thread_safe], "{
> > + static int domain_values[] = {
> > + AF_UNIX, AF_INET
> > + };
>
> Likewise here.
>
> > +:- pragma c_code(type(T::in) = (V::out),
> > + [will_not_call_mercury, thread_safe], "{
> > + static int type_values[] = {
> > + SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, SOCK_SEQPACKET, SOCK_RDM
> > + };
>
> And here.
>
> > +:- pragma c_code(mkinet_addr(A::in, P::in, Ptr::out, Len::out),
> > + [will_not_call_mercury, thread_safe], "{
> > + struct sockaddr_in *ptr;
> > +
> > + incr_hp(Ptr, (1 + sizeof(struct sockaddr_in)/sizeof(Word)));
> > + ptr = (struct sockaddr_in *) Ptr;
> > +
> > + memset((void *) ptr, 0, sizeof(struct sockaddr_in));
>
> The case to (void *) here is unnecessary, and it does not aid
> readability IMHO.
Fixed.
> > + /*
> > + ** ME_words(amt) returns the number of words necessary to
> > + ** to store `amt' bytes.
> > + */
> > + #define ME_words(x) (1+(x)/sizeof(Word))
>
> That formula allocates more memory than is needed.
> The correct formula here is `(((x) + sizeof(Word) - 1) / sizeof(Word))'.
Fixed.
>
> I suggest you delete the line between those two diffent modes for index/3.
Fixed.
>
> Other comments:
> - the type `fdset' really represents a pointer, not a set,
> so IMHO it should be named `fdset_ptr'.
Done.
> - the interface to the `text' module should be documented better
Done.
> - an interface to strerror() would be very handy
Feel free.
Revised diff follows. I'll commit it now.
Thomas
--
Thomas Conway )O+ Every sword has two edges.
Mercurian <conway at cs.mu.oz.au>
extras/posix/{MMake,MMakefile}:
Rename Mmake as Mmakefile to avoid problems on antiquated platforms.
Use MLOBJS rather than MLLIBS.
extras/posix/*.m:
Use __ rather than : as the module qualifier.
Make the flag arrays static const, rather than just static.
extras/posix/text.m:
Add some doccumentation to the interface of text.m.
cvs diff: Diffing .
cvs diff: Mmake was removed, no comparison available
cvs diff: Mmakefile is a new entry, no comparison available
Index: hello.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/extras/posix/hello.m,v
retrieving revision 1.1
diff -u -r1.1 hello.m
--- hello.m 1999/10/12 00:12:21 1.1
+++ hello.m 1999/10/18 00:11:22
@@ -4,12 +4,12 @@
:- import_module io.
-:- pred main(io:state, io:state).
+:- pred main(io__state, io__state).
:- mode main(di, uo) is det.
:- implementation.
-:- import_module posix, posix:open, posix:write, text.
+:- import_module posix, posix__open, posix__write, text.
:- import_module list, string.
main -->
Index: posix.lseek.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/extras/posix/posix.lseek.m,v
retrieving revision 1.1
diff -u -r1.1 posix.lseek.m
--- posix.lseek.m 1999/10/12 00:12:21 1.1
+++ posix.lseek.m 1999/10/18 00:13:06
@@ -4,11 +4,11 @@
% Public License - see the file COPYING.LIB in the Mercury distribution.
%------------------------------------------------------------------------------%
%
-% module: posix:lseek.m
+% module: posix__lseek.m
% main author: conway at cs.mu.oz.au
%
%------------------------------------------------------------------------------%
-:- module posix:lseek.
+:- module posix__lseek.
:- interface.
@@ -18,7 +18,7 @@
; end
.
-:- pred lseek(fd, int, lseek:whence, posix:result(int), io__state, io__state).
+:- pred lseek(fd, int, lseek__whence, posix__result(int), io__state, io__state).
:- mode lseek(in, in, in, out, di, uo) is det.
%------------------------------------------------------------------------------%
@@ -58,7 +58,7 @@
:- pragma c_code(whence(W::in) = (V::out),
[will_not_call_mercury, thread_safe], "{
- static int whence_flags[] = { SEEK_SET, SEEK_CUR, SEEK_END } ;
+ static const int whence_flags[] = { SEEK_SET, SEEK_CUR, SEEK_END } ;
V = whence_flags[W];
}").
Index: posix.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/extras/posix/posix.m,v
retrieving revision 1.1
diff -u -r1.1 posix.m
--- posix.m 1999/10/12 00:12:21 1.1
+++ posix.m 1999/10/18 00:11:51
@@ -33,12 +33,12 @@
:- import_module io.
-:- include_module posix:lseek.
-:- include_module posix:open.
-:- include_module posix:read.
-:- include_module posix:select.
-:- include_module posix:socket.
-:- include_module posix:write.
+:- include_module posix__lseek.
+:- include_module posix__open.
+:- include_module posix__read.
+:- include_module posix__select.
+:- include_module posix__socket.
+:- include_module posix__write.
% Generic file descriptors.
:- type fd ---> fd(int).
@@ -90,14 +90,14 @@
; unknown(int, string) % unknown(Errno, Msg)
.
-:- type posix:result
+:- type posix__result
---> ok
- ; error(posix:error)
+ ; error(posix__error)
.
-:- type posix:result(T)
+:- type posix__result(T)
---> ok(T)
- ; error(posix:error)
+ ; error(posix__error)
.
:- type (mode) ---> mode(int).
@@ -105,7 +105,7 @@
:- type timeval
---> timeval(int, int). % time(Sec, uSec)
-:- pred errno(posix:error, io__state, io__state).
+:- pred errno(posix__error, io__state, io__state).
:- mode errno(out, di, uo) is det.
:- implementation.
@@ -130,7 +130,7 @@
IO = IO0;
}").
-:- func error(int) = posix:error.
+:- func error(int) = posix__error.
error(Num) = Res :-
(( Num = 0, Err = e2BIG
Index: posix.open.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/extras/posix/posix.open.m,v
retrieving revision 1.1
diff -u -r1.1 posix.open.m
--- posix.open.m 1999/10/12 00:12:22 1.1
+++ posix.open.m 1999/10/18 00:13:42
@@ -4,14 +4,14 @@
% Public License - see the file COPYING.LIB in the Mercury distribution.
%------------------------------------------------------------------------------%
%
-% module: posix:open.
+% module: posix__open.
% main author: conway at cs.mu.oz.au
%
% This module provides and interface to the open function and its
% relatives.
%
%------------------------------------------------------------------------------%
-:- module posix:open.
+:- module posix__open.
:- interface.
@@ -30,17 +30,17 @@
; sync
.
-:- pred open(string, list(oflag), posix:result(fd), io__state, io__state).
+:- pred open(string, list(oflag), posix__result(fd), io__state, io__state).
:- mode open(in, in, out, di, uo) is det.
-:- pred open(string, list(oflag), (mode), posix:result(fd),
+:- pred open(string, list(oflag), (mode), posix__result(fd),
io__state, io__state).
:- mode open(in, in, in, out, di, uo) is det.
-:- pred creat(string, (mode), posix:result(fd), io__state, io__state).
+:- pred creat(string, (mode), posix__result(fd), io__state, io__state).
:- mode creat(in, in, out, di, uo) is det.
-:- pred close(fd, posix:result, io__state, io__state).
+:- pred close(fd, posix__result, io__state, io__state).
:- mode close(in, out, di, uo) is det.
%------------------------------------------------------------------------------%
@@ -154,9 +154,10 @@
:- pragma c_code(oflagval(F::in) = (V::out),
[will_not_call_mercury, thread_safe], "{
- static int oflag_values[] = {
+ static const int oflag_values[] = {
O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_EXCL, O_NOCTTY,
- O_TRUNC, O_APPEND, O_NDELAY, O_SYNC };
+ O_TRUNC, O_APPEND, O_NDELAY, O_SYNC
+ };
V = oflag_values[F];
}").
Index: posix.read.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/extras/posix/posix.read.m,v
retrieving revision 1.1
diff -u -r1.1 posix.read.m
--- posix.read.m 1999/10/12 00:12:23 1.1
+++ posix.read.m 1999/10/18 00:12:00
@@ -4,17 +4,17 @@
% Public License - see the file COPYING.LIB in the Mercury distribution.
%------------------------------------------------------------------------------%
%
-% module: posix:read.m
+% module: posix__read.m
% main author: conway at cs.mu.oz.au
%
%------------------------------------------------------------------------------%
-:- module posix:read.
+:- module posix__read.
:- interface.
:- import_module text.
-:- pred read(fd, int, posix:result(int), text, text, io__state, io__state).
+:- pred read(fd, int, posix__result(int), text, text, io__state, io__state).
:- mode read(in, in, out, di, uo, di, uo) is det.
%------------------------------------------------------------------------------%
Index: posix.select.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/extras/posix/posix.select.m,v
retrieving revision 1.1
diff -u -r1.1 posix.select.m
--- posix.select.m 1999/10/12 00:12:23 1.1
+++ posix.select.m 1999/10/18 00:37:52
@@ -4,35 +4,35 @@
% Public License - see the file COPYING.LIB in the Mercury distribution.
%------------------------------------------------------------------------------%
%
-% module: posix:select.m
+% module: posix__select.m
% main author: conway at cs.mu.oz.au
%
%------------------------------------------------------------------------------%
-:- module posix:select.
+:- module posix__select.
:- interface.
:- import_module bool.
-:- type fdset.
+:- type fdset_ptr.
-:- pred select(int, fdset, fdset, fdset, timeval, posix:result(int),
+:- pred select(int, fdset_ptr, fdset_ptr, fdset_ptr, timeval, posix__result(int),
io__state, io__state).
:- mode select(in, in, in, in, in, out, di, uo) is det.
-:- pred new_fdset(fdset, io__state, io__state).
-:- mode new_fdset(out, di, uo) is det.
+:- pred new_fdset_ptr(fdset_ptr, io__state, io__state).
+:- mode new_fdset_ptr(out, di, uo) is det.
-:- pred fd_clr(fd, fdset, io__state, io__state).
+:- pred fd_clr(fd, fdset_ptr, io__state, io__state).
:- mode fd_clr(in, in, di, uo) is det.
-:- pred fd_isset(fd, fdset, bool, io__state, io__state).
+:- pred fd_isset(fd, fdset_ptr, bool, io__state, io__state).
:- mode fd_isset(in, in, out, di, uo) is det.
-:- pred fd_set(fd, fdset, io__state, io__state).
+:- pred fd_set(fd, fdset_ptr, io__state, io__state).
:- mode fd_set(in, in, di, uo) is det.
-:- pred fd_zero(fdset, io__state, io__state).
+:- pred fd_zero(fdset_ptr, io__state, io__state).
:- mode fd_zero(in, di, uo) is det.
%------------------------------------------------------------------------------%
@@ -49,8 +49,8 @@
#include ""posix_workarounds.h""
").
-:- type fdset
- ---> fdset(c_pointer).
+:- type fdset_ptr
+ ---> fdset_ptr(c_pointer).
%------------------------------------------------------------------------------%
@@ -64,7 +64,7 @@
{ Result = ok(Res) }
).
-:- pred select0(int, fdset, fdset, fdset, int, int, int, io__state, io__state).
+:- pred select0(int, fdset_ptr, fdset_ptr, fdset_ptr, int, int, int, io__state, io__state).
:- mode select0(in, in, in, in, in, in, out, di, uo) is det.
:- pragma c_code(select0(N::in, R::in, W::in, E::in, TS::in, TM::in, Res::out,
@@ -80,7 +80,7 @@
%------------------------------------------------------------------------------%
-:- pragma c_code(new_fdset(Fds::out, IO0::di, IO::uo),
+:- pragma c_code(new_fdset_ptr(Fds::out, IO0::di, IO::uo),
[will_not_call_mercury, thread_safe], "{
incr_hp(Fds, 1+sizeof(fd_set)/sizeof(Word));
Index: posix.socket.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/extras/posix/posix.socket.m,v
retrieving revision 1.1
diff -u -r1.1 posix.socket.m
--- posix.socket.m 1999/10/12 00:12:24 1.1
+++ posix.socket.m 1999/10/18 00:48:16
@@ -4,22 +4,22 @@
% Public License - see the file COPYING.LIB in the Mercury distribution.
%------------------------------------------------------------------------------%
%
-% module posix:socket.
+% module posix__socket.
% main author: conway at cs.mu.oz.au
%
%------------------------------------------------------------------------------%
-:- module posix:socket.
+:- module posix__socket.
:- interface.
:- import_module std_util.
-:- type posix:socket:domain
+:- type posix__socket__domain
---> unix
; inet
.
-:- type posix:socket:(type)
+:- type posix__socket__type
---> stream
; dgram
; raw
@@ -41,20 +41,20 @@
:- type inet_addr
---> inet_addr(int).
-:- pred socket(domain, (type), protocol, posix:result(fd),
+:- pred socket(domain, (type), protocol, posix__result(fd),
io__state, io__state).
:- mode socket(in, in, in, out, di, uo) is det.
-:- pred accept(fd, posix:result(fd), io__state, io__state).
+:- pred accept(fd, posix__result(fd), io__state, io__state).
:- mode accept(in, out, di, uo) is det.
-:- pred bind(fd, sockaddr, posix:result, io__state, io__state).
+:- pred bind(fd, sockaddr, posix__result, io__state, io__state).
:- mode bind(in, in, out, di, uo) is det.
-:- pred connect(fd, sockaddr, posix:result, io__state, io__state).
+:- pred connect(fd, sockaddr, posix__result, io__state, io__state).
:- mode connect(in, in, out, di, uo) is det.
-:- pred listen(fd, int, posix:result, io__state, io__state).
+:- pred listen(fd, int, posix__result, io__state, io__state).
:- mode listen(in, in, out, di, uo) is det.
%------------------------------------------------------------------------------%
@@ -97,7 +97,7 @@
:- pragma c_code(domain(D::in) = (V::out),
[will_not_call_mercury, thread_safe], "{
- static int domain_values[] = {
+ static const int domain_values[] = {
AF_UNIX, AF_INET
};
@@ -109,7 +109,7 @@
:- pragma c_code(type(T::in) = (V::out),
[will_not_call_mercury, thread_safe], "{
- static int type_values[] = {
+ static const int type_values[] = {
SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, SOCK_SEQPACKET, SOCK_RDM
};
@@ -156,7 +156,7 @@
incr_hp(Ptr, (1 + sizeof(struct sockaddr_in)/sizeof(Word)));
ptr = (struct sockaddr_in *) Ptr;
- memset((void *) ptr, 0, sizeof(struct sockaddr_in));
+ memset(ptr, 0, sizeof(struct sockaddr_in));
ptr->sin_family = AF_INET;
ptr->sin_addr.s_addr = A;
ptr->sin_port = htons(P);
Index: posix.write.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/extras/posix/posix.write.m,v
retrieving revision 1.1
diff -u -r1.1 posix.write.m
--- posix.write.m 1999/10/12 00:12:24 1.1
+++ posix.write.m 1999/10/18 00:12:14
@@ -4,17 +4,17 @@
% Public License - see the file COPYING.LIB in the Mercury distribution.
%------------------------------------------------------------------------------%
%
-% module: posix:write.m
+% module: posix__write.m
% main author: conway at cs.mu.oz.au
%
%------------------------------------------------------------------------------%
-:- module posix:write.
+:- module posix__write.
:- interface.
:- import_module text.
-:- pred write(fd, int, text, posix:result(int), io__state, io__state).
+:- pred write(fd, int, text, posix__result(int), io__state, io__state).
:- mode write(in, in, in, out, di, uo) is det.
%------------------------------------------------------------------------------%
Index: text.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/extras/posix/text.m,v
retrieving revision 1.1
diff -u -r1.1 text.m
--- text.m 1999/10/12 00:12:26 1.1
+++ text.m 1999/10/18 00:37:07
@@ -19,29 +19,59 @@
:- type byte == int. % Using low 8 bits only.
+ % text(String) = Text
+ % takes a Mercury string `String' and returns the same data in the
+ % text representation `Text'.
:- func text(string) = text.
:- mode (text(in) = uo) is det.
+ % create(Size, Init, Text)
+ % creates a text object of `Size' bytes each initialized to `Init',
+ % and binds it to `Text'.
:- pred create(int, byte, text).
:- mode create(in, in, uo) is det.
+ % index(Text, Index, Value)
+ % binds `Value' to the `Index'th element of `Text'. Indices are
+ % 0 offset (Like C arrays). index/3 aborts if `Index' is out of
+ % range.
:- pred index(text, int, byte).
:- mode index(ui, in, out) is det.
:- mode index(in, in, out) is det.
+ % update(Index, Value, OldText, NewText)
+ % destroys `OldText' and binds `NewText' to a text object that
+ % is the same as `OldText' except that the byte at `Index' is
+ % replaced with `Value'. update/4 aborts if `Index' is out of
+ % range.
:- pred update(int, byte, text, text).
:- mode update(in, in, di, uo) is det.
+ % length(Text, Length)
+ % binds `Length' to the number of bytes in the text object `Text'.
:- pred length(text, int).
:- mode length(ui, out) is det.
:- mode length(in, out) is det.
+ % unique(SharedText) = UniqueText
+ % performs an unsafe uniqueness cast on `SharedText' to make
+ % `UniqueText'. This is useful if you're storing text objects
+ % inside other data structures, but is of course risky since
+ % it is unchecked by the compiler. USE AT OWN RISK!
:- func unique(text) = text.
-:- mode (unique(in) = uo) is det.
+:- mode unique(in) = uo is det.
+ % split(WholeText, Index, FirstPart, SecondPart)
+ % splits `WholeText' into two parts: `FirstPart' and `SecondPart'
+ % on the boundary `Index'.
:- pred split(text, int, text, text).
:- mode split(di, in, uo, uo) is det.
+ % combine(FirstPart, SecondPart, WholeText)
+ % combines two text objects that were created by splitting `WholeText'.
+ % combine/3 aborts if `FirstPart' and `SecondPart' did not come from
+ % the same text object (in an operational sense, not a declarative
+ % sense - this is a bug, or at least a missing feature).
:- pred combine(text, text, text).
:- mode combine(di, di, uo) is det.
@@ -60,7 +90,7 @@
** ME_words(amt) returns the number of words necessary to
** to store `amt' bytes.
*/
- #define ME_words(x) (1+(x)/sizeof(Word))
+ #define ME_words(x) (((x) + sizeof(Word) - 1) / sizeof(Word))
").
%------------------------------------------------------------------------------%
@@ -111,8 +141,6 @@
Val = txtptr->data[Ind];
}").
-
-%------------------------------------------------------------------------------%
:- pragma c_code(index(Txt::in, Ind::in, Val::out),
[will_not_call_mercury, thread_safe], "{
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to: mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions: mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------
More information about the developers
mailing list