* readrecord.c: Include <sys/types.h> for off_t.
[gcc.git] / libchill / readrecord.c
1 /* Implement Input/Output runtime actions for CHILL.
2 Copyright (C) 1992, 1993, 1998 Free Software Foundation, Inc.
3 Author: Wilfried Moser, et al
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 /* As a special exception, if you link this library with other files,
22 some of which are compiled with GCC, to produce an executable,
23 this library does not by itself cause the resulting executable
24 to be covered by the GNU General Public License.
25 This exception does not however invalidate any other reasons why
26 the executable file might be covered by the GNU General Public License. */
27
28 #include <setjmp.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33
34 #include "fileio.h"
35
36 #ifdef EOF
37 #undef EOF
38 #endif
39 #define EOF -1
40
41 static
42 Boolean
43 doRead( Access_Mode* the_access, void* buf, size_t nbyte )
44 {
45 size_t nread;
46
47 nread = read( the_access->association->handle, buf, nbyte );
48 if( nread == nbyte )
49 {
50 CLR_FLAG( the_access, IO_OUTOFFILE );
51 return True;
52 }
53 if( nread == 0 )
54 {
55 SET_FLAG( the_access, IO_OUTOFFILE );
56 return False;
57 }
58 the_access->association->syserrno = errno;
59 RWEXCEPTION( READFAIL, OS_IO_ERROR );
60 /* no return */
61 }
62
63 static
64 int bgetc( int handle, readbuf_t* rbptr )
65 {
66 if( rbptr->cur >= rbptr->len )
67 {
68 rbptr->len = read( handle, rbptr->buf, READBUFLEN );
69 if( rbptr->len == 0 )
70 return EOF;
71 rbptr->cur = 0;
72 }
73 return rbptr->buf[rbptr->cur++];
74 }
75
76 static
77 void bungetc( readbuf_t* rbptr, int c )
78 {
79 rbptr->buf[--rbptr->cur] = c;
80 }
81
82 void*
83 __readrecord( Access_Mode* the_access,
84 signed long the_index,
85 char* the_buf_addr,
86 char* file,
87 int line )
88 {
89 unsigned long info;
90 char* actaddr;
91 unsigned short actlen;
92 off_t filepos;
93 unsigned short reclen;
94 unsigned long readlen;
95
96 if( !the_access )
97 CHILLEXCEPTION( file, line, EMPTY, NULL_ACCESS );
98
99 if( !the_access->association )
100 CHILLEXCEPTION( file, line, NOTCONNECTED, IS_NOT_CONNECTED );
101
102 /* Usage must not be WriteOnly */
103 if( the_access->association->usage == WriteOnly )
104 CHILLEXCEPTION( file, line, READFAIL, BAD_USAGE );
105
106 /* OUTOFFILE must not be True when connected for sequential read */
107 if( !TEST_FLAG( the_access, IO_INDEXED )
108 && TEST_FLAG( the_access, IO_OUTOFFILE ) )
109 CHILLEXCEPTION( file, line, READFAIL, OUT_OF_FILE );
110
111 /*
112 * Positioning
113 */
114 if( TEST_FLAG( the_access, IO_INDEXED ) )
115 {
116 /* index expression must be within bounds of index mode */
117 if( the_index < the_access->lowindex
118 || the_access->highindex < the_index )
119 CHILLEXCEPTION( file, line, RANGEFAIL, BAD_INDEX );
120
121 filepos = the_access->base +
122 (the_index - the_access->lowindex) * the_access->reclength;
123
124 if( lseek( the_access->association->handle, filepos, SEEK_SET ) == -1L )
125 CHILLEXCEPTION( file, line, READFAIL, LSEEK_FAILS );
126 }
127
128 /* establish store loc */
129 if( !(actaddr = the_buf_addr ))
130 {
131 /* if not yet allocated, do it now */
132 if (!the_access->store_loc)
133 if( !(the_access->store_loc = (char*)malloc( the_access->reclength ) ) )
134 CHILLEXCEPTION( file, line, SPACEFAIL, STORE_LOC_ALLOC );
135 actaddr = the_access->store_loc;
136 }
137 actlen = the_access->reclength;
138
139 if( (info = setjmp( __rw_exception )) )
140 CHILLEXCEPTION( file, line, info>>16, info & 0xffff );
141
142 if( TEST_FLAG( the_access, IO_TEXTIO ) )
143 {
144 readlen = actlen - 2;
145 if( TEST_FLAG( the_access, IO_INDEXED ) )
146 {
147 if( ! doRead( the_access, &reclen, sizeof(reclen) ) )
148 return NULL;
149 if( reclen > readlen )
150 CHILLEXCEPTION( file, line, RANGEFAIL, RECORD_TOO_LONG );
151 if( ! doRead( the_access, actaddr + 2, reclen ) )
152 CHILLEXCEPTION( file, line, READFAIL, RECORD_TOO_SHORT );
153 }
154 else
155 {
156 Association_Mode *assoc = the_access->association;
157 int handle = assoc->handle;
158 readbuf_t* rbuf = assoc->bufptr;
159 char* cptr = actaddr+2;
160 int curr;
161
162 reclen = 0;
163 while( readlen-- )
164 {
165 curr = bgetc( handle, rbuf );
166 if( curr == '\n' )
167 goto end_of_line;
168 if( curr == EOF )
169 {
170 if( !reclen )
171 SET_FLAG( the_access, IO_OUTOFFILE );
172 goto end_of_line;
173 }
174 *cptr++ = curr;
175 reclen++;
176 }
177 if( (curr = bgetc( handle, rbuf )) != '\n' )
178 {
179 bungetc( rbuf, curr );
180 CHILLEXCEPTION( file, line, RANGEFAIL, RECORD_TOO_LONG );
181 }
182 end_of_line: ;
183 }
184 MOV2(actaddr,&reclen);
185 }
186 else
187 {
188 switch( the_access->rectype )
189 {
190 case Fixed:
191 if( ! doRead( the_access, actaddr, actlen ) )
192 return NULL;
193 break;
194 case VaryingChars:
195 if( TEST_FLAG( the_access->association, IO_VARIABLE ) )
196 {
197 if( ! doRead( the_access, &reclen, sizeof(reclen) ) )
198 return NULL;
199 if( reclen > actlen - 2 )
200 CHILLEXCEPTION( file, line, RANGEFAIL, RECORD_TOO_LONG );
201 readlen = TEST_FLAG( the_access, IO_INDEXED ) ? actlen - 2 : reclen;
202 if( ! doRead( the_access, actaddr + 2, readlen ) )
203 CHILLEXCEPTION( file, line, READFAIL, RECORD_TOO_SHORT );
204 }
205 else
206 {
207 if( ! doRead( the_access, actaddr + 2, reclen = actlen - 2 ) )
208 CHILLEXCEPTION( file, line, READFAIL, RECORD_TOO_SHORT );
209 }
210 MOV2(actaddr,&reclen);
211 break;
212 }
213 }
214
215 return actaddr;
216 }