1 /* Implement Input/Output runtime actions for CHILL.
2 Copyright (C) 1992, 1993, 1998 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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* As a special exception, if you link this library with other files,
23 some of which are compiled with GCC, to produce an executable,
24 this library does not by itself cause the resulting executable
25 to be covered by the GNU General Public License.
26 This exception does not however invalidate any other reasons why
27 the executable file might be covered by the GNU General Public License. */
32 #include <sys/types.h>
44 doRead( Access_Mode
* the_access
, void* buf
, size_t nbyte
)
48 nread
= read( the_access
->association
->handle
, buf
, nbyte
);
51 CLR_FLAG( the_access
, IO_OUTOFFILE
);
56 SET_FLAG( the_access
, IO_OUTOFFILE
);
59 the_access
->association
->syserrno
= errno
;
60 RWEXCEPTION( READFAIL
, OS_IO_ERROR
);
65 int bgetc( int handle
, readbuf_t
* rbptr
)
67 if( rbptr
->cur
>= rbptr
->len
)
69 rbptr
->len
= read( handle
, rbptr
->buf
, READBUFLEN
);
74 return rbptr
->buf
[rbptr
->cur
++];
78 void bungetc( readbuf_t
* rbptr
, int c
)
80 rbptr
->buf
[--rbptr
->cur
] = c
;
84 __readrecord( Access_Mode
* the_access
,
85 signed long the_index
,
92 unsigned short actlen
;
94 unsigned short reclen
;
95 unsigned long readlen
;
98 CHILLEXCEPTION( file
, line
, EMPTY
, NULL_ACCESS
);
100 if( !the_access
->association
)
101 CHILLEXCEPTION( file
, line
, NOTCONNECTED
, IS_NOT_CONNECTED
);
103 /* Usage must not be WriteOnly */
104 if( the_access
->association
->usage
== WriteOnly
)
105 CHILLEXCEPTION( file
, line
, READFAIL
, BAD_USAGE
);
107 /* OUTOFFILE must not be True when connected for sequential read */
108 if( !TEST_FLAG( the_access
, IO_INDEXED
)
109 && TEST_FLAG( the_access
, IO_OUTOFFILE
) )
110 CHILLEXCEPTION( file
, line
, READFAIL
, OUT_OF_FILE
);
115 if( TEST_FLAG( the_access
, IO_INDEXED
) )
117 /* index expression must be within bounds of index mode */
118 if( the_index
< the_access
->lowindex
119 || the_access
->highindex
< the_index
)
120 CHILLEXCEPTION( file
, line
, RANGEFAIL
, BAD_INDEX
);
122 filepos
= the_access
->base
+
123 (the_index
- the_access
->lowindex
) * the_access
->reclength
;
125 if( lseek( the_access
->association
->handle
, filepos
, SEEK_SET
) == -1L )
126 CHILLEXCEPTION( file
, line
, READFAIL
, LSEEK_FAILS
);
129 /* establish store loc */
130 if( !(actaddr
= the_buf_addr
))
132 /* if not yet allocated, do it now */
133 if (!the_access
->store_loc
)
134 if( !(the_access
->store_loc
= (char*)malloc( the_access
->reclength
) ) )
135 CHILLEXCEPTION( file
, line
, SPACEFAIL
, STORE_LOC_ALLOC
);
136 actaddr
= the_access
->store_loc
;
138 actlen
= the_access
->reclength
;
140 if( (info
= setjmp( __rw_exception
)) )
141 CHILLEXCEPTION( file
, line
, info
>>16, info
& 0xffff );
143 if( TEST_FLAG( the_access
, IO_TEXTIO
) )
145 readlen
= actlen
- 2;
146 if( TEST_FLAG( the_access
, IO_INDEXED
) )
148 if( ! doRead( the_access
, &reclen
, sizeof(reclen
) ) )
150 if( reclen
> readlen
)
151 CHILLEXCEPTION( file
, line
, RANGEFAIL
, RECORD_TOO_LONG
);
152 if( ! doRead( the_access
, actaddr
+ 2, reclen
) )
153 CHILLEXCEPTION( file
, line
, READFAIL
, RECORD_TOO_SHORT
);
157 Association_Mode
*assoc
= the_access
->association
;
158 int handle
= assoc
->handle
;
159 readbuf_t
* rbuf
= assoc
->bufptr
;
160 char* cptr
= actaddr
+2;
166 curr
= bgetc( handle
, rbuf
);
172 SET_FLAG( the_access
, IO_OUTOFFILE
);
178 if( (curr
= bgetc( handle
, rbuf
)) != '\n' )
180 bungetc( rbuf
, curr
);
181 CHILLEXCEPTION( file
, line
, RANGEFAIL
, RECORD_TOO_LONG
);
185 MOV2(actaddr
,&reclen
);
189 switch( the_access
->rectype
)
192 if( ! doRead( the_access
, actaddr
, actlen
) )
196 if( TEST_FLAG( the_access
->association
, IO_VARIABLE
) )
198 if( ! doRead( the_access
, &reclen
, sizeof(reclen
) ) )
200 if( reclen
> actlen
- 2 )
201 CHILLEXCEPTION( file
, line
, RANGEFAIL
, RECORD_TOO_LONG
);
202 readlen
= TEST_FLAG( the_access
, IO_INDEXED
) ? actlen
- 2 : reclen
;
203 if( ! doRead( the_access
, actaddr
+ 2, readlen
) )
204 CHILLEXCEPTION( file
, line
, READFAIL
, RECORD_TOO_SHORT
);
208 if( ! doRead( the_access
, actaddr
+ 2, reclen
= actlen
- 2 ) )
209 CHILLEXCEPTION( file
, line
, READFAIL
, RECORD_TOO_SHORT
);
211 MOV2(actaddr
,&reclen
);