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/blume-private-devel/src/system/Basis/Implementation/Posix/posix-io.sml
ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1453 - (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 : mblume 1381 fun readArr (fd, asl) = let
56 :     val (buf, i, len) = Word8ArraySlice.base asl
57 :     in
58 :     readbuf' (FS.intOf fd, buf, len, i)
59 :     end
60 : monnier 416 fun readVec (fd,cnt) =
61 : mblume 1381 if cnt < 0 then raise Size else read'(FS.intOf fd, cnt)
62 : monnier 416
63 :     val writevec' : (int * Word8Vector.vector * int * int) -> int = cfun "writebuf"
64 :     val writearr' : (int * Word8Array.array * int * int) -> int = cfun "writebuf"
65 : mblume 1381 fun writeArr (fd, asl) = let
66 :     val (buf, i, len) = Word8ArraySlice.base asl
67 :     in
68 :     writearr' (FS.intOf fd, buf, len, i)
69 :     end
70 :    
71 :     fun writeVec (fd, vsl) = let
72 :     val (buf, i, len) = Word8VectorSlice.base vsl
73 :     in
74 :     writevec' (FS.intOf fd, buf, len, i)
75 :     end
76 :    
77 : monnier 416 datatype whence = SEEK_SET | SEEK_CUR | SEEK_END
78 :     val seek_set = osval "SEEK_SET"
79 :     val seek_cur = osval "SEEK_CUR"
80 :     val seek_end = osval "SEEK_END"
81 :     fun whToWord SEEK_SET = seek_set
82 :     | whToWord SEEK_CUR = seek_cur
83 :     | whToWord SEEK_END = seek_end
84 :     fun whFromWord wh =
85 :     if wh = seek_set then SEEK_SET
86 :     else if wh = seek_cur then SEEK_CUR
87 :     else if wh = seek_end then SEEK_END
88 :     else fail ("whFromWord","unknown whence "^(Int.toString wh))
89 :    
90 :     structure FD =
91 :     struct
92 : mblume 1453 local structure BF = BitFlagsFn ()
93 :     in
94 :     open BF
95 :     end
96 : monnier 416
97 : mblume 1453 val cloexec = fromWord (w_osval "cloexec")
98 : monnier 416 end
99 :    
100 :     structure O =
101 :     struct
102 : mblume 1453 local structure BF = BitFlagsFn ()
103 :     in
104 :     open BF
105 :     end
106 : monnier 416
107 : mblume 1453 val append = fromWord (w_osval "append")
108 :     val dsync = fromWord (w_osval "dsync")
109 :     val nonblock = fromWord (w_osval "nonblock")
110 :     val rsync = fromWord (w_osval "rsync")
111 :     val sync = fromWord (w_osval "sync")
112 : monnier 416 end
113 :    
114 :     val fcntl_d : s_int * s_int -> s_int = cfun "fcntl_d"
115 :     val fcntl_gfd : s_int -> word = cfun "fcntl_gfd"
116 :     val fcntl_sfd : (s_int * word) -> unit = cfun "fcntl_sfd"
117 :     val fcntl_gfl : s_int -> (word * word) = cfun "fcntl_gfl"
118 :     val fcntl_sfl : (s_int * word) -> unit = cfun "fcntl_sfl"
119 :     fun dupfd {old, base} = FS.fd (fcntl_d (FS.intOf old, FS.intOf base))
120 : mblume 1453 fun getfd fd = FD.fromWord (fcntl_gfd (FS.intOf fd))
121 :     fun setfd (fd, fl) = fcntl_sfd(FS.intOf fd, FD.toWord fl)
122 : monnier 416 fun getfl fd = let
123 :     val (sts, omode) = fcntl_gfl (FS.intOf fd)
124 :     in
125 : mblume 1453 (O.fromWord sts, FS.omodeFromWord omode)
126 : monnier 416 end
127 : mblume 1453 fun setfl (fd, sts) = fcntl_sfl (FS.intOf fd, O.toWord sts)
128 : monnier 416
129 :     datatype lock_type = F_RDLCK | F_WRLCK | F_UNLCK
130 :    
131 :     structure FLock =
132 :     struct
133 :     datatype flock = FLOCK of {
134 :     l_type : lock_type,
135 :     l_whence : whence,
136 :     l_start : Position.int,
137 :     l_len : Position.int,
138 :     l_pid : pid option
139 :     }
140 :    
141 :     fun flock fv = FLOCK fv
142 :     fun ltype (FLOCK{l_type,...}) = l_type
143 :     fun whence (FLOCK{l_whence,...}) = l_whence
144 :     fun start (FLOCK{l_start,...}) = l_start
145 :     fun len (FLOCK{l_len,...}) = l_len
146 :     fun pid (FLOCK{l_pid,...}) = l_pid
147 :     end
148 :    
149 :     type flock_rep = s_int * s_int * Position.int * Position.int * s_int
150 :    
151 :     val fcntl_l : s_int * s_int * flock_rep -> flock_rep = cfun "fcntl_l"
152 :     val f_getlk = osval "F_GETLK"
153 :     val f_setlk = osval "F_SETLK"
154 :     val f_setlkw = osval "F_SETLKW"
155 :     val f_rdlck = osval "F_RDLCK"
156 :     val f_wrlck = osval "F_WRLCK"
157 :     val f_unlck = osval "F_UNLCK"
158 :    
159 :     fun flockToRep (FLock.FLOCK{l_type,l_whence,l_start,l_len,...}) = let
160 :     fun ltypeOf F_RDLCK = f_rdlck
161 :     | ltypeOf F_WRLCK = f_wrlck
162 :     | ltypeOf F_UNLCK = f_unlck
163 :     in
164 :     (ltypeOf l_type,whToWord l_whence, l_start, l_len, 0)
165 :     end
166 :     fun flockFromRep (usepid,(ltype,whence,start,len,pid)) = let
167 :     fun ltypeOf ltype =
168 :     if ltype = f_rdlck then F_RDLCK
169 :     else if ltype = f_wrlck then F_WRLCK
170 :     else if ltype = f_unlck then F_UNLCK
171 :     else fail ("flockFromRep","unknown lock type "^(Int.toString ltype))
172 :     in
173 :     FLock.FLOCK {
174 :     l_type = ltypeOf ltype,
175 :     l_whence = whFromWord whence,
176 :     l_start = start,
177 :     l_len = len,
178 :     l_pid = if usepid then SOME(POSIX_Process.PID pid) else NONE
179 :     }
180 :     end
181 :    
182 :     fun getlk (fd, flock) =
183 :     flockFromRep(true,fcntl_l(FS.intOf fd,f_getlk,flockToRep flock))
184 :     fun setlk (fd, flock) =
185 :     flockFromRep(false,fcntl_l(FS.intOf fd,f_setlk,flockToRep flock))
186 :     fun setlkw (fd, flock) =
187 :     flockFromRep(false,fcntl_l(FS.intOf fd,f_setlkw,flockToRep flock))
188 :    
189 :     val lseek' : s_int * Position.int * s_int -> Position.int = cfun "lseek"
190 :     fun lseek (fd,offset,whence) = lseek'(FS.intOf fd,offset, whToWord whence)
191 :    
192 :     val fsync' : s_int -> unit = cfun "fsync"
193 :     fun fsync fd = fsync' (FS.intOf fd)
194 :    
195 :     end (* structure POSIX_IO *)
196 :     end
197 :    

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