2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: fileObject.c,v 1.2 1998/12/02 13:27:26 simonm Exp $
6 * hPutStr Runtime Support
11 #include "fileObject.h"
18 ((IOFileObject*)fo)->flags = flg;
27 ((IOFileObject*)fo)->bufWPtr = len;
35 return (((IOFileObject*)fo)->bufWPtr);
42 return (((IOFileObject*)fo)->bufSize);
51 ((IOFileObject*)fo)->buf = buf;
52 ((IOFileObject*)fo)->bufSize = sz;
59 { return (((IOFileObject*)fo)->buf); }
65 /* getWriteableBuf() is called prior to starting to pack
66 a Haskell string into the IOFileObject buffer. It takes
67 care of flushing the (input) buffer in the case we're
68 dealing with a RW handle.
70 IOFileObject* fo = (IOFileObject*)ptr;
72 if ( FILEOBJ_READABLE(fo) && FILEOBJ_JUST_READ(fo) ) {
73 flushReadBuffer(ptr); /* ignoring return code */
74 /* Ahead of time really, but indicate that we're (just about to) write */
76 fo->flags = (fo->flags & ~FILEOBJ_RW_READ) | FILEOBJ_RW_WRITE;
84 { return ((char*)((IOFileObject*)fo)->buf + (((IOFileObject*)fo)->bufRPtr) - count); }
89 { return (((IOFileObject*)fo)->fd); }
94 { return (((IOFileObject*)fo)->connectedTo->fd); }
101 { ((IOFileObject*)fo)->fd = fp;
106 setConnectedTo(fo, fw, flg)
111 if( flg && (! isatty(((IOFileObject*)fo)->fd) || !isatty(((IOFileObject*)fw)->fd)) ) {
114 ((IOFileObject*)fo)->connectedTo = (IOFileObject*)fw;
118 static int __pushback_buf_size__ = 2;
121 setPushbackBufSize(i)
123 { __pushback_buf_size__ = (i > 0 ? i : 0); }
127 { return (__pushback_buf_size__); }
130 clearNonBlockingIOFlag__ (ptr)
132 { ((IOFileObject*)ptr)->flags &= ~FILEOBJ_NONBLOCKING_IO; }
135 setNonBlockingIOFlag__ (ptr)
137 { ((IOFileObject*)ptr)->flags |= FILEOBJ_NONBLOCKING_IO; }
140 clearConnNonBlockingIOFlag__ (ptr)
142 { ((IOFileObject*)ptr)->connectedTo->flags &= ~FILEOBJ_NONBLOCKING_IO; }
145 setConnNonBlockingIOFlag__ (ptr)
148 if ( ((IOFileObject*)ptr)->connectedTo != NULL ) {
149 ((IOFileObject*)ptr)->connectedTo->flags |= FILEOBJ_NONBLOCKING_IO;
155 /* Only ever called on line-buffered file objects */
157 fill_up_line_buffer(fo)
163 /* ToDo: deal with buffer overflow (i.e., realloc buffer if this happens) */
165 if ( fo->bufRPtr == fo->bufWPtr ) { /* There's nothing in the buffer, reset */
170 len = fo->bufSize - fo->bufWPtr + 1;
171 p = (unsigned char*)fo->buf + fo->bufWPtr;
173 if ( fo->flags & FILEOBJ_NONBLOCKING_IO && inputReady ((StgForeignPtr)fo,0) != 1 )
174 return FILEOBJ_BLOCKED_READ;
176 if ((count = read(fo->fd, p, len)) <= 0) {
178 ghc_errtype = ERR_EOF;
182 } else if ( count == -1 && errno == EAGAIN) {
184 return FILEOBJ_BLOCKED_READ;
185 } else if ( count == -1 && errno != EINTR ) {
191 fo->bufWPtr += count;
192 return (fo->bufWPtr - ipos);