1 /* Implement Input/Output runtime actions for CHILL.
2 Copyright (C) 1992,1993 Free Software Foundation, Inc.
3 Author: Wilfried Moser, et al
5 This file is part of GNU CC.
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)
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.
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. */
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. */
42 doRead( Access_Mode* the_access, void* buf, size_t nbyte )
46 nread = read( the_access->association->handle, buf, nbyte );
49 CLR_FLAG( the_access, IO_OUTOFFILE );
54 SET_FLAG( the_access, IO_OUTOFFILE );
57 the_access->association->syserrno = errno;
58 RWEXCEPTION( READFAIL, OS_IO_ERROR );
63 int bgetc( int handle, readbuf_t* rbptr )
65 if( rbptr->cur >= rbptr->len )
67 rbptr->len = read( handle, rbptr->buf, READBUFLEN );
72 return rbptr->buf[rbptr->cur++];
76 void bungetc( readbuf_t* rbptr, int c )
78 rbptr->buf[--rbptr->cur] = c;
82 __readrecord( Access_Mode* the_access,
83 signed long the_index,
90 unsigned short actlen;
92 unsigned short reclen;
93 unsigned long readlen;
96 CHILLEXCEPTION( file, line, EMPTY, NULL_ACCESS );
98 if( !the_access->association )
99 CHILLEXCEPTION( file, line, NOTCONNECTED, IS_NOT_CONNECTED );
101 /* Usage must not be WriteOnly */
102 if( the_access->association->usage == WriteOnly )
103 CHILLEXCEPTION( file, line, READFAIL, BAD_USAGE );
105 /* OUTOFFILE must not be True when connected for sequential read */
106 if( !TEST_FLAG( the_access, IO_INDEXED )
107 && TEST_FLAG( the_access, IO_OUTOFFILE ) )
108 CHILLEXCEPTION( file, line, READFAIL, OUT_OF_FILE );
113 if( TEST_FLAG( the_access, IO_INDEXED ) )
115 /* index expression must be within bounds of index mode */
116 if( the_index < the_access->lowindex
117 || the_access->highindex < the_index )
118 CHILLEXCEPTION( file, line, RANGEFAIL, BAD_INDEX );
120 filepos = the_access->base +
121 (the_index - the_access->lowindex) * the_access->reclength;
123 if( lseek( the_access->association->handle, filepos, SEEK_SET ) == -1L )
124 CHILLEXCEPTION( file, line, READFAIL, LSEEK_FAILS );
127 /* establish store loc */
128 if( !(actaddr = the_buf_addr ))
130 /* if not yet allocated, do it now */
131 if (!the_access->store_loc)
132 if( !(the_access->store_loc = (char*)malloc( the_access->reclength ) ) )
133 CHILLEXCEPTION( file, line, SPACEFAIL, STORE_LOC_ALLOC );
134 actaddr = the_access->store_loc;
136 actlen = the_access->reclength;
138 if( (info = setjmp( __rw_exception )) )
139 CHILLEXCEPTION( file, line, info>>16, info & 0xffff );
141 if( TEST_FLAG( the_access, IO_TEXTIO ) )
143 readlen = actlen - 2;
144 if( TEST_FLAG( the_access, IO_INDEXED ) )
146 if( ! doRead( the_access, &reclen, sizeof(reclen) ) )
148 if( reclen > readlen )
149 CHILLEXCEPTION( file, line, RANGEFAIL, RECORD_TOO_LONG );
150 if( ! doRead( the_access, actaddr + 2, reclen ) )
151 CHILLEXCEPTION( file, line, READFAIL, RECORD_TOO_SHORT );
155 Association_Mode *assoc = the_access->association;
156 int handle = assoc->handle;
157 readbuf_t* rbuf = assoc->bufptr;
158 char* cptr = actaddr+2;
164 curr = bgetc( handle, rbuf );
170 SET_FLAG( the_access, IO_OUTOFFILE );
176 if( (curr = bgetc( handle, rbuf )) != '\n' )
178 bungetc( rbuf, curr );
179 CHILLEXCEPTION( file, line, RANGEFAIL, RECORD_TOO_LONG );
183 MOV2(actaddr,&reclen);
187 switch( the_access->rectype )
190 if( ! doRead( the_access, actaddr, actlen ) )
194 if( TEST_FLAG( the_access->association, IO_VARIABLE ) )
196 if( ! doRead( the_access, &reclen, sizeof(reclen) ) )
198 if( reclen > actlen - 2 )
199 CHILLEXCEPTION( file, line, RANGEFAIL, RECORD_TOO_LONG );
200 readlen = TEST_FLAG( the_access, IO_INDEXED ) ? actlen - 2 : reclen;
201 if( ! doRead( the_access, actaddr + 2, readlen ) )
202 CHILLEXCEPTION( file, line, READFAIL, RECORD_TOO_SHORT );
206 if( ! doRead( the_access, actaddr + 2, reclen = actlen - 2 ) )
207 CHILLEXCEPTION( file, line, READFAIL, RECORD_TOO_SHORT );
209 MOV2(actaddr,&reclen);