summaryrefslogtreecommitdiff
path: root/ghc/lib/std/cbits/readFile.c
blob: 432a7385c68708c5f699864641532510c188fca3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* 
 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
 *
 * $Id: readFile.c,v 1.10 2000/01/18 12:42:12 simonmar Exp $
 *
 * hGetContents Runtime Support
 */

#include "Rts.h"
#include "stgio.h"

#if defined(HAVE_WINSOCK_H) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
#define USE_WINSOCK
#endif

#ifdef USE_WINSOCK
#include <winsock.h>
#endif

#define EOT 4

/* Filling up a (block-buffered) buffer, that
   is completely empty. */
StgInt
readBlock(StgForeignPtr ptr)
{
    IOFileObject* fo = (IOFileObject*)ptr;
    int count,rc=0;
    int fd;

    /* Check if someone hasn't zapped us */
    if ( fo == NULL || fo->fd == -1 )
       return -2;

    fd = fo->fd;

    if ( FILEOBJ_IS_EOF(fo) ) {
	ghc_errtype = ERR_EOF;
	ghc_errstr = "";
	return -1;
    }

    /* Weird case: buffering has suddenly been turned off.
       Return non-std value and deal with this case on the Haskell side.
    */
    if ( FILEOBJ_UNBUFFERED(fo) ) {
        return -3;
    }

    /* if input stream is connect to an output stream, flush this one first. */
    if ( fo->connectedTo != NULL   &&
         fo->connectedTo->fd != -1 &&
         (fo->connectedTo->flags & FILEOBJ_WRITE)
       ) {
       rc = flushFile((StgForeignPtr)fo->connectedTo);
    }
    if (rc < 0) return (rc == FILEOBJ_BLOCKED_WRITE ? FILEOBJ_BLOCKED_CONN_WRITE : rc);

    /* RW object: flush the (output) buffer first. */
    if ( FILEOBJ_WRITEABLE(fo) && FILEOBJ_JUST_WRITTEN(fo) && FILEOBJ_NEEDS_FLUSHING(fo) ) {
        rc = flushBuffer(ptr);
	if (rc < 0) return rc;
    }
    fo->flags = (fo->flags & ~FILEOBJ_RW_WRITE) | FILEOBJ_RW_READ;

    /* return the unread parts of the file buffer..*/
    if ( fo->flags & FILEOBJ_READ && 
    	 fo->bufRPtr > 0 	  &&
	 fo->bufWPtr > fo->bufRPtr ) {
	count = fo->bufWPtr - fo->bufRPtr;
        fo->bufRPtr=0;
        return count;
    }

#if 0
    fprintf(stderr, "rb: %d %d %d\n", fo->bufRPtr, fo->bufWPtr, fo->bufSize);
#endif

    while ((count =
	     (
#ifdef USE_WINSOCK
	       fo->flags & FILEOBJ_WINSOCK ?
	         recv(fd, fo->buf, fo->bufSize, 0) :
	         read(fd, fo->buf, fo->bufSize))) <= 0 ) {
#else
	         read(fd, fo->buf, fo->bufSize))) <= 0 ) {
#endif
	if ( count == 0 ) {
            FILEOBJ_SET_EOF(fo);
	    ghc_errtype = ERR_EOF;
	    ghc_errstr = "";
	    return -1;
	} else if ( count == -1 && errno == EAGAIN) {
	    errno = 0;
	    return FILEOBJ_BLOCKED_READ;
	} else if ( count == -1 && errno != EINTR) {
	    cvtErrno();
	    stdErrno();
	    return -1;
	}
    }
    fo->bufWPtr = count;
    fo->bufRPtr = 0;
    return count;
}

/* Filling up a (block-buffered) buffer of length len */
StgInt
readChunk(StgForeignPtr ptr, StgAddr buf, StgInt len)
{
    IOFileObject* fo = (IOFileObject*)ptr;
    int count=0,rc=0, total_count;
    int fd;
    char* p;

    /* Check if someone hasn't zapped us */
    if ( fo == NULL )
       return -2;

    fd = fo->fd;

    if ( fd == -1 ) /* File has been closed for us */
       return -2;

    if ( FILEOBJ_IS_EOF(fo) ) {
	ghc_errtype = ERR_EOF;
	ghc_errstr = "";
	return -1;
    }

    /* if input stream is connect to an output stream, flush it first */
    if ( fo->connectedTo != NULL   &&
         fo->connectedTo->fd != -1 &&
         (fo->connectedTo->flags & FILEOBJ_WRITE)
       ) {
       rc = flushFile((StgForeignPtr)fo->connectedTo);
    }
    if (rc < 0) return (rc == FILEOBJ_BLOCKED_WRITE ? FILEOBJ_BLOCKED_CONN_WRITE : rc);

    /* RW object: flush the (output) buffer first. */
    if ( FILEOBJ_WRITEABLE(fo) && FILEOBJ_JUST_WRITTEN(fo) && FILEOBJ_NEEDS_FLUSHING(fo) ) {
        rc = flushBuffer(ptr);
	if (rc < 0) return rc;
    }
    fo->flags = (fo->flags & ~FILEOBJ_RW_WRITE) | FILEOBJ_RW_READ;

    /* copy the unread parts of the file buffer..*/
    if ( FILEOBJ_READABLE(fo) && 
    	 fo->bufRPtr > 0      &&
	 fo->bufWPtr >= fo->bufRPtr ) {
	count = ( len < (fo->bufWPtr - fo->bufRPtr)) ? len : (fo->bufWPtr - fo->bufRPtr);
	memcpy(buf,fo->buf, count);
	fo->bufWPtr=0;
	fo->bufRPtr=0;
	
    }

    if (len - count <= 0)
       return count;

    len -= count;
    p = buf;
    p += count;
    total_count = count;

    while ((count =
             (
#ifdef USE_WINSOCK
	       fo->flags & FILEOBJ_WINSOCK ?
	         recv(fd, p, len, 0) :
	         read(fd, p, len))) <= 0 ) {
#else
	         read(fd, p, len))) <= 0 ) {
#endif
	if ( count == 0 ) { /* EOF */
	    break;
	} else if ( count == -1 && errno == EAGAIN) {
	    /* ToDo: partial/blocked reads??????  Looks like we don't recover
	     * from this properly.
	     */
	    errno = 0;
	    return FILEOBJ_BLOCKED_READ;
	} else if ( count == -1 && errno != EINTR) {
	    cvtErrno();
	    stdErrno();
	    return -1;
	}
        total_count += count;
	len -= count;
	p += count;
    }

    total_count += count;
    /* ToDo: might point beyond the end of the buffer??? */
    fo->bufWPtr = total_count;
    fo->bufRPtr = 0;
    return total_count;
}

/*
  readLine() tries to fill the buffer up with a line of chars, returning
  the length of the resulting line. 
  
  Users of readLine() should immediately afterwards copy out the line
  from the buffer.

*/

StgInt
readLine(StgForeignPtr ptr)
{
    IOFileObject* fo = (IOFileObject*)ptr;
    int rc=0, count;

    /* Check if someone hasn't zapped us */
    if ( fo == NULL || fo->fd == -1 )
       return -2;

    if ( FILEOBJ_IS_EOF(fo) ) {
	ghc_errtype = ERR_EOF;
	ghc_errstr = "";
	return -1;
    }

    /* Weird case: buffering has been turned off.
       Return non-std value and deal with this case on the Haskell side.
    */
    if ( FILEOBJ_UNBUFFERED(fo) ) {
        return -3;
    }

    /* if input stream is connect to an output stream, flush it first */
    if ( fo->connectedTo != NULL   &&
         fo->connectedTo->fd != -1 &&
         (fo->connectedTo->flags & FILEOBJ_WRITE)
       ) {
       rc = flushFile((StgForeignPtr)fo->connectedTo);
    }
    if (rc < 0) return (rc == FILEOBJ_BLOCKED_WRITE ? FILEOBJ_BLOCKED_CONN_WRITE : rc);

    /* RW object: flush the (output) buffer first. */
    if ( FILEOBJ_WRITEABLE(fo) && FILEOBJ_JUST_WRITTEN(fo) ) {
        rc = flushBuffer(ptr);
	if (rc < 0) return rc;
    }
    fo->flags = (fo->flags & ~FILEOBJ_RW_WRITE) | FILEOBJ_RW_READ;

    if ( fo->bufRPtr < 0 || fo->bufRPtr >= fo->bufWPtr ) { /* Buffer is empty */
       fo->bufRPtr=0; fo->bufWPtr=0;
       rc = fill_up_line_buffer(fo);
       if (rc < 0) return rc;
    }

    while (1) {
       unsigned char* s1 = memchr((unsigned char *)fo->buf+fo->bufRPtr, '\n', fo->bufWPtr - fo->bufRPtr);
       if (s1 != NULL ) {  /* Found one */
	  /* Note: we *don't* zero terminate the line */
	  count = s1 - ((unsigned char*)fo->buf + fo->bufRPtr) + 1;
	  fo->bufRPtr += count;
          return count;
       } else {
          /* Just return partial line */
	  count = fo->bufWPtr - fo->bufRPtr;
	  fo->bufRPtr += count;
          return count;
       }
    }

}

StgInt
readChar(StgForeignPtr ptr)
{
    IOFileObject* fo= (IOFileObject*)ptr;
    int count,rc=0;
    unsigned char c;

    /* Check if someone hasn't zapped us */
    if ( fo == NULL || fo->fd == -1)
       return -2;

    if ( FILEOBJ_IS_EOF(fo) ) {
	ghc_errtype = ERR_EOF;
	ghc_errstr = "";
	return -1;
    }

    /* Buffering has been changed, report back */
    if ( FILEOBJ_LINEBUFFERED(fo) ) {
       return -3;
    } else if ( FILEOBJ_BLOCKBUFFERED(fo) ) {
       return -4;
    }

    /* if input stream is connect to an output stream, flush it first */
    if ( fo->connectedTo != NULL   &&
         fo->connectedTo->fd != -1 &&
         (fo->connectedTo->flags & FILEOBJ_WRITE)
       ) {
       rc = flushFile((StgForeignPtr)fo->connectedTo);
    }
    if (rc < 0) return (rc == FILEOBJ_BLOCKED_WRITE ? FILEOBJ_BLOCKED_CONN_WRITE : rc);

    /* RW object: flush the (output) buffer first. */
    if ( FILEOBJ_WRITEABLE(fo) && FILEOBJ_JUST_WRITTEN(fo) && FILEOBJ_NEEDS_FLUSHING(fo) ) {
        rc = flushBuffer(ptr);
	if (rc < 0) return rc;
    }
    fo->flags = (fo->flags & ~FILEOBJ_RW_WRITE) | FILEOBJ_RW_READ;

    while ( (count = 
	       (
#ifdef USE_WINSOCK
	         fo->flags & FILEOBJ_WINSOCK ?
		 recv(fo->fd, &c, 1, 0) :
		 read(fo->fd, &c, 1))) <= 0 ) {
#else
		 read(fo->fd, &c, 1))) <= 0 ) {
#endif
	if ( count == 0 ) {
	    ghc_errtype = ERR_EOF;
	    ghc_errstr = "";
	    return -1;
	} else if ( count == -1 && errno == EAGAIN) {
	    errno = 0;
	    return FILEOBJ_BLOCKED_READ;
	} else if ( count == -1 && errno != EINTR) {
	    cvtErrno();
	    stdErrno();
	    return -1;
	}
    }

    if ( isatty(fo->fd) && c == EOT ) {
	return EOF;
    } else {
        return (int)c;
    }
}