1 /* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
2 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
4 Namelist transfer functions contributed by Paul Thomas
5 F2003 I/O support contributed by Jerry DeLisle
7 This file is part of the GNU Fortran 95 runtime library (libgfortran).
9 Libgfortran is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 Libgfortran is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 Under Section 7 of GPL version 3, you are granted additional
20 permissions described in the GCC Runtime Library Exception, version
21 3.1, as published by the Free Software Foundation.
23 You should have received a copy of the GNU General Public License and
24 a copy of the GCC Runtime Library Exception along with this program;
25 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
26 <http://www.gnu.org/licenses/>. */
29 /* transfer.c -- Top level handling of data transfer statements. */
41 /* Calling conventions: Data transfer statements are unlike other
42 library calls in that they extend over several calls.
44 The first call is always a call to st_read() or st_write(). These
45 subroutines return no status unless a namelist read or write is
46 being done, in which case there is the usual status. No further
47 calls are necessary in this case.
49 For other sorts of data transfer, there are zero or more data
50 transfer statement that depend on the format of the data transfer
56 transfer_character_wide
60 These subroutines do not return status.
62 The last call is a call to st_[read|write]_done(). While
63 something can easily go wrong with the initial st_read() or
64 st_write(), an error inhibits any data from actually being
67 extern void transfer_integer (st_parameter_dt
*, void *, int);
68 export_proto(transfer_integer
);
70 extern void transfer_real (st_parameter_dt
*, void *, int);
71 export_proto(transfer_real
);
73 extern void transfer_logical (st_parameter_dt
*, void *, int);
74 export_proto(transfer_logical
);
76 extern void transfer_character (st_parameter_dt
*, void *, int);
77 export_proto(transfer_character
);
79 extern void transfer_character_wide (st_parameter_dt
*, void *, int, int);
80 export_proto(transfer_character_wide
);
82 extern void transfer_complex (st_parameter_dt
*, void *, int);
83 export_proto(transfer_complex
);
85 extern void transfer_array (st_parameter_dt
*, gfc_array_char
*, int,
87 export_proto(transfer_array
);
89 static void us_read (st_parameter_dt
*, int);
90 static void us_write (st_parameter_dt
*, int);
91 static void next_record_r_unf (st_parameter_dt
*, int);
92 static void next_record_w_unf (st_parameter_dt
*, int);
94 static const st_option advance_opt
[] = {
101 static const st_option decimal_opt
[] = {
102 {"point", DECIMAL_POINT
},
103 {"comma", DECIMAL_COMMA
},
107 static const st_option round_opt
[] = {
109 {"down", ROUND_DOWN
},
110 {"zero", ROUND_ZERO
},
111 {"nearest", ROUND_NEAREST
},
112 {"compatible", ROUND_COMPATIBLE
},
113 {"processor_defined", ROUND_PROCDEFINED
},
118 static const st_option sign_opt
[] = {
120 {"suppress", SIGN_SS
},
121 {"processor_defined", SIGN_S
},
125 static const st_option blank_opt
[] = {
126 {"null", BLANK_NULL
},
127 {"zero", BLANK_ZERO
},
131 static const st_option delim_opt
[] = {
132 {"apostrophe", DELIM_APOSTROPHE
},
133 {"quote", DELIM_QUOTE
},
134 {"none", DELIM_NONE
},
138 static const st_option pad_opt
[] = {
145 { FORMATTED_SEQUENTIAL
, UNFORMATTED_SEQUENTIAL
,
146 FORMATTED_DIRECT
, UNFORMATTED_DIRECT
, FORMATTED_STREAM
, UNFORMATTED_STREAM
152 current_mode (st_parameter_dt
*dtp
)
156 m
= FORM_UNSPECIFIED
;
158 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
160 m
= dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
?
161 FORMATTED_DIRECT
: UNFORMATTED_DIRECT
;
163 else if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
165 m
= dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
?
166 FORMATTED_SEQUENTIAL
: UNFORMATTED_SEQUENTIAL
;
168 else if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_STREAM
)
170 m
= dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
?
171 FORMATTED_STREAM
: UNFORMATTED_STREAM
;
178 /* Mid level data transfer statements. */
180 /* When reading sequential formatted records we have a problem. We
181 don't know how long the line is until we read the trailing newline,
182 and we don't want to read too much. If we read too much, we might
183 have to do a physical seek backwards depending on how much data is
184 present, and devices like terminals aren't seekable and would cause
187 Given this, the solution is to read a byte at a time, stopping if
188 we hit the newline. For small allocations, we use a static buffer.
189 For larger allocations, we are forced to allocate memory on the
190 heap. Hopefully this won't happen very often. */
192 /* Read sequential file - internal unit */
195 read_sf_internal (st_parameter_dt
*dtp
, int * length
)
197 static char *empty_string
[0];
201 /* Zero size array gives internal unit len of 0. Nothing to read. */
202 if (dtp
->internal_unit_len
== 0
203 && dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
206 /* If we have seen an eor previously, return a length of 0. The
207 caller is responsible for correctly padding the input field. */
208 if (dtp
->u
.p
.sf_seen_eor
)
211 /* Just return something that isn't a NULL pointer, otherwise the
212 caller thinks an error occured. */
213 return (char*) empty_string
;
217 base
= mem_alloc_r (dtp
->u
.p
.current_unit
->s
, length
);
218 if (unlikely (lorig
> *length
))
224 dtp
->u
.p
.current_unit
->bytes_left
-= *length
;
226 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
227 dtp
->u
.p
.size_used
+= (GFC_IO_INT
) *length
;
233 /* Read sequential file - external unit */
236 read_sf (st_parameter_dt
*dtp
, int * length
)
238 static char *empty_string
[0];
240 int n
, lorig
, seen_comma
;
242 /* If we have seen an eor previously, return a length of 0. The
243 caller is responsible for correctly padding the input field. */
244 if (dtp
->u
.p
.sf_seen_eor
)
247 /* Just return something that isn't a NULL pointer, otherwise the
248 caller thinks an error occured. */
249 return (char*) empty_string
;
254 /* Read data into format buffer and scan through it. */
256 base
= p
= fbuf_read (dtp
->u
.p
.current_unit
, length
);
264 if (q
== '\n' || q
== '\r')
266 /* Unexpected end of line. Set the position. */
267 fbuf_seek (dtp
->u
.p
.current_unit
, n
+ 1 ,SEEK_CUR
);
268 dtp
->u
.p
.sf_seen_eor
= 1;
270 /* If we see an EOR during non-advancing I/O, we need to skip
271 the rest of the I/O statement. Set the corresponding flag. */
272 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
|| dtp
->u
.p
.seen_dollar
)
273 dtp
->u
.p
.eor_condition
= 1;
275 /* If we encounter a CR, it might be a CRLF. */
276 if (q
== '\r') /* Probably a CRLF */
278 /* See if there is an LF. Use fbuf_read rather then fbuf_getc so
279 the position is not advanced unless it really is an LF. */
281 p
= fbuf_read (dtp
->u
.p
.current_unit
, &readlen
);
282 if (*p
== '\n' && readlen
== 1)
284 dtp
->u
.p
.sf_seen_eor
= 2;
285 fbuf_seek (dtp
->u
.p
.current_unit
, 1 ,SEEK_CUR
);
289 /* Without padding, terminate the I/O statement without assigning
290 the value. With padding, the value still needs to be assigned,
291 so we can just continue with a short read. */
292 if (dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
294 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
301 /* Short circuit the read if a comma is found during numeric input.
302 The flag is set to zero during character reads so that commas in
303 strings are not ignored */
305 if (dtp
->u
.p
.sf_read_comma
== 1)
308 notify_std (&dtp
->common
, GFC_STD_GNU
,
309 "Comma in formatted numeric read.");
317 fbuf_seek (dtp
->u
.p
.current_unit
, n
+ seen_comma
, SEEK_CUR
);
319 /* A short read implies we hit EOF, unless we hit EOR, a comma, or
320 some other stuff. Set the relevant flags. */
321 if (lorig
> *length
&& !dtp
->u
.p
.sf_seen_eor
&& !seen_comma
)
325 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
)
327 if (dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
333 dtp
->u
.p
.eor_condition
= 1;
338 else if (dtp
->u
.p
.advance_status
== ADVANCE_NO
339 || dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
340 || dtp
->u
.p
.current_unit
->bytes_left
341 == dtp
->u
.p
.current_unit
->recl
)
350 dtp
->u
.p
.current_unit
->bytes_left
-= n
;
352 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
353 dtp
->u
.p
.size_used
+= (GFC_IO_INT
) n
;
359 /* Function for reading the next couple of bytes from the current
360 file, advancing the current position. We return FAILURE on end of record or
361 end of file. This function is only for formatted I/O, unformatted uses
364 If the read is short, then it is because the current record does not
365 have enough data to satisfy the read request and the file was
366 opened with PAD=YES. The caller must assume tailing spaces for
370 read_block_form (st_parameter_dt
*dtp
, int * nbytes
)
375 if (!is_stream_io (dtp
))
377 if (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) *nbytes
)
379 /* For preconnected units with default record length, set bytes left
380 to unit record length and proceed, otherwise error. */
381 if (dtp
->u
.p
.current_unit
->unit_number
== options
.stdin_unit
382 && dtp
->u
.p
.current_unit
->recl
== DEFAULT_RECL
)
383 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
386 if (unlikely (dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
387 && !is_internal_unit (dtp
))
389 /* Not enough data left. */
390 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
395 if (unlikely (dtp
->u
.p
.current_unit
->bytes_left
== 0
396 && !is_internal_unit(dtp
)))
402 *nbytes
= dtp
->u
.p
.current_unit
->bytes_left
;
406 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
&&
407 (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
||
408 dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_STREAM
))
410 if (is_internal_unit (dtp
))
411 source
= read_sf_internal (dtp
, nbytes
);
413 source
= read_sf (dtp
, nbytes
);
415 dtp
->u
.p
.current_unit
->strm_pos
+=
416 (gfc_offset
) (*nbytes
+ dtp
->u
.p
.sf_seen_eor
);
420 /* If we reach here, we can assume it's direct access. */
422 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) *nbytes
;
425 source
= fbuf_read (dtp
->u
.p
.current_unit
, nbytes
);
426 fbuf_seek (dtp
->u
.p
.current_unit
, *nbytes
, SEEK_CUR
);
428 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
429 dtp
->u
.p
.size_used
+= (GFC_IO_INT
) *nbytes
;
431 if (norig
!= *nbytes
)
433 /* Short read, this shouldn't happen. */
434 if (!dtp
->u
.p
.current_unit
->pad_status
== PAD_YES
)
436 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
441 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) *nbytes
;
447 /* Reads a block directly into application data space. This is for
448 unformatted files. */
451 read_block_direct (st_parameter_dt
*dtp
, void *buf
, size_t nbytes
)
453 ssize_t to_read_record
;
454 ssize_t have_read_record
;
455 ssize_t to_read_subrecord
;
456 ssize_t have_read_subrecord
;
459 if (is_stream_io (dtp
))
461 have_read_record
= sread (dtp
->u
.p
.current_unit
->s
, buf
,
463 if (unlikely (have_read_record
< 0))
465 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
469 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) have_read_record
;
471 if (unlikely ((ssize_t
) nbytes
!= have_read_record
))
473 /* Short read, e.g. if we hit EOF. For stream files,
474 we have to set the end-of-file condition. */
480 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
482 if (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) nbytes
)
485 to_read_record
= dtp
->u
.p
.current_unit
->bytes_left
;
486 nbytes
= to_read_record
;
491 to_read_record
= nbytes
;
494 dtp
->u
.p
.current_unit
->bytes_left
-= to_read_record
;
496 to_read_record
= sread (dtp
->u
.p
.current_unit
->s
, buf
, to_read_record
);
497 if (unlikely (to_read_record
< 0))
499 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
503 if (to_read_record
!= (ssize_t
) nbytes
)
505 /* Short read, e.g. if we hit EOF. Apparently, we read
506 more than was written to the last record. */
510 if (unlikely (short_record
))
512 generate_error (&dtp
->common
, LIBERROR_SHORT_RECORD
, NULL
);
517 /* Unformatted sequential. We loop over the subrecords, reading
518 until the request has been fulfilled or the record has run out
519 of continuation subrecords. */
521 /* Check whether we exceed the total record length. */
523 if (dtp
->u
.p
.current_unit
->flags
.has_recl
524 && ((gfc_offset
) nbytes
> dtp
->u
.p
.current_unit
->bytes_left
))
526 to_read_record
= dtp
->u
.p
.current_unit
->bytes_left
;
531 to_read_record
= nbytes
;
534 have_read_record
= 0;
538 if (dtp
->u
.p
.current_unit
->bytes_left_subrecord
539 < (gfc_offset
) to_read_record
)
541 to_read_subrecord
= dtp
->u
.p
.current_unit
->bytes_left_subrecord
;
542 to_read_record
-= to_read_subrecord
;
546 to_read_subrecord
= to_read_record
;
550 dtp
->u
.p
.current_unit
->bytes_left_subrecord
-= to_read_subrecord
;
552 have_read_subrecord
= sread (dtp
->u
.p
.current_unit
->s
,
553 buf
+ have_read_record
, to_read_subrecord
);
554 if (unlikely (have_read_subrecord
) < 0)
556 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
560 have_read_record
+= have_read_subrecord
;
562 if (unlikely (to_read_subrecord
!= have_read_subrecord
))
565 /* Short read, e.g. if we hit EOF. This means the record
566 structure has been corrupted, or the trailing record
567 marker would still be present. */
569 generate_error (&dtp
->common
, LIBERROR_CORRUPT_FILE
, NULL
);
573 if (to_read_record
> 0)
575 if (likely (dtp
->u
.p
.current_unit
->continued
))
577 next_record_r_unf (dtp
, 0);
582 /* Let's make sure the file position is correctly pre-positioned
583 for the next read statement. */
585 dtp
->u
.p
.current_unit
->current_record
= 0;
586 next_record_r_unf (dtp
, 0);
587 generate_error (&dtp
->common
, LIBERROR_SHORT_RECORD
, NULL
);
593 /* Normal exit, the read request has been fulfilled. */
598 dtp
->u
.p
.current_unit
->bytes_left
-= have_read_record
;
599 if (unlikely (short_record
))
601 generate_error (&dtp
->common
, LIBERROR_SHORT_RECORD
, NULL
);
608 /* Function for writing a block of bytes to the current file at the
609 current position, advancing the file pointer. We are given a length
610 and return a pointer to a buffer that the caller must (completely)
611 fill in. Returns NULL on error. */
614 write_block (st_parameter_dt
*dtp
, int length
)
618 if (!is_stream_io (dtp
))
620 if (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) length
)
622 /* For preconnected units with default record length, set bytes left
623 to unit record length and proceed, otherwise error. */
624 if (likely ((dtp
->u
.p
.current_unit
->unit_number
625 == options
.stdout_unit
626 || dtp
->u
.p
.current_unit
->unit_number
627 == options
.stderr_unit
)
628 && dtp
->u
.p
.current_unit
->recl
== DEFAULT_RECL
))
629 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
632 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
637 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) length
;
640 if (is_internal_unit (dtp
))
642 dest
= mem_alloc_w (dtp
->u
.p
.current_unit
->s
, &length
);
646 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
650 if (unlikely (dtp
->u
.p
.current_unit
->endfile
== AT_ENDFILE
))
651 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
655 dest
= fbuf_alloc (dtp
->u
.p
.current_unit
, length
);
658 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
663 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
664 dtp
->u
.p
.size_used
+= (GFC_IO_INT
) length
;
666 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) length
;
672 /* High level interface to swrite(), taking care of errors. This is only
673 called for unformatted files. There are three cases to consider:
674 Stream I/O, unformatted direct, unformatted sequential. */
677 write_buf (st_parameter_dt
*dtp
, void *buf
, size_t nbytes
)
680 ssize_t have_written
;
681 ssize_t to_write_subrecord
;
686 if (is_stream_io (dtp
))
688 have_written
= swrite (dtp
->u
.p
.current_unit
->s
, buf
, nbytes
);
689 if (unlikely (have_written
< 0))
691 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
695 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) have_written
;
700 /* Unformatted direct access. */
702 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
704 if (unlikely (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) nbytes
))
706 generate_error (&dtp
->common
, LIBERROR_DIRECT_EOR
, NULL
);
710 if (buf
== NULL
&& nbytes
== 0)
713 have_written
= swrite (dtp
->u
.p
.current_unit
->s
, buf
, nbytes
);
714 if (unlikely (have_written
< 0))
716 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
720 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) have_written
;
721 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) have_written
;
726 /* Unformatted sequential. */
730 if (dtp
->u
.p
.current_unit
->flags
.has_recl
731 && (gfc_offset
) nbytes
> dtp
->u
.p
.current_unit
->bytes_left
)
733 nbytes
= dtp
->u
.p
.current_unit
->bytes_left
;
745 (size_t) dtp
->u
.p
.current_unit
->bytes_left_subrecord
< nbytes
?
746 (size_t) dtp
->u
.p
.current_unit
->bytes_left_subrecord
: nbytes
;
748 dtp
->u
.p
.current_unit
->bytes_left_subrecord
-=
749 (gfc_offset
) to_write_subrecord
;
751 to_write_subrecord
= swrite (dtp
->u
.p
.current_unit
->s
,
752 buf
+ have_written
, to_write_subrecord
);
753 if (unlikely (to_write_subrecord
< 0))
755 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
759 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) to_write_subrecord
;
760 nbytes
-= to_write_subrecord
;
761 have_written
+= to_write_subrecord
;
766 next_record_w_unf (dtp
, 1);
769 dtp
->u
.p
.current_unit
->bytes_left
-= have_written
;
770 if (unlikely (short_record
))
772 generate_error (&dtp
->common
, LIBERROR_SHORT_RECORD
, NULL
);
779 /* Master function for unformatted reads. */
782 unformatted_read (st_parameter_dt
*dtp
, bt type
,
783 void *dest
, int kind
, size_t size
, size_t nelems
)
785 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
)
788 if (type
== BT_CHARACTER
)
789 size
*= GFC_SIZE_OF_CHAR_KIND(kind
);
790 read_block_direct (dtp
, dest
, size
* nelems
);
800 /* Handle wide chracters. */
801 if (type
== BT_CHARACTER
&& kind
!= 1)
807 /* Break up complex into its constituent reals. */
808 if (type
== BT_COMPLEX
)
814 /* By now, all complex variables have been split into their
815 constituent reals. */
817 for (i
= 0; i
< nelems
; i
++)
819 read_block_direct (dtp
, buffer
, size
);
820 reverse_memcpy (p
, buffer
, size
);
827 /* Master function for unformatted writes. NOTE: For kind=10 the size is 16
828 bytes on 64 bit machines. The unused bytes are not initialized and never
829 used, which can show an error with memory checking analyzers like
833 unformatted_write (st_parameter_dt
*dtp
, bt type
,
834 void *source
, int kind
, size_t size
, size_t nelems
)
836 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
)
839 size_t stride
= type
== BT_CHARACTER
?
840 size
* GFC_SIZE_OF_CHAR_KIND(kind
) : size
;
842 write_buf (dtp
, source
, stride
* nelems
);
852 /* Handle wide chracters. */
853 if (type
== BT_CHARACTER
&& kind
!= 1)
859 /* Break up complex into its constituent reals. */
860 if (type
== BT_COMPLEX
)
866 /* By now, all complex variables have been split into their
867 constituent reals. */
869 for (i
= 0; i
< nelems
; i
++)
871 reverse_memcpy(buffer
, p
, size
);
873 write_buf (dtp
, buffer
, size
);
879 /* Return a pointer to the name of a type. */
904 internal_error (NULL
, "type_name(): Bad type");
911 /* Write a constant string to the output.
912 This is complicated because the string can have doubled delimiters
913 in it. The length in the format node is the true length. */
916 write_constant_string (st_parameter_dt
*dtp
, const fnode
*f
)
918 char c
, delimiter
, *p
, *q
;
921 length
= f
->u
.string
.length
;
925 p
= write_block (dtp
, length
);
932 for (; length
> 0; length
--)
935 if (c
== delimiter
&& c
!= 'H' && c
!= 'h')
936 q
++; /* Skip the doubled delimiter. */
941 /* Given actual and expected types in a formatted data transfer, make
942 sure they agree. If not, an error message is generated. Returns
943 nonzero if something went wrong. */
946 require_type (st_parameter_dt
*dtp
, bt expected
, bt actual
, const fnode
*f
)
950 if (actual
== expected
)
953 /* Adjust item_count before emitting error message. */
954 sprintf (buffer
, "Expected %s for item %d in formatted transfer, got %s",
955 type_name (expected
), dtp
->u
.p
.item_count
- 1, type_name (actual
));
957 format_error (dtp
, f
, buffer
);
962 /* This function is in the main loop for a formatted data transfer
963 statement. It would be natural to implement this as a coroutine
964 with the user program, but C makes that awkward. We loop,
965 processing format elements. When we actually have to transfer
966 data instead of just setting flags, we return control to the user
967 program which calls a function that supplies the address and type
968 of the next element, then comes back here to process it. */
971 formatted_transfer_scalar_read (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
978 int consume_data_flag
;
980 /* Change a complex data item into a pair of reals. */
982 n
= (p
== NULL
) ? 0 : ((type
!= BT_COMPLEX
) ? 1 : 2);
983 if (type
== BT_COMPLEX
)
989 /* If there's an EOR condition, we simulate finalizing the transfer
991 if (dtp
->u
.p
.eor_condition
)
994 /* Set this flag so that commas in reads cause the read to complete before
995 the entire field has been read. The next read field will start right after
996 the comma in the stream. (Set to 0 for character reads). */
997 dtp
->u
.p
.sf_read_comma
=
998 dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
? 0 : 1;
1002 /* If reversion has occurred and there is another real data item,
1003 then we have to move to the next record. */
1004 if (dtp
->u
.p
.reversion_flag
&& n
> 0)
1006 dtp
->u
.p
.reversion_flag
= 0;
1007 next_record (dtp
, 0);
1010 consume_data_flag
= 1;
1011 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1014 f
= next_format (dtp
);
1017 /* No data descriptors left. */
1018 if (unlikely (n
> 0))
1019 generate_error (&dtp
->common
, LIBERROR_FORMAT
,
1020 "Insufficient data descriptors in format after reversion");
1026 bytes_used
= (int)(dtp
->u
.p
.current_unit
->recl
1027 - dtp
->u
.p
.current_unit
->bytes_left
);
1029 if (is_stream_io(dtp
))
1036 goto need_read_data
;
1037 if (require_type (dtp
, BT_INTEGER
, type
, f
))
1039 read_decimal (dtp
, f
, p
, kind
);
1044 goto need_read_data
;
1045 if (compile_options
.allow_std
< GFC_STD_GNU
1046 && require_type (dtp
, BT_INTEGER
, type
, f
))
1048 read_radix (dtp
, f
, p
, kind
, 2);
1053 goto need_read_data
;
1054 if (compile_options
.allow_std
< GFC_STD_GNU
1055 && require_type (dtp
, BT_INTEGER
, type
, f
))
1057 read_radix (dtp
, f
, p
, kind
, 8);
1062 goto need_read_data
;
1063 if (compile_options
.allow_std
< GFC_STD_GNU
1064 && require_type (dtp
, BT_INTEGER
, type
, f
))
1066 read_radix (dtp
, f
, p
, kind
, 16);
1071 goto need_read_data
;
1073 /* It is possible to have FMT_A with something not BT_CHARACTER such
1074 as when writing out hollerith strings, so check both type
1075 and kind before calling wide character routines. */
1076 if (type
== BT_CHARACTER
&& kind
== 4)
1077 read_a_char4 (dtp
, f
, p
, size
);
1079 read_a (dtp
, f
, p
, size
);
1084 goto need_read_data
;
1085 read_l (dtp
, f
, p
, kind
);
1090 goto need_read_data
;
1091 if (require_type (dtp
, BT_REAL
, type
, f
))
1093 read_f (dtp
, f
, p
, kind
);
1098 goto need_read_data
;
1099 if (require_type (dtp
, BT_REAL
, type
, f
))
1101 read_f (dtp
, f
, p
, kind
);
1106 goto need_read_data
;
1107 if (require_type (dtp
, BT_REAL
, type
, f
))
1109 read_f (dtp
, f
, p
, kind
);
1114 goto need_read_data
;
1115 if (require_type (dtp
, BT_REAL
, type
, f
))
1117 read_f (dtp
, f
, p
, kind
);
1122 goto need_read_data
;
1123 if (require_type (dtp
, BT_REAL
, type
, f
))
1125 read_f (dtp
, f
, p
, kind
);
1130 goto need_read_data
;
1134 read_decimal (dtp
, f
, p
, kind
);
1137 read_l (dtp
, f
, p
, kind
);
1141 read_a_char4 (dtp
, f
, p
, size
);
1143 read_a (dtp
, f
, p
, size
);
1146 read_f (dtp
, f
, p
, kind
);
1149 internal_error (&dtp
->common
, "formatted_transfer(): Bad type");
1154 consume_data_flag
= 0;
1155 format_error (dtp
, f
, "Constant string in input format");
1158 /* Format codes that don't transfer data. */
1161 consume_data_flag
= 0;
1162 dtp
->u
.p
.skips
+= f
->u
.n
;
1163 pos
= bytes_used
+ dtp
->u
.p
.skips
- 1;
1164 dtp
->u
.p
.pending_spaces
= pos
- dtp
->u
.p
.max_pos
+ 1;
1165 read_x (dtp
, f
->u
.n
);
1170 consume_data_flag
= 0;
1172 if (f
->format
== FMT_TL
)
1174 /* Handle the special case when no bytes have been used yet.
1175 Cannot go below zero. */
1176 if (bytes_used
== 0)
1178 dtp
->u
.p
.pending_spaces
-= f
->u
.n
;
1179 dtp
->u
.p
.skips
-= f
->u
.n
;
1180 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
< 0 ? 0 : dtp
->u
.p
.skips
;
1183 pos
= bytes_used
- f
->u
.n
;
1188 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1189 left tab limit. We do not check if the position has gone
1190 beyond the end of record because a subsequent tab could
1191 bring us back again. */
1192 pos
= pos
< 0 ? 0 : pos
;
1194 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
+ pos
- bytes_used
;
1195 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
1196 + pos
- dtp
->u
.p
.max_pos
;
1197 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
< 0
1198 ? 0 : dtp
->u
.p
.pending_spaces
;
1199 if (dtp
->u
.p
.skips
== 0)
1202 /* Adjust everything for end-of-record condition */
1203 if (dtp
->u
.p
.sf_seen_eor
&& !is_internal_unit (dtp
))
1205 dtp
->u
.p
.current_unit
->bytes_left
-= dtp
->u
.p
.sf_seen_eor
;
1206 dtp
->u
.p
.skips
-= dtp
->u
.p
.sf_seen_eor
;
1208 dtp
->u
.p
.sf_seen_eor
= 0;
1210 if (dtp
->u
.p
.skips
< 0)
1212 if (is_internal_unit (dtp
))
1213 sseek (dtp
->u
.p
.current_unit
->s
, dtp
->u
.p
.skips
, SEEK_CUR
);
1215 fbuf_seek (dtp
->u
.p
.current_unit
, dtp
->u
.p
.skips
, SEEK_CUR
);
1216 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) dtp
->u
.p
.skips
;
1217 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1220 read_x (dtp
, dtp
->u
.p
.skips
);
1224 consume_data_flag
= 0;
1225 dtp
->u
.p
.sign_status
= SIGN_S
;
1229 consume_data_flag
= 0;
1230 dtp
->u
.p
.sign_status
= SIGN_SS
;
1234 consume_data_flag
= 0;
1235 dtp
->u
.p
.sign_status
= SIGN_SP
;
1239 consume_data_flag
= 0 ;
1240 dtp
->u
.p
.blank_status
= BLANK_NULL
;
1244 consume_data_flag
= 0;
1245 dtp
->u
.p
.blank_status
= BLANK_ZERO
;
1249 consume_data_flag
= 0;
1250 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_COMMA
;
1254 consume_data_flag
= 0;
1255 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_POINT
;
1259 consume_data_flag
= 0;
1260 dtp
->u
.p
.current_unit
->round_status
= ROUND_COMPATIBLE
;
1264 consume_data_flag
= 0;
1265 dtp
->u
.p
.current_unit
->round_status
= ROUND_DOWN
;
1269 consume_data_flag
= 0;
1270 dtp
->u
.p
.current_unit
->round_status
= ROUND_NEAREST
;
1274 consume_data_flag
= 0;
1275 dtp
->u
.p
.current_unit
->round_status
= ROUND_PROCDEFINED
;
1279 consume_data_flag
= 0;
1280 dtp
->u
.p
.current_unit
->round_status
= ROUND_UP
;
1284 consume_data_flag
= 0;
1285 dtp
->u
.p
.current_unit
->round_status
= ROUND_ZERO
;
1289 consume_data_flag
= 0;
1290 dtp
->u
.p
.scale_factor
= f
->u
.k
;
1294 consume_data_flag
= 0;
1295 dtp
->u
.p
.seen_dollar
= 1;
1299 consume_data_flag
= 0;
1300 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1301 next_record (dtp
, 0);
1305 /* A colon descriptor causes us to exit this loop (in
1306 particular preventing another / descriptor from being
1307 processed) unless there is another data item to be
1309 consume_data_flag
= 0;
1315 internal_error (&dtp
->common
, "Bad format node");
1318 /* Adjust the item count and data pointer. */
1320 if ((consume_data_flag
> 0) && (n
> 0))
1323 p
= ((char *) p
) + size
;
1328 pos
= (int)(dtp
->u
.p
.current_unit
->recl
- dtp
->u
.p
.current_unit
->bytes_left
);
1329 dtp
->u
.p
.max_pos
= (dtp
->u
.p
.max_pos
> pos
) ? dtp
->u
.p
.max_pos
: pos
;
1334 /* Come here when we need a data descriptor but don't have one. We
1335 push the current format node back onto the input, then return and
1336 let the user program call us back with the data. */
1338 unget_format (dtp
, f
);
1343 formatted_transfer_scalar_write (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
1346 int pos
, bytes_used
;
1350 int consume_data_flag
;
1352 /* Change a complex data item into a pair of reals. */
1354 n
= (p
== NULL
) ? 0 : ((type
!= BT_COMPLEX
) ? 1 : 2);
1355 if (type
== BT_COMPLEX
)
1361 /* If there's an EOR condition, we simulate finalizing the transfer
1362 by doing nothing. */
1363 if (dtp
->u
.p
.eor_condition
)
1366 /* Set this flag so that commas in reads cause the read to complete before
1367 the entire field has been read. The next read field will start right after
1368 the comma in the stream. (Set to 0 for character reads). */
1369 dtp
->u
.p
.sf_read_comma
=
1370 dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
? 0 : 1;
1374 /* If reversion has occurred and there is another real data item,
1375 then we have to move to the next record. */
1376 if (dtp
->u
.p
.reversion_flag
&& n
> 0)
1378 dtp
->u
.p
.reversion_flag
= 0;
1379 next_record (dtp
, 0);
1382 consume_data_flag
= 1;
1383 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1386 f
= next_format (dtp
);
1389 /* No data descriptors left. */
1390 if (unlikely (n
> 0))
1391 generate_error (&dtp
->common
, LIBERROR_FORMAT
,
1392 "Insufficient data descriptors in format after reversion");
1396 /* Now discharge T, TR and X movements to the right. This is delayed
1397 until a data producing format to suppress trailing spaces. */
1400 if (dtp
->u
.p
.mode
== WRITING
&& dtp
->u
.p
.skips
!= 0
1401 && ((n
>0 && ( t
== FMT_I
|| t
== FMT_B
|| t
== FMT_O
1402 || t
== FMT_Z
|| t
== FMT_F
|| t
== FMT_E
1403 || t
== FMT_EN
|| t
== FMT_ES
|| t
== FMT_G
1404 || t
== FMT_L
|| t
== FMT_A
|| t
== FMT_D
))
1405 || t
== FMT_STRING
))
1407 if (dtp
->u
.p
.skips
> 0)
1410 write_x (dtp
, dtp
->u
.p
.skips
, dtp
->u
.p
.pending_spaces
);
1411 tmp
= (int)(dtp
->u
.p
.current_unit
->recl
1412 - dtp
->u
.p
.current_unit
->bytes_left
);
1414 dtp
->u
.p
.max_pos
> tmp
? dtp
->u
.p
.max_pos
: tmp
;
1416 if (dtp
->u
.p
.skips
< 0)
1418 if (is_internal_unit (dtp
))
1419 sseek (dtp
->u
.p
.current_unit
->s
, dtp
->u
.p
.skips
, SEEK_CUR
);
1421 fbuf_seek (dtp
->u
.p
.current_unit
, dtp
->u
.p
.skips
, SEEK_CUR
);
1422 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) dtp
->u
.p
.skips
;
1424 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1427 bytes_used
= (int)(dtp
->u
.p
.current_unit
->recl
1428 - dtp
->u
.p
.current_unit
->bytes_left
);
1430 if (is_stream_io(dtp
))
1438 if (require_type (dtp
, BT_INTEGER
, type
, f
))
1440 write_i (dtp
, f
, p
, kind
);
1446 if (compile_options
.allow_std
< GFC_STD_GNU
1447 && require_type (dtp
, BT_INTEGER
, type
, f
))
1449 write_b (dtp
, f
, p
, kind
);
1455 if (compile_options
.allow_std
< GFC_STD_GNU
1456 && require_type (dtp
, BT_INTEGER
, type
, f
))
1458 write_o (dtp
, f
, p
, kind
);
1464 if (compile_options
.allow_std
< GFC_STD_GNU
1465 && require_type (dtp
, BT_INTEGER
, type
, f
))
1467 write_z (dtp
, f
, p
, kind
);
1474 /* It is possible to have FMT_A with something not BT_CHARACTER such
1475 as when writing out hollerith strings, so check both type
1476 and kind before calling wide character routines. */
1477 if (type
== BT_CHARACTER
&& kind
== 4)
1478 write_a_char4 (dtp
, f
, p
, size
);
1480 write_a (dtp
, f
, p
, size
);
1486 write_l (dtp
, f
, p
, kind
);
1492 if (require_type (dtp
, BT_REAL
, type
, f
))
1494 write_d (dtp
, f
, p
, kind
);
1500 if (require_type (dtp
, BT_REAL
, type
, f
))
1502 write_e (dtp
, f
, p
, kind
);
1508 if (require_type (dtp
, BT_REAL
, type
, f
))
1510 write_en (dtp
, f
, p
, kind
);
1516 if (require_type (dtp
, BT_REAL
, type
, f
))
1518 write_es (dtp
, f
, p
, kind
);
1524 if (require_type (dtp
, BT_REAL
, type
, f
))
1526 write_f (dtp
, f
, p
, kind
);
1535 write_i (dtp
, f
, p
, kind
);
1538 write_l (dtp
, f
, p
, kind
);
1542 write_a_char4 (dtp
, f
, p
, size
);
1544 write_a (dtp
, f
, p
, size
);
1547 if (f
->u
.real
.w
== 0)
1548 write_real_g0 (dtp
, p
, kind
, f
->u
.real
.d
);
1550 write_d (dtp
, f
, p
, kind
);
1553 internal_error (&dtp
->common
,
1554 "formatted_transfer(): Bad type");
1559 consume_data_flag
= 0;
1560 write_constant_string (dtp
, f
);
1563 /* Format codes that don't transfer data. */
1566 consume_data_flag
= 0;
1568 dtp
->u
.p
.skips
+= f
->u
.n
;
1569 pos
= bytes_used
+ dtp
->u
.p
.skips
- 1;
1570 dtp
->u
.p
.pending_spaces
= pos
- dtp
->u
.p
.max_pos
+ 1;
1571 /* Writes occur just before the switch on f->format, above, so
1572 that trailing blanks are suppressed, unless we are doing a
1573 non-advancing write in which case we want to output the blanks
1575 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
)
1577 write_x (dtp
, dtp
->u
.p
.skips
, dtp
->u
.p
.pending_spaces
);
1578 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1584 consume_data_flag
= 0;
1586 if (f
->format
== FMT_TL
)
1589 /* Handle the special case when no bytes have been used yet.
1590 Cannot go below zero. */
1591 if (bytes_used
== 0)
1593 dtp
->u
.p
.pending_spaces
-= f
->u
.n
;
1594 dtp
->u
.p
.skips
-= f
->u
.n
;
1595 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
< 0 ? 0 : dtp
->u
.p
.skips
;
1598 pos
= bytes_used
- f
->u
.n
;
1601 pos
= f
->u
.n
- dtp
->u
.p
.pending_spaces
- 1;
1603 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1604 left tab limit. We do not check if the position has gone
1605 beyond the end of record because a subsequent tab could
1606 bring us back again. */
1607 pos
= pos
< 0 ? 0 : pos
;
1609 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
+ pos
- bytes_used
;
1610 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
1611 + pos
- dtp
->u
.p
.max_pos
;
1612 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
< 0
1613 ? 0 : dtp
->u
.p
.pending_spaces
;
1617 consume_data_flag
= 0;
1618 dtp
->u
.p
.sign_status
= SIGN_S
;
1622 consume_data_flag
= 0;
1623 dtp
->u
.p
.sign_status
= SIGN_SS
;
1627 consume_data_flag
= 0;
1628 dtp
->u
.p
.sign_status
= SIGN_SP
;
1632 consume_data_flag
= 0 ;
1633 dtp
->u
.p
.blank_status
= BLANK_NULL
;
1637 consume_data_flag
= 0;
1638 dtp
->u
.p
.blank_status
= BLANK_ZERO
;
1642 consume_data_flag
= 0;
1643 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_COMMA
;
1647 consume_data_flag
= 0;
1648 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_POINT
;
1652 consume_data_flag
= 0;
1653 dtp
->u
.p
.current_unit
->round_status
= ROUND_COMPATIBLE
;
1657 consume_data_flag
= 0;
1658 dtp
->u
.p
.current_unit
->round_status
= ROUND_DOWN
;
1662 consume_data_flag
= 0;
1663 dtp
->u
.p
.current_unit
->round_status
= ROUND_NEAREST
;
1667 consume_data_flag
= 0;
1668 dtp
->u
.p
.current_unit
->round_status
= ROUND_PROCDEFINED
;
1672 consume_data_flag
= 0;
1673 dtp
->u
.p
.current_unit
->round_status
= ROUND_UP
;
1677 consume_data_flag
= 0;
1678 dtp
->u
.p
.current_unit
->round_status
= ROUND_ZERO
;
1682 consume_data_flag
= 0;
1683 dtp
->u
.p
.scale_factor
= f
->u
.k
;
1687 consume_data_flag
= 0;
1688 dtp
->u
.p
.seen_dollar
= 1;
1692 consume_data_flag
= 0;
1693 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1694 next_record (dtp
, 0);
1698 /* A colon descriptor causes us to exit this loop (in
1699 particular preventing another / descriptor from being
1700 processed) unless there is another data item to be
1702 consume_data_flag
= 0;
1708 internal_error (&dtp
->common
, "Bad format node");
1711 /* Adjust the item count and data pointer. */
1713 if ((consume_data_flag
> 0) && (n
> 0))
1716 p
= ((char *) p
) + size
;
1719 pos
= (int)(dtp
->u
.p
.current_unit
->recl
- dtp
->u
.p
.current_unit
->bytes_left
);
1720 dtp
->u
.p
.max_pos
= (dtp
->u
.p
.max_pos
> pos
) ? dtp
->u
.p
.max_pos
: pos
;
1725 /* Come here when we need a data descriptor but don't have one. We
1726 push the current format node back onto the input, then return and
1727 let the user program call us back with the data. */
1729 unget_format (dtp
, f
);
1732 /* This function is first called from data_init_transfer to initiate the loop
1733 over each item in the format, transferring data as required. Subsequent
1734 calls to this function occur for each data item foound in the READ/WRITE
1735 statement. The item_count is incremented for each call. Since the first
1736 call is from data_transfer_init, the item_count is always one greater than
1737 the actual count number of the item being transferred. */
1740 formatted_transfer (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
1741 size_t size
, size_t nelems
)
1747 size_t stride
= type
== BT_CHARACTER
?
1748 size
* GFC_SIZE_OF_CHAR_KIND(kind
) : size
;
1749 if (dtp
->u
.p
.mode
== READING
)
1751 /* Big loop over all the elements. */
1752 for (elem
= 0; elem
< nelems
; elem
++)
1754 dtp
->u
.p
.item_count
++;
1755 formatted_transfer_scalar_read (dtp
, type
, tmp
+ stride
*elem
, kind
, size
);
1760 /* Big loop over all the elements. */
1761 for (elem
= 0; elem
< nelems
; elem
++)
1763 dtp
->u
.p
.item_count
++;
1764 formatted_transfer_scalar_write (dtp
, type
, tmp
+ stride
*elem
, kind
, size
);
1770 /* Data transfer entry points. The type of the data entity is
1771 implicit in the subroutine call. This prevents us from having to
1772 share a common enum with the compiler. */
1775 transfer_integer (st_parameter_dt
*dtp
, void *p
, int kind
)
1777 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1779 dtp
->u
.p
.transfer (dtp
, BT_INTEGER
, p
, kind
, kind
, 1);
1784 transfer_real (st_parameter_dt
*dtp
, void *p
, int kind
)
1787 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1789 size
= size_from_real_kind (kind
);
1790 dtp
->u
.p
.transfer (dtp
, BT_REAL
, p
, kind
, size
, 1);
1795 transfer_logical (st_parameter_dt
*dtp
, void *p
, int kind
)
1797 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1799 dtp
->u
.p
.transfer (dtp
, BT_LOGICAL
, p
, kind
, kind
, 1);
1804 transfer_character (st_parameter_dt
*dtp
, void *p
, int len
)
1806 static char *empty_string
[0];
1808 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1811 /* Strings of zero length can have p == NULL, which confuses the
1812 transfer routines into thinking we need more data elements. To avoid
1813 this, we give them a nice pointer. */
1814 if (len
== 0 && p
== NULL
)
1817 /* Set kind here to 1. */
1818 dtp
->u
.p
.transfer (dtp
, BT_CHARACTER
, p
, 1, len
, 1);
1822 transfer_character_wide (st_parameter_dt
*dtp
, void *p
, int len
, int kind
)
1824 static char *empty_string
[0];
1826 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1829 /* Strings of zero length can have p == NULL, which confuses the
1830 transfer routines into thinking we need more data elements. To avoid
1831 this, we give them a nice pointer. */
1832 if (len
== 0 && p
== NULL
)
1835 /* Here we pass the actual kind value. */
1836 dtp
->u
.p
.transfer (dtp
, BT_CHARACTER
, p
, kind
, len
, 1);
1841 transfer_complex (st_parameter_dt
*dtp
, void *p
, int kind
)
1844 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1846 size
= size_from_complex_kind (kind
);
1847 dtp
->u
.p
.transfer (dtp
, BT_COMPLEX
, p
, kind
, size
, 1);
1852 transfer_array (st_parameter_dt
*dtp
, gfc_array_char
*desc
, int kind
,
1853 gfc_charlen_type charlen
)
1855 index_type count
[GFC_MAX_DIMENSIONS
];
1856 index_type extent
[GFC_MAX_DIMENSIONS
];
1857 index_type stride
[GFC_MAX_DIMENSIONS
];
1858 index_type stride0
, rank
, size
, type
, n
;
1863 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1866 type
= GFC_DESCRIPTOR_TYPE (desc
);
1867 size
= GFC_DESCRIPTOR_SIZE (desc
);
1869 /* FIXME: What a kludge: Array descriptors and the IO library use
1870 different enums for types. */
1873 case GFC_DTYPE_UNKNOWN
:
1874 iotype
= BT_NULL
; /* Is this correct? */
1876 case GFC_DTYPE_INTEGER
:
1877 iotype
= BT_INTEGER
;
1879 case GFC_DTYPE_LOGICAL
:
1880 iotype
= BT_LOGICAL
;
1882 case GFC_DTYPE_REAL
:
1885 case GFC_DTYPE_COMPLEX
:
1886 iotype
= BT_COMPLEX
;
1888 case GFC_DTYPE_CHARACTER
:
1889 iotype
= BT_CHARACTER
;
1892 case GFC_DTYPE_DERIVED
:
1893 internal_error (&dtp
->common
,
1894 "Derived type I/O should have been handled via the frontend.");
1897 internal_error (&dtp
->common
, "transfer_array(): Bad type");
1900 rank
= GFC_DESCRIPTOR_RANK (desc
);
1901 for (n
= 0; n
< rank
; n
++)
1904 stride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(desc
,n
);
1905 extent
[n
] = GFC_DESCRIPTOR_EXTENT(desc
,n
);
1907 /* If the extent of even one dimension is zero, then the entire
1908 array section contains zero elements, so we return after writing
1909 a zero array record. */
1914 dtp
->u
.p
.transfer (dtp
, iotype
, data
, kind
, size
, tsize
);
1919 stride0
= stride
[0];
1921 /* If the innermost dimension has a stride of 1, we can do the transfer
1922 in contiguous chunks. */
1923 if (stride0
== size
)
1928 data
= GFC_DESCRIPTOR_DATA (desc
);
1932 dtp
->u
.p
.transfer (dtp
, iotype
, data
, kind
, size
, tsize
);
1933 data
+= stride0
* tsize
;
1936 while (count
[n
] == extent
[n
])
1939 data
-= stride
[n
] * extent
[n
];
1956 /* Preposition a sequential unformatted file while reading. */
1959 us_read (st_parameter_dt
*dtp
, int continued
)
1966 if (compile_options
.record_marker
== 0)
1967 n
= sizeof (GFC_INTEGER_4
);
1969 n
= compile_options
.record_marker
;
1971 nr
= sread (dtp
->u
.p
.current_unit
->s
, &i
, n
);
1972 if (unlikely (nr
< 0))
1974 generate_error (&dtp
->common
, LIBERROR_BAD_US
, NULL
);
1980 return; /* end of file */
1982 else if (unlikely (n
!= nr
))
1984 generate_error (&dtp
->common
, LIBERROR_BAD_US
, NULL
);
1988 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
1989 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
))
1993 case sizeof(GFC_INTEGER_4
):
1994 memcpy (&i4
, &i
, sizeof (i4
));
1998 case sizeof(GFC_INTEGER_8
):
1999 memcpy (&i8
, &i
, sizeof (i8
));
2004 runtime_error ("Illegal value for record marker");
2011 case sizeof(GFC_INTEGER_4
):
2012 reverse_memcpy (&i4
, &i
, sizeof (i4
));
2016 case sizeof(GFC_INTEGER_8
):
2017 reverse_memcpy (&i8
, &i
, sizeof (i8
));
2022 runtime_error ("Illegal value for record marker");
2028 dtp
->u
.p
.current_unit
->bytes_left_subrecord
= i
;
2029 dtp
->u
.p
.current_unit
->continued
= 0;
2033 dtp
->u
.p
.current_unit
->bytes_left_subrecord
= -i
;
2034 dtp
->u
.p
.current_unit
->continued
= 1;
2038 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2042 /* Preposition a sequential unformatted file while writing. This
2043 amount to writing a bogus length that will be filled in later. */
2046 us_write (st_parameter_dt
*dtp
, int continued
)
2053 if (compile_options
.record_marker
== 0)
2054 nbytes
= sizeof (GFC_INTEGER_4
);
2056 nbytes
= compile_options
.record_marker
;
2058 if (swrite (dtp
->u
.p
.current_unit
->s
, &dummy
, nbytes
) != nbytes
)
2059 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2061 /* For sequential unformatted, if RECL= was not specified in the OPEN
2062 we write until we have more bytes than can fit in the subrecord
2063 markers, then we write a new subrecord. */
2065 dtp
->u
.p
.current_unit
->bytes_left_subrecord
=
2066 dtp
->u
.p
.current_unit
->recl_subrecord
;
2067 dtp
->u
.p
.current_unit
->continued
= continued
;
2071 /* Position to the next record prior to transfer. We are assumed to
2072 be before the next record. We also calculate the bytes in the next
2076 pre_position (st_parameter_dt
*dtp
)
2078 if (dtp
->u
.p
.current_unit
->current_record
)
2079 return; /* Already positioned. */
2081 switch (current_mode (dtp
))
2083 case FORMATTED_STREAM
:
2084 case UNFORMATTED_STREAM
:
2085 /* There are no records with stream I/O. If the position was specified
2086 data_transfer_init has already positioned the file. If no position
2087 was specified, we continue from where we last left off. I.e.
2088 there is nothing to do here. */
2091 case UNFORMATTED_SEQUENTIAL
:
2092 if (dtp
->u
.p
.mode
== READING
)
2099 case FORMATTED_SEQUENTIAL
:
2100 case FORMATTED_DIRECT
:
2101 case UNFORMATTED_DIRECT
:
2102 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2106 dtp
->u
.p
.current_unit
->current_record
= 1;
2110 /* Initialize things for a data transfer. This code is common for
2111 both reading and writing. */
2114 data_transfer_init (st_parameter_dt
*dtp
, int read_flag
)
2116 unit_flags u_flags
; /* Used for creating a unit if needed. */
2117 GFC_INTEGER_4 cf
= dtp
->common
.flags
;
2118 namelist_info
*ionml
;
2120 ionml
= ((cf
& IOPARM_DT_IONML_SET
) != 0) ? dtp
->u
.p
.ionml
: NULL
;
2122 memset (&dtp
->u
.p
, 0, sizeof (dtp
->u
.p
));
2124 dtp
->u
.p
.ionml
= ionml
;
2125 dtp
->u
.p
.mode
= read_flag
? READING
: WRITING
;
2127 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
2130 if ((cf
& IOPARM_DT_HAS_SIZE
) != 0)
2131 dtp
->u
.p
.size_used
= 0; /* Initialize the count. */
2133 dtp
->u
.p
.current_unit
= get_unit (dtp
, 1);
2134 if (dtp
->u
.p
.current_unit
->s
== NULL
)
2135 { /* Open the unit with some default flags. */
2136 st_parameter_open opp
;
2139 if (dtp
->common
.unit
< 0)
2141 close_unit (dtp
->u
.p
.current_unit
);
2142 dtp
->u
.p
.current_unit
= NULL
;
2143 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2144 "Bad unit number in statement");
2147 memset (&u_flags
, '\0', sizeof (u_flags
));
2148 u_flags
.access
= ACCESS_SEQUENTIAL
;
2149 u_flags
.action
= ACTION_READWRITE
;
2151 /* Is it unformatted? */
2152 if (!(cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
2153 | IOPARM_DT_IONML_SET
)))
2154 u_flags
.form
= FORM_UNFORMATTED
;
2156 u_flags
.form
= FORM_UNSPECIFIED
;
2158 u_flags
.delim
= DELIM_UNSPECIFIED
;
2159 u_flags
.blank
= BLANK_UNSPECIFIED
;
2160 u_flags
.pad
= PAD_UNSPECIFIED
;
2161 u_flags
.decimal
= DECIMAL_UNSPECIFIED
;
2162 u_flags
.encoding
= ENCODING_UNSPECIFIED
;
2163 u_flags
.async
= ASYNC_UNSPECIFIED
;
2164 u_flags
.round
= ROUND_UNSPECIFIED
;
2165 u_flags
.sign
= SIGN_UNSPECIFIED
;
2167 u_flags
.status
= STATUS_UNKNOWN
;
2169 conv
= get_unformatted_convert (dtp
->common
.unit
);
2171 if (conv
== GFC_CONVERT_NONE
)
2172 conv
= compile_options
.convert
;
2174 /* We use big_endian, which is 0 on little-endian machines
2175 and 1 on big-endian machines. */
2178 case GFC_CONVERT_NATIVE
:
2179 case GFC_CONVERT_SWAP
:
2182 case GFC_CONVERT_BIG
:
2183 conv
= big_endian
? GFC_CONVERT_NATIVE
: GFC_CONVERT_SWAP
;
2186 case GFC_CONVERT_LITTLE
:
2187 conv
= big_endian
? GFC_CONVERT_SWAP
: GFC_CONVERT_NATIVE
;
2191 internal_error (&opp
.common
, "Illegal value for CONVERT");
2195 u_flags
.convert
= conv
;
2197 opp
.common
= dtp
->common
;
2198 opp
.common
.flags
&= IOPARM_COMMON_MASK
;
2199 dtp
->u
.p
.current_unit
= new_unit (&opp
, dtp
->u
.p
.current_unit
, &u_flags
);
2200 dtp
->common
.flags
&= ~IOPARM_COMMON_MASK
;
2201 dtp
->common
.flags
|= (opp
.common
.flags
& IOPARM_COMMON_MASK
);
2202 if (dtp
->u
.p
.current_unit
== NULL
)
2206 /* Check the action. */
2208 if (read_flag
&& dtp
->u
.p
.current_unit
->flags
.action
== ACTION_WRITE
)
2210 generate_error (&dtp
->common
, LIBERROR_BAD_ACTION
,
2211 "Cannot read from file opened for WRITE");
2215 if (!read_flag
&& dtp
->u
.p
.current_unit
->flags
.action
== ACTION_READ
)
2217 generate_error (&dtp
->common
, LIBERROR_BAD_ACTION
,
2218 "Cannot write to file opened for READ");
2222 dtp
->u
.p
.first_item
= 1;
2224 /* Check the format. */
2226 if ((cf
& IOPARM_DT_HAS_FORMAT
) != 0)
2229 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
2230 && (cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
))
2233 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2234 "Format present for UNFORMATTED data transfer");
2238 if ((cf
& IOPARM_DT_HAS_NAMELIST_NAME
) != 0 && dtp
->u
.p
.ionml
!= NULL
)
2240 if ((cf
& IOPARM_DT_HAS_FORMAT
) != 0)
2241 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2242 "A format cannot be specified with a namelist");
2244 else if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
&&
2245 !(cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
)))
2247 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2248 "Missing format for FORMATTED data transfer");
2251 if (is_internal_unit (dtp
)
2252 && dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
)
2254 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2255 "Internal file cannot be accessed by UNFORMATTED "
2260 /* Check the record or position number. */
2262 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
2263 && (cf
& IOPARM_DT_HAS_REC
) == 0)
2265 generate_error (&dtp
->common
, LIBERROR_MISSING_OPTION
,
2266 "Direct access data transfer requires record number");
2270 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
2271 && (cf
& IOPARM_DT_HAS_REC
) != 0)
2273 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2274 "Record number not allowed for sequential access "
2279 /* Process the ADVANCE option. */
2281 dtp
->u
.p
.advance_status
2282 = !(cf
& IOPARM_DT_HAS_ADVANCE
) ? ADVANCE_UNSPECIFIED
:
2283 find_option (&dtp
->common
, dtp
->advance
, dtp
->advance_len
, advance_opt
,
2284 "Bad ADVANCE parameter in data transfer statement");
2286 if (dtp
->u
.p
.advance_status
!= ADVANCE_UNSPECIFIED
)
2288 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
2290 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2291 "ADVANCE specification conflicts with sequential "
2296 if (is_internal_unit (dtp
))
2298 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2299 "ADVANCE specification conflicts with internal file");
2303 if ((cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
))
2304 != IOPARM_DT_HAS_FORMAT
)
2306 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2307 "ADVANCE specification requires an explicit format");
2314 dtp
->u
.p
.current_unit
->previous_nonadvancing_write
= 0;
2316 if ((cf
& IOPARM_EOR
) != 0 && dtp
->u
.p
.advance_status
!= ADVANCE_NO
)
2318 generate_error (&dtp
->common
, LIBERROR_MISSING_OPTION
,
2319 "EOR specification requires an ADVANCE specification "
2324 if ((cf
& IOPARM_DT_HAS_SIZE
) != 0
2325 && dtp
->u
.p
.advance_status
!= ADVANCE_NO
)
2327 generate_error (&dtp
->common
, LIBERROR_MISSING_OPTION
,
2328 "SIZE specification requires an ADVANCE "
2329 "specification of NO");
2334 { /* Write constraints. */
2335 if ((cf
& IOPARM_END
) != 0)
2337 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2338 "END specification cannot appear in a write "
2343 if ((cf
& IOPARM_EOR
) != 0)
2345 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2346 "EOR specification cannot appear in a write "
2351 if ((cf
& IOPARM_DT_HAS_SIZE
) != 0)
2353 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2354 "SIZE specification cannot appear in a write "
2360 if (dtp
->u
.p
.advance_status
== ADVANCE_UNSPECIFIED
)
2361 dtp
->u
.p
.advance_status
= ADVANCE_YES
;
2363 /* Check the decimal mode. */
2364 dtp
->u
.p
.current_unit
->decimal_status
2365 = !(cf
& IOPARM_DT_HAS_DECIMAL
) ? DECIMAL_UNSPECIFIED
:
2366 find_option (&dtp
->common
, dtp
->decimal
, dtp
->decimal_len
,
2367 decimal_opt
, "Bad DECIMAL parameter in data transfer "
2370 if (dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_UNSPECIFIED
)
2371 dtp
->u
.p
.current_unit
->decimal_status
= dtp
->u
.p
.current_unit
->flags
.decimal
;
2373 /* Check the round mode. */
2374 dtp
->u
.p
.current_unit
->round_status
2375 = !(cf
& IOPARM_DT_HAS_ROUND
) ? ROUND_UNSPECIFIED
:
2376 find_option (&dtp
->common
, dtp
->round
, dtp
->round_len
,
2377 round_opt
, "Bad ROUND parameter in data transfer "
2380 if (dtp
->u
.p
.current_unit
->round_status
== ROUND_UNSPECIFIED
)
2381 dtp
->u
.p
.current_unit
->round_status
= dtp
->u
.p
.current_unit
->flags
.round
;
2383 /* Check the sign mode. */
2384 dtp
->u
.p
.sign_status
2385 = !(cf
& IOPARM_DT_HAS_SIGN
) ? SIGN_UNSPECIFIED
:
2386 find_option (&dtp
->common
, dtp
->sign
, dtp
->sign_len
, sign_opt
,
2387 "Bad SIGN parameter in data transfer statement");
2389 if (dtp
->u
.p
.sign_status
== SIGN_UNSPECIFIED
)
2390 dtp
->u
.p
.sign_status
= dtp
->u
.p
.current_unit
->flags
.sign
;
2392 /* Check the blank mode. */
2393 dtp
->u
.p
.blank_status
2394 = !(cf
& IOPARM_DT_HAS_BLANK
) ? BLANK_UNSPECIFIED
:
2395 find_option (&dtp
->common
, dtp
->blank
, dtp
->blank_len
,
2397 "Bad BLANK parameter in data transfer statement");
2399 if (dtp
->u
.p
.blank_status
== BLANK_UNSPECIFIED
)
2400 dtp
->u
.p
.blank_status
= dtp
->u
.p
.current_unit
->flags
.blank
;
2402 /* Check the delim mode. */
2403 dtp
->u
.p
.current_unit
->delim_status
2404 = !(cf
& IOPARM_DT_HAS_DELIM
) ? DELIM_UNSPECIFIED
:
2405 find_option (&dtp
->common
, dtp
->delim
, dtp
->delim_len
,
2406 delim_opt
, "Bad DELIM parameter in data transfer statement");
2408 if (dtp
->u
.p
.current_unit
->delim_status
== DELIM_UNSPECIFIED
)
2409 dtp
->u
.p
.current_unit
->delim_status
= dtp
->u
.p
.current_unit
->flags
.delim
;
2411 /* Check the pad mode. */
2412 dtp
->u
.p
.current_unit
->pad_status
2413 = !(cf
& IOPARM_DT_HAS_PAD
) ? PAD_UNSPECIFIED
:
2414 find_option (&dtp
->common
, dtp
->pad
, dtp
->pad_len
, pad_opt
,
2415 "Bad PAD parameter in data transfer statement");
2417 if (dtp
->u
.p
.current_unit
->pad_status
== PAD_UNSPECIFIED
)
2418 dtp
->u
.p
.current_unit
->pad_status
= dtp
->u
.p
.current_unit
->flags
.pad
;
2420 /* Check to see if we might be reading what we wrote before */
2422 if (dtp
->u
.p
.mode
!= dtp
->u
.p
.current_unit
->mode
2423 && !is_internal_unit (dtp
))
2425 int pos
= fbuf_reset (dtp
->u
.p
.current_unit
);
2427 sseek (dtp
->u
.p
.current_unit
->s
, pos
, SEEK_CUR
);
2428 sflush(dtp
->u
.p
.current_unit
->s
);
2431 /* Check the POS= specifier: that it is in range and that it is used with a
2432 unit that has been connected for STREAM access. F2003 9.5.1.10. */
2434 if (((cf
& IOPARM_DT_HAS_POS
) != 0))
2436 if (is_stream_io (dtp
))
2441 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2442 "POS=specifier must be positive");
2446 if (dtp
->pos
>= dtp
->u
.p
.current_unit
->maxrec
)
2448 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2449 "POS=specifier too large");
2453 dtp
->rec
= dtp
->pos
;
2455 if (dtp
->u
.p
.mode
== READING
)
2457 /* Reset the endfile flag; if we hit EOF during reading
2458 we'll set the flag and generate an error at that point
2459 rather than worrying about it here. */
2460 dtp
->u
.p
.current_unit
->endfile
= NO_ENDFILE
;
2463 if (dtp
->pos
!= dtp
->u
.p
.current_unit
->strm_pos
)
2465 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
2466 if (sseek (dtp
->u
.p
.current_unit
->s
, dtp
->pos
- 1, SEEK_SET
) < 0)
2468 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2471 dtp
->u
.p
.current_unit
->strm_pos
= dtp
->pos
;
2476 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2477 "POS=specifier not allowed, "
2478 "Try OPEN with ACCESS='stream'");
2484 /* Sanity checks on the record number. */
2485 if ((cf
& IOPARM_DT_HAS_REC
) != 0)
2489 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2490 "Record number must be positive");
2494 if (dtp
->rec
>= dtp
->u
.p
.current_unit
->maxrec
)
2496 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2497 "Record number too large");
2501 /* Make sure format buffer is reset. */
2502 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
)
2503 fbuf_reset (dtp
->u
.p
.current_unit
);
2506 /* Check whether the record exists to be read. Only
2507 a partial record needs to exist. */
2509 if (dtp
->u
.p
.mode
== READING
&& (dtp
->rec
- 1)
2510 * dtp
->u
.p
.current_unit
->recl
>= file_length (dtp
->u
.p
.current_unit
->s
))
2512 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2513 "Non-existing record number");
2517 /* Position the file. */
2518 if (sseek (dtp
->u
.p
.current_unit
->s
, (gfc_offset
) (dtp
->rec
- 1)
2519 * dtp
->u
.p
.current_unit
->recl
, SEEK_SET
) < 0)
2521 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2525 /* TODO: This is required to maintain compatibility between
2526 4.3 and 4.4 runtime. Remove when ABI changes from 4.3 */
2528 if (is_stream_io (dtp
))
2529 dtp
->u
.p
.current_unit
->strm_pos
= dtp
->rec
;
2531 /* TODO: Un-comment this code when ABI changes from 4.3.
2532 if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM)
2534 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2535 "Record number not allowed for stream access "
2541 /* Bugware for badly written mixed C-Fortran I/O. */
2542 flush_if_preconnected(dtp
->u
.p
.current_unit
->s
);
2544 dtp
->u
.p
.current_unit
->mode
= dtp
->u
.p
.mode
;
2546 /* Set the maximum position reached from the previous I/O operation. This
2547 could be greater than zero from a previous non-advancing write. */
2548 dtp
->u
.p
.max_pos
= dtp
->u
.p
.current_unit
->saved_pos
;
2553 /* Set up the subroutine that will handle the transfers. */
2557 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
)
2558 dtp
->u
.p
.transfer
= unformatted_read
;
2561 if ((cf
& IOPARM_DT_LIST_FORMAT
) != 0)
2562 dtp
->u
.p
.transfer
= list_formatted_read
;
2564 dtp
->u
.p
.transfer
= formatted_transfer
;
2569 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
)
2570 dtp
->u
.p
.transfer
= unformatted_write
;
2573 if ((cf
& IOPARM_DT_LIST_FORMAT
) != 0)
2574 dtp
->u
.p
.transfer
= list_formatted_write
;
2576 dtp
->u
.p
.transfer
= formatted_transfer
;
2580 /* Make sure that we don't do a read after a nonadvancing write. */
2584 if (dtp
->u
.p
.current_unit
->read_bad
&& !is_stream_io (dtp
))
2586 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2587 "Cannot READ after a nonadvancing WRITE");
2593 if (dtp
->u
.p
.advance_status
== ADVANCE_YES
&& !dtp
->u
.p
.seen_dollar
)
2594 dtp
->u
.p
.current_unit
->read_bad
= 1;
2597 /* Start the data transfer if we are doing a formatted transfer. */
2598 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
2599 && ((cf
& (IOPARM_DT_LIST_FORMAT
| IOPARM_DT_HAS_NAMELIST_NAME
)) == 0)
2600 && dtp
->u
.p
.ionml
== NULL
)
2601 formatted_transfer (dtp
, 0, NULL
, 0, 0, 1);
2604 /* Initialize an array_loop_spec given the array descriptor. The function
2605 returns the index of the last element of the array, and also returns
2606 starting record, where the first I/O goes to (necessary in case of
2607 negative strides). */
2610 init_loop_spec (gfc_array_char
*desc
, array_loop_spec
*ls
,
2611 gfc_offset
*start_record
)
2613 int rank
= GFC_DESCRIPTOR_RANK(desc
);
2622 for (i
=0; i
<rank
; i
++)
2624 ls
[i
].idx
= GFC_DESCRIPTOR_LBOUND(desc
,i
);
2625 ls
[i
].start
= GFC_DESCRIPTOR_LBOUND(desc
,i
);
2626 ls
[i
].end
= GFC_DESCRIPTOR_UBOUND(desc
,i
);
2627 ls
[i
].step
= GFC_DESCRIPTOR_STRIDE(desc
,i
);
2628 empty
= empty
|| (GFC_DESCRIPTOR_UBOUND(desc
,i
)
2629 < GFC_DESCRIPTOR_LBOUND(desc
,i
));
2631 if (GFC_DESCRIPTOR_STRIDE(desc
,i
) > 0)
2633 index
+= (GFC_DESCRIPTOR_EXTENT(desc
,i
) - 1)
2634 * GFC_DESCRIPTOR_STRIDE(desc
,i
);
2638 index
-= (GFC_DESCRIPTOR_EXTENT(desc
,i
) - 1)
2639 * GFC_DESCRIPTOR_STRIDE(desc
,i
);
2640 *start_record
-= (GFC_DESCRIPTOR_EXTENT(desc
,i
) - 1)
2641 * GFC_DESCRIPTOR_STRIDE(desc
,i
);
2651 /* Determine the index to the next record in an internal unit array by
2652 by incrementing through the array_loop_spec. */
2655 next_array_record (st_parameter_dt
*dtp
, array_loop_spec
*ls
, int *finished
)
2663 for (i
= 0; i
< dtp
->u
.p
.current_unit
->rank
; i
++)
2668 if (ls
[i
].idx
> ls
[i
].end
)
2670 ls
[i
].idx
= ls
[i
].start
;
2676 index
= index
+ (ls
[i
].idx
- ls
[i
].start
) * ls
[i
].step
;
2686 /* Skip to the end of the current record, taking care of an optional
2687 record marker of size bytes. If the file is not seekable, we
2688 read chunks of size MAX_READ until we get to the right
2692 skip_record (st_parameter_dt
*dtp
, ssize_t bytes
)
2694 ssize_t rlength
, readb
;
2695 static const ssize_t MAX_READ
= 4096;
2698 dtp
->u
.p
.current_unit
->bytes_left_subrecord
+= bytes
;
2699 if (dtp
->u
.p
.current_unit
->bytes_left_subrecord
== 0)
2702 if (is_seekable (dtp
->u
.p
.current_unit
->s
))
2704 /* Direct access files do not generate END conditions,
2706 if (sseek (dtp
->u
.p
.current_unit
->s
,
2707 dtp
->u
.p
.current_unit
->bytes_left_subrecord
, SEEK_CUR
) < 0)
2708 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2710 dtp
->u
.p
.current_unit
->bytes_left_subrecord
= 0;
2713 { /* Seek by reading data. */
2714 while (dtp
->u
.p
.current_unit
->bytes_left_subrecord
> 0)
2717 (MAX_READ
< dtp
->u
.p
.current_unit
->bytes_left_subrecord
) ?
2718 MAX_READ
: dtp
->u
.p
.current_unit
->bytes_left_subrecord
;
2720 readb
= sread (dtp
->u
.p
.current_unit
->s
, p
, rlength
);
2723 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2727 dtp
->u
.p
.current_unit
->bytes_left_subrecord
-= readb
;
2734 /* Advance to the next record reading unformatted files, taking
2735 care of subrecords. If complete_record is nonzero, we loop
2736 until all subrecords are cleared. */
2739 next_record_r_unf (st_parameter_dt
*dtp
, int complete_record
)
2743 bytes
= compile_options
.record_marker
== 0 ?
2744 sizeof (GFC_INTEGER_4
) : compile_options
.record_marker
;
2749 /* Skip over tail */
2751 skip_record (dtp
, bytes
);
2753 if ( ! (complete_record
&& dtp
->u
.p
.current_unit
->continued
))
2761 static inline gfc_offset
2762 min_off (gfc_offset a
, gfc_offset b
)
2764 return (a
< b
? a
: b
);
2768 /* Space to the next record for read mode. */
2771 next_record_r (st_parameter_dt
*dtp
, int done
)
2778 switch (current_mode (dtp
))
2780 /* No records in unformatted STREAM I/O. */
2781 case UNFORMATTED_STREAM
:
2784 case UNFORMATTED_SEQUENTIAL
:
2785 next_record_r_unf (dtp
, 1);
2786 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2789 case FORMATTED_DIRECT
:
2790 case UNFORMATTED_DIRECT
:
2791 skip_record (dtp
, dtp
->u
.p
.current_unit
->bytes_left
);
2794 case FORMATTED_STREAM
:
2795 case FORMATTED_SEQUENTIAL
:
2796 /* read_sf has already terminated input because of an '\n', or
2798 if (dtp
->u
.p
.sf_seen_eor
)
2800 dtp
->u
.p
.sf_seen_eor
= 0;
2804 if (is_internal_unit (dtp
))
2806 if (is_array_io (dtp
))
2810 record
= next_array_record (dtp
, dtp
->u
.p
.current_unit
->ls
,
2812 if (!done
&& finished
)
2815 /* Now seek to this record. */
2816 record
= record
* dtp
->u
.p
.current_unit
->recl
;
2817 if (sseek (dtp
->u
.p
.current_unit
->s
, record
, SEEK_SET
) < 0)
2819 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
2822 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2826 bytes_left
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
2827 bytes_left
= min_off (bytes_left
,
2828 file_length (dtp
->u
.p
.current_unit
->s
)
2829 - stell (dtp
->u
.p
.current_unit
->s
));
2830 if (sseek (dtp
->u
.p
.current_unit
->s
,
2831 bytes_left
, SEEK_CUR
) < 0)
2833 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
2836 dtp
->u
.p
.current_unit
->bytes_left
2837 = dtp
->u
.p
.current_unit
->recl
;
2846 cc
= fbuf_getc (dtp
->u
.p
.current_unit
);
2850 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2853 if (is_stream_io (dtp
)
2854 || dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
2855 || dtp
->u
.p
.current_unit
->bytes_left
2856 == dtp
->u
.p
.current_unit
->recl
)
2862 if (is_stream_io (dtp
))
2863 dtp
->u
.p
.current_unit
->strm_pos
++;
2874 /* Small utility function to write a record marker, taking care of
2875 byte swapping and of choosing the correct size. */
2878 write_us_marker (st_parameter_dt
*dtp
, const gfc_offset buf
)
2883 char p
[sizeof (GFC_INTEGER_8
)];
2885 if (compile_options
.record_marker
== 0)
2886 len
= sizeof (GFC_INTEGER_4
);
2888 len
= compile_options
.record_marker
;
2890 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
2891 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
))
2895 case sizeof (GFC_INTEGER_4
):
2897 return swrite (dtp
->u
.p
.current_unit
->s
, &buf4
, len
);
2900 case sizeof (GFC_INTEGER_8
):
2902 return swrite (dtp
->u
.p
.current_unit
->s
, &buf8
, len
);
2906 runtime_error ("Illegal value for record marker");
2914 case sizeof (GFC_INTEGER_4
):
2916 reverse_memcpy (p
, &buf4
, sizeof (GFC_INTEGER_4
));
2917 return swrite (dtp
->u
.p
.current_unit
->s
, p
, len
);
2920 case sizeof (GFC_INTEGER_8
):
2922 reverse_memcpy (p
, &buf8
, sizeof (GFC_INTEGER_8
));
2923 return swrite (dtp
->u
.p
.current_unit
->s
, p
, len
);
2927 runtime_error ("Illegal value for record marker");
2934 /* Position to the next (sub)record in write mode for
2935 unformatted sequential files. */
2938 next_record_w_unf (st_parameter_dt
*dtp
, int next_subrecord
)
2940 gfc_offset m
, m_write
, record_marker
;
2942 /* Bytes written. */
2943 m
= dtp
->u
.p
.current_unit
->recl_subrecord
2944 - dtp
->u
.p
.current_unit
->bytes_left_subrecord
;
2946 /* Write the length tail. If we finish a record containing
2947 subrecords, we write out the negative length. */
2949 if (dtp
->u
.p
.current_unit
->continued
)
2954 if (unlikely (write_us_marker (dtp
, m_write
) < 0))
2957 if (compile_options
.record_marker
== 0)
2958 record_marker
= sizeof (GFC_INTEGER_4
);
2960 record_marker
= compile_options
.record_marker
;
2962 /* Seek to the head and overwrite the bogus length with the real
2965 if (unlikely (sseek (dtp
->u
.p
.current_unit
->s
, - m
- 2 * record_marker
,
2974 if (unlikely (write_us_marker (dtp
, m_write
) < 0))
2977 /* Seek past the end of the current record. */
2979 if (unlikely (sseek (dtp
->u
.p
.current_unit
->s
, m
+ record_marker
,
2986 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2992 /* Utility function like memset() but operating on streams. Return
2993 value is same as for POSIX write(). */
2996 sset (stream
* s
, int c
, ssize_t nbyte
)
2998 static const int WRITE_CHUNK
= 256;
2999 char p
[WRITE_CHUNK
];
3000 ssize_t bytes_left
, trans
;
3002 if (nbyte
< WRITE_CHUNK
)
3003 memset (p
, c
, nbyte
);
3005 memset (p
, c
, WRITE_CHUNK
);
3008 while (bytes_left
> 0)
3010 trans
= (bytes_left
< WRITE_CHUNK
) ? bytes_left
: WRITE_CHUNK
;
3011 trans
= swrite (s
, p
, trans
);
3014 bytes_left
-= trans
;
3017 return nbyte
- bytes_left
;
3020 /* Position to the next record in write mode. */
3023 next_record_w (st_parameter_dt
*dtp
, int done
)
3025 gfc_offset m
, record
, max_pos
;
3028 /* Zero counters for X- and T-editing. */
3029 max_pos
= dtp
->u
.p
.max_pos
;
3030 dtp
->u
.p
.max_pos
= dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
3032 switch (current_mode (dtp
))
3034 /* No records in unformatted STREAM I/O. */
3035 case UNFORMATTED_STREAM
:
3038 case FORMATTED_DIRECT
:
3039 if (dtp
->u
.p
.current_unit
->bytes_left
== 0)
3042 fbuf_seek (dtp
->u
.p
.current_unit
, 0, SEEK_END
);
3043 fbuf_flush (dtp
->u
.p
.current_unit
, WRITING
);
3044 if (sset (dtp
->u
.p
.current_unit
->s
, ' ',
3045 dtp
->u
.p
.current_unit
->bytes_left
)
3046 != dtp
->u
.p
.current_unit
->bytes_left
)
3051 case UNFORMATTED_DIRECT
:
3052 if (dtp
->u
.p
.current_unit
->bytes_left
> 0)
3054 length
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
3055 if (sset (dtp
->u
.p
.current_unit
->s
, 0, length
) != length
)
3060 case UNFORMATTED_SEQUENTIAL
:
3061 next_record_w_unf (dtp
, 0);
3062 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
3065 case FORMATTED_STREAM
:
3066 case FORMATTED_SEQUENTIAL
:
3068 if (is_internal_unit (dtp
))
3070 if (is_array_io (dtp
))
3074 length
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
3076 /* If the farthest position reached is greater than current
3077 position, adjust the position and set length to pad out
3078 whats left. Otherwise just pad whats left.
3079 (for character array unit) */
3080 m
= dtp
->u
.p
.current_unit
->recl
3081 - dtp
->u
.p
.current_unit
->bytes_left
;
3084 length
= (int) (max_pos
- m
);
3085 if (sseek (dtp
->u
.p
.current_unit
->s
,
3086 length
, SEEK_CUR
) < 0)
3088 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
3091 length
= (int) (dtp
->u
.p
.current_unit
->recl
- max_pos
);
3094 if (sset (dtp
->u
.p
.current_unit
->s
, ' ', length
) != length
)
3096 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
3100 /* Now that the current record has been padded out,
3101 determine where the next record in the array is. */
3102 record
= next_array_record (dtp
, dtp
->u
.p
.current_unit
->ls
,
3105 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
3107 /* Now seek to this record */
3108 record
= record
* dtp
->u
.p
.current_unit
->recl
;
3110 if (sseek (dtp
->u
.p
.current_unit
->s
, record
, SEEK_SET
) < 0)
3112 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
3116 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
3122 /* If this is the last call to next_record move to the farthest
3123 position reached and set length to pad out the remainder
3124 of the record. (for character scaler unit) */
3127 m
= dtp
->u
.p
.current_unit
->recl
3128 - dtp
->u
.p
.current_unit
->bytes_left
;
3131 length
= (int) (max_pos
- m
);
3132 if (sseek (dtp
->u
.p
.current_unit
->s
,
3133 length
, SEEK_CUR
) < 0)
3135 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
3138 length
= (int) (dtp
->u
.p
.current_unit
->recl
- max_pos
);
3141 length
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
3144 if (sset (dtp
->u
.p
.current_unit
->s
, ' ', length
) != length
)
3146 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
3158 fbuf_seek (dtp
->u
.p
.current_unit
, 0, SEEK_END
);
3159 char * p
= fbuf_alloc (dtp
->u
.p
.current_unit
, len
);
3166 if (is_stream_io (dtp
))
3168 dtp
->u
.p
.current_unit
->strm_pos
+= len
;
3169 if (dtp
->u
.p
.current_unit
->strm_pos
3170 < file_length (dtp
->u
.p
.current_unit
->s
))
3171 unit_truncate (dtp
->u
.p
.current_unit
,
3172 dtp
->u
.p
.current_unit
->strm_pos
- 1,
3180 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
3185 /* Position to the next record, which means moving to the end of the
3186 current record. This can happen under several different
3187 conditions. If the done flag is not set, we get ready to process
3191 next_record (st_parameter_dt
*dtp
, int done
)
3193 gfc_offset fp
; /* File position. */
3195 dtp
->u
.p
.current_unit
->read_bad
= 0;
3197 if (dtp
->u
.p
.mode
== READING
)
3198 next_record_r (dtp
, done
);
3200 next_record_w (dtp
, done
);
3202 if (!is_stream_io (dtp
))
3204 /* Keep position up to date for INQUIRE */
3206 update_position (dtp
->u
.p
.current_unit
);
3208 dtp
->u
.p
.current_unit
->current_record
= 0;
3209 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
3211 fp
= stell (dtp
->u
.p
.current_unit
->s
);
3212 /* Calculate next record, rounding up partial records. */
3213 dtp
->u
.p
.current_unit
->last_record
=
3214 (fp
+ dtp
->u
.p
.current_unit
->recl
- 1) /
3215 dtp
->u
.p
.current_unit
->recl
;
3218 dtp
->u
.p
.current_unit
->last_record
++;
3224 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
3228 /* Finalize the current data transfer. For a nonadvancing transfer,
3229 this means advancing to the next record. For internal units close the
3230 stream associated with the unit. */
3233 finalize_transfer (st_parameter_dt
*dtp
)
3236 GFC_INTEGER_4 cf
= dtp
->common
.flags
;
3238 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
3239 *dtp
->size
= dtp
->u
.p
.size_used
;
3241 if (dtp
->u
.p
.eor_condition
)
3243 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
3247 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
3249 if (dtp
->u
.p
.current_unit
&& current_mode (dtp
) == UNFORMATTED_SEQUENTIAL
)
3250 dtp
->u
.p
.current_unit
->current_record
= 0;
3254 if ((dtp
->u
.p
.ionml
!= NULL
)
3255 && (cf
& IOPARM_DT_HAS_NAMELIST_NAME
) != 0)
3257 if ((cf
& IOPARM_DT_NAMELIST_READ_MODE
) != 0)
3258 namelist_read (dtp
);
3260 namelist_write (dtp
);
3263 dtp
->u
.p
.transfer
= NULL
;
3264 if (dtp
->u
.p
.current_unit
== NULL
)
3267 dtp
->u
.p
.eof_jump
= &eof_jump
;
3268 if (setjmp (eof_jump
))
3270 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
3274 if ((cf
& IOPARM_DT_LIST_FORMAT
) != 0 && dtp
->u
.p
.mode
== READING
)
3276 finish_list_read (dtp
);
3280 if (dtp
->u
.p
.mode
== WRITING
)
3281 dtp
->u
.p
.current_unit
->previous_nonadvancing_write
3282 = dtp
->u
.p
.advance_status
== ADVANCE_NO
;
3284 if (is_stream_io (dtp
))
3286 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
3287 && dtp
->u
.p
.advance_status
!= ADVANCE_NO
)
3288 next_record (dtp
, 1);
3293 dtp
->u
.p
.current_unit
->current_record
= 0;
3295 if (!is_internal_unit (dtp
) && dtp
->u
.p
.seen_dollar
)
3297 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
3298 dtp
->u
.p
.seen_dollar
= 0;
3302 /* For non-advancing I/O, save the current maximum position for use in the
3303 next I/O operation if needed. */
3304 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
)
3306 int bytes_written
= (int) (dtp
->u
.p
.current_unit
->recl
3307 - dtp
->u
.p
.current_unit
->bytes_left
);
3308 dtp
->u
.p
.current_unit
->saved_pos
=
3309 dtp
->u
.p
.max_pos
> 0 ? dtp
->u
.p
.max_pos
- bytes_written
: 0;
3310 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
3313 else if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
3314 && dtp
->u
.p
.mode
== WRITING
&& !is_internal_unit (dtp
))
3315 fbuf_seek (dtp
->u
.p
.current_unit
, 0, SEEK_END
);
3317 dtp
->u
.p
.current_unit
->saved_pos
= 0;
3319 next_record (dtp
, 1);
3322 /* Transfer function for IOLENGTH. It doesn't actually do any
3323 data transfer, it just updates the length counter. */
3326 iolength_transfer (st_parameter_dt
*dtp
, bt type
__attribute__((unused
)),
3327 void *dest
__attribute__ ((unused
)),
3328 int kind
__attribute__((unused
)),
3329 size_t size
, size_t nelems
)
3331 if ((dtp
->common
.flags
& IOPARM_DT_HAS_IOLENGTH
) != 0)
3332 *dtp
->iolength
+= (GFC_IO_INT
) (size
* nelems
);
3336 /* Initialize the IOLENGTH data transfer. This function is in essence
3337 a very much simplified version of data_transfer_init(), because it
3338 doesn't have to deal with units at all. */
3341 iolength_transfer_init (st_parameter_dt
*dtp
)
3343 if ((dtp
->common
.flags
& IOPARM_DT_HAS_IOLENGTH
) != 0)
3346 memset (&dtp
->u
.p
, 0, sizeof (dtp
->u
.p
));
3348 /* Set up the subroutine that will handle the transfers. */
3350 dtp
->u
.p
.transfer
= iolength_transfer
;
3354 /* Library entry point for the IOLENGTH form of the INQUIRE
3355 statement. The IOLENGTH form requires no I/O to be performed, but
3356 it must still be a runtime library call so that we can determine
3357 the iolength for dynamic arrays and such. */
3359 extern void st_iolength (st_parameter_dt
*);
3360 export_proto(st_iolength
);
3363 st_iolength (st_parameter_dt
*dtp
)
3365 library_start (&dtp
->common
);
3366 iolength_transfer_init (dtp
);
3369 extern void st_iolength_done (st_parameter_dt
*);
3370 export_proto(st_iolength_done
);
3373 st_iolength_done (st_parameter_dt
*dtp
__attribute__((unused
)))
3380 /* The READ statement. */
3382 extern void st_read (st_parameter_dt
*);
3383 export_proto(st_read
);
3386 st_read (st_parameter_dt
*dtp
)
3388 library_start (&dtp
->common
);
3390 data_transfer_init (dtp
, 1);
3393 extern void st_read_done (st_parameter_dt
*);
3394 export_proto(st_read_done
);
3397 st_read_done (st_parameter_dt
*dtp
)
3399 finalize_transfer (dtp
);
3400 if (is_internal_unit (dtp
) || dtp
->u
.p
.format_not_saved
)
3401 free_format_data (dtp
->u
.p
.fmt
);
3403 if (dtp
->u
.p
.current_unit
!= NULL
)
3404 unlock_unit (dtp
->u
.p
.current_unit
);
3406 free_internal_unit (dtp
);
3411 extern void st_write (st_parameter_dt
*);
3412 export_proto(st_write
);
3415 st_write (st_parameter_dt
*dtp
)
3417 library_start (&dtp
->common
);
3418 data_transfer_init (dtp
, 0);
3421 extern void st_write_done (st_parameter_dt
*);
3422 export_proto(st_write_done
);
3425 st_write_done (st_parameter_dt
*dtp
)
3427 finalize_transfer (dtp
);
3429 /* Deal with endfile conditions associated with sequential files. */
3431 if (dtp
->u
.p
.current_unit
!= NULL
3432 && dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
3433 switch (dtp
->u
.p
.current_unit
->endfile
)
3435 case AT_ENDFILE
: /* Remain at the endfile record. */
3439 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
; /* Just at it now. */
3443 /* Get rid of whatever is after this record. */
3444 if (!is_internal_unit (dtp
))
3445 unit_truncate (dtp
->u
.p
.current_unit
,
3446 stell (dtp
->u
.p
.current_unit
->s
),
3448 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
3452 if (is_internal_unit (dtp
) || dtp
->u
.p
.format_not_saved
)
3453 free_format_data (dtp
->u
.p
.fmt
);
3455 if (dtp
->u
.p
.current_unit
!= NULL
)
3456 unlock_unit (dtp
->u
.p
.current_unit
);
3458 free_internal_unit (dtp
);
3464 /* F2003: This is a stub for the runtime portion of the WAIT statement. */
3466 st_wait (st_parameter_wait
*wtp
__attribute__((unused
)))
3471 /* Receives the scalar information for namelist objects and stores it
3472 in a linked list of namelist_info types. */
3474 extern void st_set_nml_var (st_parameter_dt
*dtp
, void *, char *,
3475 GFC_INTEGER_4
, gfc_charlen_type
, GFC_INTEGER_4
);
3476 export_proto(st_set_nml_var
);
3480 st_set_nml_var (st_parameter_dt
*dtp
, void * var_addr
, char * var_name
,
3481 GFC_INTEGER_4 len
, gfc_charlen_type string_length
,
3482 GFC_INTEGER_4 dtype
)
3484 namelist_info
*t1
= NULL
;
3486 size_t var_name_len
= strlen (var_name
);
3488 nml
= (namelist_info
*) get_mem (sizeof (namelist_info
));
3490 nml
->mem_pos
= var_addr
;
3492 nml
->var_name
= (char*) get_mem (var_name_len
+ 1);
3493 memcpy (nml
->var_name
, var_name
, var_name_len
);
3494 nml
->var_name
[var_name_len
] = '\0';
3496 nml
->len
= (int) len
;
3497 nml
->string_length
= (index_type
) string_length
;
3499 nml
->var_rank
= (int) (dtype
& GFC_DTYPE_RANK_MASK
);
3500 nml
->size
= (index_type
) (dtype
>> GFC_DTYPE_SIZE_SHIFT
);
3501 nml
->type
= (bt
) ((dtype
& GFC_DTYPE_TYPE_MASK
) >> GFC_DTYPE_TYPE_SHIFT
);
3503 if (nml
->var_rank
> 0)
3505 nml
->dim
= (descriptor_dimension
*)
3506 get_mem (nml
->var_rank
* sizeof (descriptor_dimension
));
3507 nml
->ls
= (array_loop_spec
*)
3508 get_mem (nml
->var_rank
* sizeof (array_loop_spec
));
3518 if ((dtp
->common
.flags
& IOPARM_DT_IONML_SET
) == 0)
3520 dtp
->common
.flags
|= IOPARM_DT_IONML_SET
;
3521 dtp
->u
.p
.ionml
= nml
;
3525 for (t1
= dtp
->u
.p
.ionml
; t1
->next
; t1
= t1
->next
);
3530 /* Store the dimensional information for the namelist object. */
3531 extern void st_set_nml_var_dim (st_parameter_dt
*, GFC_INTEGER_4
,
3532 index_type
, index_type
,
3534 export_proto(st_set_nml_var_dim
);
3537 st_set_nml_var_dim (st_parameter_dt
*dtp
, GFC_INTEGER_4 n_dim
,
3538 index_type stride
, index_type lbound
,
3541 namelist_info
* nml
;
3546 for (nml
= dtp
->u
.p
.ionml
; nml
->next
; nml
= nml
->next
);
3548 GFC_DIMENSION_SET(nml
->dim
[n
],lbound
,ubound
,stride
);
3551 /* Reverse memcpy - used for byte swapping. */
3553 void reverse_memcpy (void *dest
, const void *src
, size_t n
)
3559 s
= (char *) src
+ n
- 1;
3561 /* Write with ascending order - this is likely faster
3562 on modern architectures because of write combining. */
3568 /* Once upon a time, a poor innocent Fortran program was reading a
3569 file, when suddenly it hit the end-of-file (EOF). Unfortunately
3570 the OS doesn't tell whether we're at the EOF or whether we already
3571 went past it. Luckily our hero, libgfortran, keeps track of this.
3572 Call this function when you detect an EOF condition. See Section
3576 hit_eof (st_parameter_dt
* dtp
)
3578 dtp
->u
.p
.current_unit
->flags
.position
= POSITION_APPEND
;
3580 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
3581 switch (dtp
->u
.p
.current_unit
->endfile
)
3585 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
3586 if (!is_internal_unit (dtp
))
3588 dtp
->u
.p
.current_unit
->endfile
= AFTER_ENDFILE
;
3589 dtp
->u
.p
.current_unit
->current_record
= 0;
3592 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
3596 generate_error (&dtp
->common
, LIBERROR_ENDFILE
, NULL
);
3597 dtp
->u
.p
.current_unit
->current_record
= 0;
3602 /* Non-sequential files don't have an ENDFILE record, so we
3603 can't be at AFTER_ENDFILE. */
3604 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
3605 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
3606 dtp
->u
.p
.current_unit
->current_record
= 0;