2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: readFile.c,v 1.11 2000/03/10 15:23:40 simonmar Exp $
6 * hGetContents Runtime Support
12 #if defined(HAVE_WINSOCK_H) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
22 /* Filling up a (block-buffered) buffer, that
23 is completely empty. */
25 readBlock(StgForeignPtr ptr)
27 IOFileObject* fo = (IOFileObject*)ptr;
31 /* Check if someone hasn't zapped us */
32 if ( fo == NULL || fo->fd == -1 )
37 if ( FILEOBJ_IS_EOF(fo) ) {
38 ghc_errtype = ERR_EOF;
43 /* Weird case: buffering has suddenly been turned off.
44 Return non-std value and deal with this case on the Haskell side.
46 if ( FILEOBJ_UNBUFFERED(fo) ) {
50 /* if input stream is connect to an output stream, flush this one first. */
51 if ( fo->connectedTo != NULL &&
52 fo->connectedTo->fd != -1 &&
53 (fo->connectedTo->flags & FILEOBJ_WRITE)
55 rc = flushFile((StgForeignPtr)fo->connectedTo);
57 if (rc < 0) return (rc == FILEOBJ_BLOCKED_WRITE ? FILEOBJ_BLOCKED_CONN_WRITE : rc);
59 /* RW object: flush the (output) buffer first. */
60 if ( FILEOBJ_WRITEABLE(fo) && FILEOBJ_JUST_WRITTEN(fo) && FILEOBJ_NEEDS_FLUSHING(fo) ) {
61 rc = flushBuffer(ptr);
62 if (rc < 0) return rc;
64 fo->flags = (fo->flags & ~FILEOBJ_RW_WRITE) | FILEOBJ_RW_READ;
66 /* return the unread parts of the file buffer..*/
67 if ( fo->flags & FILEOBJ_READ &&
69 fo->bufWPtr > fo->bufRPtr ) {
70 count = fo->bufWPtr - fo->bufRPtr;
76 fprintf(stderr, "rb: %d %d %d\n", fo->bufRPtr, fo->bufWPtr, fo->bufSize);
82 fo->flags & FILEOBJ_WINSOCK ?
83 recv(fd, fo->buf, fo->bufSize, 0) :
84 read(fd, fo->buf, fo->bufSize))) <= 0 ) {
86 read(fd, fo->buf, fo->bufSize))) <= 0 ) {
90 ghc_errtype = ERR_EOF;
93 } else if ( count == -1 && errno == EAGAIN) {
95 return FILEOBJ_BLOCKED_READ;
96 } else if ( count == -1 && errno != EINTR) {
107 /* Filling up a (block-buffered) buffer of length len */
109 /* readChunk(FileObjet *, void *, int)
113 * FILEOBJ_BLOCKED_CONN_WRITE blocking while flushing
114 * buffer of connected handle.
115 * FILEOBJ_BLOCKED_READ didn't read anything; would block
116 * n, where n > 0 read n bytes into buffer.
120 readChunk(StgForeignPtr ptr, StgAddr buf, StgInt off, StgInt len)
122 IOFileObject* fo = (IOFileObject*)ptr;
123 int count=0,rc=0, total_count;
127 /* Check if someone hasn't zapped us */
133 if ( fd == -1 ) /* File has been closed for us */
136 if ( FILEOBJ_IS_EOF(fo) ) {
137 ghc_errtype = ERR_EOF;
142 /* if input stream is connect to an output stream, flush it first */
143 if ( fo->connectedTo != NULL &&
144 fo->connectedTo->fd != -1 &&
145 (fo->connectedTo->flags & FILEOBJ_WRITE)
147 rc = flushFile((StgForeignPtr)fo->connectedTo);
149 if (rc < 0) return (rc == FILEOBJ_BLOCKED_WRITE ? FILEOBJ_BLOCKED_CONN_WRITE : rc);
151 /* RW object: flush the (output) buffer first. */
152 if ( FILEOBJ_WRITEABLE(fo) && FILEOBJ_JUST_WRITTEN(fo) && FILEOBJ_NEEDS_FLUSHING(fo) ) {
153 rc = flushBuffer(ptr);
154 if (rc < 0) return rc;
156 fo->flags = (fo->flags & ~FILEOBJ_RW_WRITE) | FILEOBJ_RW_READ;
158 /* copy the unread parts of the file buffer..*/
159 if ( FILEOBJ_READABLE(fo) &&
161 fo->bufWPtr >= fo->bufRPtr ) {
162 count = ( len < (fo->bufWPtr - fo->bufRPtr)) ? len : (fo->bufWPtr - fo->bufRPtr);
163 memcpy(buf,fo->buf, count);
169 if (len - count <= 0)
180 fo->flags & FILEOBJ_WINSOCK ?
181 recv(fd, p, len, 0) :
182 read(fd, p, len))) <= 0 ) {
184 read(fd, p, len))) <= 0 ) {
192 } else if ( count == -1 && errno == EAGAIN) {
195 return total_count; /* partial read */
197 return FILEOBJ_BLOCKED_READ;
200 } else if ( count == -1 && errno != EINTR) {
207 total_count += count;
212 readLine() tries to fill the buffer up with a line of chars, returning
213 the length of the resulting line.
215 Users of readLine() should immediately afterwards copy out the line
221 readLine(StgForeignPtr ptr)
223 IOFileObject* fo = (IOFileObject*)ptr;
226 /* Check if someone hasn't zapped us */
227 if ( fo == NULL || fo->fd == -1 )
230 if ( FILEOBJ_IS_EOF(fo) ) {
231 ghc_errtype = ERR_EOF;
236 /* Weird case: buffering has been turned off.
237 Return non-std value and deal with this case on the Haskell side.
239 if ( FILEOBJ_UNBUFFERED(fo) ) {
243 /* if input stream is connect to an output stream, flush it first */
244 if ( fo->connectedTo != NULL &&
245 fo->connectedTo->fd != -1 &&
246 (fo->connectedTo->flags & FILEOBJ_WRITE)
248 rc = flushFile((StgForeignPtr)fo->connectedTo);
250 if (rc < 0) return (rc == FILEOBJ_BLOCKED_WRITE ? FILEOBJ_BLOCKED_CONN_WRITE : rc);
252 /* RW object: flush the (output) buffer first. */
253 if ( FILEOBJ_WRITEABLE(fo) && FILEOBJ_JUST_WRITTEN(fo) ) {
254 rc = flushBuffer(ptr);
255 if (rc < 0) return rc;
257 fo->flags = (fo->flags & ~FILEOBJ_RW_WRITE) | FILEOBJ_RW_READ;
259 if ( fo->bufRPtr < 0 || fo->bufRPtr >= fo->bufWPtr ) { /* Buffer is empty */
260 fo->bufRPtr=0; fo->bufWPtr=0;
261 rc = fill_up_line_buffer(fo);
262 if (rc < 0) return rc;
266 unsigned char* s1 = memchr((unsigned char *)fo->buf+fo->bufRPtr, '\n', fo->bufWPtr - fo->bufRPtr);
267 if (s1 != NULL ) { /* Found one */
268 /* Note: we *don't* zero terminate the line */
269 count = s1 - ((unsigned char*)fo->buf + fo->bufRPtr) + 1;
270 fo->bufRPtr += count;
273 /* Just return partial line */
274 count = fo->bufWPtr - fo->bufRPtr;
275 fo->bufRPtr += count;
283 readChar(StgForeignPtr ptr)
285 IOFileObject* fo= (IOFileObject*)ptr;
289 /* Check if someone hasn't zapped us */
290 if ( fo == NULL || fo->fd == -1)
293 if ( FILEOBJ_IS_EOF(fo) ) {
294 ghc_errtype = ERR_EOF;
299 /* Buffering has been changed, report back */
300 if ( FILEOBJ_LINEBUFFERED(fo) ) {
302 } else if ( FILEOBJ_BLOCKBUFFERED(fo) ) {
306 /* if input stream is connect to an output stream, flush it first */
307 if ( fo->connectedTo != NULL &&
308 fo->connectedTo->fd != -1 &&
309 (fo->connectedTo->flags & FILEOBJ_WRITE)
311 rc = flushFile((StgForeignPtr)fo->connectedTo);
313 if (rc < 0) return (rc == FILEOBJ_BLOCKED_WRITE ? FILEOBJ_BLOCKED_CONN_WRITE : rc);
315 /* RW object: flush the (output) buffer first. */
316 if ( FILEOBJ_WRITEABLE(fo) && FILEOBJ_JUST_WRITTEN(fo) && FILEOBJ_NEEDS_FLUSHING(fo) ) {
317 rc = flushBuffer(ptr);
318 if (rc < 0) return rc;
320 fo->flags = (fo->flags & ~FILEOBJ_RW_WRITE) | FILEOBJ_RW_READ;
325 fo->flags & FILEOBJ_WINSOCK ?
326 recv(fo->fd, &c, 1, 0) :
327 read(fo->fd, &c, 1))) <= 0 ) {
329 read(fo->fd, &c, 1))) <= 0 ) {
332 ghc_errtype = ERR_EOF;
335 } else if ( count == -1 && errno == EAGAIN) {
337 return FILEOBJ_BLOCKED_READ;
338 } else if ( count == -1 && errno != EINTR) {
345 if ( isatty(fo->fd) && c == EOT ) {