[project @ 1999-09-16 13:14:38 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / cbits / fileObject.c
1 /* 
2  * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
3  *
4  * $Id: fileObject.c,v 1.6 1999/09/16 13:14:42 simonmar Exp $
5  *
6  * hPutStr Runtime Support
7  */
8
9 #include "Rts.h"
10 #include "stgio.h"
11 #include "fileObject.h"
12
13 #include <stdio.h>
14
15 #if defined(HAVE_WINSOCK_H) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
16 #define USE_WINSOCK
17 #endif
18
19 #ifdef USE_WINSOCK
20 #include <winsock.h>
21 #endif
22
23 void
24 setBufFlags(fo, flg)
25 StgForeignPtr fo;
26 StgInt flg;
27 {
28   ((IOFileObject*)fo)->flags = flg;
29   return;
30 }
31
32 void
33 setBufWPtr(fo, len)
34 StgForeignPtr fo;
35 StgInt len;
36 {
37   ((IOFileObject*)fo)->bufWPtr = len;
38   return;
39 }
40
41 StgInt
42 getBufWPtr(fo)
43 StgForeignPtr fo;
44 {
45   return (((IOFileObject*)fo)->bufWPtr);
46 }
47
48 StgInt
49 getBufSize(fo)
50 StgForeignPtr fo;
51 {
52   return (((IOFileObject*)fo)->bufSize);
53 }
54
55 void
56 setBuf(fo, buf,sz)
57 StgForeignPtr fo;
58 StgAddr buf;
59 StgInt sz;
60 {
61   ((IOFileObject*)fo)->buf     = buf;
62   ((IOFileObject*)fo)->bufSize = sz;
63   return;
64 }
65
66 StgAddr
67 getBuf(fo)
68 StgForeignPtr fo;
69 { return (((IOFileObject*)fo)->buf); }
70
71 StgAddr
72 getWriteableBuf(ptr)
73 StgForeignPtr ptr;
74
75    /* getWriteableBuf() is called prior to starting to pack
76       a Haskell string into the IOFileObject buffer. It takes
77       care of flushing the (input) buffer in the case we're
78       dealing with a RW handle.
79    */
80    IOFileObject* fo = (IOFileObject*)ptr;
81
82    if ( FILEOBJ_READABLE(fo) && FILEOBJ_JUST_READ(fo) ) {
83       flushReadBuffer(ptr);  /* ignoring return code */
84       /* Ahead of time really, but indicate that we're (just about to) write */
85    }
86    fo->flags = (fo->flags & ~FILEOBJ_RW_READ) | FILEOBJ_RW_WRITE;
87    return (fo->buf);
88 }
89
90 StgAddr
91 getBufStart(fo,count)
92 StgForeignPtr fo;
93 StgInt count;
94 { return ((char*)((IOFileObject*)fo)->buf + (((IOFileObject*)fo)->bufRPtr) - count); }
95
96 StgInt
97 getFileFd(fo)
98 StgForeignPtr fo;
99 { return (((IOFileObject*)fo)->fd); }
100
101 StgInt
102 getConnFileFd(fo)
103 StgForeignPtr fo;
104 { return (((IOFileObject*)fo)->connectedTo->fd); }
105
106
107 void
108 setFd(fo,fp)
109 StgForeignPtr fo;
110 StgInt fp;
111 { ((IOFileObject*)fo)->fd = fp;
112   return;
113 }
114
115 void
116 setConnectedTo(fo, fw, flg)
117 StgForeignPtr fo;
118 StgForeignPtr fw;
119 StgInt flg;
120 {
121   if( flg && (! isatty(((IOFileObject*)fo)->fd) || !isatty(((IOFileObject*)fw)->fd)) ) {
122       return;
123   }
124  ((IOFileObject*)fo)->connectedTo = (IOFileObject*)fw;
125   return;
126 }
127
128 static int __pushback_buf_size__ = 2;
129
130 void
131 setPushbackBufSize(i)
132 StgInt i;
133 { __pushback_buf_size__ = (i > 0 ? i : 0); }
134
135 StgInt
136 getPushbackBufSize()
137 { return (__pushback_buf_size__); }
138
139 /* Only ever called on line-buffered file objects */
140 StgInt
141 fill_up_line_buffer(fo)
142 IOFileObject* fo;
143 {
144   int count,len, ipos;
145   unsigned char* p;
146
147   /* ToDo: deal with buffer overflow (i.e., realloc buffer if this happens) */
148  
149   if ( fo->bufRPtr == fo->bufWPtr ) { /* There's nothing in the buffer, reset */
150       fo->bufRPtr=0;
151       fo->bufWPtr=0;
152   }
153   ipos = fo->bufWPtr;
154   len = fo->bufSize - fo->bufWPtr + 1;
155   p   = (unsigned char*)fo->buf + fo->bufWPtr;
156
157   if ((count = 
158          (
159 #ifdef USE_WINSOCK
160            fo->flags & FILEOBJ_WINSOCK ?
161            recv(fo->fd, p, len, 0) :
162            read(fo->fd, p, len))) <= 0 ) {
163 #else
164            read(fo->fd, p, len))) <= 0 ) {
165 #endif    
166       if (count == 0) {
167          ghc_errtype = ERR_EOF;
168          ghc_errstr = "";
169          FILEOBJ_SET_EOF(fo);
170          return -1;
171       } else if ( count == -1 && errno == EAGAIN) {
172          errno = 0;
173          return FILEOBJ_BLOCKED_READ;
174       } else if ( count == -1 && errno != EINTR ) {
175          cvtErrno();
176          stdErrno();
177          return -1;
178       }
179   }
180   fo->bufWPtr += count;
181   return (fo->bufWPtr - ipos);
182 }