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 867 - (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 : blume 867 val all = FDF (SysWord.notb 0w0)
125 :    
126 : monnier 416 fun flags ms = FDF(List.foldl (fn (FDF m,acc) => m ++ acc) 0w0 ms)
127 : blume 867 fun intersect ms = FDF(List.foldl (fn (FDF m,acc) => m & acc)
128 :     (SysWord.notb 0w0) ms)
129 :     fun clear (FDF m, FDF m') = FDF (SysWord.notb m & m')
130 : monnier 416 fun anySet (FDF m, FDF m') = (m & m') <> 0w0
131 :     fun allSet (FDF m, FDF m') = (m & m') = m
132 :    
133 :     val cloexec = FDF(w_osval "cloexec")
134 :     end
135 :    
136 :     structure O =
137 :     struct
138 :     datatype flags = FS of word
139 :    
140 :     fun fromWord w = FS w
141 :     fun toWord (FS w) = w
142 :    
143 : blume 867 val all = FS (SysWord.notb 0w0) (* too much?? *)
144 :    
145 : monnier 416 fun flags ms = FS(List.foldl (fn (FS m,acc) => m ++ acc) 0w0 ms)
146 : blume 867 fun intersect ms = FS(List.foldl (fn (FS m,acc) => m & acc)
147 :     (SysWord.notb 0w0) ms)
148 :     fun clear (FS m, FS m') = FS (SysWord.notb m & m')
149 : monnier 416 fun anySet (FS m, FS m') = (m & m') <> 0w0
150 :     fun allSet (FS m, FS m') = (m & m') = m
151 :    
152 :     val append = FS(w_osval "append")
153 :     val dsync = FS(w_osval "dsync")
154 :     val nonblock = FS(w_osval "nonblock")
155 :     val rsync = FS(w_osval "rsync")
156 :     val sync = FS(w_osval "sync")
157 :     end
158 :    
159 :     val fcntl_d : s_int * s_int -> s_int = cfun "fcntl_d"
160 :     val fcntl_gfd : s_int -> word = cfun "fcntl_gfd"
161 :     val fcntl_sfd : (s_int * word) -> unit = cfun "fcntl_sfd"
162 :     val fcntl_gfl : s_int -> (word * word) = cfun "fcntl_gfl"
163 :     val fcntl_sfl : (s_int * word) -> unit = cfun "fcntl_sfl"
164 :     fun dupfd {old, base} = FS.fd (fcntl_d (FS.intOf old, FS.intOf base))
165 :     fun getfd fd = FD.FDF (fcntl_gfd (FS.intOf fd))
166 :     fun setfd (fd, FD.FDF fl) = fcntl_sfd(FS.intOf fd, fl)
167 :     fun getfl fd = let
168 :     val (sts, omode) = fcntl_gfl (FS.intOf fd)
169 :     in
170 :     (O.FS sts, FS.omodeFromWord omode)
171 :     end
172 :     fun setfl (fd, O.FS sts) = fcntl_sfl (FS.intOf fd, sts)
173 :    
174 :     datatype lock_type = F_RDLCK | F_WRLCK | F_UNLCK
175 :    
176 :     structure FLock =
177 :     struct
178 :     datatype flock = FLOCK of {
179 :     l_type : lock_type,
180 :     l_whence : whence,
181 :     l_start : Position.int,
182 :     l_len : Position.int,
183 :     l_pid : pid option
184 :     }
185 :    
186 : blume 867 fun flock { ltype, whence, start, len, pid } =
187 :     FLOCK { l_type = ltype, l_whence = whence, l_start = start,
188 :     l_len = len, l_pid = pid }
189 : monnier 416 fun ltype (FLOCK{l_type,...}) = l_type
190 :     fun whence (FLOCK{l_whence,...}) = l_whence
191 :     fun start (FLOCK{l_start,...}) = l_start
192 :     fun len (FLOCK{l_len,...}) = l_len
193 :     fun pid (FLOCK{l_pid,...}) = l_pid
194 :     end
195 :    
196 :     type flock_rep = s_int * s_int * Position.int * Position.int * s_int
197 :    
198 :     val fcntl_l : s_int * s_int * flock_rep -> flock_rep = cfun "fcntl_l"
199 :     val f_getlk = osval "F_GETLK"
200 :     val f_setlk = osval "F_SETLK"
201 :     val f_setlkw = osval "F_SETLKW"
202 :     val f_rdlck = osval "F_RDLCK"
203 :     val f_wrlck = osval "F_WRLCK"
204 :     val f_unlck = osval "F_UNLCK"
205 :    
206 :     fun flockToRep (FLock.FLOCK{l_type,l_whence,l_start,l_len,...}) = let
207 :     fun ltypeOf F_RDLCK = f_rdlck
208 :     | ltypeOf F_WRLCK = f_wrlck
209 :     | ltypeOf F_UNLCK = f_unlck
210 :     in
211 :     (ltypeOf l_type,whToWord l_whence, l_start, l_len, 0)
212 :     end
213 :     fun flockFromRep (usepid,(ltype,whence,start,len,pid)) = let
214 :     fun ltypeOf ltype =
215 :     if ltype = f_rdlck then F_RDLCK
216 :     else if ltype = f_wrlck then F_WRLCK
217 :     else if ltype = f_unlck then F_UNLCK
218 :     else fail ("flockFromRep","unknown lock type "^(Int.toString ltype))
219 :     in
220 :     FLock.FLOCK {
221 :     l_type = ltypeOf ltype,
222 :     l_whence = whFromWord whence,
223 :     l_start = start,
224 :     l_len = len,
225 :     l_pid = if usepid then SOME(POSIX_Process.PID pid) else NONE
226 :     }
227 :     end
228 :    
229 :     fun getlk (fd, flock) =
230 :     flockFromRep(true,fcntl_l(FS.intOf fd,f_getlk,flockToRep flock))
231 :     fun setlk (fd, flock) =
232 :     flockFromRep(false,fcntl_l(FS.intOf fd,f_setlk,flockToRep flock))
233 :     fun setlkw (fd, flock) =
234 :     flockFromRep(false,fcntl_l(FS.intOf fd,f_setlkw,flockToRep flock))
235 :    
236 :     val lseek' : s_int * Position.int * s_int -> Position.int = cfun "lseek"
237 :     fun lseek (fd,offset,whence) = lseek'(FS.intOf fd,offset, whToWord whence)
238 :    
239 :     val fsync' : s_int -> unit = cfun "fsync"
240 :     fun fsync fd = fsync' (FS.intOf fd)
241 :    
242 : blume 867 fun mkBinReader _ = raise Fail "mkBinReader not implemented"
243 :     fun mkTextReader _ = raise Fail "mkTextReader not implemented"
244 :     fun mkBinWriter _ = raise Fail "mkBinWriter not implemented"
245 :     fun mkTextWriter _ = raise Fail "mkTextWriter not implemented"
246 :    
247 : monnier 416 end (* structure POSIX_IO *)
248 :     end

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