Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/branches/idlbasis-devel/src/system/Basis/Implementation/Posix/posix-io.sml
ViewVC logotype

Annotation of /sml/branches/idlbasis-devel/src/system/Basis/Implementation/Posix/posix-io.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 848 - (view) (download)

1 : monnier 416 (* posix-io.sml
2 :     *
3 :     * COPYRIGHT (c) 1995 AT&T Bell Laboratories.
4 :     *
5 :     * Structure for POSIX 1003.1 primitive I/O operations
6 :     *
7 :     *)
8 :    
9 :     local
10 :     structure SysWord = SysWordImp
11 :     structure Int = IntImp
12 :     in
13 :     structure POSIX_IO =
14 :     struct
15 :    
16 :     structure FS = POSIX_FileSys
17 :    
18 :     structure OM : sig
19 :     datatype open_mode = O_RDONLY | O_WRONLY | O_RDWR
20 :     end = FS
21 :     open OM
22 :    
23 :     type word = SysWord.word
24 :     type s_int = SysInt.int
25 :    
26 :     val ++ = SysWord.orb
27 :     val & = SysWord.andb
28 :     infix ++ &
29 :    
30 :     fun cfun x = CInterface.c_function "POSIX-IO" x
31 :     val osval : string -> s_int = cfun "osval"
32 :     val w_osval = SysWord.fromInt o osval
33 :     fun fail (fct,msg) = raise Fail ("POSIX_IO."^fct^": "^msg)
34 :    
35 :     type file_desc = FS.file_desc
36 :     type pid = POSIX_Process.pid
37 :    
38 :     val pipe' : unit -> s_int * s_int = cfun "pipe"
39 :     fun pipe () = let
40 :     val (ifd, ofd) = pipe' ()
41 :     in
42 :     {infd = FS.fd ifd, outfd = FS.fd ofd}
43 :     end
44 :    
45 :     val dup' : s_int -> s_int = cfun "dup"
46 :     val dup2' : s_int * s_int -> unit = cfun "dup2"
47 :     fun dup fd = FS.fd(dup' (FS.intOf fd))
48 :     fun dup2 {old, new} = dup2'(FS.intOf old, FS.intOf new)
49 :    
50 :     val close' : s_int -> unit = cfun "close"
51 :     fun close fd = close' (FS.intOf fd)
52 :    
53 :     val read' : int * int -> Word8Vector.vector = cfun "read"
54 :     val readbuf' : int * Word8Array.array * int * int -> int = cfun "readbuf"
55 :     fun readArr (fd, {buf, i, sz=NONE}) = let
56 :     val alen = Word8Array.length buf
57 :     in
58 :     if 0 <= i andalso i <= alen
59 :     then readbuf'(FS.intOf fd, buf, alen - i, i)
60 :     else raise Subscript
61 :     end
62 :     | readArr (fd, {buf, i, sz=SOME sz}) = let
63 :     val alen = Word8Array.length buf
64 :     in
65 :     if 0 <= i andalso 0 <= sz andalso i + sz <= alen
66 :     then readbuf'(FS.intOf fd, buf, sz, i)
67 :     else raise Subscript
68 :     end
69 :     fun readVec (fd,cnt) =
70 :     if cnt < 0 then raise Subscript else read'(FS.intOf fd, cnt)
71 :    
72 :     val writevec' : (int * Word8Vector.vector * int * int) -> int = cfun "writebuf"
73 :     val writearr' : (int * Word8Array.array * int * int) -> int = cfun "writebuf"
74 :     fun writeArr (fd,{buf, i, sz=NONE}) = let
75 :     val alen = Word8Array.length buf
76 :     in
77 :     if 0 <= i andalso i <= alen
78 :     then writearr'(FS.intOf fd, buf, alen-i, i)
79 :     else raise Subscript
80 :     end
81 :     | writeArr (fd,{buf, i, sz=SOME sz}) = let
82 :     val alen = Word8Array.length buf
83 :     in
84 :     if 0 <= i andalso 0 <= sz andalso i + sz <= alen
85 :     then writearr'(FS.intOf fd, buf, sz, i)
86 :     else raise Subscript
87 :     end
88 :    
89 :     fun writeVec (fd,{buf, i, sz=NONE}) = let
90 :     val vlen = Word8Vector.length buf
91 :     in
92 :     if 0 <= i andalso i <= vlen
93 :     then writevec'(FS.intOf fd, buf, vlen-i, i)
94 :     else raise Subscript
95 :     end
96 :     | writeVec (fd,{buf, i, sz=SOME sz}) = let
97 :     val vlen = Word8Vector.length buf
98 :     in
99 :     if 0 <= i andalso 0 <= sz andalso i + sz <= vlen
100 :     then writevec'(FS.intOf fd, buf, sz, i)
101 :     else raise Subscript
102 :     end
103 :    
104 :     datatype whence = SEEK_SET | SEEK_CUR | SEEK_END
105 :     val seek_set = osval "SEEK_SET"
106 :     val seek_cur = osval "SEEK_CUR"
107 :     val seek_end = osval "SEEK_END"
108 :     fun whToWord SEEK_SET = seek_set
109 :     | whToWord SEEK_CUR = seek_cur
110 :     | whToWord SEEK_END = seek_end
111 :     fun whFromWord wh =
112 :     if wh = seek_set then SEEK_SET
113 :     else if wh = seek_cur then SEEK_CUR
114 :     else if wh = seek_end then SEEK_END
115 :     else fail ("whFromWord","unknown whence "^(Int.toString wh))
116 :    
117 :     structure FD =
118 :     struct
119 :     datatype flags = FDF of word
120 :    
121 :     fun fromWord w = FDF w
122 :     fun toWord (FDF w) = w
123 :    
124 :     fun flags ms = FDF(List.foldl (fn (FDF m,acc) => m ++ acc) 0w0 ms)
125 :     fun anySet (FDF m, FDF m') = (m & m') <> 0w0
126 :     fun allSet (FDF m, FDF m') = (m & m') = m
127 :    
128 :     val cloexec = FDF(w_osval "cloexec")
129 :     end
130 :    
131 :     structure O =
132 :     struct
133 :     datatype flags = FS of word
134 :    
135 :     fun fromWord w = FS w
136 :     fun toWord (FS w) = w
137 :    
138 :     fun flags ms = FS(List.foldl (fn (FS m,acc) => m ++ acc) 0w0 ms)
139 :     fun anySet (FS m, FS m') = (m & m') <> 0w0
140 :     fun allSet (FS m, FS m') = (m & m') = m
141 :    
142 :     val append = FS(w_osval "append")
143 :     val dsync = FS(w_osval "dsync")
144 :     val nonblock = FS(w_osval "nonblock")
145 :     val rsync = FS(w_osval "rsync")
146 :     val sync = FS(w_osval "sync")
147 :     end
148 :    
149 :     val fcntl_d : s_int * s_int -> s_int = cfun "fcntl_d"
150 :     val fcntl_gfd : s_int -> word = cfun "fcntl_gfd"
151 :     val fcntl_sfd : (s_int * word) -> unit = cfun "fcntl_sfd"
152 :     val fcntl_gfl : s_int -> (word * word) = cfun "fcntl_gfl"
153 :     val fcntl_sfl : (s_int * word) -> unit = cfun "fcntl_sfl"
154 :     fun dupfd {old, base} = FS.fd (fcntl_d (FS.intOf old, FS.intOf base))
155 :     fun getfd fd = FD.FDF (fcntl_gfd (FS.intOf fd))
156 :     fun setfd (fd, FD.FDF fl) = fcntl_sfd(FS.intOf fd, fl)
157 :     fun getfl fd = let
158 :     val (sts, omode) = fcntl_gfl (FS.intOf fd)
159 :     in
160 :     (O.FS sts, FS.omodeFromWord omode)
161 :     end
162 :     fun setfl (fd, O.FS sts) = fcntl_sfl (FS.intOf fd, sts)
163 :    
164 :     datatype lock_type = F_RDLCK | F_WRLCK | F_UNLCK
165 :    
166 :     structure FLock =
167 :     struct
168 :     datatype flock = FLOCK of {
169 :     l_type : lock_type,
170 :     l_whence : whence,
171 :     l_start : Position.int,
172 :     l_len : Position.int,
173 :     l_pid : pid option
174 :     }
175 :    
176 :     fun flock fv = FLOCK fv
177 :     fun ltype (FLOCK{l_type,...}) = l_type
178 :     fun whence (FLOCK{l_whence,...}) = l_whence
179 :     fun start (FLOCK{l_start,...}) = l_start
180 :     fun len (FLOCK{l_len,...}) = l_len
181 :     fun pid (FLOCK{l_pid,...}) = l_pid
182 :     end
183 :    
184 :     type flock_rep = s_int * s_int * Position.int * Position.int * s_int
185 :    
186 :     val fcntl_l : s_int * s_int * flock_rep -> flock_rep = cfun "fcntl_l"
187 :     val f_getlk = osval "F_GETLK"
188 :     val f_setlk = osval "F_SETLK"
189 :     val f_setlkw = osval "F_SETLKW"
190 :     val f_rdlck = osval "F_RDLCK"
191 :     val f_wrlck = osval "F_WRLCK"
192 :     val f_unlck = osval "F_UNLCK"
193 :    
194 :     fun flockToRep (FLock.FLOCK{l_type,l_whence,l_start,l_len,...}) = let
195 :     fun ltypeOf F_RDLCK = f_rdlck
196 :     | ltypeOf F_WRLCK = f_wrlck
197 :     | ltypeOf F_UNLCK = f_unlck
198 :     in
199 :     (ltypeOf l_type,whToWord l_whence, l_start, l_len, 0)
200 :     end
201 :     fun flockFromRep (usepid,(ltype,whence,start,len,pid)) = let
202 :     fun ltypeOf ltype =
203 :     if ltype = f_rdlck then F_RDLCK
204 :     else if ltype = f_wrlck then F_WRLCK
205 :     else if ltype = f_unlck then F_UNLCK
206 :     else fail ("flockFromRep","unknown lock type "^(Int.toString ltype))
207 :     in
208 :     FLock.FLOCK {
209 :     l_type = ltypeOf ltype,
210 :     l_whence = whFromWord whence,
211 :     l_start = start,
212 :     l_len = len,
213 :     l_pid = if usepid then SOME(POSIX_Process.PID pid) else NONE
214 :     }
215 :     end
216 :    
217 :     fun getlk (fd, flock) =
218 :     flockFromRep(true,fcntl_l(FS.intOf fd,f_getlk,flockToRep flock))
219 :     fun setlk (fd, flock) =
220 :     flockFromRep(false,fcntl_l(FS.intOf fd,f_setlk,flockToRep flock))
221 :     fun setlkw (fd, flock) =
222 :     flockFromRep(false,fcntl_l(FS.intOf fd,f_setlkw,flockToRep flock))
223 :    
224 :     val lseek' : s_int * Position.int * s_int -> Position.int = cfun "lseek"
225 :     fun lseek (fd,offset,whence) = lseek'(FS.intOf fd,offset, whToWord whence)
226 :    
227 :     val fsync' : s_int -> unit = cfun "fsync"
228 :     fun fsync fd = fsync' (FS.intOf fd)
229 :    
230 :     end (* structure POSIX_IO *)
231 :     end
232 :    

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0