2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: readFile.c,v 1.13 2000/03/21 17:41:02 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, rc=0, total_count=0;
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;
160 /* copy the unread parts of the file buffer..*/
161 if ( FILEOBJ_READABLE(fo) &&
163 fo->bufWPtr >= fo->bufRPtr ) {
165 if (fo->bufWPtr - fo->bufRPtr >= len) {
166 /* buffer has enough data to fulfill the request */
167 memcpy(buf, fo->buf + fo->bufRPtr, len);
171 /* can only partially fulfill the request from the buffer */
172 count = fo->bufWPtr - fo->bufRPtr;
173 memcpy(buf, fo->buf + fo->bufRPtr, count);
185 fo->flags & FILEOBJ_WINSOCK ?
186 recv(fd, p, len, 0) :
187 read(fd, p, len))) <= 0 ) {
189 read(fd, p, len))) <= 0 ) {
197 } else if ( count == -1 && errno == EAGAIN) {
200 return total_count; /* partial read */
202 return FILEOBJ_BLOCKED_READ;
205 } else if ( count == -1 && errno != EINTR) {
212 total_count += count;
217 readLine() tries to fill the buffer up with a line of chars, returning
218 the length of the resulting line.
220 Users of readLine() should immediately afterwards copy out the line
226 readLine(StgForeignPtr ptr)
228 IOFileObject* fo = (IOFileObject*)ptr;
231 /* Check if someone hasn't zapped us */
232 if ( fo == NULL || fo->fd == -1 )
235 if ( FILEOBJ_IS_EOF(fo) ) {
236 ghc_errtype = ERR_EOF;
241 /* Weird case: buffering has been turned off.
242 Return non-std value and deal with this case on the Haskell side.
244 if ( FILEOBJ_UNBUFFERED(fo) ) {
248 /* if input stream is connect to an output stream, flush it first */
249 if ( fo->connectedTo != NULL &&
250 fo->connectedTo->fd != -1 &&
251 (fo->connectedTo->flags & FILEOBJ_WRITE)
253 rc = flushFile((StgForeignPtr)fo->connectedTo);
255 if (rc < 0) return (rc == FILEOBJ_BLOCKED_WRITE ? FILEOBJ_BLOCKED_CONN_WRITE : rc);
257 /* RW object: flush the (output) buffer first. */
258 if ( FILEOBJ_WRITEABLE(fo) && FILEOBJ_JUST_WRITTEN(fo) ) {
259 rc = flushBuffer(ptr);
260 if (rc < 0) return rc;
262 fo->flags = (fo->flags & ~FILEOBJ_RW_WRITE) | FILEOBJ_RW_READ;
264 if ( fo->bufRPtr < 0 || fo->bufRPtr >= fo->bufWPtr ) { /* Buffer is empty */
265 fo->bufRPtr=0; fo->bufWPtr=0;
266 rc = fill_up_line_buffer(fo);
267 if (rc < 0) return rc;
271 unsigned char* s1 = memchr((unsigned char *)fo->buf+fo->bufRPtr, '\n', fo->bufWPtr - fo->bufRPtr);
272 if (s1 != NULL ) { /* Found one */
273 /* Note: we *don't* zero terminate the line */
274 count = s1 - ((unsigned char*)fo->buf + fo->bufRPtr) + 1;
275 fo->bufRPtr += count;
278 /* Just return partial line */
279 count = fo->bufWPtr - fo->bufRPtr;
280 fo->bufRPtr += count;
288 readChar(StgForeignPtr ptr)
290 IOFileObject* fo= (IOFileObject*)ptr;
294 /* Check if someone hasn't zapped us */
295 if ( fo == NULL || fo->fd == -1)
298 if ( FILEOBJ_IS_EOF(fo) ) {
299 ghc_errtype = ERR_EOF;
304 /* Buffering has been changed, report back */
305 if ( FILEOBJ_LINEBUFFERED(fo) ) {
307 } else if ( FILEOBJ_BLOCKBUFFERED(fo) ) {
311 /* if input stream is connect to an output stream, flush it first */
312 if ( fo->connectedTo != NULL &&
313 fo->connectedTo->fd != -1 &&
314 (fo->connectedTo->flags & FILEOBJ_WRITE)
316 rc = flushFile((StgForeignPtr)fo->connectedTo);
318 if (rc < 0) return (rc == FILEOBJ_BLOCKED_WRITE ? FILEOBJ_BLOCKED_CONN_WRITE : rc);
320 /* RW object: flush the (output) buffer first. */
321 if ( FILEOBJ_WRITEABLE(fo) && FILEOBJ_JUST_WRITTEN(fo) && FILEOBJ_NEEDS_FLUSHING(fo) ) {
322 rc = flushBuffer(ptr);
323 if (rc < 0) return rc;
325 fo->flags = (fo->flags & ~FILEOBJ_RW_WRITE) | FILEOBJ_RW_READ;
330 fo->flags & FILEOBJ_WINSOCK ?
331 recv(fo->fd, &c, 1, 0) :
332 read(fo->fd, &c, 1))) <= 0 ) {
334 read(fo->fd, &c, 1))) <= 0 ) {
337 ghc_errtype = ERR_EOF;
340 } else if ( count == -1 && errno == EAGAIN) {
342 return FILEOBJ_BLOCKED_READ;
343 } else if ( count == -1 && errno != EINTR) {
350 if ( isatty(fo->fd) && c == EOT ) {