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, 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. */
31 #include <sys/types.h>
43 doRead( Access_Mode
* the_access
, void* buf
, size_t nbyte
)
47 nread
= read( the_access
->association
->handle
, buf
, nbyte
);
50 CLR_FLAG( the_access
, IO_OUTOFFILE
);
55 SET_FLAG( the_access
, IO_OUTOFFILE
);
58 the_access
->association
->syserrno
= errno
;
59 RWEXCEPTION( READFAIL
, OS_IO_ERROR
);
64 int bgetc( int handle
, readbuf_t
* rbptr
)
66 if( rbptr
->cur
>= rbptr
->len
)
68 rbptr
->len
= read( handle
, rbptr
->buf
, READBUFLEN
);
73 return rbptr
->buf
[rbptr
->cur
++];
77 void bungetc( readbuf_t
* rbptr
, int c
)
79 rbptr
->buf
[--rbptr
->cur
] = c
;
83 __readrecord( Access_Mode
* the_access
,
84 signed long the_index
,
91 unsigned short actlen
;
93 unsigned short reclen
;
94 unsigned long readlen
;
97 CHILLEXCEPTION( file
, line
, EMPTY
, NULL_ACCESS
);
99 if( !the_access
->association
)
100 CHILLEXCEPTION( file
, line
, NOTCONNECTED
, IS_NOT_CONNECTED
);
102 /* Usage must not be WriteOnly */
103 if( the_access
->association
->usage
== WriteOnly
)
104 CHILLEXCEPTION( file
, line
, READFAIL
, BAD_USAGE
);
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
);
114 if( TEST_FLAG( the_access
, IO_INDEXED
) )
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
);
121 filepos
= the_access
->base
+
122 (the_index
- the_access
->lowindex
) * the_access
->reclength
;
124 if( lseek( the_access
->association
->handle
, filepos
, SEEK_SET
) == -1L )
125 CHILLEXCEPTION( file
, line
, READFAIL
, LSEEK_FAILS
);
128 /* establish store loc */
129 if( !(actaddr
= the_buf_addr
))
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
;
137 actlen
= the_access
->reclength
;
139 if( (info
= setjmp( __rw_exception
)) )
140 CHILLEXCEPTION( file
, line
, info
>>16, info
& 0xffff );
142 if( TEST_FLAG( the_access
, IO_TEXTIO
) )
144 readlen
= actlen
- 2;
145 if( TEST_FLAG( the_access
, IO_INDEXED
) )
147 if( ! doRead( the_access
, &reclen
, sizeof(reclen
) ) )
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
);
156 Association_Mode
*assoc
= the_access
->association
;
157 int handle
= assoc
->handle
;
158 readbuf_t
* rbuf
= assoc
->bufptr
;
159 char* cptr
= actaddr
+2;
165 curr
= bgetc( handle
, rbuf
);
171 SET_FLAG( the_access
, IO_OUTOFFILE
);
177 if( (curr
= bgetc( handle
, rbuf
)) != '\n' )
179 bungetc( rbuf
, curr
);
180 CHILLEXCEPTION( file
, line
, RANGEFAIL
, RECORD_TOO_LONG
);
184 MOV2(actaddr
,&reclen
);
188 switch( the_access
->rectype
)
191 if( ! doRead( the_access
, actaddr
, actlen
) )
195 if( TEST_FLAG( the_access
->association
, IO_VARIABLE
) )
197 if( ! doRead( the_access
, &reclen
, sizeof(reclen
) ) )
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
);
207 if( ! doRead( the_access
, actaddr
+ 2, reclen
= actlen
- 2 ) )
208 CHILLEXCEPTION( file
, line
, READFAIL
, RECORD_TOO_SHORT
);
210 MOV2(actaddr
,&reclen
);