2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: fileObject.c,v 1.3 1999/05/05 10:33:14 sof Exp $
6 * hPutStr Runtime Support
11 #include "fileObject.h"
24 ((IOFileObject*)fo)->flags = flg;
33 ((IOFileObject*)fo)->bufWPtr = len;
41 return (((IOFileObject*)fo)->bufWPtr);
48 return (((IOFileObject*)fo)->bufSize);
57 ((IOFileObject*)fo)->buf = buf;
58 ((IOFileObject*)fo)->bufSize = sz;
65 { return (((IOFileObject*)fo)->buf); }
71 /* getWriteableBuf() is called prior to starting to pack
72 a Haskell string into the IOFileObject buffer. It takes
73 care of flushing the (input) buffer in the case we're
74 dealing with a RW handle.
76 IOFileObject* fo = (IOFileObject*)ptr;
78 if ( FILEOBJ_READABLE(fo) && FILEOBJ_JUST_READ(fo) ) {
79 flushReadBuffer(ptr); /* ignoring return code */
80 /* Ahead of time really, but indicate that we're (just about to) write */
82 fo->flags = (fo->flags & ~FILEOBJ_RW_READ) | FILEOBJ_RW_WRITE;
90 { return ((char*)((IOFileObject*)fo)->buf + (((IOFileObject*)fo)->bufRPtr) - count); }
95 { return (((IOFileObject*)fo)->fd); }
100 { return (((IOFileObject*)fo)->connectedTo->fd); }
107 { ((IOFileObject*)fo)->fd = fp;
112 setConnectedTo(fo, fw, flg)
117 if( flg && (! isatty(((IOFileObject*)fo)->fd) || !isatty(((IOFileObject*)fw)->fd)) ) {
120 ((IOFileObject*)fo)->connectedTo = (IOFileObject*)fw;
124 static int __pushback_buf_size__ = 2;
127 setPushbackBufSize(i)
129 { __pushback_buf_size__ = (i > 0 ? i : 0); }
133 { return (__pushback_buf_size__); }
136 clearNonBlockingIOFlag__ (ptr)
138 { ((IOFileObject*)ptr)->flags &= ~FILEOBJ_NONBLOCKING_IO; }
141 setNonBlockingIOFlag__ (ptr)
143 { ((IOFileObject*)ptr)->flags |= FILEOBJ_NONBLOCKING_IO; }
146 clearConnNonBlockingIOFlag__ (ptr)
148 { ((IOFileObject*)ptr)->connectedTo->flags &= ~FILEOBJ_NONBLOCKING_IO; }
151 setConnNonBlockingIOFlag__ (ptr)
154 if ( ((IOFileObject*)ptr)->connectedTo != NULL ) {
155 ((IOFileObject*)ptr)->connectedTo->flags |= FILEOBJ_NONBLOCKING_IO;
161 /* Only ever called on line-buffered file objects */
163 fill_up_line_buffer(fo)
169 /* ToDo: deal with buffer overflow (i.e., realloc buffer if this happens) */
171 if ( fo->bufRPtr == fo->bufWPtr ) { /* There's nothing in the buffer, reset */
176 len = fo->bufSize - fo->bufWPtr + 1;
177 p = (unsigned char*)fo->buf + fo->bufWPtr;
179 if ( fo->flags & FILEOBJ_NONBLOCKING_IO && inputReady ((StgForeignPtr)fo,0) != 1 )
180 return FILEOBJ_BLOCKED_READ;
184 #ifdef HAVE_WINSOCK_H
185 fo->flags & FILEOBJ_WINSOCK ?
186 recv(fo->fd, p, len, 0) :
187 read(fo->fd, p, len))) <= 0 ) {
189 read(fo->fd, p, len))) <= 0 ) {
192 ghc_errtype = ERR_EOF;
196 } else if ( count == -1 && errno == EAGAIN) {
198 return FILEOBJ_BLOCKED_READ;
199 } else if ( count == -1 && errno != EINTR ) {
205 fo->bufWPtr += count;
206 return (fo->bufWPtr - ipos);