[m-rev.] diff: further cleanups for posix binding

Julien Fischer juliensf at csse.unimelb.edu.au
Mon Apr 23 18:40:17 AEST 2007


Estimated hours taken: 2
Branches: main

Further cleanups and updates for the posix binding.

extras/posix/hello.m:
extras/posix/posix.exec.m:
extras/posix/posix.kill.m:
extras/posix/posix.lseek.m:
extras/posix/posix.mkdir.m:
extras/posix/posix.open.m:
extras/posix/posix.pipe.m:
extras/posix/posix.read.m:
extras/posix/posix.rmdir.m:
extras/posix/posix.select.m:
extras/posix/posix.socket.m:
extras/posix/posix.stat.m:
extras/posix/posix.wait.m:
extras/posix/posix.write.m:
 	Use 4-space indentation throughout.

 	Use the new foreign language interface throughout.

 	Use state variables for threading the I/O state.

 	Add missing `MR_' prefixes.

 	Use foreign_types rather than c_pointer in a number of places; the
 	former is more type safe.  (TODO: there are quite a few remaining uses
 	of c_pointer in these modules that could be replaced by foreign
 	types.)

 	Put no_inline pragmas on foreign_procs containing static data so we
 	don't end up duplicating that data.
 	(TODO: the static data should be shifted into foreign_code pragmas, but
 	that's a separate change.)

 	Rename posix.socket.(type)/0 to socket_type/0.  This avoids having to
 	parenthesise the name.  Redefine the former as an equivalence type for
 	the latter and add a comment mentioning that it is now obsolete.

 	Reformat code as per our current coding standard.

Julien.

Index: hello.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/extras/posix/hello.m,v
retrieving revision 1.2
diff -u -b -r1.2 hello.m
--- hello.m	18 Oct 1999 00:50:25 -0000	1.2
+++ hello.m	23 Apr 2007 08:36:33 -0000
@@ -1,38 +1,43 @@
+% vim: ft=mercury ts=4 sw=4 et
  :- module hello.
-
  :- interface.

  :- import_module io.

-:- pred main(io__state, io__state).
-:- mode main(di, uo) is det.
+:- pred main(io::di, io::uo) is det.

  :- implementation.

-:- import_module posix, posix__open, posix__write, text.
-:- import_module list, string.
+:- import_module posix.
+:- import_module posix.open.
+:- import_module posix.write.
+:- import_module text.
+
+:- import_module list.
+:- import_module string.

-main -->
-	open("/dev/tty", [wronly], Res0),
+main(!IO) :-
+    open("/dev/tty", [wronly], Res0, !IO),
  	(
-		{ Res0 = ok(Fd) },
-		{ Str = "hello world.\n" },
-		{ length(Str, Len) },
-		write(Fd, Len, text(Str), Res1),
+        Res0 = ok(Fd),
+        Str = "hello world.\n",
+        length(Str, Len),
+        write(Fd, Len, text(Str), Res1, !IO),
  		(
-			{ Res1 = ok(NWritten) },
-			( { NWritten \= Len } ->
+            Res1 = ok(NWritten),
+            ( NWritten \= Len ->
  				% We didn't write all of it!
-				write("failed to write it all\n")
+                io.write_string("failed to write it all\n", !IO)
  			;
-				[]
+                true
  			)
  		;
-			{ Res1 = error(Err) },
-			write(Err), nl
+            Res1 = error(Err),
+            io.write(Err, !IO),
+            io.nl(!IO)
  		)
  	;
-		{ Res0 = error(Err) },
-		write(Err), nl
+        Res0 = error(Err),
+        io.write(Err, !IO),
+        io.nl(!IO)
  	).
-
Index: posix.exec.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/extras/posix/posix.exec.m,v
retrieving revision 1.2
diff -u -b -r1.2 posix.exec.m
--- posix.exec.m	28 Jul 2006 05:14:28 -0000	1.2
+++ posix.exec.m	23 Apr 2007 08:36:33 -0000
@@ -1,27 +1,34 @@
-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et
+%-----------------------------------------------------------------------------%
  % Copyright (C) 2001, 2006 The University of Melbourne.
  % This file may only be copied under the terms of the GNU Library General
  % Public License - see the file COPYING.LIB in the Mercury distribution.
-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
  %
-% module: posix__exec.m
-% main author: Michael Day <miked at lendtech.com.au>
+% Module: posix.exec.m.
+% Main author: Michael Day <miked at lendtech.com.au>
  %
-%------------------------------------------------------------------------------%
-:- module posix__exec.
+%-----------------------------------------------------------------------------%

+:- module posix.exec.
  :- interface.

-:- import_module string, list, map.
+:- import_module list.
+:- import_module map.
+:- import_module string.
+
+%-----------------------------------------------------------------------------%

  :- type argv == list(string).

  :- type env == map(string, string).

-:- pred exec(string, argv, env, posix__result, io__state, io__state).
-:- mode exec(in, in, in, out, di, uo) is det.
+:- pred exec(string::in, argv::in, env::in, posix.result::out,
+    io::di, io::uo) is det.

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

  :- implementation.

@@ -30,35 +37,42 @@

  :- pragma foreign_decl("C", "#include <unistd.h>").

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

-exec(Command, Args, Env, Result) -->
+exec(Command, Args, Env, Result, !IO) :-
  	exec0(Command,
  	    array(Args ++ [null]),
-	    array(list__map(variable, map__to_assoc_list(Env)) ++ [null])
+        array(list.map(variable, map.to_assoc_list(Env)) ++ [null]),
+        !IO
  	),
-	errno(Err),
-	{ Result = error(Err) }.
+    errno(Err, !IO),
+    Result = error(Err).

  :- func variable(pair(string)) = string.

  variable(Name - Value) = Name ++ "=" ++ Value.

  :- func null = string.
-
-:- pragma c_code(null = (Null::out), [will_not_call_mercury, thread_safe],
-    "Null = NULL; ").
-
-:- pred exec0(string, array(string), array(string), io__state, io__state).
-:- mode exec0(in, array_ui, array_ui, di, uo) is det.
-
-:- pragma c_code(exec0(Command::in, Args::array_ui, Env::array_ui,
-		IO0::di, IO::uo), [will_not_call_mercury], "{
+:- pragma foreign_proc("C",
+    null = (Null::out),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
+    Null = NULL;
+").
+
+:- pred exec0(string::in,
+    array(string)::array_ui, array(string)::array_ui,
+    io::di, io::uo) is det.
+:- pragma foreign_proc("C",
+    exec0(Command::in, Args::array_ui, Env::array_ui, IO0::di, IO::uo),
+    [promise_pure, will_not_call_mercury, tabled_for_io],
+"
  	execve(Command,
  	    ((MR_ArrayType *)Args)->elements,
  	    ((MR_ArrayType *)Env)->elements);
  	IO = IO0;
-}").
-
-%------------------------------------------------------------------------------%
+").

+%-----------------------------------------------------------------------------%
+:- end_module posix.exec.
+%-----------------------------------------------------------------------------%
Index: posix.kill.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/extras/posix/posix.kill.m,v
retrieving revision 1.1
diff -u -b -r1.1 posix.kill.m
--- posix.kill.m	25 Jul 2001 08:37:23 -0000	1.1
+++ posix.kill.m	23 Apr 2007 08:36:33 -0000
@@ -1,50 +1,53 @@
-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et
+%-----------------------------------------------------------------------------%
  % Copyright (C) 2001 The University of Melbourne.
  % This file may only be copied under the terms of the GNU Library General
  % Public License - see the file COPYING.LIB in the Mercury distribution.
-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
  %
-% module: posix__kill.m
-% main author: Michael Day <miked at lendtech.com.au>
+% Module: posix.kill.m
+% Main author: Michael Day <miked at lendtech.com.au>
  %
-%------------------------------------------------------------------------------%
-:- module posix__kill.
+%-----------------------------------------------------------------------------%

+:- module posix.kill.
  :- interface.

  :- import_module int.

-:- pred kill(pid_t, int, posix__result, io__state, io__state).
-:- mode kill(in, in, out, di, uo) is det.
+:- pred kill(pid_t::in, int::in, posix.result::out, io::di, io::uo) is det.

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

  :- implementation.

-:- pragma c_header_code("
+:- pragma foreign_decl("C", "
  	#include <sys/types.h>
  	#include <signal.h>
  ").

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

-kill(Pid, Sig, Result) -->
-	kill0(Pid, Sig, Res),
-	( if { Res \= 0 } then
-		errno(Err),
-		{ Result = error(Err) }
+kill(Pid, Sig, Result, !IO) :-
+    kill0(Pid, Sig, Res, !IO),
+    ( if Res \= 0 then
+        errno(Err, !IO),
+        Result = error(Err)
  	else
-		{ Result = ok }
+        Result = ok
  	).

-:- pred kill0(pid_t, int, int, io__state, io__state).
-:- mode kill0(in, in, out, di, uo) is det.
-
-:- pragma c_code(kill0(Pid::in, Sig::in, Res::out, IO0::di, IO::uo),
-		[will_not_call_mercury], "{
+:- pred kill0(pid_t::in, int::in, int::out, io::di, io::uo) is det.
+:- pragma foreign_proc("C",
+    kill0(Pid::in, Sig::in, Res::out, IO0::di, IO::uo),
+    [promise_pure, will_not_call_mercury, tabled_for_io],
+"
  	Res = kill(Pid, Sig);
  	IO = IO0;
-}").
-
-%------------------------------------------------------------------------------%
+").

+%-----------------------------------------------------------------------------%
+:- end_module posix.kill.
+%-----------------------------------------------------------------------------%
Index: posix.lseek.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/extras/posix/posix.lseek.m,v
retrieving revision 1.3
diff -u -b -r1.3 posix.lseek.m
--- posix.lseek.m	20 Oct 2005 05:01:44 -0000	1.3
+++ posix.lseek.m	23 Apr 2007 08:36:33 -0000
@@ -1,66 +1,69 @@
+%-----------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et
  %------------------------------------------------------------------------------%
  % Copyright (C) 1999, 2005 The University of Melbourne.
  % This file may only be copied under the terms of the GNU Library General
  % Public License - see the file COPYING.LIB in the Mercury distribution.
  %------------------------------------------------------------------------------%
  %
-% module: posix__lseek.m
-% main author: conway at cs.mu.oz.au
+% Module: posix.lseek.m
+% Main author: conway at cs.mu.oz.au
  %
  %------------------------------------------------------------------------------%
-:- module posix__lseek.

+:- module posix.lseek.
  :- interface.

  :- type whence
  	--->	set
  	;	cur
-	;	end
-	.
+    ;       end.

-:- pred lseek(fd, int, lseek__whence, posix__result(int), io__state, io__state).
-:- mode lseek(in, in, in, out, di, uo) is det.
+:- pred lseek(fd::in, int::in, lseek.whence::in, posix.result(int)::out,
+    io::di, io::uo) is det.

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

  :- implementation.

  :- import_module int.

-:- pragma c_header_code("
+:- pragma foreign_decl("C", "
  	#include <sys/types.h>
  	#include <unistd.h>
  ").

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

-lseek(Fd, Offset, Whence, Result) -->
-	lseek0(Fd, Offset, whence(Whence), Res),
-	( { Res < 0 } ->
-		errno(Err),
-		{ Result = error(Err) }
+lseek(Fd, Offset, Whence, Result, !IO) :-
+    lseek0(Fd, Offset, whence(Whence), Res, !IO),
+    ( Res < 0 ->
+        errno(Err, !IO),
+        Result = error(Err)
  	;
-		{ Result = ok(Res) }
+        Result = ok(Res)
  	).

-:- pred lseek0(fd, int, int, int, io__state, io__state).
-:- mode lseek0(in, in, in, out, di, uo) is det.
-
-:- pragma c_code(lseek0(Fd::in, Offset::in, Whence::in, Res::out,
-		IO0::di, IO::uo), [will_not_call_mercury, thread_safe], "{
-
+:- pred lseek0(fd::in, int::in, int::in, int::out, io::di, io::uo) is det.
+:- pragma foreign_proc("C",
+    lseek0(Fd::in, Offset::in, Whence::in, Res::out, IO0::di, IO::uo),
+    [promise_pure, will_not_call_mercury, thread_safe, tabled_for_io],
+"
  	Res = lseek(Fd, Offset, Whence);
-
  	IO = IO0;
-}").
+").

+:- pragma no_inline(whence/1).
  :- func whence(lseek.whence) = int.
-
-:- pragma c_code(whence(W::in) = (V::out),
-		[will_not_call_mercury, thread_safe], "{
+:- pragma foreign_proc("C",
+    whence(W::in) = (V::out),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
  	static const int whence_flags[] = { SEEK_SET, SEEK_CUR, SEEK_END } ;
  	V = whence_flags[W];
-}").
-
-%------------------------------------------------------------------------------%
+").

+%-----------------------------------------------------------------------------%
+:- end_module posix.lseek.
+%-----------------------------------------------------------------------------%
Index: posix.mkdir.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/extras/posix/posix.mkdir.m,v
retrieving revision 1.2
diff -u -b -r1.2 posix.mkdir.m
--- posix.mkdir.m	25 Jul 2001 08:37:23 -0000	1.2
+++ posix.mkdir.m	23 Apr 2007 08:36:33 -0000
@@ -1,52 +1,52 @@
-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et
+%-----------------------------------------------------------------------------%
  % Copyright (C) 2001 The University of Melbourne.
  % This file may only be copied under the terms of the GNU Library General
  % Public License - see the file COPYING.LIB in the Mercury distribution.
-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
  %
-% module: posix__mkdir.m
-% main author: Michael Day <miked at lendtech.com.au>
+% Module: posix.mkdir.m
+% Main author: Michael Day <miked at lendtech.com.au>
  %
-%------------------------------------------------------------------------------%
-:- module posix__mkdir.
+%-----------------------------------------------------------------------------%

+:- module posix.mkdir.
  :- interface.

-:- import_module string.
+:- pred mkdir(string::in, mode_t::in, posix.result::out, io::di, io::uo)
+    is det.

-:- pred mkdir(string, mode_t, posix__result, io__state, io__state).
-:- mode mkdir(in, in, out, di, uo) is det.
-
-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

  :- implementation.

-:- import_module int.
-
-:- pragma c_header_code("
+:- pragma foreign_decl("C", "
  	#include <sys/types.h>
  	#include <sys/stat.h>
  ").

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

-mkdir(Path, Mode, Result) -->
-	mkdir0(Path, Mode, Res),
-	( if { Res = 0 } then
-	    { Result = ok }
+mkdir(Path, Mode, Result, !IO) :-
+    mkdir0(Path, Mode, Res, !IO),
+    ( if Res = 0 then
+        Result = ok
  	else
-	    errno(Err),
-	    { Result = error(Err) }
+        errno(Err, !IO),
+        Result = error(Err)
  	).

-:- pred mkdir0(string, mode_t, int, io__state, io__state).
-:- mode mkdir0(in, in, out, di, uo) is det.
-
-:- pragma c_code(mkdir0(Path::in, Mode::in, Res::out, IO0::di, IO::uo),
-	    [will_not_call_mercury, thread_safe], "
+:- pred mkdir0(string::in, mode_t::in, int::out, io::di, io::uo) is det.
+:- pragma foreign_proc("C",
+    mkdir0(Path::in, Mode::in, Res::out, IO0::di, IO::uo),
+    [promise_pure, will_not_call_mercury, thread_safe, tabled_for_io],
+"
  	Res = mkdir(Path, Mode);
  	IO = IO0;
  ").

-%------------------------------------------------------------------------------%
-
+%-----------------------------------------------------------------------------%
+:- end_module posix.mkdir.
+%-----------------------------------------------------------------------------%
Index: posix.open.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/extras/posix/posix.open.m,v
retrieving revision 1.3
diff -u -b -r1.3 posix.open.m
--- posix.open.m	25 Jul 2001 08:37:23 -0000	1.3
+++ posix.open.m	23 Apr 2007 08:36:33 -0000
@@ -1,22 +1,25 @@
-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et
+%-----------------------------------------------------------------------------%
  % Copyright (C) 1999, 2001 The University of Melbourne.
  % This file may only be copied under the terms of the GNU Library General
  % Public License - see the file COPYING.LIB in the Mercury distribution.
-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
  %
-% module: posix__open.
-% main author: conway at cs.mu.oz.au
+% Module: posix.open.
+% Main author: conway at cs.mu.oz.au
  %
-% This module provides and interface to the open function and its
-% relatives.
+% This module provides and interface to the open function and its relatives.
  %
-%------------------------------------------------------------------------------%
-:- module posix__open.
+%-----------------------------------------------------------------------------%

+:- module posix.open.
  :- interface.

  :- import_module list.

+%-----------------------------------------------------------------------------%
+
  :- type oflag
  	--->	rdonly
  	;	wronly
@@ -27,138 +30,140 @@
  	;	trunc
  	;	append
  	;	ndelay
-	;	sync
-	.
+    ;       sync.

-:- pred open(string, list(oflag), posix__result(fd), io__state, io__state).
-:- mode open(in, in, out, di, uo) is det.
+:- pred open(string::in, list(oflag)::in, posix.result(fd)::out,
+    io::di, io::uo) is det.

-:- pred open(string, list(oflag), mode_t, posix__result(fd),
-		io__state, io__state).
-:- mode open(in, in, in, out, di, uo) is det.
+:- pred open(string::in, list(oflag)::in, mode_t::in, posix.result(fd)::out,
+    io::di, io::uo) is det.

-:- pred creat(string, mode_t, posix__result(fd), io__state, io__state).
-:- mode creat(in, in, out, di, uo) is det.
+:- pred creat(string::in, mode_t::in, posix.result(fd)::out,
+    io::di, io::uo) is det.

-:- pred close(fd, posix__result, io__state, io__state).
-:- mode close(in, out, di, uo) is det.
+:- pred close(fd::in, posix.result::out, io::di, io::uo) is det.

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

  :- implementation.

  :- import_module int.

-:- pragma c_header_code("
+:- pragma foreign_decl("C", "
  	#include <unistd.h>
  	#include <fcntl.h>
  ").

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

-open(PathName, FlagList, Result) -->
-	open0(PathName, oflags(FlagList), FdNo),
-	( { FdNo < 0 } ->
-		errno(Error),
-		{ Result = error(Error) }
+open(PathName, FlagList, Result, !IO) :-
+    open0(PathName, oflags(FlagList), FdNo, !IO),
+    ( FdNo < 0 ->
+        errno(Error, !IO),
+        Result = error(Error)
  	;
-		{ Result = ok(fd(FdNo)) }
+        Result = ok(fd(FdNo))
  	).

-:- pred open0(string, int, int, io__state, io__state).
-:- mode open0(in, in, out, di, uo) is det.
-
-:- pragma c_code(open0(PathName::in, Flags::in, FileDes::out, IO0::di, IO::uo),
-		[will_not_call_mercury, thread_safe], "{
+:- pred open0(string::in, int::in, int::out, io::di, io::uo) is det.
+:- pragma foreign_proc("C",
+    open0(PathName::in, Flags::in, FileDes::out, IO0::di, IO::uo),
+    [promise_pure, will_not_call_mercury, thread_safe, tabled_for_io],
+ "
  	FileDes = open(PathName, Flags);
  	IO = IO0;
-}").
+").

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

-open(PathName, FlagList, Mode, Result) -->
-	open0(PathName, oflags(FlagList), Mode, FdNo),
-	( { FdNo < 0 } ->
-		errno(Error),
-		{ Result = error(Error) }
+open(PathName, FlagList, Mode, Result, !IO) :-
+    open0(PathName, oflags(FlagList), Mode, FdNo, !IO),
+    ( FdNo < 0 ->
+        errno(Error, !IO),
+        Result = error(Error)
  	;
-		{ Result = ok(fd(FdNo)) }
+        Result = ok(fd(FdNo))
  	).

-:- pred open0(string, int, mode_t, int, io__state, io__state).
-:- mode open0(in, in, in, out, di, uo) is det.
-
-:- pragma c_code(open0(PathName::in, Flags::in, Mode::in, FileDes::out,
-		IO0::di, IO::uo), [will_not_call_mercury, thread_safe], "{
+:- pred open0(string::in, int::in, mode_t::in, int::out, io::di, io::uo)
+    is det.
+:- pragma foreign_proc("C",
+    open0(PathName::in, Flags::in, Mode::in, FileDes::out, IO0::di, IO::uo),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
  	FileDes = open(PathName, Flags, Mode);
  	IO = IO0;
-}").
+").

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

-creat(PathName, Mode, Result) -->
-	creat0(PathName, Mode, FdNo),
-	( { FdNo < 0 } ->
-		errno(Error),
-		{ Result = error(Error) }
+creat(PathName, Mode, Result, !IO) :-
+    creat0(PathName, Mode, FdNo, !IO),
+    ( FdNo < 0 ->
+        errno(Error, !IO),
+        Result = error(Error)
  	;
-		{ Result = ok(fd(FdNo)) }
+        Result = ok(fd(FdNo))
  	).

-:- pred creat0(string, mode_t, int, io__state, io__state).
-:- mode creat0(in, in, out, di, uo) is det.
-
-:- pragma c_code(creat0(PathName::in, Mode::in, FileDes::out,
-		IO0::di, IO::uo), [will_not_call_mercury, thread_safe], "{
+:- pred creat0(string::in, mode_t::in, int::out, io::di, io::uo) is det.
+:- pragma foreign_proc("C",
+    creat0(PathName::in, Mode::in, FileDes::out, IO0::di, IO::uo),
+    [promise_pure, will_not_call_mercury, thread_safe, tabled_for_io],
+"
  	FileDes = creat(PathName, Mode);
  	IO = IO0;
-}").
+").

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

-close(fd(FdNo), Result) -->
-	close0(FdNo, Res0),
-	( { Res0 < 0 } ->
-		errno(Error),
-		{ Result = error(Error) }
+close(fd(FdNo), Result, !IO) :-
+    close0(FdNo, Res0, !IO),
+    ( Res0 < 0 ->
+        errno(Error, !IO),
+        Result = error(Error)
  	;
-		{ Result = ok }
+        Result = ok
  	).

-:- pred close0(int, int, io__state, io__state).
-:- mode close0(in, out, di, uo) is det.
-
-:- pragma c_code(close0(Fd::in, Res::out, IO0::di, IO::uo),
-		[will_not_call_mercury, thread_safe], "{
+:- pred close0(int::in, int::out, io::di, io::uo) is det.
+:- pragma foreign_proc("C",
+    close0(Fd::in, Res::out, IO0::di, IO::uo),
+    [promise_pure, will_not_call_mercury, thread_safe, tabled_for_io],
+"
  	Res = close(Fd);
  	IO = IO0;
-}").
+").

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

  :- func oflags(list(oflag)) = int.

  oflags(FlagList) = Or :-
  	orflags(FlagList, 0, Or).

-:- pred orflags(list(oflag), int, int).
-:- mode orflags(in, in, out) is det.
+:- pred orflags(list(oflag)::in, int::in, int::out) is det.

-orflags([], Or, Or).
-orflags([F|Fs], Or0, Or) :-
-	Or1 = Or0 \/ oflagval(F),
-	orflags(Fs, Or1, Or).
+orflags([], !Or).
+orflags([F | Fs], !Or) :-
+    !:Or = !.Or \/ oflagval(F),
+    orflags(Fs, !Or).

+:- pragma no_inline(oflagval/1).
  :- func oflagval(oflag) = int.
-:- mode (oflagval(in) = out) is det.
-
-:- pragma c_code(oflagval(F::in) = (V::out),
-		[will_not_call_mercury, thread_safe], "{
+:- pragma foreign_proc("C", 
+    oflagval(F::in) = (V::out),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
  	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
  	};

  	V = oflag_values[F];
-}").
+").

+%-----------------------------------------------------------------------------%
+:- end_module posix.open.
+%-----------------------------------------------------------------------------%
Index: posix.pipe.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/extras/posix/posix.pipe.m,v
retrieving revision 1.1
diff -u -b -r1.1 posix.pipe.m
--- posix.pipe.m	25 Jul 2001 08:37:23 -0000	1.1
+++ posix.pipe.m	23 Apr 2007 08:36:33 -0000
@@ -1,53 +1,58 @@
-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et
+%-----------------------------------------------------------------------------%
  % Copyright (C) 2001 The University of Melbourne.
  % This file may only be copied under the terms of the GNU Library General
  % Public License - see the file COPYING.LIB in the Mercury distribution.
-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
  %
-% module: posix__pipe.m
-% main author: Michael Day <miked at lendtech.com.au>
+% Module: posix__pipe.m
+% Main author: Michael Day <miked at lendtech.com.au>
  %
-%------------------------------------------------------------------------------%
-:- module posix__pipe.
+%-----------------------------------------------------------------------------%

+:- module posix.pipe.
  :- interface.

-:- pred pipe(posix__result({fd, fd}), io__state, io__state).
-:- mode pipe(out, di, uo) is det.
+%-----------------------------------------------------------------------------%

-%------------------------------------------------------------------------------%
+:- pred pipe(posix.result({fd, fd})::out, io::di, io::uo) is det.
+
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

  :- implementation.

  :- import_module int.

-:- pragma c_header_code("
+:- pragma foreign_decl("C", "
  	#include <sys/types.h>
  	#include <unistd.h>
  ").

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

-pipe(Result) -->
-	pipe0(Reading, Writing, Res),
-	( if { Res \= 0 } then
-		errno(Err),
-		{ Result = error(Err) }
+pipe(Result, !IO) :-
+    pipe0(Reading, Writing, Res, !IO),
+    ( if Res \= 0 then
+        errno(Err, !IO),
+        Result = error(Err)
  	else
-		{ Result = ok({Reading, Writing}) }
+        Result = ok({Reading, Writing})
  	).

-:- pred pipe0(fd, fd, int, io__state, io__state).
-:- mode pipe0(out, out, out, di, uo) is det.
-
-:- pragma c_code(pipe0(R::out, W::out, Res::out, IO0::di, IO::uo),
-		[will_not_call_mercury], "{
+:- pred pipe0(fd::out, fd::out, int::out, io::di, io::uo) is det.
+:- pragma foreign_proc("C",
+    pipe0(R::out, W::out, Res::out, IO0::di, IO::uo),
+    [promise_pure, will_not_call_mercury, tabled_for_io],
+"
  	int filedes[2];
  	Res = pipe(filedes);
  	R = filedes[0];
  	W = filedes[1];
  	IO = IO0;
-}").
-
-%------------------------------------------------------------------------------%
+").

+%-----------------------------------------------------------------------------%
+:- end_module posix.pipe.
+%-----------------------------------------------------------------------------%
Index: posix.read.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/extras/posix/posix.read.m,v
retrieving revision 1.2
diff -u -b -r1.2 posix.read.m
--- posix.read.m	18 Oct 1999 00:50:26 -0000	1.2
+++ posix.read.m	23 Apr 2007 08:36:33 -0000
@@ -1,49 +1,53 @@
-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et
+%-----------------------------------------------------------------------------%
  % Copyright (C) 1999 The University of Melbourne.
  % This file may only be copied under the terms of the GNU Library General
  % Public License - see the file COPYING.LIB in the Mercury distribution.
-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
  %
-% module: posix__read.m
-% main author: conway at cs.mu.oz.au
+% Module: posix.read.
+% 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).
-:- mode read(in, in, out, di, uo, di, uo) is det.
+:- pred read(fd::in, int::in, posix.result(int)::out,
+    text::di, text::uo, io::di, io::uo) is det.

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

  :- implementation.

  :- import_module int.

-:- pragma c_header_code("
+:- pragma foreign_decl("C", "
  	#include <unistd.h>
  	#include ""text_header.h""
  ").

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

-read(Fd, ToRead, Result, Text0, Text) -->
-	read0(Fd, ToRead, Read, Text0, Text),
-	( { Read < 0 } ->
-		errno(Err),
-		{ Result = error(Err) }
+read(Fd, ToRead, Result, !Text, !IO) :-
+    read0(Fd, ToRead, Read, !Text, !IO),
+    ( Read < 0 ->
+        errno(Err, !IO),
+        Result = error(Err)
  	;
-		{ Result = ok(Read) }
+        Result = ok(Read)
  	).

-:- pred read0(fd, int, int, text, text, io__state, io__state).
-:- mode read0(in, in, out, di, uo, di, uo) is det.
-
-:- pragma c_code(read0(Fd::in, ToRead::in, Read::out, Text0::di, Text::uo,
-		IO0::di, IO::uo), [will_not_call_mercury, thread_safe], "{
+:- pred read0(fd::in, int::in, int::out, text::di, text::uo,
+    io::di, io::uo) is det.
+:- pragma foreign_proc("C",
+    read0(Fd::in, ToRead::in, Read::out, Text0::di, Text::uo, IO0::di, IO::uo),
+    [promise_pure, will_not_call_mercury, thread_safe, tabled_for_io],
+ "
  	ME_Text *txtptr;

  	txtptr = (ME_Text *) Text0;
@@ -52,7 +56,8 @@

  	Text = Text0;
  	IO = IO0;
-}").
-
-%------------------------------------------------------------------------------%
+").

+%-----------------------------------------------------------------------------%
+:- end_module posix.read.
+%-----------------------------------------------------------------------------%
Index: posix.rmdir.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/extras/posix/posix.rmdir.m,v
retrieving revision 1.1
diff -u -b -r1.1 posix.rmdir.m
--- posix.rmdir.m	16 Jul 2001 03:08:19 -0000	1.1
+++ posix.rmdir.m	23 Apr 2007 08:36:33 -0000
@@ -1,51 +1,52 @@
-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et
+%-----------------------------------------------------------------------------%
  % Copyright (C) 2001 The University of Melbourne.
  % This file may only be copied under the terms of the GNU Library General
  % Public License - see the file COPYING.LIB in the Mercury distribution.
-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
  %
-% module: posix__rmdir.m
-% main author: Michael Day <miked at lendtech.com.au>
+% Module: posix.rmdir.
+% Main author: Michael Day <miked at lendtech.com.au>
  %
  %------------------------------------------------------------------------------%
-:- module posix__rmdir.

+:- module posix.rmdir.
  :- interface.

  :- import_module string.

-:- pred rmdir(string, posix__result, io__state, io__state).
-:- mode rmdir(in, out, di, uo) is det.
+:- pred rmdir(string::in, posix.result::out, io::di, io::uo) is det.

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

  :- implementation.

-:- import_module int.
-
-:- pragma c_header_code("
+:- pragma foreign_decl("C", "
  	#include <unistd.h>
  ").

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

-rmdir(Path, Result) -->
-	rmdir0(Path, Res),
-	( if { Res = 0 } then
-	    { Result = ok }
+rmdir(Path, Result, !IO) :-
+    rmdir0(Path, Res, !IO),
+    ( if Res = 0 then
+        Result = ok
  	else
-	    errno(Err),
-	    { Result = error(Err) }
+        errno(Err, !IO),
+        Result = error(Err)
  	).

-:- pred rmdir0(string, int, io__state, io__state).
-:- mode rmdir0(in, out, di, uo) is det.
-
-:- pragma c_code(rmdir0(Path::in, Res::out, IO0::di, IO::uo),
-	    [will_not_call_mercury, thread_safe], "
+:- pred rmdir0(string::in, int::out, io::di, io::uo) is det.
+:- pragma foreign_proc("C",
+    rmdir0(Path::in, Res::out, IO0::di, IO::uo),
+    [promise_pure, will_not_call_mercury, thread_safe, tabled_for_io],
+"
  	Res = rmdir(Path);
  	IO = IO0;
  ").

-%------------------------------------------------------------------------------%
-
+%-----------------------------------------------------------------------------%
+:- end_module posix.rmdir.
+%-----------------------------------------------------------------------------%
Index: posix.select.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/extras/posix/posix.select.m,v
retrieving revision 1.4
diff -u -b -r1.4 posix.select.m
--- posix.select.m	30 Nov 2004 14:53:39 -0000	1.4
+++ posix.select.m	23 Apr 2007 08:36:33 -0000
@@ -1,47 +1,44 @@
-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et
+%-----------------------------------------------------------------------------%
  % Copyright (C) 1999-2000, 2004 The University of Melbourne.
  % This file may only be copied under the terms of the GNU Library General
  % Public License - see the file COPYING.LIB in the Mercury distribution.
-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
  %
-% module: posix__select.m
-% main author: conway at cs.mu.oz.au
+% Module: posix.select.
+% Main author: conway at cs.mu.oz.au
  %
-%------------------------------------------------------------------------------%
-:- module posix__select.
+%-----------------------------------------------------------------------------%

+:- module posix.select.
  :- interface.

  :- import_module bool.

+%-----------------------------------------------------------------------------%
+
  :- type fdset_ptr.

-:- 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 select(int::in, fdset_ptr::in, fdset_ptr::in, fdset_ptr::in,
+    timeval::in, posix.result(int)::out, io::di, io::uo) is det.

-:- pred new_fdset_ptr(fdset_ptr, io__state, io__state).
-:- mode new_fdset_ptr(out, di, uo) is det.
+:- pred new_fdset_ptr(fdset_ptr::out, io::di, io::uo) is det.

-:- pred fd_clr(fd, fdset_ptr, io__state, io__state).
-:- mode fd_clr(in, in, di, uo) is det.
+:- pred fd_clr(fd::in, fdset_ptr::in, io::di, io::uo) is det.

-:- pred fd_isset(fd, fdset_ptr, bool, io__state, io__state).
-:- mode fd_isset(in, in, out, di, uo) is det.
+:- pred fd_isset(fd::in, fdset_ptr::in, bool::out, io::di, io::uo) is det.

-:- pred fd_set(fd, fdset_ptr, io__state, io__state).
-:- mode fd_set(in, in, di, uo) is det.
+:- pred fd_set(fd::in, fdset_ptr::in, io::di, io::uo) is det.

-:- pred fd_zero(fdset_ptr, io__state, io__state).
-:- mode fd_zero(in, di, uo) is det.
+:- pred fd_zero(fdset_ptr::in, io::di, io::uo) is det.

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

  :- implementation.

-:- import_module int, std_util.
-
-:- pragma c_header_code("
+:- pragma foreign_decl("C", "
  	#include <sys/time.h>
  	#include <sys/types.h>
  	#include <unistd.h>
@@ -49,75 +46,90 @@
  	#include ""posix_workarounds.h""
  ").

-:- type fdset_ptr
-	--->	fdset_ptr(c_pointer).
+:- type fdset_ptr.
+:- pragma foreign_type("C", fdset_ptr, "fd_set *", [can_pass_as_mercury_type]).

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

-select(Fd, R, W, E, Timeout, Result) -->
-	{ Timeout = timeval(TS, TM) },
-	select0(Fd, R, W, E, TS, TM, Res),
-	( { Res < 0 } ->
-		errno(Err),
-		{ Result = error(Err) }
+select(Fd, R, W, E, Timeout, Result, !IO) :-
+    Timeout = timeval(TS, TM),
+    select0(Fd, R, W, E, TS, TM, Res, !IO),
+    ( Res < 0 ->
+        errno(Err, !IO),
+        Result = error(Err)
  	;
-		{ Result = ok(Res) }
+        Result = ok(Res)
  	).

-:- 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,
-		IO0::di, IO::uo), [will_not_call_mercury, thread_safe], "{
+:- pred select0(int::in, fdset_ptr::in, fdset_ptr::in, fdset_ptr::in, int::in,
+    int::in, int::out, io::di, io::uo) is det.
+:- pragma foreign_proc("C",
+    select0(N::in, R::in, W::in, E::in, TS::in, TM::in, Res::out,
+        IO0::di, IO::uo),
+    [promise_pure, will_not_call_mercury, thread_safe, tabled_for_io],
+"
  	struct timeval tv;

  	tv.tv_sec = TS;
  	tv.tv_usec = TM;
-	Res = select(N, (fd_set *)R, (fd_set *)W, (fd_set *)E, &tv);
-
+    Res = select(N, R, W, E, &tv);
  	IO = IO0;
-}").
-
-%------------------------------------------------------------------------------%
-
-:- pragma c_code(new_fdset_ptr(Fds::out, IO0::di, IO::uo),
-		[will_not_call_mercury, thread_safe], "{
+").

-	MR_incr_hp(Fds, 1+sizeof(fd_set)/sizeof(MR_Word));
-	ME_fd_zero((fd_set *) Fds);
+%-----------------------------------------------------------------------------%

+:- pragma foreign_proc("C",
+    new_fdset_ptr(Fds::out, IO0::di, IO::uo),
+    [promise_pure, will_not_call_mercury, thread_safe, tabled_for_io],
+"
+    MR_Word Fds0;
+
+    MR_incr_hp(Fds0, 1+sizeof(fd_set)/sizeof(MR_Word));
+    Fds = (fd_set *) Fds0;
+    ME_fd_zero(Fds);
  	IO = IO0;
-}").
+").

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

-:- pragma c_code(fd_clr(Fd::in, Fds::in, IO0::di, IO::uo),
-		[will_not_call_mercury, thread_safe], "{
-	ME_fd_clr(Fd, (fd_set *) Fds);
+:- pragma foreign_proc("C",
+    fd_clr(Fd::in, Fds::in, IO0::di, IO::uo),
+    [promise_pure, will_not_call_mercury, thread_safe, tabled_for_io],
+"
+    ME_fd_clr(Fd, Fds);
  	IO = IO0;
-}").
+").

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

-:- pragma c_code(fd_zero(Fds::in, IO0::di, IO::uo),
-		[will_not_call_mercury, thread_safe], "{
-	ME_fd_zero((fd_set *) Fds);
+:- pragma foreign_proc("C",
+    fd_zero(Fds::in, IO0::di, IO::uo),
+    [promise_pure, will_not_call_mercury, thread_safe, tabled_for_io],
+"
+    ME_fd_zero(Fds);
  	IO = IO0;
-}").
+").

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

-:- pragma c_code(fd_isset(Fd::in, Fds::in, Res::out, IO0::di, IO::uo),
-		[will_not_call_mercury, thread_safe], "{
-	Res = (ME_fd_isset(Fd, (fd_set *) Fds) ? MR_YES : MR_NO );
+:- pragma foreign_proc("C",
+    fd_isset(Fd::in, Fds::in, Res::out, IO0::di, IO::uo),
+    [promise_pure, will_not_call_mercury, thread_safe, tabled_for_io],
+"
+    Res = (ME_fd_isset(Fd, Fds) ? MR_YES : MR_NO );
  	IO = IO0;
-}").
+").

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

-:- pragma c_code(fd_set(Fd::in, Fds::in, IO0::di, IO::uo),
-		[will_not_call_mercury, thread_safe], "{
-	ME_fd_set(Fd, (fd_set *) Fds);
+:- pragma foreign_proc("C",
+    fd_set(Fd::in, Fds::in, IO0::di, IO::uo),
+    [promise_pure, will_not_call_mercury, thread_safe, tabled_for_io],
+"
+    ME_fd_set(Fd, Fds);
  	IO = IO0;
-}").
+").

+%-----------------------------------------------------------------------------%
+:- end_module posix.select.
+%-----------------------------------------------------------------------------%
Index: posix.socket.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/extras/posix/posix.socket.m,v
retrieving revision 1.5
diff -u -b -r1.5 posix.socket.m
--- posix.socket.m	15 Jan 2004 10:30:19 -0000	1.5
+++ posix.socket.m	23 Apr 2007 08:36:33 -0000
@@ -1,37 +1,41 @@
-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et
+%-----------------------------------------------------------------------------%
  % Copyright (C) 1999-2000, 2004 The University of Melbourne.
  % This file may only be copied under the terms of the GNU Library General
  % Public License - see the file COPYING.LIB in the Mercury distribution.
-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
  %
-% module posix__socket.
-% main author: conway at cs.mu.oz.au
+% Module posix.socket.
+% Main author: conway at cs.mu.oz.au
  %
-%------------------------------------------------------------------------------%
-:- module posix__socket.
+%-----------------------------------------------------------------------------%

+:- module posix.socket.
  :- interface.

-:- type posix__socket__domain
+%-----------------------------------------------------------------------------%
+
+:- type posix.socket.domain
  	--->	unix
-	;	inet
-	.
+    ;       inet.

-:- type posix__socket__type
+    % OBSOLETE: this was the old name for socket_type/0.
+    %
+:- type posix.socket.(type) == socket_type.
+
+:- type socket_type
  	--->	stream
  	;	dgram
  	;	raw
  	;	seqpacket
-	;	rdm
-	.
+    ;       rdm.

  :- type protocol
-	--->	protocol(int)
-	.
+    --->    protocol(int).

  :- type sockaddr
-	--->	inet(port, inet_addr)
-	.
+    --->    inet(port, inet_addr).

  :- type port
  	--->	port(int).
@@ -39,21 +43,19 @@
  :- type inet_addr
  	--->	inet_addr(int).

-:- pred socket(domain, (type), protocol, posix__result(fd),
-		io__state, io__state).
-:- mode socket(in, in, in, out, di, uo) is det.
+%-----------------------------------------------------------------------------%
+
+:- pred socket(domain::in, socket_type::in, protocol::in,
+    posix.result(fd)::out, io::di, io::uo) is det.

-:- pred accept(fd, posix__result(fd), io__state, io__state).
-:- mode accept(in, out, di, uo) is det.
+:- pred accept(fd::in, posix.result(fd)::out, io::di, io::uo) is det.

-:- pred bind(fd, sockaddr, posix__result, io__state, io__state).
-:- mode bind(in, in, out, di, uo) is det.
+:- pred bind(fd::in, sockaddr::in, posix.result::out, io::di, io::uo) is det.

-:- pred connect(fd, sockaddr, posix__result, io__state, io__state).
-:- mode connect(in, in, out, di, uo) is det.
+:- pred connect(fd::in, sockaddr::in, posix.result::out, io::di, io::uo)
+    is det.

-:- pred listen(fd, int, posix__result, io__state, io__state).
-:- mode listen(in, in, out, di, uo) is det.
+:- pred listen(fd::in, int::in, posix.result::out, io::di, io::uo) is det.

  %------------------------------------------------------------------------------%
  %------------------------------------------------------------------------------%
@@ -62,7 +64,7 @@

  :- import_module int.

-:- pragma c_header_code("
+:- pragma foreign_decl("C", "
  	#include <string.h>
  	#include <sys/types.h>
  	#include <sys/socket.h>
@@ -70,157 +72,169 @@
  	#include <arpa/inet.h>
  ").

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

-socket(Dom, Typ, protocol(Prot), Result) -->
-	socket0(domain(Dom), type(Typ), Prot, FdNo),
-	( { FdNo < 0 } ->
-		errno(Err),
-		{ Result = error(Err) }
+socket(Dom, Typ, protocol(Prot), Result, !IO) :-
+    socket0(domain(Dom), socket_type(Typ), Prot, FdNo, !IO),
+    ( FdNo < 0 ->
+        errno(Err, !IO),
+        Result = error(Err)
  	;
-		{ Result = ok(fd(FdNo)) }
+        Result = ok(fd(FdNo))
  	).

-:- pred socket0(int, int, int, int, io__state, io__state).
-:- mode socket0(in, in, in, out, di, uo) is det.
-
-:- pragma c_code(socket0(Dom::in, Typ::in, Prot::in, Fd::out, IO0::di, IO::uo),
-		[will_not_call_mercury, thread_safe], "{
+:- pred socket0(int::in, int::in, int::in, int::out, io::di, io::uo) is det.
+:- pragma foreign_proc("C",
+    socket0(Dom::in, Typ::in, Prot::in, Fd::out, IO0::di, IO::uo),
+    [promise_pure, will_not_call_mercury, thread_safe, tabled_for_io],
+"
  	Fd = socket(Dom, Typ, Prot);
  	IO = IO0;
-}").
+").

+:- pragma no_inline(domain/1).
  :- func domain(domain) = int.
-:- mode (domain(in) = out) is det.
-
-:- pragma c_code(domain(D::in) = (V::out),
-		[will_not_call_mercury, thread_safe], "{
+:- pragma foreign_proc("C",
+    domain(D::in) = (V::out),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
  	static const int domain_values[] = {
-		AF_UNIX, AF_INET
+        AF_UNIX,
+        AF_INET
  	};

  	V = domain_values[D];
-}").
-
-:- func type(type) = int.
-:- mode (type(in) = out) is det.
+").

-:- pragma c_code(type(T::in) = (V::out),
-		[will_not_call_mercury, thread_safe], "{
+:- pragma no_inline(socket_type/1).
+:- func socket_type(socket_type) = int.
+:- pragma foreign_proc("C",
+    socket_type(T::in) = (V::out),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
  	static const int type_values[] = {
-		SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, SOCK_SEQPACKET, SOCK_RDM
+        SOCK_STREAM,
+        SOCK_DGRAM,
+        SOCK_RAW,
+        SOCK_SEQPACKET,
+        SOCK_RDM
  	};

  	V = type_values[T];
-}").
+").

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

  :- type sockaddr_ptr
  	--->	sockaddr_ptr(c_pointer).

-bind(Fd, SockAddr, Result) -->
-	{ mksockaddr_struct(SockAddr, Ptr, Len) },
-	bind0(Fd, Ptr, Len, Res0),
-	( { Res0 = 0 } ->
-		{ Result = ok }
+bind(Fd, SockAddr, Result, !IO) :-
+    mksockaddr_struct(SockAddr, Ptr, Len),
+    bind0(Fd, Ptr, Len, Res0, !IO),
+    ( Res0 = 0 ->
+        Result = ok
  	;
-		errno(Errno),
-		{ Result = error(Errno) }
+        errno(Errno, !IO),
+        Result = error(Errno)
  	).

-:- pred bind0(fd, sockaddr_ptr, int, int, io__state, io__state).
-:- mode bind0(in, in, in, out, di, uo) is det.
-
-:- pragma c_code(bind0(Fd::in, Addr::in, Len::in, Res::out, IO0::di, IO::uo),
-		[will_not_call_mercury, thread_safe], "{
+:- pred bind0(fd::in, sockaddr_ptr::in, int::in, int::out,
+    io::di, io::uo) is det.
+:- pragma foreign_proc("C",
+    bind0(Fd::in, Addr::in, Len::in, Res::out, IO0::di, IO::uo),
+    [promise_pure, will_not_call_mercury, thread_safe, tabled_for_io],
+"
  	Res = bind(Fd, (struct sockaddr *) Addr, Len);
  	IO = IO0;
-}").
+").

-:- pred mksockaddr_struct(sockaddr, sockaddr_ptr, int).
-:- mode mksockaddr_struct(in, out, out) is det.
+:- pred mksockaddr_struct(sockaddr::in, sockaddr_ptr::out, int::out) is det.

  mksockaddr_struct(inet(Port, Addr), Ptr, Len) :-
  	mkinet_addr(Addr, Port, Ptr, Len).

-:- pred mkinet_addr(inet_addr, port, sockaddr_ptr, int).
-:- mode mkinet_addr(in, in, out, out) is det.
+:- pred mkinet_addr(inet_addr::in, port::in, sockaddr_ptr::out, int::out)
+    is det.

-:- pragma c_code(mkinet_addr(A::in, P::in, Ptr::out, Len::out), 
-		[will_not_call_mercury, thread_safe], "{
+:- pragma foreign_proc("C",
+    mkinet_addr(A::in, P::in, Ptr::out, Len::out), 
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
  	struct sockaddr_in *ptr;

  	MR_incr_hp(Ptr, (1 + sizeof(struct sockaddr_in)/sizeof(MR_Word)));
  	ptr = (struct sockaddr_in *) Ptr;

-	memset(ptr, 0, sizeof(struct sockaddr_in));
+    MR_memset(ptr, 0, sizeof(struct sockaddr_in));
  	ptr->sin_family = AF_INET;
  	ptr->sin_addr.s_addr = A;
  	ptr->sin_port = htons(P);

  	Len = sizeof(struct sockaddr_in);
-}").
+").

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

-connect(Fd, SockAddr, Result) -->
-	{ mksockaddr_struct(SockAddr, Ptr, Len) },
-	connect0(Fd, Ptr, Len, Res),
-	( { Res = 0 } ->
-		{ Result = ok }
+connect(Fd, SockAddr, Result, !IO) :-
+    mksockaddr_struct(SockAddr, Ptr, Len),
+    connect0(Fd, Ptr, Len, Res, !IO),
+    ( Res = 0 ->
+        Result = ok
  	;
-		errno(Err),
-		{ Result = error(Err) }
+        errno(Err, !IO),
+        Result = error(Err)
  	).

-:- pred connect0(fd, sockaddr_ptr, int, int, io__state, io__state).
-:- mode connect0(in, in, in, out, di, uo) is det.
-
-:- pragma c_code(connect0(Fd::in, Addr::in, Len::in, Res::out, IO0::di, IO::uo),
-		[will_not_call_mercury, thread_safe], "{
+:- pred connect0(fd::in, sockaddr_ptr::in, int::in, int::out,
+    io::di, io::uo) is det.
+:- pragma foreign_proc("C",
+    connect0(Fd::in, Addr::in, Len::in, Res::out, IO0::di, IO::uo),
+    [promise_pure, will_not_call_mercury, thread_safe, tabled_for_io],
+"
  	Res = connect(Fd, (struct sockaddr *) Addr, Len);
  	IO = IO0;
-}").
+").

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

-listen(Fd, N, Result) -->
-	listen0(Fd, N, Res0),
-	( { Res0 = 0 } ->
-		{ Result = ok }
+listen(Fd, N, Result, !IO) :-
+    listen0(Fd, N, Res0, !IO),
+    ( Res0 = 0 ->
+        Result = ok
  	;
-		errno(Errno),
-		{ Result = error(Errno) }
+        errno(Errno, !IO),
+        Result = error(Errno)
  	).

-:- pred listen0(fd, int, int, io__state, io__state).
-:- mode listen0(in, in, out, di, uo) is det.
-
-:- pragma c_code(listen0(Fd::in, N::in, Res::out, IO0::di, IO::uo),
-		[will_not_call_mercury, thread_safe], "{
+:- pred listen0(fd::in, int::in, int::out, io::di, io::uo) is det.
+:- pragma foreign_proc("C",
+    listen0(Fd::in, N::in, Res::out, IO0::di, IO::uo),
+    [promise_pure, will_not_call_mercury, thread_safe, tabled_for_io],
+"
  	Res = listen(Fd, N);
  	IO = IO0;
-}").
+").

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

-accept(Fd, Result) -->
-	accept0(Fd, Ptr, NewFd),
-	( { NewFd < 0 } ->
-		errno(Errno),
-		{ Result = error(Errno) }
+accept(Fd, Result, !IO) :-
+    accept0(Fd, _Ptr, NewFd, !IO),
+    ( NewFd < 0 ->
+        errno(Errno, !IO),
+        Result = error(Errno)
  	;
-		% { cons_sockaddr(Ptr, SockAddr) },
-		% { Result = ok(SockAddr - fd(NewFd)) }
-		{ Result = ok(fd(NewFd)) }
+        % cons_sockaddr(Ptr, SockAddr),
+        % Result = ok(SockAddr - fd(NewFd))
+        Result = ok(fd(NewFd))
  	).

-:- pred accept0(fd, sockaddr_ptr, int, io__state, io__state).
-:- mode accept0(in, out, out, di, uo) is det.
+:- pred accept0(fd::in, sockaddr_ptr::out, int::out, io::di, io::uo)
+    is det.

-:- pragma c_code(accept0(Fd::in, Ptr::out, NewFd::out, IO0::di, IO::uo), 
-		[will_not_call_mercury, thread_safe], "{
+:- pragma foreign_proc("C",
+    accept0(Fd::in, Ptr::out, NewFd::out, IO0::di, IO::uo), 
+    [promise_pure, will_not_call_mercury, thread_safe, tabled_for_io],
+"
  	struct sockaddr_in *ptr;
  	int	len = sizeof(struct sockaddr_in);

@@ -229,23 +243,26 @@

  	NewFd = accept(Fd, ptr, &len);
  	IO = IO0;
-}").
-
-:- pred cons_sockaddr(sockaddr_ptr, sockaddr).
-:- mode cons_sockaddr(in, out) is det.
+").

-:- pragma c_code(cons_sockaddr(Ptr::in, Sok::out),
-		[will_not_call_mercury, thread_safe], "{
+:- pred cons_sockaddr(sockaddr_ptr::in, sockaddr::out) is det.
+:- pragma foreign_proc("C",
+    cons_sockaddr(Ptr::in, Sok::out),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
  	struct sockaddr_in *ptr;

  	ptr = (struct sockaddr_in *) Ptr;

  	if (ptr->sin_family == AF_INET) {
  		MR_incr_hp(Ptr, 2);
-		field(MR_mktag(0), Ptr, 0) = ntohs(ptr->sin_port);
-		field(MR_mktag(0), Ptr, 1) = ptr->sin_addr.s_addr;
+        MR_field(MR_mktag(0), Ptr, 0) = ntohs(ptr->sin_port);
+        MR_field(MR_mktag(0), Ptr, 1) = ptr->sin_addr.s_addr;
  	} else {
  		MR_fatal_error(""cons_sockaddr: unknown type"");
  	}
-}").
+").

+%-----------------------------------------------------------------------------%
+:- end_module posix.socket.
+%-----------------------------------------------------------------------------%
Index: posix.stat.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/extras/posix/posix.stat.m,v
retrieving revision 1.3
diff -u -b -r1.3 posix.stat.m
--- posix.stat.m	15 Jan 2004 10:30:19 -0000	1.3
+++ posix.stat.m	23 Apr 2007 08:36:33 -0000
@@ -1,18 +1,23 @@
-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et
+%-----------------------------------------------------------------------------%
  % Copyright (C) 2001, 2004 The University of Melbourne.
  % This file may only be copied under the terms of the GNU Library General
  % Public License - see the file COPYING.LIB in the Mercury distribution.
-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
  %
-% module: posix__stat.m
-% main author: Michael Day <miked at lendtech.com.au>
+% Module: posix.stat.
+% Main author: Michael Day <miked at lendtech.com.au>
  %
-%------------------------------------------------------------------------------%
-:- module posix__stat.
+%-----------------------------------------------------------------------------%

+:- module posix.stat.
  :- interface.

-:- import_module string, time.
+:- import_module string.
+:- import_module time.
+
+%-----------------------------------------------------------------------------%

  :- type file_type
      --->    file
@@ -21,15 +26,14 @@
      ;	    character_device
      ;	    block_device
      ;	    fifo
-    ;	    unknown
-    .
+    ;       unknown.

  :- type stat.

  :- func dev(stat) = dev_t.
  :- func ino(stat) = ino_t.
  :- func mode(stat) = mode_t.
-:- func file_type(stat) = posix__stat__file_type.
+:- func file_type(stat) = posix.stat.file_type.
  :- func nlink(stat) = nlink_t.
  :- func uid(stat) = uid_t.
  :- func gid(stat) = gid_t.
@@ -41,41 +45,51 @@
  :- func mtime(stat) = time_t.
  :- func ctime(stat) = time_t.

-:- pred stat(string, posix__result(stat), io__state, io__state).
-:- mode stat(in, out, di, uo) is det.
+:- pred stat(string::in, posix.result(stat)::out, io::di, io::uo) is det.

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

  :- implementation.

-:- pragma c_header_code("
+:- pragma foreign_decl("C", "
  	#include <sys/types.h>
  	#include <sys/stat.h>
  	#include <unistd.h>
  ").

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+
+:- interface.
+
+:- pragma foreign_type("C", stat, "struct stat *", [can_pass_as_mercury_type]).
+
+%-----------------------------------------------------------------------------%
+
+:- implementation.

-:- type stat ---> stat(c_pointer).
+%-----------------------------------------------------------------------------%

-stat(Path, Result) -->
-	stat0(Path, Res, Stat),
-	( if { Res = 0 } then
-		{ Result = ok(Stat) }
+stat(Path, Result, !IO) :-
+    stat0(Path, Res, Stat, !IO),
+    ( if Res = 0 then
+        Result = ok(Stat)
  	else
-		errno(Err),
-		{ Result = error(Err) }
+        errno(Err, !IO),
+        Result = error(Err)
  	).

-:- pred stat0(string, int, stat, io__state, io__state).
-:- mode stat0(in, out, out, di, uo) is det.
-
-:- pragma c_code(stat0(Path::in, Res::out, Stat::out, IO0::di, IO::uo),
-		[will_not_call_mercury, thread_safe], "{
-	Stat = (MR_Word) MR_GC_NEW(struct stat);
-	Res = stat(Path, (struct stat *)Stat);
+:- pred stat0(string::in, int::out, stat::out, io::di, io::uo) is det.
+:- pragma foreign_proc("C",
+    stat0(Path::in, Res::out, Stat::out, IO0::di, IO::uo),
+    [promise_pure, will_not_call_mercury, thread_safe, tabled_for_io],
+"
+    Stat = MR_GC_NEW(struct stat);
+    Res = stat(Path, Stat);
  	IO = IO0;
-}").
+").
+
+%-----------------------------------------------------------------------------%

  file_type(Stat) =
  	( if is_slnk(Mode) then symbolic_link
@@ -86,101 +100,153 @@
  	else if is_fifo(Mode) then fifo
  	else unknown ) :- Mode = Stat ^ (mode).

-:- pred is_slnk(mode_t).
-:- mode is_slnk(in) is semidet.
-
-:- pragma c_code(is_slnk(Mode::in), [will_not_call_mercury, thread_safe], "
-	SUCCESS_INDICATOR = S_ISLNK(Mode); ").
-
-:- pred is_reg(mode_t).
-:- mode is_reg(in) is semidet.
-
-:- pragma c_code(is_reg(Mode::in), [will_not_call_mercury, thread_safe], "
-	SUCCESS_INDICATOR = S_ISREG(Mode); ").
-
-:- pred is_dir(mode_t).
-:- mode is_dir(in) is semidet.
-
-:- pragma c_code(is_dir(Mode::in), [will_not_call_mercury, thread_safe], "
-	SUCCESS_INDICATOR = S_ISDIR(Mode); ").
-
-:- pred is_chr(mode_t).
-:- mode is_chr(in) is semidet.
+:- pred is_slnk(mode_t::in) is semidet.
+:- pragma foreign_proc("C",
+    is_slnk(Mode::in),
+    [promise_pure, will_not_call_mercury, thread_safe],
+ "
+    SUCCESS_INDICATOR = S_ISLNK(Mode);
+ ").
+
+:- pred is_reg(mode_t::in) is semidet.
+:- pragma foreign_proc("C",
+    is_reg(Mode::in),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
+    SUCCESS_INDICATOR = S_ISREG(Mode);
+").

-:- pragma c_code(is_chr(Mode::in), [will_not_call_mercury, thread_safe], "
-	SUCCESS_INDICATOR = S_ISCHR(Mode); ").
+:- pred is_dir(mode_t::in) is semidet.
+:- pragma foreign_proc("C",
+    is_dir(Mode::in),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
+    SUCCESS_INDICATOR = S_ISDIR(Mode);
+").

-:- pred is_blk(mode_t).
-:- mode is_blk(in) is semidet.
+:- pred is_chr(mode_t::in) is semidet.
+:- pragma foreign_proc("C",
+    is_chr(Mode::in),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
+    SUCCESS_INDICATOR = S_ISCHR(Mode);
+").

-:- pragma c_code(is_blk(Mode::in), [will_not_call_mercury, thread_safe], "
-	SUCCESS_INDICATOR = S_ISBLK(Mode); ").
+:- pred is_blk(mode_t::in) is semidet.
+:- pragma foreign_proc("C",
+    is_blk(Mode::in),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
+    SUCCESS_INDICATOR = S_ISBLK(Mode);
+").

-:- pred is_fifo(mode_t).
-:- mode is_fifo(in) is semidet.
+:- pred is_fifo(mode_t::in) is semidet.
+:- pragma foreign_proc("C",
+    is_fifo(Mode::in),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
+    SUCCESS_INDICATOR = S_ISFIFO(Mode);
+").

-:- pragma c_code(is_fifo(Mode::in), [will_not_call_mercury, thread_safe], "
-	SUCCESS_INDICATOR = S_ISFIFO(Mode); ").
+%-----------------------------------------------------------------------------%

-:- pragma c_code(dev(S::in) = (Dev::out),
-	[will_not_call_mercury, thread_safe],
-	"Dev = ((struct stat *)S)->st_dev; ").
+:- pragma foreign_proc("C",
+    dev(S::in) = (Dev::out),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
+    Dev = S->st_dev;
+").

-:- pragma c_code(ino(S::in) = (Ino::out),
-	[will_not_call_mercury, thread_safe],
-	"Ino = ((struct stat *)S)->st_ino; ").
+:- pragma foreign_proc("C",
+    ino(S::in) = (Ino::out),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
+    Ino = S->st_ino;
+").

-:- pragma c_code(mode(S::in) = (Mode::out),
-	[will_not_call_mercury, thread_safe],
-	"Mode = ((struct stat *)S)->st_mode; ").
+:- pragma foreign_proc("C",
+    mode(S::in) = (Mode::out),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
+    Mode = S->st_mode;
+").

-:- pragma c_code(nlink(S::in) = (Nlink::out),
-	[will_not_call_mercury, thread_safe],
-	"Nlink = ((struct stat *)S)->st_nlink; ").
+:- pragma foreign_proc("C",
+    nlink(S::in) = (Nlink::out),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
+    Nlink = S->st_nlink;
+").

-:- pragma c_code(uid(S::in) = (Uid::out),
-	[will_not_call_mercury, thread_safe],
-	"Uid = ((struct stat *)S)->st_uid; ").
+:- pragma foreign_proc("C",
+    uid(S::in) = (Uid::out),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
+    Uid = S->st_uid;
+").

-:- pragma c_code(gid(S::in) = (Gid::out),
-	[will_not_call_mercury, thread_safe],
-	"Gid = ((struct stat *)S)->st_gid; ").
+:- pragma foreign_proc("C",
+    gid(S::in) = (Gid::out),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
+    Gid = S->st_gid;
+").

-:- pragma c_code(rdev(S::in) = (Rdev::out),
-	[will_not_call_mercury, thread_safe],
-	"Rdev = ((struct stat *)S)->st_rdev; ").
+:- pragma foreign_proc("C",
+    rdev(S::in) = (Rdev::out),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
+    Rdev = S->st_rdev;
+").

  size(S) = off(integer(size0(S))).

  :- func size0(stat) = int.
+:- pragma foreign_proc("C",
+    size0(S::in) = (Size::out),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
+    Size = S->st_size;
+").

-:- pragma c_code(size0(S::in) = (Size::out),
-	[will_not_call_mercury, thread_safe],
-	"Size = ((struct stat *)S)->st_size; ").
-
-:- pragma c_code(blksize(S::in) = (Blksize::out),
-	[will_not_call_mercury, thread_safe],
-	"Blksize = ((struct stat *)S)->st_blksize; ").
+:- pragma foreign_proc("C",
+    blksize(S::in) = (Blksize::out),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
+    Blksize = S->st_blksize;
+").

  blocks(S) = blkcnt(integer(blocks0(S))).

  :- func blocks0(stat) = int.
+:- pragma foreign_proc("C",
+    blocks0(S::in) = (Blocks::out),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
+    Blocks = S->st_blocks;
+").

-:- pragma c_code(blocks0(S::in) = (Blocks::out),
-	[will_not_call_mercury, thread_safe],
-	"Blocks = ((struct stat *)S)->st_blocks; ").
-
-:- pragma c_code(atime(S::in) = (Atime::out),
-	[will_not_call_mercury, thread_safe],
-	"Atime = ((struct stat *)S)->st_atime; ").
-
-:- pragma c_code(mtime(S::in) = (Mtime::out),
-	[will_not_call_mercury, thread_safe],
-	"Mtime = ((struct stat*)S)->st_mtime; ").
-
-:- pragma c_code(ctime(S::in) = (Ctime::out),
-	[will_not_call_mercury, thread_safe],
-	"Ctime = ((struct stat *)S)->st_ctime; ").
+:- pragma foreign_proc("C",
+    atime(S::in) = (Atime::out),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
+    Atime = S->st_atime;
+").

-%------------------------------------------------------------------------------%
+:- pragma foreign_proc("C",
+    mtime(S::in) = (Mtime::out),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
+    Mtime = S->st_mtime;
+").
+
+:- pragma foreign_proc("C",
+    ctime(S::in) = (Ctime::out),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
+    Ctime = S->st_ctime;
+").

+%------------------------------------------------------------------------------%
+:- end_module posix.stat.
+%------------------------------------------------------------------------------%
Index: posix.wait.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/extras/posix/posix.wait.m,v
retrieving revision 1.3
diff -u -b -r1.3 posix.wait.m
--- posix.wait.m	7 Sep 2001 01:20:42 -0000	1.3
+++ posix.wait.m	23 Apr 2007 08:36:33 -0000
@@ -1,138 +1,146 @@
-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et
+%-----------------------------------------------------------------------------%
  % Copyright (C) 2001 The University of Melbourne.
  % This file may only be copied under the terms of the GNU Library General
  % Public License - see the file COPYING.LIB in the Mercury distribution.
-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
  %
-% module: posix__wait.m
-% main author: Michael Day <miked at lendtech.com.au>
+% Module: posix.wait.m
+% Main author: Michael Day <miked at lendtech.com.au>
  %
-%------------------------------------------------------------------------------%
-:- module posix__wait.
+%-----------------------------------------------------------------------------%

+:- module posix.wait.
  :- interface.

+%-----------------------------------------------------------------------------%
+
  :- type wait_for
  	--->	any_child
  	;	child(pid_t)
  	;	child_in_group(pid_t)
-	;	child_in_same_group
-	.
+    ;       child_in_same_group.

  :- type status
  	--->	exit(int)
-	;	signal(int)
-	.
+    ;       signal(int).

-:- pred wait(posix__result({pid_t, status}), io__state, io__state).
-:- mode wait(out, di, uo) is det.
+:- pred wait(posix.result({pid_t, status})::out, io::di, io::uo) is det.

-:- pred waitpid(wait_for, posix__result({pid_t, status}), io__state, io__state).
-:- mode waitpid(in, out, di, uo) is det.
+:- pred waitpid(wait_for::in, posix.result({pid_t, status})::out,
+    io::di, io::uo) is det.

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

  :- implementation.

  :- import_module int.

-:- pragma c_header_code("
+:- pragma foreign_decl("C", "
  	#include <sys/types.h>
  	#include <sys/wait.h>
  ").

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

-wait(Result) -->
-	wait0(Pid, Status),
-	( if { Pid < 0 } then
-		errno(Err),
-		{ Result = error(Err) }
+wait(Result, !IO) :-
+    wait0(Pid, Status, !IO),
+    ( if Pid < 0 then
+        errno(Err, !IO),
+        Result = error(Err)
  	else
-		{ if if_exited(Status) then
+        ( if if_exited(Status) then
  			Result = ok({pid(Pid), exit(exit_status(Status))})
  		else
  			Result = ok({pid(Pid), signal(term_sig(Status))})
-		}
+        )
  	).

-:- pred wait0(int, int, io__state, io__state).
-:- mode wait0(out, out, di, uo) is det.
-
-:- pragma c_code(wait0(Pid::out, Status::out, IO0::di, IO::uo),
-		[will_not_call_mercury, thread_safe], "{
+:- pred wait0(int::out, int::out, io::di, io::uo) is det.
+:- pragma foreign_proc("C",
+    wait0(Pid::out, Status::out, IO0::di, IO::uo),
+    [promise_pure, will_not_call_mercury, thread_safe, tabled_for_io],
+"
  	int status;
  	Pid = wait(&status);
  	Status = status;
  	IO = IO0;
-}").
+").

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

-waitpid(WaitFor, Result) -->
+waitpid(WaitFor, Result, !IO) :-
  	(
-		{ WaitFor = any_child, Pid0 = pid(-1) }
+        WaitFor = any_child,
+        Pid0 = pid(-1)
  	;
-		{ WaitFor = child(Pid0) }
+        WaitFor = child(Pid0)
  	;
-		{ WaitFor = child_in_group(pid(Group)), Pid0 = pid(-Group) }
+        WaitFor = child_in_group(pid(Group)),
+        Pid0 = pid(-Group)
  	;
-		{ WaitFor = child_in_same_group, Pid0 = pid(0) }
+        WaitFor = child_in_same_group,
+        Pid0 = pid(0)
  	),
-	waitpid0(Pid0, Pid, Status),
-	( if { Pid < 0 } then
-		errno(Err),
-		{ Result = error(Err) }
+    waitpid0(Pid0, Pid, Status, !IO),
+    ( if Pid < 0 then
+        errno(Err, !IO),
+        Result = error(Err)
  	else
-		{ if if_exited(Status) then
+        ( if if_exited(Status) then
  			Result = ok({pid(Pid), exit(exit_status(Status))})
  		else
  			Result = ok({pid(Pid), signal(term_sig(Status))})
-		}
+        )
  	).

-:- pred waitpid0(pid_t, int, int, io__state, io__state).
-:- mode waitpid0(in, out, out, di, uo) is det.
-
-:- pragma c_code(waitpid0(Pid0::in, Pid::out, Status::out, IO0::di, IO::uo),
-		[will_not_call_mercury, thread_safe], "{
+:- pred waitpid0(pid_t::in, int::out, int::out, io::di, io::uo) is det.
+:- pragma foreign_proc("C",
+    waitpid0(Pid0::in, Pid::out, Status::out, IO0::di, IO::uo),
+    [promise_pure, will_not_call_mercury, thread_safe, tabled_for_io],
+"
  	int status;
  	Pid = waitpid(Pid0, &status, 0);
  	Status = status;
  	IO = IO0;
-}").
-
-%------------------------------------------------------------------------------%
+").

-:- pred if_exited(int).
-:- mode if_exited(in) is semidet.
+%-----------------------------------------------------------------------------%

-:- pragma c_code(if_exited(Status::in), [will_not_call_mercury, thread_safe], "
+:- pred if_exited(int::in) is semidet.
+:- pragma foreign_proc("C",
+    if_exited(Status::in),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
  	SUCCESS_INDICATOR = WIFEXITED(Status);
  ").

-:- pred if_signaled(int).
-:- mode if_signaled(in) is semidet.
-
-:- pragma c_code(if_signaled(Status::in), [will_not_call_mercury, thread_safe], "
+:- pred if_signaled(int::in) is semidet.
+:- pragma foreign_proc("C",
+    if_signaled(Status::in),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
  	SUCCESS_INDICATOR = WIFSIGNALED(Status);
  ").

  :- func exit_status(int) = int.
-
-:- pragma c_code(exit_status(Status::in) = (ExitCode::out),
-		[will_not_call_mercury, thread_safe],
+:- pragma foreign_proc("C",
+    exit_status(Status::in) = (ExitCode::out),
+    [promise_pure, will_not_call_mercury, thread_safe],
  "
  	ExitCode = WEXITSTATUS(Status);
  ").

  :- func term_sig(int) = int.
-
-:- pragma c_code(term_sig(Status::in) = (ExitCode::out),
-		[will_not_call_mercury, thread_safe],
+:- pragma foreign_proc("C",
+    term_sig(Status::in) = (ExitCode::out),
+    [promise_pure, will_not_call_mercury, thread_safe],
  "
  	ExitCode = WTERMSIG(Status);
  ").

-%------------------------------------------------------------------------------%
-
+%-----------------------------------------------------------------------------%
+:- end_module posix.wait.
+%-----------------------------------------------------------------------------%
Index: posix.write.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/extras/posix/posix.write.m,v
retrieving revision 1.2
diff -u -b -r1.2 posix.write.m
--- posix.write.m	18 Oct 1999 00:50:26 -0000	1.2
+++ posix.write.m	23 Apr 2007 08:36:33 -0000
@@ -1,57 +1,60 @@
-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et
+%-----------------------------------------------------------------------------%
  % Copyright (C) 1999 The University of Melbourne.
  % This file may only be copied under the terms of the GNU Library General
  % Public License - see the file COPYING.LIB in the Mercury distribution.
-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
  %
-% module: posix__write.m
-% main author: conway at cs.mu.oz.au
+% Module: posix.write.
+% 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).
-:- mode write(in, in, in, out, di, uo) is det.
+%-----------------------------------------------------------------------------%

-%------------------------------------------------------------------------------%
+:- pred write(fd::in, int::in, text::in, posix.result(int)::out,
+    io::di, io::uo) is det.
+
+%-----------------------------------------------------------------------------%

  :- implementation.

  :- import_module int.

-:- pragma c_header_code("
+:- pragma foreign_decl("C", "
  	#include <unistd.h>
  	#include ""text_header.h""
  ").

-%------------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

-write(Fd, ToWrite, Text, Result) -->
-	write0(Fd, ToWrite, Text, Res),
-	( { Res < 0 } ->
-		errno(Err),
-		{ Result = error(Err) }
+write(Fd, ToWrite, Text, Result, !IO) :-
+    write0(Fd, ToWrite, Text, Res, !IO),
+    ( Res < 0 ->
+        errno(Err, !IO),
+        Result = error(Err)
  	;
-		{ Result = ok(Res) }
+        Result = ok(Res)
  	).

-:- pred write0(fd, int, text, int, io__state, io__state).
-:- mode write0(in, in, in, out, di, uo) is det.
-
-:- pragma c_code(write0(Fd::in, ToWrite::in, Text::in, Res::out,
-		IO0::di, IO::uo), [will_not_call_mercury, thread_safe], "{
+:- pred write0(fd::in, int::in, text::in, int::out, io::di, io::uo) is det.
+:- pragma foreign_proc("C",
+    write0(Fd::in, ToWrite::in, Text::in, Res::out, IO0::di, IO::uo),
+    [promise_pure, will_not_call_mercury, thread_safe, tabled_for_io],
+"
  	ME_Text *txtptr;

  	txtptr = (ME_Text *) Text;
-
  	Res = write(Fd, txtptr->data, ToWrite);
-
  	IO = IO0;
-}").
-
-%------------------------------------------------------------------------------%
+").

+%-----------------------------------------------------------------------------%
+:- end_module posix.write.
+%-----------------------------------------------------------------------------%

--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list