OSDN Git Service

- update comment to clarify that this file is specific to AIX and 32-bit mode.
[pf3gnuchains/gcc-fork.git] / libchill / readrecord.c
1 /* Implement Input/Output runtime actions for CHILL.
2    Copyright (C) 1992,1993 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 <unistd.h>
32
33 #include "fileio.h"
34
35 #ifdef EOF
36 #undef EOF
37 #endif
38 #define EOF -1
39
40 static
41 Boolean
42 doRead( Access_Mode* the_access, void* buf, size_t nbyte )
43 {
44   size_t nread;
45
46   nread = read( the_access->association->handle, buf, nbyte );
47   if( nread == nbyte )
48   {
49     CLR_FLAG( the_access, IO_OUTOFFILE );
50     return True;
51   }
52   if( nread == 0 )
53   {
54     SET_FLAG( the_access, IO_OUTOFFILE );
55     return False;
56   }
57   the_access->association->syserrno = errno;
58   RWEXCEPTION( READFAIL, OS_IO_ERROR );
59   /* no return */
60 }
61
62 static
63 int bgetc( int handle, readbuf_t* rbptr )
64 {
65   if( rbptr->cur >= rbptr->len )
66     {
67       rbptr->len = read( handle, rbptr->buf, READBUFLEN );
68       if( rbptr->len == 0 )
69         return EOF;
70       rbptr->cur = 0;
71     }
72   return rbptr->buf[rbptr->cur++];
73 }
74
75 static
76 void bungetc( readbuf_t* rbptr, int c )
77 {
78   rbptr->buf[--rbptr->cur] = c;
79 }
80
81 void*
82 __readrecord( Access_Mode*  the_access,
83               signed long   the_index,
84               char*         the_buf_addr,
85               char*         file,
86               int           line )
87 {
88   unsigned long  info;
89   char*          actaddr;
90   unsigned short actlen;
91   off_t          filepos;
92   unsigned short reclen;
93   unsigned long  readlen;
94
95   if( !the_access )
96     CHILLEXCEPTION( file, line, EMPTY, NULL_ACCESS );
97
98   if( !the_access->association )
99     CHILLEXCEPTION( file, line, NOTCONNECTED, IS_NOT_CONNECTED );
100
101   /* Usage must not be WriteOnly */
102   if( the_access->association->usage == WriteOnly )
103     CHILLEXCEPTION( file, line, READFAIL, BAD_USAGE );
104
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 );
109
110   /*
111    *  Positioning
112    */
113   if( TEST_FLAG( the_access, IO_INDEXED ) )
114   {
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 );
119
120     filepos = the_access->base + 
121               (the_index - the_access->lowindex) * the_access->reclength;
122
123     if( lseek( the_access->association->handle, filepos, SEEK_SET ) == -1L )
124       CHILLEXCEPTION( file, line, READFAIL, LSEEK_FAILS );
125   }
126
127   /* establish store loc */
128   if( !(actaddr = the_buf_addr ))
129   {
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;
135   }
136   actlen  = the_access->reclength;
137
138   if( (info = setjmp( __rw_exception )) )
139     CHILLEXCEPTION( file, line, info>>16, info & 0xffff );
140
141   if( TEST_FLAG( the_access, IO_TEXTIO ) )
142   {
143     readlen = actlen - 2;
144     if( TEST_FLAG( the_access, IO_INDEXED ) )
145     {
146       if( ! doRead( the_access, &reclen, sizeof(reclen) ) )
147         return NULL;
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 );
152     }
153     else
154     { 
155       Association_Mode *assoc = the_access->association;
156       int              handle = assoc->handle;
157       readbuf_t*       rbuf   = assoc->bufptr;
158       char* cptr = actaddr+2;
159       int   curr;
160
161       reclen = 0;
162       while( readlen-- )
163       {
164         curr = bgetc( handle, rbuf );
165         if( curr == '\n' )
166           goto end_of_line;
167         if( curr == EOF )
168         {
169           if( !reclen )
170             SET_FLAG( the_access, IO_OUTOFFILE );
171           goto end_of_line;
172         }
173         *cptr++ = curr;
174         reclen++;
175       }
176       if( (curr = bgetc( handle, rbuf )) != '\n' )
177         {
178           bungetc( rbuf, curr );
179           CHILLEXCEPTION( file, line, RANGEFAIL, RECORD_TOO_LONG );
180         }
181 end_of_line: ;
182     }
183     MOV2(actaddr,&reclen);
184   }
185   else
186   {
187     switch( the_access->rectype )
188     {
189     case Fixed:
190       if( ! doRead( the_access, actaddr, actlen ) )
191         return NULL;
192       break;
193     case VaryingChars:
194       if( TEST_FLAG( the_access->association, IO_VARIABLE ) )
195       {
196         if( ! doRead( the_access, &reclen, sizeof(reclen) ) )
197           return NULL;
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 );
203       }
204       else
205       {
206         if( ! doRead( the_access, actaddr + 2, reclen = actlen - 2 ) )
207           CHILLEXCEPTION( file, line, READFAIL, RECORD_TOO_SHORT );
208       }
209       MOV2(actaddr,&reclen);
210       break;
211     }
212   }
213
214   return actaddr;
215 }